* [PATCH v2 0/3] spapr: nested: Fix OOB read and return codes in H_GUEST_{GET, SET}_STATE
@ 2026-11-30 10:48 Utkarsh Verma
2026-11-30 10:48 ` [PATCH v2 1/3] spapr: nested: Check guest state buffer len " Utkarsh Verma
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Utkarsh Verma @ 2026-11-30 10:48 UTC (permalink / raw)
To: qemu-devel, qemu-ppc
Cc: Chinmay Rath, Harsh Prateek Bora, Amit Machhiwal, Nicholas Piggin,
qemu-stable, Utkarsh Verma
This series fixes an out-of-bounds read of the guest state buffer in
H_GUEST_{GET,SET}_STATE (and H_GUEST_RUN_VCPU, which shares the code),
and makes these hcalls return the error codes PAPR specifies instead of
asserting or returning H_P3 for everything.
Patch 1 adds length checks before the element header and payload are
read, and fixes the over-read in log_element().
Patch 2 makes guest_state_request_check() report why it failed, and
returns PAPR-compliant codes per hcall.
Patch 3 (from Amit) rejects buflen == 0, which crashed QEMU with a
SIGSEGV in address_space_unmap().
Changes since v1:
- Split the original patch into three.
- Return codes now depend on the hcall, as PAPR specifies.
- Added Amit's zero-length fix.
v1: https://lore.kernel.org/qemu-devel/20261120080829.195131-1-uverma@linux.ibm.com/
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4741
Amit Machhiwal (1):
spapr: nested: Reject zero length buffer in H_GUEST_{GET,SET}_STATE
Utkarsh Verma (2):
spapr: nested: Check guest state buffer len in H_GUEST_{GET,SET}_STATE
spapr: nested: Fix the return codes for H_GUEST_{GET,SET}_STATE and
H_GUEST_RUN_VCPU
hw/ppc/spapr_nested.c | 78 +++++++++++++++++++++++++++--------
include/hw/ppc/spapr.h | 4 ++
include/hw/ppc/spapr_nested.h | 1 +
3 files changed, 65 insertions(+), 18 deletions(-)
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/3] spapr: nested: Check guest state buffer len in H_GUEST_{GET, SET}_STATE
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 ` Utkarsh Verma
2026-11-30 10:48 ` [PATCH v2 2/3] spapr: nested: Fix the return codes for H_GUEST_{GET, SET}_STATE and H_GUEST_RUN_VCPU Utkarsh Verma
2026-11-30 10:48 ` [PATCH v2 3/3] spapr: nested: Reject zero length buffer in H_GUEST_{GET, SET}_STATE Utkarsh Verma
2 siblings, 0 replies; 4+ messages in thread
From: Utkarsh Verma @ 2026-11-30 10:48 UTC (permalink / raw)
To: qemu-devel, qemu-ppc
Cc: Chinmay Rath, Harsh Prateek Bora, Amit Machhiwal, Nicholas Piggin,
qemu-stable, Utkarsh Verma
Add validations to check for guest state buffer length before accessing
the element header (id, size) and value fields to avoid out of bound
memory reads. Also, do the similar fix in log_element() function.
Fixes: 64c43909b297 ("spapr: nested: Introduce H_GUEST_[GET|SET]_STATE hcalls.")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4741
Cc: qemu-stable@nongnu.org
Signed-off-by: Utkarsh Verma <uverma@linux.ibm.com>
---
hw/ppc/spapr_nested.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/hw/ppc/spapr_nested.c b/hw/ppc/spapr_nested.c
index 10cf634da1..35b5ae9785 100644
--- a/hw/ppc/spapr_nested.c
+++ b/hw/ppc/spapr_nested.c
@@ -1104,7 +1104,11 @@ static void log_element(struct guest_state_element *element,
qemu_log_mask(LOG_GUEST_ERROR, "h_guest_%s_state id:0x%04x size:0x%04x",
gsr->flags & GUEST_STATE_REQUEST_SET ? "set" : "get",
be16_to_cpu(element->id), be16_to_cpu(element->size));
- qemu_log_mask(LOG_GUEST_ERROR, "buf:0x%016"PRIx64" ...\n",
+ if (be16_to_cpu(element->size) < 8) {
+ qemu_log_mask(LOG_GUEST_ERROR, "\n");
+ return;
+ }
+ qemu_log_mask(LOG_GUEST_ERROR, " buf:0x%016"PRIx64" ...\n",
be64_to_cpu(*(uint64_t *)element->value));
}
@@ -1117,7 +1121,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 < (int64_t)sizeof(gsb->num_elements)) {
+ return false;
+ }
num_elements = be32_to_cpu(gsb->num_elements);
element = gsb->elements;
@@ -1126,6 +1132,9 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
/* Walk the buffer to validate the length */
while (num_elements) {
+ if (len < (int64_t)offsetof(struct guest_state_element, value)) {
+ return false;
+ }
id = be16_to_cpu(element->id);
size = be16_to_cpu(element->size);
@@ -1133,7 +1142,8 @@ static bool guest_state_request_check(struct guest_state_request *gsr)
log_element(element, gsr);
}
/* buffer size too small */
- if (len < 0) {
+ if (len < (int64_t)(offsetof(struct guest_state_element, value) +
+ size)) {
return false;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] spapr: nested: Fix the return codes for H_GUEST_{GET, SET}_STATE and H_GUEST_RUN_VCPU
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
2026-11-30 10:48 ` [PATCH v2 3/3] spapr: nested: Reject zero length buffer in H_GUEST_{GET, SET}_STATE Utkarsh Verma
2 siblings, 0 replies; 4+ messages in thread
From: Utkarsh Verma @ 2026-11-30 10:48 UTC (permalink / raw)
To: qemu-devel, qemu-ppc
Cc: Chinmay Rath, Harsh Prateek Bora, Amit Machhiwal, Nicholas Piggin,
qemu-stable, Utkarsh Verma
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] spapr: nested: Reject zero length buffer in H_GUEST_{GET, SET}_STATE
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 ` [PATCH v2 2/3] spapr: nested: Fix the return codes for H_GUEST_{GET, SET}_STATE and H_GUEST_RUN_VCPU Utkarsh Verma
@ 2026-11-30 10:48 ` Utkarsh Verma
2 siblings, 0 replies; 4+ messages in thread
From: Utkarsh Verma @ 2026-11-30 10:48 UTC (permalink / raw)
To: qemu-devel, qemu-ppc
Cc: Chinmay Rath, Harsh Prateek Bora, Amit Machhiwal, Nicholas Piggin,
qemu-stable
From: Amit Machhiwal <amachhiw@linux.ibm.com>
PAPR requires H_GUEST_GET_STATE and H_GUEST_SET_STATE to return H_P5
when dataBufferSizeInBytes is 0. QEMU does not check for it, and
address_space_map() returns NULL for a zero length.
map_and_getset_state() then jumps to out1 and calls
address_space_unmap() on the NULL pointer, so an L1 guest that passes
buflen = 0 crashes QEMU:
Program received signal SIGSEGV, Segmentation fault.
#0 address_space_unmap (buffer=0x0, len=0, ...) at system/physmem.c:3847
#1 map_and_getset_state (...) at hw/ppc/spapr_nested.c:1536
#2 h_guest_getset_state (...) at hw/ppc/spapr_nested.c:1592
Return H_P5 for a zero length together with the existing upper bound
check. Also return directly when address_space_map() fails for any
other reason, as there is nothing to unmap.
Fixes: 64c43909b297 ("spapr: nested: Introduce H_GUEST_[GET|SET]_STATE hcalls.")
Cc: qemu-stable@nongnu.org
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
hw/ppc/spapr_nested.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/hw/ppc/spapr_nested.c b/hw/ppc/spapr_nested.c
index b636da8dd0..ee922bb133 100644
--- a/hw/ppc/spapr_nested.c
+++ b/hw/ppc/spapr_nested.c
@@ -1549,7 +1549,7 @@ static target_ulong map_and_getset_state(PowerPCCPU *cpu,
} else {
rc = H_P4;
}
- goto out1;
+ return rc;
}
if (len != gsr->len) {
@@ -1582,7 +1582,7 @@ static target_ulong h_guest_getset_state(PowerPCCPU *cpu,
SpaprMachineStateNestedGuest *guest = NULL;
gsr.buf = buf;
- if (buflen > GSB_MAX_BUF_SIZE) {
+ if (buflen == 0 || buflen > GSB_MAX_BUF_SIZE) {
return H_P5;
}
gsr.len = buflen;
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-30 10:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2 2/3] spapr: nested: Fix the return codes for H_GUEST_{GET, SET}_STATE and H_GUEST_RUN_VCPU Utkarsh Verma
2026-11-30 10:48 ` [PATCH v2 3/3] spapr: nested: Reject zero length buffer in H_GUEST_{GET, SET}_STATE Utkarsh Verma
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.