From nobody Thu Sep 19 01:23:53 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1809219E7D1; Tue, 30 Jul 2024 15:06:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722351988; cv=none; b=q91YG6+t5zB5cK+TlH9eg5YiCwV1Oygm5aIHLrIwKlkUx/vK8oJBxaWmn5KJ9ArN6Ceiag6JSJesP+vEbT5FqVdvjIdNJPlSaWmsFDzGAafeZB/xyEcJYB3G/eZlgQseupn09+QiC4vPvYh/0CaKvZOL9PW+6XFc1FYCDs+W4MQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722351988; c=relaxed/simple; bh=RN7PLKyOec9Ie9QnazVGvkDHyZY3E6LKutJuncjDfyM=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=Ff2omGe4i1bA3SDua/IzObWerBiBK2Ec85Da9tRVPUWcJYBdTAbAG9ziSpaqmbOZ7edyb67QGBK9uTL6mI1/VNt3OPzKJM5bkcTeIQVeQv3xIG91MZwtHwYgbimUbdM5vSGCi+YHDOIlX65mHVccT4uhsvXFRPt64SCEb6b1+SA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C83B3C4AF0A; Tue, 30 Jul 2024 15:06:25 +0000 (UTC) Date: Tue, 30 Jul 2024 11:06:57 -0400 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mathieu Desnoyers , Ajay Kaher , Mathias Krause , Ilkka =?UTF-8?B?TmF1bGFww6TDpA==?= , Linus Torvalds , Al Viro , regressions@leemhuis.info, Dan Carpenter , Beau Belgrave , Florian Fainelli , Alexey Makhalov , Vasavi Sirnapalli Subject: [PATCH v3] tracing: Have format file honor EVENT_FILE_FL_FREED Message-ID: <20240730110657.3b69d3c1@gandalf.local.home> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt When eventfs was introduced, special care had to be done to coordinate the freeing of the file meta data with the files that are exposed to user space. The file meta data would have a ref count that is set when the file is created and would be decremented and freed after the last user that opened the file closed it. When the file meta data was to be freed, it would set a flag (EVENT_FILE_FL_FREED) to denote that the file is freed, and any new references made (like new opens or reads) would fail as it is marked freed. This allowed other meta data to be freed after this flag was set (under the event_mutex). All the files that were dynamically created in the events directory had a pointer to the file meta data and would call event_release() when the last reference to the user space file was closed. This would be the time that it is safe to free the file meta data. A shortcut was made for the "format" file. It's i_private would point to the "call" entry directly and not point to the file's meta data. This is because all format files are the same for the same "call", so it was thought there was no reason to differentiate them. The other files maintain state (like the "enable", "trigger", etc). But this meant if the file were to disappear, the "format" file would be unaware of it. This caused a race that could be trigger via the user_events test (that would create dynamic events and free them), and running a loop that would read the user_events format files: In one console run: # cd tools/testing/selftests/user_events # while true; do ./ftrace_test; done And in another console run: # cd /sys/kernel/tracing/ # while true; do cat events/user_events/__test_event/format; done 2>/dev/n= ull With KASAN memory checking, it would trigger a use-after-free bug report (which was a real bug). This was because the format file was not checking the file's meta data flag "EVENT_FILE_FL_FREED", so it would access the event that the file meta data pointed to after the event was freed. After inspection, there are other locations that were found to not check the EVENT_FILE_FL_FREED flag when accessing the trace_event_file. Add a new helper function: event_file_file() that will make sure that the event_mutex is held, and will return NULL if the trace_event_file has the EVENT_FILE_FL_FREED flag set. Have the first reference of the struct file pointer use event_file_file() and check for NULL. Later uses can still use the event_file_data() helper function if the event_mutex is still held and was not released since the event_file_file() call. Link: https://lore.kernel.org/all/20240719204701.1605950-1-minipli@grsecuri= ty.net/ Cc: stable@vger.kernel.org Fixes: b63db58e2fa5d ("eventfs/tracing: Add callback for release of an even= tfs_inode") Reported-by: Mathias Krause Tested-by: Mathias Krause Signed-off-by: Steven Rostedt (Google) --- Changes since v3: https://lore.kernel.org/20240726132811.306a449e@rorschach= .local.home - After inspecting the code, there's several users of event_file_data() that return the trace_event_file descriptor when it could be set to be freed. Add a new helper function: event_file_file() that will make sure the event_mutex is held and check the flag and return NULL if the file is freed. kernel/trace/trace.h | 23 ++++++++++++++++++++ kernel/trace/trace_events.c | 33 +++++++++++++++++------------ kernel/trace/trace_events_hist.c | 4 ++-- kernel/trace/trace_events_inject.c | 2 +- kernel/trace/trace_events_trigger.c | 6 +++--- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 749a182dab48..8edab43580d5 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1573,6 +1573,29 @@ static inline void *event_file_data(struct file *fil= p) extern struct mutex event_mutex; extern struct list_head ftrace_events; =20 +/* + * When the trace_event_file is the filp->i_private pointer, + * it must be taken under the event_mutex lock, and then checked + * if the EVENT_FILE_FL_FREED flag is set. If it is, then the + * data pointed to by the trace_event_file can not be trusted. + * + * Use the event_file_file() to access the trace_event_file from + * the filp the first time under the event_mutex and check for + * NULL. If it is needed to be retrieved again and the event_mutex + * is still held, then the event_file_data() can be used and it + * is guaranteed to be valid. + */ +static inline struct trace_event_file *event_file_file(struct file *filp) +{ + struct trace_event_file *file; + + lockdep_assert_held(&event_mutex); + file =3D READ_ONCE(file_inode(filp)->i_private); + if (!file || file->flags & EVENT_FILE_FL_FREED) + return NULL; + return file; +} + extern const struct file_operations event_trigger_fops; extern const struct file_operations event_hist_fops; extern const struct file_operations event_hist_debug_fops; diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index 6ef29eba90ce..f08fbaf8cad6 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -1386,12 +1386,12 @@ event_enable_read(struct file *filp, char __user *u= buf, size_t cnt, char buf[4] =3D "0"; =20 mutex_lock(&event_mutex); - file =3D event_file_data(filp); + file =3D event_file_file(filp); if (likely(file)) flags =3D file->flags; mutex_unlock(&event_mutex); =20 - if (!file || flags & EVENT_FILE_FL_FREED) + if (!file) return -ENODEV; =20 if (flags & EVENT_FILE_FL_ENABLED && @@ -1424,8 +1424,8 @@ event_enable_write(struct file *filp, const char __us= er *ubuf, size_t cnt, case 1: ret =3D -ENODEV; mutex_lock(&event_mutex); - file =3D event_file_data(filp); - if (likely(file && !(file->flags & EVENT_FILE_FL_FREED))) { + file =3D event_file_file(filp); + if (likely(file)) { ret =3D tracing_update_buffers(file->tr); if (ret < 0) { mutex_unlock(&event_mutex); @@ -1540,7 +1540,8 @@ enum { =20 static void *f_next(struct seq_file *m, void *v, loff_t *pos) { - struct trace_event_call *call =3D event_file_data(m->private); + struct trace_event_file *file =3D event_file_data(m->private); + struct trace_event_call *call =3D file->event_call; struct list_head *common_head =3D &ftrace_common_fields; struct list_head *head =3D trace_get_fields(call); struct list_head *node =3D v; @@ -1572,7 +1573,8 @@ static void *f_next(struct seq_file *m, void *v, loff= _t *pos) =20 static int f_show(struct seq_file *m, void *v) { - struct trace_event_call *call =3D event_file_data(m->private); + struct trace_event_file *file =3D event_file_data(m->private); + struct trace_event_call *call =3D file->event_call; struct ftrace_event_field *field; const char *array_descriptor; =20 @@ -1627,12 +1629,14 @@ static int f_show(struct seq_file *m, void *v) =20 static void *f_start(struct seq_file *m, loff_t *pos) { + struct trace_event_file *file; void *p =3D (void *)FORMAT_HEADER; loff_t l =3D 0; =20 /* ->stop() is called even if ->start() fails */ mutex_lock(&event_mutex); - if (!event_file_data(m->private)) + file =3D event_file_file(m->private); + if (!file) return ERR_PTR(-ENODEV); =20 while (l < *pos && p) @@ -1706,8 +1710,8 @@ event_filter_read(struct file *filp, char __user *ubu= f, size_t cnt, trace_seq_init(s); =20 mutex_lock(&event_mutex); - file =3D event_file_data(filp); - if (file && !(file->flags & EVENT_FILE_FL_FREED)) + file =3D event_file_file(filp); + if (file) print_event_filter(file, s); mutex_unlock(&event_mutex); =20 @@ -1736,9 +1740,13 @@ event_filter_write(struct file *filp, const char __u= ser *ubuf, size_t cnt, return PTR_ERR(buf); =20 mutex_lock(&event_mutex); - file =3D event_file_data(filp); - if (file) - err =3D apply_event_filter(file, buf); + file =3D event_file_file(filp); + if (file) { + if (file->flags & EVENT_FILE_FL_FREED) + err =3D -ENODEV; + else + err =3D apply_event_filter(file, buf); + } mutex_unlock(&event_mutex); =20 kfree(buf); @@ -2485,7 +2493,6 @@ static int event_callback(const char *name, umode_t *= mode, void **data, if (strcmp(name, "format") =3D=3D 0) { *mode =3D TRACE_MODE_READ; *fops =3D &ftrace_event_format_fops; - *data =3D call; return 1; } =20 diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_h= ist.c index 6ece1308d36a..5f9119eb7c67 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -5601,7 +5601,7 @@ static int hist_show(struct seq_file *m, void *v) =20 mutex_lock(&event_mutex); =20 - event_file =3D event_file_data(m->private); + event_file =3D event_file_file(m->private); if (unlikely(!event_file)) { ret =3D -ENODEV; goto out_unlock; @@ -5880,7 +5880,7 @@ static int hist_debug_show(struct seq_file *m, void *= v) =20 mutex_lock(&event_mutex); =20 - event_file =3D event_file_data(m->private); + event_file =3D event_file_file(m->private); if (unlikely(!event_file)) { ret =3D -ENODEV; goto out_unlock; diff --git a/kernel/trace/trace_events_inject.c b/kernel/trace/trace_events= _inject.c index 8650562bdaa9..a8f076809db4 100644 --- a/kernel/trace/trace_events_inject.c +++ b/kernel/trace/trace_events_inject.c @@ -299,7 +299,7 @@ event_inject_write(struct file *filp, const char __user= *ubuf, size_t cnt, strim(buf); =20 mutex_lock(&event_mutex); - file =3D event_file_data(filp); + file =3D event_file_file(filp); if (file) { call =3D file->event_call; size =3D parse_entry(buf, call, &entry); diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_event= s_trigger.c index 4bec043c8690..a5e3d6acf1e1 100644 --- a/kernel/trace/trace_events_trigger.c +++ b/kernel/trace/trace_events_trigger.c @@ -159,7 +159,7 @@ static void *trigger_start(struct seq_file *m, loff_t *= pos) =20 /* ->stop() is called even if ->start() fails */ mutex_lock(&event_mutex); - event_file =3D event_file_data(m->private); + event_file =3D event_file_file(m->private); if (unlikely(!event_file)) return ERR_PTR(-ENODEV); =20 @@ -213,7 +213,7 @@ static int event_trigger_regex_open(struct inode *inode= , struct file *file) =20 mutex_lock(&event_mutex); =20 - if (unlikely(!event_file_data(file))) { + if (unlikely(!event_file_file(file))) { mutex_unlock(&event_mutex); return -ENODEV; } @@ -293,7 +293,7 @@ static ssize_t event_trigger_regex_write(struct file *f= ile, strim(buf); =20 mutex_lock(&event_mutex); - event_file =3D event_file_data(file); + event_file =3D event_file_file(file); if (unlikely(!event_file)) { mutex_unlock(&event_mutex); kfree(buf); --=20 2.43.0