Function Granular Kernel Address Space Layout Randomization
Subsystems (6)
cover Function Granular Kernel Address Space Layout Randomization
This patch set is an implementation of finer grained kernel address space randomization. It rearranges your kernel code at load time
on a per-function level granularity, with only around a second added to boot time.
Background
----------
KASLR was merged into the kernel with the objective of increasing the difficulty of code reuse attacks. Code reuse attacks reused existing code snippets to get around existing memory protections. They exploit software bugs which expose addresses of useful code snippets to control the flow of execution for their own nefarious purposes. KASLR moves the entire kernel code text as a unit at boot time in order to make addresses less predictable. The order of the code within the segment is unchanged - only the base address is shifted. There are a few shortcomings to this algorithm.
1. Low Entropy - there are only so many locations the kernel can fit in. This means an attacker could guess without too much trouble.
2. Knowledge of a single address can reveal the offset of the base address, exposing all other locations for a published/known kernel image.
3. Info leaks abound.
Finer grained ASLR has been proposed as a way to make ASLR more resistant to info leaks. It is not a new concept at all, and there are many variations possible. Function reordering is an implementation of finer grained ASLR which randomizes the layout of an address space on a function level granularity. We use the term "fgkaslr" in this document to refer to the technique of function reordering when used with KASLR, as well as finer grained KASLR in general.
Proposed Improvement
--------------------
This patch set proposes adding function reordering on top of the existing KASLR base address randomization. The over-arching objective is incremental improvement over what we already have, as well as something that can be merged and deployed with as little disruption to our existing kernel/ecosystem as possible. It is designed to work with the existing solution, although it can be used independently (not sure why you would do that though...). The implementation is really pretty simple, and there are 2 main area where changes occur:
* Build time
GCC has an option to place functions into individual .text sections. We can use this option to implement function reordering at load time. The final compiled vmlinux retains all the section headers, which can be used to help us find the address ranges of each function. Using this information and an expanded table of relocation addresses, we can shuffle the individual text sections immediately after decompression. You are probably asking yourself how this could possibly work given the number of tables of addresses that exist inside the kernel today for features such as exception handling and kprobes. Most of these tables generate relocations and require a simple update, and some tables that have assumptions about order require sorting after the update. In order to modify these tables, we preserve a few key symbols from the objcopy symbol stripping process for use after shuffling the text segments.
Some highlights from the build time changes to look for:
The top level kernel Makefile was modified to add the gcc flag if it is supported. Currently, I am applying this flag to everything it is possible to randomize. Future work could turn off this flags for selected files or even entire subsystems, although obviously at the cost of security.
The relocs tool is updated to add relative relocations. This information previously wasn't included because it wasn't necessary when moving the entire .text segment as a unit.
A new file was created to contain a list of symbols that objcopy should keep. We use those symbols at load time as described below.
* Load time
The boot kernel was modified to parse the vmlinux elf file after decompression to check for our interesting symbols that we kept, and to look for any .text.* sections to randomize. We then shuffle the sections and update any tables that need to be updated or resorted. The existing code which updated relocation addresses was modified to account for not just a fixed delta from the load address, but the offset that the function section was moved to. This requires inspection of each address to see if it was impacted by a randomization. We use a bsearch to make this less horrible on performance.
In order to hide our new layout, symbols reported through /proc/kallsyms will be sorted by name alphabetically rather than by address.
Security Considerations
-----------------------
The objective of this patch set is to improve a technology that is already merged into the kernel (KASLR). Obviously, this code is not a one stop shopping place for blocking code reuse attacks, but should instead be considered as one of several tools that can be used. In particular, this code is meant to make KASLR more effective in the presence of info leaks. A key point to note is that we are basically accepting that there are many and various ways to leak address today and in the future, and rather than assume that we can stop them all, we should find a method which makes individual info leaks less important. How much entropy we are adding to the existing entropy of standard KASLR will depend on a few variables. Firstly and most obviously, the number of functions you randomize matters. This implementation keeps the existing .text section for code that cannot be randomized - for example, because it was assembly code, or we opted out of randomization for performance reasons. The less sections to randomize, the less entropy. In addition, due to alignment (16 bytes for x86_64), the number of bits in a address that the attacker needs to guess is reduced, as the lower bits are identical.
Performance Impact
------------------
There are two areas where function reordering can impact performance: boot time latency, and run time performance.
* Boot time latency
This implementation of finer grained KASLR impacts the boot time of the kernel in several places. It requires additional parsing of the kernel ELF file to obtain the section headers of the sections to be randomized. It calls the random number generator for each section to be randomized to determine that section's new memory location. It copies the decompressed kernel into a new area of memory to avoid corruption when laying out the newly randomized sections. It increases the number of relocations the kernel has to perform at boot time vs. standard KASLR, and it also requires a lookup on each address that needs to be relocated to see if it was in a randomized section and needs to be adjusted by a new offset. Finally, it re-sorts a few data tables that are required to be sorted by address.
Booting a test VM on a modern, well appointed system showed an increase in latency of approximately 1 second.
* Run time
The performance impact at run-time of function reordering varies by workload. Using kcbench, a kernel compilation benchmark, the performance of a kernel build with finer grained KASLR was about 1% slower than a kernel with standard KASLR. Analysis with perf showed a slightly higher percentage of L1-icache-load-misses. Other workloads were examined as well, with varied results. Some workloads performed significantly worse under FGKASLR, while others stayed the same or were mysteriously better. In general, it will depend on the code flow whether or not finer grained KASLR will impact your workload, and how the underlying code was designed. Future work could identify hot areas that may not be randomized and either leave them in the .text section or group them together into a single section that may be randomized. If grouping things together helps, one other thing to consider is that if we could identify text blobs that should be grouped together to benefit a particular code flow, it could be interesting to explore whether this security feature could be also be used as a performance feature if you are interested in optimizing your kernel layout for a particular workload at boot time. Optimizing function layout for a particular workload has been researched and proven effective - for more information read the Facebook paper "Optimizing Function Placement for Large-Scale Data-Center Applications" (see references section below).
Image Size
----------
Adding additional section headers as a result of compiling with -ffunction-sections will increase the size of the vmlinux ELF file. With a standard distro config, the resulting vmlinux was increased by about 3%. The compressed image is also increased due to the header files, as well as the extra relocations that must be added. You can expect fgkaslr to increase the size of the compressed image by about 15%.
Building
--------
To enable fine grained KASLR, you need to have the following config options set (including all the ones you would use to build normal KASLR)
CONFIG_FG_KASLR=y
In addition, fgkaslr is only supported for the X86_64 architecture.
Modules
-------
Modules are randomized similarly to the rest of the kernel by shuffling the sections at load time prior to moving them into memory. The module must also have been build with the -ffunction-sections compiler option.
References
----------
There are a lot of academic papers which explore finer grained ASLR. This paper in particular contributed the most to my implementation design as well as my overall understanding of the problem space:
Selfrando: Securing the Tor Browser against De-anonymization Exploits, M. Conti, S. Crane, T. Frassetto, et al.
For more information on how function layout impacts performance, see:
Optimizing Function Placement for Large-Scale Data-Center Applications, G. Ottoni, B. Maher
Kees Cook (1):
x86/boot: Allow a "silent" kaslr random byte fetch
Kristen Carlson Accardi (8):
objtool: do not assume order of parent/child functions
x86: tools/relocs: Support >64K section headers
x86: Makefile: Add build and config option for CONFIG_FG_KASLR
x86: make sure _etext includes function sections x86/tools: Adding relative relocs for randomized functions
x86: Add support for function granular KASLR
kallsyms: hide layout
module: Reorder functions
Documentation/security/fgkaslr.rst | 151 +++++ Documentation/security/index.rst | 1 + Makefile | 4 + arch/x86/Kconfig | 13 + arch/x86/boot/compressed/Makefile | 10 +- arch/x86/boot/compressed/fgkaslr.c | 809 +++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 7 +- arch/x86/boot/compressed/misc.c | 107 ++- arch/x86/boot/compressed/misc.h | 31 + arch/x86/boot/compressed/utils.c | 12 + arch/x86/boot/compressed/vmlinux.symbols | 18 + arch/x86/include/asm/boot.h | 15 +- arch/x86/kernel/vmlinux.lds.S | 18 +- arch/x86/lib/kaslr.c | 18 +- arch/x86/tools/relocs.c | 143 +++- arch/x86/tools/relocs.h | 4 +- arch/x86/tools/relocs_common.c | 14 +- include/asm-generic/vmlinux.lds.h | 2 +- include/uapi/linux/elf.h | 1 + kernel/kallsyms.c | 138 +++- kernel/module.c | 82 +++ tools/objtool/elf.c | 8 +- 22 files changed, 1543 insertions(+), 63 deletions(-) create mode 100644 Documentation/security/fgkaslr.rst create mode 100644 arch/x86/boot/compressed/fgkaslr.c create mode 100644 arch/x86/boot/compressed/utils.c create mode 100644 arch/x86/boot/compressed/vmlinux.symbols
Signature · 1 line
2.20.1
PATCH 1/9 objtool: do not assume order of parent/child functions
If a .cold function is examined prior to it's parent, the link to the parent/child function can be overwritten when the parent is examined. Only update pfunc and cfunc if they were previously nil to prevent this from happening.
--- tools/objtool/elf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/objtool/elf.c +7 -1
@@ -408,7 +408,13 @@ static int read_symbols(struct elf *elf)size_t pnamelen;if (sym->type !=STT_FUNC )continue ; - sym->pfunc = sym->cfunc = sym; + +if (sym->pfunc ==NULL ) + sym->pfunc = sym; + +if (sym->cfunc ==NULL ) + sym->cfunc = sym; + coldstr =strstr (sym->name,".cold" );if (!coldstr)continue ;
Signature · 1 line
2.20.1
Re: [PATCH 1/9] objtool: do not assume order of parent/child functions
Quoted 7 lines · Kristen Carlson Accardi
If a .cold function is examined prior to it's parent, the link to the parent/child function can be overwritten when the parent is examined. Only update pfunc and cfunc if they were previously nil to prevent this from happening.
Signed-off-by Kristen Carlson Accardi <[email protected]>Acked-by Josh Poimboeuf <[email protected]>
Hi Kristen,
I grabbed this one and it will be merged into the -tip tree soon. Thanks!
Signature · 1 line
Josh
PATCH 2/9 x86: tools/relocs: Support >64K section headers
In reply to Kristen Carlson AccardiWhile it is already supported to find the total number of section headers if we exceed 64K sections, we need to support the extended symbol table to get section header indexes for symbols when there are > 64K sections. 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 we can read the value directly or whether we need to pull it out of the extended table.
--- arch/x86/tools/relocs.c | 104 ++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 26 deletions(-)
arch/x86/tools/relocs.c +78 -26
@@ -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;
@@ -32,6 +36,7 @@ struct section {Elf_Shdr shdr;struct section *link;Elf_Sym *symtab; +Elf32_Word *xsymtab;Elf_Rel *reltab;char *strtab; };
@@ -265,7 +270,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; }
@@ -335,6 +340,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 ) {
@@ -468,31 +490,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 %d bytes for xsymtab failed\n" , + sec->shdr.sh_size); + } +if (fseek (fp, sec->shdr.sh_offset,SEEK_SET ) <0 ) { +die ("Seek to %d 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 %d bytes for symtab failed\n" , + sec->shdr.sh_size); + } +if (fseek (fp, sec->shdr.sh_offset,SEEK_SET ) <0 ) { +die ("Seek to %d 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 %d bytes for symtab failed\n" , - sec->shdr.sh_size); - } -if (fseek (fp, sec->shdr.sh_offset,SEEK_SET ) <0 ) { -die ("Seek to %d 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); } } }
@@ -759,13 +810,14 @@ 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 ); } -static int do_reloc64 (struct section *sec,Elf_Rel *rel,ElfW (Sym) *sym,const char *symname) {
@@ -1088,7 +1140,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 ; }
Signature · 1 line
2.20.1
PATCH 3/9 x86/boot: Allow a "silent" kaslr random byte fetch
In reply to Kristen Carlson AccardiFrom: Kees Cook <[email protected]>
Under earlyprintk, each RNG call produces a debug report line. When shuffling hundreds of functions, this is not useful information (each line is identical and tells us nothing new). Instead, allow for a NULL "purpose" to suppress the debug reporting.
--- arch/x86/lib/kaslr.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
arch/x86/lib/kaslr.c +12 -6
@@ -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; }
Signature · 1 line
2.20.1
PATCH 4/9 x86: Makefile: Add build and config option for CONFIG_FG_KASLR
In reply to Kristen Carlson AccardiAllow user to select CONFIG_FG_KASLR if dependencies are met. Change the make file to build with -ffunction-sections if CONFIG_FG_KASLR
--- Makefile | 4 ++++ arch/x86/Kconfig | 13 +++++++++++++ 2 files changed, 17 insertions(+)
Makefile +4 -0
@@ -866,6 +866,10 @@ ifdef CONFIG_LIVEPATCH KBUILD_CFLAGS += $(call cc-option, -flive-patching=inline-clone) endif +ifdef CONFIG_FG_KASLR +KBUILD_CFLAGS += -ffunction-sections +endif + # arch Makefile may override CC so keep this after arch Makefile is included NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
arch/x86/Kconfig +13 -0
@@ -2182,6 +2182,19 @@ config RANDOMIZE_BASE If unsure, say Y. +config FG_KASLR + bool "Function Granular Kernel Address Space Layout Randomization" + depends on $(cc-option, -ffunction-sections) + depends on RANDOMIZE_BASE && X86_64 + help + This option improves the randomness of the kernel text + over basic Kernel Address Space Layout Randomization (KASLR) + by reordering the kernel text at boot time. This feature + uses information generated at compile time to re-layout the + kernel text section at boot time at function level granularity. + + If unsure, say N. + # Relocation on x86 needs some additional build support config X86_NEED_RELOCS def_bool y
Signature · 1 line
2.20.1
PATCH 5/9 x86: make sure _etext includes function sections
In reply to Kristen Carlson AccardiWe will be using -ffunction-sections to place each function in it's own text section so it can be randomized at load time. The linker considers these .text.* sections "orphaned sections", and will place them after the first similar section (.text). However, we need to move _etext so that it is after both .text and .text.* We also need to calculate text size to include .text AND .text.*
--- arch/x86/kernel/vmlinux.lds.S | 18 +++++++++++++++++- include/asm-generic/vmlinux.lds.h | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kernel/vmlinux.lds.S +17 -1
@@ -147,8 +147,24 @@ SECTIONS #endif } :text =0xcccc - /* End of text section, which should occupy whole number of pages */ +#ifdef CONFIG_FG_KASLR + /* + * -ffunction-sections creates .text.* sections, which are considered + * "orphan sections" and added after the first similar section (.text). + * Adding this ALIGN statement causes the address of _etext + * to be below that of all the .text.* orphaned sections + */ + . = ALIGN(PAGE_SIZE); +#endif _etext = .; + + /* + * the size of the .text section is used to calculate the address + * range for orc lookups. If we just use SIZEOF(.text), we will + * miss all the .text.* sections. Calculate the size using _etext + * and _stext and save the value for later. + */ + text_size = _etext - _stext; . = ALIGN(PAGE_SIZE); X86_ALIGN_RODATA_BEGIN
include/asm-generic/vmlinux.lds.h +1 -1
@@ -813,7 +813,7 @@ . = 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 = .; \ }
Signature · 1 line
2.20.1
PATCH 6/9 x86/tools: Adding relative relocs for randomized functions
In reply to Kristen Carlson AccardiIf we are randomizing function sections, we are going to need to recalculate relative offsets for relocs that are either in the randomized sections, or refer to the randomized sections. Add code to detect whether a reloc satisifies these cases, and if so, add them to the appropriate reloc list.
--- arch/x86/boot/compressed/Makefile | 7 +++++- arch/x86/tools/relocs.c | 41 ++++++++++++++++++++++++++++--- arch/x86/tools/relocs.h | 4 +-- arch/x86/tools/relocs_common.c | 14 +++++++---- 4 files changed, 54 insertions(+), 12 deletions(-)
arch/x86/boot/compressed/Makefile +6 -1
@@ -117,6 +117,11 @@ $(obj)/vmlinux: $(vmlinux-objs-y) FORCE $(call if_changed,check-and-link-vmlinux) OBJCOPYFLAGS_vmlinux.bin := -R .comment -S + +ifdef CONFIG_FG_KASLR + RELOCS_ARGS += --fg-kaslr +endif + $(obj)/vmlinux.bin: vmlinux FORCE $(call if_changed,objcopy)
@@ -124,7 +129,7 @@ targets += $(patsubst $(obj)/%,%,$(vmlinux-objs-y)) vmlinux.bin.all vmlinux.relo CMD_RELOCS = arch/x86/tools/relocs quiet_cmd_relocs = RELOCS $@ - cmd_relocs = $(CMD_RELOCS) $< > $@;$(CMD_RELOCS) --abs-relocs $< + cmd_relocs = $(CMD_RELOCS) $(RELOCS_ARGS) $< > $@;$(CMD_RELOCS) $(RELOCS_ARGS) --abs-relocs $< $(obj)/vmlinux.relocs: vmlinux FORCE $(call if_changed,relocs)
arch/x86/tools/relocs.c +37 -4
70 lines not on this page · raw message
arch/x86/tools/relocs.h +2 -2
@@ -31,8 +31,8 @@ enum symtype {void process_32 (FILE *fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs, -int show_reloc_info); +int show_reloc_info,int fg_kaslr);void process_64 (FILE *fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs, -int show_reloc_info); +int show_reloc_info,int fg_kaslr);#endif /* RELOCS_H */
arch/x86/tools/relocs_common.c +9 -5
@@ -12,14 +12,13 @@ void die(char *fmt, ...)static void usage (void ) { -die ("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode]" \ -" vmlinux\n" ); +die ("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode|--fg-kaslr] vmlinux\n" ); }int main (int argc,char **argv) {int show_absolute_syms, show_absolute_relocs, show_reloc_info; -int as_text, use_real_mode; +int as_text, use_real_mode, fg_kaslr;const char *fname;FILE *fp;int i;
@@ -30,6 +29,7 @@ int main(int argc, char **argv) show_reloc_info =0 ; as_text =0 ; use_real_mode =0 ; + fg_kaslr =0 ; fname =NULL ;for (i =1 ; i < argc; i++) {char *arg = argv[i];
@@ -54,6 +54,10 @@ int main(int argc, char **argv) use_real_mode =1 ;continue ; } +if (strcmp (arg,"--fg-kaslr" ) ==0 ) { + fg_kaslr =1 ; +continue ; + } }else if (!fname) { fname = arg;
@@ -75,11 +79,11 @@ int main(int argc, char **argv)if (e_ident[EI_CLASS ] ==ELFCLASS64 )process_64 (fp, use_real_mode, as_text, show_absolute_syms, show_absolute_relocs, - show_reloc_info); + show_reloc_info, fg_kaslr);else process_32 (fp, use_real_mode, as_text, show_absolute_syms, show_absolute_relocs, - show_reloc_info); + show_reloc_info, fg_kaslr);fclose (fp);return 0 ; }
Signature · 1 line
2.20.1
PATCH 7/9 x86: Add support for function granular KASLR
In reply to Kristen Carlson AccardiAt boot time, find all the function sections that have separate .text sections, shuffle them, and then copy them to new locations. Adjust any relocations accordingly.
--- Documentation/security/fgkaslr.rst | 151 +++++ Documentation/security/index.rst | 1 + arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/fgkaslr.c | 809 +++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 7 +- arch/x86/boot/compressed/misc.c | 107 ++- arch/x86/boot/compressed/misc.h | 31 + arch/x86/boot/compressed/utils.c | 12 + arch/x86/boot/compressed/vmlinux.symbols | 18 + arch/x86/include/asm/boot.h | 15 +- include/uapi/linux/elf.h | 1 + 11 files changed, 1139 insertions(+), 16 deletions(-) create mode 100644 Documentation/security/fgkaslr.rst create mode 100644 arch/x86/boot/compressed/fgkaslr.c create mode 100644 arch/x86/boot/compressed/utils.c create mode 100644 arch/x86/boot/compressed/vmlinux.symbols
Documentation/security/fgkaslr.rst new file +151 -0
@@ -0,0 +1,151 @@ +===================================================================== +Function Granular Kernel Address Space Layout Randomization (fgkaslr) +===================================================================== + +:Date: 6 April 2020 +:Author: Kristen Accardi + +Kernel Address Space Layout Randomization (KASLR) was merged into the kernel +with the objective of increasing the difficulty of code reuse attacks. Code +reuse attacks reused existing code snippets to get around existing memory +protections. They exploit software bugs which expose addresses of useful code +snippets to control the flow of execution for their own nefarious purposes. +KASLR as it was originally implemented moves the entire kernel code text as a +unit at boot time in order to make addresses less predictable. The order of the +code within the segment is unchanged - only the base address is shifted. There +are a few shortcomings to this algorithm. + +1. Low Entropy - there are only so many locations the kernel can fit in. This + means an attacker could guess without too much trouble. +2. Knowledge of a single address can reveal the offset of the base address, + exposing all other locations for a published/known kernel image. +3. Info leaks abound. + +Finer grained ASLR has been proposed as a way to make ASLR more resistant +to info leaks. It is not a new concept at all, and there are many variations +possible. Function reordering is an implementation of finer grained ASLR +which randomizes the layout of an address space on a function level +granularity. We use the term "fgkaslr" in this document to refer to the +technique of function reordering when used with KASLR, as well as finer grained +KASLR in general. + +Implementation Details +====================== + +The over-arching objective of the fgkaslr implementation is incremental +improvement over what we already have, as well as something that can be +merged and deployed with as little disruption to our existing kernel/ecosystem +as possible. It is designed to work with the existing solution, and there are +two main area where code changes occur: Build time, and Load time. + +Build time +---------- + +GCC has an option to place functions into individual .text sections. We can +use this option to implement function reordering at load time. The final +compiled vmlinux retains all the section headers, which can be used to help +us find the address ranges of each function. Using this information and +an expanded table of relocation addresses, we can shuffle the individual +text sections immediately after decompression. You are probably asking +yourself how this could possibly work given the number of tables of +addresses that exist inside the kernel today for features such as exception +handling and kprobes. Most of these tables generate relocations and +require a simple update, and some tables that have assumptions about order +require sorting after the update. In order to modify these tables, we +preserve a few key symbols from the objcopy symbol stripping process for use +after shuffling the text segments. + +Load time +--------- + +The boot kernel was modified to parse the vmlinux elf file after +decompression to check for our interesting symbols that we kept, and to +look for any .text.* sections to randomize. We then shuffle the sections +and update any tables that need to be updated or resorted. The existing +code which updated relocation addresses was modified to account for not +just a fixed delta from the load address, but the offset that the function +section was moved to. This requires inspection of each address to see if +it was impacted by a randomization. In order to hide our new layout, +symbols reported through /proc/kallsyms will be sorted by name alphabetically +rather than by address. + +Security Considerations +======================= + +The objective of this patch set is to improve a technology that is already +merged into the kernel (KASLR). Obviously, this code is not a one stop +shopping place for blocking code reuse attacks, but should instead be +considered as one of several tools that can be used. In particular, this +code is meant to make KASLR more effective in the presence of info leaks. +A key point to note is that we are basically accepting that there are +many and various ways to leak address today and in the future, and rather +than assume that we can stop them all, we should find a method which makes +individual info leaks less important. How much entropy we are adding to +the existing entropy of standard KASLR will depend on a few variables. +Firstly and most obviously, the number of functions you randomize matters. +This implementation keeps the existing .text section for code that cannot be +randomized - for example, because it was assembly code, or we opted out of +randomization for performance reasons. The less sections to randomize, the +less entropy. In addition, due to alignment (16 bytes for x86_64), the number +of bits in a address that the attacker needs to guess is reduced, as the lower +bits are identical. + +Performance Impact +================== + +There are two areas where function reordering can impact performance: boot +time latency, and run time performance. + +Boot time latency +----------------- + +This implementation of finer grained KASLR impacts the boot time of the kernel +in several places. It requires additional parsing of the kernel ELF file to +obtain the section headers of the sections to be randomized. It calls the +random number generator for each section to be randomized to determine that +section's new memory location. It copies the decompressed kernel into a new +area of memory to avoid corruption when laying out the newly randomized +sections. It increases the number of relocations the kernel has to perform at +boot time vs. standard KASLR, and it also requires a lookup on each address +that needs to be relocated to see if it was in a randomized section and needs +to be adjusted by a new offset. Finally, it re-sorts a few data tables that +are required to be sorted by address. + +Booting a test VM on a modern, well appointed system showed an increase in +latency of approximately 1 second. + +Run time +-------- + +The performance impact at run-time of function reordering varies by workload. +Randomly reordering the functions will cause an increase in cache misses +for some workloads. You should expect to see some workloads perform +significantly worse under FGKASLR, while others stay the same or even +mysteriously improve. In general, it will depend on the code flow whether or +not finer grained KASLR will impact your workload, and how the underlying code +was designed. + +Image Size +========== + +fgkaslr increases the size of the kernel binary due to the extra section +headers that are included, as well as the extra relocations that need to +be added. You can expect fgkaslr to increase the size of the resulting +vmlinux by about 3%, and the compressed image (bzImage) by 15%. + +Building +======== + +To enable fine grained KASLR, you need to have the following config options +set (including all the ones you would use to build normal KASLR) + +``CONFIG_FG_KASLR=y`` + +In addition, fgkaslr is only supported for the X86_64 architecture. + +Modules +======= + +Modules are randomized similarly to the rest of the kernel by shuffling +the sections at load time prior to moving them into memory. The module must +also have been build with the -ffunction-sections compiler option.
Documentation/security/index.rst +1 -0
@@ -7,6 +7,7 @@ Security Documentation credentials IMA-templates + fgkaslr keys/index lsm lsm-development
arch/x86/boot/compressed/Makefile +3 -0
@@ -80,10 +80,12 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/kernel_info.o $(obj)/head_$(BITS).o vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o +vmlinux-objs-$(CONFIG_FG_KASLR) += $(obj)/utils.o ifdef CONFIG_X86_64 vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr_64.o vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o + vmlinux-objs-$(CONFIG_FG_KASLR) += $(obj)/fgkaslr.o endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
@@ -120,6 +122,7 @@ OBJCOPYFLAGS_vmlinux.bin := -R .comment -S ifdef CONFIG_FG_KASLR RELOCS_ARGS += --fg-kaslr + OBJCOPYFLAGS += --keep-symbols=$(srctree)/$(src)/vmlinux.symbols endif $(obj)/vmlinux.bin: vmlinux FORCE
arch/x86/boot/compressed/fgkaslr.c new file +809 -0
809 lines not on this page · raw message
arch/x86/boot/compressed/kaslr.c +2 -5
@@ -39,10 +39,6 @@#include <generated/utsrelease.h> #include <asm/efi.h> -/* Macros used by the included decompressor code below. */ -#define STATIC -#include <linux/decompress/mm.h> -#ifdef CONFIG_X86_5LEVEL unsigned int __pgtable_l5_enabled;unsigned int pgdir_shift__ro_after_init =39 ;
@@ -897,7 +893,8 @@ void choose_random_location(unsigned long input, {unsigned long random_addr, min_addr; -if (cmdline_find_option_bool ("nokaslr" )) { + nokaslr =cmdline_find_option_bool ("nokaslr" ); +if (nokaslr) {warn ("KASLR disabled: 'nokaslr' on cmdline." );return ; }
arch/x86/boot/compressed/misc.c +98 -9
164 lines not on this page · raw message
arch/x86/boot/compressed/misc.h +31 -0
@@ -39,7 +39,12 @@/* misc.c */ extern memptr free_mem_ptr;extern memptr free_mem_end_ptr; +#define STATIC +#define STATIC_RW_DATA extern +#include <linux/decompress/mm.h> +extern struct boot_params *boot_params; +extern int nokaslr;void __putstr (const char *s);void __puthex (unsigned long value);#define error_putstr (__x) __putstr(__x)
@@ -74,6 +79,32 @@ struct mem_vector {unsigned long long size; }; +#ifdef CONFIG_X86_64 +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr +#define Elf_Shdr Elf64_Shdr +#else +#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr +#define Elf_Shdr Elf32_Shdr +#endif + +#if CONFIG_FG_KASLR +void parse_sections_headers (void *output,Elf_Ehdr *ehdr,Elf_Phdr *phdrs); +void pre_relocations_cleanup (unsigned long map); +void post_relocations_cleanup (unsigned long map); +Elf_Shdr *adjust_address (long *address); +void adjust_relative_offset (long pc,long *value,Elf_Shdr *section); +bool is_percpu_addr (long pc,long offset); +#else +static inline void parse_sections_headers (void *output,Elf_Ehdr *ehdr,Elf_Phdr *phdrs) { } +static inline void pre_relocations_cleanup (unsigned long map) { } +static inline void post_relocations_cleanup (unsigned long map) { } +static inline Elf_Shdr *adjust_address (long *address) {return NULL ; } +static inline void adjust_relative_offset (long pc,long *value,Elf_Shdr *section) { } +static inline bool is_percpu_addr (long pc,long offset) {return true; } +#endif /* CONFIG_FG_KASLR */ +#if CONFIG_RANDOMIZE_BASE /* kaslr.c */ void choose_random_location(unsigned long input,
arch/x86/boot/compressed/utils.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* utils.c +* +* This contains various libraries that are needed for fgkaslr +*/ +#define __DISABLE_EXPORTS +#define _LINUX_KPROBES_H +#define NOKPROBE_SYMBOL (fname) +#include "../../../../lib/sort.c" +#include "../../../../lib/bsearch.c" +
arch/x86/boot/compressed/vmlinux.symbols new file +18 -0
@@ -0,0 +1,18 @@ +kallsyms_offsets +kallsyms_addresses +kallsyms_num_syms +kallsyms_relative_base +kallsyms_names +kallsyms_token_table +kallsyms_token_index +kallsyms_markers +__start___ex_table +__stop___ex_table +_sinittext +_einittext +_stext +_etext +__start_orc_unwind_ip +__stop_orc_unwind_ip +__stop_orc_unwind +__start_orc_unwind
arch/x86/include/asm/boot.h +13 -2
@@ -26,8 +26,19 @@#ifdef CONFIG_KERNEL_BZIP2 # define BOOT_HEAP_SIZE 0x400000 -#else /* !CONFIG_KERNEL_BZIP2 */ -# define BOOT_HEAP_SIZE 0x10000 +#elif CONFIG_FG_KASLR +/* +* We need extra boot heap when using fgkaslr because we make a copy +* of the original decompressed kernel to avoid issues with writing +* over ourselves when shuffling the sections. We also need extra +* space for resorting kallsyms after shuffling. This value could +* be decreased if free() would release memory properly, or if we +* could avoid the kernel copy. It would need to be increased if we +* find additional tables that need to be resorted. +*/ +# define BOOT_HEAP_SIZE 0x4000000 +#else /* !CONFIG_KERNEL_BZIP2 && !CONFIG_FG_KASLR */ +# define BOOT_HEAP_SIZE 0x10000#endif #ifdef CONFIG_X86_64
include/uapi/linux/elf.h +1 -0
@@ -298,6 +298,7 @@ typedef struct elf64_phdr {#define SHN_LIVEPATCH 0xff20#define SHN_ABS 0xfff1#define SHN_COMMON 0xfff2 +#define SHN_XINDEX 0xffff#define SHN_HIRESERVE 0xffff typedef struct elf32_shdr {
Signature · 1 line
2.20.1
kbuild test robot
build botRe: [PATCH 7/9] x86: Add support for function granular KASLR
Thank you for the patch! Yet something to improve:
Hi Kristen,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on tip/auto-latest linus/master tip/x86/core v5.7-rc1 next-20200415] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Kristen-Carlson-Accardi/Function-Granular-Kernel-Address-Space-Layout-Randomization/20200416-072741 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: i386-tinyconfig (attached as .config) compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ld: arch/x86/boot/compressed/misc.o: in function `index_update.isra.12':
>> misc.c:(.text+0x102c): undefined reference to `xz_crc32_table' ld: arch/x86/boot/compressed/misc.o: in function `xz_crc32_init': misc.c:(.text+0x11c0): undefined reference to `xz_crc32_table' ld: arch/x86/boot/compressed/misc.o: in function `xz_crc32': misc.c:(.text+0x1213): undefined reference to `xz_crc32_table' ld: arch/x86/boot/compressed/misc.o: in function `xz_dec_run': misc.c:(.text+0x1e69): undefined reference to `xz_crc32_table' >> ld: misc.c:(.text+0x1f2d): undefined reference to `xz_crc32_table'
ld: arch/x86/boot/compressed/misc.o:misc.c:(.text+0x1ff3): more undefined references to `xz_crc32_table' follow
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH 7/9] x86: Add support for function granular KASLR
In reply to Kristen Carlson AccardiOn 04/15/20 at 02:04pm, Kristen Carlson Accardi wrote:
...
arch/x86/boot/compressed/misc.c line 31
Quoted 11 lines
arch/x86/boot/compressed/misc.c +0
-3@@ -26,9 +26,6 @@* it is not safe to place pointers in static structures. */ -/* Macros used by the included decompressor code below. */ -#define STATIC static -
Here removing STATIC definition might be the reason why the LKP reported build error to patch 7/9.
Quoted 24 lines
arch/x86/boot/compressed/misc.c +7
-3/* * Use normal definitions of mem*() from string.c. There are already * included header files which expect a definition of memset() and by @@ -49,6 +46,8 @@ struct boot_params *boot_params;memptr free_mem_ptr;memptr free_mem_end_ptr; +unsigned long malloc_ptr; +int malloc_count;static char *vidmem;static int vidport;@@ -203,10 +202,20 @@ static void handle_relocations(void *output, unsigned long output_len,if (IS_ENABLED (CONFIG_X86_64 )) delta = virt_addr -LOAD_PHYSICAL_ADDR ; -if (!delta) { -debug_putstr ("No relocation needed... " ); -return ; +/* +* it is possible to have delta be zero and +* still have enabled fg kaslr. We need to perform relocations +* for fgkaslr regardless of whether the base address has moved. +*/
arch/x86/boot/compressed/misc.c static void handle_relocations(void *output, unsigned long output_len, · line 219
Quoted 10 lines
arch/x86/boot/compressed/misc.c +8
-0+if (!IS_ENABLED (CONFIG_FG_KASLR ) || nokaslr) { +if (!delta) { +debug_putstr ("No relocation needed... " ); +return ; + } } + +pre_relocations_cleanup (map); +debug_putstr ("Performing relocations... " );
I testes this patchset on x86_64 machine, it works well. Seems the debug printing need a little bit adjustment.
- debug_putstr("Performing relocations... ");
+ debug_putstr("\nPerforming relocations... ");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Decompressing Linux... Parsing ELF...
Parsing ELF section headers...
Looking for symbols...
Re-sorting kallsyms ...Performing relocations...
~~~~~~~~^
Updating exception table...
Re-sorting exception table...
Quoted 261 lines
arch/x86/boot/compressed/misc.c +83
-3/* @@ -230,35 +239,106 @@ static void handle_relocations(void *output, unsigned long output_len,*/ for (reloc = output + output_len -sizeof (*reloc); *reloc; reloc--) {long extended = *reloc; +long value; + +/* +* if using fgkaslr, we might have moved the address +* of the relocation. Check it to see if it needs adjusting +* from the original address. +*/ + (void )adjust_address (&extended); + extended += map; ptr = (unsigned long )extended;if (ptr < min_addr || ptr > max_addr)error ("32-bit relocation outside of kernel!\n" ); - *(uint32_t *)ptr += delta; + value = *(int32_t *)ptr; + +/* +* If using fgkaslr, the value of the relocation +* might need to be changed because it referred +* to an address that has moved. +*/ +adjust_address (&value); + + value += delta; + + *(uint32_t *)ptr = value; }#ifdef CONFIG_X86_64 while (*--reloc) {long extended = *reloc; +long value; +long oldvalue; +Elf64_Shdr *s; + +/* +* if using fgkaslr, we might have moved the address +* of the relocation. Check it to see if it needs adjusting +* from the original address. +*/ + s =adjust_address (&extended); + extended += map; ptr = (unsigned long )extended;if (ptr < min_addr || ptr > max_addr)error ("inverse 32-bit relocation outside of kernel!\n" ); - *(int32_t *)ptr -= delta; + value = *(int32_t *)ptr; + oldvalue = value; + +/* +* If using fgkaslr, these relocs will contain +* relative offsets which might need to be +* changed because it referred +* to an address that has moved. +*/ +adjust_relative_offset (*reloc, &value, s); + +/* +* only percpu symbols need to have their values adjusted for +* base address kaslr since relative offsets within the .text +* and .text.* sections are ok wrt each other. +*/ +if (is_percpu_addr (*reloc, oldvalue)) + value -= delta; + + *(int32_t *)ptr = value; }for (reloc--; *reloc; reloc--) {long extended = *reloc; +long value; + +/* +* if using fgkaslr, we might have moved the address +* of the relocation. Check it to see if it needs adjusting +* from the original address. +*/ + (void )adjust_address (&extended); + extended += map; ptr = (unsigned long )extended;if (ptr < min_addr || ptr > max_addr)error ("64-bit relocation outside of kernel!\n" ); - *(uint64_t *)ptr += delta; + value = *(int64_t *)ptr; + +/* +* If using fgkaslr, the value of the relocation +* might need to be changed because it referred +* to an address that has moved. +*/ + (void )adjust_address (&value); + + value += delta; + + *(uint64_t *)ptr = value; } +post_relocations_cleanup (map); #endif }#else @@ -296,6 +376,15 @@ static void parse_elf(void *output)memcpy (phdrs, output + ehdr.e_phoff,sizeof (*phdrs) * ehdr.e_phnum); +if (IS_ENABLED (CONFIG_FG_KASLR )) { +if (!nokaslr) { +parse_sections_headers (output, &ehdr, phdrs); +return ; + }else { +warn ("FG_KASLR disabled: 'nokaslr' on cmdline." ); + } + } +for (i =0 ; i < ehdr.e_phnum; i++) { phdr = &phdrs[i];arch/x86/boot/compressed/misc.h +31
-0@@ -39,7 +39,12 @@/* misc.c */ extern memptr free_mem_ptr;extern memptr free_mem_end_ptr; +#define STATIC +#define STATIC_RW_DATA extern +#include <linux/decompress/mm.h> +extern struct boot_params *boot_params; +extern int nokaslr;void __putstr (const char *s);void __puthex (unsigned long value);#define error_putstr (__x) __putstr(__x)@@ -74,6 +79,32 @@ struct mem_vector {unsigned long long size; }; +#ifdef CONFIG_X86_64 +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr +#define Elf_Shdr Elf64_Shdr +#else +#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr +#define Elf_Shdr Elf32_Shdr +#endif + +#if CONFIG_FG_KASLR +void parse_sections_headers (void *output,Elf_Ehdr *ehdr,Elf_Phdr *phdrs); +void pre_relocations_cleanup (unsigned long map); +void post_relocations_cleanup (unsigned long map); +Elf_Shdr *adjust_address (long *address); +void adjust_relative_offset (long pc,long *value,Elf_Shdr *section); +bool is_percpu_addr (long pc,long offset); +#else +static inline void parse_sections_headers (void *output,Elf_Ehdr *ehdr,Elf_Phdr *phdrs) { } +static inline void pre_relocations_cleanup (unsigned long map) { } +static inline void post_relocations_cleanup (unsigned long map) { } +static inline Elf_Shdr *adjust_address (long *address) {return NULL ; } +static inline void adjust_relative_offset (long pc,long *value,Elf_Shdr *section) { } +static inline bool is_percpu_addr (long pc,long offset) {return true; } +#endif /* CONFIG_FG_KASLR */ +#if CONFIG_RANDOMIZE_BASE /* kaslr.c */ void choose_random_location(unsigned long input,arch/x86/boot/compressed/utils.c new file +12
-0@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* utils.c +* +* This contains various libraries that are needed for fgkaslr +*/ +#define __DISABLE_EXPORTS +#define _LINUX_KPROBES_H +#define NOKPROBE_SYMBOL (fname) +#include "../../../../lib/sort.c" +#include "../../../../lib/bsearch.c" +arch/x86/boot/compressed/vmlinux.symbols new file +18
-0@@ -0,0 +1,18 @@ +kallsyms_offsets +kallsyms_addresses +kallsyms_num_syms +kallsyms_relative_base +kallsyms_names +kallsyms_token_table +kallsyms_token_index +kallsyms_markers +__start___ex_table +__stop___ex_table +_sinittext +_einittext +_stext +_etext +__start_orc_unwind_ip +__stop_orc_unwind_ip +__stop_orc_unwind +__start_orc_unwindarch/x86/include/asm/boot.h +13
-2@@ -26,8 +26,19 @@#ifdef CONFIG_KERNEL_BZIP2 # define BOOT_HEAP_SIZE 0x400000 -#else /* !CONFIG_KERNEL_BZIP2 */ -# define BOOT_HEAP_SIZE 0x10000 +#elif CONFIG_FG_KASLR +/* +* We need extra boot heap when using fgkaslr because we make a copy +* of the original decompressed kernel to avoid issues with writing +* over ourselves when shuffling the sections. We also need extra +* space for resorting kallsyms after shuffling. This value could +* be decreased if free() would release memory properly, or if we +* could avoid the kernel copy. It would need to be increased if we +* find additional tables that need to be resorted. +*/ +# define BOOT_HEAP_SIZE 0x4000000 +#else /* !CONFIG_KERNEL_BZIP2 && !CONFIG_FG_KASLR */ +# define BOOT_HEAP_SIZE 0x10000#endif #ifdef CONFIG_X86_64 include/uapi/linux/elf.h +1
-0@@ -298,6 +298,7 @@ typedef struct elf64_phdr {#define SHN_LIVEPATCH 0xff20#define SHN_ABS 0xfff1#define SHN_COMMON 0xfff2 +#define SHN_XINDEX 0xffff#define SHN_HIRESERVE 0xffff typedef struct elf32_shdr {Signature · 1 line
2.20.1
PATCH 8/9 kallsyms: hide layout
In reply to Kristen Carlson AccardiTo support finer grained kaslr (fgkaslr), we need to hide our sorted list of symbols, since this will give away our new layout. This patch makes /proc/kallsyms only visible to priviledged users.
--- kernel/kallsyms.c | 138 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-)
kernel/kallsyms.c +137 -1
@@ -25,6 +25,7 @@#include <linux/filter.h> #include <linux/ftrace.h> #include <linux/compiler.h> +#include <linux/list_sort.h> /* * These will be re-linked against their real values
@@ -446,6 +447,11 @@ struct kallsym_iter {int show_value; }; +struct kallsyms_iter_list { +struct kallsym_iter iter; +struct list_head next; +}; +int __weak arch_get_kallsym (unsigned int symnum,unsigned long *value,char *type,char *name) {
@@ -660,6 +666,121 @@ int kallsyms_show_value(void) } } +static int sorted_show (struct seq_file *m,void *p) +{ +struct list_head *list = m->private; +struct kallsyms_iter_list *iter; +int rc; + +if (list_empty (list)) +return 0 ; + + iter = list_first_entry(list, struct kallsyms_iter_list, next); + + m->private = iter; + rc =s_show (m, p); + m->private = list; + +list_del (&iter->next); +kfree (iter); + +return rc; +} + +static void *sorted_start (struct seq_file *m,loff_t *pos) +{ +return m->private; +} + +static void *sorted_next (struct seq_file *m,void *p,loff_t *pos) +{ +struct list_head *list = m->private; + + (*pos)++; + +if (list_empty (list)) +return NULL ; + +return p; +} + +static const struct seq_operations kallsyms_sorted_op = { + .start = sorted_start, + .next = sorted_next, + .stop = s_stop, + .show = sorted_show +}; + +static int kallsyms_list_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ +struct kallsyms_iter_list *iter_a, *iter_b; + + iter_a = list_entry(a, struct kallsyms_iter_list, next); + iter_b = list_entry(b, struct kallsyms_iter_list, next); + +return strcmp (iter_a->iter.name, iter_b->iter.name); +} + +int get_all_symbol_name (void *data,const char *name,struct module *mod, +unsigned long addr) +{ +unsigned long sym_pos; +struct kallsyms_iter_list *node, *last; +struct list_head *head = (struct list_head *)data; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) +return -ENOMEM ; + +if (list_empty (head)) { + sym_pos =0 ; +memset (node,0 ,sizeof (*node)); +reset_iter (&node->iter,0 ); + node->iter.show_value =kallsyms_show_value (); + }else { + last = list_first_entry(head, struct kallsyms_iter_list, next); +memcpy (node, last,sizeof (*node)); + sym_pos = last->iter.pos; + } + +INIT_LIST_HEAD (&node->next); +list_add (&node->next, head); + +/* +* update_iter returns false when at end of file +* which in this case we don't care about and can +* safely ignore. update_iter() will increment +* the value of iter->pos, for ksymbol_core. +*/ +if (sym_pos >= kallsyms_num_syms) + sym_pos++; + + (void )update_iter (&node->iter, sym_pos); + +return 0 ; +} + +static int kallsyms_sorted_open (struct inode *inode,struct file *file) +{ +int ret; +struct list_head *list; + + list =__seq_open_private (file, &kallsyms_sorted_op,sizeof (*list)); +if (!list) +return -ENOMEM ; + +INIT_LIST_HEAD (list); + + ret =kallsyms_on_each_symbol (get_all_symbol_name, list); +if (ret !=0 ) +return ret; + +list_sort (NULL , list, kallsyms_list_cmp); + +return 0 ; +} +static int kallsyms_open (struct inode *inode,struct file *file) {/*
@@ -704,9 +825,24 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +static const struct proc_ops kallsyms_sorted_proc_ops = { + .proc_open = kallsyms_sorted_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +static int __init kallsyms_init (void ) { -proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_proc_ops); +/* +* When fine grained kaslr is enabled, we need to +* print out the symbols sorted by name rather than by +* by address, because this reveals the randomization order. +*/ +if (!IS_ENABLED (CONFIG_FG_KASLR )) +proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_proc_ops); +else +proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_sorted_proc_ops);return 0 ; }device_initcall (kallsyms_init);
Signature · 1 line
2.20.1
Re: [PATCH 8/9] kallsyms: hide layout
Quoted 5 lines · Kristen Carlson Accardi
To support finer grained kaslr (fgkaslr), we need to hide our sorted list of symbols, since this will give away our new layout. This patch makes
/proc/kallsymsonly visible to priviledged users.
Does it?
Quoted 135 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>--- kernel/kallsyms.c | 138 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-)kernel/kallsyms.c +108
-0@@ -25,6 +25,7 @@#include <linux/filter.h> #include <linux/ftrace.h> #include <linux/compiler.h> +#include <linux/list_sort.h> /* * These will be re-linked against their real values @@ -446,6 +447,11 @@ struct kallsym_iter {int show_value; }; +struct kallsyms_iter_list { +struct kallsym_iter iter; +struct list_head next; +}; +int __weak arch_get_kallsym (unsigned int symnum,unsigned long *value,char *type,char *name) {@@ -660,6 +666,121 @@ int kallsyms_show_value(void) } } +static int sorted_show (struct seq_file *m,void *p) +{ +struct list_head *list = m->private; +struct kallsyms_iter_list *iter; +int rc; + +if (list_empty (list)) +return 0 ; + + iter = list_first_entry(list, struct kallsyms_iter_list, next); + + m->private = iter; + rc =s_show (m, p); + m->private = list; + +list_del (&iter->next); +kfree (iter); + +return rc; +} + +static void *sorted_start (struct seq_file *m,loff_t *pos) +{ +return m->private; +} + +static void *sorted_next (struct seq_file *m,void *p,loff_t *pos) +{ +struct list_head *list = m->private; + + (*pos)++; + +if (list_empty (list)) +return NULL ; + +return p; +} + +static const struct seq_operations kallsyms_sorted_op = { + .start = sorted_start, + .next = sorted_next, + .stop = s_stop, + .show = sorted_show +}; + +static int kallsyms_list_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ +struct kallsyms_iter_list *iter_a, *iter_b; + + iter_a = list_entry(a, struct kallsyms_iter_list, next); + iter_b = list_entry(b, struct kallsyms_iter_list, next); + +return strcmp (iter_a->iter.name, iter_b->iter.name); +} + +int get_all_symbol_name (void *data,const char *name,struct module *mod, +unsigned long addr) +{ +unsigned long sym_pos; +struct kallsyms_iter_list *node, *last; +struct list_head *head = (struct list_head *)data; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) +return -ENOMEM ; + +if (list_empty (head)) { + sym_pos =0 ; +memset (node,0 ,sizeof (*node)); +reset_iter (&node->iter,0 ); + node->iter.show_value =kallsyms_show_value (); + }else { + last = list_first_entry(head, struct kallsyms_iter_list, next); +memcpy (node, last,sizeof (*node)); + sym_pos = last->iter.pos; + } + +INIT_LIST_HEAD (&node->next); +list_add (&node->next, head); + +/* +* update_iter returns false when at end of file +* which in this case we don't care about and can +* safely ignore. update_iter() will increment +* the value of iter->pos, for ksymbol_core. +*/ +if (sym_pos >= kallsyms_num_syms) + sym_pos++; + + (void )update_iter (&node->iter, sym_pos); + +return 0 ; +} + +static int kallsyms_sorted_open (struct inode *inode,struct file *file) +{ +int ret; +struct list_head *list; + + list =__seq_open_private (file, &kallsyms_sorted_op,sizeof (*list)); +if (!list)
kernel/kallsyms.c int kallsyms_show_value(void) · line 780
Quoted 10 lines
kernel/kallsyms.c +10
-0+return -ENOMEM ; + +INIT_LIST_HEAD (list); + + ret =kallsyms_on_each_symbol (get_all_symbol_name, list); +if (ret !=0 ) +return ret; + +list_sort (NULL , list, kallsyms_list_cmp); +
Could we do the sort at init time rather than open time?
Quoted 19 lines
kernel/kallsyms.c +10
-0+return 0 ; +} +static int kallsyms_open (struct inode *inode,struct file *file) {/* @@ -704,9 +825,24 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +static const struct proc_ops kallsyms_sorted_proc_ops = { + .proc_open = kallsyms_sorted_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +static int __init kallsyms_init (void ) {
kernel/kallsyms.c static const struct proc_ops kallsyms_proc_ops = { · line 845
Quoted 10 lines
kernel/kallsyms.c +9
-1-proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_proc_ops); +/* +* When fine grained kaslr is enabled, we need to +* print out the symbols sorted by name rather than by +* by address, because this reveals the randomization order. +*/ +if (!IS_ENABLED (CONFIG_FG_KASLR )) +proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_proc_ops); +else +proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_sorted_proc_ops);
Can we just switch to the sorted version unconditionally instead? Or is the output order of /proc/kallsyms considered kernel ABI?
Quoted 6 lines
kernel/kallsyms.c +0
-1return 0 ; }device_initcall (kallsyms_init); --2.20.1
Re: [PATCH 8/9] kallsyms: hide layout
Quoted 9 lines · Ard Biesheuvel
Quoted 5 lines · Kristen Carlson Accardi
To support finer grained kaslr (fgkaslr), we need to hide our sorted
list of symbols, since this will give away our new layout. This patch makes/proc/kallsymsonly visible to priviledged users.Does it?
Oops, I forgot to update my commit message since I changed algorithms. Thanks for catching that. I'll fix it in the next version.
Quoted 147 lines
Quoted 147 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>--- kernel/kallsyms.c | 138 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-)kernel/kallsyms.c +6
-0@@ -25,6 +25,7 @@#include <linux/filter.h> #include <linux/ftrace.h> #include <linux/compiler.h> +#include <linux/list_sort.h> /* * These will be re-linked against their real values @@ -446,6 +447,11 @@ struct kallsym_iter {int show_value; }; +struct kallsyms_iter_list { +struct kallsym_iter iter; +struct list_head next; +}; +int __weak arch_get_kallsym (unsigned int symnum,unsigned long *value,char *type,char *name){
kernel/kallsyms.c +96
-0@@ -660,6 +666,121 @@ int kallsyms_show_value(void) } } +static int sorted_show (struct seq_file *m,void *p) +{ +struct list_head *list = m->private; +struct kallsyms_iter_list *iter; +int rc; + +if (list_empty (list)) +return 0 ; + + iter = list_first_entry(list, struct kallsyms_iter_list, next); + + m->private = iter; + rc =s_show (m, p); + m->private = list; + +list_del (&iter->next); +kfree (iter); + +return rc; +} + +static void *sorted_start (struct seq_file *m,loff_t *pos) +{ +return m->private; +} + +static void *sorted_next (struct seq_file *m,void *p,loff_t *pos) +{ +struct list_head *list = m->private; + + (*pos)++; + +if (list_empty (list)) +return NULL ; + +return p; +} + +static const struct seq_operations kallsyms_sorted_op = { + .start = sorted_start, + .next = sorted_next, + .stop = s_stop, + .show = sorted_show +}; + +static int kallsyms_list_cmp (void *priv,struct list_head *a, +struct list_head *b) +{ +struct kallsyms_iter_list *iter_a, *iter_b; + + iter_a = list_entry(a, struct kallsyms_iter_list, next); + iter_b = list_entry(b, struct kallsyms_iter_list, next); + +return strcmp (iter_a->iter.name, iter_b->iter.name); +} + +int get_all_symbol_name (void *data,const char *name,struct module *mod, +unsigned long addr) +{ +unsigned long sym_pos; +struct kallsyms_iter_list *node, *last; +struct list_head *head = (struct list_head *)data; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) +return -ENOMEM ; + +if (list_empty (head)) { + sym_pos =0 ; +memset (node,0 ,sizeof (*node)); +reset_iter (&node->iter,0 ); + node->iter.show_value =kallsyms_show_value (); + }else { + last = list_first_entry(head, struct kallsyms_iter_list, next); +memcpy (node, last,sizeof (*node)); + sym_pos = last->iter.pos; + } + +INIT_LIST_HEAD (&node->next); +list_add (&node->next, head); + +/* +* update_iter returns false when at end of file +* which in this case we don't care about and can +* safely ignore. update_iter() will increment +* the value of iter->pos, for ksymbol_core. +*/ +if (sym_pos >= kallsyms_num_syms) + sym_pos++; + + (void )update_iter (&node->iter, sym_pos); + +return 0 ; +} + +static int kallsyms_sorted_open(struct inode *inode, struct file*file)
kernel/kallsyms.c +5
-0+{ +int ret; +struct list_head *list; + + list = __seq_open_private(file, &kallsyms_sorted_op,
sizeof(*list));kernel/kallsyms.c +5
-0+if (!list) +return -ENOMEM ; + +INIT_LIST_HEAD (list); +
Quoted 8 lines
kernel/kallsyms.c int kallsyms_show_value(void) · line 780
Quoted 6 lines
kernel/kallsyms.c +6
-0+ ret =kallsyms_on_each_symbol (get_all_symbol_name, list); +if (ret !=0 ) +return ret; + +list_sort (NULL , list, kallsyms_list_cmp); +Could we do the sort at init time rather than open time
I could do this, but module symbols would not be included, and also we would have to maintain a separate symbol list in memory forever since we have to keep the list sorted by address for bsearch. So, it does increase memory consumption vs. just allocating space, sorting, and then deallocating. I personally feel it's a better trade off to sort at open.
Quoted 29 lines
Quoted 29 lines
kernel/kallsyms.c +14
-1+return 0 ; +} +static int kallsyms_open (struct inode *inode,struct file *file) {/* @@ -704,9 +825,24 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +static const struct proc_ops kallsyms_sorted_proc_ops = { + .proc_open = kallsyms_sorted_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +static int __init kallsyms_init (void ) { -proc_create ("kallsyms" ,0444 ,NULL , &kallsyms_proc_ops); +/* +* When fine grained kaslr is enabled, we need to +* print out the symbols sorted by name rather than by +* by address, because this reveals the randomization order.
kernel/kallsyms.c +2
-0+*/ +if (!IS_ENABLED (CONFIG_FG_KASLR ))
Quoted 8 lines
Quoted 5 lines
+ proc_create("kallsyms", 0444, NULL,
&kallsyms_proc_ops);kernel/kallsyms.c +2
-0+else + proc_create("kallsyms", 0444, NULL,
&kallsyms_sorted_proc_ops);Can we just switch to the sorted version unconditionally instead? Or is the output order of
/proc/kallsymsconsidered kernel ABI?
I didn't know the answer to this question, which is why to be safe I maintained the existing behavior when my feature is not enabled.
Quoted 1 line
PATCH 9/9 module: Reorder functions
In reply to Kristen Carlson AccardiIf a module has functions split out into separate text sections (i.e. compiled with the -ffunction-sections flag), reorder the functions to provide some code diversification to modules.
--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
kernel/module.c +82 -0
@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h"
@@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod, unsigned int *size,return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections. +*/ +static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections. +* Store sections in an array and then shuffle the sections +* to reorder the functions. +*/ +static void randomize_text (struct module *mod,struct load_info *info) +{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list =kmalloc_array (max_sections,sizeof (*text_list),GFP_KERNEL ); +if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; +const char *sname = info->secstrings + shdr->sh_name; + +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last +* argument, that is only used by arch_mod_section_prepend(), +* which is only defined by parisc. Since this this type +* of randomization isn't supported on parisc, we can +* safely pass in zero as the last argument, as it is +* ignored. +*/ + shdr->sh_entsize =get_offset (mod, &size, shdr,0 ); + } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld might -- code, read-only data, read-write data, small data. Tally sizes, and place the offsets into sh_entsize fields: high bit means it
@@ -2460,6 +2539,9 @@ static void layout_sections(struct module *mod, struct load_info *info)break ; } } + +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ()) +randomize_text (mod, info); }static void set_license (struct module *mod,const char *license)
Signature · 1 line
2.20.1
Re: [PATCH 9/9] module: Reorder functions
Quoted 5 lines · Kristen Carlson Accardi
If a module has functions split out into separate text sections (i.e. compiled with the -ffunction-sections flag), reorder the functions to provide some code diversification to modules.
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines CONFIG_FG_KASLR=y)
Quoted 104 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>Reviewed-by Kees Cook <[email protected]>--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)kernel/module.c +79
-0@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h" @@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod, unsigned int *size,return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections. +*/ +static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections. +* Store sections in an array and then shuffle the sections +* to reorder the functions. +*/ +static void randomize_text (struct module *mod,struct load_info *info) +{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list =kmalloc_array (max_sections,sizeof (*text_list),GFP_KERNEL ); +if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; +const char *sname = info->secstrings + shdr->sh_name; + +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last +* argument, that is only used by arch_mod_section_prepend(), +* which is only defined by parisc. Since this this type +* of randomization isn't supported on parisc, we can +* safely pass in zero as the last argument, as it is +* ignored. +*/ + shdr->sh_entsize =get_offset (mod, &size, shdr,0 ); + } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld might -- code, read-only data, read-write data, small data. Tally sizes, and place the offsets into sh_entsize fields: high bit means it
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2543
Quoted 5 lines
kernel/module.c +2
-0@@ -2460,6 +2539,9 @@ static void layout_sections(struct module *mod, struct load_info *info)break ; } } + +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ())
kaslr_enabled() only exists [as a function] on x86
Quoted 7 lines
kernel/module.c +1
-1+randomize_text (mod, info); }static void set_license (struct module *mod,const char *license) --2.20.1
Re: [PATCH 9/9] module: Reorder functions
Quoted 3 lines · Ard Biesheuvel
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines
CONFIG_FG_KASLR=y)
I suspect you also need/want at least normal KASLR enabled as a "does it even make sense" common sense threshold
Re: [PATCH 9/9] module: Reorder functions
Quoted 8 lines · Arjan van de Ven
Quoted 3 lines · Ard Biesheuvel
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines
CONFIG_FG_KASLR=y)I suspect you also need/want at least normal KASLR enabled as a "does it even make sense" common sense threshold
Fair enough. But that is more of a policy concern than a functional concern.
FWIW I took patches #8 and #9, hardwired a couple of CONFIG_FG_KASLR=y checks and added the -ffunction-sections GCC option for the modules, and everything appears to be working as expected on arm64. I was just wondering if I was missing something.
Note that arm64 does not have a decompressor, so there the fine grained randomization of the core kernel is not really feasible using the approach presented here.
Re: [PATCH 9/9] module: Reorder functions
Quoted 4 lines · Ard Biesheuvel
Note that arm64 does not have a decompressor, so there the fine grained randomization of the core kernel is not really feasible using the approach presented here.
maybe do a "memcpy" decompressor as an option? :-)
Quoted 1 line
Re: [PATCH 9/9] module: Reorder functions
In reply to Ard BiesheuvelQuoted 10 lines · Ard Biesheuvel
Quoted 4 lines · Kristen Carlson Accardi
If a module has functions split out into separate text sections (i.e. compiled with the -ffunction-sections flag), reorder the functions to provide some code diversification to modules.
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines
CONFIG_FG_KASLR=y)
I think it would work for modules. I've not tested this of course. It might not make sense for some architectures (like 32 bit), but it would probably work.
Quoted 119 lines
Quoted 119 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>Reviewed-by Kees Cook <[email protected]>--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)kernel/module.c +5
-0@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h" @@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod,unsigned int *size, return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections.
+ */kernel/module.c +22
-0+static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections.
+ * Store sections in an array and then shuffle the sections
+ * to reorder the functions.
+ */
+static voidrandomize_text(struct module *mod, struct load_info *info)kernel/module.c +12
-0+{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list = kmalloc_array(max_sections, sizeof(*text_list),
GFP_KERNEL);kernel/module.c +6
-0+if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; + const char *sname = info->secstrings + shdr-Quoted 1 line
sh_name;kernel/module.c +18
-0+ +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last +* argument, that is only used by
arch_mod_section_prepend(),
+ * which is only defined by parisc. Since this this
type
+ * of randomization isn't supported on parisc, we
can
+ * safely pass in zero as the last argument, as it
iskernel/module.c +8
-0+* ignored. +*/ + shdr->sh_entsize =get_offset (mod, &size, shdr,0 ); + } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
might -- code, read-only data, read-write data, small
data. Tally
sizes, and place the offsets intosh_entsizefields: high bit
means itkernel/module.c +0
-0@@ -2460,6 +2539,9 @@ static void layout_sections(struct module
Quoted 8 lines
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2543
Quoted 6 lines
kernel/module.c +2
-0*mod, struct load_info *info)break ; } } + +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ())
kaslr_enabled()only exists [as a function] on x86
CONFIG_FG_KASLR is dependant on x86_64. If people really think there is value in having the module randomization not dependent on the kernel randomization it can be changed to a different config option - but I am not sure that there is a ton of value in the module randomization on it's own.
Quoted 9 lines
Quoted 9 lines
kernel/module.c +1
-0+randomize_text (mod, info); }static void
set_license(struct module *mod, const char *license)
--
2.20.1
Re: [PATCH 9/9] module: Reorder functions
Quoted 148 lines · Kristen Carlson Accardi
Quoted 10 lines · Ard Biesheuvel
Quoted 4 lines · Kristen Carlson Accardi
If a module has functions split out into separate text sections (i.e. compiled with the -ffunction-sections flag), reorder the functions to provide some code diversification to modules.
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines
CONFIG_FG_KASLR=y)I think it would work for modules. I've not tested this of course. It might not make sense for some architectures (like 32 bit), but it would
probably work.Quoted 122 lines
Quoted 122 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>Reviewed-by Kees Cook <[email protected]>--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)kernel/module.c +5
-0@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h" @@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod,unsigned int *size, return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections.
+ */kernel/module.c +22
-0+static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections.
+ * Store sections in an array and then shuffle the sections
+ * to reorder the functions.
+ */
+static voidrandomize_text(struct module *mod, struct load_info *info)kernel/module.c +12
-0+{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list = kmalloc_array(max_sections,
sizeof(*text_list),GFP_KERNEL);kernel/module.c +6
-0+if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; + const char *sname = info->secstrings + shdr-Quoted 1 line
sh_name;kernel/module.c +18
-0+ +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last +* argument, that is only used by
arch_mod_section_prepend(),
+ * which is only defined by parisc. Since this
this
type
+ * of randomization isn't supported on parisc, we
can
+ * safely pass in zero as the last argument, as
it
iskernel/module.c +3
-0+* ignored. +*/ + shdr->sh_entsize = get_offset(mod, &size, shdr,0);
kernel/module.c +5
-0+ } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
might -- code, read-only data, read-write data, small
data. Tally
sizes, and place the offsets intosh_entsizefields: high bit
means itkernel/module.c +0
-0@@ -2460,6 +2539,9 @@ static void layout_sections(struct moduleQuoted 8 lines
kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2543
Quoted 6 lines
kernel/module.c +2
-0*mod, struct load_info *info)break ; } } + +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ())
kaslr_enabled()only exists [as a function] on x86
Quoted 7 lines
CONFIG_FG_KASLRis dependant onx86_64. If people really think there is
value in having the module randomization not dependent on the kernel randomization it can be changed to a different config option - but I am
not sure that there is a ton of value in the module randomization on it's own.
I should have added - thank you for pointing this out, I will fix it in the next version. I was on a tangent about whether you should even use this without the main kernel randomization :).
Re: [PATCH 9/9] module: Reorder functions
Quoted 150 lines · Kristen Carlson Accardi
Quoted 148 lines · Kristen Carlson Accardi
Quoted 10 lines · Ard Biesheuvel
Quoted 4 lines · Kristen Carlson Accardi
If a module has functions split out into separate text sections (i.e. compiled with the -ffunction-sections flag), reorder the functions to provide some code diversification to modules.
Is that the only prerequisite? I.e., is it sufficient for another architecture to add -ffunction-sections to the module CFLAGS to get this functionality? (assuming it defines
CONFIG_FG_KASLR=y)I think it would work for modules. I've not tested this of course. It might not make sense for some architectures (like 32 bit), but it would
probably work.Quoted 130 lines
Quoted 122 lines
Signed-off-by Kristen Carlson Accardi <[email protected]>Reviewed-by Kees Cook <[email protected]>--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)kernel/module.c +5
-0@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h" @@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod,unsigned int *size, return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections.
+ */kernel/module.c +22
-0+static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections.
+ * Store sections in an array and then shuffle the sections
+ * to reorder the functions.
+ */
+static voidrandomize_text(struct module *mod, struct load_info *info)kernel/module.c +12
-0+{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list = kmalloc_array(max_sections,
sizeof(*text_list),GFP_KERNEL);kernel/module.c +6
-0+if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; + const char *sname = info->secstrings + shdr-Quoted 1 line
sh_name;kernel/module.c +18
-0+ +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last +* argument, that is only used by
arch_mod_section_prepend(),
+ * which is only defined by parisc. Since this
this
type
+ * of randomization isn't supported on parisc, we
can
+ * safely pass in zero as the last argument, as
it
iskernel/module.c +3
-0+* ignored. +*/ + shdr->sh_entsize = get_offset(mod, &size, shdr,0);
kernel/module.c +5
-0+ } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
might -- code, read-only data, read-write data, small
data. Tally
sizes, and place the offsets intosh_entsizefields: high bit
means itkernel/module.c static void layout_sections(struct module · line 2544
Quoted 6 lines
kernel/module.c +2
-0@@ -2460,6 +2539,9 @@ static void layout_sections(struct module *mod, struct load_info *info)break ; } } + +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ())
kaslr_enabled()only exists [as a function] on x86
Quoted 8 lines
Quoted 7 lines
CONFIG_FG_KASLRis dependant onx86_64. If people really think there is
value in having the module randomization not dependent on the kernel randomization it can be changed to a different config option - but I am
not sure that there is a ton of value in the module randomization on it's own.
I think there is. The modules are a sizable attack surface, and made up of drivers for a large part, many of which are not as carefully reviewed as core code. Also, as I pointed out, the ELF loading trick from the decompressor is simply infeasible on arm64, given that there is no decompressor in the first place.
Quoted 4 lines
I should have added - thank you for pointing this out, I will fix it in the next version. I was on a tangent about whether you should even use this without the main kernel randomization :).
Yeah. I was just pointing out that this breaks the build for all other architectures :-)
Re: [PATCH 9/9] module: Reorder functions
Quoted 179 lines · Ard Biesheuvel
Quoted 165 lines · Kristen Carlson Accardi
Quoted 164 lines · Kristen Carlson Accardi
Quoted 9 lines · Ard Biesheuvel
Quoted 6 lines · Kristen Carlson Accardi
If a module has functions split out into separate text sections
(i.e. compiled with the -ffunction-sections flag), reorder the
functions to provide some code diversification to modules.Quoted 5 lines
Is that the only prerequisite? I.e., is it sufficient for another
architecture to add -ffunction-sections to the module CFLAGS to get
this functionality? (assuming it definesCONFIG_FG_KASLR=y)I think it would work for modules. I've not tested this of course. It
might not make sense for some architectures (like 32 bit), but it would
probably work.Quoted 141 lines
Quoted 137 lines
Signed-off-by: Kristen Carlson Accardi <
[email protected]>Reviewed-by Kees Cook <[email protected]>--- kernel/module.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)kernel/module.c +5
-0@@ -53,6 +53,8 @@#include <linux/bsearch.h> #include <linux/dynamic_debug.h> #include <linux/audit.h> +#include <linux/random.h> +#include <asm/setup.h> #include <uapi/linux/module.h> #include "module-internal.h" @@ -2370,6 +2372,83 @@ static long get_offset(struct module *mod, unsigned int *size, return ret; } +/* +* shuffle_text_list() +* Use a Fisher Yates algorithm to shuffle a list of text sections.
+ */kernel/module.c +22
-0+static void shuffle_text_list (Elf_Shdr **list,int size) +{ +int i; +unsigned int j; +Elf_Shdr *temp; + +for (i = size -1 ; i >0 ; i--) { +/* +* pick a random index from 0 to i +*/ +get_random_bytes (&j,sizeof (j)); + j = j % (i +1 ); + + temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } +} + +/* +* randomize_text() +* Look through the core section looking for executable code sections.
+ * Store sections in an array and then shuffle the sections
+ * to reorder the functions.
+ */
+static void randomize_text(struct module *mod, structload_info
*info)kernel/module.c +12
-0+{ +int i; +int num_text_sections =0 ; +Elf_Shdr **text_list; +int size =0 ; +int max_sections = info->hdr->e_shnum; +unsigned int sec =find_sec (info,".text" ); + +if (sec ==0 ) +return ; + + text_list = kmalloc_array(max_sections,
sizeof(*text_list),GFP_KERNEL);kernel/module.c +6
-0+if (text_list ==NULL ) +return ; + +for (i =0 ; i < max_sections; i++) { +Elf_Shdr *shdr = &info->sechdrs[i]; + const char *sname = info->secstrings + shdr-Quoted 1 line
sh_name;kernel/module.c +17
-0+ +if (!(shdr->sh_flags &SHF_ALLOC ) || + !(shdr->sh_flags &SHF_EXECINSTR ) || +strstarts (sname,".init" )) +continue ; + + text_list[num_text_sections] = shdr; + num_text_sections++; + } + +shuffle_text_list (text_list, num_text_sections); + +for (i =0 ; i < num_text_sections; i++) { +Elf_Shdr *shdr = text_list[i]; + +/* +* get_offset has a section index for it's last
+ * argument, that is only used byarch_mod_section_prepend(),
+ * which is only defined by parisc. Since
this
this
type
+ * of randomization isn't supported on
parisc, we
can
+ * safely pass in zero as the last argument,
as
it
iskernel/module.c +3
-0+* ignored. +*/ + shdr->sh_entsize = get_offset(mod, &size,shdr,
0);kernel/module.c +5
-0+ } + +kfree (text_list); +} +/* Lay out the SHF_ALLOC sections in a way not dissimilar to how
ld
might -- code, read-only data, read-write data, small
data. Tally
sizes, and place the offsets intosh_entsizefields: high
bit
means itkernel/module.c +0
-0@@ -2460,6 +2539,9 @@ static void layout_sections(structmodule
*mod, structload_info*info)break; } }kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2543
Quoted 2 lines
kernel/module.c +2
-0+ +if (IS_ENABLED (CONFIG_FG_KASLR ) &&kaslr_enabled ())
kaslr_enabled()only exists [as a function] on x86Quoted 11 lines
Quoted 11 lines
CONFIG_FG_KASLRis dependant onx86_64. If people really think there
is
value in having the module randomization not dependent on the kernel
randomization it can be changed to a different config option - but I
am
not sure that there is a ton of value in the module randomization on
it's own.
Quoted 5 lines
I think there is. The modules are a sizable attack surface, and made up of drivers for a large part, many of which are not as carefully reviewed as core code. Also, as I pointed out, the ELF loading trick from the decompressor is simply infeasible on arm64, given that there is no decompressor in the first place.
I can make a separate config option for modules so that you can enable this separately from the main kernel text randomization. I'll make that change for v2.
Quoted 3 lines · Kristen Carlson Accardi
Kristen Carlson Accardi (8):
objtool: do not assume order of parent/child functions
x86:tools/relocs: Support >64K section headers
Can these two patches land separately (i.e. "now")? CFI will need the relocs fix too, so better to have this landed upstream for both features. I see Josh's Ack in the first...
Signature · 1 line
Kees Cook