xen: add assertions in default_vcpu0_location to protect against broken masks
PATCH 1/1 xen: add assertions in default_vcpu0_location to protect against broken masks
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this in the future.
--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
xen/common/domctl.c +2 -0
@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t *online)*/ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);for_each_cpu (i, online) { +ASSERT (i < nr_cpus);if (cpumask_test_cpu (i, &cpu_exclude_map) )continue ; if ( (i == cpumask_first(per_cpu(cpu_sibling_mask, i))) &&
Signature · 1 line
1.7.9.1
Quoted 18 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this in the future.
Signed-off-by Ian Campbell <[email protected]>Cc Keir (Xen.org) <[email protected]>Cc Jan Beulich <[email protected]>--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)xen/common/domctl.c +0
-0
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t · line 196
Quoted 7 lines
xen/common/domctl.c +1
-0@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t*online) */ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);
You may want to add another ASSERT() here in case you care about the difference between nr_cpus and nr_cpu_ids. Or move the ASSERT() here if the difference is insignificant (as I would think), as cpumask_next() invokes cpumask_check() (see also below).
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t *online) · line 198
Quoted 3 lines
xen/common/domctl.c +1
-0for_each_cpu (i, online) { +ASSERT (i < nr_cpus);
This one is almost redundant with the one in cpumask_check() called from cpumask_test_cpu() (and the difference between using nr_cpu_ids and nr_cpus doesn't seem relevant here), so I'd recommend going with just the single earlier addition.
Jan
Quoted 5 lines
xen/common/domctl.c +0
-0if (cpumask_test_cpu (i, &cpu_exclude_map) )continue ;if ( (i == cpumask_first(per_cpu(cpu_sibling_mask, i))) &&Signature · 1 line
1.7.9.1
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this in the future.
Signed-off-by Ian Campbell <[email protected]>Cc Keir (Xen.org) <[email protected]>Cc Jan Beulich <[email protected]>--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)xen/common/domctl.c +1
-0@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t*online) */ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);
Quoted 8 lines
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t *online) · line 195
Quoted 2 lines
xen/common/domctl.c +0
-0if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);You may want to add another
ASSERT()here in case you care about the difference betweennr_cpusandnr_cpu_ids. Or move theASSERT()here if the difference is insignificant (as I would think), ascpumask_next()invokescpumask_check()(see also below).
It is possible to get through this code without calling cpumask_next(cpu, ..) though, I think, due to the if.
Perhaps
ASSERT(cpu < nr_cpu_ids);
after the if would be the best option?
Quoted 9 lines
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t *online) · line 198
Quoted 4 lines
xen/common/domctl.c +1
-0for_each_cpu (i, online) { +ASSERT (i < nr_cpus);This one is almost redundant with the one in
cpumask_check()called fromcpumask_test_cpu()(and the difference between usingnr_cpu_idsandnr_cpusdoesn't seem relevant here), so I'd recommend going with just the single earlier addition.
If mask is invalid you can the first iteration with the bad i (from cpumask_first, which doesn't have any checks), which may scribble off the end of the cnt array.
I'm not sure why I wasn't hitting an assert from the subsequent cpumask_next, but I wasn't.
If you are still convinced this ASSERT isn't worth it I'll drop it, I've found my bug now and it'd be a pretty uncommon one to repeat...
Ian.
Quoted 35 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this
in
Quoted 19 lines
Quoted 19 lines
the future.
Signed-off-by Ian Campbell <[email protected]>Cc Keir (Xen.org) <[email protected]>Cc Jan Beulich <[email protected]>--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)xen/common/domctl.c +1
-0@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t*online) */ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);Quoted 8 lines
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t *online) · line 195
Quoted 2 lines
xen/common/domctl.c +0
-0if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);You may want to add another
ASSERT()here in case you care about the difference betweennr_cpusandnr_cpu_ids. Or move theASSERT()here if the difference is insignificant (as I would think), ascpumask_next()invokescpumask_check()(see also below).
Quoted 6 lines
It is possible to get through this code without calling
cpumask_next(cpu, ..)though, I think, due to the if.Perhaps
ASSERT(cpu < nr_cpu_ids);after the if would be the best option?
That's what I meant with "here" in my first response.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
xen/common/domctl.c +1
-0for_each_cpu (i, online) { +ASSERT (i < nr_cpus);
Quoted 8 lines
Quoted 4 lines
This one is almost redundant with the one in
cpumask_check()called fromcpumask_test_cpu()(and the difference between usingnr_cpu_idsandnr_cpusdoesn't seem relevant here), so I'd recommend going with just the single earlier addition.If mask is invalid you can the first iteration with the bad i (from
cpumask_first, which doesn't have any checks), which may scribble off the end of the cnt array.
Immediately after the ASSERT() here there is an invocation of cpumask_test_cpu(), which itself uses cpumask_check(). That's what the added ASSERT() is redundant with.
Quoted 5 lines
I'm not sure why I wasn't hitting an assert from the subsequent
cpumask_next, but I wasn't.If you are still convinced this ASSERT isn't worth it I'll drop it, I've found my bug now and it'd be a pretty uncommon one to repeat...
Please do.
Jan
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this
in
Quoted 27 lines
Quoted 14 lines
the future.
Signed-off-by Ian Campbell <[email protected]>Cc Keir (Xen.org) <[email protected]>Cc Jan Beulich <[email protected]>--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)xen/common/domctl.c +0
-0xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t · line 196
Quoted 7 lines
xen/common/domctl.c +1
-0@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t*online) */ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);You may want to add another
ASSERT()here in case you care about the difference betweennr_cpusandnr_cpu_ids. Or move theASSERT()here if the difference is insignificant (as I would think), ascpumask_next()invokescpumask_check()(see also below).
Quoted 8 lines
Quoted 6 lines
It is possible to get through this code without calling
cpumask_next(cpu, ..)though, I think, due to the if.Perhaps
ASSERT(cpu < nr_cpu_ids);after the if would be the best option?
That's what I meant with "here" in my first response.
Sorry, I thought you meant inside the if, for some reason...
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
xen/common/domctl.c static unsigned int default_vcpu0_location(cpumask_t · line 199
Quoted 4 lines
xen/common/domctl.c +1
-0for_each_cpu (i, online) { +ASSERT (i < nr_cpus);This one is almost redundant with the one in
cpumask_check()called fromcpumask_test_cpu()(and the difference between usingnr_cpu_idsandnr_cpusdoesn't seem relevant here), so I'd recommend going with just the single earlier addition.
Quoted 7 lines
Quoted 3 lines
If mask is invalid you can the first iteration with the bad i (from
cpumask_first, which doesn't have any checks), which may scribble off the end of the cnt array.Immediately after the
ASSERT()here there is an invocation ofcpumask_test_cpu(), which itself usescpumask_check(). That's what the addedASSERT()is redundant with.
Ah, I was confused because it was on a different mask -- but of course for the purposes of that assert it doesn't matter which mask it is.
Quoted 7 lines
Quoted 5 lines
I'm not sure why I wasn't hitting an assert from the subsequent
cpumask_next, but I wasn't.If you are still convinced this ASSERT isn't worth it I'll drop it, I've found my bug now and it'd be a pretty uncommon one to repeat...
Please do.
Patch below.
My memory wrt heap corruption was faulty -- actually it was just that this function was returning a cpu > nr_cpu_ids which caused a later function to access an invalid per-cpu segment -- which just looked like heap corruption when I first started investigating it. I've updated the comment to reflect that.
8<---------------------------------------------------------------
Quoted 1 line
From
02d59965931509b9eb8e1e69504a0b09cc9765c5Mon Sep 17 00:00:00 2001
From: Ian Campbell <[email protected]>
Date: Tue, 26 Jun 2012 09:44:34 +0000
Subject: [PATCH] xen: add assertion in default_vcpu0_location to protect
against broken masks
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to return an invalid cpu number which caused errors later when we tried to access per_cpu data for that invalid cpu.
Add an assert to catch this in the future.
--- xen/common/domctl.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
xen/common/domctl.c +1 -0
@@ -192,6 +192,7 @@ static unsigned int default_vcpu0_location(cpumask_t *online) cpu =cpumask_first (&cpu_exclude_map);if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map); +ASSERT (cpu < nr_cpu_ids);for_each_cpu (i, online) {if (cpumask_test_cpu (i, &cpu_exclude_map) )
Signature · 1 line
1.7.9.1
Quoted 4 lines · Ian Campbell
When setting up the cpu sibling/etc masks on ARM I accidentally and incorrectly omitted a CPU from it's own sibling mask which caused this function to scribble over the heap. Add a couple of asserts to catch this in the future.
I'm happy with either or both the assertions you add.
Quoted 25 lines
Signed-off-by Ian Campbell <[email protected]>Cc Keir (Xen.org) <[email protected]>Cc Jan Beulich <[email protected]>--- xen/common/domctl.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)xen/common/domctl.c +2
-0@@ -190,10 +190,12 @@ static unsigned int default_vcpu0_location(cpumask_t*online) */ cpumask_copy (&cpu_exclude_map,per_cpu (cpu_sibling_mask,0 )); cpu =cpumask_first (&cpu_exclude_map); +ASSERT (cpu < nr_cpus);if (cpumask_weight (&cpu_exclude_map) >1 ) cpu =cpumask_next (cpu, &cpu_exclude_map);for_each_cpu (i, online) { +ASSERT (i < nr_cpus);if (cpumask_test_cpu (i, &cpu_exclude_map) )continue ;if ( (i == cpumask_first(per_cpu(cpu_sibling_mask, i))) &&