All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] qemu-sockets: fix unix socket path copy (again)
@ 2021-08-31 18:26 Michael Tokarev
  2021-08-31 19:21 ` Marc-André Lureau
  2021-08-31 19:47 ` Peter Maydell
  0 siblings, 2 replies; 11+ messages in thread
From: Michael Tokarev @ 2021-08-31 18:26 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marc-André Lureau, Michael Tokarev, Daniel P . Berrangé,
	qemu-stable

We test whenever the path of unix-domain socket
address is non-empty and strictly-less than
the length of the path buffer. Both these
conditions are wrong: the socket can be unnamed,
with empty path, or socket can have pathname
null-terminated _after_ the sun_path buffer,
since we provided more room when asking kernel.

So on one side, allow empty, unnamed sockets
(and adjust the test for abstract socket too -
only do that if the socket is not unnamed),
and on another side, allow path length to be
up to our own maximum, - we have up to size
of sockaddr_storage there.

While at it, fix the duplication of regular
pathname socket to not require trailing \0
(since it can be missing for unnamed sockets).

Fixes: 4cfd970ec188558daa6214f26203fe553fb1e01f (first in 6.1.0)
Fixes: http://bugs.debian.org/993145
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Cc: qemu-stable@nongnu.org
--
Two questions.
1. Why do we store the name of the socket to start with?
2. The code in the abstract socket case should not use
   g_strndup but g_memdup instead, since the whole thing
   is a blob of the given length, not a \0-terminated string.
---
 util/qemu-sockets.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index f2f3676d1f..7c83d81792 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -1345,13 +1345,20 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,
     SocketAddress *addr;
     struct sockaddr_un *su = (struct sockaddr_un *)sa;
 
-    assert(salen >= sizeof(su->sun_family) + 1 &&
-           salen <= sizeof(struct sockaddr_un));
+    /* there's a corner case when trailing \0 does not fit into
+     * sockaddr_un. Compare length with sizeof(sockaddr_storage),
+     * not with sizeof(sockaddr_un), since this is what we actually
+     * provide, to ensure we had no truncation and a room for
+     * the trailing \0 which we add below.
+     * When salen == sizeof(sun_family) it is unnamed socket,
+     * and when first byte of sun_path is \0, it is abstract. */
+    assert(salen >= sizeof(su->sun_family) &&
+           salen <= sizeof(struct sockaddr_storage));
 
     addr = g_new0(SocketAddress, 1);
     addr->type = SOCKET_ADDRESS_TYPE_UNIX;
 #ifdef CONFIG_LINUX
-    if (!su->sun_path[0]) {
+    if (salen > sizeof(su->sun_family) && !su->sun_path[0]) {
         /* Linux abstract socket */
         addr->u.q_unix.path = g_strndup(su->sun_path + 1,
                                         salen - sizeof(su->sun_family) - 1);
@@ -1363,7 +1370,7 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,
     }
 #endif
 
-    addr->u.q_unix.path = g_strndup(su->sun_path, sizeof(su->sun_path));
+    addr->u.q_unix.path = g_strndup(su->sun_path, salen - sizeof(su->sun_family));
     return addr;
 }
 #endif /* WIN32 */
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-09-01 12:14 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-31 18:26 [PATCH] qemu-sockets: fix unix socket path copy (again) Michael Tokarev
2021-08-31 19:21 ` Marc-André Lureau
2021-08-31 19:26   ` Michael Tokarev
2021-09-01  9:12   ` Daniel P. Berrangé
2021-09-01  9:20     ` Michael Tokarev
2021-08-31 19:47 ` Peter Maydell
2021-09-01  8:29   ` Michael Tokarev
2021-09-01  9:52     ` Peter Maydell
2021-09-01 11:45       ` Michael Tokarev
2021-09-01 11:58         ` Daniel P. Berrangé
2021-09-01 11:57     ` Daniel P. Berrangé

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.