KASLR feature to randomize each loadable module
Subsystems (3)
v2 cover KASLR feature to randomize each loadable module
Hi,
This is v2 of the "KASLR feature to randomize each loadable module" patchset. The purpose is to increase the randomization and makes 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.
This code needs some refactoring and simplification, but I was hoping to get some feedback on the benchmarks and provide an update.
V2 brings the TLB flushes down to close to the existing algorithm and increases the modules that get high randomness based on the concerns raised by Jann Horn about the BPF JIT use case. It also tries to address Kees Cook's comments about possible minimal boot time regression by measuring the average allocation time to be below the existing allocator. It also addresses Mathew Wilcox's comment on the GFP_NOWARN not being needed. There is also some data on PTE memory use which is higher than the original algorithm, as suggested by Jann.
This is off of 4.18-RC3.
Changes since v1:
- 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)
- Spelling fix .(thanks to Jann)
- Data on module_alloc performance and TLB flushes. (brought up by Kees and Jann)
- Data on memory usage. (suggested by Jann)
Todo:
- Refactor __vmalloc_node_try_addr to be smaller and share more code with the normal vmalloc path, and misc cleanup
- More real world testing other than the synthetic micro benchmarks described below. BPF kselftest brought up by Daniel Borkman.
New Algorithm
=============
In addition to __vmalloc_node_try_addr only purging the lazy free areas when it needs to, it also now supports a mode where it will fail to allocate instead of doing any purge. In this case it reports when the allocation would have succeeded if it was allowed to purge. It returns this information via an ERR_PTR.
The logic for the selection of a location in the random ara is changed as well. The number of tries is increased from 10 to 10000, which actually still gives good performance. At a high level, the vmalloc algorithm quickly traverses an RB-Tree to find a start position and then more slowly traverses a link list to look for an open spot. Since this new algorithm randomly picks a spot, it mostly just needs to traverse the RB-Tree, and as a result the "tries" are fast enough that the number can be high and still be faster than traversing the linked list. In the data below you can see that the random algorithm is on average actually faster than the existing one.
The increase in number of tries is also to support the BPF JIT use case, by increasing the number of modules that can get high randomness.
Since the __vmalloc_node_try_addr now can optionally fail instead of purging, for the first half of the tries, the algorithm tries to find a spot where it doesn't need to do a purge. For the second half it allows purges. The 50:50 split is to try to be a happy medium between reducing TLB flushes and reducing average allocation time.
Randomness
==========
In the last patchset the size of the random area used in the calculations was incorrect. The entropy should have been closer to 17 bits, not 18, which is why its lower here even though the number of random area tries is cranked up. 17.3 bits is likely maintained to much higher number of allocations than shown here in reality, since it seems that the BPF JIT allocations are usually smaller than modules. If you assume the majority of allocations are 1 page, 17 bits is maintained to 8000 modules.
Modules Min Info
1000 17.3
2000 17.3
3000 17.3
4000 17.3
5000 17.08
6000 16.30
7000 15.67
8000 14.92
Allocation Time
===============
The average module_alloc time over N modules was actually always faster with the random algorithm:
Modules Existing(ns) New(ns)
1000 4,761 1,134
2000 9,730 1,149
3000 15,572 1,396
4000 20,723 2,161
5000 26,206 4,349
6000 31,374 8,615
7000 36,123 14,009
8000 40,174 23,396
Average Nth Allocation time was usually better than the existing algorithm, until the modules get very high.
Module Original(ns) New(ns)
1000 8,800 1,288
2000 20,949 1,477
3000 31,980 2,583
4000 44,539 9,250
5000 55,212 25,986
6000 65,968 39,540
7000 74,883 57,798
8000 85,392 97,319
TLB Flushes Per Allocation
==========================
The new algorithm flushes the TLB a little bit more than the existing algorithm. For the sake of comparison to the old simpler __vmalloc_node_try_addr implementation, this is about a 238x improvement in some cases.
Modules Existing New
1000 0.0014 0.001407
2000 0.001746 0.0018155
3000 0.0018186667 0.0021186667
4000 0.00187525 0.00249675
5000 0.001897 0.0025334
6000 0.0019066667 0.0025228333
7000 0.001925 0.0025315714
8000 0.0019325 0.002553
Memory Usage
============
A downside is that since the random area is fragmented, it uses extra PTE pages. It first approaches 1.3 MB of PTEs as the random area fills. After that it increases more slowly. I am not sure this can be improved without reducing randomness.
Modules Existing(pages) New(pages)
100 6 159
200 11 240
300 15 285
400 20 307
500 23 315
1000 41 330
2000 80 335
3000 118 338
Module Capacity
===============
The estimate of module capacity also now goes back up to ~17000, so the real value should be close to the existing algorithm.
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 | 1 + arch/x86/kernel/module.c | 103 +++++++++++- include/linux/vmalloc.h | 3 + mm/vmalloc.c | 275 +++++++++++++++++++++++++++++++- 4 files changed, 375 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 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.
--- include/linux/vmalloc.h | 3 + mm/vmalloc.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+)
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 +174 -0
180 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 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.
The algorithm evenly breaks the module space in two, a random area and a backup area. For module text allocations, it first tries to allocate at a number of randomly located starting pages inside the random section. If this fails, 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 | 1 + arch/x86/kernel/module.c | 103 ++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 6 deletions(-)
arch/x86/include/asm/pgtable_64_types.h +1 -0
@@ -141,6 +141,7 @@ extern unsigned int ptrs_per_p4d;/* The module sections ends with the start of the fixmap */ #define MODULES_END _AC(0xffffffffff000000, UL)#define MODULES_LEN (MODULES_END - MODULES_VADDR) +#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 +97 -6
119 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 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 RANDOMIZE_BASE and X86_64 is configured:
Largest free space: 847253504
External Memory Fragementation: 20%
Allocations in backup area: 0
Sample output when just X86_64:
Largest free space: 847253504
External Memory Fragementation: 20%
--- mm/vmalloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-)
mm/vmalloc.c +100 -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>
@@ -2922,7 +2924,104 @@ 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 proc_vmalloc_init (void ) +{ +return 0 ; +} +#endif +#if defined(CONFIG_DEBUG_FS ) && defined(CONFIG_X86_64 ) +#if defined(CONFIG_RANDOMIZE_BASE ) +static void print_backup_area (struct seq_file *m,unsigned long backup_cnt) +{ +if (kaslr_enabled ()) +seq_printf (m,"Allocations in backup area:\t%lu\n" , backup_cnt); +} +#else +static void print_backup_area (struct seq_file *m,unsigned long backup_cnt) +{ +}#endif +static int modulefraginfo_debug_show (struct seq_file *m,void *v) +{ +struct list_head *i; +unsigned long last_end =MODULES_VADDR ; +unsigned long total_free =0 ; +unsigned long largest_free =0 ; +unsigned long backup_cnt =0 ; +unsigned long gap; + +spin_lock (&vmap_area_lock); + +list_for_each (i, &vmap_area_list) { + struct vmap_area *obj = list_entry(i, struct vmap_area, list); + +if (!(obj->flags &VM_LAZY_FREE ) + && obj->va_start >=MODULES_VADDR + && obj->va_end <=MODULES_END ) { + +if (obj->va_start >=MODULES_VADDR +MODULES_RAND_LEN ) + backup_cnt++; + + gap = (obj->va_start - last_end); +if (gap > largest_free) + largest_free = gap; + total_free += gap; + + last_end = obj->va_end; + } + } + + gap = (MODULES_END - last_end); +if (gap > largest_free) + largest_free = gap; + total_free += gap; + +spin_unlock (&vmap_area_lock); +seq_printf (m,"Largest free space:\t\t%lu\n" , largest_free); +if (total_free) +seq_printf (m,"External Memory Fragmentation:\t%lu%%\n" , +100 -(100 *largest_free/total_free)); +else +seq_puts (m,"External Memory Fragmentation:\t0%%\n" ); + +print_backup_area (m, 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 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 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
kbuild test robot
build botRe: [PATCH 3/3] vmalloc: Add debugfs modfraginfo
Thank you for the patch! Yet something to improve:
Hi Rick,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc5]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Rick-Edgecombe/KASLR-feature-to-randomize-each-loadable-module/20180707-083637
config: um-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:
# save the attached .config to linux build tree
make ARCH=um All errors (new ones prefixed by >>):
mm/vmalloc.c: In function 'modulefraginfo_debug_show':
>> mm/vmalloc.c:2964:41: error: 'MODULES_RAND_LEN' undeclared (first use in this function); did you mean 'MODULE_NAME_LEN'?
if (obj->va_start >= MODULES_VADDR + MODULES_RAND_LEN)
^~~~~~~~~~~~~~~~
MODULE_NAME_LEN mm/vmalloc.c:2964:41: note: each undeclared identifier is reported only once for each function it appears in
vim +2964 mm/vmalloc.c
2933
2934 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64)
2935 #if defined(CONFIG_RANDOMIZE_BASE)
2936 static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
2937 {
2938 if (kaslr_enabled())
2939 seq_printf(m, "Allocations in backup area:\t%lu\n", backup_cnt);
2940 }
2941 #else
2942 static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
2943 {
2944 }
2945 #endif
2946 static int modulefraginfo_debug_show(struct seq_file *m, void *v)
2947 {
2948 struct list_head *i;
2949 unsigned long last_end = MODULES_VADDR;
2950 unsigned long total_free = 0;
2951 unsigned long largest_free = 0;
2952 unsigned long backup_cnt = 0;
2953 unsigned long gap;
2954
2955 spin_lock(&vmap_area_lock);
2956
2957 list_for_each(i, &vmap_area_list) {
2958 struct vmap_area *obj = list_entry(i, struct vmap_area, list);
2959
2960 if (!(obj->flags & VM_LAZY_FREE)
2961 && obj->va_start >= MODULES_VADDR
2962 && obj->va_end <= MODULES_END) {
2963
> 2964 if (obj->va_start >= MODULES_VADDR + MODULES_RAND_LEN)
2965 backup_cnt++;
2966
2967 gap = (obj->va_start - last_end);
2968 if (gap > largest_free)
2969 largest_free = gap;
2970 total_free += gap;
2971
2972 last_end = obj->va_end;
2973 }
2974 }
2975
2976 gap = (MODULES_END - last_end);
2977 if (gap > largest_free)
2978 largest_free = gap;
2979 total_free += gap;
2980
2981 spin_unlock(&vmap_area_lock);
2982
2983 seq_printf(m, "Largest free space:\t\t%lu\n", largest_free);
2984 if (total_free)
2985 seq_printf(m, "External Memory Fragmentation:\t%lu%%\n",
2986 100-(100*largest_free/total_free));
2987 else
2988 seq_puts(m, "External Memory Fragmentation:\t0%%\n");
2989
2990 print_backup_area(m, backup_cnt);
2991
2992 return 0;
2993 }
2994
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation