Skip to contentSkip to conversation

spapr: nested: Check guest state buffer len in H_GUEST_{GET, SET}_STATE

PATCH In review Utkarsh Verma 2 participants 2 messages Nov 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spapr: nested: Check guest state buffer len in H_GUEST_{GET, SET}_STATE

Add validations to check for guest state buffer length before accessing the fields to avoid out of bound memory reads.

Also, replace the assertions for invalid arguments with proper return error codes and fix the return error codes at other places as well (return code H_P4 for buf and H_P5 for buflen)

Fixes 64c43909b297 ("spapr: nested: Introduce H_GUEST_[GET|SET]_STATE hcalls.")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4741

---
 hw/ppc/spapr_nested.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
hw/ppc/spapr_nested.c +13 -6
index 10cf634da1..8b981fff14 100644
@@ -1117,7 +1117,9 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
     uint16_t id, size;
 
     /* gsb->num_elements = 0 == 32 bits long */
-    assert(len >= 4);
+    if (len < sizeof(gsb->num_elements)) {
+        return false;
+    }
 
     num_elements = be32_to_cpu(gsb->num_elements);
     element = gsb->elements;
@@ -1126,6 +1128,9 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
     /* Walk the buffer to validate the length */
     while (num_elements) {
 
+        if (len < offsetof(struct guest_state_element, value)) {
+            return false;
+        }
         id = be16_to_cpu(element->id);
         size = be16_to_cpu(element->size);
 
@@ -1133,7 +1138,7 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
             log_element(element, gsr);
         }
         /* buffer size too small */
-        if (len < 0) {
+        if (len < offsetof(struct guest_state_element, value) + size) {
             return false;
         }
 
@@ -1464,7 +1469,7 @@ static target_ulong getset_state(SpaprMachineState *spapr,
     lenleft = gsr->len;
 
     if (!guest_state_request_check(gsr)) {
-        return H_P3;
+        return H_P4;
     }
 
     num_elements = be32_to_cpu(gsr->gsb->num_elements);
@@ -1516,12 +1521,12 @@ static target_ulong map_and_getset_state(PowerPCCPU *cpu,
     gsr->gsb = address_space_map(CPU(cpu)->as, gsr->buf, (uint64_t *)&len,
                                  is_write, MEMTXATTRS_UNSPECIFIED);
     if (!gsr->gsb) {
-        rc = H_P3;
+        rc = H_P4;
         goto out1;
     }
 
     if (len != gsr->len) {
-        rc = H_P3;
+        rc = H_P4;
         goto out1;
     }
 
@@ -1546,7 +1551,9 @@ static target_ulong h_guest_getset_state(PowerPCCPU *cpu,
     SpaprMachineStateNestedGuest *guest = NULL;
 
     gsr.buf = buf;
-    assert(buflen <= GSB_MAX_BUF_SIZE);
+    if (buflen > GSB_MAX_BUF_SIZE) {
+        return H_P5;
+    }
     gsr.len = buflen;
     gsr.flags = 0;
 
Signature · 1 line
2.55.0
#1 lore raw

Hi Utkarsh,

Thanks for working on this and sending the patch. Please find my comments inline below.

Quoted 8 lines · Utkarsh Verma

Add validations to check for guest state buffer length before accessing the fields to avoid out of bound memory reads.

Also, replace the assertions for invalid arguments with proper return error codes and fix the return error codes at other places as well (return code H_P4 for buf and H_P5 for buflen)

Fixes 64c43909b297 ("spapr: nested: Introduce H_GUEST_[GET|SET]_STATE hcalls.")

Please consider adding:
Cc: [email protected]

Quoted 11 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4741

---
 hw/ppc/spapr_nested.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
hw/ppc/spapr_nested.c +0 -0
index 10cf634da1..8b981fff14 100644

hw/ppc/spapr_nested.c static bool guest_state_request_check(struct guest_state_request *gsr) · line 1122

Quoted 7 lines
hw/ppc/spapr_nested.c +3 -1
@@ -1117,7 +1117,9 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
     uint16_t id, size;
 
     /* gsb->num_elements = 0 == 32 bits long */
-    assert(len >= 4);
+    if (len < sizeof(gsb->num_elements)) {
+        return false;
+    }

Removing assert for an input validation makes sense. We always expect a return from an hcall and an assert can't return anything to the guest. A failed assert calls abort(), which kills the whole QEMU process, i.e. the L1 guest and all its nested guests.

Quoted 14 lines
hw/ppc/spapr_nested.c +3 -0
     num_elements = be32_to_cpu(gsb->num_elements);
     element = gsb->elements;
@@ -1126,6 +1128,9 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
     /* Walk the buffer to validate the length */
     while (num_elements) {
 
+        if (len < offsetof(struct guest_state_element, value)) {
+            return false;
+        }
         id = be16_to_cpu(element->id);
         size = be16_to_cpu(element->size);
 

hw/ppc/spapr_nested.c static bool guest_state_request_check(struct guest_state_request *gsr) · line 1141

Quoted 5 lines
hw/ppc/spapr_nested.c +1 -1
@@ -1133,7 +1138,7 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
             log_element(element, gsr);
         }
         /* buffer size too small */
-        if (len < 0) {
+        if (len < offsetof(struct guest_state_element, value) + size) {

The old check ran after id/size were read, and it only noticed an overrun at the next iteration, so it could never catch the last element. Two notes on this function:

- len is int64_t and these comparisons are against size_t, so a negative len would compare as huge and pass. That cannot happen today: on the hcall path len starts at buflen (<= GSB_MAX_BUF_SIZE) and it is only reduced after these checks pass. It depends on that ordering, so a cast or a signed constant might be worth it.

- Two paths below still call log_element() after the new checks: unknown element ID and size mismatch. log_element() reads 8 bytes from element->value whatever element->size is, so an element header that ends exactly at the end of the buffer would still read past the buffer, but only with -d guest_errors. I have not tested this; it comes from reading the code.

hw/ppc/spapr_nested.c static target_ulong getset_state(SpaprMachineState *spapr, · line 1472

Quoted 9 lines
hw/ppc/spapr_nested.c +1 -1
             return false;
         }
 
@@ -1464,7 +1469,7 @@ static target_ulong getset_state(SpaprMachineState *spapr,
     lenleft = gsr->len;
 
     if (!guest_state_request_check(gsr)) {
-        return H_P3;
+        return H_P4;

I don't think H_P4 is right for all of these. PAPR describes H_P4 as "the dataBuffer had an invalid data buffer address", and for H_GUEST_GET_STATE H_P5 as "the dataBufferSizeInBytes parameter had an invalid data buffer size or the buffer was too small to fit the requested guest state".

The three new length checks above (len < 4, truncated header, truncated payload) are "buffer too small" cases, so for GET_STATE they should return H_P5. The tables also have H_Invalid_Element_Id and H_Invalid_Element_Size, which fit the unknown-ID and size-mismatch failures below. Since H_GUEST_RUN_VCPU reaches this function too and uses different codes (H_Input_Buffer_Too_Small, H_Invalid_Element_*), guest_state_request_check() would need to tell the caller why it failed, not only that it did.

Quoted 5 lines
hw/ppc/spapr_nested.c +0 -0
     }
 
     num_elements = be32_to_cpu(gsr->gsb->num_elements);
@@ -1516,12 +1521,12 @@ static target_ulong map_and_getset_state(PowerPCCPU *cpu,
     gsr->gsb = address_space_map(CPU(cpu)->as, gsr->buf, (uint64_t *)&len,

hw/ppc/spapr_nested.c static target_ulong map_and_getset_state(PowerPCCPU *cpu, · line 1529

Quoted 10 lines
hw/ppc/spapr_nested.c +2 -2
                                  is_write, MEMTXATTRS_UNSPECIFIED);
     if (!gsr->gsb) {
-        rc = H_P3;
+        rc = H_P4;
         goto out1;
     }
 
     if (len != gsr->len) {
-        rc = H_P3;
+        rc = H_P4;

H_P4 is right for the two map failures.

The !gsr->gsb branch has a separate problem that this patch does not fix: with buflen = 0, address_space_map() returns NULL, we jump to out1 and call address_space_unmap() on the NULL buffer, which crashes QEMU with SIGSEGV on TCG. This is not a regression and should not block this patch. I have a possible fix that I'm still testing. I can send it as a follow-up on top of your next version, or you can fold it into v2 if you prefer; let me know.

Also, map_and_getset_state() is used by h_guest_run_vcpu() as well, where the buffer is the vCPU's registered input buffer (runbufin) and not an hcall argument. As implemented here, H_GUEST_RUN_VCPU only takes flags, lpid and vcpuid (H_P2 for lpid, H_P3 for vcpuid), and PAPR has no H_P4 for it, so a bad input buffer used to return H_P3 (the same as a bad vcpuid) and now returns H_P4. PAPR lists H_Input_Buffer_Not_Defined, H_Input_Buffer_Too_Small, H_Hardware and H_Invalid_Element_{Id,Size,Value} for the input buffer, so this path needs those codes instead.

hw/ppc/spapr_nested.c static target_ulong h_guest_getset_state(PowerPCCPU *cpu, · line 1558

Quoted 13 lines
hw/ppc/spapr_nested.c +3 -1
         goto out1;
     }
 
@@ -1546,7 +1551,9 @@ static target_ulong h_guest_getset_state(PowerPCCPU *cpu,
     SpaprMachineStateNestedGuest *guest = NULL;
 
     gsr.buf = buf;
-    assert(buflen <= GSB_MAX_BUF_SIZE);
+    if (buflen > GSB_MAX_BUF_SIZE) {
+        return H_P5;
+    }
     gsr.len = buflen;
     gsr.flags = 0;

I reproduced #4741 on an unpatched TCG build (SIGSEGV with buf = 0x0FFF_FFFC, buflen = 4, num_elements = 1 in a 256M guest). With the patch the hcall returns an error and QEMU keeps running. buflen = 3 and buflen = 0x110000 (SIGABRT before) also return an error now. My test image only prints S/E, so I did not check which return code comes back.

Thanks,
Amit