linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/boot: minor improvement in kaslr
@ 2019-02-01  5:48 Cao jin
  2019-02-01  8:20 ` Kees Cook
  0 siblings, 1 reply; 4+ messages in thread
From: Cao jin @ 2019-02-01  5:48 UTC (permalink / raw)
  To: linux-kernel, x86; +Cc: tglx, mingo, bp, hpa, bhe, keescook, fanc.fnst

comments fix: input_size is ZO image size which just don't count .bss
in, but has .text, .data, etc;
drop unecessary alignment: minimum is either 512M or output, both are
CONFIG_PHYSICAL_ALIGN aligned(output is aligned in head_32/64.S). But
mention it in earlier comments.

Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
---
 arch/x86/boot/compressed/kaslr.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 9ed9709d9947..a947c5aba34e 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -360,7 +360,7 @@ static void handle_mem_options(void)
  * (i.e. it does not include its run size). This range must be avoided
  * because it contains the data used for decompression.
  *
- * [input+input_size, output+init_size) is [_text, _end) for ZO. This
+ * [input+input_size, output+init_size) is [_bss, _end) for ZO. This
  * range includes ZO's heap and stack, and must be avoided since it
  * performs the decompression.
  *
@@ -763,9 +763,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
 		return 0;
 	}
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
-
 	if (process_efi_entries(minimum, image_size))
 		return slots_fetch_random();
 
@@ -831,8 +828,8 @@ void choose_random_location(unsigned long input,
 
 	/*
 	 * Low end of the randomization range should be the
-	 * smaller of 512M or the initial kernel image
-	 * location:
+	 * smaller of 512M or the initial kernel image location.
+	 * Should be aligned to CONFIG_PHYSICAL_ALIGN.
 	 */
 	min_addr = min(*output, 512UL << 20);
 
-- 
2.17.0




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: minor improvement in kaslr
  2019-02-01  5:48 [PATCH] x86/boot: minor improvement in kaslr Cao jin
@ 2019-02-01  8:20 ` Kees Cook
  2019-02-01  9:41   ` Cao jin
  2019-02-13  3:10   ` Cao jin
  0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2019-02-01  8:20 UTC (permalink / raw)
  To: Cao jin
  Cc: LKML, X86 ML, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H. Peter Anvin, Baoquan He, Chao Fan

On Fri, Feb 1, 2019 at 6:51 PM Cao jin <caoj.fnst@cn.fujitsu.com> wrote:
>
> comments fix: input_size is ZO image size which just don't count .bss
> in, but has .text, .data, etc;
> drop unecessary alignment: minimum is either 512M or output, both are
> CONFIG_PHYSICAL_ALIGN aligned(output is aligned in head_32/64.S). But
> mention it in earlier comments.
>
> Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
> ---
>  arch/x86/boot/compressed/kaslr.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
> index 9ed9709d9947..a947c5aba34e 100644
> --- a/arch/x86/boot/compressed/kaslr.c
> +++ b/arch/x86/boot/compressed/kaslr.c
> @@ -360,7 +360,7 @@ static void handle_mem_options(void)
>   * (i.e. it does not include its run size). This range must be avoided
>   * because it contains the data used for decompression.
>   *
> - * [input+input_size, output+init_size) is [_text, _end) for ZO. This
> + * [input+input_size, output+init_size) is [_bss, _end) for ZO. This

This isn't right. The comment was correct before. See
arch/x86/boot/compressed/vmlinux.lds.S for the layout of the ZO image:
after the compressed image is _text, _rodata, _got, _data, _bss,
_pgtable, and _end. "[_text, _end)" correctly identifies the span
used.

>   * range includes ZO's heap and stack, and must be avoided since it
>   * performs the decompression.
>   *
> @@ -763,9 +763,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
>                 return 0;
>         }
>
> -       /* Make sure minimum is aligned. */
> -       minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
> -

I would prefer to keep this runtime calculation since it enforces the
requirement instead of making leaving it in a comment. When this goes
wrong, you get an unbootable kernel, which is very frustrating to
debug.

>         if (process_efi_entries(minimum, image_size))
>                 return slots_fetch_random();
>
> @@ -831,8 +828,8 @@ void choose_random_location(unsigned long input,
>
>         /*
>          * Low end of the randomization range should be the
> -        * smaller of 512M or the initial kernel image
> -        * location:
> +        * smaller of 512M or the initial kernel image location.
> +        * Should be aligned to CONFIG_PHYSICAL_ALIGN.

This is fine to mention, sure.

-Kees

>          */
>         min_addr = min(*output, 512UL << 20);
>
> --
> 2.17.0
>
>
>


-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: minor improvement in kaslr
  2019-02-01  8:20 ` Kees Cook
@ 2019-02-01  9:41   ` Cao jin
  2019-02-13  3:10   ` Cao jin
  1 sibling, 0 replies; 4+ messages in thread
From: Cao jin @ 2019-02-01  9:41 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, X86 ML, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H. Peter Anvin, Baoquan He, Chao Fan

On 2/1/19 4:20 PM, Kees Cook wrote:
> On Fri, Feb 1, 2019 at 6:51 PM Cao jin <caoj.fnst@cn.fujitsu.com> wrote:
>>
>> comments fix: input_size is ZO image size which just don't count .bss
>> in, but has .text, .data, etc;
>> drop unecessary alignment: minimum is either 512M or output, both are
>> CONFIG_PHYSICAL_ALIGN aligned(output is aligned in head_32/64.S). But
>> mention it in earlier comments.
>>
>> Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
>> ---
>>  arch/x86/boot/compressed/kaslr.c | 9 +++------
>>  1 file changed, 3 insertions(+), 6 deletions(-)
>>
>> diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
>> index 9ed9709d9947..a947c5aba34e 100644
>> --- a/arch/x86/boot/compressed/kaslr.c
>> +++ b/arch/x86/boot/compressed/kaslr.c
>> @@ -360,7 +360,7 @@ static void handle_mem_options(void)
>>   * (i.e. it does not include its run size). This range must be avoided
>>   * because it contains the data used for decompression.
>>   *
>> - * [input+input_size, output+init_size) is [_text, _end) for ZO. This
>> + * [input+input_size, output+init_size) is [_bss, _end) for ZO. This
> 
> This isn't right. The comment was correct before. See
> arch/x86/boot/compressed/vmlinux.lds.S for the layout of the ZO image:
> after the compressed image is _text, _rodata, _got, _data, _bss,
> _pgtable, and _end. "[_text, _end)" correctly identifies the span
> used.
> 

I am confused, doesn't input_size = ZO image size = .head.text +
.rodata..compressed + .rodata + .got + .data + .pgtable ? As I know,
.bss don't occupy any space in file, and ZO's full run size is against
the end of buffer, so I think the tiny gap here is just .bss, which is
also the stack and heap. Do I get it wrong?

>>   * range includes ZO's heap and stack, and must be avoided since it
>>   * performs the decompression.
>>   *
>> @@ -763,9 +763,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
>>                 return 0;
>>         }
>>
>> -       /* Make sure minimum is aligned. */
>> -       minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
>> -
> 
> I would prefer to keep this runtime calculation since it enforces the
> requirement instead of making leaving it in a comment. When this goes
> wrong, you get an unbootable kernel, which is very frustrating to
> debug.
> 

I find that I maybe wrong here. It is said that CONFIG_PHYSICAL_ALIGN
must be a multiple of 0x200000 on 64-bit, so it could be 2M, 4M, 6M, 8M,
12M, 14M, 16M, while 512M can't be divided by 6, 10, 12, 14 without
remainder.

-- 
Sincerely,
Cao jin



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: minor improvement in kaslr
  2019-02-01  8:20 ` Kees Cook
  2019-02-01  9:41   ` Cao jin
@ 2019-02-13  3:10   ` Cao jin
  1 sibling, 0 replies; 4+ messages in thread
From: Cao jin @ 2019-02-13  3:10 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, X86 ML, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H. Peter Anvin, Baoquan He, Chao Fan

On 2/1/19 4:20 PM, Kees Cook wrote:
> On Fri, Feb 1, 2019 at 6:51 PM Cao jin <caoj.fnst@cn.fujitsu.com> wrote:
>>
>> comments fix: input_size is ZO image size which just don't count .bss
>> in, but has .text, .data, etc;
>> drop unecessary alignment: minimum is either 512M or output, both are
>> CONFIG_PHYSICAL_ALIGN aligned(output is aligned in head_32/64.S). But
>> mention it in earlier comments.
>>
>> Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
>> ---
>>  arch/x86/boot/compressed/kaslr.c | 9 +++------
>>  1 file changed, 3 insertions(+), 6 deletions(-)
>>
>> diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
>> index 9ed9709d9947..a947c5aba34e 100644
>> --- a/arch/x86/boot/compressed/kaslr.c
>> +++ b/arch/x86/boot/compressed/kaslr.c
>> @@ -360,7 +360,7 @@ static void handle_mem_options(void)
>>   * (i.e. it does not include its run size). This range must be avoided
>>   * because it contains the data used for decompression.
>>   *
>> - * [input+input_size, output+init_size) is [_text, _end) for ZO. This
>> + * [input+input_size, output+init_size) is [_bss, _end) for ZO. This
> 
> This isn't right. The comment was correct before. See
> arch/x86/boot/compressed/vmlinux.lds.S for the layout of the ZO image:
> after the compressed image is _text, _rodata, _got, _data, _bss,
> _pgtable, and _end. "[_text, _end)" correctly identifies the span
> used.
> 

Finally see why I am wrong here, I mixed up with the input_size & ZO
image file size. Sorry for the noise, and thanks very much for your
hint, Kees!

-- 
Sincerely,
Cao jin



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-02-13  3:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-01  5:48 [PATCH] x86/boot: minor improvement in kaslr Cao jin
2019-02-01  8:20 ` Kees Cook
2019-02-01  9:41   ` Cao jin
2019-02-13  3:10   ` Cao jin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).