Skip to contentSkip to conversation

ui/vnc: Fix problem with sending too many bytes as server name

PATCH In review Thomas Huth 3 participants 5 messages Nov 21, 2016 – Jan 4, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/vnc: Fix problem with sending too many bytes as server name

If the buffer is not big enough, snprintf() does not return the number of bytes that have been written to the buffer, but the number of bytes that would be needed for writing the whole string. By using this value for the following vnc_write() calls, we send some junk at the end of the name in case the qemu_name is longer than 1017 bytes, which could confuse the VNC clients. Fix this by adding an additional size check here.

---
 ui/vnc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
ui/vnc.c +6 -2
index 2c28a59..29aa9c4 100644
@@ -2459,10 +2459,14 @@ static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
 
     pixel_format_message(vs);
 
-    if (qemu_name)
+    if (qemu_name) {
         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
-    else
+        if (size > sizeof(buf)) {
+            size = sizeof(buf);
+        }
+    } else {
         size = snprintf(buf, sizeof(buf), "QEMU");
+    }
 
     vnc_write_u32(vs, size);
     vnc_write(vs, buf, size);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thomas Huth

If the buffer is not big enough, snprintf() does not return the number of bytes that have been written to the buffer, but the number of bytes that would be needed for writing the whole string. By using this value for the following vnc_write() calls, we send some junk at the end of the name in case the qemu_name is longer than 1017 bytes, which could confuse the VNC clients. Fix this by adding an additional size check here.

Quoted 5 lines
---
 ui/vnc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Worth having in 2.8, I think.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

If the buffer is not big enough, snprintf() does not return the number of bytes that have been written to the buffer, but the number of bytes that would be needed for writing the whole string. By using this value for the following vnc_write() calls, we send some junk at the end of the name in case the qemu_name is longer than 1017 bytes, which could confuse the VNC clients. Fix this by adding an additional size check here.

Use g_strdup_printf instead?

cheers,
Gerd

#3 lore raw
Quoted 10 lines · Gerd Hoffmann
Quoted 7 lines · Thomas Huth

If the buffer is not big enough, snprintf() does not return the number of bytes that have been written to the buffer, but the number of bytes that would be needed for writing the whole string. By using this value for the following vnc_write() calls, we send some junk at the end of the name in case the qemu_name is longer than 1017 bytes, which could confuse the VNC clients. Fix this by adding an additional size check here.

Use g_strdup_printf instead?

Not sure ... If I'd use g_strdup_printf instead here, we might end up with a string that is bigger than 1024 bytes here. Maybe there is/was a reason that the string is limited to 1024 bytes here? Are there clients that can not deal with more that 1024 bytes?
So unless you feel very confident that it does not matter, using the size check instead of g_strdup_printf sounds like the less invasive solution to me.

Thomas

#4 lore raw
In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

If the buffer is not big enough, snprintf() does not return the number of bytes that have been written to the buffer, but the number of bytes that would be needed for writing the whole string. By using this value for the following vnc_write() calls, we send some junk at the end of the name in case the qemu_name is longer than 1017 bytes, which could confuse the VNC clients. Fix this by adding an additional size check here.

Added to ui queue.

thanks,
Gerd