kasan: support backing vmalloc space with real shadow memory
Subsystems (11)
cover kasan: support backing vmalloc space with real shadow memory
Currently, vmalloc space is backed by the early shadow page. This means that kasan is incompatible with VMAP_STACK, and it also provides a hurdle for architectures that do not have a dedicated module space (like powerpc64).
This series provides a mechanism to back vmalloc space with real, dynamically allocated memory. I have only wired up x86, because that's the only currently supported arch I can work with easily, but it's very easy to wire up other architectures.
This has been discussed before in the context of VMAP_STACK:
- https://bugzilla.kernel.org/show_bug.cgi?id=202009
- https://lkml.org/lkml/2018/7/22/198
In terms of implementation details:
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE.
Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage appears to grow at first but then stay fairly stable.
If we run into practical memory exhaustion issues, I'm happy to consider hooking into the book-keeping that vmap does, but I am not convinced that it will be an issue.
Daniel Axtens (3):
kasan: support backing vmalloc space with real shadow memory
fork: support VMAP_STACK with KASAN_VMALLOC x86/kasan: support KASAN_VMALLOC
Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ arch/Kconfig | 9 ++--- arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 13 +++++++ arch/x86/mm/kasan_init_64.c | 10 ++++++ include/linux/kasan.h | 16 +++++++++ kernel/fork.c | 4 +++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 13 files changed, 220 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/3 kasan: support backing vmalloc space with real shadow memory
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE.
Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
This allows KASAN with VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)
Documentation/dev-tools/kasan.rst +60 -0
@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.
include/linux/kasan.h +16 -0
@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);
@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */
lib/Kconfig.kasan +16 -0
@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)
@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASAN
lib/test_kasan.c +26 -0
@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in
@@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/*
@@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);
mm/kasan/common.c +51 -0
66 lines not on this page · raw message
mm/kasan/generic_report.c +3 -0
@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;
mm/kasan/kasan.h +1 -0
@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values
mm/vmalloc.c +14 -1
@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)
@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized.
@@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms;
Signature · 1 line
2.20.1
Dmitry Vyukov
reviewerQuoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).
Hi Daniel,
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.
Some comments below.
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(
mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);
Page table allocations will be protected by mm->page_table_lock, right?
Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),
mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );
Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).
On impl side: kasan_unpoison_shadow seems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:
kasan_unpoison_shadow(area->addr, requested_size);
requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE);
kasan_poison_shadow(area->addr + requested_size,
area->size - requested_size,
KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Dmitry Vyukov
reviewerQuoted 35 lines · Dmitry Vyukov
Quoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).Hi Daniel,
Quoted 8 lines
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.Some comments below.
Marco, please test this with your stack overflow test and with syzkaller (to estimate the amount of new OOBs :)). Also are there any concerns with performance/memory consumption for us?
Quoted 375 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Quoted 44 lines · Dmitry Vyukov
Quoted 42 lines · Dmitry Vyukov
Quoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).Hi Daniel,
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.
Quoted 6 lines
Quoted 1 line
Some comments below.
Marco, please test this with your stack overflow test and with syzkaller (to estimate the amount of new OOBs :)). Also are there any concerns with performance/memory consumption for us?
It appears that stack overflows are *not* detected when KASAN_VMALLOC and VMAP_STACK are enabled.
Tested with:
insmod drivers/misc/lkdtm/lkdtm.ko cpoint_name=DIRECT cpoint_type=EXHAUST_STACK
I've also attached the .config. Anything I missed?
Thanks,
-- Marco
Quoted 375 lines
Quoted 375 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Quoted 51 lines · Marco Elver
Quoted 44 lines · Dmitry Vyukov
Quoted 42 lines · Dmitry Vyukov
Quoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).Hi Daniel,
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.Quoted 5 lines
Quoted 1 line
Some comments below.
Marco, please test this with your stack overflow test and with syzkaller (to estimate the amount of new OOBs :)). Also are there any concerns with performance/memory consumption for us?
Quoted 5 lines
It appears that stack overflows are *not* detected when
KASAN_VMALLOCandVMAP_STACKare enabled.Tested with:
insmoddrivers/misc/lkdtm/lkdtm.kocpoint_name=DIRECT cpoint_type=EXHAUST_STACK
Could you elaborate on what exactly happens?
i.e. does the test fail entirely, or is it detected as a fault (but not reported as a stack overflow)?
If you could post a log, that would be ideal!
Thanks,
Mark.
Quoted 59 lines · Mark Rutland
Quoted 51 lines · Marco Elver
Quoted 44 lines · Dmitry Vyukov
Quoted 42 lines · Dmitry Vyukov
Quoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).Hi Daniel,
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.Quoted 5 lines
Quoted 1 line
Some comments below.
Marco, please test this with your stack overflow test and with syzkaller (to estimate the amount of new OOBs :)). Also are there any concerns with performance/memory consumption for us?
Quoted 5 lines
It appears that stack overflows are *not* detected when
KASAN_VMALLOCandVMAP_STACKare enabled.Tested with:
insmoddrivers/misc/lkdtm/lkdtm.kocpoint_name=DIRECT cpoint_type=EXHAUST_STACK
Quoted 6 lines
Could you elaborate on what exactly happens?
i.e. does the test fail entirely, or is it detected as a fault (but not reported as a stack overflow)?
If you could post a log, that would be ideal!
No fault, system just appears to freeze.
Log:
[ 18.408553] lkdtm: Calling function with 1024 frame size to depth 64 ... [ 18.409546] lkdtm: loop 64/64 ... [ 18.410030] lkdtm: loop 63/64 ... [ 18.410497] lkdtm: loop 62/64 ... [ 18.410972] lkdtm: loop 61/64 ... [ 18.411470] lkdtm: loop 60/64 ... [ 18.411946] lkdtm: loop 59/64 ... [ 18.412415] lkdtm: loop 58/64 ... [ 18.412890] lkdtm: loop 57/64 ... [ 18.413356] lkdtm: loop 56/64 ... [ 18.413830] lkdtm: loop 55/64 ... [ 18.414297] lkdtm: loop 54/64 ... [ 18.414801] lkdtm: loop 53/64 ... [ 18.415269] lkdtm: loop 52/64 ... [ 18.415751] lkdtm: loop 51/64 ... [ 18.416219] lkdtm: loop 50/64 ... [ 18.416698] lkdtm: loop 49/64 ... [ 18.417201] lkdtm: loop 48/64 ... [ 18.417712] lkdtm: loop 47/64 ... [ 18.418216] lkdtm: loop 46/64 ... [ 18.418728] lkdtm: loop 45/64 ... [ 18.419232] lkdtm: loop 44/64 ... [ 18.419747] lkdtm: loop 43/64 ... [ 18.420262] lkdtm: loop 42/64 ...
< no further output, system appears unresponsive at this point >
Thanks,
-- Marco
Hi Marco,
Quoted 8 lines
It appears that stack overflows are *not* detected when
KASAN_VMALLOCandVMAP_STACKare enabled.Tested with:
insmoddrivers/misc/lkdtm/lkdtm.kocpoint_name=DIRECT cpoint_type=EXHAUST_STACKI've also attached the .config. Anything I missed?
Fascinating - it seems to work on my config, a lightly modified defconfig (attached):
[ 111.287854] lkdtm: loop 46/64 ... [ 111.287856] lkdtm: loop 45/64 ... [ 111.287859] lkdtm: loop 44/64 ... [ 111.287862] lkdtm: loop 43/64 ... [ 111.287864] lkdtm: loop 42/64 ... [ 111.287867] lkdtm: loop 41/64 ... [ 111.287869] lkdtm: loop 40/64 ... [ 111.288498] BUG: stack guard page was hit at 000000007bf6ef1a (stack is 000000005952e5cc..00000000ba40316c) [ 111.288499] kernel stack overflow (double-fault): 0000 [#1] SMP KASAN PTI [ 111.288500] CPU: 0 PID: 767 Comm: modprobe Not tainted 5.3.0-rc1-next-20190723+ #91 [ 111.288501] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 [ 111.288501] RIP: 0010:__lock_acquire+0x43/0x3b50 [ 111.288503] Code: 84 24 90 00 00 00 48 c7 84 24 90 00 00 00 b3 8a b5 41 48 8b 9c 24 28 01 00 00 48 c7 84 24 98 00 00 00 f8 5a a9 84 48 c1 e8 03 <48> 89 44 24 18 48 89 c7 48 b8 00 00 00 00 00 fc ff df 48 c7 84 24 [ 111.288504] RSP: 0018:ffffc90000a37fd8 EFLAGS: 00010802 [ 111.288505] RAX: 1ffff9200014700d RBX: 0000000000000000 RCX: 0000000000000000 [ 111.288506] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff84cf3ff8 [ 111.288507] RBP: ffffffff84cf3ff8 R08: 0000000000000001 R09: 0000000000000001 [ 111.288507] R10: fffffbfff0a440cf R11: ffffffff8522067f R12: 0000000000000000 [ 111.288508] R13: 0000000000000000 R14: 0000000000000001 R15: 0000000000000000 [ 111.288509] FS: 00007f97f1f23740(0000) GS:ffff88806c400000(0000) knlGS:0000000000000000 [ 111.288510] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 111.288510] CR2: ffffc90000a37fc8 CR3: 000000006a0fc005 CR4: 0000000000360ef0 [ 111.288511] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 111.288512] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 111.288512] Call Trace: [ 111.288513] lock_acquire+0x125/0x300 [ 111.288513] ? vprintk_emit+0x6c/0x250 [ 111.288514] _raw_spin_lock+0x20/0x30
I will test with your config and see if I can narrow it down tomorrow.
Regards,
Daniel
Quoted 380 lines
Thanks,
-- MarcoQuoted 375 lines
Quoted 375 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Quoted 8 lines
Quoted 8 lines
It appears that stack overflows are *not* detected when
KASAN_VMALLOCandVMAP_STACKare enabled.Tested with:
insmoddrivers/misc/lkdtm/lkdtm.kocpoint_name=DIRECT cpoint_type=EXHAUST_STACKI've also attached the .config. Anything I missed?
So this is a pretty fun bug.
From qemu it seems that CPU#0 is stuck in queued_spin_lock_slowpath. Some registers contain the address of logbuf_lock. Looking at a stack in crash, we're printing:
crash> bt -S 0xffffc90000530000 695
PID: 695 TASK: ffff888069933b00 CPU: 0 COMMAND: "modprobe"
#0 [ffffc90000530000] __schedule at ffffffff834832e5 #1 [ffffc900005300d0] vscnprintf at ffffffff83464398 #2 [ffffc900005300f8] vprintk_store at ffffffff8123d9f0 #3 [ffffc90000530160] vprintk_emit at ffffffff8123e2f9 #4 [ffffc900005301b0] vprintk_func at ffffffff8123ff06 #5 [ffffc900005301c8] printk at ffffffff8123efb0
#6 [ffffc90000530278] recursive_loop at ffffffffc0459939 [lkdtm] #7 [ffffc90000530708] recursive_loop at ffffffffc045994a [lkdtm] #8 [ffffc90000530b98] recursive_loop at ffffffffc045994a [lkdtm]
...
We seem to be deadlocking on logbuf_lock because we take the stack overflow inside printk after it takes the lock, as recursive_loop attempts to print its status. Then we try to printk() some information about the double-fault, which tries to take the lock again, and blam, we're deadlocked.
I didn't see it in my build because I happen to just access the stack differently with lock debugging on - we happen to overflow the stack while not holding the lock.
So I think this is a generic bug, not related to KASAN_VMALLOC. IIUC, it's not safe to kill stack-overflowing tasks with die() because they could be holding arbitrary locks. Instead we should panic() the box. (panic prints without taking locks.)
The following patch works for me, does it fix things for you?
-----------------------------------------------------
arch/x86/kernel/traps.c +7 -6
@@ -301,13 +301,14 @@ __visible void __noreturn handle_stack_overflow(const char *message, struct pt_regs *regs, unsigned long fault_address) { - printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n", - (void *)fault_address, current->stack, - (char *)current->stack + THREAD_SIZE - 1); -die (message, regs,0 ); + /* + * It's not safe to kill the task, as it's in kernel space and + * might be holding important locks. Just panic. + */ -/* Be absolutely certain we don't return. */ -panic ("%s" , message); + panic("%s - stack guard page was hit at %p (stack is %p..%p)", + message, (void *)fault_address, current->stack, + (char *)current->stack + THREAD_SIZE - 1); }
-----------------------------------------------------
Regards,
Daniel
Quoted 419 lines
Fascinating - it seems to work on my config, a lightly modified defconfig (attached):
[ 111.287854] lkdtm: loop 46/64 ... [ 111.287856] lkdtm: loop 45/64 ... [ 111.287859] lkdtm: loop 44/64 ... [ 111.287862] lkdtm: loop 43/64 ... [ 111.287864] lkdtm: loop 42/64 ... [ 111.287867] lkdtm: loop 41/64 ... [ 111.287869] lkdtm: loop 40/64 ... [ 111.288498] BUG: stack guard page was hit at 000000007bf6ef1a (stack is 000000005952e5cc..00000000ba40316c) [ 111.288499] kernel stack overflow (double-fault): 0000 [#1] SMP KASAN PTI [ 111.288500] CPU: 0 PID: 767 Comm: modprobe Not tainted 5.3.0-rc1-next-20190723+ #91 [ 111.288501] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 [ 111.288501] RIP: 0010:__lock_acquire+0x43/0x3b50 [ 111.288503] Code: 84 24 90 00 00 00 48 c7 84 24 90 00 00 00 b3 8a b5 41 48 8b 9c 24 28 01 00 00 48 c7 84 24 98 00 00 00 f8 5a a9 84 48 c1 e8 03 <48> 89 44 24 18 48 89 c7 48 b8 00 00 00 00 00 fc ff df 48 c7 84 24 [ 111.288504] RSP: 0018:ffffc90000a37fd8 EFLAGS: 00010802 [ 111.288505] RAX: 1ffff9200014700d RBX: 0000000000000000 RCX: 0000000000000000 [ 111.288506] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff84cf3ff8 [ 111.288507] RBP: ffffffff84cf3ff8 R08: 0000000000000001 R09: 0000000000000001 [ 111.288507] R10: fffffbfff0a440cf R11: ffffffff8522067f R12: 0000000000000000 [ 111.288508] R13: 0000000000000000 R14: 0000000000000001 R15: 0000000000000000 [ 111.288509] FS: 00007f97f1f23740(0000) GS:ffff88806c400000(0000) knlGS:0000000000000000 [ 111.288510] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 111.288510] CR2: ffffc90000a37fc8 CR3: 000000006a0fc005 CR4: 0000000000360ef0 [ 111.288511] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 111.288512] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 111.288512] Call Trace: [ 111.288513] lock_acquire+0x125/0x300 [ 111.288513] ? vprintk_emit+0x6c/0x250 [ 111.288514] _raw_spin_lock+0x20/0x30I will test with your config and see if I can narrow it down tomorrow.
Regards,
DanielQuoted 378 lines
Thanks,
-- MarcoQuoted 375 lines
Quoted 375 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Quoted 64 lines · Daniel Axtens
Quoted 9 lines
Quoted 9 lines
It appears that stack overflows are *not* detected when
KASAN_VMALLOCandVMAP_STACKare enabled.Tested with:
insmoddrivers/misc/lkdtm/lkdtm.kocpoint_name=DIRECT cpoint_type=EXHAUST_STACKI've also attached the .config. Anything I missed?
So this is a pretty fun bug.
From qemu it seems that CPU#0 is stuck in
queued_spin_lock_slowpath. Some registers contain the address oflogbuf_lock. Looking at a stack in crash, we're printing:crash> bt -S
0xffffc90000530000695
PID: 695 TASK:ffff888069933b00CPU: 0 COMMAND: "modprobe"
#0 [ffffc90000530000]__scheduleatffffffff834832e5#1 [ffffc900005300d0] vscnprintf atffffffff83464398#2 [ffffc900005300f8]vprintk_storeatffffffff8123d9f0#3 [ffffc90000530160]vprintk_emitatffffffff8123e2f9#4 [ffffc900005301b0]vprintk_funcatffffffff8123ff06#5 [ffffc900005301c8] printk atffffffff8123efb0
#6 [ffffc90000530278]recursive_loopatffffffffc0459939[lkdtm] #7 [ffffc90000530708]recursive_loopatffffffffc045994a[lkdtm] #8 [ffffc90000530b98]recursive_loopatffffffffc045994a[lkdtm]
...We seem to be deadlocking on
logbuf_lockbecause we take the stack overflow inside printk after it takes the lock, asrecursive_loopattempts to print its status. Then we try toprintk()some information about the double-fault, which tries to take the lock again, and blam, we're deadlocked.I didn't see it in my build because I happen to just access the stack differently with lock debugging on - we happen to overflow the stack while not holding the lock.
So I think this is a generic bug, not related to
KASAN_VMALLOC. IIUC, it's not safe to kill stack-overflowing tasks withdie()because they could be holding arbitrary locks. Instead we shouldpanic()the box. (panic prints without taking locks.)The following patch works for me, does it fix things for you?
-----------------------------------------------------
arch/x86/kernel/traps.c +4
-4@@ -301,13 +301,14 @@ __visible void __noreturn handle_stack_overflow(const char *message,struct pt_regs *regs, unsigned long fault_address) { - printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n", - (void *)fault_address, current->stack, - (char *)current->stack + THREAD_SIZE - 1); -die (message, regs,0 ); +/* +* It's not safe to kill the task, as it's in kernel space and +* might be holding important locks. Just panic. +*/
arch/x86/kernel/traps.c __visible void __noreturn handle_stack_overflow(const char *message,
Quoted 10 lines
arch/x86/kernel/traps.c +3
-3-/* Be absolutely certain we don't return. */ -panic ("%s" , message); +panic ("%s - stack guard page was hit at %p (stack is %p..%p)" , + message, (void *)fault_address, current->stack, + (char *)current->stack +THREAD_SIZE -1 ); } -----------------------------------------------------
Many thanks for debugging this! Indeed, this seems to fix things for me.
Best Wishes,
-- Marco
Quoted 427 lines
Regards,
DanielQuoted 415 lines
Fascinating - it seems to work on my config, a lightly modified defconfig (attached):
[ 111.287854] lkdtm: loop 46/64 ... [ 111.287856] lkdtm: loop 45/64 ... [ 111.287859] lkdtm: loop 44/64 ... [ 111.287862] lkdtm: loop 43/64 ... [ 111.287864] lkdtm: loop 42/64 ... [ 111.287867] lkdtm: loop 41/64 ... [ 111.287869] lkdtm: loop 40/64 ... [ 111.288498] BUG: stack guard page was hit at 000000007bf6ef1a (stack is 000000005952e5cc..00000000ba40316c) [ 111.288499] kernel stack overflow (double-fault): 0000 [#1] SMP KASAN PTI [ 111.288500] CPU: 0 PID: 767 Comm: modprobe Not tainted 5.3.0-rc1-next-20190723+ #91 [ 111.288501] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 [ 111.288501] RIP: 0010:__lock_acquire+0x43/0x3b50 [ 111.288503] Code: 84 24 90 00 00 00 48 c7 84 24 90 00 00 00 b3 8a b5 41 48 8b 9c 24 28 01 00 00 48 c7 84 24 98 00 00 00 f8 5a a9 84 48 c1 e8 03 <48> 89 44 24 18 48 89 c7 48 b8 00 00 00 00 00 fc ff df 48 c7 84 24 [ 111.288504] RSP: 0018:ffffc90000a37fd8 EFLAGS: 00010802 [ 111.288505] RAX: 1ffff9200014700d RBX: 0000000000000000 RCX: 0000000000000000 [ 111.288506] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff84cf3ff8 [ 111.288507] RBP: ffffffff84cf3ff8 R08: 0000000000000001 R09: 0000000000000001 [ 111.288507] R10: fffffbfff0a440cf R11: ffffffff8522067f R12: 0000000000000000 [ 111.288508] R13: 0000000000000000 R14: 0000000000000001 R15: 0000000000000000 [ 111.288509] FS: 00007f97f1f23740(0000) GS:ffff88806c400000(0000) knlGS:0000000000000000 [ 111.288510] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 111.288510] CR2: ffffc90000a37fc8 CR3: 000000006a0fc005 CR4: 0000000000360ef0 [ 111.288511] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 111.288512] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 111.288512] Call Trace: [ 111.288513] lock_acquire+0x125/0x300 [ 111.288513] ? vprintk_emit+0x6c/0x250 [ 111.288514] _raw_spin_lock+0x20/0x30I will test with your config and see if I can narrow it down tomorrow.
Regards,
DanielQuoted 374 lines
Thanks,
-- MarcoQuoted 371 lines
Quoted 371 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 73 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/87h879gz1g.fsf%40dja-thinkpad.axtens.net.
Quoted 44 lines · Dmitry Vyukov
Quoted 42 lines · Dmitry Vyukov
Quoted 24 lines · Daniel Axtens
Hook into vmalloc and vmap, and dynamically allocate real shadow memory to back the mappings.
Most mappings in vmalloc space are small, requiring less than a full page of shadow space. Allocating a full shadow page per mapping would therefore be wasteful. Furthermore, to ensure that different mappings use different shadow pages, mappings would have to be aligned to
KASAN_SHADOW_SCALE_SIZE*PAGE_SIZE.Instead, share backing space across multiple mappings. Allocate a backing page the first time a mapping in vmalloc space uses a particular page of the shadow region. Keep this page around regardless of whether the mapping is later freed - in the mean time the page could have become shared by another vmalloc mapping.
This can in theory lead to unbounded memory growth, but the vmalloc allocator is pretty good at reusing addresses, so the practical memory usage grows at first but then stays fairly stable.
This requires architecture support to actually use: arches must stop mapping the read-only zero page over portion of the shadow region that covers the vmalloc space and instead leave it unmapped.
Quoted 6 lines
This allows KASAN with
VMAP_STACK, and will be needed for architectures that do not have a separate module space (e.g. powerpc64, which I am currently working on).Hi Daniel,
This is awesome! Thanks so much for taking over this!
I agree with memory/simplicity tradeoffs. Provided that virtual addresses are reused, this should be fine (I hope). If we will ever need to optimize memory consumption, I would even consider something like aligning all vmalloc allocations to PAGE_SIZE*KASAN_SHADOW_SCALE to make things simpler.
Quoted 6 lines
Quoted 1 line
Some comments below.
Marco, please test this with your stack overflow test and with syzkaller (to estimate the amount of new OOBs :)). Also are there any concerns with performance/memory consumption for us?
FYI: I have been running Syzkaller for a few hours; performance is fine, no RCU timeouts. AFAIK no new bugs (yet).
Quoted 375 lines
Quoted 375 lines
Quoted 237 lines
--- Documentation/dev-tools/kasan.rst | 60 +++++++++++++++++++++++++++++++ include/linux/kasan.h | 16 +++++++++ lib/Kconfig.kasan | 16 +++++++++ lib/test_kasan.c | 26 ++++++++++++++ mm/kasan/common.c | 51 ++++++++++++++++++++++++++ mm/kasan/generic_report.c | 3 ++ mm/kasan/kasan.h | 1 + mm/vmalloc.c | 15 +++++++- 8 files changed, 187 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +60
-0@@ -215,3 +215,63 @@ brk handler is used to print bug reports. A potential expansion of this mode is a hardware tag-based mode, which would use hardware memory tagging support instead of compiler instrumentation and manual shadow memory manipulation. + +What memory accesses are sanitised by KASAN? +-------------------------------------------- + +The kernel maps memory in a number of different parts of the address +space. This poses something of a problem for KASAN, which requires +that all addresses accessed by instrumented code have a valid shadow +region. + +The range of kernel virtual addresses is large: there is not enough +real memory to support a real shadow region for every address that +could be accessed by the kernel. + +By default +~~~~~~~~~~ + +By default, architectures only map real memory over the shadow region +for the linear mapping (and potentially other small areas). For all +other areas - such as vmalloc and vmemmap space - a single read-only +page is mapped over the shadow area. This read-only shadow page +declares all memory accesses as permitted. + +This presents a problem for modules: they do not live in the linear +mapping, but in a dedicated module space. By hooking in to the module +allocator, KASAN can temporarily map real shadow memory to cover +them. This allows detection of invalid accesses to module globals, for +example. + +This also creates an incompatibility with ``VMAP_STACK``: if the stack +lives in vmalloc space, it will be shadowed by the read-only page, and +the kernel will fault when trying to set up the shadow data for stack +variables. + +CONFIG_KASAN_VMALLOC +~~~~~~~~~~~~~~~~~~~~ + +With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the +cost of greater memory usage. Currently this is only supported on x86. + +This works by hooking into vmalloc and vmap, and dynamically +allocating real shadow memory to back the mappings. + +Most mappings in vmalloc space are small, requiring less than a full +page of shadow space. Allocating a full shadow page per mapping would +therefore be wasteful. Furthermore, to ensure that different mappings +use different shadow pages, mappings would have to be aligned to +``KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE``. + +Instead, we share backing space across multiple mappings. We allocate +a backing page the first time a mapping in vmalloc space uses a +particular page of the shadow region. We keep this page around +regardless of whether the mapping is later freed - in the mean time +this page could have become shared by another vmalloc mapping. + +This can in theory lead to unbounded memory growth, but the vmalloc +allocator is pretty good at reusing addresses, so the practical memory +usage grows at first but then stays fairly stable. + +This allows ``VMAP_STACK`` support on x86, and enables support of +architectures that do not have a fixed module region.include/linux/kasan.h +16
-0@@ -70,8 +70,18 @@ struct kasan_cache {int free_meta_offset; }; +/* +* These functions provide a special case to support backing module +* allocations with real shadow memory. With KASAN vmalloc, the special +* case is unnecessary, as the work is handled in the generic case. +*/ +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size);void kasan_free_shadow (const struct vm_struct *vm); +#else +static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; } +static inline void kasan_free_shadow (const struct vm_struct *vm) {} +#endif int kasan_add_zero_shadow (void *start,unsigned long size);void kasan_remove_zero_shadow (void *start,unsigned long size);@@ -194,4 +204,10 @@ static inline void *kasan_reset_tag(const void *addr)#endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area); +#else +static inline void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) {} +#endif +#endif /* LINUX_KASAN_H */ lib/Kconfig.kasan +16
-0@@ -6,6 +6,9 @@ config HAVE_ARCH_KASAN config HAVE_ARCH_KASAN_SW_TAGS bool +config HAVE_ARCH_KASAN_VMALLOC + bool + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address)@@ -135,6 +138,19 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_VMALLOC + bool "Back mappings in vmalloc space with real shadow memory" + depends on KASAN && HAVE_ARCH_KASAN_VMALLOC + help + By default, the shadow region for vmalloc space is the read-only + zero page. This means that KASAN cannot detect errors involving + vmalloc space. + + Enabling this option will hook in to vmap/vmalloc and back those + mappings with real shadow memory allocated on demand. This allows + for KASAN to detect more sorts of errors (and to support vmapped + stacks), but at the cost of higher memory usage. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASANlib/test_kasan.c +26
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/vmalloc.h> /* * Note: test functions are marked noinline so that their names appear in @@ -709,6 +710,30 @@ static noinline void __init kmalloc_double_kzfree(void)kzfree (ptr); } +#ifdef CONFIG_KASAN_VMALLOC +static noinline void __init vmalloc_oob(void) +{ +void *area; + +pr_info ("vmalloc out-of-bounds\n" ); + +/* +* We have to be careful not to hit the guard page. +* The MMU will catch that and crash us. +*/ + area =vmalloc (3000 ); +if (!area) { +pr_err ("Allocation failed\n" ); +return ; + } + + ((volatile char *)area)[3100 ]; +vfree (area); +} +#else +static void __init vmalloc_oob (void ) {} +#endif +static int __init kmalloc_tests_init (void ) {/* @@ -752,6 +777,7 @@ static int __init kmalloc_tests_init(void)kasan_strings ();kasan_bitops ();kmalloc_double_kzfree (); +vmalloc_oob ();kasan_restore_multi_shot (multishot);mm/kasan/common.c +17
-0@@ -568,6 +568,7 @@ void kasan_kfree_large(void *ptr, unsigned long ip)/* The object will be poisoned by page_alloc. */ } +#ifndef CONFIG_KASAN_VMALLOC int kasan_module_alloc (void *addr,size_t size) {void *ret;@@ -603,6 +604,7 @@ void kasan_free_shadow(const struct vm_struct *vm)if (vm->flags &VM_KASAN ) vfree(kasan_mem_to_shadow(vm->addr)); } +#endif extern void __kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip);@@ -722,3 +724,52 @@ static int __init kasan_memhotplug_init(void)core_initcall (kasan_memhotplug_init);#endif + +#ifdef CONFIG_KASAN_VMALLOC +void kasan_cover_vmalloc (unsigned long requested_size,struct vm_struct *area) +{ +unsigned long shadow_alloc_start, shadow_alloc_end; +unsigned long addr; +unsigned long backing; +pgd_t *pgdp; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep; +pte_t backing_pte; + + shadow_alloc_start = ALIGN_DOWN(mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 10 lines
mm/kasan/common.c +10
-0+ (unsigned long )kasan_mem_to_shadow (area->addr), + PAGE_SIZE); + shadow_alloc_end =ALIGN ( + (unsigned long )kasan_mem_to_shadow (area->addr + area->size), +PAGE_SIZE ); + + addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Quoted 77 lines
mm/kasan/common.c +2
-0+} +#endif mm/kasan/generic_report.c +3
-0@@ -86,6 +86,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)case KASAN_ALLOCA_RIGHT : bug_type ="alloca-out-of-bounds" ;break ; +case KASAN_VMALLOC_INVALID : + bug_type ="vmalloc-out-of-bounds" ; +break ; }return bug_type;mm/kasan/kasan.h +1
-0@@ -25,6 +25,7 @@#endif #define KASAN_GLOBAL_REDZONE 0xFA/* redzone for global variable */ +#define KASAN_VMALLOC_INVALID 0xF9/* unallocated space in vmapped page */ /* * Stack redzone shadow values mm/vmalloc.c +14
-2@@ -2012,6 +2012,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, va->vm = vm; va->flags |=VM_VM_AREA ;spin_unlock (&vmap_area_lock); + +/* +* If we are in vmalloc space we need to cover the shadow area with +* real memory. If we come here through VM_ALLOC, this is done +* by a higher level function that has access to the true size, +* which might not be a full page. +*/ +if (is_vmalloc_addr (vm->addr) && !(vm->flags &VM_ALLOC )) +kasan_cover_vmalloc (vm->size, vm); }static void clear_vm_uninitialized_flag (struct vm_struct *vm)@@ -2483,6 +2492,8 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; +kasan_cover_vmalloc (real_size, area); +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3324,9 +3335,11 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,spin_unlock (&vmap_area_lock);/* insert all vm's */ -for (area =0 ; area < nr_vms; area++) +for (area =0 ; area < nr_vms; area++) {setup_vmalloc_vm (vms[area], vas[area],VM_ALLOC , pcpu_get_vm_areas); +kasan_cover_vmalloc (sizes[area], vms[area]); + }kfree (vas);return vms; --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-2-dja%40axtens.net.
Hi Dmitry,
Thanks for the feedback!
Quoted 6 lines
mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 4 lines
mm/kasan/common.c +4
-0+ addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?
Yes, each of those alloc functions take the lock if they end up in the slow-path that does the actual allocation (e.g. __p4d_alloc()).
Quoted 26 lines
Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
Quoted 8 lines
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).
So the implementation of vmalloc will always round it up. The description of the function reads, in part:
* Allocate enough pages to cover @size from the page level
* allocator and map them into contiguous kernel virtual space.
So in short it's not quite clear - you could argue that you have a guarantee that you get full pages, but you could also argue that you've specifically asked for @size bytes and @size bytes only.
So far it seems that users are well behaved in terms of using the amount of memory they ask for, but you'll get a better idea than me very quickly as I only tested with trinity. :)
I also handle vmap - for vmap there's no way to specify sub-page allocations so you get as many pages as you ask for.
Quoted 9 lines
On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);
Will do for v2.
Regards,
Daniel
Dmitry Vyukov
reviewerQuoted 56 lines · Daniel Axtens
Hi Dmitry,
Thanks for the feedback!
Quoted 6 lines
mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 751
Quoted 4 lines
mm/kasan/common.c +4
-0+ addr = shadow_alloc_start; + do { + pgdp = pgd_offset_k(addr); + p4dp = p4d_alloc(&init_mm, pgdp, addr);Page table allocations will be protected by
mm->page_table_lock, right?Yes, each of those alloc functions take the lock if they end up in the slow-path that does the actual allocation (e.g.
__p4d_alloc()).Quoted 26 lines
Quoted 12 lines
mm/kasan/common.c +12
-0+ pudp =pud_alloc (&init_mm, p4dp, addr); + pmdp =pmd_alloc (&init_mm, pudp, addr); + ptep =pte_alloc_kernel (pmdp, addr); + +/* +* we can validly get here if pte is not none: it means we +* allocated this page earlier to use part of it for another +* allocation +*/ +if (pte_none (*ptep)) { + backing =__get_free_page (GFP_KERNEL ); + backing_pte = pfn_pte(PFN_DOWN(__pa(backing)),mm/kasan/common.c static int __init kasan_memhotplug_init(void) · line 773
Quoted 10 lines
mm/kasan/common.c +10
-0+ PAGE_KERNEL); +set_pte_at (&init_mm, addr, ptep, backing_pte); + } + }while (addr +=PAGE_SIZE , addr != shadow_alloc_end); + + requested_size =round_up (requested_size,KASAN_SHADOW_SCALE_SIZE ); +kasan_unpoison_shadow (area->addr, requested_size); +kasan_poison_shadow (area->addr + requested_size, + area->size - requested_size, +KASAN_VMALLOC_INVALID );Do I read this correctly that if kernel code does vmalloc(64), they will have exactly 64 bytes available rather than full page? To make
Quoted 8 lines
sure: vmalloc does not guarantee that the available size is rounded up to page size? I suspect we will see a throw out of new bugs related to OOBs on vmalloc memory. So I want to make sure that these will be indeed bugs that we agree need to be fixed.
I am sure there will be bugs where the size is controlled by user-space, so these are bad bugs under any circumstances. But there will also probably be OOBs, where people will try to "prove" that that's fine and will work (just based on our previous experiences :)).So the implementation of vmalloc will always round it up. The description of the function reads, in part:
* Allocate enough pages to cover @size from the page level
* allocator and map them into contiguous kernel virtual space.
Quoted 7 lines
So in short it's not quite clear - you could argue that you have a guarantee that you get full pages, but you could also argue that you've specifically asked for @size bytes and @size bytes only.
So far it seems that users are well behaved in terms of using the amount of memory they ask for, but you'll get a better idea than me very quickly as I only tested with trinity. :)
Ack.
Let's try and see then. There is always an easy fix -- round up size explicitly before vmalloc, which will make the code more explicit and clear. I can hardly see any potential downsides for rounding up the size explicitly.
Quoted 22 lines
I also handle vmap - for vmap there's no way to specify sub-page allocations so you get as many pages as you ask for.
Quoted 9 lines
On impl side:
kasan_unpoison_shadowseems to be capable of handling non-KASAN_SHADOW_SCALE_SIZE-aligned sizes exactly in the way we want. So I think it's better to do:kasan_unpoison_shadow(area->addr, requested_size); requested_size = round_up(requested_size, KASAN_SHADOW_SCALE_SIZE); kasan_poison_shadow(area->addr + requested_size, area->size - requested_size, KASAN_VMALLOC_INVALID);Will do for v2.
Regards,
Daniel--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/87blxdgn9k.fsf%40dja-thinkpad.axtens.net.
PATCH 2/3 fork: support VMAP_STACK with KASAN_VMALLOC
In reply to Daniel AxtensSupporting VMAP_STACK with KASAN_VMALLOC is straightforward:
- clear the shadow region of vmapped stacks when swapping them in
- tweak Kconfig to allow VMAP_STACK to be turned on with KASAN
--- arch/Kconfig | 9 +++++---- kernel/fork.c | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-)
arch/Kconfig +5 -4
@@ -825,16 +825,17 @@ config HAVE_ARCH_VMAP_STACK config VMAP_STACK default y bool "Use a virtually-mapped stack" - depends on HAVE_ARCH_VMAP_STACK && !KASAN + depends on HAVE_ARCH_VMAP_STACK + depends on !KASAN || KASAN_VMALLOC ---help--- Enable this if you want the use virtually-mapped kernel stacks with guard pages. This causes kernel stack overflows to be caught immediately rather than causing difficult-to-diagnose corruption. - This is presently incompatible with KASAN because KASAN expects - the stack to map directly to the KASAN shadow map using a formula - that is incorrect if the stack is in vmalloc space. + To use this with KASAN, the architecture must support backing + virtual mappings with real shadow memory, and KASAN_VMALLOC must + be enabled. config ARCH_OPTIONAL_KERNEL_RWX def_bool n
kernel/fork.c +4 -0
@@ -94,6 +94,7 @@#include <linux/livepatch.h> #include <linux/thread_info.h> #include <linux/stackleak.h> +#include <linux/kasan.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -215,6 +216,9 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)if (!s)continue ; +/* Clear the KASAN shadow of the stack. */ +kasan_unpoison_shadow (s->addr,THREAD_SIZE ); +/* Clear stale pointers from reused stack. */ memset (s->addr,0 ,THREAD_SIZE );
Signature · 1 line
2.20.1
Re: [PATCH 2/3] fork: support VMAP_STACK with KASAN_VMALLOC
Quoted 7 lines · Daniel Axtens
Supporting
VMAP_STACKwithKASAN_VMALLOCis straightforward:- clear the shadow region of vmapped stacks when swapping them in
- tweakKconfigto allowVMAP_STACKto be turned on with KASANSigned-off-by Daniel Axtens <[email protected]>
Quoted 60 lines
--- arch/Kconfig | 9 +++++---- kernel/fork.c | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-)arch/Kconfig +5
-4@@ -825,16 +825,17 @@ config HAVE_ARCH_VMAP_STACK config VMAP_STACK default y bool "Use a virtually-mapped stack" - depends on HAVE_ARCH_VMAP_STACK && !KASAN + depends on HAVE_ARCH_VMAP_STACK + depends on !KASAN || KASAN_VMALLOC ---help--- Enable this if you want the use virtually-mapped kernel stacks with guard pages. This causes kernel stack overflows to be caught immediately rather than causing difficult-to-diagnose corruption. - This is presently incompatible with KASAN because KASAN expects - the stack to map directly to the KASAN shadow map using a formula - that is incorrect if the stack is in vmalloc space. + To use this with KASAN, the architecture must support backing + virtual mappings with real shadow memory, and KASAN_VMALLOC must + be enabled. config ARCH_OPTIONAL_KERNEL_RWX def_bool nkernel/fork.c +4
-1@@ -94,6 +94,7 @@#include <linux/livepatch.h> #include <linux/thread_info.h> #include <linux/stackleak.h> +#include <linux/kasan.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -215,6 +216,9 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)if (!s)continue ; +/* Clear the KASAN shadow of the stack. */ +kasan_unpoison_shadow (s->addr,THREAD_SIZE ); +/* Clear stale pointers from reused stack. */ memset (s->addr,0 ,THREAD_SIZE ); --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-3-dja%40axtens.net.
PATCH 3/3 x86/kasan: support KASAN_VMALLOC
In reply to Daniel AxtensIn the case where KASAN directly allocates memory to back vmalloc space, don't map the early shadow page over it.
Not mapping the early shadow page over the whole shadow space means that there are some pgds that are not populated on boot. Allow the vmalloc fault handler to also fault in vmalloc shadow as needed.
--- arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 13 +++++++++++++ arch/x86/mm/kasan_init_64.c | 10 ++++++++++ 3 files changed, 24 insertions(+)
arch/x86/Kconfig +1 -0
@@ -134,6 +134,7 @@ config X86 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 + select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT
arch/x86/mm/fault.c +13 -0
@@ -340,8 +340,21 @@ static noinline int vmalloc_fault(unsigned long address)pte_t *pte;/* Make sure we are in vmalloc area: */ +#ifndef CONFIG_KASAN_VMALLOC if (!(address >=VMALLOC_START && address <VMALLOC_END ))return -1 ; +#else +/* +* Some of the shadow mapping for the vmalloc area lives outside the +* pgds populated by kasan init. They are created dynamically and so +* we may need to fault them in. +* +* You can observe this with test_vmalloc's align_shift_alloc_test +*/ +if (!((address >=VMALLOC_START && address <VMALLOC_END ) || + (address >=KASAN_SHADOW_START && address <KASAN_SHADOW_END ))) +return -1 ; +#endif /* * Copy kernel mappings over when needed. This can also
arch/x86/mm/kasan_init_64.c +10 -0
@@ -352,9 +352,19 @@ void __init kasan_init(void) shadow_cpu_entry_end = (void *)round_up ( (unsigned long )shadow_cpu_entry_end,PAGE_SIZE ); +/* +* If we're in full vmalloc mode, don't back vmalloc space with early +* shadow pages. +*/ +#ifdef CONFIG_KASAN_VMALLOC +kasan_populate_early_shadow ( +kasan_mem_to_shadow ((void *)VMALLOC_END +1 ), + shadow_cpu_entry_begin); +#else kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)PAGE_OFFSET +MAXMEM ), shadow_cpu_entry_begin); +#endif kasan_populate_shadow ((unsigned long )shadow_cpu_entry_begin, (unsigned long )shadow_cpu_entry_end,0 );
Signature · 1 line
2.20.1
Re: [PATCH 3/3] x86/kasan: support KASAN_VMALLOC
Quoted 9 lines · Daniel Axtens
In the case where KASAN directly allocates memory to back vmalloc space, don't map the early shadow page over it.
Not mapping the early shadow page over the whole shadow space means that there are some pgds that are not populated on boot. Allow the vmalloc fault handler to also fault in vmalloc shadow as needed.
Signed-off-by Daniel Axtens <[email protected]>
Would it make things simpler if we pre-populate the top level page tables for the whole vmalloc region? That would be (16<<40)/4096/512/512*8 = 131072 bytes?
The check in vmalloc_fault in not really a big burden, so I am not sure. Just brining as an option.
Quoted 75 lines
--- arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 13 +++++++++++++ arch/x86/mm/kasan_init_64.c | 10 ++++++++++ 3 files changed, 24 insertions(+)arch/x86/Kconfig +1
-0@@ -134,6 +134,7 @@ config X86 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 + select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPATarch/x86/mm/fault.c +13
-0@@ -340,8 +340,21 @@ static noinline int vmalloc_fault(unsigned long address)pte_t *pte;/* Make sure we are in vmalloc area: */ +#ifndef CONFIG_KASAN_VMALLOC if (!(address >=VMALLOC_START && address <VMALLOC_END ))return -1 ; +#else +/* +* Some of the shadow mapping for the vmalloc area lives outside the +* pgds populated by kasan init. They are created dynamically and so +* we may need to fault them in. +* +* You can observe this with test_vmalloc's align_shift_alloc_test +*/ +if (!((address >=VMALLOC_START && address <VMALLOC_END ) || + (address >=KASAN_SHADOW_START && address <KASAN_SHADOW_END ))) +return -1 ; +#endif /* * Copy kernel mappings over when needed. This can also arch/x86/mm/kasan_init_64.c +10
-1@@ -352,9 +352,19 @@ void __init kasan_init(void) shadow_cpu_entry_end = (void *)round_up ( (unsigned long )shadow_cpu_entry_end,PAGE_SIZE ); +/* +* If we're in full vmalloc mode, don't back vmalloc space with early +* shadow pages. +*/ +#ifdef CONFIG_KASAN_VMALLOC +kasan_populate_early_shadow ( +kasan_mem_to_shadow ((void *)VMALLOC_END +1 ), + shadow_cpu_entry_begin); +#else kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)PAGE_OFFSET +MAXMEM ), shadow_cpu_entry_begin); +#endif kasan_populate_shadow ((unsigned long )shadow_cpu_entry_begin, (unsigned long )shadow_cpu_entry_end,0 ); --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-4-dja%40axtens.net.
Re: [PATCH 3/3] x86/kasan: support KASAN_VMALLOC
Quoted 13 lines · Dmitry Vyukov
Quoted 10 lines · Daniel Axtens
In the case where KASAN directly allocates memory to back vmalloc space, don't map the early shadow page over it.
Not mapping the early shadow page over the whole shadow space means that there are some pgds that are not populated on boot. Allow the vmalloc fault handler to also fault in vmalloc shadow as needed.
Quoted 8 lines
Quoted 1 line
Signed-off-by Daniel Axtens <[email protected]>Would it make things simpler if we pre-populate the top level page tables for the whole vmalloc region? That would be (16<<40)/4096/512/512*8 = 131072 bytes?
The check invmalloc_faultin not really a big burden, so I am not sure. Just brining as an option.
I prefer pre-populating them. In particular, I have already spent far too much time debugging the awful explosions when the stack doesn’t have KASAN backing, and the vmap stack code is very careful to pre-populate the stack pgds — vmalloc_fault fundamentally can’t recover when the stack itself isn’t mapped.
So the vmalloc_fault code, if it stays, needs some careful analysis to make sure it will actually survive all the various context switch cases. Or you can pre-populate it.
Quoted 78 lines
Acked-by Dmitry Vyukov <[email protected]>Quoted 75 lines
---
arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 13 +++++++++++++ arch/x86/mm/kasan_init_64.c | 10 ++++++++++ 3 files changed, 24 insertions(+)arch/x86/Kconfig +1
-0@@ -134,6 +134,7 @@ config X86 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 + select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPATarch/x86/mm/fault.c +13
-0@@ -340,8 +340,21 @@ static noinline int vmalloc_fault(unsigned long address)pte_t *pte;/* Make sure we are in vmalloc area: */ +#ifndef CONFIG_KASAN_VMALLOC if (!(address >=VMALLOC_START && address <VMALLOC_END ))return -1 ; +#else +/* +* Some of the shadow mapping for the vmalloc area lives outside the +* pgds populated by kasan init. They are created dynamically and so +* we may need to fault them in. +* +* You can observe this with test_vmalloc's align_shift_alloc_test +*/ +if (!((address >=VMALLOC_START && address <VMALLOC_END ) || + (address >=KASAN_SHADOW_START && address <KASAN_SHADOW_END ))) +return -1 ; +#endif /* * Copy kernel mappings over when needed. This can also arch/x86/mm/kasan_init_64.c +10
-1@@ -352,9 +352,19 @@ void __init kasan_init(void) shadow_cpu_entry_end = (void *)round_up ( (unsigned long )shadow_cpu_entry_end,PAGE_SIZE ); +/* +* If we're in full vmalloc mode, don't back vmalloc space with early +* shadow pages. +*/ +#ifdef CONFIG_KASAN_VMALLOC +kasan_populate_early_shadow ( +kasan_mem_to_shadow ((void *)VMALLOC_END +1 ), + shadow_cpu_entry_begin); +#else kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)PAGE_OFFSET +MAXMEM ), shadow_cpu_entry_begin); +#endif kasan_populate_shadow ((unsigned long )shadow_cpu_entry_begin, (unsigned long )shadow_cpu_entry_end,0 ); --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-4-dja%40axtens.net.
Re: [PATCH 3/3] x86/kasan: support KASAN_VMALLOC
Quoted 11 lines
Quoted 6 lines
Would it make things simpler if we pre-populate the top level page tables for the whole vmalloc region? That would be (16<<40)/4096/512/512*8 = 131072 bytes?
The check invmalloc_faultin not really a big burden, so I am not sure. Just brining as an option.I prefer pre-populating them. In particular, I have already spent far too much time debugging the awful explosions when the stack doesn’t have KASAN backing, and the vmap stack code is very careful to pre-populate the stack pgds —
vmalloc_faultfundamentally can’t recover when the stack itself isn’t mapped.So the
vmalloc_faultcode, if it stays, needs some careful analysis to make sure it will actually survive all the various context switch cases. Or you can pre-populate it.
No worries - I'll have another crack at prepopulating them for v2.
I tried prepopulating them at first, but because I'm really a powerpc developer rather than an x86 developer (and because I find mm code confusing at the best of times) I didn't have a lot of luck. I think on reflection I stuffed up the pgd/p4d stuff and I think I know how to fix it. So I'll give it another go and ask for help here if I get stuck :)
Regards,
Daniel
Quoted 78 lines
Quoted 78 lines
Acked-by Dmitry Vyukov <[email protected]>Quoted 75 lines
---
arch/x86/Kconfig | 1 + arch/x86/mm/fault.c | 13 +++++++++++++ arch/x86/mm/kasan_init_64.c | 10 ++++++++++ 3 files changed, 24 insertions(+)arch/x86/Kconfig +1
-0@@ -134,6 +134,7 @@ config X86 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 + select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPATarch/x86/mm/fault.c +13
-0@@ -340,8 +340,21 @@ static noinline int vmalloc_fault(unsigned long address)pte_t *pte;/* Make sure we are in vmalloc area: */ +#ifndef CONFIG_KASAN_VMALLOC if (!(address >=VMALLOC_START && address <VMALLOC_END ))return -1 ; +#else +/* +* Some of the shadow mapping for the vmalloc area lives outside the +* pgds populated by kasan init. They are created dynamically and so +* we may need to fault them in. +* +* You can observe this with test_vmalloc's align_shift_alloc_test +*/ +if (!((address >=VMALLOC_START && address <VMALLOC_END ) || + (address >=KASAN_SHADOW_START && address <KASAN_SHADOW_END ))) +return -1 ; +#endif /* * Copy kernel mappings over when needed. This can also arch/x86/mm/kasan_init_64.c +10
-1@@ -352,9 +352,19 @@ void __init kasan_init(void) shadow_cpu_entry_end = (void *)round_up ( (unsigned long )shadow_cpu_entry_end,PAGE_SIZE ); +/* +* If we're in full vmalloc mode, don't back vmalloc space with early +* shadow pages. +*/ +#ifdef CONFIG_KASAN_VMALLOC +kasan_populate_early_shadow ( +kasan_mem_to_shadow ((void *)VMALLOC_END +1 ), + shadow_cpu_entry_begin); +#else kasan_populate_early_shadow (kasan_mem_to_shadow ((void *)PAGE_OFFSET +MAXMEM ), shadow_cpu_entry_begin); +#endif kasan_populate_shadow ((unsigned long )shadow_cpu_entry_begin, (unsigned long )shadow_cpu_entry_end,0 ); --2.20.1
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190725055503.19507-4-dja%40axtens.net.
Andy Lutomirski
maintainerRe: [PATCH 3/3] x86/kasan: support KASAN_VMALLOC
Quoted 13 lines · Daniel Axtens
Quoted 8 lines
Quoted 7 lines
Would it make things simpler if we pre-populate the top level page tables for the whole vmalloc region? That would be (16<<40)/4096/512/512*8 = 131072 bytes?
The check invmalloc_faultin not really a big burden, so I am not sure. Just brining as an option.Quoted 4 lines
I prefer pre-populating them. In particular, I have already spent far too much time debugging the awful explosions when the stack doesn’t have KASAN backing, and the vmap stack code is very careful to pre-populate the stack pgds —
vmalloc_faultfundamentally can’t recover when the stack itself isn’t mapped.So the
vmalloc_faultcode, if it stays, needs some careful analysis to make sure it will actually survive all the various context switch cases. Or you can pre-populate it.
Quoted 8 lines
No worries - I'll have another crack at prepopulating them for v2.
I tried prepopulating them at first, but because I'm really a powerpc developer rather than an x86 developer (and because I find mm code confusing at the best of times) I didn't have a lot of luck. I think on reflection I stuffed up the pgd/p4d stuff and I think I know how to fix it. So I'll give it another go and ask for help here if I get stuck :)
I looked at this a bit more, and I think the vmalloc_fault approach is fine with one tweak. In prepare_switch_to(), you'll want to add something like:
kasan_probe_shadow(next->thread.sp);
where kasan_probe_shadow() is a new function that, depending on kernel config, either does nothing or reads the shadow associated with the passed-in address. Also, if you take this approach, I think you should refactor vmalloc_fault() to push the address check to a new helper:
static bool is_vmalloc_fault_addr(unsigned long addr) {
if (addr >= VMALLOC_START && addr < VMALLOC_END)
return true;
#ifdef CONFIG_WHATEVER
if (addr >= whatever && etc)
return true;
#endif
return false;
}
and call that from vmalloc_fault() rather than duplicating the logic.
Also, thanks for doing this series!