* [PATCH v7] tracing/probes: Zero out faulted dereferenced fields
@ 2026-11-24 2:32 Steven Rostedt
2026-11-24 2:51 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Steven Rostedt @ 2026-11-24 2:32 UTC (permalink / raw)
To: LKML, Linux Trace Kernel; +Cc: Masami Hiramatsu, Mathieu Desnoyers
From: Steven Rostedt <rostedt@goodmis.org>
While looking at tasks global descriptor tables, I discovered the
following anomaly:
~# echo 't:sched sched_switch prev=prev mm=(task_struct)prev->mm ppgd=(task_struct)prev->mm->pgd' > /sys/kernel/tracing/dynamic_events
The above creates a tprobe on the sched_switch tracepoint and records the
previous task_struct pointer, it's mm_struct and the pgd the mm_struct
points to.
~# trace-cmd start -e tracepoints:sched
bash-1050 [000] d..3. 90.829478: sched: (__probestub_sched_switch+0x0/0x10) prev=0xffff88d30b623600 mm=0xffff88d30108c600 ppgd=0xffff88d33352b000
<idle>-0 [002] d..3. 90.836024: sched: (__probestub_sched_switch+0x0/0x10) prev=0xffff88d301870000 mm=0x0 ppgd=0x0
rcu_preempt-15 [002] d..3. 90.836031: sched: (__probestub_sched_switch+0x0/0x10) prev=0xffff88d3013ad100 mm=0x0 ppgd=0xffff88d33352b000
The first event has is a user task that has both a valid mm_struct and a pgd.
The second event is a kernel task where the mm_struct is NULL and so is the
dereferenced pgd pointer.
The third event is where things looked strange. The mm_struct is NULL but
the dereferenced pgd has a value! That is: NULL->pgd produced something
other than zero.
The reason for this is that the probe_mem_read() and probe_mem_read_user()
do not write to the destination on failure. The process_fetch_insn_bottom()
loop does not check the return value of the dereference (as it is the last
one), and does not update the event. That leaves the value for that failed
field to be whatever was in the ring buffer at that time.
Have both probe_mem_read() and probe_mem_read_user() set the destination to
zero on failure to read the address.
Now all pgd are zero when mm is zero.
Also update the stage 2 of process_fetch_insn_bottom() to not return on
fault but to just set the value to zero and also do not even bother
dereferencing the address if the value is already zero. This allows the
code to continue and fill in the zero into the memory location on the
ring buffer.
Cc: stable@vger.kernel.org
Fixes: 533059281ee59 ("tracing: probeevent: Introduce new argument fetching code")
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
v6 was supposed to be this patch but a broken version was sent out by mistake.
Changes since v5: https://patch.msgid.link/20261120152432.0dc2bdf0@gandalf.local.home
- Handle fetch_store_string*() when val is NULL (Sashiko)
kernel/trace/trace_probe_tmpl.h | 110 ++++++++++++++++++++++++++------
1 file changed, 89 insertions(+), 21 deletions(-)
diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
index 8acc0f0ebd7a..9672474b067b 100644
--- a/kernel/trace/trace_probe_tmpl.h
+++ b/kernel/trace/trace_probe_tmpl.h
@@ -90,13 +90,15 @@ fetch_store_symstring(unsigned long addr, void *dest, void *base)
char namebuf[KSYM_SYMBOL_LEN];
int maxlen = get_loc_len(*(u32 *)dest);
void *__dest;
- int len;
+ int len = -1;
if (unlikely(!maxlen))
return -ENOMEM;
__dest = get_loc_data(dest, base);
- len = sprint_symbol(namebuf, addr);
+ /* Do not dereference NULL pointers */
+ if (addr)
+ len = sprint_symbol(namebuf, addr);
if (len < 0) {
*(u32 *)dest = make_data_loc(0, __dest - base);
return 0;
@@ -133,13 +135,63 @@ process_common_fetch_insn(struct fetch_insn *code, unsigned long *val)
return 0;
}
+/* On failure, zero out the destination */
+static nokprobe_inline int
+do_probe_mem_read_user(void *dest, void *src, size_t size)
+{
+ int ret;
+
+ ret = probe_mem_read_user(dest, src, size);
+ if (ret < 0)
+ memset(dest, 0, size);
+ return ret;
+}
+
+/* On failure, zero out the destination */
+static nokprobe_inline int
+do_probe_mem_read(void *dest, void *src, size_t size)
+{
+ int ret;
+
+ ret = probe_mem_read(dest, src, size);
+ if (ret < 0)
+ memset(dest, 0, size);
+ return ret;
+}
+
+static nokprobe_inline int
+do_fetch_store_string(unsigned long addr, void *dest, void *base)
+{
+ /* NULL addr gets zero size string */
+ if (!addr) {
+ void *__dest = get_loc_data(dest, base);
+
+ *(u32 *)dest = make_data_loc(0, __dest - base);
+ return 0;
+ }
+ return fetch_store_string(addr, dest, base);
+}
+
+static nokprobe_inline int
+do_fetch_store_string_user(unsigned long addr, void *dest, void *base)
+{
+ /* NULL addr gets zero size string */
+ if (!addr) {
+ void *__dest = get_loc_data(dest, base);
+
+ *(u32 *)dest = make_data_loc(0, __dest - base);
+ return 0;
+ }
+ return fetch_store_string_user(addr, dest, base);
+}
+
/* From the 2nd stage, routine is same */
static nokprobe_inline int
process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
void *dest, void *base)
{
struct fetch_insn *s3 = NULL;
- int total = 0, ret = 0, i = 0;
+ int total = 0, ret = 0, i = 0, offset;
u32 loc = 0;
unsigned long lval, llval = val;
@@ -147,25 +199,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
/* 2nd stage: dereference memory if needed */
do {
lval = val;
+ /* Prevent dereferencing NULL val pointer */
+ offset = val ? code->offset : 0;
switch (code->op) {
case FETCH_OP_DEREF:
- ret = probe_mem_read(&val, (void *)val + code->offset,
- sizeof(val));
+ /* Do not dereference NULL pointers */
+ if (val)
+ do_probe_mem_read(&val, (void *)val + offset,
+ sizeof(val));
break;
case FETCH_OP_UDEREF:
- ret = probe_mem_read_user(&val,
- (void *)val + code->offset, sizeof(val));
+ /* Do not dereference NULL pointers */
+ if (val)
+ do_probe_mem_read_user(&val, (void *)val + offset,
+ sizeof(val));
break;
case FETCH_OP_CPU_PTR:
- val = (unsigned long)this_cpu_ptr((void __percpu *)val);
- ret = 0;
+ /* Do not dereference NULL pointers */
+ if (val)
+ val = (unsigned long)this_cpu_ptr((void __percpu *)val);
break;
default:
lval = llval;
goto out;
}
- if (ret)
- return ret;
llval = lval;
code++;
} while (1);
@@ -173,19 +230,25 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
s3 = code;
stage3:
+ /*
+ * Once val is zero, it is pointing to NULL. Any new dereference is
+ * also treated as a NULL pointer.
+ */
+ offset = val ? code->offset : 0;
+
/* 3rd stage: store value to buffer */
if (unlikely(!dest)) {
switch (code->op) {
case FETCH_OP_ST_STRING:
- ret = fetch_store_strlen(val + code->offset);
+ ret = val ? fetch_store_strlen(val + offset) : 0;
code++;
goto array;
case FETCH_OP_ST_USTRING:
- ret = fetch_store_strlen_user(val + code->offset);
+ ret = val ? fetch_store_strlen_user(val + offset) : 0;
code++;
goto array;
case FETCH_OP_ST_SYMSTR:
- ret = fetch_store_symstrlen(val + code->offset);
+ ret = val ? fetch_store_symstrlen(val + offset) : 0;
code++;
goto array;
default:
@@ -193,27 +256,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
}
}
+ /* When val is NULL, so should the val + offset */
+ offset = val ? code->offset : 0;
+
switch (code->op) {
case FETCH_OP_ST_RAW:
fetch_store_raw(val, code, dest);
break;
case FETCH_OP_ST_MEM:
- probe_mem_read(dest, (void *)val + code->offset, code->size);
+ do_probe_mem_read(dest, (void *)val + offset, code->size);
break;
case FETCH_OP_ST_UMEM:
- probe_mem_read_user(dest, (void *)val + code->offset, code->size);
+ do_probe_mem_read_user(dest, (void *)val + offset, code->size);
break;
case FETCH_OP_ST_STRING:
loc = *(u32 *)dest;
- ret = fetch_store_string(val + code->offset, dest, base);
+ ret = do_fetch_store_string(val + offset, dest, base);
break;
case FETCH_OP_ST_USTRING:
loc = *(u32 *)dest;
- ret = fetch_store_string_user(val + code->offset, dest, base);
+ ret = do_fetch_store_string_user(val + offset, dest, base);
break;
case FETCH_OP_ST_SYMSTR:
loc = *(u32 *)dest;
- ret = fetch_store_symstring(val + code->offset, dest, base);
+ ret = fetch_store_symstring(val + offset, dest, base);
break;
default:
return -EILSEQ;
@@ -237,11 +303,13 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
if (s3->op != FETCH_OP_ST_STRING &&
s3->op != FETCH_OP_ST_USTRING) {
dest += s3->size;
- val += s3->size;
+ /* Only update a non NULL val */
+ if (val)
+ val += s3->size;
goto stage3;
}
code--;
- val = lval + sizeof(char *);
+ val = lval ? lval + sizeof(char *) : 0;
if (dest) {
dest += sizeof(u32);
*(u32 *)dest = update_data_loc(loc, ret);
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v7] tracing/probes: Zero out faulted dereferenced fields
2026-11-24 2:32 [PATCH v7] tracing/probes: Zero out faulted dereferenced fields Steven Rostedt
@ 2026-11-24 2:51 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24 2:51 UTC (permalink / raw)
To: Steven Rostedt, Linux Trace Kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tracing/probes: do_probe_mem_read() dereferences NULL in
stage 3
--
commit fd104aec084b16bdb5ef3c044c9c69e47141c17c
Author: Steven Rostedt <rostedt@goodmis.org>
tracing/probes: Zero out faulted dereferenced fields
This commit zeroes the destination buffer when probe memory reads or
dereferences fail or encounter NULL pointers during probe argument
fetching.
> diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
> index 8acc0f0ebd7a6..9672474b067b3 100644
> --- a/kernel/trace/trace_probe_tmpl.h
> +++ b/kernel/trace/trace_probe_tmpl.h
[ ... ]
> @@ -133,13 +135,63 @@ process_common_fetch_insn(struct fetch_insn *code, unsigned long *val)
> return 0;
> }
>
> +/* On failure, zero out the destination */
> +static nokprobe_inline int
> +do_probe_mem_read_user(void *dest, void *src, size_t size)
> +{
> + int ret;
> +
> + ret = probe_mem_read_user(dest, src, size);
> + if (ret < 0)
> + memset(dest, 0, size);
> + return ret;
> +}
> +
> +/* On failure, zero out the destination */
> +static nokprobe_inline int
> +do_probe_mem_read(void *dest, void *src, size_t size)
> +{
> + int ret;
> +
> + ret = probe_mem_read(dest, src, size);
> + if (ret < 0)
> + memset(dest, 0, size);
> + return ret;
> +}
[ ... ]
> @@ -193,27 +256,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
> }
> }
>
> + /* When val is NULL, so should the val + offset */
> + offset = val ? code->offset : 0;
> +
> switch (code->op) {
> case FETCH_OP_ST_RAW:
> fetch_store_raw(val, code, dest);
> break;
> case FETCH_OP_ST_MEM:
> - probe_mem_read(dest, (void *)val + code->offset, code->size);
> + do_probe_mem_read(dest, (void *)val + offset, code->size);
> break;
> case FETCH_OP_ST_UMEM:
> - probe_mem_read_user(dest, (void *)val + code->offset, code->size);
> + do_probe_mem_read_user(dest, (void *)val + offset, code->size);
> break;
[Severity: Medium]
Should do_probe_mem_read() and do_probe_mem_read_user() check for a NULL
src before calling probe_mem_read() and probe_mem_read_user()?
When val is 0 in stage 3 (for example, when prev->mm is NULL in
prev->mm->pgd, where finalize_fetch_insn() turns the final dereference
into FETCH_OP_ST_MEM), offset is set to 0 and (void *)val + offset passes
NULL as src to do_probe_mem_read() or do_probe_mem_read_user().
Because neither helper checks for !src, every NULL pointer hit attempts a
real memory read at virtual address 0x0, raising a hardware page fault on
every event when page 0 is unmapped. If address 0x0 is readable, the
read succeeds instead of zeroing dest: on s390 it copies kernel lowcore
bytes from 0x0, and on CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
architectures probe_mem_read() sees (unsigned long)src < TASK_SIZE and
reads user-space address 0x0 if mapped:
kernel/trace/trace_probe_kernel.h:probe_mem_read() {
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
if ((unsigned long)src < TASK_SIZE)
return probe_mem_read_user(dest, src, size);
#endif
return copy_from_kernel_nofault(dest, src, size);
}
Also, in process_fetch_insn_bottom(), offset = val ? code->offset : 0 is
computed twice in stage 3, and in stage 2 offset is only used inside
if (val) blocks where code->offset can be used directly.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123213246.00440bfc@gandalf.local.home?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 2:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 2:32 [PATCH v7] tracing/probes: Zero out faulted dereferenced fields Steven Rostedt
2026-11-24 2:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox