kaslr: check if kernel location is changed
Subsystems (4)
PATCH v2 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 hang when kernel loading address is not equal to LOAD_PHYSICAL_ADDR.
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 need be done.
--- arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/misc.c +22 -4
@@ -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 · 7 lines
1.8.5.3 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 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
@@ -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
Quoted 10 lines · Baoquan He
Function
handle_relocations()is used to do the relocations handling for i686 and kaslr ofx86_64. For 32 bit the relocation handling is mandotary to perform. Forx86_64only 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 andLOAD_PHYSICAL_ADDRwhere kernel were compiled to run at.This would casue system to hang when kernel loading address is not equal to
LOAD_PHYSICAL_ADDR.
I don't think above is correct. It will not always fails. It will fail only 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.
Thanks
Vivek
Quoted 78 lines
So in this patch check if kernel location is changed after
choose_kernel_location()whenx86_64. If and only if inx86_64and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling need be done.Signed-off-by Baoquan He <[email protected]>--- arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/boot/compressed/misc.c +22
-4@@ -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
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 12 lines · Vivek Goyal
Quoted 10 lines · Baoquan He
Function
handle_relocations()is used to do the relocations handling for i686 and kaslr ofx86_64. For 32 bit the relocation handling is mandotary to perform. Forx86_64only 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 andLOAD_PHYSICAL_ADDRwhere kernel were compiled to run at.This would casue system to hang when kernel loading address is not equal to
LOAD_PHYSICAL_ADDR.
Quoted 6 lines
I don't think above is correct. It will not always fails. It will fail only 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.
Yes, will repost with changed description.
Quoted 69 lines
Thanks
VivekQuoted 66 lines
So in this patch check if kernel location is changed after
choose_kernel_location()whenx86_64. If and only if inx86_64and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling need be done.Signed-off-by Baoquan He <[email protected]>--- arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/boot/compressed/misc.c +21
-3@@ -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;
Quoted 12 lines
Quoted 12 lines
arch/x86/boot/compressed/misc.c +1
-1sanitize_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
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
PATCH v3 1/2 kaslr: check if kernel location is changed
In reply to Baoquan HeFunction 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
@@ -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 · 8 lines
1.8.5.3 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Quoted 12 lines · Baoquan He
Function
handle_relocations()is used to do the relocations handling for i686 and kaslr ofx86_64. For 32 bit the relocation handling is mandotary to perform. Forx86_64only 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 andLOAD_PHYSICAL_ADDRwhere 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.
Quoted 6 lines
So in this patch check if kernel location is changed after
choose_kernel_location()whenx86_64. If and only if inx86_64and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.Signed-off-by Baoquan He <[email protected]>
I think this patch should make kexec and kdump working with kaslr enabled (CONFIG_RANDOMIZE_BASE=y).
In case of kdump, we will need to pass "nokaslr" to make sure kernel does not move from kexec chosen address.
In case of kexec, I think it should be ok to not pass "nokaslr". This case is no different than any other bootloader.
Hence.
Thomas D.,
You had reported kexec issues with CONFIG_RANDOMIZE_BASE=y. Does this patch resolve the issue for you?
Thanks
Vivek
Quoted 72 lines
--- arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/boot/compressed/misc.c +22
-4@@ -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
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Hi,
Quoted 2 lines · Vivek Goyal
You had reported kexec issues with
CONFIG_RANDOMIZE_BASE=y. Does this patch resolve the issue for you?
Yup! Tested against kernel-3.16.2.
-Thomas
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 7 lines · Thomas D.
Hi,
Quoted 2 lines · Vivek Goyal
You had reported kexec issues with
CONFIG_RANDOMIZE_BASE=y. Does this patch resolve the issue for you?Yup! Tested against kernel-3.16.2.
Thanks. Given this patch is small and should not break anything else, I think it might make sense to send it to stable too.
Thanks
Vivek
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Kees Cook
Acked-byQuoted 23 lines · Vivek Goyal
Quoted 12 lines · Baoquan He
Function
handle_relocations()is used to do the relocations handling for i686 and kaslr ofx86_64. For 32 bit the relocation handling is mandotary to perform. Forx86_64only 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 andLOAD_PHYSICAL_ADDRwhere 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.Quoted 6 lines
So in this patch check if kernel location is changed after
choose_kernel_location()whenx86_64. If and only if inx86_64and kernel location is changed, we say a kaslr random kernel location is chosen, then the relocation handling is needed.Signed-off-by Baoquan He <[email protected]>I think this patch should make kexec and kdump working with kaslr enabled (
CONFIG_RANDOMIZE_BASE=y).
Quoted 8 lines
In case of kdump, we will need to pass "nokaslr" to make sure kernel does not move from kexec chosen address.
In case of kexec, I think it should be ok to not pass "nokaslr". This case is no different than any other bootloader.
Hence.
Acked-by Vivek Goyal <[email protected]>
Yup, I think this looks fine to me too. Thanks for getting this fixed! And as Vivek mentioned later, I think this should probably go into -stable as well.
-Kees
Quoted 81 lines
Thomas D.,
You had reported kexec issues with
CONFIG_RANDOMIZE_BASE=y. Does this patch resolve the issue for you?Thanks
VivekQuoted 72 lines
--- arch/x86/boot/compressed/misc.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)arch/x86/boot/compressed/misc.c +22
-5@@ -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; } --1.8.5.3
Signature · 2 lines
Kees Cook Chrome OS Security