Re: [tip:x86/boot] x86/mm: Enable KASLR for physical mapping memory regions

From: Borislav Petkov
Date: Wed Aug 17 2016 - 07:33:41 EST


On Wed, Aug 17, 2016 at 12:19:48PM +0200, Ingo Molnar wrote:
> So I really hate this pattern, and we already have it in
> arch/x86/kernel/cpu/microcode/intel.c as well:
>
> start += PAGE_OFFSET - __PAGE_OFFSET_BASE;
>
> and note that it's not #ifdefed there - I think it's safe to leave out the #ifdef?

Ah, yes, we got rid of the ifdeffery in:

4a1a8e1b8f9f ("x86/asm, x86/microcode: Add __PAGE_OFFSET_BASE define on 32-bit")

So here's v2:

---
From: Borislav Petkov <bp@xxxxxxx>
Date: Wed, 17 Aug 2016 08:23:29 +0200
Subject: [PATCH] x86/microcode/AMD: Fix initrd loading with
CONFIG_RANDOMIZE_MEMORY=y

Similar to

efaad554b4ff ("x86/microcode/intel: Fix initrd loading with CONFIG_RANDOMIZE_MEMORY=y")

fix microcode loading from the initrd on AMD by adding the randomization
offset to the microcode patch container within the initrd.

Reported-and-tested-by: Brian Gerst <brgerst@xxxxxxxxx>
Signed-off-by: Borislav Petkov <bp@xxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
---
arch/x86/kernel/cpu/microcode/amd.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 27a0228c9cae..b816971f5da4 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -355,6 +355,7 @@ void load_ucode_amd_ap(void)
unsigned int cpu = smp_processor_id();
struct equiv_cpu_entry *eq;
struct microcode_amd *mc;
+ u8 *cont = container;
u32 rev, eax;
u16 eq_id;

@@ -371,8 +372,11 @@ void load_ucode_amd_ap(void)
if (check_current_patch_level(&rev, false))
return;

+ /* Add CONFIG_RANDOMIZE_MEMORY offset. */
+ cont += PAGE_OFFSET - __PAGE_OFFSET_BASE;
+
eax = cpuid_eax(0x00000001);
- eq = (struct equiv_cpu_entry *)(container + CONTAINER_HDR_SZ);
+ eq = (struct equiv_cpu_entry *)(cont + CONTAINER_HDR_SZ);

eq_id = find_equiv_id(eq, eax);
if (!eq_id)
@@ -434,6 +438,9 @@ int __init save_microcode_in_initrd_amd(void)
else
container = cont_va;

+ /* Add CONFIG_RANDOMIZE_MEMORY offset. */
+ container += PAGE_OFFSET - __PAGE_OFFSET_BASE;
+
eax = cpuid_eax(0x00000001);
eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);

--
2.8.4

--
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--