linux-stable/include
Steven Rostedt (Google) 2c9de867ca tracing: Have trace_event_file have ref counters
commit bb32500fb9 upstream.

The following can crash the kernel:

 # cd /sys/kernel/tracing
 # echo 'p:sched schedule' > kprobe_events
 # exec 5>>events/kprobes/sched/enable
 # > kprobe_events
 # exec 5>&-

The above commands:

 1. Change directory to the tracefs directory
 2. Create a kprobe event (doesn't matter what one)
 3. Open bash file descriptor 5 on the enable file of the kprobe event
 4. Delete the kprobe event (removes the files too)
 5. Close the bash file descriptor 5

The above causes a crash!

 BUG: kernel NULL pointer dereference, address: 0000000000000028
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 0 P4D 0
 Oops: 0000 [#1] PREEMPT SMP PTI
 CPU: 6 PID: 877 Comm: bash Not tainted 6.5.0-rc4-test-00008-g2c6b6b1029d4-dirty #186
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
 RIP: 0010:tracing_release_file_tr+0xc/0x50

What happens here is that the kprobe event creates a trace_event_file
"file" descriptor that represents the file in tracefs to the event. It
maintains state of the event (is it enabled for the given instance?).
Opening the "enable" file gets a reference to the event "file" descriptor
via the open file descriptor. When the kprobe event is deleted, the file is
also deleted from the tracefs system which also frees the event "file"
descriptor.

But as the tracefs file is still opened by user space, it will not be
totally removed until the final dput() is called on it. But this is not
true with the event "file" descriptor that is already freed. If the user
does a write to or simply closes the file descriptor it will reference the
event "file" descriptor that was just freed, causing a use-after-free bug.

To solve this, add a ref count to the event "file" descriptor as well as a
new flag called "FREED". The "file" will not be freed until the last
reference is released. But the FREE flag will be set when the event is
removed to prevent any more modifications to that event from happening,
even if there's still a reference to the event "file" descriptor.

Link: https://lore.kernel.org/linux-trace-kernel/20231031000031.1e705592@gandalf.local.home/
Link: https://lore.kernel.org/linux-trace-kernel/20231031122453.7a48b923@gandalf.local.home

Cc: stable@vger.kernel.org
Cc: Mark Rutland <mark.rutland@arm.com>
Fixes: f5ca233e2e ("tracing: Increase trace array ref count on enable and filter files")
Reported-by: Beau Belgrave <beaub@linux.microsoft.com>
Tested-by: Beau Belgrave <beaub@linux.microsoft.com>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-11-28 17:15:19 +00:00
..
acpi ACPI: APEI: Fix AER info corruption when error status data has multiple sections 2023-11-28 17:14:42 +00:00
asm-generic hyperv-fixes for 6.5-rc5 2023-08-04 17:16:14 -07:00
clocksource
crypto crypto: api - Use work queue in crypto_destroy_instance 2023-09-13 09:53:05 +02:00
drm drm: bridge: samsung-dsim: Fix waiting for empty cmd transfer FIFO on older Exynos 2023-11-20 11:56:58 +01:00
dt-bindings dt-bindings: clock: qcom,gcc-sc8280xp: Add missing GDSCs 2023-09-13 09:53:27 +02:00
keys
kunit
kvm KVM: arm64: vgic-v4: Make the doorbell request robust w.r.t preemption 2023-07-13 22:23:34 +00:00
linux tracing: Have trace_event_file have ref counters 2023-11-28 17:15:19 +00:00
math-emu
media media: cec: core: add adap_unconfigured() callback 2023-09-13 09:53:40 +02:00
memory
misc
net net: sched: do not offload flows with a helper in act_ct 2023-11-28 17:14:57 +00:00
pcmcia
ras
rdma
rv
scsi scsi: sd: Introduce manage_shutdown device flag 2023-11-02 09:36:55 +01:00
soc firmware: tegra: Add suspend hook and reset BPMP IPC early on resume 2023-11-20 11:57:06 +01:00
sound ASoC: soc-dai: add flag to mute and unmute stream during trigger 2023-11-28 17:15:06 +00:00
target
trace btrfs: remove v0 extent handling 2023-11-02 09:36:50 +01:00
uapi mm: make PR_MDWE_REFUSE_EXEC_GAIN an unsigned long 2023-11-28 17:15:08 +00:00
ufs scsi: ufs: Fix residual handling 2023-09-13 09:53:36 +02:00
vdso
video fbdev: stifb: Make the STI next font pointer a 32-bit signed offset 2023-11-28 17:15:01 +00:00
xen xen/evtchn: Introduce new IOCTL to bind static evtchn 2023-07-26 08:42:34 +02:00