chardev: avoid use-after-free when client disconnect
Quoted 32 lines · Wangjing(Hogan)
Quoted 31 lines
Quoted 31 lines · Marc-André Lureau
Quoted 18 lines
Hi
Quoted 11 lines · Hogan Wang via
IOWatchPoll object did not hold the @ioc and @src objects reference, then
io_watch_poll_prepareexecute in IO thread, if IOWatchPoll removed by main thread, thenio_watch_poll_prepareaccess @ioc or @src concurrently lead to coredump.In IO thread monitor scene, the IO thread used to accept client, receive qmp request and handle hung-up event. Main thread used to handle qmp request and send response, it will remove IOWatchPoll and free @ioc when send response fail, then cause use-after-free
Quoted 3 lines
I wonder if we are misusing GSources in that case, by removing sources from different threads.. Could you be more specific about the code path that leads to that?
It is permitted, but unfortunately every version of glib prior to 2.64 has a race condition that means you'll periodically get a use-after-free and a crash:
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1358
Libvirt worked around this problem by not calling '
g_source_unref'
directly, but instead have a helper that usesg_idle_addto delay the unref such that its guaranteed to happen inside the main event loop thread.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
So I'd like to know what version of glib Hogan is using
I am using glib2-2.62.5 in test environment, so it's looks like a glib2 known issue.
Hmm, actually the fix should have been backported into the 2.62.5 release according to this
According to the current source code:
Client connect to the monitor server, chr->gsource(IOWatchPoll) will be initialized by io_add_watch_poll.
According to io_watch_poll_prepare(Execute in IO Thread) function:IOWatchPoll->src will be added to chr->gsource as a child source while the channel readable, but IOWatchPoll not hold the child source's reference because of g_source_unref(iwp->src), so left the only one reference hold by the child source list.
If client disconnect,then main thread will destroy the IOWatchPoll and unref all the child source by g_source_destroy.
Call trace:
tcp_chr_read
tcp_chr_disconnect
tcp_chr_disconnect_locked
tcp_chr_free_connection
remove_fd_in_watch
g_source_destroy(chr->gsource) /* unref all the child source */
object_unref(OBJECT(s->ioc)); /* QIOChannel freed and IOWatchPoll->ioc is a wild pointer */
s->ioc = NULL;At this time, IOWatchPoll->src and IOWatchPoll->ioc freed, io_watch_poll_prepare called frequently and low-probability use-after-free.