Skip to contentSkip to conversation

ui/vnc: fix bad address parsing

PATCH In review Vladimir Sementsov-Ogievskiy 2 participants 4 messages Dec 6, 2022 – Jan 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/vnc: fix bad address parsing

IF addrstr == "[" and websocket is true, hostlen becomes 0 and we try to access addrstr[hostlen-1] which is bad idea.

---
 ui/vnc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ui/vnc.c +1 -1
index 88f55cbf3c..8830bfe382 100644
@@ -3765,7 +3765,7 @@ static int vnc_display_get_address(const char *addrstr,
 
         addr->type = SOCKET_ADDRESS_TYPE_INET;
         inet = &addr->u.inet;
-        if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
+        if (hostlen >= 2 && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
             inet->host = g_strndup(addrstr + 1, hostlen - 2);
         } else {
             inet->host = g_strndup(addrstr, hostlen);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 13 lines · Vladimir Sementsov-Ogievskiy

IF addrstr == "[" and websocket is true, hostlen becomes 0 and we try to access addrstr[hostlen-1] which is bad idea.

---
  ui/vnc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ui/vnc.c +0 -0
index 88f55cbf3c..8830bfe382 100644

ui/vnc.c static int vnc_display_get_address(const char *addrstr, · line 3771

Quoted 8 lines
ui/vnc.c +1 -1
@@ -3765,7 +3765,7 @@ static int vnc_display_get_address(const char *addrstr,
 
          addr->type = SOCKET_ADDRESS_TYPE_INET;
          inet = &addr->u.inet;
-        if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
+        if (hostlen >= 2 && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
              inet->host = g_strndup(addrstr + 1, hostlen - 2);
          } else {
              inet->host = g_strndup(addrstr, hostlen);

If addrstr is "[" then inet->host ends up being "[" too now, right?

I was pretty sure we had a helper for that, but can't find any.

#2 lore raw
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Vladimir Sementsov-Ogievskiy

IF addrstr == "[" and websocket is true, hostlen becomes 0 and we try to access addrstr[hostlen-1] which is bad idea.

---
  ui/vnc.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
ui/vnc.c +0 -0
index 88f55cbf3c..8830bfe382 100644

ui/vnc.c static int vnc_display_get_address(const char *addrstr, · line 3770

Quoted 7 lines
ui/vnc.c +1 -1
@@ -3765,7 +3765,7 @@ static int vnc_display_get_address(const char *addrstr,
           addr->type = SOCKET_ADDRESS_TYPE_INET;
           inet = &addr->u.inet;
-        if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
+        if (hostlen >= 2 && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
               inet->host = g_strndup(addrstr + 1, hostlen - 2);
           } else {
               inet->host = g_strndup(addrstr, hostlen);

If addrstr is "[" then inet->host ends up being "[" too now, right?

Quoted 1 line

I was pretty sure we had a helper for that, but can't find any.

that's all a bit strange, let's add a bit of debugging:

ui/vnc.c +1 -0
index 88f55cbf3c..b1d463e67a 100644
@@ -3770,6 +3770,7 @@ static int vnc_display_get_address(const char *addrstr,
         } else {
             inet->host = g_strndup(addrstr, hostlen);
         }
+        printf("%s: websocket: %d, host: %s, port: %s\n", __func__, websocket, inet->host, port);
         /* plain VNC port is just an offset, for websocket
          * port is absolute */
         if (websocket) {

then:

./build/qemu-system-x86_64 -vnc [
qemu-system-x86_64: -vnc [: no vnc port specified

./build/qemu-system-x86_64 -vnc [,websocket
qemu-system-x86_64: -vnc [,websocket: warning: short-form boolean option 'websocket' deprecated Please use websocket=on instead
qemu-system-x86_64: -vnc [,websocket: no vnc port specified

./build/qemu-system-x86_64 -vnc [:0,websocket
qemu-system-x86_64: -vnc [:0,websocket: warning: short-form boolean option 'websocket' deprecated Please use websocket=on instead
vnc_display_get_address: websocket: 0, host: [, port: 0
vnc_display_get_address: websocket: 1, host: , port: on
qemu-system-x86_64: -vnc [:0,websocket: address resolution failed for [:5900: Name or service not known

./build/qemu-system-x86_64 -vnc [:0,websocket=on
vnc_display_get_address: websocket: 0, host: [, port: 0
vnc_display_get_address: websocket: 1, host: , port: on
qemu-system-x86_64: -vnc [:0,websocket=on: address resolution failed for [:5900: Name or service not known

so, "on" is treated as address string? (aha, that's OK, and it's parsed later in the code)

./build/qemu-system-x86_64 -vnc :0,websocket=[
vnc_display_get_address: websocket: 0, host: , port: 0
we are going to do bad thing!
vnc_display_get_address: websocket: 1, host: , port: [
qemu-system-x86_64: -vnc :0,websocket=[: address resolution failed for :[: Servname not supported for ai_socktype

Signature · 2 lines
Best regards,
Vladimir