Commit e60b613
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/[email protected]
Cc: [email protected]
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Fixes: ae6aa16 ("kprobes: introduce ftrace based optimization")
Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Zheng Yejian <[email protected]>
Signed-off-by: Steven Rostedt (Google) <[email protected]>1 parent d2cc859 commit e60b613
1 file changed
+23
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1595 | 1595 | | |
1596 | 1596 | | |
1597 | 1597 | | |
| 1598 | + | |
1598 | 1599 | | |
| 1600 | + | |
1599 | 1601 | | |
1600 | 1602 | | |
1601 | | - | |
| 1603 | + | |
| 1604 | + | |
1602 | 1605 | | |
1603 | | - | |
| 1606 | + | |
1604 | 1607 | | |
1605 | 1608 | | |
1606 | 1609 | | |
| |||
1614 | 1617 | | |
1615 | 1618 | | |
1616 | 1619 | | |
1617 | | - | |
| 1620 | + | |
1618 | 1621 | | |
1619 | 1622 | | |
1620 | 1623 | | |
1621 | | - | |
1622 | | - | |
| 1624 | + | |
| 1625 | + | |
1623 | 1626 | | |
1624 | 1627 | | |
1625 | 1628 | | |
1626 | 1629 | | |
1627 | 1630 | | |
1628 | | - | |
| 1631 | + | |
1629 | 1632 | | |
1630 | 1633 | | |
1631 | | - | |
1632 | | - | |
1633 | | - | |
1634 | 1634 | | |
1635 | | - | |
| 1635 | + | |
1636 | 1636 | | |
1637 | 1637 | | |
1638 | 1638 | | |
| |||
6591 | 6591 | | |
6592 | 6592 | | |
6593 | 6593 | | |
| 6594 | + | |
| 6595 | + | |
6594 | 6596 | | |
6595 | 6597 | | |
6596 | 6598 | | |
| |||
6804 | 6806 | | |
6805 | 6807 | | |
6806 | 6808 | | |
| 6809 | + | |
| 6810 | + | |
| 6811 | + | |
6807 | 6812 | | |
6808 | 6813 | | |
6809 | 6814 | | |
| |||
7137 | 7142 | | |
7138 | 7143 | | |
7139 | 7144 | | |
| 7145 | + | |
7140 | 7146 | | |
7141 | 7147 | | |
7142 | 7148 | | |
| |||
7178 | 7184 | | |
7179 | 7185 | | |
7180 | 7186 | | |
7181 | | - | |
7182 | | - | |
7183 | | - | |
7184 | | - | |
7185 | | - | |
7186 | | - | |
| 7187 | + | |
| 7188 | + | |
7187 | 7189 | | |
7188 | 7190 | | |
7189 | 7191 | | |
| |||
7200 | 7202 | | |
7201 | 7203 | | |
7202 | 7204 | | |
| 7205 | + | |
| 7206 | + | |
| 7207 | + | |
| 7208 | + | |
| 7209 | + | |
7203 | 7210 | | |
7204 | 7211 | | |
7205 | 7212 | | |
| |||
0 commit comments