Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-control: Fix use-after-free in on_receive #336

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

layercak3
Copy link
Contributor

If a client is closed between the time that one of its receive contexts is created and the time that the receive context is finished reading from its fd, when it is finished and finally calls nvnc_send_cut_text it will access ctx->data_control->server, but data_control is part of the wayvnc_client object which was freed when the client was closed.

The only purpose of the data_control pointer being in receive_context was to pass data_control->server to nvnc_send_cut_text, so receive_context can just store that server pointer itself.

This was the issue I found in any1/neatvnc#114 (comment)
Can be reproduced by having a VNC client open, then wl-copy < /some/obnoxiously/large/uncompressible/text on the server and immediately closing the VNC client after running that command.
I have read and understood CONTRIBUTING.md.

@any1
Copy link
Owner

any1 commented Sep 17, 2024

Thanks for tracking this down!

The aml_handle is responsible for cleaning up the context, and it shouldn't call on_receive after it's cleaned up, so this looks more like an aml bug.

@any1
Copy link
Owner

any1 commented Sep 17, 2024

Actually, the handler should be referenced by data_control and stopped and unreffed in data_control_destroy.

@layercak3
Copy link
Contributor Author

layercak3 commented Sep 17, 2024

Actually, the handler should be referenced by data_control and stopped and unreffed in data_control_destroy.

That makes sense. There can be multiple handlers running concurrently so they would need to be stored in some list/queue though.

I also think it that it makes sense that the receive context is decoupled from the client. I thought that was the intention when reading the code, and none of the things in receive_context are necessarily tied to the client (cleaned up when it's gone).

Edit: But I guess aml_stop would then need to be called for them when the server stops. So between server and client it makes more sense to just stop them when the client is destroyed if they are still running.

@layercak3
Copy link
Contributor Author

I just added the receive context/aml handler to a linked list so I can stop the receive contexts when closing the client.
I'll properly check this and test again in some hours.
I'm not familiar with aml and need to learn this but right now I think I need to aml_unref too (probably in on_receive too)

include/data-control.h Outdated Show resolved Hide resolved
src/data-control.c Outdated Show resolved Hide resolved
src/data-control.c Outdated Show resolved Hide resolved
@layercak3
Copy link
Contributor Author

I understand it better now. Thanks for your help and blog post.

@any1
Copy link
Owner

any1 commented Sep 18, 2024

blog post?

@layercak3
Copy link
Contributor Author

https://andri.yngvason.is/managing-object-lifetimes-in-c.html
It's pretty basic stuff but I don't have much experience :p

Previously the receive handlers were kept alive after the client was
closed. These can just be cancelled when the client is closed. This also
fixes a use-after-free because a reference to data_control (part of
wayvnc_client) is in the receive_context.
The data_control reference was only used to get to the server.
@any1
Copy link
Owner

any1 commented Sep 18, 2024

LGTM.

@any1 any1 merged commit 607bbaf into any1:master Sep 18, 2024
3 checks passed
@any1
Copy link
Owner

any1 commented Sep 18, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants