Skip to contentSkip to conversation

tracing: Fixes for v6.16

PATCH In review Steven Rostedt 3 participants 9 messages Jun 3, 2025 – Jun 5, 2025

Subsystems (8)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover tracing: Fixes for v6.16

tracing fixes:

- Fix UAF in module unload in ftrace when there's a bug in the module

If a module is buggy and triggers ftrace_disable which is set when an anomaly is detected, when it gets unloaded it doesn't free the hooks into kallsyms, and when a kallsyms lookup is performed it may access the mod->modname field and crash via UAF.

Fix this by still freeing the mod_maps that are attached to kallsyms on module unload regardless if ftrace_disable is set or not.

- Do not bother allocating mod_maps for kallsyms if ftrace_disable is set

- Remove unused trace events

When a trace event or tracepoint is created but not used, it still creates the code and data structures needed for that trace event. This just wastes memory.

A patch is being worked on to warn when a trace event is created but
not used: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Remove the trace events that are created but not used. This does not remove trace events that are created but are not used due configs not being set. That will be handled later.

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace/fixes

Head SHA1: 2adb87c9409e11e8bc6c56f8e1e253eb25a7d6ae

Steven Rostedt (3):
xdp: Remove unused mem_return_failed event genirq/matrix: Remove unused irq_matrix_alloc_reserved tracepoint
fsdax: Remove unused trace events for dax insert mapping

Ye Bin (2):
ftrace: Fix UAF when lookup kallsym after ftrace disabled
ftrace: Don't allocate ftrace module map if ftrace is disabled

----

 include/trace/events/fs_dax.h     | 78 ---------------------------------------
 include/trace/events/irq_matrix.h |  8 ----
 include/trace/events/xdp.h        | 26 -------------
 kernel/trace/ftrace.c             | 13 +++++--
 4 files changed, 10 insertions(+), 115 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 ftrace: Fix UAF when lookup kallsym after ftrace disabled

From: Ye Bin <[email protected]>

The following issue happens with a buggy module:

BUG: unable to handle page fault for address: ffffffffc05d0218
PGD 1bd66f067 P4D 1bd66f067 PUD 1bd671067 PMD 101808067 PTE 0
Oops: Oops: 0000 [#1] SMP KASAN PTI
Tainted: [O]=OOT_MODULE, [E]=UNSIGNED_MODULE
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
RIP: 0010:sized_strscpy+0x81/0x2f0
RSP: 0018:ffff88812d76fa08 EFLAGS: 00010246
RAX: 0000000000000000 RBX: ffffffffc0601010 RCX: dffffc0000000000
RDX: 0000000000000038 RSI: dffffc0000000000 RDI: ffff88812608da2d
RBP: 8080808080808080 R08: ffff88812608da2d R09: ffff88812608da68
R10: ffff88812608d82d R11: ffff88812608d810 R12: 0000000000000038
R13: ffff88812608da2d R14: ffffffffc05d0218 R15: fefefefefefefeff
FS:  00007fef552de740(0000) GS:ffff8884251c7000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffffffffc05d0218 CR3: 00000001146f0000 CR4: 00000000000006f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 <TASK>
 ftrace_mod_get_kallsym+0x1ac/0x590
 update_iter_mod+0x239/0x5b0
 s_next+0x5b/0xa0
 seq_read_iter+0x8c9/0x1070
 seq_read+0x249/0x3b0
 proc_reg_read+0x1b0/0x280
 vfs_read+0x17f/0x920
 ksys_read+0xf3/0x1c0
 do_syscall_64+0x5f/0x2e0
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

The above issue may happen as follows:
(1) Add kprobe tracepoint;
(2) insmod test.ko;
(3) Module triggers ftrace disabled;
(4) rmmod test.ko;
(5) cat /proc/kallsyms; --> Will trigger UAF as test.ko already removed; ftrace_mod_get_kallsym()
...
strscpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
...

The problem is when a module triggers an issue with ftrace and sets ftrace_disable. The ftrace_disable is set when an anomaly is discovered and to prevent any more damage, ftrace stops all text modification. The issue that happened was that the ftrace_disable stops more than just the text modification.

When a module is loaded, its init functions can also be traced. Because kallsyms deletes the init functions after a module has loaded, ftrace saves them when the module is loaded and function tracing is enabled. This allows the output of the function trace to show the init function names instead of just their raw memory addresses.

When a module is removed, ftrace_release_mod() is called, and if ftrace_disable is set, it just returns without doing anything more. The problem here is that it leaves the mod_list still around and if kallsyms is called, it will call into this code and access the module memory that has already been freed as it will return:

  strscpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);

Where the "mod" no longer exists and triggers a UAF bug.

Fixes aba4b5c22cba ("ftrace: Save module init functions kallsyms symbols for tracing")
---
 kernel/trace/ftrace.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/trace/ftrace.c +7 -3
index 1af952cba48d..84fd2f8263fa 100644
@@ -7438,9 +7438,10 @@ void ftrace_release_mod(struct module *mod)
 
 	mutex_lock(&ftrace_lock);
 
-	if (ftrace_disabled)
-		goto out_unlock;
-
+	/*
+	 * To avoid the UAF problem after the module is unloaded, the
+	 * 'mod_map' resource needs to be released unconditionally.
+	 */
 	list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
 		if (mod_map->mod == mod) {
 			list_del_rcu(&mod_map->list);
@@ -7449,6 +7450,9 @@ void ftrace_release_mod(struct module *mod)
 		}
 	}
 
+	if (ftrace_disabled)
+		goto out_unlock;
+
 	/*
 	 * Each module has its own ftrace_pages, remove
 	 * them from the list.
Signature · 1 line
2.47.2
author maintainer
#2 lore raw

PATCH 2/5 ftrace: Dont allocate ftrace module map if ftrace is disabled

In reply to Steven Rostedt

From: Ye Bin <[email protected]>

If ftrace is disabled, it is meaningless to allocate a module map. Add a check in allocate_ftrace_mod_map() to not allocate if ftrace is disabled.

---
 kernel/trace/ftrace.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/ftrace.c +3 -0
index 84fd2f8263fa..a7291685902e 100644
@@ -7631,6 +7631,9 @@ allocate_ftrace_mod_map(struct module *mod,
 {
 	struct ftrace_mod_map *mod_map;
 
+	if (ftrace_disabled)
+		return NULL;
+
 	mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
 	if (!mod_map)
 		return NULL;
Signature · 1 line
2.47.2
author maintainer
#3 lore raw

PATCH 3/5 xdp: Remove unused mem_return_failed event

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

The change to allow page_pool to handle its own page destruction instead of relying on XDP removed the trace_mem_return_failed() tracepoint caller, but did not remove the mem_return_failed trace event. As trace events take up memory when they are created regardless of if they are used or not, having this unused event around wastes around 5K of memory.

Remove the unused event.

Cc netdev <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Fixes c3f812cea0d7 ("page_pool: do not release pool until inflight == 0.")
---
 include/trace/events/xdp.h | 26 --------------------------
 1 file changed, 26 deletions(-)
include/trace/events/xdp.h +0 -26
index a7e5452b5d21..d3ef86c97ae3 100644
@@ -379,32 +379,6 @@ TRACE_EVENT(mem_connect,
 	)
 );
 
-TRACE_EVENT(mem_return_failed,
-
-	TP_PROTO(const struct xdp_mem_info *mem,
-		 const struct page *page),
-
-	TP_ARGS(mem, page),
-
-	TP_STRUCT__entry(
-		__field(const struct page *,	page)
-		__field(u32,		mem_id)
-		__field(u32,		mem_type)
-	),
-
-	TP_fast_assign(
-		__entry->page		= page;
-		__entry->mem_id		= mem->id;
-		__entry->mem_type	= mem->type;
-	),
-
-	TP_printk("mem_id=%d mem_type=%s page=%p",
-		  __entry->mem_id,
-		  __print_symbolic(__entry->mem_type, __MEM_TYPE_SYM_TAB),
-		  __entry->page
-	)
-);
-
 TRACE_EVENT(bpf_xdp_link_attach_failed,
 
 	TP_PROTO(const char *msg),
Signature · 1 line
2.47.2
maintainer Reviewed-by on v1 3/5
#4 lore raw

Re: [for-linus][PATCH 3/5] xdp: Remove unused mem_return_failed event

Quoted 21 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

The change to allow page_pool to handle its own page destruction instead of relying on XDP removed the trace_mem_return_failed() tracepoint caller, but did not remove the mem_return_failed trace event. As trace events take up memory when they are created regardless of if they are used or not, having this unused event around wastes around 5K of memory.

Remove the unused event.

Cc netdev <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Fixes c3f812cea0d7 ("page_pool: do not release pool until inflight == 0.")
Quoted 3 lines
---
 include/trace/events/xdp.h | 26 --------------------------
 1 file changed, 26 deletions(-)
author maintainer
#5 lore raw

PATCH 4/5 genirq/matrix: Remove unused irq_matrix_alloc_reserved tracepoint

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

The tracepoint irq_matrix_alloc_reserved was added but never used. Remove it.

Cc Juergen Gross <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Fixes ec0f7cd273dc4 ("genirq/matrix: Add tracepoints")
---
 include/trace/events/irq_matrix.h | 8 --------
 1 file changed, 8 deletions(-)
include/trace/events/irq_matrix.h +0 -8
index 267d4cbbf360..93244078b4e6 100644
@@ -138,14 +138,6 @@ DEFINE_EVENT(irq_matrix_global_update, irq_matrix_assign_system,
 	TP_ARGS(bit, matrix)
 );
 
-DEFINE_EVENT(irq_matrix_cpu, irq_matrix_alloc_reserved,
-
-	TP_PROTO(int bit, unsigned int cpu,
-		 struct irq_matrix *matrix, struct cpumap *cmap),
-
-	TP_ARGS(bit, cpu, matrix, cmap)
-);
-
 DEFINE_EVENT(irq_matrix_cpu, irq_matrix_reserve_managed,
 
 	TP_PROTO(int bit, unsigned int cpu,
Signature · 1 line
2.47.2
author maintainer
#6 lore raw

PATCH 5/5 fsdax: Remove unused trace events for dax insert mapping

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

When the dax_fault_actor() helper was factored out, it removed the calls to the dax_pmd_insert_mapping and dax_insert_mapping events but never removed the events themselves. As each event created takes up memory (roughly 5K each), this is a waste as it is never used.

Remove the unused dax_pmd_insert_mapping and dax_insert_mapping trace events.

Cc Dan Williams <[email protected]>
Cc Shiyang Ruan <[email protected]>
Cc "Darrick J. Wong" <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Andrew Morton <[email protected]>
Fixes c2436190e492 ("fsdax: factor out a dax_fault_actor() helper")
---
 include/trace/events/fs_dax.h | 78 -----------------------------------
 1 file changed, 78 deletions(-)
include/trace/events/fs_dax.h +0 -78
index 86fe6aecff1e..76b56f78abb0 100644
@@ -102,54 +102,6 @@ DEFINE_EVENT(dax_pmd_load_hole_class, name, \
 DEFINE_PMD_LOAD_HOLE_EVENT(dax_pmd_load_hole);
 DEFINE_PMD_LOAD_HOLE_EVENT(dax_pmd_load_hole_fallback);
 
-DECLARE_EVENT_CLASS(dax_pmd_insert_mapping_class,
-	TP_PROTO(struct inode *inode, struct vm_fault *vmf,
-		long length, pfn_t pfn, void *radix_entry),
-	TP_ARGS(inode, vmf, length, pfn, radix_entry),
-	TP_STRUCT__entry(
-		__field(unsigned long, ino)
-		__field(unsigned long, vm_flags)
-		__field(unsigned long, address)
-		__field(long, length)
-		__field(u64, pfn_val)
-		__field(void *, radix_entry)
-		__field(dev_t, dev)
-		__field(int, write)
-	),
-	TP_fast_assign(
-		__entry->dev = inode->i_sb->s_dev;
-		__entry->ino = inode->i_ino;
-		__entry->vm_flags = vmf->vma->vm_flags;
-		__entry->address = vmf->address;
-		__entry->write = vmf->flags & FAULT_FLAG_WRITE;
-		__entry->length = length;
-		__entry->pfn_val = pfn.val;
-		__entry->radix_entry = radix_entry;
-	),
-	TP_printk("dev %d:%d ino %#lx %s %s address %#lx length %#lx "
-			"pfn %#llx %s radix_entry %#lx",
-		MAJOR(__entry->dev),
-		MINOR(__entry->dev),
-		__entry->ino,
-		__entry->vm_flags & VM_SHARED ? "shared" : "private",
-		__entry->write ? "write" : "read",
-		__entry->address,
-		__entry->length,
-		__entry->pfn_val & ~PFN_FLAGS_MASK,
-		__print_flags_u64(__entry->pfn_val & PFN_FLAGS_MASK, "|",
-			PFN_FLAGS_TRACE),
-		(unsigned long)__entry->radix_entry
-	)
-)
-
-#define DEFINE_PMD_INSERT_MAPPING_EVENT(name) \
-DEFINE_EVENT(dax_pmd_insert_mapping_class, name, \
-	TP_PROTO(struct inode *inode, struct vm_fault *vmf, \
-		long length, pfn_t pfn, void *radix_entry), \
-	TP_ARGS(inode, vmf, length, pfn, radix_entry))
-
-DEFINE_PMD_INSERT_MAPPING_EVENT(dax_pmd_insert_mapping);
-
 DECLARE_EVENT_CLASS(dax_pte_fault_class,
 	TP_PROTO(struct inode *inode, struct vm_fault *vmf, int result),
 	TP_ARGS(inode, vmf, result),
@@ -194,36 +146,6 @@ DEFINE_PTE_FAULT_EVENT(dax_load_hole);
 DEFINE_PTE_FAULT_EVENT(dax_insert_pfn_mkwrite_no_entry);
 DEFINE_PTE_FAULT_EVENT(dax_insert_pfn_mkwrite);
 
-TRACE_EVENT(dax_insert_mapping,
-	TP_PROTO(struct inode *inode, struct vm_fault *vmf, void *radix_entry),
-	TP_ARGS(inode, vmf, radix_entry),
-	TP_STRUCT__entry(
-		__field(unsigned long, ino)
-		__field(unsigned long, vm_flags)
-		__field(unsigned long, address)
-		__field(void *, radix_entry)
-		__field(dev_t, dev)
-		__field(int, write)
-	),
-	TP_fast_assign(
-		__entry->dev = inode->i_sb->s_dev;
-		__entry->ino = inode->i_ino;
-		__entry->vm_flags = vmf->vma->vm_flags;
-		__entry->address = vmf->address;
-		__entry->write = vmf->flags & FAULT_FLAG_WRITE;
-		__entry->radix_entry = radix_entry;
-	),
-	TP_printk("dev %d:%d ino %#lx %s %s address %#lx radix_entry %#lx",
-		MAJOR(__entry->dev),
-		MINOR(__entry->dev),
-		__entry->ino,
-		__entry->vm_flags & VM_SHARED ? "shared" : "private",
-		__entry->write ? "write" : "read",
-		__entry->address,
-		(unsigned long)__entry->radix_entry
-	)
-)
-
 DECLARE_EVENT_CLASS(dax_writeback_range_class,
 	TP_PROTO(struct inode *inode, pgoff_t start_index, pgoff_t end_index),
 	TP_ARGS(inode, start_index, end_index),
Signature · 1 line
2.47.2
Acked-by on v1 5/5
#7 lore raw

Re: [for-linus][PATCH 5/5] fsdax: Remove unused trace events for dax insert mapping

Quoted 7 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

When the dax_fault_actor() helper was factored out, it removed the calls to the dax_pmd_insert_mapping and dax_insert_mapping events but never removed the events themselves. As each event created takes up memory (roughly 5K each), this is a waste as it is never used.

Quoted 6 lines

Remove the unused dax_pmd_insert_mapping and dax_insert_mapping trace events.

Cc Dan Williams <[email protected]>
author maintainer
#8 lore raw

Re: [for-linus][PATCH 5/5] fsdax: Remove unused trace events for dax insert mapping

Quoted 8 lines · On Tue, 3 Jun 2025 11:13:58 -0700 Dan Williams <[email protected]> wrote:
Quoted 7 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

When the dax_fault_actor() helper was factored out, it removed the calls to the dax_pmd_insert_mapping and dax_insert_mapping events but never removed the events themselves. As each event created takes up memory (roughly 5K each), this is a waste as it is never used.

Quoted 8 lines
Quoted 6 lines

Remove the unused dax_pmd_insert_mapping and dax_insert_mapping trace events.

Cc Dan Williams <[email protected]>

Thanks!

-- Steve