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

[PATCH v1 12/21] hw/i386/kvm/xen_xenstore: Apply offset before truncation in xs_append_strings()



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

Truncating a string to the remaining payload space before applying the
requested starting offset corrupts the offset accounting, so
XS_DIRECTORY_PART returned wrong data for non-zero offsets. Skip to the
offset first, then check what fits.

Fixes: 0254c4d19df3 ("hw/xen: Add xenstore wire implementation and 
implementation stubs")
Reported-by: Hui Peng <benquike@xxxxxxxxx>
Closes: https://gitlab.com/qemu-project/qemu/-/work_items/4525
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 hw/i386/kvm/xen_xenstore.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/hw/i386/kvm/xen_xenstore.c b/hw/i386/kvm/xen_xenstore.c
index ff0dfb1bff8..7aa5d97e01a 100644
--- a/hw/i386/kvm/xen_xenstore.c
+++ b/hw/i386/kvm/xen_xenstore.c
@@ -614,6 +614,21 @@ static void xs_append_strings(XenXenstoreState *s, struct 
xsd_sockmsg *rsp,
         size_t len = strlen(l->data) + 1; /* Including the NUL termination */
         char *str = l->data;
 
+        /*
+         * Skip to the requested offset before checking what fits, so that
+         * truncation does not corrupt the offset accounting.
+         */
+        if (start) {
+            if (start >= len) {
+                start -= len;
+                continue;
+            }
+
+            str += start;
+            len -= start;
+            start = 0;
+        }
+
         if (rsp->len + len > XENSTORE_PAYLOAD_MAX) {
             if (truncate) {
                 len = XENSTORE_PAYLOAD_MAX - rsp->len;
@@ -626,17 +641,6 @@ static void xs_append_strings(XenXenstoreState *s, struct 
xsd_sockmsg *rsp,
             }
         }
 
-        if (start) {
-            if (start >= len) {
-                start -= len;
-                continue;
-            }
-
-            str += start;
-            len -= start;
-            start = 0;
-        }
-
         memcpy(&rsp_data[rsp->len], str, len);
         rsp->len += len;
     }
-- 
2.43.0




 


Rackspace

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