[PATCH 3/3] x86/boot: avoid recompiling kaslr.c for incremental rebuilds

Jann Horn posted 3 patches 1 year, 11 months ago
[PATCH 3/3] x86/boot: avoid recompiling kaslr.c for incremental rebuilds
Posted by Jann Horn 1 year, 11 months ago
Currently, every kernel rebuild needs to compile kaslr.c again because
UTS_VERSION changes on every rebuild.
Move the build string into a separate object file to speed things up.

Signed-off-by: Jann Horn <jannh@google.com>
---
 arch/x86/boot/compressed/dynamic_vars.c |  8 ++++++++
 arch/x86/boot/compressed/dynamic_vars.h |  3 +++
 arch/x86/boot/compressed/kaslr.c        | 10 ++--------
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/arch/x86/boot/compressed/dynamic_vars.c b/arch/x86/boot/compressed/dynamic_vars.c
index cda64ff4b6da..15a57fbb05e3 100644
--- a/arch/x86/boot/compressed/dynamic_vars.c
+++ b/arch/x86/boot/compressed/dynamic_vars.c
@@ -1,9 +1,17 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/const.h>
 #include "dynamic_vars.h"
+#include <generated/compile.h>
+#include <generated/utsrelease.h>
+#include <generated/utsversion.h>
 #include "../voffset.h"
 
 const unsigned long vo__text = VO__text;
 const unsigned long vo___bss_start = VO___bss_start;
 const unsigned long vo__end = VO__end;
 const unsigned long kernel_total_size = VO__end - VO__text;
+
+/* Simplified build-specific string for starting entropy. */
+const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
+		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
+unsigned long build_str_len = sizeof(build_str)-1;
diff --git a/arch/x86/boot/compressed/dynamic_vars.h b/arch/x86/boot/compressed/dynamic_vars.h
index a0f7dc359cb6..3ebc4a3144d4 100644
--- a/arch/x86/boot/compressed/dynamic_vars.h
+++ b/arch/x86/boot/compressed/dynamic_vars.h
@@ -9,3 +9,6 @@
 /* Variables containing VO__text, VO___bss_start, VO__end */
 extern const unsigned long vo__text, vo___bss_start, vo__end;
 extern const unsigned long kernel_total_size;
+
+extern const char build_str[];
+extern unsigned long build_str_len;
diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 3ede59ad67eb..c14e4e7a6b08 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -23,14 +23,12 @@
 #include "error.h"
 #include "../string.h"
 #include "efi.h"
+#include "dynamic_vars.h"
 
-#include <generated/compile.h>
 #include <linux/module.h>
 #include <linux/uts.h>
 #include <linux/utsname.h>
 #include <linux/ctype.h>
-#include <generated/utsversion.h>
-#include <generated/utsrelease.h>
 
 #define _SETUP
 #include <asm/setup.h>	/* For COMMAND_LINE_SIZE */
@@ -38,10 +36,6 @@
 
 extern unsigned long get_cmd_line_ptr(void);
 
-/* Simplified build-specific string for starting entropy. */
-static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
-		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
-
 static unsigned long rotate_xor_one(unsigned long hash, unsigned long val)
 {
 	/* Rotate by odd number of bits and XOR. */
@@ -75,7 +69,7 @@ static unsigned long get_boot_seed(void)
 {
 	unsigned long hash = 0;
 
-	hash = rotate_xor(hash, build_str, sizeof(build_str));
+	hash = rotate_xor(hash, build_str, build_str_len);
 	hash = rotate_xor(hash, boot_params_ptr, sizeof(*boot_params_ptr));
 
 	return hash;
-- 
2.44.0.rc0.258.g7320e95886-goog
Re: [PATCH 3/3] x86/boot: avoid recompiling kaslr.c for incremental rebuilds
Posted by Kees Cook 1 year, 11 months ago
On Tue, Feb 20, 2024 at 08:21:44PM +0100, Jann Horn wrote:
> Currently, every kernel rebuild needs to compile kaslr.c again because
> UTS_VERSION changes on every rebuild.
> Move the build string into a separate object file to speed things up.
> 
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
>  arch/x86/boot/compressed/dynamic_vars.c |  8 ++++++++
>  arch/x86/boot/compressed/dynamic_vars.h |  3 +++
>  arch/x86/boot/compressed/kaslr.c        | 10 ++--------
>  3 files changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/boot/compressed/dynamic_vars.c b/arch/x86/boot/compressed/dynamic_vars.c
> index cda64ff4b6da..15a57fbb05e3 100644
> --- a/arch/x86/boot/compressed/dynamic_vars.c
> +++ b/arch/x86/boot/compressed/dynamic_vars.c
> @@ -1,9 +1,17 @@
>  // SPDX-License-Identifier: GPL-2.0
>  #include <linux/const.h>
>  #include "dynamic_vars.h"
> +#include <generated/compile.h>
> +#include <generated/utsrelease.h>
> +#include <generated/utsversion.h>
>  #include "../voffset.h"
>  
>  const unsigned long vo__text = VO__text;
>  const unsigned long vo___bss_start = VO___bss_start;
>  const unsigned long vo__end = VO__end;
>  const unsigned long kernel_total_size = VO__end - VO__text;
> +
> +/* Simplified build-specific string for starting entropy. */
> +const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
> +		LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
> +unsigned long build_str_len = sizeof(build_str)-1;

This can be const too, yes? (Also, you didn't want to include the
trailing NUL in the xor?

Otherwise, yeah, I like this whole series.

-- 
Kees Cook
Re: [PATCH 3/3] x86/boot: avoid recompiling kaslr.c for incremental rebuilds
Posted by Kees Cook 1 year, 11 months ago
On Tue, Feb 20, 2024 at 08:21:44PM +0100, Jann Horn wrote:
> Currently, every kernel rebuild needs to compile kaslr.c again because
> UTS_VERSION changes on every rebuild.
> Move the build string into a separate object file to speed things up.

Heh, I think I don't see this because I force my UTS_VERSION to stay
still by default so I can do binary difference comparisons more easily. :P

args += ['KBUILD_BUILD_TIMESTAMP=1970-01-01']
args += ['KBUILD_BUILD_VERSION=1']
args += ['KBUILD_BUILD_USER=user']
args += ['KBUILD_BUILD_HOST=host']


-- 
Kees Cook