Skip to contentSkip to conversation

x86/ucode/amd: Fix OoB read in cpu_request_microcode()

PATCH In review Andrew Cooper 2 participants 3 messages Feb 9, 2021 – Feb 10, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/ucode/amd: Fix OoB read in cpu_request_microcode()

verify_patch_size() is a maximum size check, and doesn't have a minimum bound.

If the microcode container encodes a blob with a length less than 64 bytes, the subsequent calls to microcode_fits()/compare_header() may read off the end of the buffer.

Fixes 4de936a38a ("x86/ucode/amd: Rework parsing logic in cpu_request_microcode()")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Ian Jackson <[email protected]>

In practice, processor_rev_id is the only field read, which is 2 bytes at offset 24 into the header. Not that this makes the bug any less bad.

For 4.15. Only dom0 can load new microcode, hence no XSA, but the bug is bad and the fix simple and obvious.

---
 xen/arch/x86/cpu/microcode/amd.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/cpu/microcode/amd.c +1 -0
index c4ab395799..cf5947389f 100644
@@ -349,6 +349,7 @@ static struct microcode_patch *cpu_request_microcode(const void *buf, size_t siz
             if ( size < sizeof(*mc) ||
                  (mc = buf)->type != UCODE_UCODE_TYPE ||
                  size - sizeof(*mc) < mc->len ||
+                 mc->len < sizeof(struct microcode_patch) ||
                  (!skip_ucode && !verify_patch_size(mc->len)) )
             {
                 printk(XENLOG_ERR "microcode: Bad microcode data\n");
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

verify_patch_size() is a maximum size check, and doesn't have a minimum bound.

If the microcode container encodes a blob with a length less than 64 bytes, the subsequent calls to microcode_fits()/compare_header() may read off the end of the buffer.

Fixes 4de936a38a ("x86/ucode/amd: Rework parsing logic in cpu_request_microcode()")

xen/arch/x86/cpu/microcode/amd.c static struct microcode_patch *cpu_request_microcode(const void *buf, size_t siz · line 352

Quoted 7 lines
xen/arch/x86/cpu/microcode/amd.c +1 -0
@@ -349,6 +349,7 @@ static struct microcode_patch *cpu_request_microcode(const void *buf, size_t siz
             if ( size < sizeof(*mc) ||
                  (mc = buf)->type != UCODE_UCODE_TYPE ||
                  size - sizeof(*mc) < mc->len ||
+                 mc->len < sizeof(struct microcode_patch) ||

I was inclined to suggest to use <= here, but I guess a blob with 1 byte of data is as bogus as one with 0 bytes of data.

Jan

#2 lore raw
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

verify_patch_size() is a maximum size check, and doesn't have a minimum bound.

If the microcode container encodes a blob with a length less than 64 bytes, the subsequent calls to microcode_fits()/compare_header() may read off the end of the buffer.

Fixes 4de936a38a ("x86/ucode/amd: Rework parsing logic in cpu_request_microcode()")

Thanks.

Quoted 10 lines

xen/arch/x86/cpu/microcode/amd.c static struct microcode_patch *cpu_request_microcode(const void *buf, size_t siz · line 352

Quoted 8 lines
xen/arch/x86/cpu/microcode/amd.c +1 -0
@@ -349,6 +349,7 @@ static struct microcode_patch *cpu_request_microcode(const void *buf, size_t siz
             if ( size < sizeof(*mc) ||
                  (mc = buf)->type != UCODE_UCODE_TYPE ||
                  size - sizeof(*mc) < mc->len ||
+                 mc->len < sizeof(struct microcode_patch) ||

I was inclined to suggest to use <= here, but I guess a blob with 1 byte of data is as bogus as one with 0 bytes of data.

Yeah - its unfortunate.  On the Intel side, we've got a fairly rigorous set of sanity checks we can perform on the blob, and on the AMD side we appear to have nothing.

This at least prevents our minimal header checks from causing OoB accesses.

~Andrew