kasan: assorted clean-ups
Subsystems (5)
cover kasan: assorted clean-ups
From: Andrey Konovalov <[email protected]>
Code clean-ups, nothing worthy of being backported to stable.
This series goes on top of the "kasan: save mempool stack traces" one.
Andrey Konovalov (11):
kasan/arm64: improve comments for KASAN_SHADOW_START/END mm, kasan: use KASAN_TAG_KERNEL instead of 0xff
kasan: improve kasan_non_canonical_hook
kasan: clean up kasan_requires_meta
kasan: update kasan_poison documentation comment
kasan: clean up is_kfence_address checks
kasan: respect CONFIG_KASAN_VMALLOC for kasan_flag_vmalloc
kasan: check kasan_vmalloc_enabled in vmalloc tests
kasan: export kasan_poison as GPL
kasan: remove SLUB checks for page_alloc fallbacks in tests
kasan: speed up match_all_mem_tag test for SW_TAGS
arch/arm64/include/asm/kasan.h | 22 +-------------- arch/arm64/include/asm/memory.h | 38 +++++++++++++++++++++----- arch/arm64/mm/kasan_init.c | 5 ++++ include/linux/kasan.h | 1 + include/linux/mm.h | 4 +-- mm/kasan/common.c | 26 +++++++++++------- mm/kasan/hw_tags.c | 8 ++++++ mm/kasan/kasan.h | 48 ++++++++++++++++----------------- mm/kasan/kasan_test.c | 45 ++++++++++++++----------------- mm/kasan/report.c | 34 +++++++++++++---------- mm/kasan/shadow.c | 14 +--------- mm/page_alloc.c | 2 +- 12 files changed, 131 insertions(+), 116 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/11 kasan/arm64: improve comments for KASAN_SHADOW_START/END
From: Andrey Konovalov <[email protected]>
Unify and improve the comments for KASAN_SHADOW_START/END definitions from include/asm/kasan.h and include/asm/memory.h.
Also put both definitions together in include/asm/memory.h.
Also clarify the related BUILD_BUG_ON checks in mm/kasan_init.c.
--- arch/arm64/include/asm/kasan.h | 22 +------------------ arch/arm64/include/asm/memory.h | 38 +++++++++++++++++++++++++++------ arch/arm64/mm/kasan_init.c | 5 +++++ 3 files changed, 38 insertions(+), 27 deletions(-)
arch/arm64/include/asm/kasan.h +1 -21
@@ -15,29 +15,9 @@#if defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS ) +asmlinkage void kasan_early_init(void);void kasan_init (void ); - -/* -* KASAN_SHADOW_START: beginning of the kernel virtual addresses. -* KASAN_SHADOW_END: KASAN_SHADOW_START + 1/N of kernel virtual addresses, -* where N = (1 << KASAN_SHADOW_SCALE_SHIFT). -* -* KASAN_SHADOW_OFFSET: -* This value is used to map an address to the corresponding shadow -* address by the following formula: -* shadow_addr = (address >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET -* -* (1 << (64 - KASAN_SHADOW_SCALE_SHIFT)) shadow addresses that lie in range -* [KASAN_SHADOW_OFFSET, KASAN_SHADOW_END) cover all 64-bits of virtual -* addresses. So KASAN_SHADOW_OFFSET should satisfy the following equation: -* KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - -* (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT)) -*/ -#define _KASAN_SHADOW_START (va) (KASAN_SHADOW_END - (1UL << ((va) - KASAN_SHADOW_SCALE_SHIFT))) -#define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) -void kasan_copy_shadow (pgd_t *pgdir); -asmlinkage void kasan_early_init(void);#else static inline void kasan_init (void ) { }
arch/arm64/include/asm/memory.h +32 -6
@@ -65,15 +65,41 @@#define KERNEL_END _end/* -* Generic and tag-based KASAN require 1/8th and 1/16th of the kernel virtual -* address space for the shadow region respectively. They can bloat the stack -* significantly, so double the (minimum) stack size when they are in use. +* Generic and Software Tag-Based KASAN modes require 1/8th and 1/16th of the +* kernel virtual address space for storing the shadow memory respectively. +* +* The mapping between a virtual memory address and its corresponding shadow +* memory address is defined based on the formula: +* +* shadow_addr = (addr >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET +* +* where KASAN_SHADOW_SCALE_SHIFT is the order of the number of bits that map +* to a single shadow byte and KASAN_SHADOW_OFFSET is a constant that offsets +* the mapping. Note that KASAN_SHADOW_OFFSET does not point to the start of +* the shadow memory region. +* +* Based on this mapping, we define two constants: +* +* KASAN_SHADOW_START: the start of the shadow memory region; +* KASAN_SHADOW_END: the end of the shadow memory region. +* +* KASAN_SHADOW_END is defined first as the shadow address that corresponds to +* the upper bound of possible virtual kernel memory addresses UL(1) << 64 +* according to the mapping formula. +* +* KASAN_SHADOW_START is defined second based on KASAN_SHADOW_END. The shadow +* memory start must map to the lowest possible kernel virtual memory address +* and thus it depends on the actual bitness of the address space. +* +* As KASAN inserts redzones between stack variables, this increases the stack +* memory usage significantly. Thus, we double the (minimum) stack size. */ #if defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS )#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) -#define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) \ - + KASAN_SHADOW_OFFSET) -#define PAGE_END (KASAN_SHADOW_END - (1UL << (vabits_actual - KASAN_SHADOW_SCALE_SHIFT))) +#define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) + KASAN_SHADOW_OFFSET) +#define _KASAN_SHADOW_START (va) (KASAN_SHADOW_END - (UL(1) << ((va) - KASAN_SHADOW_SCALE_SHIFT))) +#define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) +#define PAGE_END KASAN_SHADOW_START#define KASAN_THREAD_SHIFT 1#else #define KASAN_THREAD_SHIFT 0
arch/arm64/mm/kasan_init.c +5 -0
@@ -170,6 +170,11 @@ asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (KASAN_SHADOW_OFFSET !=KASAN_SHADOW_END - (1UL << (64 -KASAN_SHADOW_SCALE_SHIFT ))); +/* +* We cannot check the actual value of KASAN_SHADOW_START during build, +* as it depends on vabits_actual. As a best-effort approach, check +* potential values calculated based on VA_BITS and VA_BITS_MIN. +*/ BUILD_BUG_ON (!IS_ALIGNED (_KASAN_SHADOW_START (VA_BITS ),PGDIR_SIZE ));BUILD_BUG_ON (!IS_ALIGNED (_KASAN_SHADOW_START (VA_BITS_MIN ),PGDIR_SIZE ));BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_END ,PGDIR_SIZE ));
Signature · 1 line
2.25.1
PATCH 2/11 mm, kasan: use KASAN_TAG_KERNEL instead of 0xff
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Use the KASAN_TAG_KERNEL marco instead of open-coding 0xff in the mm code. This macro is provided by include/linux/kasan-tags.h, which does not include any other headers, so it's safe to include it into mm.h without causing circular include dependencies.
--- include/linux/kasan.h | 1 + include/linux/mm.h | 4 ++-- mm/page_alloc.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)
include/linux/kasan.h +1 -0
@@ -4,6 +4,7 @@#include <linux/bug.h> #include <linux/kasan-enabled.h> +#include <linux/kasan-tags.h> #include <linux/kernel.h> #include <linux/static_key.h> #include <linux/types.h>
include/linux/mm.h +2 -2
@@ -1815,7 +1815,7 @@ static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)static inline u8 page_kasan_tag (const struct page *page) { -u8 tag =0xff ; +u8 tag =KASAN_TAG_KERNEL ;if (kasan_enabled ()) { tag = (page->flags >>KASAN_TAG_PGSHIFT ) &KASAN_TAG_MASK ;
@@ -1844,7 +1844,7 @@ static inline void page_kasan_tag_set(struct page *page, u8 tag)static inline void page_kasan_tag_reset (struct page *page) {if (kasan_enabled ()) -page_kasan_tag_set (page,0xff ); +page_kasan_tag_set (page,KASAN_TAG_KERNEL ); }#else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
mm/page_alloc.c +1 -1
@@ -1059,7 +1059,7 @@ static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags)if (IS_ENABLED (CONFIG_KASAN_GENERIC ))return deferred_pages_enabled (); -return page_kasan_tag (page) ==0xff ; +return page_kasan_tag (page) ==KASAN_TAG_KERNEL ; }static void kernel_init_pages (struct page *page,int numpages)
Signature · 1 line
2.25.1
PATCH 3/11 kasan: improve kasan_non_canonical_hook
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Make kasan_non_canonical_hook to be more sure in its report (i.e. say "probably" instead of "maybe") if the address belongs to the shadow memory region for kernel addresses.
Also use the kasan_shadow_to_mem helper to calculate the original address.
Also improve the comments in kasan_non_canonical_hook.
--- mm/kasan/kasan.h | 6 ++++++ mm/kasan/report.c | 34 ++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 14 deletions(-)
mm/kasan/kasan.h +6 -0
@@ -307,6 +307,12 @@ struct kasan_stack_ring { #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) +static __always_inline bool addr_in_shadow(const void *addr) +{ + return addr >= (void *)KASAN_SHADOW_START && + addr < (void *)KASAN_SHADOW_END; +} + #ifndef kasan_shadow_to_mem static inline const void *kasan_shadow_to_mem(const void *shadow_addr) {
mm/kasan/report.c +20 -14
@@ -635,37 +635,43 @@ void kasan_report_async(void)#if defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS )/* -* With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high -* canonical half of the address space) cause out-of-bounds shadow memory reads -* before the actual access. For addresses in the low canonical half of the -* address space, as well as most non-canonical addresses, that out-of-bounds -* shadow memory access lands in the non-canonical part of the address space. -* Help the user figure out what the original bogus pointer was. +* With compiler-based KASAN modes, accesses to bogus pointers (outside of the +* mapped kernel address space regions) cause faults when KASAN tries to check +* the shadow memory before the actual memory access. This results in cryptic +* GPF reports, which are hard for users to interpret. This hook helps users to +* figure out what the original bogus pointer was. */ void kasan_non_canonical_hook (unsigned long addr) {unsigned long orig_addr;const char *bug_type; +/* +* All addresses that came as a result of the memory-to-shadow mapping +* (even for bogus pointers) must be >= KASAN_SHADOW_OFFSET. +*/ if (addr <KASAN_SHADOW_OFFSET )return ; - orig_addr = (addr -KASAN_SHADOW_OFFSET ) <<KASAN_SHADOW_SCALE_SHIFT ; + orig_addr = (unsigned long )kasan_shadow_to_mem ((void *)addr); +/* * For faults near the shadow address for NULL, we can be fairly certain * that this is a KASAN shadow memory access. -* For faults that correspond to shadow for low canonical addresses, we -* can still be pretty sure - that shadow region is a fairly narrow -* chunk of the non-canonical address space. -* But faults that look like shadow for non-canonical addresses are a -* really large chunk of the address space. In that case, we still -* print the decoded address, but make it clear that this is not -* necessarily what's actually going on. +* For faults that correspond to the shadow for low or high canonical +* addresses, we can still be pretty sure: these shadow regions are a +* fairly narrow chunk of the address space. +* But the shadow for non-canonical addresses is a really large chunk +* of the address space. For this case, we still print the decoded +* address, but make it clear that this is not necessarily what's +* actually going on. */ if (orig_addr <PAGE_SIZE ) bug_type ="null-ptr-deref" ;else if (orig_addr <TASK_SIZE ) bug_type ="probably user-memory-access" ; +else if (addr_in_shadow ((void *)addr)) + bug_type ="probably wild-memory-access" ;else bug_type ="maybe wild-memory-access" ; pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
Signature · 1 line
2.25.1
PATCH 4/11 kasan: clean up kasan_requires_meta
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Currently, for Generic KASAN mode, kasan_requires_meta is defined to return kasan_stack_collection_enabled.
Even though the Generic mode does not support disabling stack trace collection, kasan_requires_meta was implemented in this way to make it easier to implement the disabling for the Generic mode in the future.
However, for the Generic mode, the per-object metadata also stores the quarantine link. So even if disabling stack collection is implemented, the per-object metadata will still be required.
Fix kasan_requires_meta to return true for the Generic mode and update the related comments.
This change does not fix any observable bugs but rather just brings the code to a cleaner state.
--- mm/kasan/kasan.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/kasan/kasan.h +9 -9
@@ -101,21 +101,21 @@ static inline bool kasan_sample_page_alloc(unsigned int order)#ifdef CONFIG_KASAN_GENERIC -/* Generic KASAN uses per-object metadata to store stack traces. */ +/* +* Generic KASAN uses per-object metadata to store alloc and free stack traces +* and the quarantine link. +*/ static inline bool kasan_requires_meta (void ) { -/* -* Technically, Generic KASAN always collects stack traces right now. -* However, let's use kasan_stack_collection_enabled() in case the -* kasan.stacktrace command-line argument is changed to affect -* Generic KASAN. -*/ -return kasan_stack_collection_enabled (); +return true; }#else /* CONFIG_KASAN_GENERIC */ -/* Tag-based KASAN modes do not use per-object metadata. */ +/* +* Tag-based KASAN modes do not use per-object metadata: they use the stack +* ring to store alloc and free stack traces and do not use qurantine. +*/ static inline bool kasan_requires_meta (void ) {return false;
Signature · 1 line
2.25.1
PATCH 5/11 kasan: update kasan_poison documentation comment
In reply to [email protected]From: Andrey Konovalov <[email protected]>
The comment for kasan_poison says that the size argument gets aligned by the function to KASAN_GRANULE_SIZE, which is wrong: the argument must be already aligned when it is passed to the function.
Remove the invalid part of the comment.
--- mm/kasan/kasan.h | 2 -- 1 file changed, 2 deletions(-)
mm/kasan/kasan.h +0 -2
@@ -513,8 +513,6 @@ static inline bool kasan_byte_accessible(const void *addr)* @size - range size, must be aligned to KASAN_GRANULE_SIZE * @value - value that's written to metadata for the range * @init - whether to initialize the memory range (only for hardware tag-based) -* -* The size gets aligned to KASAN_GRANULE_SIZE before marking the range. */ void kasan_poison (const void *addr,size_t size,u8 value,bool init);
Signature · 1 line
2.25.1
PATCH 6/11 kasan: clean up is_kfence_address checks
In reply to [email protected]From: Andrey Konovalov <[email protected]>
1. Do not untag addresses that are passed to is_kfence_address: it tolerates tagged addresses.
2. Move is_kfence_address checks from internal KASAN functions (kasan_poison/unpoison, etc.) to external-facing ones.
Note that kasan_poison/unpoison are never called outside of KASAN/slab code anymore; the comment is wrong, so drop it.
3. Simplify/reorganize the code around the updated checks.
--- mm/kasan/common.c | 26 +++++++++++++++++--------- mm/kasan/kasan.h | 16 ++-------------- mm/kasan/shadow.c | 12 ------------ 3 files changed, 19 insertions(+), 35 deletions(-)
mm/kasan/common.c +17 -9
@@ -79,6 +79,9 @@ EXPORT_SYMBOL(kasan_disable_current);void __kasan_unpoison_range (const void *address,size_t size) { +if (is_kfence_address (address)) +return ; +kasan_unpoison (address, size, false); }
@@ -218,9 +221,6 @@ static inline bool poison_slab_object(struct kmem_cache *cache, void *object, tagged_object = object; object =kasan_reset_tag (object); -if (is_kfence_address (object)) -return false; -if (unlikely (nearest_obj (cache,virt_to_slab (object), object) != object)) {kasan_report_invalid_free (tagged_object, ip,KASAN_REPORT_INVALID_FREE );return true;
@@ -247,7 +247,12 @@ static inline bool poison_slab_object(struct kmem_cache *cache, void *object,bool __kasan_slab_free (struct kmem_cache *cache,void *object,unsigned long ip,bool init) { -bool buggy_object =poison_slab_object (cache, object, ip, init); +bool buggy_object; + +if (is_kfence_address (object)) +return false; + + buggy_object =poison_slab_object (cache, object, ip, init);return buggy_object ? true :kasan_quarantine_put (cache, object); }
@@ -359,7 +364,7 @@ void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *objectif (unlikely (object ==NULL ))return NULL ; -if (is_kfence_address (kasan_reset_tag (object))) +if (is_kfence_address (object))return (void *)object;/* The object has already been unpoisoned by kasan_slab_alloc(). */
@@ -417,7 +422,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flagif (unlikely (object ==ZERO_SIZE_PTR ))return (void *)object; -if (is_kfence_address (kasan_reset_tag (object))) +if (is_kfence_address (object))return (void *)object;/*
@@ -483,6 +488,9 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)return true; } +if (is_kfence_address (ptr)) +return false; + slab =folio_slab (folio);return !poison_slab_object (slab->slab_cache, ptr, ip, false); }
@@ -492,9 +500,6 @@ void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)struct slab *slab;gfp_t flags =0 ;/* Might be executing under a lock. */ -if (is_kfence_address (kasan_reset_tag (ptr))) -return ; - slab =virt_to_slab (ptr);/*
@@ -507,6 +512,9 @@ void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)return ; } +if (is_kfence_address (ptr)) +return ; +/* Unpoison the object and save alloc info for non-kmalloc() allocations. */ unpoison_slab_object (slab->slab_cache, ptr, size, flags);
mm/kasan/kasan.h +2 -14
@@ -466,35 +466,23 @@ static inline u8 kasan_random_tag(void) { return 0; }static inline void kasan_poison (const void *addr,size_t size,u8 value,bool init) { - addr =kasan_reset_tag (addr); - -/* Skip KFENCE memory if called explicitly outside of sl*b. */ -if (is_kfence_address (addr)) -return ; -if (WARN_ON ((unsigned long )addr &KASAN_GRANULE_MASK ))return ;if (WARN_ON (size &KASAN_GRANULE_MASK ))return ; -hw_set_mem_tag_range ((void *)addr, size, value, init); +hw_set_mem_tag_range (kasan_reset_tag (addr), size, value, init); }static inline void kasan_unpoison (const void *addr,size_t size,bool init) {u8 tag =get_tag (addr); - addr =kasan_reset_tag (addr); - -/* Skip KFENCE memory if called explicitly outside of sl*b. */ -if (is_kfence_address (addr)) -return ; -if (WARN_ON ((unsigned long )addr &KASAN_GRANULE_MASK ))return ; size =round_up (size,KASAN_GRANULE_SIZE ); -hw_set_mem_tag_range ((void *)addr, size, tag, init); +hw_set_mem_tag_range (kasan_reset_tag (addr), size, tag, init); }static inline bool kasan_byte_accessible (const void *addr)
mm/kasan/shadow.c +0 -12
@@ -135,10 +135,6 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)*/ addr =kasan_reset_tag (addr); -/* Skip KFENCE memory if called explicitly outside of sl*b. */ -if (is_kfence_address (addr)) -return ; -if (WARN_ON ((unsigned long )addr &KASAN_GRANULE_MASK ))return ;if (WARN_ON (size &KASAN_GRANULE_MASK ))
@@ -175,14 +171,6 @@ void kasan_unpoison(const void *addr, size_t size, bool init)*/ addr =kasan_reset_tag (addr); -/* -* Skip KFENCE memory if called explicitly outside of sl*b. Also note -* that calls to ksize(), where size is not a multiple of machine-word -* size, would otherwise poison the invalid portion of the word. -*/ -if (is_kfence_address (addr)) -return ; -if (WARN_ON ((unsigned long )addr &KASAN_GRANULE_MASK ))return ;
Signature · 1 line
2.25.1
PATCH 7/11 kasan: respect CONFIG_KASAN_VMALLOC for kasan_flag_vmalloc
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Never enable the kasan_flag_vmalloc static branch unless CONFIG_KASAN_VMALLOC is enabled.
This does not fix any observable bugs (vmalloc annotations for the HW_TAGS mode are no-op with CONFIG_KASAN_VMALLOC disabled) but rather just cleans up the code.
--- mm/kasan/hw_tags.c | 7 +++++++ mm/kasan/kasan.h | 1 + 2 files changed, 8 insertions(+)
mm/kasan/hw_tags.c +7 -0
@@ -57,7 +57,11 @@ enum kasan_mode kasan_mode __ro_after_init;EXPORT_SYMBOL_GPL (kasan_mode);/* Whether to enable vmalloc tagging. */ +#ifdef CONFIG_KASAN_VMALLOC DEFINE_STATIC_KEY_TRUE (kasan_flag_vmalloc); +#else +DEFINE_STATIC_KEY_FALSE (kasan_flag_vmalloc); +#endif #define PAGE_ALLOC_SAMPLE_DEFAULT 1#define PAGE_ALLOC_SAMPLE_ORDER_DEFAULT 3
@@ -119,6 +123,9 @@ static int __init early_kasan_flag_vmalloc(char *arg)if (!arg)return -EINVAL ; +if (!IS_ENABLED (CONFIG_KASAN_VMALLOC )) +return 0 ; +if (!strcmp (arg,"off" )) kasan_arg_vmalloc =KASAN_ARG_VMALLOC_OFF ;else if (!strcmp (arg,"on" ))
mm/kasan/kasan.h +1 -0
@@ -49,6 +49,7 @@ DECLARE_PER_CPU(long, kasan_page_alloc_skip);static inline bool kasan_vmalloc_enabled (void ) { +/* Static branch is never enabled with CONFIG_KASAN_VMALLOC disabled. */ return static_branch_likely (&kasan_flag_vmalloc); }
Signature · 1 line
2.25.1
PATCH 8/11 kasan: check kasan_vmalloc_enabled in vmalloc tests
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Check that vmalloc poisoning is not disabled via command line when running the vmalloc-related KASAN tests. Skip the tests otherwise.
--- mm/kasan/hw_tags.c | 1 + mm/kasan/kasan.h | 5 +++++ mm/kasan/kasan_test.c | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-)
mm/kasan/hw_tags.c +1 -0
@@ -62,6 +62,7 @@ DEFINE_STATIC_KEY_TRUE(kasan_flag_vmalloc);#else DEFINE_STATIC_KEY_FALSE (kasan_flag_vmalloc);#endif +EXPORT_SYMBOL_GPL (kasan_flag_vmalloc);#define PAGE_ALLOC_SAMPLE_DEFAULT 1#define PAGE_ALLOC_SAMPLE_ORDER_DEFAULT 3
mm/kasan/kasan.h +5 -0
@@ -83,6 +83,11 @@ static inline bool kasan_sample_page_alloc(unsigned int order)#else /* CONFIG_KASAN_HW_TAGS */ +static inline bool kasan_vmalloc_enabled (void ) +{ +return IS_ENABLED (CONFIG_KASAN_VMALLOC ); +} +static inline bool kasan_async_fault_possible (void ) {return false;
mm/kasan/kasan_test.c +10 -1
@@ -1540,6 +1540,9 @@ static void vmalloc_helpers_tags(struct kunit *test)KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_VMALLOC ); +if (!kasan_vmalloc_enabled ()) +kunit_skip (test,"Test requires kasan.vmalloc=on" ); + ptr =vmalloc (PAGE_SIZE );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr);
@@ -1574,6 +1577,9 @@ static void vmalloc_oob(struct kunit *test)KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_VMALLOC ); +if (!kasan_vmalloc_enabled ()) +kunit_skip (test,"Test requires kasan.vmalloc=on" ); + v_ptr =vmalloc (size);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, v_ptr);
@@ -1627,6 +1633,9 @@ static void vmap_tags(struct kunit *test)KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_VMALLOC ); +if (!kasan_vmalloc_enabled ()) +kunit_skip (test,"Test requires kasan.vmalloc=on" ); + p_page =alloc_pages (GFP_KERNEL ,1 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, p_page); p_ptr =page_address (p_page);
@@ -1745,7 +1754,7 @@ static void match_all_not_assigned(struct kunit *test)free_pages ((unsigned long )ptr, order); } -if (!IS_ENABLED (CONFIG_KASAN_VMALLOC )) +if (!kasan_vmalloc_enabled ())return ;for (i =0 ; i <256 ; i++) {
Signature · 1 line
2.25.1
PATCH 9/11 kasan: export kasan_poison as GPL
In reply to [email protected]From: Andrey Konovalov <[email protected]>
KASAN uses EXPORT_SYMBOL_GPL for symbols whose exporting is only required for KASAN tests when they are built as a module.
kasan_poison is one on those symbols, so export it as GPL.
--- mm/kasan/shadow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/kasan/shadow.c +1 -1
@@ -145,7 +145,7 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)__memset (shadow_start, value, shadow_end - shadow_start); } -EXPORT_SYMBOL (kasan_poison); +EXPORT_SYMBOL_GPL (kasan_poison);#ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule (const void *addr,size_t size)
Signature · 1 line
2.25.1
PATCH 10/11 kasan: remove SLUB checks for page_alloc fallbacks in tests
In reply to [email protected]From: Andrey Konovalov <[email protected]>
A number of KASAN tests rely on the fact that calling kmalloc with a size larger than an order-1 page falls back onto page_alloc.
This fallback was originally only implemented for SLUB, but since commit d6a71648dbc0 ("mm/slab: kmalloc: pass requests larger than order-1 page to page allocator"), it is also implemented for SLAB.
Thus, drop the SLUB checks from the tests.
--- mm/kasan/kasan_test.c | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-)
mm/kasan/kasan_test.c +2 -24
@@ -215,7 +215,7 @@ static void kmalloc_node_oob_right(struct kunit *test)/* * Check that KASAN detects an out-of-bounds access for a big object allocated -* via kmalloc(). But not as big as to trigger the page_alloc fallback for SLUB. +* via kmalloc(). But not as big as to trigger the page_alloc fallback. */ static void kmalloc_big_oob_right (struct kunit *test) {
@@ -233,8 +233,7 @@ static void kmalloc_big_oob_right(struct kunit *test)/* * The kmalloc_large_* tests below use kmalloc() to allocate a memory chunk * that does not fit into the largest slab cache and therefore is allocated via -* the page_alloc fallback for SLUB. SLAB has no such fallback, and thus these -* tests are not supported for it. +* the page_alloc fallback. */ static void kmalloc_large_oob_right (struct kunit *test)
@@ -242,8 +241,6 @@ static void kmalloc_large_oob_right(struct kunit *test)char *ptr;size_t size =KMALLOC_MAX_CACHE_SIZE +10 ; -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - ptr =kmalloc (size,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr);
@@ -258,8 +255,6 @@ static void kmalloc_large_uaf(struct kunit *test)char *ptr;size_t size =KMALLOC_MAX_CACHE_SIZE +10 ; -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - ptr =kmalloc (size,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr);kfree (ptr);
@@ -272,8 +267,6 @@ static void kmalloc_large_invalid_free(struct kunit *test)char *ptr;size_t size =KMALLOC_MAX_CACHE_SIZE +10 ; -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - ptr =kmalloc (size,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr);
@@ -407,18 +400,12 @@ static void krealloc_less_oob(struct kunit *test)static void krealloc_large_more_oob (struct kunit *test) { -/* page_alloc fallback is only implemented for SLUB. */ -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); -krealloc_more_oob_helper (test,KMALLOC_MAX_CACHE_SIZE +201 ,KMALLOC_MAX_CACHE_SIZE +235 ); }static void krealloc_large_less_oob (struct kunit *test) { -/* page_alloc fallback is only implemented for SLUB. */ -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); -krealloc_less_oob_helper (test,KMALLOC_MAX_CACHE_SIZE +235 ,KMALLOC_MAX_CACHE_SIZE +201 ); }
@@ -1144,9 +1131,6 @@ static void mempool_kmalloc_large_uaf(struct kunit *test)size_t size =KMALLOC_MAX_CACHE_SIZE +1 ;void *extra_elem; -/* page_alloc fallback is only implemented for SLUB. */ -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - extra_elem =mempool_prepare_kmalloc (test, &pool, size);mempool_uaf_helper (test, &pool, false);
@@ -1215,9 +1199,6 @@ static void mempool_kmalloc_large_double_free(struct kunit *test)size_t size =KMALLOC_MAX_CACHE_SIZE +1 ;char *extra_elem; -/* page_alloc fallback is only implemented for SLUB. */ -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - extra_elem =mempool_prepare_kmalloc (test, &pool, size);mempool_double_free_helper (test, &pool);
@@ -1272,9 +1253,6 @@ static void mempool_kmalloc_large_invalid_free(struct kunit *test)size_t size =KMALLOC_MAX_CACHE_SIZE +1 ;char *extra_elem; -/* page_alloc fallback is only implemented for SLUB. */ -KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_SLUB ); - extra_elem =mempool_prepare_kmalloc (test, &pool, size);mempool_kmalloc_invalid_free_helper (test, &pool);
Signature · 1 line
2.25.1
PATCH 11/11 kasan: speed up match_all_mem_tag test for SW_TAGS
In reply to [email protected]From: Andrey Konovalov <[email protected]>
Checking all 256 possible tag values in the match_all_mem_tag KASAN test is slow and produces 256 reports. Instead, just check the first 8 and the last 8.
--- mm/kasan/kasan_test.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/kasan/kasan_test.c +8 -0
@@ -1785,6 +1785,14 @@ static void match_all_mem_tag(struct kunit *test)/* For each possible tag value not matching the pointer tag. */ for (tag =KASAN_TAG_MIN ; tag <=KASAN_TAG_KERNEL ; tag++) { +/* +* For Software Tag-Based KASAN, skip the majority of tag +* values to avoid the test printing too many reports. +*/ +if (IS_ENABLED (CONFIG_KASAN_SW_TAGS ) && + tag >=KASAN_TAG_MIN +8 && tag <=KASAN_TAG_KERNEL -8 ) +continue ; +if (tag ==get_tag (ptr))continue ;
Signature · 1 line
2.25.1