Skip to contentSkip to conversation

sockets: Fix socket_address_to_string() hostname truncation

PATCH In review Markus Armbruster 4 participants 4 messages Mar 23, 2017 – Apr 3, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sockets: Fix socket_address_to_string() hostname truncation

We first snprintf() to a fixed buffer, then g_strdup() the result *boggle*.

Worse, the size of the fixed buffer INET6_ADDRSTRLEN + 5 + 4 is bogus:
the 4 correctly accounts for '[', ']', ':' and '\0', but INET6_ADDRSTRLEN is not a suitable limit for inet->host, and 5 is not one for inet->port! They are for host and port in *numeric* form (exploiting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN), but inet->host can also be a hostname, and inet->port can be a service name, to be resolved with getaddrinfo().

Fortunately, the only user so far is the "socket" network backend's net_socket_connected(), which uses it to initialize a NetSocketState's info_str[]. info_str[] has considerable more space: 256 instead of
55. So the bug's impact appears to be limited to truncated "info networks" with the "socket" network backend.

The fix is obvious: use g_strdup_printf().

---
 util/qemu-sockets.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
util/qemu-sockets.c +2 -7
index 7c120c4..40164bf 100644
@@ -1307,19 +1307,14 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
 {
     char *buf;
     InetSocketAddress *inet;
-    char host_port[INET6_ADDRSTRLEN + 5 + 4];
 
     switch (addr->type) {
     case SOCKET_ADDRESS_KIND_INET:
         inet = addr->u.inet.data;
         if (strchr(inet->host, ':') == NULL) {
-            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);
+            buf = g_strdup_printf("%s:%s", inet->host, inet->port);
         } else {
-            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);
+            buf = g_strdup_printf("[%s]:%s", inet->host, inet->port);
         }
         break;
 
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Markus Armbruster

We first snprintf() to a fixed buffer, then g_strdup() the result *boggle*.

Worse, the size of the fixed buffer INET6_ADDRSTRLEN + 5 + 4 is bogus:
the 4 correctly accounts for '[', ']', ':' and '\0', but INET6_ADDRSTRLEN is not a suitable limit for inet->host, and 5 is not one for inet->port! They are for host and port in *numeric* form (exploiting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN), but inet->host can also be a hostname, and inet->port can be a service name, to be resolved with getaddrinfo().

Fortunately, the only user so far is the "socket" network backend's net_socket_connected(), which uses it to initialize a NetSocketState's info_str[]. info_str[] has considerable more space: 256 instead of
55. So the bug's impact appears to be limited to truncated "info networks" with the "socket" network backend.

The fix is obvious: use g_strdup_printf().

---
 util/qemu-sockets.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
util/qemu-sockets.c +0 -0
index 7c120c4..40164bf 100644

util/qemu-sockets.c char *socket_address_to_string(struct SocketAddress *addr, Error **errp) · line 1310

Quoted 4 lines
util/qemu-sockets.c +0 -1
@@ -1307,19 +1307,14 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
 {
     char *buf;
     InetSocketAddress *inet;
-    char host_port[INET6_ADDRSTRLEN + 5 + 4];

/me learnt about INET6_ADDRSTRLEN today.

Quoted 7 lines
util/qemu-sockets.c +0 -3
     switch (addr->type) {
     case SOCKET_ADDRESS_KIND_INET:
         inet = addr->u.inet.data;
         if (strchr(inet->host, ':') == NULL) {
-            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);

util/qemu-sockets.c char *socket_address_to_string(struct SocketAddress *addr, Error **errp)

Quoted 10 lines
util/qemu-sockets.c +2 -3
+            buf = g_strdup_printf("%s:%s", inet->host, inet->port);
         } else {
-            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);
+            buf = g_strdup_printf("[%s]:%s", inet->host, inet->port);
         }
         break;
 
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to Markus Armbruster
Quoted 17 lines · Markus Armbruster

We first snprintf() to a fixed buffer, then g_strdup() the result *boggle*.

Worse, the size of the fixed buffer INET6_ADDRSTRLEN + 5 + 4 is bogus:
the 4 correctly accounts for '[', ']', ':' and '\0', but INET6_ADDRSTRLEN is not a suitable limit for inet->host, and 5 is not one for inet->port! They are for host and port in *numeric* form (exploiting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN), but inet->host can also be a hostname, and inet->port can be a service name, to be resolved with getaddrinfo().

Fortunately, the only user so far is the "socket" network backend's net_socket_connected(), which uses it to initialize a NetSocketState's info_str[]. info_str[] has considerable more space: 256 instead of
55. So the bug's impact appears to be limited to truncated "info networks" with the "socket" network backend.

Quoted 6 lines

The fix is obvious: use g_strdup_printf().

---
 util/qemu-sockets.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
Reviewed-by on v1 1/1
#3 lore raw
In reply to Markus Armbruster
Quoted 17 lines · Markus Armbruster

We first snprintf() to a fixed buffer, then g_strdup() the result *boggle*.

Worse, the size of the fixed buffer INET6_ADDRSTRLEN + 5 + 4 is bogus:
the 4 correctly accounts for '[', ']', ':' and '\0', but INET6_ADDRSTRLEN is not a suitable limit for inet->host, and 5 is not one for inet->port! They are for host and port in *numeric* form (exploiting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN), but inet->host can also be a hostname, and inet->port can be a service name, to be resolved with getaddrinfo().

Fortunately, the only user so far is the "socket" network backend's net_socket_connected(), which uses it to initialize a NetSocketState's info_str[]. info_str[] has considerable more space: 256 instead of
55. So the bug's impact appears to be limited to truncated "info networks" with the "socket" network backend.

Quoted 3 lines

The fix is obvious: use g_strdup_printf().

Quoted 31 lines
---
 util/qemu-sockets.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
util/qemu-sockets.c +2 -7
index 7c120c4..40164bf 100644
@@ -1307,19 +1307,14 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
 {
     char *buf;
     InetSocketAddress *inet;
-    char host_port[INET6_ADDRSTRLEN + 5 + 4];
 
     switch (addr->type) {
     case SOCKET_ADDRESS_KIND_INET:
         inet = addr->u.inet.data;
         if (strchr(inet->host, ':') == NULL) {
-            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);
+            buf = g_strdup_printf("%s:%s", inet->host, inet->port);
         } else {
-            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
-                    inet->port);
-            buf = g_strdup(host_port);
+            buf = g_strdup_printf("[%s]:%s", inet->host, inet->port);
         }
         break;