Skip to content

Commit

Permalink
rcu: kasan: record and print call_rcu() call stack
Browse files Browse the repository at this point in the history
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

[[email protected]: build fix]
  Link: http://lkml.kernel.org/r/[email protected]

Suggested-by: Dmitry Vyukov <[email protected]>
Signed-off-by: Walter Wu <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Tested-by: Dmitry Vyukov <[email protected]>
Reviewed-by: Dmitry Vyukov <[email protected]>
Reviewed-by: Andrey Konovalov <[email protected]>
Acked-by: Paul E. McKenney <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Josh Triplett <[email protected]>
Cc: Mathieu Desnoyers <[email protected]>
Cc: Lai Jiangshan <[email protected]>
Cc: Joel Fernandes <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Matthias Brugger <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
truhuan authored and torvalds committed Aug 7, 2020
1 parent ac4766b commit 26e760c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/linux/kasan.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,13 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }

void kasan_cache_shrink(struct kmem_cache *cache);
void kasan_cache_shutdown(struct kmem_cache *cache);
void kasan_record_aux_stack(void *ptr);

#else /* CONFIG_KASAN_GENERIC */

static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
static inline void kasan_record_aux_stack(void *ptr) {}

#endif /* CONFIG_KASAN_GENERIC */

Expand Down
2 changes: 2 additions & 0 deletions kernel/rcu/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <linux/sched/clock.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/kasan.h>
#include "../time/tick-internal.h"

#include "tree.h"
Expand Down Expand Up @@ -2890,6 +2891,7 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
head->func = func;
head->next = NULL;
local_irq_save(flags);
kasan_record_aux_stack(head);
rdp = this_cpu_ptr(&rcu_data);

/* Add the callback to our list. */
Expand Down
4 changes: 2 additions & 2 deletions mm/kasan/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "kasan.h"
#include "../slab.h"

static inline depot_stack_handle_t save_stack(gfp_t flags)
depot_stack_handle_t kasan_save_stack(gfp_t flags)
{
unsigned long entries[KASAN_STACK_DEPTH];
unsigned int nr_entries;
Expand All @@ -53,7 +53,7 @@ static inline depot_stack_handle_t save_stack(gfp_t flags)
static inline void set_track(struct kasan_track *track, gfp_t flags)
{
track->pid = current->pid;
track->stack = save_stack(flags);
track->stack = kasan_save_stack(flags);
}

void kasan_enable_current(void)
Expand Down
21 changes: 21 additions & 0 deletions mm/kasan/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,24 @@ DEFINE_ASAN_SET_SHADOW(f2);
DEFINE_ASAN_SET_SHADOW(f3);
DEFINE_ASAN_SET_SHADOW(f5);
DEFINE_ASAN_SET_SHADOW(f8);

void kasan_record_aux_stack(void *addr)
{
struct page *page = kasan_addr_to_page(addr);
struct kmem_cache *cache;
struct kasan_alloc_meta *alloc_info;
void *object;

if (!(page && PageSlab(page)))
return;

cache = page->slab_cache;
object = nearest_obj(cache, page, addr);
alloc_info = get_alloc_info(cache, object);

/*
* record the last two call_rcu() call stacks.
*/
alloc_info->aux_stack[1] = alloc_info->aux_stack[0];
alloc_info->aux_stack[0] = kasan_save_stack(GFP_NOWAIT);
}
9 changes: 9 additions & 0 deletions mm/kasan/kasan.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ struct kasan_track {

struct kasan_alloc_meta {
struct kasan_track alloc_track;
#ifdef CONFIG_KASAN_GENERIC
/*
* call_rcu() call stack is stored into struct kasan_alloc_meta.
* The free stack is stored into struct kasan_free_meta.
*/
depot_stack_handle_t aux_stack[2];
#endif
struct kasan_track free_track[KASAN_NR_FREE_STACKS];
#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
u8 free_pointer_tag[KASAN_NR_FREE_STACKS];
Expand Down Expand Up @@ -159,6 +166,8 @@ void kasan_report_invalid_free(void *object, unsigned long ip);

struct page *kasan_addr_to_page(const void *addr);

depot_stack_handle_t kasan_save_stack(gfp_t flags);

#if defined(CONFIG_KASAN_GENERIC) && \
(defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
Expand Down
28 changes: 23 additions & 5 deletions mm/kasan/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ static void end_report(unsigned long *flags)
kasan_enable_current();
}

static void print_stack(depot_stack_handle_t stack)
{
unsigned long *entries;
unsigned int nr_entries;

nr_entries = stack_depot_fetch(stack, &entries);
stack_trace_print(entries, nr_entries, 0);
}

static void print_track(struct kasan_track *track, const char *prefix)
{
pr_err("%s by task %u:\n", prefix, track->pid);
if (track->stack) {
unsigned long *entries;
unsigned int nr_entries;

nr_entries = stack_depot_fetch(track->stack, &entries);
stack_trace_print(entries, nr_entries, 0);
print_stack(track->stack);
} else {
pr_err("(stack is not available)\n");
}
Expand Down Expand Up @@ -193,6 +198,19 @@ static void describe_object(struct kmem_cache *cache, void *object,
free_track = kasan_get_free_track(cache, object, tag);
print_track(free_track, "Freed");
pr_err("\n");

#ifdef CONFIG_KASAN_GENERIC
if (alloc_info->aux_stack[0]) {
pr_err("Last call_rcu():\n");
print_stack(alloc_info->aux_stack[0]);
pr_err("\n");
}
if (alloc_info->aux_stack[1]) {
pr_err("Second to last call_rcu():\n");
print_stack(alloc_info->aux_stack[1]);
pr_err("\n");
}
#endif
}

describe_object_addr(cache, object, addr);
Expand Down

0 comments on commit 26e760c

Please sign in to comment.