• Steven Rostedt (Google)'s avatar
    eventfs/tracing: Add callback for release of an eventfs_inode · b63db58e
    Steven Rostedt (Google) authored
    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
    
    Cc: stable@vger.kernel.org
    Cc: Masami Hiramatsu <mhiramat@kernel.org>
    Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
    Fixes: 5790b1fb ("eventfs: Remove eventfs_file and just use eventfs_inode")
    Reported-by: default avatarTze-nan wu <Tze-nan.Wu@mediatek.com>
    Tested-by: default avatarTze-nan Wu (吳澤南) <Tze-nan.Wu@mediatek.com>
    Signed-off-by: default avatarSteven Rostedt (Google) <rostedt@goodmis.org>
    b63db58e
trace_events.c 101 KB