tracing: Fix how the tracer options file ups the trace_array ref count
Subsystems (2)
cover tracing: Fix how the tracer options file ups the trace_array ref count
Sashiko reported that the tracer option files pass a descriptor to the file system operations that points to the trace_array it is apart of as well as information on the tracer option. It uses the trace_array to up its reference counter so that it can not be deleted when the option is open. The issue is that this descriptor sent to the open call itself can be removed by the removal of the instance the trace_array represents. There's nothing protecting it for going away and dereferencing it after it has been freed.
Another issue is that the tracer options are an array on an array for each tracer in the trace_array descriptor. The first patch converts that to a single array. This is need for the second patch.
The second patch changes the open function to search all the existing trace_arrays under lock and compare their topts array with the topt passed to the open function. If the topt is within the range of a trace_array topts array then that trace_array is considered a match and its reference is updated.
Steven Rostedt (2):
tracing: Use a single array to represent tracer options files
tracing: Take trace_array reference when opening a tracer options file
----
kernel/trace/trace.c | 81 +++++++++++++++++++++++++++++++++------------------- kernel/trace/trace.h | 21 ++++++-------- 2 files changed, 60 insertions(+), 42 deletions(-)
PATCH 1/2 tracing: Use a single array to represent tracer options files
From: Steven Rostedt <[email protected]>
An instance is represented by a trace_array descriptor that has an array for options of each tracer. That array is a pointer to another array that represents every option for a specific tracer. Instead of having two arrays use just a single array for every tracer option within a trace_array.
This not only simplifies the logic for representing the tracer option in the callbacks, but also can be used to simplify a way to find the trace_array descriptor from this one single array when needing to find which trace_array the option is attached to in order to take its reference.
--- kernel/trace/trace.c | 47 ++++++++++++++++++-------------------------- kernel/trace/trace.h | 21 ++++++++------------ 2 files changed, 27 insertions(+), 41 deletions(-)
kernel/trace/trace.c +19 -28
@@ -7689,7 +7689,7 @@ static ssize_t trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) { - struct trace_option_dentry *topt = filp->private_data; + struct trace_options *topt = filp->private_data;char *buf;if (topt->flags->val & topt->opt->bit)
@@ -7704,7 +7704,7 @@ static ssize_t trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos) { - struct trace_option_dentry *topt = filp->private_data; + struct trace_options *topt = filp->private_data;unsigned long val;int ret;
@@ -7730,7 +7730,7 @@ trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,static int tracing_open_options (struct inode *inode,struct file *filp) { -struct trace_option_dentry *topt = inode->i_private; +struct trace_options *topt = inode->i_private;int ret; ret =tracing_check_open_get_tr (topt->tr);
@@ -7743,7 +7743,7 @@ static int tracing_open_options(struct inode *inode, struct file *filp)static int tracing_release_options (struct inode *inode,struct file *file) { -struct trace_option_dentry *topt = file->private_data; +struct trace_options *topt = file->private_data;trace_array_put (topt->tr);return 0 ;
@@ -7946,8 +7946,9 @@ static struct dentry *trace_options_init_dentry(struct trace_array *tr)static void create_trace_option_file (struct trace_array *tr, -struct trace_option_dentry *topt, +struct trace_options *topt,struct tracer_flags *flags, +struct tracer *tracer,struct tracer_opt *opt) {struct dentry *t_options;
@@ -7959,6 +7960,7 @@ create_trace_option_file(struct trace_array *tr, topt->flags = flags; topt->opt = opt; topt->tr = tr; + topt->tracer = tracer; topt->entry =trace_create_file (opt->name,TRACE_MODE_WRITE , t_options, topt, &trace_options_fops);
@@ -7968,10 +7970,9 @@ static int create_trace_option_files(struct trace_array *tr, struct tracer *tracer, struct tracer_flags *flags) { - struct trace_option_dentry *topts; -struct trace_options *tr_topts; + struct trace_options *topts;struct tracer_opt *opts; -int cnt; +int i, cnt;if (!flags || !flags->opts)return 0 ;
@@ -7981,29 +7982,22 @@ create_trace_option_files(struct trace_array *tr, struct tracer *tracer,for (cnt =0 ; opts[cnt].name; cnt++) ; - topts =kzalloc_objs (*topts, cnt +1 ); -if (!topts) -return 0 ; - - tr_topts =krealloc_array (tr->topts, tr->nr_topts +1 ,sizeof (*tr->topts), + topts =krealloc_array (tr->topts, tr->nr_topts + cnt,sizeof (*tr->topts),GFP_KERNEL ); -if (!tr_topts) { -kfree (topts); +if (!topts)return -ENOMEM ; - } - tr->topts = tr_topts; - tr->topts[tr->nr_topts].tracer = tracer; - tr->topts[tr->nr_topts].topts = topts; - tr->nr_topts++; + tr->topts = topts; -for (cnt =0 ; opts[cnt].name; cnt++) { -create_trace_option_file (tr, &topts[cnt], flags, - &opts[cnt]); -MEM_FAIL (topts[cnt].entry ==NULL , +for (topts += tr->nr_topts, i =0 ; i < cnt; topts++, i++) { +create_trace_option_file (tr, topts, flags, tracer, + &opts[i]); +MEM_FAIL (topts->entry ==NULL ,"Failed to create trace option: %s" , - opts[cnt].name); + opts[i].name); } + + tr->nr_topts += cnt;return 0 ; }
@@ -8865,9 +8859,6 @@ static int __remove_instance(struct trace_array *tr)if (tr->flags &TRACE_ARRAY_FL_VMALLOC )vfree ((void *)tr->range_addr_start); -for (i =0 ; i < tr->nr_topts; i++) { -kfree (tr->topts[i].topts); - }kfree (tr->topts);free_cpumask_var (tr->pipe_cpumask);
kernel/trace/trace.h +8 -13
@@ -224,11 +224,6 @@ struct array_buffer {#define TRACE_FLAGS_MAX_SIZE 64 -struct trace_options { -struct tracer *tracer; -struct trace_option_dentry *topts; -}; -struct trace_pid_list *trace_pid_list_alloc (void );void trace_pid_list_free (struct trace_pid_list *pid_list);bool trace_pid_list_is_set (struct trace_pid_list *pid_list,unsigned int pid);
@@ -325,6 +320,14 @@ struct trace_module_delta {long delta[]; }; +struct trace_options { +struct tracer_opt *opt; +struct tracer_flags *flags; +struct trace_array *tr; +struct tracer *tracer; +struct dentry *entry; +}; +/* * The trace array - an array of per-CPU trace arrays. This is the * highest level data structure that individual tracers deal with.
@@ -605,14 +608,6 @@ struct tracer_flags {/* Makes more easy to define a tracer opt */ #define TRACER_OPT (s, b) .name = #s, .bit = b - -struct trace_option_dentry { -struct tracer_opt *opt; -struct tracer_flags *flags; -struct trace_array *tr; -struct dentry *entry; -}; -/** * struct tracer - a specific tracer and its callbacks to interact with tracefs * @name: the name chosen to select it on the available_tracers file
Signature · 1 line
2.53.0
PATCH 2/2 tracing: Take trace_array reference when opening a tracer options file
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
When a tracer option file is opened, it is passed a descriptor that points to an element on the trace_array's topts array. This element has information to find the trace array and other information. It uses this element to take a reference of the trace_array so that the trace_array does not get removed while this file is opened.
Unfortunately, there's a race condition where the element itself could be freed by the removal of the instance the trace_array represents causing a use-after-free as this element that is used to find the trace_array to increment its reference counter is also freed when the instance is removed.
To solve this, add a trace_array_tracer_options_get() helper function that will take the address of the element that is passed to the open function by the inode->i_private pointer and search all the trace_arrays under a lock to find the one that the element's address is in the range of the trace_arrays topts array. When a match happens, that trace_array's reference would be increased.
Note, there's a race where if an admin was deleting and creating trace instances at the same time and the memory of the old trace_array's array matched the memory of the new trace_array that it could in theory open the option from the wrong trace array. But we do not care because it would be stupid to perform that kind of action. As long as the only thing that can happen is that the option from the wrong trace array is used and doesn't crash the kernel it will only make the user confused. But if they are doing something stupid like this, they are already confused, so no harm done.
--- kernel/trace/trace.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
kernel/trace/trace.c +33 -1
@@ -7728,12 +7728,44 @@ trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,return cnt; } +/* +* The topt is the address of a trace_array->topts[] element that holds the +* the tracer options descriptor. But since the trace_array reference has not +* been taken yet, it cannot be dereferenced as it could have been freed by +* a rmdir of the instance the trace_array represents. +* +* Search the list of trace_arrays and compare the topt to the address of +* the entire trace_array topts array for each trace_array in the list. +* If one is matched, then take the reference and return it. If not, the +* trace_array no longer exits. +*/ +static int trace_array_tracer_options_get (void *topt) +{ +struct trace_array *tr; +int ret; + + ret =security_locked_down (LOCKDOWN_TRACEFS ); +if (ret) +return ret; + +if (tracing_disabled) +return -ENODEV ; + +guard (mutex)(&trace_types_lock); +list_for_each_entry (tr, &ftrace_trace_arrays, list) { +if (topt >= (void *)&tr->topts[0 ] && + topt < (void *)&tr->topts[tr->nr_topts]) +return __trace_array_get (tr); + } +return -ENODEV ; +} +static int tracing_open_options (struct inode *inode,struct file *filp) {struct trace_options *topt = inode->i_private;int ret; - ret =tracing_check_open_get_tr (topt->tr); + ret =trace_array_tracer_options_get (topt);if (ret)return ret;
Signature · 1 line
2.53.0