History

eventfs/tracing: Add callback for release of an eventfs_inode

Synthetic events create and destroy tracefs files when they are created and removed. The tracing subsystem has its own file descriptor representing the state of the events attached to the tracefs files. There's a race between the eventfs files and this file descriptor of the tracing system where the following can cause an issue: With two scripts 'A' and 'B' doing: Script 'A': echo "hello int aaa" > /sys/kernel/tracing/synthetic_events while : do echo 0 > /sys/kernel/tracing/events/synthetic/hello/enable done Script 'B': echo > /sys/kernel/tracing/synthetic_events Script 'A' creates a synthetic event "hello" and then just writes zero into its enable file. Script 'B' removes all synthetic events (including the newly created "hello" event). What happens is that the opening of the "enable" file has: { struct trace_event_file *file = inode->i_private; int ret; ret = tracing_check_open_get_tr(file->tr); [..] But deleting the events frees the "file" descriptor, and a "use after free" happens with the dereference at "file->tr". The file descriptor does have a reference counter, but there needs to be a way to decrement it from the eventfs when the eventfs_inode is removed that represents this file descriptor. Add an optional "release" callback to the eventfs_entry array structure, that gets called when the eventfs file is about to be removed. This allows for the creating on the eventfs file to increment the tracing file descriptor ref counter. When the eventfs file is deleted, it can call the release function that will call the put function for the tracing file descriptor. This will protect the tracing file from being freed while a eventfs file that references it is being opened. Link: https://lore.kernel.org/linux-trace-kernel/20240426073410.17154-1-Tze-nan.Wu@mediatek.com/ Link: https://lore.kernel.org/linux-trace-kernel/20240502090315.448cba46@gandalf.local.home

Steven Rostedt (Google) committed 2 years ago

Commit b63db581 parent e67572c3 files · +36 −2Browse files

Showing the changes to include/linux (1 of 3 files). Show all

Unified · Split

include/linux/tracefs.h+3−0
@@ -62,6 +62,8 @@ struct eventfs_file; · 61 unmodified lines
62typedef int (*eventfs_callback)(const char *name, umode_t *mode, void **data,62typedef int (*eventfs_callback)(const char *name, umode_t *mode, void **data,
63 const struct file_operations **fops);63 const struct file_operations **fops);
64 64
65typedef void (*eventfs_release)(const char *name, void *data);
66
65/**67/**
66 * struct eventfs_entry - dynamically created eventfs file call back handler68 * struct eventfs_entry - dynamically created eventfs file call back handler
67 * @name: Then name of the dynamic file in an eventfs directory69 * @name: Then name of the dynamic file in an eventfs directory
@@ -72,6 +74,7 @@ typedef int (*eventfs_callback)(const char *name, umode_t *mode, void **data, · 4 unmodified lines
72struct eventfs_entry {74struct eventfs_entry {
73 const char *name;75 const char *name;
74 eventfs_callback callback;76 eventfs_callback callback;
77 eventfs_release release;
75};78};
76 79
77struct eventfs_inode;80struct eventfs_inode;