Skip to content

Commit d9a6029

Browse files
Zheng Yejiangregkh
Zheng Yejian
authored andcommitted
tracing: Fix uaf issue when open the hist or hist_debug file
[ Upstream commit 1cc111b ] KASAN report following issue. The root cause is when opening 'hist' file of an instance and accessing 'trace_event_file' in hist_show(), but 'trace_event_file' has been freed due to the instance being removed. 'hist_debug' file has the same problem. To fix it, call tracing_{open,release}_file_tr() in file_operations callback to have the ref count and avoid 'trace_event_file' being freed. BUG: KASAN: slab-use-after-free in hist_show+0x11e0/0x1278 Read of size 8 at addr ffff242541e336b8 by task head/190 CPU: 4 PID: 190 Comm: head Not tainted 6.7.0-rc5-g26aff849438c torvalds#133 Hardware name: linux,dummy-virt (DT) Call trace: dump_backtrace+0x98/0xf8 show_stack+0x1c/0x30 dump_stack_lvl+0x44/0x58 print_report+0xf0/0x5a0 kasan_report+0x80/0xc0 __asan_report_load8_noabort+0x1c/0x28 hist_show+0x11e0/0x1278 seq_read_iter+0x344/0xd78 seq_read+0x128/0x1c0 vfs_read+0x198/0x6c8 ksys_read+0xf4/0x1e0 __arm64_sys_read+0x70/0xa8 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Allocated by task 188: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_alloc_info+0x20/0x30 __kasan_slab_alloc+0x6c/0x80 kmem_cache_alloc+0x15c/0x4a8 trace_create_new_event+0x84/0x348 __trace_add_new_event+0x18/0x88 event_trace_add_tracer+0xc4/0x1a0 trace_array_create_dir+0x6c/0x100 trace_array_create+0x2e8/0x568 instance_mkdir+0x48/0x80 tracefs_syscall_mkdir+0x90/0xe8 vfs_mkdir+0x3c4/0x610 do_mkdirat+0x144/0x200 __arm64_sys_mkdirat+0x8c/0xc0 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Freed by task 191: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_free_info+0x34/0x58 __kasan_slab_free+0xe4/0x158 kmem_cache_free+0x19c/0x508 event_file_put+0xa0/0x120 remove_event_file_dir+0x180/0x320 event_trace_del_tracer+0xb0/0x180 __remove_instance+0x224/0x508 instance_rmdir+0x44/0x78 tracefs_syscall_rmdir+0xbc/0x140 vfs_rmdir+0x1cc/0x4c8 do_rmdir+0x220/0x2b8 __arm64_sys_unlinkat+0xc0/0x100 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Suggested-by: Steven Rostedt <[email protected]> Signed-off-by: Zheng Yejian <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 0e73f1b commit d9a6029

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

kernel/trace/trace.c

+6
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,12 @@ int tracing_release_file_tr(struct inode *inode, struct file *filp)
49904990
return 0;
49914991
}
49924992

4993+
int tracing_single_release_file_tr(struct inode *inode, struct file *filp)
4994+
{
4995+
tracing_release_file_tr(inode, filp);
4996+
return single_release(inode, filp);
4997+
}
4998+
49934999
static int tracing_mark_open(struct inode *inode, struct file *filp)
49945000
{
49955001
stream_open(inode, filp);

kernel/trace/trace.h

+1
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ int tracing_open_generic(struct inode *inode, struct file *filp);
612612
int tracing_open_generic_tr(struct inode *inode, struct file *filp);
613613
int tracing_open_file_tr(struct inode *inode, struct file *filp);
614614
int tracing_release_file_tr(struct inode *inode, struct file *filp);
615+
int tracing_single_release_file_tr(struct inode *inode, struct file *filp);
615616
bool tracing_is_disabled(void);
616617
bool tracer_tracing_is_on(struct trace_array *tr);
617618
void tracer_tracing_on(struct trace_array *tr);

kernel/trace/trace_events_hist.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -5630,18 +5630,20 @@ static int event_hist_open(struct inode *inode, struct file *file)
56305630
{
56315631
int ret;
56325632

5633-
ret = security_locked_down(LOCKDOWN_TRACEFS);
5633+
ret = tracing_open_file_tr(inode, file);
56345634
if (ret)
56355635
return ret;
56365636

5637+
/* Clear private_data to avoid warning in single_open() */
5638+
file->private_data = NULL;
56375639
return single_open(file, hist_show, file);
56385640
}
56395641

56405642
const struct file_operations event_hist_fops = {
56415643
.open = event_hist_open,
56425644
.read = seq_read,
56435645
.llseek = seq_lseek,
5644-
.release = single_release,
5646+
.release = tracing_single_release_file_tr,
56455647
};
56465648

56475649
#ifdef CONFIG_HIST_TRIGGERS_DEBUG
@@ -5907,18 +5909,20 @@ static int event_hist_debug_open(struct inode *inode, struct file *file)
59075909
{
59085910
int ret;
59095911

5910-
ret = security_locked_down(LOCKDOWN_TRACEFS);
5912+
ret = tracing_open_file_tr(inode, file);
59115913
if (ret)
59125914
return ret;
59135915

5916+
/* Clear private_data to avoid warning in single_open() */
5917+
file->private_data = NULL;
59145918
return single_open(file, hist_debug_show, file);
59155919
}
59165920

59175921
const struct file_operations event_hist_debug_fops = {
59185922
.open = event_hist_debug_open,
59195923
.read = seq_read,
59205924
.llseek = seq_lseek,
5921-
.release = single_release,
5925+
.release = tracing_single_release_file_tr,
59225926
};
59235927
#endif
59245928

0 commit comments

Comments
 (0)