Skip to contentSkip to conversation

x86/mce: Fix buggy error path in cpu_bank_alloc() leading to UAF

PATCH In review Andrew Cooper 2 participants 2 messages Mar 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mce: Fix buggy error path in cpu_bank_alloc() leading to UAF

When cleaning up from a mcabanks_alloc() failure, the memory is freed but stale pointers are left in the percpu variables.

Use cpu_bank_free() which is idempotent and behaves correctly.

Fixes 2e6c8f182c9c ("x86: distinguish CPU offlining from CPU removal")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

2e6c8f182c9c updated the success path but missed the associated error path.

Prior to that, the paths were at least consistent since their introduction in commit 78c579426fb5 ("x86/MCE: Implement clearbank callback for AMD").

---
 xen/arch/x86/cpu/mcheck/mce.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/cpu/mcheck/mce.c +1 -2
index 9a91807cfb33..684871b216a4 100644
@@ -694,8 +694,7 @@ static int cpu_bank_alloc(unsigned int cpu)
 
     if ( !poll || !clr )
     {
-        mcabanks_free(poll);
-        mcabanks_free(clr);
+        cpu_bank_free(cpu);
         return -ENOMEM;
     }
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 5 lines · Andrew Cooper

When cleaning up from a mcabanks_alloc() failure, the memory is freed but stale pointers are left in the percpu variables.

Use cpu_bank_free() which is idempotent and behaves correctly.

Quoted 7 lines
Fixes 2e6c8f182c9c ("x86: distinguish CPU offlining from CPU removal")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

2e6c8f182c9c updated the success path but missed the associated error path.

I don't think so, see below.

Quoted 11 lines

Prior to that, the paths were at least consistent since their introduction in commit 78c579426fb5 ("x86/MCE: Implement clearbank callback for AMD").

---
 xen/arch/x86/cpu/mcheck/mce.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/cpu/mcheck/mce.c +0 -0
index 9a91807cfb33..684871b216a4 100644

xen/arch/x86/cpu/mcheck/mce.c static int cpu_bank_alloc(unsigned int cpu) · line 697

Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce.c +1 -2
@@ -694,8 +694,7 @@ static int cpu_bank_alloc(unsigned int cpu)
 
     if ( !poll || !clr )
     {
-        mcabanks_free(poll);
-        mcabanks_free(clr);
+        cpu_bank_free(cpu);

But this way we'll leak the allocation that may have succeeded, as the per-CPU data is updated only afterwards. Yet it's those per-CPU items which cpu_bank_free() reads.

I don't think there's any bug to fix here: Neither of the two pointers is allocated anywhere else, so the per-CPU slots are always both NULL or both non-NULL.

Jan