Skip to contentSkip to conversation

kaslr: check if kernel location is changed

PATCH RESEND In review Baoquan He 4 participants 25 messages Sep 30, 2014 – Feb 9, 2015

Subsystems (5)
lore
Share & tools
lore

Baoquan He

author
#0 lore raw

PATCH RESEND v3 1/2 kaslr: check if kernel location is changed

Function handle_relocations() is used to do the relocations handling for i686 and kaslr of x86_64. For 32 bit the relocation handling is mandotary to perform. For x86_64 only when kaslr is enabled and a random kernel location is chosen successfully the relocation handling shound be done. However previous implementation only compared the kernel loading address and LOAD_PHYSICAL_ADDR where kernel were compiled to run at. This would casue system to be exceptional in few conditions like when delta between load address and compiled address is bigger than what 32bit signed relocations can handle. Also there will be limitations that delta can't be too big otherwise kernel text virtual addresses will overflow in module address space.

So in this patch check if kernel location is changed after choose_kernel_location() when x86_64. If and only if in x86_64 and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.

---
 arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/misc.c +22 -4
index 57ab74d..3bb2a17 100644
@@ -230,8 +230,9 @@ static void error(char *x)
 		asm("hlt");
 }
 
-#if CONFIG_X86_NEED_RELOCS
-static void handle_relocations(void *output, unsigned long output_len)
+#ifdef CONFIG_X86_NEED_RELOCS
+static void handle_relocations(void *output_orig, void *output,
+			       unsigned long output_len)
 {
 	int *reloc;
 	unsigned long delta, map, ptr;
@@ -239,6 +240,20 @@ static void handle_relocations(void *output, unsigned long output_len)
 	unsigned long max_addr = min_addr + output_len;
 
 	/*
+	* 32bit always requires relocations to be performed. For x86_64,
+	* relocations need to be performed only if kaslr has chosen a
+	* different load address then kernel was originally loaded at.
+	*
+	* If we are here, either kaslr is not configured in or kaslr is disabled
+	* or kaslr has chosen not to change the load location of kernel. Don't
+	* perform any relocations.
+	*/
+#if CONFIG_X86_64
+	if (output_orig == output)
+		return;
+#endif
+
+	/*
 	 * Calculate the delta between where vmlinux was linked to load
 	 * and where it was actually loaded.
 	 */
@@ -299,7 +314,8 @@ static void handle_relocations(void *output, unsigned long output_len)
 #endif
 }
 #else
-static inline void handle_relocations(void *output, unsigned long output_len)
+static inline void handle_relocations(void *output_orig, void *output,
+				      unsigned long output_len)
 { }
 #endif
 
@@ -360,6 +376,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
 				  unsigned char *output,
 				  unsigned long output_len)
 {
+	unsigned char *output_orig = output;
+
 	real_mode = rmode;
 
 	sanitize_boot_params(real_mode);
@@ -402,7 +420,7 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
 	debug_putstr("\nDecompressing Linux... ");
 	decompress(input_data, input_len, NULL, NULL, output, NULL, error);
 	parse_elf(output);
-	handle_relocations(output, output_len);
+	handle_relocations(output_orig, output, output_len);
 	debug_putstr("done.\nBooting the kernel.\n");
 	return output;
 }
Signature · 1 line
1.8.5.3
author
#1 lore raw

PATCH RESEND v3 2/2 export the kernel image size KERNEL_IMAGE_SIZE

Now kaslr makes kernel image size changable, not the fixed size 512M. So KERNEL_IMAGE_SIZE need be exported to VMCOREINFO, otherwise makedumfile will crash.

---
 kernel/kexec.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kexec.c +3 -0
index 2bee072..bd680d3 100644
@@ -2003,6 +2003,9 @@ static int __init crash_save_vmcoreinfo_init(void)
 #endif
 	VMCOREINFO_NUMBER(PG_head_mask);
 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
+#ifdef CONFIG_X86
+       VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
+#endif
 #ifdef CONFIG_HUGETLBFS
 	VMCOREINFO_SYMBOL(free_huge_page);
 #endif
Signature · 7 lines
1.8.5.3


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#2 lore raw

Re: [resend Patch v3 2/2] export the kernel image size KERNEL_IMAGE_SIZE

Hi Thomas,

Could you please also merge this patch? Since you have merged this patch "x86, boot: Skip relocs when load address unchanged [commit f285f4a21]", and this issue was raised because it broke kexec/kdump, then I posted these 2 patches. Without this patch makedumpfile will be broken when enable kaslr and do the kdump.

Thanks
Baouqan

Quoted 17 lines · Baoquan He

Now kaslr makes kernel image size changable, not the fixed size 512M. So KERNEL_IMAGE_SIZE need be exported to VMCOREINFO, otherwise makedumfile will crash.

---
 kernel/kexec.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kexec.c +0 -0
index 2bee072..bd680d3 100644

kernel/kexec.c static int __init crash_save_vmcoreinfo_init(void) · line 2011

Quoted 9 lines
kernel/kexec.c +3 -0
@@ -2003,6 +2003,9 @@ static int __init crash_save_vmcoreinfo_init(void)
 #endif
 	VMCOREINFO_NUMBER(PG_head_mask);
 	VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
+#ifdef CONFIG_X86
+       VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
+#endif
 #ifdef CONFIG_HUGETLBFS
 	VMCOREINFO_SYMBOL(free_huge_page);
 #endif

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw

Re: [resend Patch v3 2/2] export the kernel image size KERNEL_IMAGE_SIZE

Ping?

Can someone pull this into x86 tip?

Thanks!

-Kees

Quoted 40 lines · Baoquan He

Hi Thomas,

Could you please also merge this patch? Since you have merged this patch "x86, boot: Skip relocs when load address unchanged [commit f285f4a21]", and this issue was raised because it broke kexec/kdump, then I posted these 2 patches. Without this patch makedumpfile will be broken when enable kaslr and do the kdump.

Thanks
Baouqan

Quoted 26 lines · Baoquan He

Now kaslr makes kernel image size changable, not the fixed size 512M. So KERNEL_IMAGE_SIZE need be exported to VMCOREINFO, otherwise makedumfile will crash.

---
 kernel/kexec.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kexec.c +3 -0
index 2bee072..bd680d3 100644
@@ -2003,6 +2003,9 @@ static int __init crash_save_vmcoreinfo_init(void)
 #endif
      VMCOREINFO_NUMBER(PG_head_mask);
      VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
+#ifdef CONFIG_X86
+       VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
+#endif
 #ifdef CONFIG_HUGETLBFS
      VMCOREINFO_SYMBOL(free_huge_page);
 #endif
Signature · 2 lines
Kees Cook
Chrome OS Security
#4 lore raw
In reply to Baoquan He
Quoted 17 lines · Baoquan He

Function handle_relocations() is used to do the relocations handling for i686 and kaslr of x86_64. For 32 bit the relocation handling is mandotary to perform. For x86_64 only when kaslr is enabled and a random kernel location is chosen successfully the relocation handling shound be done. However previous implementation only compared the kernel loading address and LOAD_PHYSICAL_ADDR where kernel were compiled to run at. This would casue system to be exceptional in few conditions like when delta between load address and compiled address is bigger than what 32bit signed relocations can handle. Also there will be limitations that delta can't be too big otherwise kernel text virtual addresses will overflow in module address space.

So in this patch check if kernel location is changed after choose_kernel_location() when x86_64. If and only if in x86_64 and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.

Quoted 5 lines

Could you clarify under what conditions we may end up with 32-bit signed overflow, and yet have a functional kernel?

-hpa

#5 lore raw
Quoted 18 lines · H. Peter Anvin
Quoted 17 lines · Baoquan He

Function handle_relocations() is used to do the relocations handling for i686 and kaslr of x86_64. For 32 bit the relocation handling is mandotary to perform. For x86_64 only when kaslr is enabled and a random kernel location is chosen successfully the relocation handling shound be done. However previous implementation only compared the kernel loading address and LOAD_PHYSICAL_ADDR where kernel were compiled to run at. This would casue system to be exceptional in few conditions like when delta between load address and compiled address is bigger than what 32bit signed relocations can handle. Also there will be limitations that delta can't be too big otherwise kernel text virtual addresses will overflow in module address space.

So in this patch check if kernel location is changed after choose_kernel_location() when x86_64. If and only if in x86_64 and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.

Quoted 8 lines
Quoted 5 lines

Could you clarify under what conditions we may end up with 32-bit signed overflow, and yet have a functional kernel?

Hi Peter,

I think there is some confusion. I will try to clarify.

If we have 32bit signed overflow, we will not have a functional kernel. And that's the message we get when we try to kexec with CONFIG_RANDOMIZE_BASE=y.

**********************************************************************

[  340.709078] kexec: Starting new kernel

early console in decompress_kernel
KASLR disabled by default...

Decompressing Linux... Parsing ELF...

Performing relocations...
32-bit relocation outside of kernel!

-- System halted
*****************************************************************

We realized that kexec tries to load kernel at higher physical addresses and that can lead to problmes.

Currently for x86_64, handle_relocations() will perform relocations if kernel is not loaded at LOAD_PHYSICAL_ADDR. I think this does not work for all the cases and kerenl can not be loaded anywhere in the physical address space. And that's why we run into issues with kexec.

My understanding is that we introduce handle_relcoations() i386 style because of RANDOMIZE_BASE. If that's the case, one possible solution is that perform relocations only if ranodmize base logic has chosen a different load location for kernel than where boot loader loaded it. Otherwise don't do anything.

In case of kexec/kdump, we will pass "nokaslr" to second kernel forcing it to do nothing and let the kernel run where it was loaded by bootloader. And in that case handle_relocations() should not do any relocations and that should allow kernel to be loaded anywhere in physical memory on x86_64.

Thanks
Vivek

#6 lore raw
Quoted 9 lines · Vivek Goyal

Hi Peter,

I think there is some confusion. I will try to clarify.

If we have 32bit signed overflow, we will not have a functional kernel. And that's the message we get when we try to kexec with CONFIG_RANDOMIZE_BASE=y.

And how does that happen?

Quoted 28 lines

**********************************************************************

[  340.709078] kexec: Starting new kernel

early console in decompress_kernel
KASLR disabled by default...

Decompressing Linux... Parsing ELF...

Performing relocations...
32-bit relocation outside of kernel!

-- System halted
*****************************************************************

We realized that kexec tries to load kernel at higher physical addresses and that can lead to problmes.

Currently for x86_64, handle_relocations() will perform relocations if kernel is not loaded at LOAD_PHYSICAL_ADDR. I think this does not work for all the cases and kerenl can not be loaded anywhere in the physical address space. And that's why we run into issues with kexec.

My understanding is that we introduce handle_relcoations() i386 style because of RANDOMIZE_BASE. If that's the case, one possible solution is that perform relocations only if ranodmize base logic has chosen a different load location for kernel than where boot loader loaded it. Otherwise don't do anything.

Quoted 6 lines

In case of kexec/kdump, we will pass "nokaslr" to second kernel forcing it to do nothing and let the kernel run where it was loaded by bootloader. And in that case handle_relocations() should not do any relocations and that should allow kernel to be loaded anywhere in physical memory on x86_64.

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

-hpa

#7 lore raw
Quoted 4 lines · H. Peter Anvin
Quoted 3 lines · Vivek Goyal

Hi Peter,

Quoted 8 lines
Quoted 6 lines

I think there is some confusion. I will try to clarify.

If we have 32bit signed overflow, we will not have a functional kernel. And that's the message we get when we try to kexec with CONFIG_RANDOMIZE_BASE=y.

And how does that happen?

I compile a kernel for physical address 16MB (CONFIG_PHYSICAL_START=0x1000000). And kexec loads this kernel at physical address above 16GB (0x000000042e000000).

When we boot into second kernel it tries to perform relocations and fails as follows. I have printed bunch of variables in handle_relocations(), so referring to code will help.

min_addr=0x000000042e000000 (Physical address where decompressed kernel is
loaded).

delta=0x000000042d000000 (Difference between load and compile addr).

map=0x00000004ad000000 (map = delta - __START_KERNEL_map)

Now we start processing 32bit relocations and read first reloc.

extended=0x81e819c2 (extended = *reloc)

We add map to it and new value of extended is.

extended=0x2ee819c2 (extended += map)

Now we convert this to actual 64bit address which relocation needs to be applied and ptr value is.

ptr = 0x000000002ee819c2 (ptr = (unsigned long)extended;)

And this address is outside the range of where kernel is actually loaded. (0x000000042e000000). So we ended up with a wrong address to patch hence following check fails.

if (ptr < min_addr || ptr > max_addr)

	error("32-bit relocation outside of kernel!\n");
Quoted 36 lines
Quoted 29 lines

**********************************************************************

[  340.709078] kexec: Starting new kernel

early console in decompress_kernel
KASLR disabled by default...

Decompressing Linux... Parsing ELF...

Performing relocations...
32-bit relocation outside of kernel!

-- System halted
*****************************************************************

We realized that kexec tries to load kernel at higher physical addresses and that can lead to problmes.

Currently for x86_64, handle_relocations() will perform relocations if kernel is not loaded at LOAD_PHYSICAL_ADDR. I think this does not work for all the cases and kerenl can not be loaded anywhere in the physical address space. And that's why we run into issues with kexec.

My understanding is that we introduce handle_relcoations() i386 style because of RANDOMIZE_BASE. If that's the case, one possible solution is that perform relocations only if ranodmize base logic has chosen a different load location for kernel than where boot loader loaded it. Otherwise don't do anything.

Quoted 6 lines

In case of kexec/kdump, we will pass "nokaslr" to second kernel forcing it to do nothing and let the kernel run where it was loaded by bootloader. And in that case handle_relocations() should not do any relocations and that should allow kernel to be loaded anywhere in physical memory on x86_64.

Quoted 4 lines

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

What am I missing?

Thanks
Vivek

author
#8 lore raw
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

Thanks
Baoquan

#9 lore raw
Quoted 30 lines · Baoquan He
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

Quoted 3 lines

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

-hpa

author
#10 lore raw
Quoted 31 lines · H. Peter Anvin
Quoted 30 lines · Baoquan He
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

Quoted 8 lines
Quoted 3 lines

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

Yes, this shouldn't happen this way on x86_64. With this patch, those are fixed as expected. If kernel location is not chosen randomly, we should not do the relocations handling. If and only if kaslr is enabled and it relocated the kernel randomly as expected, we do the relocations handling.

I think this patch really makes sense and it's simple and won't impact i386 and other implementations.

Thanks
Baoquan

author
#11 lore raw
Quoted 32 lines · Baoquan He
Quoted 31 lines · H. Peter Anvin
Quoted 30 lines · Baoquan He
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

Btw, except of this bug, I didn't find other risk of kaslr currently. The code flow is straightforward and clear.

Quoted 10 lines

Yes, this shouldn't happen this way on x86_64. With this patch, those are fixed as expected. If kernel location is not chosen randomly, we should not do the relocations handling. If and only if kaslr is enabled and it relocated the kernel randomly as expected, we do the relocations handling.

I think this patch really makes sense and it's simple and won't impact i386 and other implementations.

#12 lore raw
In reply to H. Peter Anvin
Quoted 31 lines · H. Peter Anvin
Quoted 30 lines · Baoquan He
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

Quoted 7 lines
Quoted 3 lines

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Thanks
Vivek

#13 lore raw
Quoted 40 lines · Vivek Goyal
Quoted 31 lines · H. Peter Anvin
Quoted 30 lines · Baoquan He
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin

Sorry... this makes no sense.

For x86-64, there is no direct connection between the physical and virtual address spaces that the kernel runs in...

I am sorry I did not understand this one. I thought that initial relocatable kernel implementaion did not have any direct connection between virtual and physical address. One could load kernel anywhere and kernel virtual address will not change and we will just adjust page tables to map virtual address to right physical address.

Quoted 5 lines

Now handle_relocation() stuff seems to introduce a close coupling between physical and virtual address. So if kernel shifts by 16MB in physical address space, then it will shift by equal amount in virtual address space. So there seems to be a direct connection between virtual and physical address space in this case.

Yeah, it's exactly as Vivek said.

Before kaslr was introduced, x86_64 kernel can be put anywhere, and always _text is 0xffffffff81000000. Meanwhile phys_base contains the offset between the compiled addr (namely 0x1000000) and kernel loaded addr. After kaslr implementation was added, as long as kernel loaded addr is different 0x1000000, it will call handle_relocations(). The offset now is added onto each symbols including _text and phys_base becomes 0.

Quoted 7 lines
Quoted 3 lines

It's clearly showing that by checking /proc/kallsyms and value of phys_base.

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

Quoted 6 lines

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

Thanks
Vivek

#14 lore raw
Quoted 9 lines · Vivek Goyal
Quoted 9 lines
Quoted 4 lines

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

Quoted 9 lines
Quoted 2 lines

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is pretty darn pointless. Without the physical address shift the 1:1 map can be used, and again, kaslr becomes pointless. However, there is absolutely no reason why they should be coupled. They can, in fact, be independently randomized.

-hpa

#15 lore raw
Quoted 20 lines · H. Peter Anvin
Quoted 9 lines · Vivek Goyal
Quoted 9 lines
Quoted 4 lines

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

Quoted 9 lines
Quoted 2 lines

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

Quoted 5 lines

I would assume that without a virtual address shift kaslr is pretty darn pointless. Without the physical address shift the 1:1 map can be used, and again, kaslr becomes pointless. However, there is absolutely no reason why they should be coupled. They can, in fact, be independently randomized.

Agreed. On x86_64, we should be able to randomize virtual address space and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

Thanks
Vivek

#16 lore raw
Quoted 27 lines · Vivek Goyal
Quoted 20 lines · H. Peter Anvin
Quoted 9 lines · Vivek Goyal
Quoted 9 lines
Quoted 4 lines

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

Quoted 9 lines
Quoted 2 lines

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

Quoted 5 lines

I would assume that without a virtual address shift kaslr is pretty darn pointless. Without the physical address shift the 1:1 map can be used, and again, kaslr becomes pointless. However, there is absolutely no reason why they should be coupled. They can, in fact, be independently randomized.

Quoted 4 lines

Agreed. On x86_64, we should be able to randomize virtual address space and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

Hi Peter,

So what do we do about this issue in short term to make kexec work. Even if we go for above solution, to make kexec work we will have to pass "nokaslr" as we don't want kernel to move around in physical address space as it might stomp over ELF headers we have stored.

If you don't like current patch, should we just disable relocations in x86_64 if "nokaslr" command line is passed. That way kernel will not be moved in physical as well as virtual address space.

Thanks
Vivek

author
#17 lore raw
Quoted 28 lines · Vivek Goyal
Quoted 27 lines · Vivek Goyal
Quoted 25 lines · H. Peter Anvin
Quoted 9 lines · Vivek Goyal
Quoted 9 lines
Quoted 4 lines

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

Quoted 9 lines
Quoted 2 lines

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is pretty darn pointless. Without the physical address shift the 1:1 map can be used, and again, kaslr becomes pointless. However, there is absolutely no reason why they should be coupled. They can, in fact, be independently randomized.

Quoted 4 lines
Quoted 4 lines

Agreed. On x86_64, we should be able to randomize virtual address space and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then phys_base still stores an relative value, a different offset than before.

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page frame. This can make physical addr be randomized to anywhere.

So now there may be 3 options:

1) Fix this bug in current kaslr. Since when randomize the new kernel location in choose_kernel_location(), cmdline options has been checked strictly, e.g if nokaslr is specified, it's safe to do the kernel location randomization. Then in handle_relocations(), we only need to check if the kernel location is changed, comparing with kernel loaded addr. If changed, kaslr is done, let's do the relocation handling. If not changed, no kaslr id done, just skip the relocation handling like before.

2) randomize the virtual addr space and physical addr space independently. But physical addr space must be below 4G.

3) extend the identity mapping to 48bit of addr space. Then we can randomized the virtual addr space in (0x1000000, 1G) and physical addr space in (0x1000000, real physical memory end).

If option 3 is doable, it's the best. If not, I think bug fix should be better.

Quoted 7 lines

Hi Peter,

So what do we do about this issue in short term to make kexec work. Even if we go for above solution, to make kexec work we will have to pass "nokaslr" as we don't want kernel to move around in physical address space as it might stomp over ELF headers we have stored.

kexec doesn't need ELF headers. Kdump may need it. But in current kexec-tools implementation, kernel/initrd and other stuffs are placed from top to down, current implementation won't do kaslr since it only happened between kernel loaded addr and 1G. So we don't need to worry about the stomping.

Quoted 7 lines

If you don't like current patch, should we just disable relocations in x86_64 if "nokaslr" command line is passed. That way kernel will not be moved in physical as well as virtual address space.

Thanks
Vivek

#18 lore raw
Quoted 41 lines · Baoquan He
Quoted 28 lines · Vivek Goyal
Quoted 27 lines · Vivek Goyal
Quoted 25 lines · H. Peter Anvin
Quoted 9 lines · Vivek Goyal
Quoted 9 lines
Quoted 4 lines

This really shouldn't have happened this way on x86-64. It has to happen this way on i386, but I worry that this may be a serious misdesign in kaslr on x86-64. I'm also wondering if there is any other fallout of this?

I agree. On x86_64, we should stick to previous design and this new logic of performing relocations does not sound very clean and makes things very confusing.

Quoted 9 lines
Quoted 2 lines

I am wondering that why couldn't we simply adjust page tables in case of kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual address kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for kaslr or virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is pretty darn pointless. Without the physical address shift the 1:1 map can be used, and again, kaslr becomes pointless. However, there is absolutely no reason why they should be coupled. They can, in fact, be independently randomized.

Quoted 4 lines
Quoted 4 lines

Agreed. On x86_64, we should be able to randomize virtual address space and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then phys_base still stores an relative value, a different offset than before.

Quoted 4 lines

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page frame. This can make physical addr be randomized to anywhere.

I am wondering where does this 4G limit come from? Without kaslr now we are able to load kernels much higher than 4G. That would suggest that we should be able to pick randomly any address above 4G too?

Thanks
Vivek

#19 lore raw
In reply to Baoquan He

I don't see why we can't randomize anywhere in physical space. We already handle the kernel above 4 GB and it wouldn't be hard to do the equivalent for the decompress/relocation code, using a #PF handler. Not all CPUs support 1 GB pages.

Quoted 107 lines · Baoquan He
Quoted 5 lines · Vivek Goyal
Quoted 4 lines · Vivek Goyal
Quoted 3 lines · H. Peter Anvin
Quoted 2 lines · Vivek Goyal
Quoted 2 lines
Quoted 2 lines

This really shouldn't have happened this way on x86-64. It

has to happen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

this way on i386, but I worry that this may be a serious

misdesign in kaslr

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

on x86-64. I'm also wondering if there is any other fallout

of this?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I agree. On x86_64, we should stick to previous design and

this new

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

logic of performing relocations does not sound very clean and

makes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

things very confusing.

I am wondering that why couldn't we simply adjust page tables

in case of

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual

address

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for

kaslr or

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is

pretty darn

Quoted 1 line
Quoted 1 line
Quoted 1 line

pointless. Without the physical address shift the 1:1 map can be

used,

Quoted 1 line
Quoted 1 line
Quoted 1 line

and again, kaslr becomes pointless. However, there is absolutely

no

Quoted 1 line
Quoted 1 line
Quoted 1 line

reason why they should be coupled. They can, in fact, be

independently

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

randomized.

Agreed. On x86_64, we should be able to randomize virtual address

space

Quoted 3 lines
Quoted 3 lines

and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then
phys_base still stores an relative value, a different offset than before.

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page
frame. This can make physical addr be randomized to anywhere.

So now there may be 3 options:

1) Fix this bug in current kaslr. Since when randomize the new kernel location in choose_kernel_location(), cmdline options has been checked strictly, e.g if nokaslr is specified, it's safe to do the kernel location randomization. Then in handle_relocations(), we only need to check if the kernel location is changed, comparing with kernel loaded addr. If changed, kaslr is done, let's do the relocation handling. If not changed, no kaslr id done, just skip the relocation handling like before.

2) randomize the virtual addr space and physical addr space independently. But physical addr space must be below 4G.

3) extend the identity mapping to 48bit of addr space. Then we can randomized the virtual addr space in (0x1000000, 1G) and physical addr space in (0x1000000, real physical memory end).

If option 3 is doable, it's the best. If not, I think bug fix should be better.

Quoted 4 lines

Hi Peter,

So what do we do about this issue in short term to make kexec work.

Even

Quoted 2 lines

if we go for above solution, to make kexec work we will have to pass "nokaslr" as we don't want kernel to move around in physical address

space

Quoted 1 line

as it might stomp over ELF headers we have stored.

kexec doesn't need ELF headers. Kdump may need it. But in current kexec-tools implementation, kernel/initrd and other stuffs are placed from top to down, current implementation won't do kaslr since it only happened between kernel loaded addr and 1G. So we don't need to worry about the stomping.

Quoted 2 lines

If you don't like current patch, should we just disable relocations

in

Quoted 5 lines

x86_64 if "nokaslr" command line is passed. That way kernel will not be moved in physical as well as virtual address space.

Thanks
Vivek

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
author
#20 lore raw
Quoted 2 lines · H. Peter Anvin

I don't see why we can't randomize anywhere in physical space. We already handle the kernel above 4 GB and it wouldn't be hard to do the equivalent for the decompress/relocation code, using a #PF handler. Not all CPUs support 1 GB pages.

Well, IIUC, in arch/x86/boot/compressed/head_64.S pgtable contains 6 page frames and is used to build 4G identity mapping. Then this pgtable is loaded into cr3. Later jump into 64bit mode. decompress/relocation code should work in this region. Isn't it right?

Currently kernel surely can be put anywhere, even above 4G. This have been done very often in kexec/kdump. However in kexec/kdump the identity mapping is built for all physical memory after jumping to startup_64 directly from purgatory, that's why kexec/kdump kernel can be put anywhere.

So for normal kernel, don't we need to extend the physical memory mappng to a larger region if we want to randomize anywhere in physical space?

Thanks
Baoquan

Quoted 111 lines · Baoquan He
Quoted 107 lines · Vivek Goyal
Quoted 5 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin
Quoted 3 lines · Vivek Goyal
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This really shouldn't have happened this way on x86-64. It

has to happen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

this way on i386, but I worry that this may be a serious

misdesign in kaslr

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

on x86-64. I'm also wondering if there is any other fallout

of this?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I agree. On x86_64, we should stick to previous design and

this new

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

logic of performing relocations does not sound very clean and

makes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

things very confusing.

I am wondering that why couldn't we simply adjust page tables

in case of

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual

address

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for

kaslr or

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is

pretty darn

Quoted 1 line
Quoted 1 line
Quoted 1 line

pointless. Without the physical address shift the 1:1 map can be

used,

Quoted 1 line
Quoted 1 line
Quoted 1 line

and again, kaslr becomes pointless. However, there is absolutely

no

Quoted 1 line
Quoted 1 line
Quoted 1 line

reason why they should be coupled. They can, in fact, be

independently

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

randomized.

Agreed. On x86_64, we should be able to randomize virtual address

space

Quoted 3 lines
Quoted 3 lines

and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then
phys_base still stores an relative value, a different offset than before.

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page
frame. This can make physical addr be randomized to anywhere.

So now there may be 3 options:

1) Fix this bug in current kaslr. Since when randomize the new kernel location in choose_kernel_location(), cmdline options has been checked strictly, e.g if nokaslr is specified, it's safe to do the kernel location randomization. Then in handle_relocations(), we only need to check if the kernel location is changed, comparing with kernel loaded addr. If changed, kaslr is done, let's do the relocation handling. If not changed, no kaslr id done, just skip the relocation handling like before.

2) randomize the virtual addr space and physical addr space independently. But physical addr space must be below 4G.

3) extend the identity mapping to 48bit of addr space. Then we can randomized the virtual addr space in (0x1000000, 1G) and physical addr space in (0x1000000, real physical memory end).

If option 3 is doable, it's the best. If not, I think bug fix should be better.

Quoted 4 lines

Hi Peter,

So what do we do about this issue in short term to make kexec work.

Even

Quoted 2 lines

if we go for above solution, to make kexec work we will have to pass "nokaslr" as we don't want kernel to move around in physical address

space

Quoted 1 line

as it might stomp over ELF headers we have stored.

kexec doesn't need ELF headers. Kdump may need it. But in current kexec-tools implementation, kernel/initrd and other stuffs are placed from top to down, current implementation won't do kaslr since it only happened between kernel loaded addr and 1G. So we don't need to worry about the stomping.

Quoted 2 lines

If you don't like current patch, should we just disable relocations

in

Quoted 5 lines

x86_64 if "nokaslr" command line is passed. That way kernel will not be moved in physical as well as virtual address space.

Thanks
Vivek

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
author
#21 lore raw
Quoted 4 lines · Baoquan He
Quoted 2 lines · H. Peter Anvin

I don't see why we can't randomize anywhere in physical space. We already handle the kernel above 4 GB and it wouldn't be hard to do the equivalent for the decompress/relocation code, using a #PF handler. Not all CPUs support 1 GB pages.

Quoted 8 lines

Well, IIUC, in arch/x86/boot/compressed/head_64.S pgtable contains 6 page frames and is used to build 4G identity mapping. Then this pgtable is loaded into cr3. Later jump into 64bit mode. decompress/relocation code should work in this region. Isn't it right?

Currently kernel surely can be put anywhere, even above 4G. This have been done very often in kexec/kdump. However in kexec/kdump the identity mapping is built for all physical memory after jumping to startup_64

~~~~~ should be before

Quoted 121 lines

directly from purgatory, that's why kexec/kdump kernel can be put anywhere.

So for normal kernel, don't we need to extend the physical memory mappng to a larger region if we want to randomize anywhere in physical space?

Thanks
Baoquan

Quoted 111 lines · Baoquan He
Quoted 107 lines · Vivek Goyal
Quoted 5 lines · Vivek Goyal
Quoted 4 lines · H. Peter Anvin
Quoted 3 lines · Vivek Goyal
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This really shouldn't have happened this way on x86-64. It

has to happen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

this way on i386, but I worry that this may be a serious

misdesign in kaslr

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

on x86-64. I'm also wondering if there is any other fallout

of this?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I agree. On x86_64, we should stick to previous design and

this new

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

logic of performing relocations does not sound very clean and

makes

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

things very confusing.

I am wondering that why couldn't we simply adjust page tables

in case of

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

kaslr on x86_64, instead of performing relocations.

Well, IIUC, if virtual addresses are shifted w.r.t what virtual

address

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

kernel was compiled for, then relocation will have to be done.

So question will be if physical address shift is enough for

kaslr or

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

virtual address shift is necessary.

I would assume that without a virtual address shift kaslr is

pretty darn

Quoted 1 line
Quoted 1 line
Quoted 1 line

pointless. Without the physical address shift the 1:1 map can be

used,

Quoted 1 line
Quoted 1 line
Quoted 1 line

and again, kaslr becomes pointless. However, there is absolutely

no

Quoted 1 line
Quoted 1 line
Quoted 1 line

reason why they should be coupled. They can, in fact, be

independently

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

randomized.

Agreed. On x86_64, we should be able to randomize virtual address

space

Quoted 3 lines
Quoted 3 lines

and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then
phys_base still stores an relative value, a different offset than before.

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page
frame. This can make physical addr be randomized to anywhere.

So now there may be 3 options:

1) Fix this bug in current kaslr. Since when randomize the new kernel location in choose_kernel_location(), cmdline options has been checked strictly, e.g if nokaslr is specified, it's safe to do the kernel location randomization. Then in handle_relocations(), we only need to check if the kernel location is changed, comparing with kernel loaded addr. If changed, kaslr is done, let's do the relocation handling. If not changed, no kaslr id done, just skip the relocation handling like before.

2) randomize the virtual addr space and physical addr space independently. But physical addr space must be below 4G.

3) extend the identity mapping to 48bit of addr space. Then we can randomized the virtual addr space in (0x1000000, 1G) and physical addr space in (0x1000000, real physical memory end).

If option 3 is doable, it's the best. If not, I think bug fix should be better.

Quoted 4 lines

Hi Peter,

So what do we do about this issue in short term to make kexec work.

Even

Quoted 2 lines

if we go for above solution, to make kexec work we will have to pass "nokaslr" as we don't want kernel to move around in physical address

space

Quoted 1 line

as it might stomp over ELF headers we have stored.

kexec doesn't need ELF headers. Kdump may need it. But in current kexec-tools implementation, kernel/initrd and other stuffs are placed from top to down, current implementation won't do kaslr since it only happened between kernel loaded addr and 1G. So we don't need to worry about the stomping.

Quoted 2 lines

If you don't like current patch, should we just disable relocations

in

Quoted 5 lines

x86_64 if "nokaslr" command line is passed. That way kernel will not be moved in physical as well as virtual address space.

Thanks
Vivek

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
author
#22 lore raw
In reply to Baoquan He
Quoted 15 lines · Baoquan He
Quoted 6 lines · Vivek Goyal
Quoted 4 lines · Vivek Goyal

Agreed. On x86_64, we should be able to randomize virtual address space and physical address space independently. And in that case whole of the physical memory should be available for a possible location for kernel. (As opposed to a small limit (I guess 1GB) now)

It can be done to randomize virtual address space and physical address space independently. But limited by the 2G of kernel text mapping and module mapping virtual address space, virtual address can be randomized in (0x1000000, 1G) range. While physical address can be randomized in (0x1000000, 4G) according to the identity mapping of normal kernel. Then phys_base still stores an relative value, a different offset than before.

Quoted 6 lines

This can be easily implement. One thing is still there's a limit for physical addr randomization, only below 4G. So I am wondering if we can extend the identify mapping to complete mapping of 48 bit, using 1G page frame. This can make physical addr be randomized to anywhere.

So now there may be 3 options:

Hi hpa,

About kaslr, do you have a suggestion or plan? Should we fix bug based on current code, or any other solutions?

Thanks
Baoquan

Quoted 20 lines

1) Fix this bug in current kaslr. Since when randomize the new kernel location in choose_kernel_location(), cmdline options has been checked strictly, e.g if nokaslr is specified, it's safe to do the kernel location randomization. Then in handle_relocations(), we only need to check if the kernel location is changed, comparing with kernel loaded addr. If changed, kaslr is done, let's do the relocation handling. If not changed, no kaslr id done, just skip the relocation handling like before.

2) randomize the virtual addr space and physical addr space independently. But physical addr space must be below 4G.

3) extend the identity mapping to 48bit of addr space. Then we can randomized the virtual addr space in (0x1000000, 1G) and physical addr space in (0x1000000, real physical memory end).

If option 3 is doable, it's the best. If not, I think bug fix should be better.

author
#23 lore raw
In reply to H. Peter Anvin
Quoted 18 lines · H. Peter Anvin
Quoted 17 lines · Baoquan He

Function handle_relocations() is used to do the relocations handling for i686 and kaslr of x86_64. For 32 bit the relocation handling is mandotary to perform. For x86_64 only when kaslr is enabled and a random kernel location is chosen successfully the relocation handling shound be done. However previous implementation only compared the kernel loading address and LOAD_PHYSICAL_ADDR where kernel were compiled to run at. This would casue system to be exceptional in few conditions like when delta between load address and compiled address is bigger than what 32bit signed relocations can handle. Also there will be limitations that delta can't be too big otherwise kernel text virtual addresses will overflow in module address space.

So in this patch check if kernel location is changed after choose_kernel_location() when x86_64. If and only if in x86_64 and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.

Quoted 8 lines
Quoted 5 lines

Could you clarify under what conditions we may end up with 32-bit signed overflow, and yet have a functional kernel?

Hi hpa,

As Vivek has replied, and from my test result, in x86_64 when kernel is loaded above 2G by bootloader, the 32-bit signed overflow will happen. Meanwhile the error() in arch/x86/boot/compressed/misc.c will be called, this function will execute 'hlt' instruction, then system halted.

In current kaslr, there are 3 hebaviours. Since the candidate slot for randomized kernel location is only between kernel loaded address and 1G, it doesn't choose a new kernel location when kernel is loaded above 1G.

1) Kernel is loaded between 0x1000000 and 1G, kaslr works well.

2)kernel is loaded between 1G and 2G, kernel doesn't choose a new kernel location, so the ourput_orig==output > 1G. But current code compares the output with LOAD_PHYSICAL_ADDR, relocation handling is still taken and this cause kenrel text mapping stomps module mapping space. So in this case form printing we can see kaslr passed, but kernel reboot to bios immediately.

3) When kernel is loaded above 2G, this is the 32-bit signed overflow as we discussed above.

So this patch can fix case 2 and case 3.

Thanks
Baoquan

Quoted 2 lines
author
#24 lore raw
In reply to Baoquan He

Hi hpa,

Ping.

Do you have further plan or idea on this issue? Or could you please merge this patch to fix reported bug for now?

Thanks
Baoquan

Quoted 94 lines · Baoquan He

Function handle_relocations() is used to do the relocations handling for i686 and kaslr of x86_64. For 32 bit the relocation handling is mandotary to perform. For x86_64 only when kaslr is enabled and a random kernel location is chosen successfully the relocation handling shound be done. However previous implementation only compared the kernel loading address and LOAD_PHYSICAL_ADDR where kernel were compiled to run at. This would casue system to be exceptional in few conditions like when delta between load address and compiled address is bigger than what 32bit signed relocations can handle. Also there will be limitations that delta can't be too big otherwise kernel text virtual addresses will overflow in module address space.

So in this patch check if kernel location is changed after choose_kernel_location() when x86_64. If and only if in x86_64 and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.

---
 arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/misc.c +22 -4
index 57ab74d..3bb2a17 100644
@@ -230,8 +230,9 @@ static void error(char *x)
 		asm("hlt");
 }
 
-#if CONFIG_X86_NEED_RELOCS
-static void handle_relocations(void *output, unsigned long output_len)
+#ifdef CONFIG_X86_NEED_RELOCS
+static void handle_relocations(void *output_orig, void *output,
+			       unsigned long output_len)
 {
 	int *reloc;
 	unsigned long delta, map, ptr;
@@ -239,6 +240,20 @@ static void handle_relocations(void *output, unsigned long output_len)
 	unsigned long max_addr = min_addr + output_len;
 
 	/*
+	* 32bit always requires relocations to be performed. For x86_64,
+	* relocations need to be performed only if kaslr has chosen a
+	* different load address then kernel was originally loaded at.
+	*
+	* If we are here, either kaslr is not configured in or kaslr is disabled
+	* or kaslr has chosen not to change the load location of kernel. Don't
+	* perform any relocations.
+	*/
+#if CONFIG_X86_64
+	if (output_orig == output)
+		return;
+#endif
+
+	/*
 	 * Calculate the delta between where vmlinux was linked to load
 	 * and where it was actually loaded.
 	 */
@@ -299,7 +314,8 @@ static void handle_relocations(void *output, unsigned long output_len)
 #endif
 }
 #else
-static inline void handle_relocations(void *output, unsigned long output_len)
+static inline void handle_relocations(void *output_orig, void *output,
+				      unsigned long output_len)
 { }
 #endif
 
@@ -360,6 +376,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
 				  unsigned char *output,
 				  unsigned long output_len)
 {
+	unsigned char *output_orig = output;
+
 	real_mode = rmode;
 
 	sanitize_boot_params(real_mode);
@@ -402,7 +420,7 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
 	debug_putstr("\nDecompressing Linux... ");
 	decompress(input_data, input_len, NULL, NULL, output, NULL, error);
 	parse_elf(output);
-	handle_relocations(output, output_len);
+	handle_relocations(output_orig, output, output_len);
 	debug_putstr("done.\nBooting the kernel.\n");
 	return output;
 }
Signature · 1 line
1.8.5.3