Skip to content

Commit a0a1e90

Browse files
namhyunggregkh
authored andcommitted
ftrace: Fix function pid filter on instances
commit d879d0b upstream. When function tracer has a pid filter, it adds a probe to sched_switch to track if current task can be ignored. The probe checks the ftrace_ignore_pid from current tr to filter tasks. But it misses to delete the probe when removing an instance so that it can cause a crash due to the invalid tr pointer (use-after-free). This is easily reproducible with the following: # cd /sys/kernel/debug/tracing # mkdir instances/buggy # echo $$ > instances/buggy/set_ftrace_pid # rmdir instances/buggy ============================================================================ BUG: KASAN: use-after-free in ftrace_filter_pid_sched_switch_probe+0x3d/0x90 Read of size 8 by task kworker/0:1/17 CPU: 0 PID: 17 Comm: kworker/0:1 Tainted: G B 4.11.0-rc3 #198 Call Trace: dump_stack+0x68/0x9f kasan_object_err+0x21/0x70 kasan_report.part.1+0x22b/0x500 ? ftrace_filter_pid_sched_switch_probe+0x3d/0x90 kasan_report+0x25/0x30 __asan_load8+0x5e/0x70 ftrace_filter_pid_sched_switch_probe+0x3d/0x90 ? fpid_start+0x130/0x130 __schedule+0x571/0xce0 ... To fix it, use ftrace_clear_pids() to unregister the probe. As instance_rmdir() already updated ftrace codes, it can just free the filter safely. Link: http://lkml.kernel.org/r/[email protected] Fixes: 0c8916c ("tracing: Add rmdir to remove multibuffer instances") Cc: Ingo Molnar <[email protected]> Reviewed-by: Masami Hiramatsu <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 58bc856 commit a0a1e90

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

kernel/trace/ftrace.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,6 +5422,15 @@ static void clear_ftrace_pids(struct trace_array *tr)
54225422
trace_free_pid_list(pid_list);
54235423
}
54245424

5425+
void ftrace_clear_pids(struct trace_array *tr)
5426+
{
5427+
mutex_lock(&ftrace_lock);
5428+
5429+
clear_ftrace_pids(tr);
5430+
5431+
mutex_unlock(&ftrace_lock);
5432+
}
5433+
54255434
static void ftrace_pid_reset(struct trace_array *tr)
54265435
{
54275436
mutex_lock(&ftrace_lock);

kernel/trace/trace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7409,6 +7409,7 @@ static int instance_rmdir(const char *name)
74097409

74107410
tracing_set_nop(tr);
74117411
event_trace_del_tracer(tr);
7412+
ftrace_clear_pids(tr);
74127413
ftrace_destroy_function_files(tr);
74137414
tracefs_remove_recursive(tr->dir);
74147415
free_trace_buffers(tr);

kernel/trace/trace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ int using_ftrace_ops_list_func(void);
884884
void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
885885
void ftrace_init_tracefs_toplevel(struct trace_array *tr,
886886
struct dentry *d_tracer);
887+
void ftrace_clear_pids(struct trace_array *tr);
887888
#else
888889
static inline int ftrace_trace_task(struct trace_array *tr)
889890
{
@@ -902,6 +903,7 @@ ftrace_init_global_array_ops(struct trace_array *tr) { }
902903
static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
903904
static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
904905
static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
906+
static inline void ftrace_clear_pids(struct trace_array *tr) { }
905907
/* ftace_func_t type is not defined, use macro instead of static inline */
906908
#define ftrace_init_array_ops(tr, func) do { } while (0)
907909
#endif /* CONFIG_FUNCTION_TRACER */

0 commit comments

Comments
 (0)