[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PULL 13/21] hw/char/xen_console: Fix buffer trim when size exceeds twice the limit



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

In buffer_append() we handle a full buffer by discarding bytes from the
*middle*, preserving the oldest and the newest data. When the overflow
exceeded the max capacity (a limit set by the guest), the memmove()
destination was miscalculated and ended up before the start of the
buffer. In that case there is no older data worth preserving anyway;
just keep the newest max_capacity bytes.

Fixes: CVE-2026-123071
Fixes: e57dd20bb706 ("xen: add console backend driver.")
Reported-by: Giovanni Vignone <https://gitlab.com/gio24>
Reported-by: Hui Peng <benquike@xxxxxxxxx>
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4191
Reviewed-by: Paul Durrant <paul@xxxxxxx>
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 hw/char/xen_console.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index df6a708494..849a93a817 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -89,17 +89,25 @@ static bool buffer_append(XenConsole *con)
 
     if (buffer->max_capacity &&
         buffer->size > buffer->max_capacity) {
-        /* Discard the middle of the data. */
-
+        /*
+         * Discard the middle of the data: keep the head of the buffer
+         * up to where the overflowing data will be moved down to end
+         * at max_capacity, and the newest 'keep' bytes. If the buffer
+         * has grown to more than twice max_capacity (the frontend may
+         * set an arbitrarily small 'limit'), there is no head to keep
+         * and the newest max_capacity bytes alone remain.
+         */
         size_t over = buffer->size - buffer->max_capacity;
-        uint8_t *maxpos = buffer->data + buffer->max_capacity;
+        size_t keep = MIN(over, buffer->max_capacity);
 
-        memmove(maxpos - over, maxpos, over);
+        memmove(buffer->data + buffer->max_capacity - keep,
+                buffer->data + buffer->size - keep, keep);
         buffer->data = g_realloc(buffer->data, buffer->max_capacity);
         buffer->size = buffer->capacity = buffer->max_capacity;
 
-        if (buffer->consumed > buffer->max_capacity - over)
-            buffer->consumed = buffer->max_capacity - over;
+        if (buffer->consumed > buffer->max_capacity - keep) {
+            buffer->consumed = buffer->max_capacity - keep;
+        }
     }
     return true;
 }
-- 
2.55.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.