Skip to content

Commit 40ee4df

Browse files
Steven Rostedtrostedt
authored andcommitted
tracing: Have "enable" file use refcounts like the "filter" file
The "enable" file for the event system can be removed when a module is unloaded and the event system only has events from that module. As the event system nr_events count goes to zero, it may be freed if its ref_count is also set to zero. Like the "filter" file, the "enable" file may be opened by a task and referenced later, after a module has been unloaded and the events for that event system have been removed. Although the "filter" file referenced the event system structure, the "enable" file only references a pointer to the event system name. Since the name is freed when the event system is removed, it is possible that an access to the "enable" file may reference a freed pointer. Update the "enable" file to use the subsystem_open() routine that the "filter" file uses, to keep a reference to the event system structure while the "enable" file is opened. Cc: <stable@kernel.org> Reported-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent e9dbfae commit 40ee4df

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎kernel/trace/trace_events.c‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
557557
loff_t *ppos)
558558
{
559559
const char set_to_char[4] = { '?', '0', '1', 'X' };
560-
const char *system = filp->private_data;
560+
struct event_subsystem *system = filp->private_data;
561561
struct ftrace_event_call *call;
562562
char buf[2];
563563
int set = 0;
@@ -568,7 +568,7 @@ system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
568568
if (!call->name || !call->class || !call->class->reg)
569569
continue;
570570

571-
if (system && strcmp(call->class->system, system) != 0)
571+
if (system && strcmp(call->class->system, system->name) != 0)
572572
continue;
573573

574574
/*
@@ -598,7 +598,8 @@ static ssize_t
598598
system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
599599
loff_t *ppos)
600600
{
601-
const char *system = filp->private_data;
601+
struct event_subsystem *system = filp->private_data;
602+
const char *name = NULL;
602603
unsigned long val;
603604
char buf[64];
604605
ssize_t ret;
@@ -622,7 +623,14 @@ system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
622623
if (val != 0 && val != 1)
623624
return -EINVAL;
624625

625-
ret = __ftrace_set_clr_event(NULL, system, NULL, val);
626+
/*
627+
* Opening of "enable" adds a ref count to system,
628+
* so the name is safe to use.
629+
*/
630+
if (system)
631+
name = system->name;
632+
633+
ret = __ftrace_set_clr_event(NULL, name, NULL, val);
626634
if (ret)
627635
goto out;
628636

@@ -862,6 +870,9 @@ static int subsystem_open(struct inode *inode, struct file *filp)
862870
struct event_subsystem *system = NULL;
863871
int ret;
864872

873+
if (!inode->i_private)
874+
goto skip_search;
875+
865876
/* Make sure the system still exists */
866877
mutex_lock(&event_mutex);
867878
list_for_each_entry(system, &event_subsystems, list) {
@@ -880,8 +891,9 @@ static int subsystem_open(struct inode *inode, struct file *filp)
880891
if (system != inode->i_private)
881892
return -ENODEV;
882893

894+
skip_search:
883895
ret = tracing_open_generic(inode, filp);
884-
if (ret < 0)
896+
if (ret < 0 && system)
885897
put_system(system);
886898

887899
return ret;
@@ -891,7 +903,8 @@ static int subsystem_release(struct inode *inode, struct file *file)
891903
{
892904
struct event_subsystem *system = inode->i_private;
893905

894-
put_system(system);
906+
if (system)
907+
put_system(system);
895908

896909
return 0;
897910
}
@@ -1041,10 +1054,11 @@ static const struct file_operations ftrace_subsystem_filter_fops = {
10411054
};
10421055

10431056
static const struct file_operations ftrace_system_enable_fops = {
1044-
.open = tracing_open_generic,
1057+
.open = subsystem_open,
10451058
.read = system_enable_read,
10461059
.write = system_enable_write,
10471060
.llseek = default_llseek,
1061+
.release = subsystem_release,
10481062
};
10491063

10501064
static const struct file_operations ftrace_show_header_fops = {
@@ -1133,8 +1147,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
11331147
"'%s/filter' entry\n", name);
11341148
}
11351149

1136-
trace_create_file("enable", 0644, system->entry,
1137-
(void *)system->name,
1150+
trace_create_file("enable", 0644, system->entry, system,
11381151
&ftrace_system_enable_fops);
11391152

11401153
return system->entry;

0 commit comments

Comments
 (0)