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: [email protected]
Signed-off-by: Amit Machhiwal <[email protected]>
---
 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)


Reply via email to