Patch series "kasan: memorize and print call_rcu stack", v8. This patchset improves KASAN reports by making them to have call_rcu() call stack information. It is useful for programmers to solve use-after-free or double-free memory issue. The KASAN report was as follows(cleaned up slightly): BUG: KASAN: use-after-free in kasan_rcu_reclaim+0x58/0x60 Freed by task 0: kasan_save_stack+0x24/0x50 kasan_set_track+0x24/0x38 kasan_set_free_info+0x18/0x20 __kasan_slab_free+0x10c/0x170 kasan_slab_free+0x10/0x18 kfree+0x98/0x270 kasan_rcu_reclaim+0x1c/0x60 Last call_rcu(): kasan_save_stack+0x24/0x50 kasan_record_aux_stack+0xbc/0xd0 call_rcu+0x8c/0x580 kasan_rcu_uaf+0xf4/0xf8 Generic KASAN will record the last two call_rcu() call stacks and print up to 2 call_rcu() call stacks in KASAN report. it is only suitable for generic KASAN. This feature considers the size of struct kasan_alloc_meta and kasan_free_meta, we try to optimize the structure layout and size, lets it get better memory consumption. [1]https://bugzilla.kernel.org/show_bug.cgi?id=198437 [2]https://groups.google.com/forum/#!searchin/kasan-dev/better$20stack$20traces$20for$20rcu%7Csort:date/kasan-dev/KQsjT_88hDE/7rNUZprRBgAJ This patch (of 4): This feature will record the last two call_rcu() call stacks and prints up to 2 call_rcu() call stacks in KASAN report. When call_rcu() is called, we store the call_rcu() call stack into slub alloc meta-data, so that the KASAN report can print rcu stack. [1]https://bugzilla.kernel.org/show_bug.cgi?id=198437 [2]https://groups.google.com/forum/#!searchin/kasan-dev/better$20stack$20traces$20for$20rcu%7Csort:date/kasan-dev/KQsjT_88hDE/7rNUZprRBgAJ [walter-zh.wu@mediatek.com: build fix] Link: http://lkml.kernel.org/r/20200710162401.23816-1-walter-zh.wu@mediatek.com
Unified · Split
| @@ -174,11 +174,13 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } · 173 unmodified lines | ||
| 174 | 174 | |
| 175 | 175 | void kasan_cache_shrink(struct kmem_cache *cache); |
| 176 | 176 | void kasan_cache_shutdown(struct kmem_cache *cache); |
| 177 | +void kasan_record_aux_stack(void *ptr); | |
| 177 | 178 | |
| 178 | 179 | #else /* CONFIG_KASAN_GENERIC */ |
| 179 | 180 | |
| 180 | 181 | static inline void kasan_cache_shrink(struct kmem_cache *cache) {} |
| 181 | 182 | static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} |
| 183 | +static inline void kasan_record_aux_stack(void *ptr) {} | |
| 182 | 184 | |
| 183 | 185 | #endif /* CONFIG_KASAN_GENERIC */ |
| 184 | 186 | |
| @@ -59,6 +59,7 @@ · 58 unmodified lines | ||
| 59 | 59 | #include <linux/sched/clock.h> |
| 60 | 60 | #include <linux/vmalloc.h> |
| 61 | 61 | #include <linux/mm.h> |
| 62 | +#include <linux/kasan.h> | |
| 62 | 63 | #include "../time/tick-internal.h" |
| 63 | 64 | |
| 64 | 65 | #include "tree.h" |
| @@ -2890,6 +2891,7 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func) · 2825 unmodified lines | ||
| 2890 | 2891 | head->func = func; |
| 2891 | 2892 | head->next = NULL; |
| 2892 | 2893 | local_irq_save(flags); |
| 2894 | + kasan_record_aux_stack(head); | |
| 2893 | 2895 | rdp = this_cpu_ptr(&rcu_data); |
| 2894 | 2896 | |
| 2895 | 2897 | /* Add the callback to our list. */ |
| @@ -40,7 +40,7 @@ · 39 unmodified lines | ||
| 40 | 40 | #include "kasan.h" |
| 41 | 41 | #include "../slab.h" |
| 42 | 42 | |
| 43 | −static inline depot_stack_handle_t save_stack(gfp_t flags) | |
| 43 | +depot_stack_handle_t kasan_save_stack(gfp_t flags) | |
| 44 | 44 | { |
| 45 | 45 | unsigned long entries[KASAN_STACK_DEPTH]; |
| 46 | 46 | unsigned int nr_entries; |
| @@ -53,7 +53,7 @@ static inline depot_stack_handle_t save_stack(gfp_t flags) · 6 unmodified lines | ||
| 53 | 53 | static inline void set_track(struct kasan_track *track, gfp_t flags) |
| 54 | 54 | { |
| 55 | 55 | track->pid = current->pid; |
| 56 | − track->stack = save_stack(flags); | |
| 56 | + track->stack = kasan_save_stack(flags); | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | void kasan_enable_current(void) |
| @@ -324,3 +324,24 @@ DEFINE_ASAN_SET_SHADOW(f2); · 323 unmodified lines | ||
| 324 | 324 | DEFINE_ASAN_SET_SHADOW(f3); |
| 325 | 325 | DEFINE_ASAN_SET_SHADOW(f5); |
| 326 | 326 | DEFINE_ASAN_SET_SHADOW(f8); |
| 327 | + | |
| 328 | +void kasan_record_aux_stack(void *addr) | |
| 329 | +{ | |
| 330 | + struct page *page = kasan_addr_to_page(addr); | |
| 331 | + struct kmem_cache *cache; | |
| 332 | + struct kasan_alloc_meta *alloc_info; | |
| 333 | + void *object; | |
| 334 | + | |
| 335 | + if (!(page && PageSlab(page))) | |
| 336 | + return; | |
| 337 | + | |
| 338 | + cache = page->slab_cache; | |
| 339 | + object = nearest_obj(cache, page, addr); | |
| 340 | + alloc_info = get_alloc_info(cache, object); | |
| 341 | + | |
| 342 | + /* | |
| 343 | + * record the last two call_rcu() call stacks. | |
| 344 | + */ | |
| 345 | + alloc_info->aux_stack[1] = alloc_info->aux_stack[0]; | |
| 346 | + alloc_info->aux_stack[0] = kasan_save_stack(GFP_NOWAIT); | |
| 347 | +} | |
| @@ -104,6 +104,13 @@ struct kasan_track { · 103 unmodified lines | ||
| 104 | 104 | |
| 105 | 105 | struct kasan_alloc_meta { |
| 106 | 106 | struct kasan_track alloc_track; |
| 107 | +#ifdef CONFIG_KASAN_GENERIC | |
| 108 | + /* | |
| 109 | + * call_rcu() call stack is stored into struct kasan_alloc_meta. | |
| 110 | + * The free stack is stored into struct kasan_free_meta. | |
| 111 | + */ | |
| 112 | + depot_stack_handle_t aux_stack[2]; | |
| 113 | +#endif | |
| 107 | 114 | struct kasan_track free_track[KASAN_NR_FREE_STACKS]; |
| 108 | 115 | #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY |
| 109 | 116 | u8 free_pointer_tag[KASAN_NR_FREE_STACKS]; |
| @@ -159,6 +166,8 @@ void kasan_report_invalid_free(void *object, unsigned long ip); · 49 unmodified lines | ||
| 159 | 166 | |
| 160 | 167 | struct page *kasan_addr_to_page(const void *addr); |
| 161 | 168 | |
| 169 | +depot_stack_handle_t kasan_save_stack(gfp_t flags); | |
| 170 | + | |
| 162 | 171 | #if defined(CONFIG_KASAN_GENERIC) && \ |
| 163 | 172 | (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) |
| 164 | 173 | void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); |
| @@ -106,15 +106,20 @@ static void end_report(unsigned long *flags) · 105 unmodified lines | ||
| 106 | 106 | kasan_enable_current(); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | +static void print_stack(depot_stack_handle_t stack) | |
| 110 | +{ | |
| 111 | + unsigned long *entries; | |
| 112 | + unsigned int nr_entries; | |
| 113 | + | |
| 114 | + nr_entries = stack_depot_fetch(stack, &entries); | |
| 115 | + stack_trace_print(entries, nr_entries, 0); | |
| 116 | +} | |
| 117 | + | |
| 109 | 118 | static void print_track(struct kasan_track *track, const char *prefix) |
| 110 | 119 | { |
| 111 | 120 | pr_err("%s by task %u:\n", prefix, track->pid); |
| 112 | 121 | if (track->stack) { |
| 113 | − unsigned long *entries; | |
| 114 | − unsigned int nr_entries; | |
| 115 | − | |
| 116 | − nr_entries = stack_depot_fetch(track->stack, &entries); | |
| 117 | − stack_trace_print(entries, nr_entries, 0); | |
| 122 | + print_stack(track->stack); | |
| 118 | 123 | } else { |
| 119 | 124 | pr_err("(stack is not available)\n"); |
| 120 | 125 | } |
| @@ -193,6 +198,19 @@ static void describe_object(struct kmem_cache *cache, void *object, · 72 unmodified lines | ||
| 193 | 198 | free_track = kasan_get_free_track(cache, object, tag); |
| 194 | 199 | print_track(free_track, "Freed"); |
| 195 | 200 | pr_err("\n"); |
| 201 | + | |
| 202 | +#ifdef CONFIG_KASAN_GENERIC | |
| 203 | + if (alloc_info->aux_stack[0]) { | |
| 204 | + pr_err("Last call_rcu():\n"); | |
| 205 | + print_stack(alloc_info->aux_stack[0]); | |
| 206 | + pr_err("\n"); | |
| 207 | + } | |
| 208 | + if (alloc_info->aux_stack[1]) { | |
| 209 | + pr_err("Second to last call_rcu():\n"); | |
| 210 | + print_stack(alloc_info->aux_stack[1]); | |
| 211 | + pr_err("\n"); | |
| 212 | + } | |
| 213 | +#endif | |
| 196 | 214 | } |
| 197 | 215 | |
| 198 | 216 | describe_object_addr(cache, object, addr); |