Skip to contentSkip to conversation

xen/vcpu: Improve sanity checks in vcpu_create()

PATCH In review Andrew Cooper 3 participants 3 messages Jan 15, 2020 – Jan 16, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/vcpu: Improve sanity checks in vcpu_create()

The BUG_ON() is confusing to follow. The (!is_idle_domain(d) || vcpu_id) part is a vestigial remnant of architectures poisioning idle_vcpu[0] with non-NULL pointers.

Now that idle_vcpu[0] is NULL on all architectures, and d->max_vcpus specified before vcpu_create() is called, we can properly range check the requested vcpu_id.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>

---
 xen/common/domain.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
xen/common/domain.c +13 -1
index 0b1103fdb2..ee3f9ffd3e 100644
@@ -139,7 +139,19 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id)
 {
     struct vcpu *v;
 
-    BUG_ON((!is_idle_domain(d) || vcpu_id) && d->vcpu[vcpu_id]);
+    /*
+     * Sanity check some input expectations:
+     * - vcpu_id should be bounded by d->max_vcpus, and not previously
+     *   allocated.
+     * - VCPUs should be tightly packed and allocated in ascending order,
+     *   except for the idle domain which may vary based on PCPU numbering.
+     */
+    if ( vcpu_id >= d->max_vcpus || d->vcpu[vcpu_id] ||
+         (!is_idle_domain(d) && vcpu_id && !d->vcpu[vcpu_id - 1]) )
+    {
+        ASSERT_UNREACHABLE();
+        return NULL;
+    }
 
     if ( (v = alloc_vcpu_struct(d)) == NULL )
         return NULL;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andrew Cooper

The BUG_ON() is confusing to follow. The (!is_idle_domain(d) || vcpu_id) part is a vestigial remnant of architectures poisioning idle_vcpu[0] with non-NULL pointers.

Now that idle_vcpu[0] is NULL on all architectures, and d->max_vcpus specified before vcpu_create() is called, we can properly range check the requested vcpu_id.

I guess this is meant to be true on top of your Arm side change which hasn't been committed yet? And perhaps better "... starts out as NULL on all ..."?

Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#2 lore raw

Hi,

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper

The BUG_ON() is confusing to follow. The (!is_idle_domain(d) || vcpu_id) part is a vestigial remnant of architectures poisioning idle_vcpu[0] with non-NULL pointers.

Quoted 7 lines
Quoted 3 lines

Now that idle_vcpu[0] is NULL on all architectures, and d->max_vcpus specified before vcpu_create() is called, we can properly range check the requested vcpu_id.

I guess this is meant to be true on top of your Arm side change which hasn't been committed yet? And perhaps better "... starts out as NULL on all ..."?

I can commit the two together.

Quoted 4 lines
Quoted 2 lines

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel