* [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse()
2026-12-02 18:16 [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0 Amit Machhiwal
@ 2026-12-02 18:16 ` Amit Machhiwal
2026-12-02 18:16 ` [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0 Amit Machhiwal
1 sibling, 0 replies; 4+ messages in thread
From: Amit Machhiwal @ 2026-12-02 18:16 UTC (permalink / raw)
To: Madhavan Srinivasan, linuxppc-dev
Cc: Amit Machhiwal, Gautam Menghani, Harsh Prateek Bora,
Nicholas Piggin, Michael Ellerman, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Jordan Niethe, kvm, stable
kvmppc_gse_parse() validates each element by comparing its length
against kvmppc_gsid_size() for its ID. For an ID that does not belong
to any guest state class, kvmppc_gsid_size() returns 0, so an element
with such an ID and a length of 0 passes the check.
kvmppc_gsp_insert() then calls kvmppc_gse_flatten_iden(), which returns
0 for an unclassified ID. That is the bitmap position of
KVMPPC_GSID_BLANK, so the element is stored in gses[0] and bit 0 is set
in the parser's iterator bitmap.
kvmppc_gsp_for_each() uses ID 0 as its terminator, and bit 0 maps back
to ID 0 in kvmppc_gse_unflatten_iden(). Because bit 0 is the lowest
position, the iteration ends on its first step and none of the other
parsed elements are visited. Callers such as
gs_msg_ops_vcpu_refresh_info() still return 0, so the vCPU state is left
stale without any error.
The buffer contents come from the L0 hypervisor, so L1 should reject an
element it cannot interpret instead of folding it onto an unrelated bit.
Every type in kvmppc_gse_iden_len[] has a non-zero size, so a size of 0
can only mean the ID has no defined type. Treat that as invalid and
return -EINVAL, the same as a length mismatch.
Fixes: 6ccbbc33f06a ("KVM: PPC: Add helper library for Guest State Buffers")
Cc: stable@vger.kernel.org
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
arch/powerpc/kvm/guest-state-buffer.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/arch/powerpc/kvm/guest-state-buffer.c b/arch/powerpc/kvm/guest-state-buffer.c
index aeb586ba4523..21466bc4741e 100644
--- a/arch/powerpc/kvm/guest-state-buffer.c
+++ b/arch/powerpc/kvm/guest-state-buffer.c
@@ -304,10 +304,17 @@ int kvmppc_gse_parse(struct kvmppc_gs_parser *gsp, struct kvmppc_gs_buff *gsb)
{
struct kvmppc_gs_elem *curr;
int rem, i;
+ u16 size;
kvmppc_gsb_for_each_elem(i, curr, gsb, rem) {
- if (kvmppc_gse_len(curr) !=
- kvmppc_gsid_size(kvmppc_gse_iden(curr)))
+ size = kvmppc_gsid_size(kvmppc_gse_iden(curr));
+
+ /*
+ * A size of 0 means the ID is unknown (or has no defined
+ * type). Don't let a zero length element with such an ID
+ * slip through the length check below.
+ */
+ if (!size || kvmppc_gse_len(curr) != size)
return -EINVAL;
kvmppc_gsp_insert(gsp, kvmppc_gse_iden(curr), curr);
}
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0
2026-12-02 18:16 [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0 Amit Machhiwal
2026-12-02 18:16 ` [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse() Amit Machhiwal
@ 2026-12-02 18:16 ` Amit Machhiwal
2026-12-02 18:21 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Amit Machhiwal @ 2026-12-02 18:16 UTC (permalink / raw)
To: Madhavan Srinivasan, linuxppc-dev
Cc: Amit Machhiwal, Gautam Menghani, Harsh Prateek Bora,
Nicholas Piggin, Michael Ellerman, Christophe Leroy (CS GROUP),
Ritesh Harjani (IBM), Shrikanth Hegde, Jordan Niethe, kvm, stable
kvmppc_gse_flatten_iden() converts a guest state ID into a bitmap
position. For an ID that does not belong to any guest state class it
returns 0, which is also the valid position of KVMPPC_GSID_BLANK. A
caller passing such an ID therefore silently reads or modifies the
state of an unrelated element, and because ID 0 terminates the
kvmppc_gsbm_for_each() style iterators, a stray bit 0 also hides every
other element from them.
Return -1 for an unclassified ID instead and make the users of
kvmppc_gse_flatten_iden() cope with it:
- kvmppc_gsp_insert(), kvmppc_gsbm_set() and kvmppc_gsbm_clear() warn
once and do nothing.
- kvmppc_gsbm_test() warns once and returns false.
- kvmppc_gsbm_next() warns once and ends the iteration if the previous
ID is unclassified. The old "flatten(prev) + 1" form would have
turned -1 back into position 0, so check the result first.
- kvmppc_gsp_lookup() returns NULL, as for an element that is absent.
The guest state buffer is produced by the L0 hypervisor. PowerVM is not
expected to send an element that L1 cannot interpret, but other L0
implementations exist, for example the QEMU TCG nestedv2 model, and L1
should not depend on that. Elements received from L0 are already
rejected by kvmppc_gse_parse() if their ID is unclassified, so they
never reach these helpers. The warnings can therefore only be triggered
by a kernel bug, such as adding a new guest state ID without a class,
and not by the contents of a guest state buffer.
Cc: stable@vger.kernel.org
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
arch/powerpc/kvm/guest-state-buffer.c | 40 +++++++++++++++++++++++----
1 file changed, 34 insertions(+), 6 deletions(-)
diff --git a/arch/powerpc/kvm/guest-state-buffer.c b/arch/powerpc/kvm/guest-state-buffer.c
index 21466bc4741e..40966a8b5989 100644
--- a/arch/powerpc/kvm/guest-state-buffer.c
+++ b/arch/powerpc/kvm/guest-state-buffer.c
@@ -325,12 +325,19 @@ int kvmppc_gse_parse(struct kvmppc_gs_parser *gsp, struct kvmppc_gs_buff *gsb)
}
EXPORT_SYMBOL_GPL(kvmppc_gse_parse);
+/*
+ * Returns the bitmap position of @iden, or -1 if @iden does not belong to any
+ * guest state class. Bit 0 is a valid position (KVMPPC_GSID_BLANK) so an
+ * unclassified ID must not be mapped onto it.
+ */
static inline int kvmppc_gse_flatten_iden(u16 iden)
{
int bit = 0;
int class;
class = kvmppc_gsid_class(iden);
+ if (class < 0)
+ return -1;
if (class == KVMPPC_GS_CLASS_GUESTWIDE) {
bit += iden - KVMPPC_GSE_GUESTWIDE_START;
@@ -379,7 +386,7 @@ static inline int kvmppc_gse_flatten_iden(u16 iden)
return bit;
}
- return 0;
+ return -1;
}
static inline u16 kvmppc_gse_unflatten_iden(int bit)
@@ -442,6 +449,8 @@ void kvmppc_gsp_insert(struct kvmppc_gs_parser *gsp, u16 iden,
int i;
i = kvmppc_gse_flatten_iden(iden);
+ if (WARN_ON_ONCE(i < 0))
+ return;
kvmppc_gsbm_set(&gsp->iterator, iden);
gsp->gses[i] = gse;
}
@@ -459,6 +468,8 @@ struct kvmppc_gs_elem *kvmppc_gsp_lookup(struct kvmppc_gs_parser *gsp, u16 iden)
int i;
i = kvmppc_gse_flatten_iden(iden);
+ if (i < 0)
+ return NULL;
return gsp->gses[i];
}
EXPORT_SYMBOL_GPL(kvmppc_gsp_lookup);
@@ -470,7 +481,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsp_lookup);
*/
void kvmppc_gsbm_set(struct kvmppc_gs_bitmap *gsbm, u16 iden)
{
- set_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+ int bit = kvmppc_gse_flatten_iden(iden);
+
+ if (WARN_ON_ONCE(bit < 0))
+ return;
+ set_bit(bit, gsbm->bitmap);
}
EXPORT_SYMBOL_GPL(kvmppc_gsbm_set);
@@ -481,7 +496,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_set);
*/
void kvmppc_gsbm_clear(struct kvmppc_gs_bitmap *gsbm, u16 iden)
{
- clear_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+ int bit = kvmppc_gse_flatten_iden(iden);
+
+ if (WARN_ON_ONCE(bit < 0))
+ return;
+ clear_bit(bit, gsbm->bitmap);
}
EXPORT_SYMBOL_GPL(kvmppc_gsbm_clear);
@@ -492,7 +511,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_clear);
*/
bool kvmppc_gsbm_test(struct kvmppc_gs_bitmap *gsbm, u16 iden)
{
- return test_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+ int bit = kvmppc_gse_flatten_iden(iden);
+
+ if (WARN_ON_ONCE(bit < 0))
+ return false;
+ return test_bit(bit, gsbm->bitmap);
}
EXPORT_SYMBOL_GPL(kvmppc_gsbm_test);
@@ -503,9 +526,14 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_test);
*/
u16 kvmppc_gsbm_next(struct kvmppc_gs_bitmap *gsbm, u16 prev)
{
- int bit, pbit;
+ int bit, pbit = 0;
- pbit = prev ? kvmppc_gse_flatten_iden(prev) + 1 : 0;
+ if (prev) {
+ pbit = kvmppc_gse_flatten_iden(prev);
+ if (WARN_ON_ONCE(pbit < 0))
+ return 0;
+ pbit++;
+ }
bit = find_next_bit(gsbm->bitmap, KVMPPC_GSE_IDEN_COUNT, pbit);
if (bit < KVMPPC_GSE_IDEN_COUNT)
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 4+ messages in thread