linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] x86: Various clean-ups in support of FGKASLR
@ 2021-10-13 17:57 Kees Cook
  2021-10-13 17:57 ` [PATCH 1/4] x86/tools/relocs: Support >64K section headers Kees Cook
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: Kees Cook @ 2021-10-13 17:57 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Josh Poimboeuf, H. Peter Anvin,
	Kristen Carlson Accardi, Tony Luck, Alexander Lobakin,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Arnd Bergmann,
	Joerg Roedel, Arvind Sankar, Jing Yangyang, Abaci Robot,
	Jiapeng Chong, Nathan Chancellor, Nick Desaulniers,
	Vincenzo Frascino, Andrey Konovalov, Miroslav Benes,
	H. Nikolaus Schaller, Fangrui Song, linux-kernel, x86,
	linux-arch, linux-hardening

Hi,

These are a small set of patches that clean up various things that are
each stand-alone improvements, but they're also needed for the coming
FGKASLR series[1]. I thought it best to just get these landed instead
of having them continue to tag along with FGKASLR, especially the
early malloc() fix, which is a foot-gun waiting to happen. :)

Thanks!

-Kees

[1] https://lore.kernel.org/lkml/20210831144114.154-1-alexandr.lobakin@intel.com/

Kees Cook (2):
  x86/boot: Allow a "silent" kaslr random byte fetch
  x86/boot/compressed: Avoid duplicate malloc() implementations

Kristen Carlson Accardi (2):
  x86/tools/relocs: Support >64K section headers
  vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext

 arch/x86/boot/compressed/kaslr.c  |   4 --
 arch/x86/boot/compressed/misc.c   |   3 +
 arch/x86/boot/compressed/misc.h   |   2 +
 arch/x86/lib/kaslr.c              |  18 ++++--
 arch/x86/tools/relocs.c           | 103 ++++++++++++++++++++++--------
 include/asm-generic/vmlinux.lds.h |   3 +-
 include/linux/decompress/mm.h     |  12 +++-
 7 files changed, 107 insertions(+), 38 deletions(-)

-- 
2.30.2


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

* [PATCH 1/4] x86/tools/relocs: Support >64K section headers
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
@ 2021-10-13 17:57 ` Kees Cook
  2021-10-13 17:57 ` [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch Kees Cook
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Kees Cook @ 2021-10-13 17:57 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Kristen Carlson Accardi, Tony Luck, H . Peter Anvin,
	Alexander Lobakin, Josh Poimboeuf, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Arnd Bergmann, Joerg Roedel, Arvind Sankar,
	Jing Yangyang, Abaci Robot, Jiapeng Chong, Nathan Chancellor,
	Nick Desaulniers, Vincenzo Frascino, Andrey Konovalov,
	Miroslav Benes, H. Nikolaus Schaller, Fangrui Song, linux-kernel,
	x86, linux-arch, linux-hardening

From: Kristen Carlson Accardi <kristen@linux.intel.com>

While the relocs tool already supports finding the total number of
section headers if vmlinux exceeds 64K sections, it fails to read the
extended symbol table to get section header indexes for symbols, causing
incorrect symbol table indexes to be used when there are > 64K symbols.

Parse the ELF file to read the extended symbol table info, and then
replace all direct references to st_shndx with calls to sym_index(),
which will determine whether the value can be read directly or whether
the value should be pulled out of the extended table.

This is needed for future FGKASLR support, which uses a separate section
per function.

Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Acked-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/tools/relocs.c | 103 ++++++++++++++++++++++++++++++----------
 1 file changed, 78 insertions(+), 25 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 27c82207d387..3f5d39768287 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -14,6 +14,10 @@
 static Elf_Ehdr		ehdr;
 static unsigned long	shnum;
 static unsigned int	shstrndx;
+static unsigned int	shsymtabndx;
+static unsigned int	shxsymtabndx;
+
+static int sym_index(Elf_Sym *sym);
 
 struct relocs {
 	uint32_t	*offset;
@@ -35,6 +39,7 @@ struct section {
 	Elf_Shdr       shdr;
 	struct section *link;
 	Elf_Sym        *symtab;
+	Elf32_Word     *xsymtab;
 	Elf_Rel        *reltab;
 	char           *strtab;
 };
@@ -268,7 +273,7 @@ static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
 		name = sym_strtab + sym->st_name;
 	}
 	else {
-		name = sec_name(sym->st_shndx);
+		name = sec_name(sym_index(sym));
 	}
 	return name;
 }
@@ -338,6 +343,23 @@ static uint64_t elf64_to_cpu(uint64_t val)
 #define elf_xword_to_cpu(x)	elf32_to_cpu(x)
 #endif
 
+static int sym_index(Elf_Sym *sym)
+{
+	Elf_Sym *symtab = secs[shsymtabndx].symtab;
+	Elf32_Word *xsymtab = secs[shxsymtabndx].xsymtab;
+	unsigned long offset;
+	int index;
+
+	if (sym->st_shndx != SHN_XINDEX)
+		return sym->st_shndx;
+
+	/* calculate offset of sym from head of table. */
+	offset = (unsigned long)sym - (unsigned long)symtab;
+	index = offset / sizeof(*sym);
+
+	return elf32_to_cpu(xsymtab[index]);
+}
+
 static void read_ehdr(FILE *fp)
 {
 	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
@@ -471,31 +493,60 @@ static void read_strtabs(FILE *fp)
 static void read_symtabs(FILE *fp)
 {
 	int i,j;
+
 	for (i = 0; i < shnum; i++) {
 		struct section *sec = &secs[i];
-		if (sec->shdr.sh_type != SHT_SYMTAB) {
+		int num_syms;
+
+		switch (sec->shdr.sh_type) {
+		case SHT_SYMTAB_SHNDX:
+			sec->xsymtab = malloc(sec->shdr.sh_size);
+			if (!sec->xsymtab) {
+				die("malloc of %" FMT " bytes for xsymtab failed\n",
+				    sec->shdr.sh_size);
+			}
+			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
+				die("Seek to %" FMT " failed: %s\n",
+				    sec->shdr.sh_offset, strerror(errno));
+			}
+			if (fread(sec->xsymtab, 1, sec->shdr.sh_size, fp)
+			    != sec->shdr.sh_size) {
+				die("Cannot read extended symbol table: %s\n",
+				    strerror(errno));
+			}
+			shxsymtabndx = i;
+			continue;
+
+		case SHT_SYMTAB:
+			num_syms = sec->shdr.sh_size / sizeof(Elf_Sym);
+
+			sec->symtab = malloc(sec->shdr.sh_size);
+			if (!sec->symtab) {
+				die("malloc of %" FMT " bytes for symtab failed\n",
+				    sec->shdr.sh_size);
+			}
+			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
+				die("Seek to %" FMT " failed: %s\n",
+				    sec->shdr.sh_offset, strerror(errno));
+			}
+			if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
+			    != sec->shdr.sh_size) {
+				die("Cannot read symbol table: %s\n",
+				    strerror(errno));
+			}
+			for (j = 0; j < num_syms; j++) {
+				Elf_Sym *sym = &sec->symtab[j];
+
+				sym->st_name  = elf_word_to_cpu(sym->st_name);
+				sym->st_value = elf_addr_to_cpu(sym->st_value);
+				sym->st_size  = elf_xword_to_cpu(sym->st_size);
+				sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
+			}
+			shsymtabndx = i;
+			continue;
+
+		default:
 			continue;
-		}
-		sec->symtab = malloc(sec->shdr.sh_size);
-		if (!sec->symtab) {
-			die("malloc of %" FMT " bytes for symtab failed\n",
-			    sec->shdr.sh_size);
-		}
-		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %" FMT " failed: %s\n",
-			    sec->shdr.sh_offset, strerror(errno));
-		}
-		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
-		    != sec->shdr.sh_size) {
-			die("Cannot read symbol table: %s\n",
-				strerror(errno));
-		}
-		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
-			Elf_Sym *sym = &sec->symtab[j];
-			sym->st_name  = elf_word_to_cpu(sym->st_name);
-			sym->st_value = elf_addr_to_cpu(sym->st_value);
-			sym->st_size  = elf_xword_to_cpu(sym->st_size);
-			sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
 		}
 	}
 }
@@ -762,7 +813,9 @@ static void percpu_init(void)
  */
 static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
 {
-	return (sym->st_shndx == per_cpu_shndx) &&
+	int shndx = sym_index(sym);
+
+	return (shndx == per_cpu_shndx) &&
 		strcmp(symname, "__init_begin") &&
 		strcmp(symname, "__per_cpu_load") &&
 		strncmp(symname, "init_per_cpu_", 13);
@@ -1095,7 +1148,7 @@ static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
 		sec_name(sec->shdr.sh_info),
 		rel_type(ELF_R_TYPE(rel->r_info)),
 		symname,
-		sec_name(sym->st_shndx));
+		sec_name(sym_index(sym)));
 	return 0;
 }
 
-- 
2.30.2


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

* [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
  2021-10-13 17:57 ` [PATCH 1/4] x86/tools/relocs: Support >64K section headers Kees Cook
@ 2021-10-13 17:57 ` Kees Cook
  2021-10-13 18:10   ` Nick Desaulniers
  2021-10-13 17:57 ` [PATCH 3/4] x86/boot/compressed: Avoid duplicate malloc() implementations Kees Cook
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: Kees Cook @ 2021-10-13 17:57 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Josh Poimboeuf, H. Peter Anvin,
	Kristen Carlson Accardi, Tony Luck, Alexander Lobakin,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Arnd Bergmann,
	Joerg Roedel, Arvind Sankar, Jing Yangyang, Abaci Robot,
	Jiapeng Chong, Nathan Chancellor, Nick Desaulniers,
	Vincenzo Frascino, Andrey Konovalov, Miroslav Benes,
	H. Nikolaus Schaller, Fangrui Song, linux-kernel, x86,
	linux-arch, linux-hardening

Under earlyprintk, each RNG call produces a debug report line. To support
the future FGKASLR feature, which will fetch random bytes during function
shuffling, this is not useful information (each line is identical and
tells us nothing new), needlessly spamming the console. Instead, allow
for a NULL "purpose" to suppress the debug reporting.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/lib/kaslr.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/arch/x86/lib/kaslr.c b/arch/x86/lib/kaslr.c
index a53665116458..2b3eb8c948a3 100644
--- a/arch/x86/lib/kaslr.c
+++ b/arch/x86/lib/kaslr.c
@@ -56,11 +56,14 @@ unsigned long kaslr_get_random_long(const char *purpose)
 	unsigned long raw, random = get_boot_seed();
 	bool use_i8254 = true;
 
-	debug_putstr(purpose);
-	debug_putstr(" KASLR using");
+	if (purpose) {
+		debug_putstr(purpose);
+		debug_putstr(" KASLR using");
+	}
 
 	if (has_cpuflag(X86_FEATURE_RDRAND)) {
-		debug_putstr(" RDRAND");
+		if (purpose)
+			debug_putstr(" RDRAND");
 		if (rdrand_long(&raw)) {
 			random ^= raw;
 			use_i8254 = false;
@@ -68,7 +71,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
 	}
 
 	if (has_cpuflag(X86_FEATURE_TSC)) {
-		debug_putstr(" RDTSC");
+		if (purpose)
+			debug_putstr(" RDTSC");
 		raw = rdtsc();
 
 		random ^= raw;
@@ -76,7 +80,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
 	}
 
 	if (use_i8254) {
-		debug_putstr(" i8254");
+		if (purpose)
+			debug_putstr(" i8254");
 		random ^= i8254();
 	}
 
@@ -86,7 +91,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
 	    : "a" (random), "rm" (mix_const));
 	random += raw;
 
-	debug_putstr("...\n");
+	if (purpose)
+		debug_putstr("...\n");
 
 	return random;
 }
-- 
2.30.2


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

* [PATCH 3/4] x86/boot/compressed: Avoid duplicate malloc() implementations
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
  2021-10-13 17:57 ` [PATCH 1/4] x86/tools/relocs: Support >64K section headers Kees Cook
  2021-10-13 17:57 ` [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch Kees Cook
@ 2021-10-13 17:57 ` Kees Cook
  2021-10-13 17:57 ` [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext Kees Cook
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Kees Cook @ 2021-10-13 17:57 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Alexander Lobakin, Josh Poimboeuf, H. Peter Anvin,
	Kristen Carlson Accardi, Tony Luck, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Arnd Bergmann, Joerg Roedel, Arvind Sankar,
	Jing Yangyang, Abaci Robot, Jiapeng Chong, Nathan Chancellor,
	Nick Desaulniers, Vincenzo Frascino, Andrey Konovalov,
	Miroslav Benes, H. Nikolaus Schaller, Fangrui Song, linux-kernel,
	x86, linux-arch, linux-hardening

The early malloc() and free() implementation in include/linux/decompress/mm.h
(which is also included by the static decompressors) is static. This is
fine when the only thing interested in using malloc() is the decompression
code, but the x86 early boot environment may use malloc() in a couple places,
leading to a potential collision when the static copies of the available
memory region ("malloc_ptr") gets reset to the global "free_mem_ptr" value.
As it happened, the existing usage pattern was accidentally safe because each
user did 1 malloc() and 1 free() before returning and were not nested:

extract_kernel() (misc.c)
	choose_random_location() (kaslr.c)
		mem_avoid_init()
			handle_mem_options()
				malloc()
				...
				free()
	...
	parse_elf() (misc.c)
		malloc()
		...
		free()

Once the future FGKASLR series is added, however, it will insert
additional malloc() calls local to fgkaslr.c in the middle of
parse_elf()'s malloc()/free() pair:

	parse_elf() (misc.c)
		malloc()
		if (...) {
			layout_randomized_image(output, &ehdr, phdrs);
				malloc() <- boom
				...
		else
			layout_image(output, &ehdr, phdrs);
		free()

To avoid collisions, there must be a single implementation of malloc().
Adjust include/linux/decompress/mm.h so that visibility can be
controlled, provide prototypes in misc.h, and implement the functions in
misc.c. This also results in a small size savings:

$ size vmlinux.before vmlinux.after
   text    data     bss     dec     hex filename
8842314     468  178320 9021102  89a6ae vmlinux.before
8842240     468  178320 9021028  89a664 vmlinux.after

Fixed-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/boot/compressed/kaslr.c |  4 ----
 arch/x86/boot/compressed/misc.c  |  3 +++
 arch/x86/boot/compressed/misc.h  |  2 ++
 include/linux/decompress/mm.h    | 12 ++++++++++--
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 67c3208b668a..411b268bc0a2 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -32,10 +32,6 @@
 #include <generated/utsrelease.h>
 #include <asm/efi.h>
 
-/* Macros used by the included decompressor code below. */
-#define STATIC
-#include <linux/decompress/mm.h>
-
 #define _SETUP
 #include <asm/setup.h>	/* For COMMAND_LINE_SIZE */
 #undef _SETUP
diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c
index 743f13ea25c1..a4339cb2d247 100644
--- a/arch/x86/boot/compressed/misc.c
+++ b/arch/x86/boot/compressed/misc.c
@@ -28,6 +28,9 @@
 
 /* Macros used by the included decompressor code below. */
 #define STATIC		static
+/* Define an externally visible malloc()/free(). */
+#define MALLOC_VISIBLE
+#include <linux/decompress/mm.h>
 
 /*
  * Provide definitions of memzero and memmove as some of the decompressors will
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index 31139256859f..975ef4ae7395 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -44,6 +44,8 @@ extern char _head[], _end[];
 /* misc.c */
 extern memptr free_mem_ptr;
 extern memptr free_mem_end_ptr;
+void *malloc(int size);
+void free(void *where);
 extern struct boot_params *boot_params;
 void __putstr(const char *s);
 void __puthex(unsigned long value);
diff --git a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h
index 868e9eacd69e..9192986b1a73 100644
--- a/include/linux/decompress/mm.h
+++ b/include/linux/decompress/mm.h
@@ -25,13 +25,21 @@
 #define STATIC_RW_DATA static
 #endif
 
+/*
+ * When an architecture needs to share the malloc()/free() implementation
+ * between compilation units, it needs to have non-local visibility.
+ */
+#ifndef MALLOC_VISIBLE
+#define MALLOC_VISIBLE static
+#endif
+
 /* A trivial malloc implementation, adapted from
  *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  */
 STATIC_RW_DATA unsigned long malloc_ptr;
 STATIC_RW_DATA int malloc_count;
 
-static void *malloc(int size)
+MALLOC_VISIBLE void *malloc(int size)
 {
 	void *p;
 
@@ -52,7 +60,7 @@ static void *malloc(int size)
 	return p;
 }
 
-static void free(void *where)
+MALLOC_VISIBLE void free(void *where)
 {
 	malloc_count--;
 	if (!malloc_count)
-- 
2.30.2


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

* [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
                   ` (2 preceding siblings ...)
  2021-10-13 17:57 ` [PATCH 3/4] x86/boot/compressed: Avoid duplicate malloc() implementations Kees Cook
@ 2021-10-13 17:57 ` Kees Cook
  2021-10-14  0:29   ` Josh Poimboeuf
  2021-10-15 18:27 ` [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Alexander Lobakin
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: Kees Cook @ 2021-10-13 17:57 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Kristen Carlson Accardi, Tony Luck, Alexander Lobakin,
	Josh Poimboeuf, H. Peter Anvin, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Arnd Bergmann, Joerg Roedel, Arvind Sankar,
	Jing Yangyang, Abaci Robot, Jiapeng Chong, Nathan Chancellor,
	Nick Desaulniers, Vincenzo Frascino, Andrey Konovalov,
	Miroslav Benes, H. Nikolaus Schaller, Fangrui Song, linux-kernel,
	x86, linux-arch, linux-hardening

From: Kristen Carlson Accardi <kristen@linux.intel.com>

When using -ffunction-sections to place each function in its own text
section (so it can be randomized at load time in the future FGKASLR
series), the linker will place most of the functions into separate .text.*
sections. SIZEOF(.text) won't work here for calculating the ORC lookup
table size, so the total text size must be calculated to include .text
AND all .text.* sections.

Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
[ alobakin: move it to vmlinux.lds.h and make arch-indep ]
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/asm-generic/vmlinux.lds.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index f2984af2b85b..e8234911dc18 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -869,10 +869,11 @@
 		KEEP(*(.orc_unwind))					\
 		__stop_orc_unwind = .;					\
 	}								\
+	text_size = _etext - _stext;					\
 	. = ALIGN(4);							\
 	.orc_lookup : AT(ADDR(.orc_lookup) - LOAD_OFFSET) {		\
 		orc_lookup = .;						\
-		. += (((SIZEOF(.text) + LOOKUP_BLOCK_SIZE - 1) /	\
+		. += (((text_size + LOOKUP_BLOCK_SIZE - 1) /		\
 			LOOKUP_BLOCK_SIZE) + 1) * 4;			\
 		orc_lookup_end = .;					\
 	}
-- 
2.30.2


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

* Re: [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch
  2021-10-13 17:57 ` [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch Kees Cook
@ 2021-10-13 18:10   ` Nick Desaulniers
  0 siblings, 0 replies; 10+ messages in thread
From: Nick Desaulniers @ 2021-10-13 18:10 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Josh Poimboeuf, H. Peter Anvin,
	Kristen Carlson Accardi, Tony Luck, Alexander Lobakin,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Arnd Bergmann,
	Joerg Roedel, Arvind Sankar, Jing Yangyang, Abaci Robot,
	Jiapeng Chong, Nathan Chancellor, Vincenzo Frascino,
	Andrey Konovalov, Miroslav Benes, H. Nikolaus Schaller,
	Fangrui Song, linux-kernel, x86, linux-arch, linux-hardening

On Wed, Oct 13, 2021 at 10:57 AM Kees Cook <keescook@chromium.org> wrote:
>
> Under earlyprintk, each RNG call produces a debug report line. To support
> the future FGKASLR feature, which will fetch random bytes during function
> shuffling, this is not useful information (each line is identical and
> tells us nothing new), needlessly spamming the console. Instead, allow
> for a NULL "purpose" to suppress the debug reporting.

Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  arch/x86/lib/kaslr.c | 18 ++++++++++++------
>  1 file changed, 12 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/lib/kaslr.c b/arch/x86/lib/kaslr.c
> index a53665116458..2b3eb8c948a3 100644
> --- a/arch/x86/lib/kaslr.c
> +++ b/arch/x86/lib/kaslr.c
> @@ -56,11 +56,14 @@ unsigned long kaslr_get_random_long(const char *purpose)
>         unsigned long raw, random = get_boot_seed();
>         bool use_i8254 = true;
>
> -       debug_putstr(purpose);
> -       debug_putstr(" KASLR using");
> +       if (purpose) {
> +               debug_putstr(purpose);
> +               debug_putstr(" KASLR using");
> +       }
>
>         if (has_cpuflag(X86_FEATURE_RDRAND)) {
> -               debug_putstr(" RDRAND");
> +               if (purpose)
> +                       debug_putstr(" RDRAND");
>                 if (rdrand_long(&raw)) {
>                         random ^= raw;
>                         use_i8254 = false;
> @@ -68,7 +71,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
>         }
>
>         if (has_cpuflag(X86_FEATURE_TSC)) {
> -               debug_putstr(" RDTSC");
> +               if (purpose)
> +                       debug_putstr(" RDTSC");
>                 raw = rdtsc();
>
>                 random ^= raw;
> @@ -76,7 +80,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
>         }
>
>         if (use_i8254) {
> -               debug_putstr(" i8254");
> +               if (purpose)
> +                       debug_putstr(" i8254");
>                 random ^= i8254();
>         }
>
> @@ -86,7 +91,8 @@ unsigned long kaslr_get_random_long(const char *purpose)
>             : "a" (random), "rm" (mix_const));
>         random += raw;
>
> -       debug_putstr("...\n");
> +       if (purpose)
> +               debug_putstr("...\n");
>
>         return random;
>  }
> --
> 2.30.2
>


-- 
Thanks,
~Nick Desaulniers

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

* Re: [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext
  2021-10-13 17:57 ` [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext Kees Cook
@ 2021-10-14  0:29   ` Josh Poimboeuf
  0 siblings, 0 replies; 10+ messages in thread
From: Josh Poimboeuf @ 2021-10-14  0:29 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Kristen Carlson Accardi, Tony Luck,
	Alexander Lobakin, H. Peter Anvin, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Arnd Bergmann, Joerg Roedel, Arvind Sankar,
	Jing Yangyang, Abaci Robot, Jiapeng Chong, Nathan Chancellor,
	Nick Desaulniers, Vincenzo Frascino, Andrey Konovalov,
	Miroslav Benes, H. Nikolaus Schaller, Fangrui Song, linux-kernel,
	x86, linux-arch, linux-hardening

On Wed, Oct 13, 2021 at 10:57:42AM -0700, Kees Cook wrote:
> From: Kristen Carlson Accardi <kristen@linux.intel.com>
> 
> When using -ffunction-sections to place each function in its own text
> section (so it can be randomized at load time in the future FGKASLR
> series), the linker will place most of the functions into separate .text.*
> sections. SIZEOF(.text) won't work here for calculating the ORC lookup
> table size, so the total text size must be calculated to include .text
> AND all .text.* sections.
> 
> Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
> Reviewed-by: Tony Luck <tony.luck@intel.com>
> Tested-by: Tony Luck <tony.luck@intel.com>
> Reviewed-by: Kees Cook <keescook@chromium.org>
> [ alobakin: move it to vmlinux.lds.h and make arch-indep ]
> Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>

Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>

-- 
Josh


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

* Re: [PATCH 0/4] x86: Various clean-ups in support of FGKASLR
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
                   ` (3 preceding siblings ...)
  2021-10-13 17:57 ` [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext Kees Cook
@ 2021-10-15 18:27 ` Alexander Lobakin
  2021-10-27  6:10 ` Kees Cook
  2021-10-27  6:57 ` Peter Zijlstra
  6 siblings, 0 replies; 10+ messages in thread
From: Alexander Lobakin @ 2021-10-15 18:27 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alexander Lobakin, Borislav Petkov, Josh Poimboeuf,
	H. Peter Anvin, Kristen Carlson Accardi, Tony Luck,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Arnd Bergmann,
	Joerg Roedel, Arvind Sankar, Jing Yangyang, Abaci Robot,
	Jiapeng Chong, Nathan Chancellor, Nick Desaulniers,
	Vincenzo Frascino, Andrey Konovalov, Miroslav Benes,
	H. Nikolaus Schaller, Fangrui Song, linux-kernel, x86,
	linux-arch, linux-hardening

From: Kees Cook <keescook@chromium.org>
Date: Wed, 13 Oct 2021 10:57:38 -0700

> Hi,
> 
> These are a small set of patches that clean up various things that are
> each stand-alone improvements, but they're also needed for the coming
> FGKASLR series[1]. I thought it best to just get these landed instead
> of having them continue to tag along with FGKASLR, especially the
> early malloc() fix, which is a foot-gun waiting to happen. :)

Thanks for picking this! Those really are standalone guys.

> Thanks!
> 
> -Kees
> 
> [1] https://lore.kernel.org/lkml/20210831144114.154-1-alexandr.lobakin@intel.com/
> 
> Kees Cook (2):
>   x86/boot: Allow a "silent" kaslr random byte fetch
>   x86/boot/compressed: Avoid duplicate malloc() implementations
> 
> Kristen Carlson Accardi (2):
>   x86/tools/relocs: Support >64K section headers
>   vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext
> 
>  arch/x86/boot/compressed/kaslr.c  |   4 --
>  arch/x86/boot/compressed/misc.c   |   3 +
>  arch/x86/boot/compressed/misc.h   |   2 +
>  arch/x86/lib/kaslr.c              |  18 ++++--
>  arch/x86/tools/relocs.c           | 103 ++++++++++++++++++++++--------
>  include/asm-generic/vmlinux.lds.h |   3 +-
>  include/linux/decompress/mm.h     |  12 +++-
>  7 files changed, 107 insertions(+), 38 deletions(-)
> 
> -- 
> 2.30.2

Thanks,
Al

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

* Re: [PATCH 0/4] x86: Various clean-ups in support of FGKASLR
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
                   ` (4 preceding siblings ...)
  2021-10-15 18:27 ` [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Alexander Lobakin
@ 2021-10-27  6:10 ` Kees Cook
  2021-10-27  6:57 ` Peter Zijlstra
  6 siblings, 0 replies; 10+ messages in thread
From: Kees Cook @ 2021-10-27  6:10 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Josh Poimboeuf, H. Peter Anvin, Kristen Carlson Accardi,
	Tony Luck, Alexander Lobakin, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Arnd Bergmann, Joerg Roedel, Arvind Sankar,
	Jing Yangyang, Abaci Robot, Jiapeng Chong, Nathan Chancellor,
	Nick Desaulniers, Vincenzo Frascino, Andrey Konovalov,
	Miroslav Benes, H. Nikolaus Schaller, Fangrui Song, linux-kernel,
	x86, linux-arch, linux-hardening

On Wed, Oct 13, 2021 at 10:57:38AM -0700, Kees Cook wrote:
> Hi,
> 
> These are a small set of patches that clean up various things that are
> each stand-alone improvements, but they're also needed for the coming
> FGKASLR series[1]. I thought it best to just get these landed instead
> of having them continue to tag along with FGKASLR, especially the
> early malloc() fix, which is a foot-gun waiting to happen. :)
> 
> Thanks!
> 
> -Kees
> 
> [1] https://lore.kernel.org/lkml/20210831144114.154-1-alexandr.lobakin@intel.com/

Peter, Josh, Boris, can someone please take these through -tip?

They're each stand-alone correctness improvements, and while FGKASLR
depends on them, there is no reason to keep them tied to that series,
especially since anyone using the early-boot malloc or making changes to
text sections is going to trip over one or several of the issues fixed
here.

They've got a bunch of reviews and acks already:
https://patchwork.kernel.org/project/linux-hardening/list/?series=562929

Thanks!

-Kees

> 
> Kees Cook (2):
>   x86/boot: Allow a "silent" kaslr random byte fetch
>   x86/boot/compressed: Avoid duplicate malloc() implementations
> 
> Kristen Carlson Accardi (2):
>   x86/tools/relocs: Support >64K section headers
>   vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext
> 
>  arch/x86/boot/compressed/kaslr.c  |   4 --
>  arch/x86/boot/compressed/misc.c   |   3 +
>  arch/x86/boot/compressed/misc.h   |   2 +
>  arch/x86/lib/kaslr.c              |  18 ++++--
>  arch/x86/tools/relocs.c           | 103 ++++++++++++++++++++++--------
>  include/asm-generic/vmlinux.lds.h |   3 +-
>  include/linux/decompress/mm.h     |  12 +++-
>  7 files changed, 107 insertions(+), 38 deletions(-)
> 
> -- 
> 2.30.2
> 

-- 
Kees Cook

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

* Re: [PATCH 0/4] x86: Various clean-ups in support of FGKASLR
  2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
                   ` (5 preceding siblings ...)
  2021-10-27  6:10 ` Kees Cook
@ 2021-10-27  6:57 ` Peter Zijlstra
  6 siblings, 0 replies; 10+ messages in thread
From: Peter Zijlstra @ 2021-10-27  6:57 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Josh Poimboeuf, H. Peter Anvin,
	Kristen Carlson Accardi, Tony Luck, Alexander Lobakin,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Arnd Bergmann,
	Joerg Roedel, Arvind Sankar, Jing Yangyang, Abaci Robot,
	Jiapeng Chong, Nathan Chancellor, Nick Desaulniers,
	Vincenzo Frascino, Andrey Konovalov, Miroslav Benes,
	H. Nikolaus Schaller, Fangrui Song, linux-kernel, x86,
	linux-arch, linux-hardening

On Wed, Oct 13, 2021 at 10:57:38AM -0700, Kees Cook wrote:
> Kees Cook (2):
>   x86/boot: Allow a "silent" kaslr random byte fetch
>   x86/boot/compressed: Avoid duplicate malloc() implementations
> 
> Kristen Carlson Accardi (2):
>   x86/tools/relocs: Support >64K section headers
>   vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext
> 
>  arch/x86/boot/compressed/kaslr.c  |   4 --
>  arch/x86/boot/compressed/misc.c   |   3 +
>  arch/x86/boot/compressed/misc.h   |   2 +
>  arch/x86/lib/kaslr.c              |  18 ++++--
>  arch/x86/tools/relocs.c           | 103 ++++++++++++++++++++++--------
>  include/asm-generic/vmlinux.lds.h |   3 +-
>  include/linux/decompress/mm.h     |  12 +++-
>  7 files changed, 107 insertions(+), 38 deletions(-)

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

Boris, these are indeed all improvements to the status quo, irrespective
of future FGKASLR work. Do you want to take them, or should I stick them
in x86/core ?

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

end of thread, other threads:[~2021-10-27  6:58 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 17:57 [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Kees Cook
2021-10-13 17:57 ` [PATCH 1/4] x86/tools/relocs: Support >64K section headers Kees Cook
2021-10-13 17:57 ` [PATCH 2/4] x86/boot: Allow a "silent" kaslr random byte fetch Kees Cook
2021-10-13 18:10   ` Nick Desaulniers
2021-10-13 17:57 ` [PATCH 3/4] x86/boot/compressed: Avoid duplicate malloc() implementations Kees Cook
2021-10-13 17:57 ` [PATCH 4/4] vmlinux.lds.h: Have ORC lookup cover entire _etext - _stext Kees Cook
2021-10-14  0:29   ` Josh Poimboeuf
2021-10-15 18:27 ` [PATCH 0/4] x86: Various clean-ups in support of FGKASLR Alexander Lobakin
2021-10-27  6:10 ` Kees Cook
2021-10-27  6:57 ` Peter Zijlstra

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).