All of lore.kernel.org
 help / color / mirror / Atom feed
From: Utkarsh Verma <uverma@linux.ibm.com>
To: qemu-devel@nongnu.org, qemu-ppc@nongnu.org
Cc: Chinmay Rath <rathc@linux.ibm.com>,
	Harsh Prateek Bora <harshpb@linux.ibm.com>,
	Amit Machhiwal <amachhiw@linux.ibm.com>,
	Nicholas Piggin <npiggin@gmail.com>,
	qemu-stable@nongnu.org, Utkarsh Verma <uverma@linux.ibm.com>
Subject: [PATCH v2 2/3] spapr: nested: Fix the return codes for H_GUEST_{GET, SET}_STATE and H_GUEST_RUN_VCPU
Date: Mon, 30 Nov 2026 16:18:49 +0530	[thread overview]
Message-ID: <20261130104850.190510-3-uverma@linux.ibm.com> (raw)
In-Reply-To: <20261130104850.190510-1-uverma@linux.ibm.com>

Instead of returning H_P3 for any kind of error in
H_GUEST_{GET,SET}_STATE and H_GUEST_RUN_VCPU hcalls, return proper error
code.
Additionally add the missing return code values: H_INVALID_ELEMENT_ID,
H_INVALID_ELEMENT_SIZE, H_INPUT_BUFFER_NOT_DEFINED, and
H_INPUT_BUFFER_TOO_SMALL (values taken from Table 180 in PAPR v10.60)

Cc: qemu-stable@nongnu.org
Signed-off-by: Utkarsh Verma <uverma@linux.ibm.com>
---
 hw/ppc/spapr_nested.c         | 64 ++++++++++++++++++++++++++---------
 include/hw/ppc/spapr.h        |  4 +++
 include/hw/ppc/spapr_nested.h |  1 +
 3 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/hw/ppc/spapr_nested.c b/hw/ppc/spapr_nested.c
index 35b5ae9785..b636da8dd0 100644
--- a/hw/ppc/spapr_nested.c
+++ b/hw/ppc/spapr_nested.c
@@ -1112,17 +1112,23 @@ static void log_element(struct guest_state_element *element,
                   be64_to_cpu(*(uint64_t *)element->value));
 }
 
-static bool guest_state_request_check(struct guest_state_request *gsr)
+static target_ulong guest_state_request_check(struct guest_state_request *gsr)
 {
     int64_t num_elements, len = gsr->len;
     struct guest_state_buffer *gsb = gsr->gsb;
     struct guest_state_element *element;
     struct guest_state_element_type *type;
     uint16_t id, size;
+    target_ulong rc;
 
     /* gsb->num_elements = 0 == 32 bits long */
     if (len < (int64_t)sizeof(gsb->num_elements)) {
-        return false;
+        if (gsr->hcall == H_GUEST_RUN_VCPU) {
+            rc = H_INPUT_BUFFER_TOO_SMALL;
+        } else {
+            rc = H_P5;
+        }
+        return rc;
     }
 
     num_elements = be32_to_cpu(gsb->num_elements);
@@ -1133,7 +1139,12 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
     while (num_elements) {
 
         if (len < (int64_t)offsetof(struct guest_state_element, value)) {
-            return false;
+            if (gsr->hcall == H_GUEST_RUN_VCPU) {
+                rc = H_INPUT_BUFFER_TOO_SMALL;
+            } else {
+                rc = H_P5;
+            }
+            return rc;
         }
         id = be16_to_cpu(element->id);
         size = be16_to_cpu(element->size);
@@ -1144,14 +1155,19 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
         /* buffer size too small */
         if (len < (int64_t)(offsetof(struct guest_state_element, value) +
                             size)) {
-            return false;
+            if (gsr->hcall == H_GUEST_RUN_VCPU) {
+                rc = H_INPUT_BUFFER_TOO_SMALL;
+            } else {
+                rc = H_P5;
+            }
+            return rc;
         }
 
         type = guest_state_element_type_find(id);
         if (!type) {
             qemu_log_mask(LOG_GUEST_ERROR, "Element ID %04x unknown\n", id);
             log_element(element, gsr);
-            return false;
+            return H_INVALID_ELEMENT_ID;
         }
 
         if (id == GSB_HV_VCPU_IGNORED_ID) {
@@ -1162,14 +1178,14 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
             qemu_log_mask(LOG_GUEST_ERROR, "Size mismatch. Element ID:%04x."
                           "Size Exp:%i Got:%i\n", id, type->size, size);
             log_element(element, gsr);
-            return false;
+            return H_INVALID_ELEMENT_SIZE;
         }
 
         if ((type->flags & GUEST_STATE_ELEMENT_TYPE_FLAG_READ_ONLY) &&
             (gsr->flags & GUEST_STATE_REQUEST_SET)) {
             qemu_log_mask(LOG_GUEST_ERROR, "Trying to set a read-only Element "
                           "ID:%04x.\n", id);
-            return false;
+            return H_INVALID_ELEMENT_ID;
         }
 
         if (type->flags & GUEST_STATE_ELEMENT_TYPE_FLAG_HOST_WIDE) {
@@ -1177,14 +1193,14 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
             if (!(gsr->flags & GUEST_STATE_REQUEST_HOST_WIDE)) {
                 qemu_log_mask(LOG_GUEST_ERROR, "trying to get/set a host wide "
                               "Element ID:%04x.\n", id);
-                return false;
+                return H_INVALID_ELEMENT_ID;
             }
         } else  if (type->flags & GUEST_STATE_ELEMENT_TYPE_FLAG_GUEST_WIDE) {
             /* guest wide element type */
             if (!(gsr->flags & GUEST_STATE_REQUEST_GUEST_WIDE)) {
                 qemu_log_mask(LOG_GUEST_ERROR, "trying to get/set a guest wide "
                               "Element ID:%04x.\n", id);
-                return false;
+                return H_INVALID_ELEMENT_ID;
             }
         } else {
             /* thread wide element type */
@@ -1192,14 +1208,14 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
                               GUEST_STATE_REQUEST_HOST_WIDE)) {
                 qemu_log_mask(LOG_GUEST_ERROR, "trying to get/set a thread wide"
                             " Element ID:%04x.\n", id);
-                return false;
+                return H_INVALID_ELEMENT_ID;
             }
         }
 next_element:
         element = guest_state_element_next(element, &len, &num_elements);
 
     }
-    return true;
+    return H_SUCCESS;
 }
 
 static bool is_gsr_invalid(struct guest_state_request *gsr,
@@ -1470,11 +1486,13 @@ static target_ulong getset_state(SpaprMachineState *spapr,
     struct guest_state_element *element;
     struct guest_state_element_type *type;
     int64_t lenleft, num_elements;
+    target_ulong rc;
 
     lenleft = gsr->len;
 
-    if (!guest_state_request_check(gsr)) {
-        return H_P3;
+    rc = guest_state_request_check(gsr);
+    if (rc != H_SUCCESS) {
+        return rc;
     }
 
     num_elements = be32_to_cpu(gsr->gsb->num_elements);
@@ -1526,12 +1544,20 @@ 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;
+        if (gsr->hcall == H_GUEST_RUN_VCPU) {
+            rc = H_INPUT_BUFFER_NOT_DEFINED;
+        } else {
+            rc = H_P4;
+        }
         goto out1;
     }
 
     if (len != gsr->len) {
-        rc = H_P3;
+        if (gsr->hcall == H_GUEST_RUN_VCPU) {
+            rc = H_INPUT_BUFFER_TOO_SMALL;
+        } else {
+            rc = H_P5;
+        }
         goto out1;
     }
 
@@ -1556,7 +1582,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;
 
@@ -1567,11 +1595,13 @@ static target_ulong h_guest_getset_state(PowerPCCPU *cpu,
     }
 
     if (set) {
+        gsr.hcall = H_GUEST_SET_STATE;
         if (flags & ~H_GUEST_SET_STATE_FLAGS_MASK) {
             return H_PARAMETER;
         }
         gsr.flags |= GUEST_STATE_REQUEST_SET;
     } else {
+        gsr.hcall = H_GUEST_GET_STATE;
         /*
          * No reserved fields to be set in flags nor both
          * GUEST/HOST wide bits
@@ -1742,6 +1772,7 @@ static void exit_process_output_buffer(SpaprMachineState *spapr,
     gsr.gsb = gsb;
     gsr.len = VCPU_OUT_BUF_MIN_SZ;
     gsr.flags = 0; /* get + never guest wide */
+    gsr.hcall = H_GUEST_RUN_VCPU;
     getset_state(spapr, guest, vcpuid, &gsr);
 
     address_space_unmap(CPU(cpu)->as, gsb, len, true, len);
@@ -1882,6 +1913,7 @@ static target_ulong h_guest_run_vcpu(PowerPCCPU *cpu,
     gsr.buf = vcpu->runbufin.addr;
     gsr.len = vcpu->runbufin.size;
     gsr.flags = GUEST_STATE_REQUEST_SET; /* Thread wide + writing */
+    gsr.hcall = H_GUEST_RUN_VCPU;
     rc = map_and_getset_state(cpu, spapr,  guest, vcpuid, &gsr);
     if (rc == H_SUCCESS) {
         nested_papr_run_vcpu(cpu, lpid, vcpu);
diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
index 98a9099b8a..f35716311a 100644
--- a/include/hw/ppc/spapr.h
+++ b/include/hw/ppc/spapr.h
@@ -359,7 +359,11 @@ struct SpaprMachineState {
 #define H_OVERLAP         -68
 #define H_STATE           -75
 #define H_IN_USE          -77
+#define H_INVALID_ELEMENT_ID               -79
+#define H_INVALID_ELEMENT_SIZE             -80
 #define H_INVALID_ELEMENT_VALUE            -81
+#define H_INPUT_BUFFER_NOT_DEFINED         -82
+#define H_INPUT_BUFFER_TOO_SMALL           -83
 #define H_UNSUPPORTED_FLAG -256
 #define H_MULTI_THREADS_ACTIVE -9005
 
diff --git a/include/hw/ppc/spapr_nested.h b/include/hw/ppc/spapr_nested.h
index f7be0d5a95..91edaf5c92 100644
--- a/include/hw/ppc/spapr_nested.h
+++ b/include/hw/ppc/spapr_nested.h
@@ -400,6 +400,7 @@ struct guest_state_request {
     int64_t buf;
     int64_t len;
     uint16_t flags;
+    uint64_t hcall;
 };
 
 /*
-- 
2.55.0



  parent reply	other threads:[~2026-11-30 10:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:48 [PATCH v2 0/3] spapr: nested: Fix OOB read and return codes in H_GUEST_{GET, SET}_STATE Utkarsh Verma
2026-11-30 10:48 ` [PATCH v2 1/3] spapr: nested: Check guest state buffer len " Utkarsh Verma
2026-11-30 10:48 ` Utkarsh Verma [this message]
2026-11-30 10:48 ` [PATCH v2 3/3] spapr: nested: Reject zero length buffer " Utkarsh Verma

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130104850.190510-3-uverma@linux.ibm.com \
    --to=uverma@linux.ibm.com \
    --cc=amachhiw@linux.ibm.com \
    --cc=harshpb@linux.ibm.com \
    --cc=npiggin@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=rathc@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.