Skip to contentSkip to conversation

ui/vnc: fix multiple OOB memory accesses

PATCH In review Daniel P. Berrangé 3 participants 7 messages May 21

lore
Share & tools
lore
#0 lore raw

cover ui/vnc: fix multiple OOB memory accesses

The following VNC patches are in response to recent disclosures to the qemu-security list.

Daniel P. Berrangé (4):
ui/vnc: fix OOB read access in VNC SASL mechname array ui/vnc: fix OOB write in VNC stats array ui/vnc: fix OOB write in lossy rect worker code ui/vnc: fix OOB read updating VNC update frequency stats

 ui/vnc-auth-sasl.c |  2 ++
 ui/vnc.c           | 14 ++++++++------
 ui/vnc.h           |  4 ++--
 3 files changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/4 ui/vnc: fix OOB read access in VNC SASL mechname array

When reading the SASL mechname array off the VNC connection, if malicious, the received data may contain embedded NULs. If this happens the memory buffer returned by g_strndup may be shorter than the original data. Unfortunately the code continued to index into this buffer with an offset equal to the original length. This is a potential OOB read of the array.

Fixes 5847d9e1 (ui/vnc: simplify and avoid strncpy)
---
 ui/vnc-auth-sasl.c | 2 ++
 1 file changed, 2 insertions(+)
ui/vnc-auth-sasl.c +2 -0
index 9964b969ac..298c8f3769 100644
@@ -489,6 +489,8 @@ static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_
     char *mechname = g_strndup((const char *) data, len);
     trace_vnc_auth_sasl_mech_choose(vs, mechname);
 
+    /* If 'data' had embedded NUL the dup'd string might now be shorter */
+    len = strlen(mechname);
     if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
         if (vs->sasl.mechlist[len] != '\0' &&
             vs->sasl.mechlist[len] != ',') {
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/4 ui/vnc: fix OOB write in VNC stats array

In reply to Daniel P. Berrangé

The VncSurface struct maintains update statistics in an array:

    VncRectStat stats[VNC_STAT_ROWS][VNC_STAT_COLS];

where the dimensions are defined as:

  #define VNC_STAT_RECT  64
  #define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT)
  #define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT)

If VNC_MAX_WIDTH / VNC_MAX_HEIGHT are not an exact multiple of VNC_STAT_REC, the COLS/ROWS will be undersized by 1.

Unfortunately:

  #define VNC_MAX_HEIGHT 2160

is not a multiple of 64, so there is potential for OOB reads and writes in the 'stats' array, if the guest surface is over 2112 pixels in height. An array overflow occurs when vnc_update_stats() records new statistics, either scribbling over data later in the VncDisplay struct that 'stats' is embedded in, or performing an OOB write on the allocated struct memory.

---
 ui/vnc.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/vnc.h +2 -2
index 0750bf5f72..c8d87cd530 100644
@@ -85,8 +85,8 @@ typedef void VncSendHextileTile(VncState *vs,
 #define VNC_DIRTY_BPL(x) (sizeof((x)->dirty) / VNC_MAX_HEIGHT * BITS_PER_BYTE)
 
 #define VNC_STAT_RECT  64
-#define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT)
-#define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT)
+#define VNC_STAT_COLS DIV_ROUND_UP(VNC_MAX_WIDTH, VNC_STAT_RECT)
+#define VNC_STAT_ROWS DIV_ROUND_UP(VNC_MAX_HEIGHT, VNC_STAT_RECT)
 
 #define VNC_AUTH_CHALLENGE_SIZE 16
 
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/4 ui/vnc: fix OOB write in lossy rect worker code

In reply to Daniel P. Berrangé

Incorrect calculation of the boundary condition when tracking lossy rectangles in the worker thread will result in an OOB write which can corrupt further worker state, and/or trigger any guard pages that may lie beyond the VncWorker struct. This can be triggered through careful choice of the display resolution in the guest OS by an unprivileged user.

---
 ui/vnc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
ui/vnc.c +4 -4
index 56dd43d53f..ee567700a5 100644
@@ -2982,13 +2982,13 @@ void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h)
 {
     int i, j;
 
-    w = (x + w) / VNC_STAT_RECT;
-    h = (y + h) / VNC_STAT_RECT;
+    w = DIV_ROUND_UP((x + w), VNC_STAT_RECT);
+    h = DIV_ROUND_UP((y + h), VNC_STAT_RECT);
     x /= VNC_STAT_RECT;
     y /= VNC_STAT_RECT;
 
-    for (j = y; j <= h; j++) {
-        for (i = x; i <= w; i++) {
+    for (j = y; j < h; j++) {
+        for (i = x; i < w; i++) {
             worker->lossy_rect[j][i] = 1;
         }
     }
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH 3/4] ui/vnc: fix OOB write in lossy rect worker code

Quoted 20 lines · Daniel P. Berrangé

Incorrect calculation of the boundary condition when tracking lossy rectangles in the worker thread will result in an OOB write which can corrupt further worker state, and/or trigger any guard pages that may lie beyond the VncWorker struct. This can be triggered through careful choice of the display resolution in the guest OS by an unprivileged user.

---
  ui/vnc.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
ui/vnc.c +0 -0
index 56dd43d53f..ee567700a5 100644

ui/vnc.c void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h) · line 2986

Quoted 7 lines
ui/vnc.c +2 -2
@@ -2982,13 +2982,13 @@ void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h)
  {
      int i, j;
 
-    w = (x + w) / VNC_STAT_RECT;
-    h = (y + h) / VNC_STAT_RECT;
+    w = DIV_ROUND_UP((x + w), VNC_STAT_RECT);
+    h = DIV_ROUND_UP((y + h), VNC_STAT_RECT);
       assert(h < VNC_STAT_ROWS);
       assert(w < VNC_STAT_COLS);
Quoted 10 lines
ui/vnc.c +2 -2
      x /= VNC_STAT_RECT;
      y /= VNC_STAT_RECT;
 
-    for (j = y; j <= h; j++) {
-        for (i = x; i <= w; i++) {
+    for (j = y; j < h; j++) {
+        for (i = x; i < w; i++) {
              worker->lossy_rect[j][i] = 1;
          }
      }
#5 lore raw

Re: [PATCH 3/4] ui/vnc: fix OOB write in lossy rect worker code

Hi

Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Daniel P. Berrangé

Incorrect calculation of the boundary condition when tracking lossy rectangles in the worker thread will result in an OOB write which can corrupt further worker state, and/or trigger any guard pages that may lie beyond the VncWorker struct. This can be triggered through careful choice of the display resolution in the guest OS by an unprivileged user.

---
  ui/vnc.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
ui/vnc.c +0 -0
index 56dd43d53f..ee567700a5 100644
@@ -2982,13 +2982,13 @@ void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h)
  {
      int i, j;
 
Quoted 8 lines

ui/vnc.c void vnc_sent_lossy_rect(VncWorker *worker, int x, int y, int w, int h) · line 2986

Quoted 4 lines
ui/vnc.c +2 -2
-    w = (x + w) / VNC_STAT_RECT;
-    h = (y + h) / VNC_STAT_RECT;
+    w = DIV_ROUND_UP((x + w), VNC_STAT_RECT);
+    h = DIV_ROUND_UP((y + h), VNC_STAT_RECT);
        assert(h < VNC_STAT_ROWS);
        assert(w < VNC_STAT_COLS);

with <=, yes, I can add this while taking.

Quoted 12 lines
Quoted 10 lines
ui/vnc.c +2 -2
      x /= VNC_STAT_RECT;
      y /= VNC_STAT_RECT;
 
-    for (j = y; j <= h; j++) {
-        for (i = x; i <= w; i++) {
+    for (j = y; j < h; j++) {
+        for (i = x; i < w; i++) {
              worker->lossy_rect[j][i] = 1;
          }
      }
#6 lore raw

PATCH 4/4 ui/vnc: fix OOB read updating VNC update frequency stats

In reply to Daniel P. Berrangé

Incorrect loop bounds in vnc_update_freq result in iterating past the last row and past the last column in the VNC stats array. With suitably chosen dimensions this could be a OOB read that accesses memory beyond the VncDisplay struct that the stats array is embedded in.

Should this hit a guard page, it could trigger a guest crash. If it does not, then the VNC frequency stats will be updated with garbage.

---
 ui/vnc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
ui/vnc.c +4 -2
index ee567700a5..03716d7fe9 100644
@@ -3091,12 +3091,14 @@ double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
     int i, j;
     double total = 0;
     int num = 0;
+    int x_end = x + w;
+    int y_end = y + h;
 
     x =  QEMU_ALIGN_DOWN(x, VNC_STAT_RECT);
     y =  QEMU_ALIGN_DOWN(y, VNC_STAT_RECT);
 
-    for (j = y; j <= y + h; j += VNC_STAT_RECT) {
-        for (i = x; i <= x + w; i += VNC_STAT_RECT) {
+    for (j = y; j < y_end; j += VNC_STAT_RECT) {
+        for (i = x; i < x_end; i += VNC_STAT_RECT) {
             total += vnc_stat_rect(vs->vd, i, j)->freq;
             num++;
         }
Signature · 1 line
2.54.0