Skip to content

Commit e2912b0

Browse files
oleg-nesterovrostedt
authored andcommitted
tracing: Change event_filter_read/write to verify i_private != NULL
event_filter_read/write() are racy, ftrace_event_call can be already freed by trace_remove_event_call() callers. 1. Shift mutex_lock(event_mutex) from print/apply_event_filter to the callers. 2. Change the callers, event_filter_read() and event_filter_write() to read i_private under this mutex and abort if it is NULL. This fixes nothing, but now we can change debugfs_remove("filter") callers to nullify ->i_private and fix the the problem. Link: http://lkml.kernel.org/r/20130726172540.GA3619@redhat.com Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent bc6f6b0 commit e2912b0

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

‎kernel/trace/trace_events.c‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -980,21 +980,28 @@ static ssize_t
980980
event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
981981
loff_t *ppos)
982982
{
983-
struct ftrace_event_call *call = filp->private_data;
983+
struct ftrace_event_call *call;
984984
struct trace_seq *s;
985-
int r;
985+
int r = -ENODEV;
986986

987987
if (*ppos)
988988
return 0;
989989

990990
s = kmalloc(sizeof(*s), GFP_KERNEL);
991+
991992
if (!s)
992993
return -ENOMEM;
993994

994995
trace_seq_init(s);
995996

996-
print_event_filter(call, s);
997-
r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
997+
mutex_lock(&event_mutex);
998+
call = event_file_data(filp);
999+
if (call)
1000+
print_event_filter(call, s);
1001+
mutex_unlock(&event_mutex);
1002+
1003+
if (call)
1004+
r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
9981005

9991006
kfree(s);
10001007

@@ -1005,9 +1012,9 @@ static ssize_t
10051012
event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
10061013
loff_t *ppos)
10071014
{
1008-
struct ftrace_event_call *call = filp->private_data;
1015+
struct ftrace_event_call *call;
10091016
char *buf;
1010-
int err;
1017+
int err = -ENODEV;
10111018

10121019
if (cnt >= PAGE_SIZE)
10131020
return -EINVAL;
@@ -1022,7 +1029,12 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
10221029
}
10231030
buf[cnt] = '\0';
10241031

1025-
err = apply_event_filter(call, buf);
1032+
mutex_lock(&event_mutex);
1033+
call = event_file_data(filp);
1034+
if (call)
1035+
err = apply_event_filter(call, buf);
1036+
mutex_unlock(&event_mutex);
1037+
10261038
free_page((unsigned long) buf);
10271039
if (err < 0)
10281040
return err;

‎kernel/trace/trace_events_filter.c‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,15 @@ static void append_filter_err(struct filter_parse_state *ps,
637637
free_page((unsigned long) buf);
638638
}
639639

640+
/* caller must hold event_mutex */
640641
void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
641642
{
642-
struct event_filter *filter;
643+
struct event_filter *filter = call->filter;
643644

644-
mutex_lock(&event_mutex);
645-
filter = call->filter;
646645
if (filter && filter->filter_string)
647646
trace_seq_printf(s, "%s\n", filter->filter_string);
648647
else
649648
trace_seq_puts(s, "none\n");
650-
mutex_unlock(&event_mutex);
651649
}
652650

653651
void print_subsystem_event_filter(struct event_subsystem *system,
@@ -1841,23 +1839,22 @@ static int create_system_filter(struct event_subsystem *system,
18411839
return err;
18421840
}
18431841

1842+
/* caller must hold event_mutex */
18441843
int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
18451844
{
18461845
struct event_filter *filter;
1847-
int err = 0;
1848-
1849-
mutex_lock(&event_mutex);
1846+
int err;
18501847

18511848
if (!strcmp(strstrip(filter_string), "0")) {
18521849
filter_disable(call);
18531850
filter = call->filter;
18541851
if (!filter)
1855-
goto out_unlock;
1852+
return 0;
18561853
RCU_INIT_POINTER(call->filter, NULL);
18571854
/* Make sure the filter is not being used */
18581855
synchronize_sched();
18591856
__free_filter(filter);
1860-
goto out_unlock;
1857+
return 0;
18611858
}
18621859

18631860
err = create_filter(call, filter_string, true, &filter);
@@ -1884,8 +1881,6 @@ int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
18841881
__free_filter(tmp);
18851882
}
18861883
}
1887-
out_unlock:
1888-
mutex_unlock(&event_mutex);
18891884

18901885
return err;
18911886
}

0 commit comments

Comments
 (0)