Commit 94838d230a6c ("x86/microcode/AMD: Use the family,model,stepping encoded in the patch ID") made verify_equivalence_table() return early for Zen and newer CPUs because their containers no longer need the equivalence table. The early return happens before the table length in hdr[2] is checked against the container size, but the callers still use hdr[2] as a byte offset: parse_container() advances buf and shrinks size by hdr[2] + CONTAINER_HDR_SZ, and the late loader does the same with the offset returned by install_equiv_cpu_table(). A container whose hdr[2] is larger than the file therefore underflows the size_t and makes the loader parse memory past the end of the buffer. On a Zen system a crafted kernel/x86/microcode/AuthenticAMD.bin in the initrd panics the kernel in save_microcode_in_initrd() before init runs: Oops: BUG: unable to handle page fault for address: ffffed101fffbf8f #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 7ff03067 P4D 7ff03067 PUD 7ff02067 PMD 0 Oops: Oops: 0000 [#1] SMP KASAN NOPTI CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 RIP: 0010:__asan_load4+0x50/0xa0 Code: 00 00 00 00 80 ff ff 48 39 c7 72 43 48 8d 47 03 48 89 c2 83 e2 07 48 83 fa 02 76 1c 48 b9 00 00 00 00 00 fc ff df 48 c1 e8 03 <0f> b6 04 08 84 c0 75 3e 5d e9 62 7b b3 01 48 b9 00 00 00 00 00 fc RSP: 0018:ffff8880080dfb48 EFLAGS: 00000203 RAX: 1ffff1101fffbf8f RBX: ffff8880fffdfc7c RCX: dffffc0000000000 RDX: 0000000000000007 RSI: ffffffff80000034 RDI: ffff8880fffdfc7c RBP: ffff8880080dfb48 R08: ffffffff8129f15f R09: ffffed100101bf9d R10: ffff8880080dfcef R11: 0000000000000001 R12: ffffffff80000034 R13: ffff8880080dfbe0 R14: 0000000000000017 R15: ffff8880080dfbe0 FS: 0000000000000000(0000) GS:ffff8880e7930000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffed101fffbf8f CR3: 000000000448e000 CR4: 00000000000006f0 Call Trace: verify_patch+0x3f/0x1e0 parse_container+0x237/0x2f0 ? __pfx_parse_container+0x10/0x10 ? microcode_loader_disabled+0x77/0xe0 save_microcode_in_initrd+0x19d/0x2e0 ? __pfx_save_microcode_in_initrd+0x10/0x10 ? trace_irq_enable+0xe8/0x110 ? _raw_spin_unlock_irqrestore+0x35/0x60 ? rcu_read_lock_any_held+0x80/0xb0 ? __cpuhp_setup_state+0x167/0x1f0 ? __pfx_cache_ap_register+0x10/0x10 ? __pfx_save_microcode_in_initrd+0x10/0x10 do_one_initcall+0xca/0x420 ? __pfx_do_one_initcall+0x10/0x10 ? paint_ptr+0x3d/0x70 ? kmemleak_not_leak+0x2e/0x40 ? __register_sysctl_init+0x36/0x60 ? rcu_is_watching+0x25/0x60 kernel_init_freeable+0x1d8/0x420 ? __pfx_kernel_init+0x10/0x10 kernel_init+0x29/0x1f0 ret_from_fork+0x3ec/0x550 ? __pfx_ret_from_fork+0x10/0x10 ? __asan_storeN+0x16/0x20 ? __switch_to+0x2e4/0x6d0 ? __pfx_kernel_init+0x10/0x10 ret_from_fork_asm+0x1a/0x30 Modules linked in: CR2: ffffed101fffbf8f ---[ end trace 0000000000000000 ]--- RIP: 0010:__asan_load4+0x50/0xa0 Code: 00 00 00 00 80 ff ff 48 39 c7 72 43 48 8d 47 03 48 89 c2 83 e2 07 48 83 fa 02 76 1c 48 b9 00 00 00 00 00 fc ff df 48 c1 e8 03 <0f> b6 04 08 84 c0 75 3e 5d e9 62 7b b3 01 48 b9 00 00 00 00 00 fc RSP: 0018:ffff8880080dfb48 EFLAGS: 00000203 RAX: 1ffff1101fffbf8f RBX: ffff8880fffdfc7c RCX: dffffc0000000000 RDX: 0000000000000007 RSI: ffffffff80000034 RDI: ffff8880fffdfc7c RBP: ffff8880080dfb48 R08: ffffffff8129f15f R09: ffffed100101bf9d R10: ffff8880080dfcef R11: 0000000000000001 R12: ffffffff80000034 R13: ffff8880080dfbe0 R14: 0000000000000017 R15: ffff8880080dfbe0 FS: 0000000000000000(0000) GS:ffff8880e7930000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffed101fffbf8f CR3: 000000000448e000 CR4: 00000000000006f0 note: swapper/0[1] exited with irqs disabled Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 ]--- Move the size check in front of the family test so that the table length is validated for every family. The type field and the minimum table size remain pre-Zen checks only. Fixes: 94838d230a6c ("x86/microcode/AMD: Use the family,model,stepping encoded in the patch ID") Cc: stable@vger.kernel.org Signed-off-by: Xingyuan Mo --- arch/x86/kernel/cpu/microcode/amd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c index 6cdc410e75471..7b627848959dc 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -398,6 +398,14 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size) if (!verify_container(buf, buf_size)) return false; + buf_size -= CONTAINER_HDR_SZ; + + equiv_tbl_len = hdr[2]; + if (buf_size < equiv_tbl_len) { + ucode_dbg("Truncated equivalence table.\n"); + return false; + } + /* Zen and newer do not need an equivalence table. */ if (x86_family(bsp_cpuid_1_eax) >= 0x17) return true; @@ -409,11 +417,7 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size) return false; } - buf_size -= CONTAINER_HDR_SZ; - - equiv_tbl_len = hdr[2]; - if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) || - buf_size < equiv_tbl_len) { + if (equiv_tbl_len < sizeof(struct equiv_cpu_entry)) { ucode_dbg("Truncated equivalence table.\n"); return false; } -- 2.43.0