Skip to content

Commit e60b613

Browse files
Zheng Yejianrostedt
authored andcommitted
ftrace: Fix possible use-after-free issue in ftrace_location()
KASAN reports a bug: BUG: KASAN: use-after-free in ftrace_location+0x90/0x120 Read of size 8 at addr ffff888141d40010 by task insmod/424 CPU: 8 PID: 424 Comm: insmod Tainted: G W 6.9.0-rc2+ [...] Call Trace: <TASK> dump_stack_lvl+0x68/0xa0 print_report+0xcf/0x610 kasan_report+0xb5/0xe0 ftrace_location+0x90/0x120 register_kprobe+0x14b/0xa40 kprobe_init+0x2d/0xff0 [kprobe_example] do_one_initcall+0x8f/0x2d0 do_init_module+0x13a/0x3c0 load_module+0x3082/0x33d0 init_module_from_file+0xd2/0x130 __x64_sys_finit_module+0x306/0x440 do_syscall_64+0x68/0x140 entry_SYSCALL_64_after_hwframe+0x71/0x79 The root cause is that, in lookup_rec(), ftrace record of some address is being searched in ftrace pages of some module, but those ftrace pages at the same time is being freed in ftrace_release_mod() as the corresponding module is being deleted: CPU1 | CPU2 register_kprobes() { | delete_module() { check_kprobe_address_safe() { | arch_check_ftrace_location() { | ftrace_location() { | lookup_rec() // USE! | ftrace_release_mod() // Free! To fix this issue: 1. Hold rcu lock as accessing ftrace pages in ftrace_location_range(); 2. Use ftrace_location_range() instead of lookup_rec() in ftrace_location(); 3. Call synchronize_rcu() before freeing any ftrace pages both in ftrace_process_locs()/ftrace_release_mod()/ftrace_free_mem(). Link: https://lore.kernel.org/linux-trace-kernel/20240509192859.1273558-1-zhengyejian1@huawei.com Cc: stable@vger.kernel.org Cc: <mhiramat@kernel.org> Cc: <mark.rutland@arm.com> Cc: <mathieu.desnoyers@efficios.com> Fixes: ae6aa16 ("kprobes: introduce ftrace based optimization") Suggested-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent d2cc859 commit e60b613

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

kernel/trace/ftrace.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,12 +1595,15 @@ static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
15951595
unsigned long ftrace_location_range(unsigned long start, unsigned long end)
15961596
{
15971597
struct dyn_ftrace *rec;
1598+
unsigned long ip = 0;
15981599

1600+
rcu_read_lock();
15991601
rec = lookup_rec(start, end);
16001602
if (rec)
1601-
return rec->ip;
1603+
ip = rec->ip;
1604+
rcu_read_unlock();
16021605

1603-
return 0;
1606+
return ip;
16041607
}
16051608

16061609
/**
@@ -1614,25 +1617,22 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end)
16141617
*/
16151618
unsigned long ftrace_location(unsigned long ip)
16161619
{
1617-
struct dyn_ftrace *rec;
1620+
unsigned long loc;
16181621
unsigned long offset;
16191622
unsigned long size;
16201623

1621-
rec = lookup_rec(ip, ip);
1622-
if (!rec) {
1624+
loc = ftrace_location_range(ip, ip);
1625+
if (!loc) {
16231626
if (!kallsyms_lookup_size_offset(ip, &size, &offset))
16241627
goto out;
16251628

16261629
/* map sym+0 to __fentry__ */
16271630
if (!offset)
1628-
rec = lookup_rec(ip, ip + size - 1);
1631+
loc = ftrace_location_range(ip, ip + size - 1);
16291632
}
16301633

1631-
if (rec)
1632-
return rec->ip;
1633-
16341634
out:
1635-
return 0;
1635+
return loc;
16361636
}
16371637

16381638
/**
@@ -6591,6 +6591,8 @@ static int ftrace_process_locs(struct module *mod,
65916591
/* We should have used all pages unless we skipped some */
65926592
if (pg_unuse) {
65936593
WARN_ON(!skipped);
6594+
/* Need to synchronize with ftrace_location_range() */
6595+
synchronize_rcu();
65946596
ftrace_free_pages(pg_unuse);
65956597
}
65966598
return ret;
@@ -6804,6 +6806,9 @@ void ftrace_release_mod(struct module *mod)
68046806
out_unlock:
68056807
mutex_unlock(&ftrace_lock);
68066808

6809+
/* Need to synchronize with ftrace_location_range() */
6810+
if (tmp_page)
6811+
synchronize_rcu();
68076812
for (pg = tmp_page; pg; pg = tmp_page) {
68086813

68096814
/* Needs to be called outside of ftrace_lock */
@@ -7137,6 +7142,7 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
71377142
unsigned long start = (unsigned long)(start_ptr);
71387143
unsigned long end = (unsigned long)(end_ptr);
71397144
struct ftrace_page **last_pg = &ftrace_pages_start;
7145+
struct ftrace_page *tmp_page = NULL;
71407146
struct ftrace_page *pg;
71417147
struct dyn_ftrace *rec;
71427148
struct dyn_ftrace key;
@@ -7178,12 +7184,8 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
71787184
ftrace_update_tot_cnt--;
71797185
if (!pg->index) {
71807186
*last_pg = pg->next;
7181-
if (pg->records) {
7182-
free_pages((unsigned long)pg->records, pg->order);
7183-
ftrace_number_of_pages -= 1 << pg->order;
7184-
}
7185-
ftrace_number_of_groups--;
7186-
kfree(pg);
7187+
pg->next = tmp_page;
7188+
tmp_page = pg;
71877189
pg = container_of(last_pg, struct ftrace_page, next);
71887190
if (!(*last_pg))
71897191
ftrace_pages = pg;
@@ -7200,6 +7202,11 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
72007202
clear_func_from_hashes(func);
72017203
kfree(func);
72027204
}
7205+
/* Need to synchronize with ftrace_location_range() */
7206+
if (tmp_page) {
7207+
synchronize_rcu();
7208+
ftrace_free_pages(tmp_page);
7209+
}
72037210
}
72047211

72057212
void __init ftrace_free_init_mem(void)

0 commit comments

Comments
 (0)