KASLR feature to randomize each loadable module
Subsystems (5)
v4 cover KASLR feature to randomize each loadable module
Hi,
This is v4 of the "KASLR feature to randomize each loadable module" patchset. The purpose is to increase the randomization and also to make the modules randomized in relation to each other instead of just the base, so that if one module leaks the location of the others can't be inferred. It is enabled for x86_64 for now.
V4 is a few small fixes. I humbly think this is in pretty good shape at this point, unless anyone has any comments. The only other big change I was considering was moving the new randomization algorithm into vmalloc so it could be re-used for other architectures or possibly other vmalloc usages.
A few words on how this was tested - As previously mentioned, the entropy estimates were done using extracted module text sizes from the in-tree modules. These were also used to run 100,000's of simulated module allocations by calling module_alloc from a test module, including testing until allocation failure. The simulations kept track of every allocation address to make sure there were no collisions, and verified memory was actually mapped.
In addition the __vmalloc_node_try_addr function has a suite of unit tests that verify for a bunch of edge cases that it:
- Allows for allocations when it should
- Reports the right error code if it collides with a lazy-free area or real allocation
- Verifies it frees a lazy free area when it should
These synthetic tests were also how the performance metrics were gathered.
Changes for V4:
- Fix issue caused by KASAN, kmemleak being provided different allocation lengths (padding).
- Avoid kmalloc until sure its needed in __vmalloc_node_try_addr.
- Fix for debug file hang when the last VA is a lazy purge area
- Fixed issues reported by 0-day build system.
Changes for V3:
- Code cleanup based on internal feedback. (thanks to Dave Hansen and Andriy Shevchenko)
- Slight refactor of existing algorithm to more cleanly live along side new one.
- BPF synthetic benchmark
Changes for V2:
- New implementation of __vmalloc_node_try_addr based on the __vmalloc_node_range implementation, that only flushes TLB when needed.
- Modified module loading algorithm to try to reduce the TLB flushes further.
- Increase "random area" tries in order to increase the number of modules that can get high randomness.
- Increase "random area" size to 2/3 of module area in order to increase the number of modules that can get high randomness.
- Fix for 0day failures on other architectures.
- Fix for wrong debugfs permissions. (thanks to Jann Horn)
- Spelling fix. (thanks to Jann Horn)
- Data on module_alloc performance and TLB flushes. (brought up by Kees Cook and Jann Horn)
- Data on memory usage. (suggested by Jann)
Rick Edgecombe (3):
vmalloc: Add __vmalloc_node_try_addr function
x86/modules: Increase randomization for modules
vmalloc: Add debugfs modfraginfo
arch/x86/include/asm/pgtable_64_types.h | 7 + arch/x86/kernel/module.c | 165 ++++++++++++++++--- include/linux/vmalloc.h | 3 + mm/vmalloc.c | 279 +++++++++++++++++++++++++++++++- 4 files changed, 429 insertions(+), 25 deletions(-)
Signature · 1 line
2.7.4
PATCH v4 1/3 vmalloc: Add __vmalloc_node_try_addr function
Create __vmalloc_node_try_addr function that tries to allocate at a specific address and supports caller specified behavior for whether any lazy purging happens if there is a collision.
This new function draws from the __vmalloc_node_range implementation. Attempts to merge the two into a single allocator resulted in logic that was difficult to follow, so they are left separate.
--- include/linux/vmalloc.h | 3 + mm/vmalloc.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 179 insertions(+), 1 deletion(-)
include/linux/vmalloc.h +3 -0
@@ -82,6 +82,9 @@ extern void *__vmalloc_node_range(unsigned long size, unsigned long align, unsigned long start, unsigned long end, gfp_t gfp_mask, pgprot_t prot, unsigned long vm_flags, int node, const void *caller); +extern void *__vmalloc_node_try_addr (unsigned long addr,unsigned long size, +gfp_t gfp_mask,pgprot_t prot,unsigned long vm_flags, +int node,int try_purge,const void *caller);#ifndef CONFIG_MMU extern void *__vmalloc_node_flags (unsigned long size,int node,gfp_t flags); static inline void *__vmalloc_node_flags_caller(unsigned long size, int node,
mm/vmalloc.c +176 -1
195 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 2/3 x86/modules: Increase randomization for modules
In reply to Rick EdgecombeThis changes the behavior of the KASLR logic for allocating memory for the text sections of loadable modules. It randomizes the location of each module text section with about 17 bits of entropy in typical use. This is enabled on X86_64 only. For 32 bit, the behavior is unchanged.
It refactors existing code around module randomization somewhat. There are now three different behaviors for x86 module_alloc depending on config. RANDOMIZE_BASE=n, and RANDOMIZE_BASE=y ARCH=x86_64, and RANDOMIZE_BASE=y ARCH=i386. The refactor of the existing code is to try to clearly show what those behaviors are without having three separate versions or threading the behaviors in a bunch of little spots. The reason it is not enabled on 32 bit yet is because the module space is much smaller and simulations haven't been run to see how it performs.
The new algorithm breaks the module space in two, a random area and a backup area. It first tries to allocate at a number of randomly located starting pages inside the random section without purging any lazy free vmap areas and triggering the associated TLB flush. If this fails, it will try again a number of times allowing for purges if needed. It also saves any position that could have succeeded if it was allowed to purge, which doubles the chances of finding a spot that would fit. Finally if those both fail to find a position it will allocate in the backup area. The backup area base will be offset in the same way as the current algorithm does for the base area, 1024 possible locations.
--- arch/x86/include/asm/pgtable_64_types.h | 7 ++ arch/x86/kernel/module.c | 165 +++++++++++++++++++++++++++----- 2 files changed, 149 insertions(+), 23 deletions(-)
arch/x86/include/asm/pgtable_64_types.h +7 -0
@@ -143,6 +143,13 @@ extern unsigned int ptrs_per_p4d;#define MODULES_END _AC(0xffffffffff000000, UL)#define MODULES_LEN (MODULES_END - MODULES_VADDR) +/* +* Dedicate the first part of the module space to a randomized area when KASLR +* is in use. Leave the remaining part for a fallback if we are unable to +* allocate in the random area. +*/ +#define MODULES_RAND_LEN PAGE_ALIGN((MODULES_LEN/3)*2) +#define ESPFIX_PGD_ENTRY _AC(-2, UL)#define ESPFIX_BASE_ADDR (ESPFIX_PGD_ENTRY << P4D_SHIFT)
arch/x86/kernel/module.c +142 -23
192 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 3/3 vmalloc: Add debugfs modfraginfo
In reply to Rick EdgecombeAdd debugfs file "modfraginfo" for providing info on module space fragmentation. This can be used for determining if loadable module randomization is causing any problems for extreme module loading situations, like huge numbers of modules or extremely large modules.
Sample output when KASLR is enabled and X86_64 is configured:
Largest free space: 897912 kB Total free space: 1025424 kB
Allocations in backup area: 0
Sample output when just X86_64:
Largest free space: 897912 kB Total free space: 1025424 kB
--- mm/vmalloc.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-)
mm/vmalloc.c +101 -1
@@ -18,6 +18,7 @@#include <linux/interrupt.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> +#include <linux/debugfs.h> #include <linux/debugobjects.h> #include <linux/kallsyms.h> #include <linux/list.h>
@@ -33,6 +34,7 @@#include <linux/bitops.h> #include <linux/uaccess.h> +#include <asm/setup.h> #include <asm/tlbflush.h> #include <asm/shmparam.h>
@@ -2919,7 +2921,105 @@ static int __init proc_vmalloc_init(void)proc_create_seq ("vmallocinfo" ,0400 ,NULL , &vmalloc_op);return 0 ; } -module_init (proc_vmalloc_init); +#else +static int __init proc_vmalloc_init (void ) +{ +return 0 ; +} +#endif + +#if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_X86_64 ) +static inline unsigned long is_in_backup (unsigned long addr) +{ +return addr >=MODULES_VADDR +MODULES_RAND_LEN ; +} +#else +static inline unsigned long is_in_backup (unsigned long addr) +{ +return 0 ; +} +inline bool kaslr_enabled (void );#endif + +#if defined(CONFIG_DEBUG_FS ) && defined(CONFIG_X86_64 ) +static int modulefraginfo_debug_show (struct seq_file *m,void *v) +{ +unsigned long last_end =MODULES_VADDR ; +unsigned long total_free =0 ; +unsigned long largest_free =0 ; +unsigned long backup_cnt =0 ; +unsigned long gap; +struct vmap_area *prev, *cur =NULL ; + +spin_lock (&vmap_area_lock); + +if (!pvm_find_next_prev (MODULES_VADDR , &cur, &prev) || !cur) + goto done; + +for (; cur->va_end <=MODULES_END ; cur =list_next_entry (cur, list)) { +/* Don't count areas that are marked to be lazily freed */ +if (!(cur->flags &VM_LAZY_FREE )) { + backup_cnt +=is_in_backup (cur->va_start); + gap = cur->va_start - last_end; +if (gap > largest_free) + largest_free = gap; + total_free += gap; + last_end = cur->va_end; + } + +if (list_is_last (&cur->list, &vmap_area_list)) +break ; + } + +done: + gap = (MODULES_END - last_end); +if (gap > largest_free) + largest_free = gap; + total_free += gap; + +spin_unlock (&vmap_area_lock); + +seq_printf (m,"\tLargest free space:\t%lu kB\n" , largest_free /1024 ); +seq_printf (m,"\t Total free space:\t%lu kB\n" , total_free /1024 ); + +if (IS_ENABLED (CONFIG_RANDOMIZE_BASE ) &&kaslr_enabled ()) +seq_printf (m,"Allocations in backup area:\t%lu\n" , backup_cnt); + +return 0 ; +} + +static int proc_module_frag_debug_open (struct inode *inode,struct file *file) +{ +return single_open (file, modulefraginfo_debug_show,NULL ); +} + +static const struct file_operations debug_module_frag_operations = { + .open = proc_module_frag_debug_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static void __init debug_modfrag_init (void ) +{ +debugfs_create_file ("modfraginfo" ,0400 ,NULL ,NULL , + &debug_module_frag_operations); +} +#else /* defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64) */ +static void __init debug_modfrag_init (void ) +{ +} +#endif + +#if defined(CONFIG_DEBUG_FS ) || defined(CONFIG_PROC_FS ) +static int __init info_vmalloc_init (void ) +{ +proc_vmalloc_init (); +debug_modfrag_init (); +return 0 ; +} + +module_init (info_vmalloc_init); +#endif
Signature · 1 line
2.7.4
Quoted 36 lines · Rick Edgecombe
Hi,
This is v4 of the "KASLR feature to randomize each loadable module" patchset. The purpose is to increase the randomization and also to make the modules randomized in relation to each other instead of just the base, so that if one module leaks the location of the others can't be inferred. It is enabled for
x86_64for now.V4 is a few small fixes. I humbly think this is in pretty good shape at this point, unless anyone has any comments. The only other big change I was considering was moving the new randomization algorithm into vmalloc so it could be re-used for other architectures or possibly other vmalloc usages.
A few words on how this was tested - As previously mentioned, the entropy estimates were done using extracted module text sizes from the in-tree modules. These were also used to run 100,000's of simulated module allocations by calling
module_allocfrom a test module, including testing until allocation failure. The simulations kept track of every allocation address to make sure there were no collisions, and verified memory was actually mapped.In addition the
__vmalloc_node_try_addrfunction has a suite of unit tests that verify for a bunch of edge cases that it:
- Allows for allocations when it should
- Reports the right error code if it collides with a lazy-free area or real allocation
- Verifies it frees a lazy free area when it shouldThese synthetic tests were also how the performance metrics were gathered.
Changes for V4:
- Fix issue caused by KASAN, kmemleak being provided different allocation lengths (padding).
- Avoid kmalloc until sure its needed in__vmalloc_node_try_addr.
- Fix for debug file hang when the last VA is a lazy purge area
- Fixed issues reported by 0-day build system.
Quoted 6 lines
Changes for V3:
- Code cleanup based on internal feedback. (thanks to Dave Hansen and Andriy Shevchenko)
- Slight refactor of existing algorithm to more cleanly live along side new one.
- BPF synthetic benchmark
I don't see this benchmark in this patch set.
Could you prepare it as a test in tools/testing/selftests/bpf/ ? so we can double check what is being tested and run it regularly like we do for all other tests in there.
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Rick Edgecombe
Changes for V3:
- Code cleanup based on internal feedback. (thanks to Dave Hansen and Andriy
Shevchenko)
- Slight refactor of existing algorithm to more cleanly live along side new one.
- BPF synthetic benchmark
Quoted 4 lines
I don't see this benchmark in this patch set.
Could you prepare it as a test intools/testing/selftests/bpf/? so we can double check what is being tested and run it regularly like we do for all other tests in there.
Sure.
There were two benchmarks I had run with BPF in mind, one was the timing the module_alloc function in different scenarios, looking to make sure there were no slowdowns for insertions.
The other was to check if the fragmentation caused any measurable runtime performance:
"For runtime performance, a synthetic benchmark was run that does 5000000 BPF JIT invocations each, from varying numbers of parallel processes, while the kernel compiles sharing the same CPU to stand in for the cache impact of a real workload. The seccomp filter invocations were just Jann Horn's seccomp filtering test from this thread http://openwall.com/lists/kernel-hardening/2018/07/18/2,
except non-real time priority. The kernel was configured with KPTI and retpoline, and pcid was disabled. There wasn't any significant difference between the new and the old."
From what I know about the bpf kselftest, the first one would probably be a better fit. Not sure if the second one would fit, with the kernel compiling sharing the same CPU, a special config, and a huge amount of processes being spawned... I can try to add a micro-benchmark instead if that sounds good.
Rick