Fixes to FD passing with QIOChannel
cover Fixes to FD passing with QIOChannel
Two small fixes for bugs with FD passing with QIOChannel discovered indirectly while testing the chardev unit tests. Added further unit tests to directly cover the FD passing code.
Daniel P. Berrange (2):
io: fix setting of QIO_CHANNEL_FEATURE_FD_PASS on server connections
io: fix stack allocation when sending of file descriptors
io/channel-socket.c | 17 ++++-- tests/test-io-channel-socket.c | 127 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 10 deletions(-)
Signature · 1 line
2.5.0
PATCH 1/2 io: fix setting of QIO_CHANNEL_FEATURE_FD_PASS on server connections
The QIO_CHANNEL_FEATURE_FD_PASS feature flag is set in the qio_channel_socket_set_fd() method, however, this only deals with client side connections.
To ensure server side connections also have the feature flag set, we must set it in qio_channel_socket_accept() too.
--- io/channel-socket.c | 10 ++++++++-- tests/test-io-channel-socket.c | 29 +++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-)
io/channel-socket.c +8 -2
@@ -352,13 +352,19 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, goto error; } -if (getsockname (cioc->fd, (struct sockaddr *)&ioc->localAddr, - &ioc->localAddrLen) <0 ) { +if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, + &cioc->localAddrLen) <0 ) {error_setg_errno (errp,socket_error (),"Unable to query local socket address" ); goto error; } +#ifndef WIN32 +if (cioc->localAddr.ss_family ==AF_UNIX ) { +QIO_CHANNEL (cioc)->features |= (1 <<QIO_CHANNEL_FEATURE_FD_PASS ); + } +#endif /* WIN32 */ +trace_qio_channel_socket_accept_complete (ioc, cioc, cioc->fd);return cioc;
tests/test-io-channel-socket.c +25 -4
@@ -210,13 +210,19 @@ static void test_io_channel_setup_async(SocketAddress *listen_addr,static void test_io_channel (bool async,SocketAddress *listen_addr, -SocketAddress *connect_addr) +SocketAddress *connect_addr, +bool passFD) {QIOChannel *src, *dst;QIOChannelTest *test;if (async) {test_io_channel_setup_async (listen_addr, connect_addr, &src, &dst); +g_assert (!passFD || +qio_channel_has_feature (src,QIO_CHANNEL_FEATURE_FD_PASS )); +g_assert (!passFD || +qio_channel_has_feature (dst,QIO_CHANNEL_FEATURE_FD_PASS )); + test =qio_channel_test_new ();qio_channel_test_run_threads (test, true, src, dst);qio_channel_test_validate (test);
@@ -226,6 +232,11 @@ static void test_io_channel(bool async,test_io_channel_setup_async (listen_addr, connect_addr, &src, &dst); +g_assert (!passFD || +qio_channel_has_feature (src,QIO_CHANNEL_FEATURE_FD_PASS )); +g_assert (!passFD || +qio_channel_has_feature (dst,QIO_CHANNEL_FEATURE_FD_PASS )); + test =qio_channel_test_new ();qio_channel_test_run_threads (test, false, src, dst);qio_channel_test_validate (test);
@@ -235,6 +246,11 @@ static void test_io_channel(bool async, }else {test_io_channel_setup_sync (listen_addr, connect_addr, &src, &dst); +g_assert (!passFD || +qio_channel_has_feature (src,QIO_CHANNEL_FEATURE_FD_PASS )); +g_assert (!passFD || +qio_channel_has_feature (dst,QIO_CHANNEL_FEATURE_FD_PASS )); + test =qio_channel_test_new ();qio_channel_test_run_threads (test, true, src, dst);qio_channel_test_validate (test);
@@ -244,6 +260,11 @@ static void test_io_channel(bool async,test_io_channel_setup_sync (listen_addr, connect_addr, &src, &dst); +g_assert (!passFD || +qio_channel_has_feature (src,QIO_CHANNEL_FEATURE_FD_PASS )); +g_assert (!passFD || +qio_channel_has_feature (dst,QIO_CHANNEL_FEATURE_FD_PASS )); + test =qio_channel_test_new ();qio_channel_test_run_threads (test, false, src, dst);qio_channel_test_validate (test);
@@ -269,7 +290,7 @@ static void test_io_channel_ipv4(bool async) connect_addr->u.inet->host =g_strdup ("127.0.0.1" ); connect_addr->u.inet->port =NULL ;/* Filled in later */ -test_io_channel (async, listen_addr, connect_addr); +test_io_channel (async, listen_addr, connect_addr, false);qapi_free_SocketAddress (listen_addr);qapi_free_SocketAddress (connect_addr);
@@ -303,7 +324,7 @@ static void test_io_channel_ipv6(bool async) connect_addr->u.inet->host =g_strdup ("::1" ); connect_addr->u.inet->port =NULL ;/* Filled in later */ -test_io_channel (async, listen_addr, connect_addr); +test_io_channel (async, listen_addr, connect_addr, false);qapi_free_SocketAddress (listen_addr);qapi_free_SocketAddress (connect_addr);
@@ -337,7 +358,7 @@ static void test_io_channel_unix(bool async) connect_addr->u.q_unix =g_new0 (UnixSocketAddress,1 ); connect_addr->u.q_unix->path =g_strdup (TEST_SOCKET ); -test_io_channel (async, listen_addr, connect_addr); +test_io_channel (async, listen_addr, connect_addr, true);qapi_free_SocketAddress (listen_addr);qapi_free_SocketAddress (connect_addr);
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 1/2] io: fix setting of QIO_CHANNEL_FEATURE_FD_PASS on server connections
Quoted 18 lines · Daniel P. Berrange
The
QIO_CHANNEL_FEATURE_FD_PASSfeature flag is set in theqio_channel_socket_set_fd()method, however, this only deals with client side connections.To ensure server side connections also have the feature flag set, we must set it in
qio_channel_socket_accept()too.Signed-off-by Daniel P. Berrange <[email protected]>--- io/channel-socket.c | 10 ++++++++-- tests/test-io-channel-socket.c | 29 +++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-)io/channel-socket.c +0
-0
io/channel-socket.c qio_channel_socket_accept(QIOChannelSocket *ioc, · line 356
Quoted 7 lines
io/channel-socket.c +2
-2@@ -352,13 +352,19 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, goto error; } -if (getsockname (cioc->fd, (struct sockaddr *)&ioc->localAddr, - &ioc->localAddrLen) <0 ) { +if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, + &cioc->localAddrLen) <0 ) {
Looks like a typo fix while at it.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 1/2] io: fix setting of QIO_CHANNEL_FEATURE_FD_PASS on server connections
Quoted 22 lines · Eric Blake
Quoted 21 lines · Daniel P. Berrange
The
QIO_CHANNEL_FEATURE_FD_PASSfeature flag is set in theqio_channel_socket_set_fd()method, however, this only deals with client side connections.To ensure server side connections also have the feature flag set, we must set it in
qio_channel_socket_accept()too.Signed-off-by Daniel P. Berrange <[email protected]>--- io/channel-socket.c | 10 ++++++++-- tests/test-io-channel-socket.c | 29 +++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-)io/channel-socket.c +0
-0@@ -352,13 +352,19 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, goto error; }
Quoted 6 lines
io/channel-socket.c qio_channel_socket_accept(QIOChannelSocket *ioc, · line 356
Quoted 4 lines
io/channel-socket.c +2
-2-if (getsockname (cioc->fd, (struct sockaddr *)&ioc->localAddr, - &ioc->localAddrLen) <0 ) { +if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, + &cioc->localAddrLen) <0 ) {Looks like a typo fix while at it.
Yes, the latter fix exposed the need for the former fix. I'll mention this in the commit message too
Quoted 1 line
Reviewed-by Eric Blake <[email protected]>
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
PATCH 2/2 io: fix stack allocation when sending of file descriptors
In reply to Daniel P. BerrangeWhen sending file descriptors over a socket, we have to allocate a data buffer to hold the FDs in the scmsghdr. Unfortunately we allocated the buffer on the stack inside an if () {} block, but called sendmsg() outside the block. So the stack bytes holding the FDs were liable to be overwritten with other data. By luck this was not a problem when sending 1 FD, but if sending 2 or more then it would fail.
The fix is to simply move the variables outside the nested 'if' block. To keep valgrind quiet we also zero-initialize the 'control' buffer.
--- io/channel-socket.c | 7 ++- tests/test-io-channel-socket.c | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-)
io/channel-socket.c +3 -4
@@ -493,15 +493,14 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc);ssize_t ret;struct msghdr msg = {NULL , }; +char control[CMSG_SPACE (sizeof (int ) *SOCKET_MAX_FDS )] = {0 }; +size_t fdsize =sizeof (int ) * nfds; +struct cmsghdr *cmsg; msg.msg_iov = (struct iovec *)iov; msg.msg_iovlen = niov;if (nfds) { -char control[CMSG_SPACE (sizeof (int ) *SOCKET_MAX_FDS )]; -size_t fdsize =sizeof (int ) * nfds; -struct cmsghdr *cmsg; -if (nfds >SOCKET_MAX_FDS ) { error_setg_errno(errp, -EINVAL, "Only %d FDs can be sent, got %zu",
tests/test-io-channel-socket.c +98 -0
110 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 2/2] io: fix stack allocation when sending of file descriptors
Quoted 13 lines · Daniel P. Berrange
When sending file descriptors over a socket, we have to allocate a data buffer to hold the FDs in the scmsghdr. Unfortunately we allocated the buffer on the stack inside an if () {} block, but called
sendmsg()outside the block. So the stack bytes holding the FDs were liable to be overwritten with other data. By luck this was not a problem when sending 1 FD, but if sending 2 or more then it would fail.The fix is to simply move the variables outside the nested 'if' block. To keep valgrind quiet we also zero-initialize the 'control' buffer.
Quoted 6 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- io/channel-socket.c | 7 ++- tests/test-io-channel-socket.c | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-)
The fix itself is obvious from the commit message; the bulk of this patch is the testsuite addition (which is a GOOD thing - thanks!).
tests/test-io-channel-socket.c static void test_io_channel_unix_async(void) · line 441
Quoted 12 lines
tests/test-io-channel-socket.c +12
-0+qio_channel_readv_full (dst, + iorecv, +G_N_ELEMENTS (iorecv), + &fdrecv, + &nfdrecv, + &error_abort); + +g_assert (nfdrecv ==G_N_ELEMENTS (fdsend)); +/* Each recvd FD should be different from sent FD */ +for (i =0 ; i < nfdrecv; i++) { + g_assert_cmpint(fdrecv[i], !=, testfd); + }
Here, you blindly dereference fdrecv[]...
tests/test-io-channel-socket.c static void test_io_channel_unix_async(void) · line 468
Quoted 3 lines
tests/test-io-channel-socket.c +3
-0+unlink (TEST_FILE ); +close (testfd); +if (fdrecv !=NULL ) {
...so this if() is dead, and you can just always do the cleanup.
That's minor, so:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 2/2] io: fix stack allocation when sending of file descriptors
Quoted 21 lines · Eric Blake
Quoted 13 lines · Daniel P. Berrange
When sending file descriptors over a socket, we have to allocate a data buffer to hold the FDs in the scmsghdr. Unfortunately we allocated the buffer on the stack inside an if () {} block, but called
sendmsg()outside the block. So the stack bytes holding the FDs were liable to be overwritten with other data. By luck this was not a problem when sending 1 FD, but if sending 2 or more then it would fail.The fix is to simply move the variables outside the nested 'if' block. To keep valgrind quiet we also zero-initialize the 'control' buffer.
Quoted 6 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- io/channel-socket.c | 7 ++- tests/test-io-channel-socket.c | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-)
Quoted 2 lines
The fix itself is obvious from the commit message; the bulk of this patch is the testsuite addition (which is a GOOD thing - thanks!).
Yes, I wasted lots of time trying to find the flaw before I wrote the test case at which point it was trivial to find with valgrind :-)
Quoted 14 lines
tests/test-io-channel-socket.c static void test_io_channel_unix_async(void) · line 441
Quoted 13 lines
tests/test-io-channel-socket.c +12
-0+qio_channel_readv_full (dst, + iorecv, +G_N_ELEMENTS (iorecv), + &fdrecv, + &nfdrecv, + &error_abort); + +g_assert (nfdrecv ==G_N_ELEMENTS (fdsend)); +/* Each recvd FD should be different from sent FD */ +for (i =0 ; i < nfdrecv; i++) { + g_assert_cmpint(fdrecv[i], !=, testfd); + }
Quoted 7 lines
Here, you blindly dereference fdrecv[]...
tests/test-io-channel-socket.c static void test_io_channel_unix_async(void) · line 468
Quoted 3 lines
tests/test-io-channel-socket.c +3
-0+unlink (TEST_FILE ); +close (testfd); +if (fdrecv !=NULL ) {...so this
if()is dead, and you can just always do the cleanup.
Yep, will fix
Quoted 2 lines
That's minor, so:
Reviewed-by Eric Blake <[email protected]>
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|