Skip to contentSkip to conversation

vnc: avoid underflow when accessing user-provided address

PATCH In review Paolo Bonzini 3 participants 3 messages Mar 30, 2023 – Apr 21, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vnc: avoid underflow when accessing user-provided address

If hostlen is zero, there is a possibility that addrstr[hostlen - 1] underflows and, if a closing bracked is there, hostlen - 2 is passed to g_strndup() on the next line. If websocket==false then addrstr[0] would be a colon, but if websocket==true this could in principle happen.

Fix it by checking hostlen.

Reported by Coverity.

---
 ui/vnc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ui/vnc.c +1 -1
index bbd8b6baaeca..9d8a24dd8a69 100644
@@ -3751,7 +3751,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 && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
             inet->host = g_strndup(addrstr + 1, hostlen - 2);
         } else {
             inet->host = g_strndup(addrstr, hostlen);
Signature · 1 line
2.39.2
#1 lore raw
Quoted 9 lines · Paolo Bonzini

If hostlen is zero, there is a possibility that addrstr[hostlen - 1] underflows and, if a closing bracked is there, hostlen - 2 is passed to g_strndup() on the next line. If websocket==false then addrstr[0] would be a colon, but if websocket==true this could in principle happen.

Fix it by checking hostlen.

Reported by Coverity.

Also by Vladimir Sementsov-Ogievskiy few months ago:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 19 lines
---
  ui/vnc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ui/vnc.c +1 -1
index bbd8b6baaeca..9d8a24dd8a69 100644
@@ -3751,7 +3751,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 && addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
              inet->host = g_strndup(addrstr + 1, hostlen - 2);
          } else {
              inet->host = g_strndup(addrstr, hostlen);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

If hostlen is zero, there is a possibility that addrstr[hostlen - 1] underflows and, if a closing bracked is there, hostlen - 2 is passed to g_strndup() on the next line. If websocket==false then addrstr[0] would be a colon, but if websocket==true this could in principle happen.

Fix it by checking hostlen.

Reported by Coverity.

I've already sent a similar patch, yes, but let's finally merge any:)

Signature · 2 lines
Best regards,
Vladimir