Skip to content

Commit 712f210

Browse files
keessuryasaimadhu
authored andcommitted
x86/microcode/AMD: Track patch allocation size explicitly
In preparation for reducing the use of ksize(), record the actual allocation size for later memcpy(). This avoids copying extra (uninitialized!) bytes into the patch buffer when the requested allocation size isn't exactly the size of a kmalloc bucket. Additionally, fix potential future issues where runtime bounds checking will notice that the buffer was allocated to a smaller value than returned by ksize(). Fixes: 757885e ("x86, microcode, amd: Early microcode patch loading support for AMD") Suggested-by: Daniel Micay <danielmicay@gmail.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Borislav Petkov <bp@suse.de> Link: https://lore.kernel.org/lkml/CA+DvKQ+bp7Y7gmaVhacjv9uF6Ar-o4tet872h4Q8RPYPJjcJQA@mail.gmail.com/
1 parent 7fce8d6 commit 712f210

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

arch/x86/include/asm/microcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
struct ucode_patch {
1010
struct list_head plist;
1111
void *data; /* Intel uses only this one */
12+
unsigned int size;
1213
u32 patch_id;
1314
u16 equiv_cpu;
1415
};

arch/x86/kernel/cpu/microcode/amd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
788788
kfree(patch);
789789
return -EINVAL;
790790
}
791+
patch->size = *patch_size;
791792

792793
mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
793794
proc_id = mc_hdr->processor_rev_id;
@@ -869,7 +870,7 @@ load_microcode_amd(bool save, u8 family, const u8 *data, size_t size)
869870
return ret;
870871

871872
memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
872-
memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE));
873+
memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE));
873874

874875
return ret;
875876
}

0 commit comments

Comments
 (0)