Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v3] tracing/probes: Zero out faulted dereferenced fields
@ 2026-11-19 20:29 Steven Rostedt
  2026-11-19 20:50 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2026-11-19 20:29 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>
---
Changes since v2: https://patch.msgid.link/20261112105853.38bd39be@fedora

- Do not dereference any val that is zero. Set offset to zero as well.
  (sashiko)

 kernel/trace/trace_probe_kernel.h | 13 ++++++++--
 kernel/trace/trace_probe_tmpl.h   | 41 +++++++++++++++++++------------
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/kernel/trace/trace_probe_kernel.h b/kernel/trace/trace_probe_kernel.h
index 4eb4acfa09ba..00542a162d30 100644
--- a/kernel/trace/trace_probe_kernel.h
+++ b/kernel/trace/trace_probe_kernel.h
@@ -102,18 +102,27 @@ static nokprobe_inline int
 probe_mem_read_user(void *dest, void *src, size_t size)
 {
 	const void __user *uaddr =  (__force const void __user *)src;
+	int ret;
 
-	return copy_from_user_nofault(dest, uaddr, size);
+	ret = copy_from_user_nofault(dest, uaddr, size);
+	if (ret < 0)
+		memset(dest, 0, size);
+	return ret;
 }
 
 static nokprobe_inline int
 probe_mem_read(void *dest, void *src, size_t size)
 {
+	int ret;
+
 #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);
+	ret = copy_from_kernel_nofault(dest, src, size);
+	if (ret < 0)
+		memset(dest, 0, size);
+	return ret;
 }
 
 #endif /* __TRACE_PROBE_KERNEL_H_ */
diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
index 8acc0f0ebd7a..4bb5ca440b75 100644
--- a/kernel/trace/trace_probe_tmpl.h
+++ b/kernel/trace/trace_probe_tmpl.h
@@ -139,7 +139,7 @@ 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 +147,28 @@ 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)
+				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)
+				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;
 			break;
 		default:
 			lval = llval;
 			goto out;
 		}
-		if (ret)
-			return ret;
 		llval = lval;
 		code++;
 	} while (1);
@@ -173,19 +176,22 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
 
 	s3 = code;
 stage3:
+	/* Prevent dereferencing NULL val 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 = fetch_store_strlen(val + offset);
 			code++;
 			goto array;
 		case FETCH_OP_ST_USTRING:
-			ret = fetch_store_strlen_user(val + code->offset);
+			ret = fetch_store_strlen_user(val + offset);
 			code++;
 			goto array;
 		case FETCH_OP_ST_SYMSTR:
-			ret = fetch_store_symstrlen(val + code->offset);
+			ret = fetch_store_symstrlen(val + offset);
 			code++;
 			goto array;
 		default:
@@ -193,27 +199,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
 		}
 	}
 
+	/* Prevent dereferencing NULL val pointer */
+	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);
+		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);
+		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 = 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 = 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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] tracing/probes: Zero out faulted dereferenced fields
  2026-11-19 20:29 [PATCH v3] tracing/probes: Zero out faulted dereferenced fields Steven Rostedt
@ 2026-11-19 20:50 ` sashiko-bot
  2026-11-19 21:19   ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-19 20:50 UTC (permalink / raw)
  To: Steven Rostedt, Linux Trace Kernel

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] tracing/probes: stage 3 in process_fetch_insn_bottom()
  dereferences NULL val
- [Medium] tracing/probes: FETCH_OP_LP_ARRAY in
  process_fetch_insn_bottom() advances NULL
- [Medium] tracing/probes: FETCH_OP_CPU_PTR in
  process_fetch_insn_bottom() shifts NULL
- [Low] tracing/probes: trace_uprobe probe_mem_read() leaves partial
  bytes on fault
--

commit 754d85e0c0dcb5af755f3a74bd787872a07c54fd
Author: Steven Rostedt <rostedt@goodmis.org>

tracing/probes: Zero out faulted dereferenced fields

This commit zeroes the destination buffer in probe_mem_read() and
probe_mem_read_user() on read failure, and updates
process_fetch_insn_bottom() to skip NULL dereferences and continue to
stage 3 so faulted fields are zeroed in the trace event.

> diff --git a/kernel/trace/trace_probe_kernel.h b/kernel/trace/trace_probe_kernel.h
> index 4eb4acfa09ba1..00542a162d307 100644
> --- a/kernel/trace/trace_probe_kernel.h
> +++ b/kernel/trace/trace_probe_kernel.h
> @@ -102,18 +102,27 @@ static nokprobe_inline int
>  probe_mem_read_user(void *dest, void *src, size_t size)
>  {
>  	const void __user *uaddr =  (__force const void __user *)src;
> +	int ret;
>  
> -	return copy_from_user_nofault(dest, uaddr, size);
> +	ret = copy_from_user_nofault(dest, uaddr, size);
> +	if (ret < 0)
> +		memset(dest, 0, size);
> +	return ret;
>  }
>  
>  static nokprobe_inline int
>  probe_mem_read(void *dest, void *src, size_t size)
>  {
> +	int ret;
> +
>  #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);
> +	ret = copy_from_kernel_nofault(dest, src, size);
> +	if (ret < 0)
> +		memset(dest, 0, size);
> +	return ret;
>  }

[Severity: Low]
Does probe_mem_read() in kernel/trace/trace_uprobe.c need the same
memset(dest, 0, size) on error?

kernel/trace/trace_uprobe.c:probe_mem_read() {
	void __user *vaddr = (void __force __user *)src;

	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
}

trace_uprobe.c includes trace_probe_tmpl.h, where
process_fetch_insn_bottom() now ignores the return value of
probe_mem_read() in stage 2 and relies on it to zero dest on failure.
When an unaligned multi-byte read faults across a page boundary,
copy_from_user() only zeroes the uncopied trailing bytes, leaving the
initial bytes from the first page in val and causing stage 2 to
dereference a truncated non-zero address.

> diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
> index 8db12f758fdae..8be14c00d66f9 100644
> --- a/kernel/trace/trace_probe_tmpl.h
> +++ b/kernel/trace/trace_probe_tmpl.h
> @@ -127,7 +127,7 @@ 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;
>  
> @@ -135,25 +135,28 @@ 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)
> +				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)
> +				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;
>  			break;

[Severity: Medium]
Should FETCH_OP_CPU_PTR also check if (val) before calling
this_cpu_ptr()?

Now that stage 2 continues after a failed or NULL dereference, an inner
dereference in an expression such as this_cpu_ptr(+8($arg1)) or
this_cpu_read(+8($arg1)) can reach FETCH_OP_CPU_PTR with val == 0.
On SMP kernels, this_cpu_ptr((void __percpu *)0) evaluates to
__my_cpu_offset, so subsequent instructions will record or dereference
the current CPU's per-CPU base address + offset instead of zero.

>  		default:
>  			lval = llval;
>  			goto out;
>  		}
> -		if (ret)
> -			return ret;
>  		llval = lval;
>  		code++;
>  	} while (1);
> @@ -161,19 +164,22 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
>  
>  	s3 = code;
>  stage3:
> +	/* Prevent dereferencing NULL val 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 = fetch_store_strlen(val + offset);
>  			code++;
>  			goto array;
>  		case FETCH_OP_ST_USTRING:
> -			ret = fetch_store_strlen_user(val + code->offset);
> +			ret = fetch_store_strlen_user(val + offset);
>  			code++;
>  			goto array;
>  		case FETCH_OP_ST_SYMSTR:
> -			ret = fetch_store_symstrlen(val + code->offset);
> +			ret = fetch_store_symstrlen(val + offset);
>  			code++;
>  			goto array;
>  		default:
> @@ -181,27 +187,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
>  		}
>  	}
>  
> +	/* Prevent dereferencing NULL val pointer */
> +	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);
> +		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);
> +		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 = 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 = fetch_store_string_user(val + offset, dest, base);
>  		break;
>  	case FETCH_OP_ST_SYMSTR:
>  		loc = *(u32 *)dest;
> -		ret = fetch_store_symstring(val + offset, dest, base);
> +		ret = fetch_store_symstring(val + offset, dest, base);
>  		break;
>  	default:
>  		return -EILSEQ;

[Severity: Medium]
Does setting offset to 0 in stage 3 still dereference address 0 when val
is NULL?

finalize_fetch_insn() converts the final FETCH_OP_DEREF or
FETCH_OP_UDEREF of a dereference chain (such as ->pgd in prev->mm->pgd)
into FETCH_OP_ST_MEM, FETCH_OP_ST_UMEM, FETCH_OP_ST_STRING or
FETCH_OP_ST_USTRING in stage 3.  When val is 0, val + offset is 0, and
stage 3 still calls probe_mem_read(), fetch_store_strlen(),
fetch_store_string() and their user counterparts with address 0.

On CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE architectures, 0 <
TASK_SIZE is true, so probe_mem_read(), fetch_store_strlen() and
fetch_store_string() redirect the NULL kernel pointer read to current's
user virtual address 0, faulting on every NULL pointer or reading user
page 0 if mapped.  On s390, where address 0 is mapped to struct lowcore,
copy_from_kernel_nofault() succeeds and copies lowcore bytes into the
trace buffer instead of zeroing dest.

[Severity: Medium]
What happens in the FETCH_OP_LP_ARRAY loop below stage 3 when val or
lval is 0?

kernel/trace/trace_probe_tmpl.h:process_fetch_insn_bottom() {
    ...
	if (code->op == FETCH_OP_LP_ARRAY) {
		if (ret < 0)
			ret = 0;
		total += ret;
		if (++i < code->param) {
			code = s3;
			if (s3->op != FETCH_OP_ST_STRING &&
			    s3->op != FETCH_OP_ST_USTRING) {
				dest += s3->size;
				val += s3->size;
				goto stage3;
			}
			code--;
			val = lval + sizeof(char *);
    ...
}

Because stage 2 no longer returns early on a dereference fault, a NULL or
faulted array base pointer now enters the FETCH_OP_LP_ARRAY loop with
val == 0 (or lval == 0).  On the next iteration, val += s3->size or
val = lval + sizeof(char *) turns 0 into a non-zero value (such as 8), so
val ? code->offset : 0 and if (val) treat it as a valid pointer and
dereference i * elem_size + code->offset for the remaining array
elements.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119152922.5a66f10e@gandalf.local.home?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] tracing/probes: Zero out faulted dereferenced fields
  2026-11-19 20:50 ` sashiko-bot
@ 2026-11-19 21:19   ` Steven Rostedt
  2026-11-20  0:10     ` Masami Hiramatsu (Google)
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2026-11-19 21:19 UTC (permalink / raw)
  To: sashiko-bot; +Cc: sashiko-reviews, Linux Trace Kernel

On Thu, 19 Nov 2026 20:50:02 +0000
sashiko-bot@kernel.org wr
> 
> [Severity: Low]
> Does probe_mem_read() in kernel/trace/trace_uprobe.c need the same
> memset(dest, 0, size) on error?

Probably.

> 
> kernel/trace/trace_uprobe.c:probe_mem_read() {
> 	void __user *vaddr = (void __force __user *)src;
> 
> 	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
> }
> 
> trace_uprobe.c includes trace_probe_tmpl.h, where
> process_fetch_insn_bottom() now ignores the return value of
> probe_mem_read() in stage 2 and relies on it to zero dest on failure.
> When an unaligned multi-byte read faults across a page boundary,
> copy_from_user() only zeroes the uncopied trailing bytes, leaving the
> initial bytes from the first page in val and causing stage 2 to
> dereference a truncated non-zero address.
> 
> > diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
> > index 8db12f758fdae..8be14c00d66f9 100644
> > --- a/kernel/trace/trace_probe_tmpl.h
> > +++ b/kernel/trace/trace_probe_tmpl.h
> > @@ -127,7 +127,7 @@ 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;
> >  
> > @@ -135,25 +135,28 @@ 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)
> > +				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)
> > +				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;
> >  			break;  
> 
> [Severity: Medium]
> Should FETCH_OP_CPU_PTR also check if (val) before calling
> this_cpu_ptr()?
> 
> Now that stage 2 continues after a failed or NULL dereference, an inner
> dereference in an expression such as this_cpu_ptr(+8($arg1)) or
> this_cpu_read(+8($arg1)) can reach FETCH_OP_CPU_PTR with val == 0.
> On SMP kernels, this_cpu_ptr((void __percpu *)0) evaluates to
> __my_cpu_offset, so subsequent instructions will record or dereference
> the current CPU's per-CPU base address + offset instead of zero.

Yeah, I'll update it to not do the per_cpu assignment if val is zero.

> 
> >  		default:
> >  			lval = llval;
> >  			goto out;
> >  		}
> > -		if (ret)
> > -			return ret;
> >  		llval = lval;
> >  		code++;
> >  	} while (1);
> > @@ -161,19 +164,22 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
> >  
> >  	s3 = code;
> >  stage3:
> > +	/* Prevent dereferencing NULL val 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 = fetch_store_strlen(val + offset);
> >  			code++;
> >  			goto array;
> >  		case FETCH_OP_ST_USTRING:
> > -			ret = fetch_store_strlen_user(val + code->offset);
> > +			ret = fetch_store_strlen_user(val + offset);
> >  			code++;
> >  			goto array;
> >  		case FETCH_OP_ST_SYMSTR:
> > -			ret = fetch_store_symstrlen(val + code->offset);
> > +			ret = fetch_store_symstrlen(val + offset);
> >  			code++;
> >  			goto array;
> >  		default:
> > @@ -181,27 +187,30 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
> >  		}
> >  	}
> >  
> > +	/* Prevent dereferencing NULL val pointer */
> > +	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);
> > +		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);
> > +		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 = 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 = fetch_store_string_user(val + offset, dest, base);
> >  		break;
> >  	case FETCH_OP_ST_SYMSTR:
> >  		loc = *(u32 *)dest;
> > -		ret = fetch_store_symstring(val + offset, dest, base);
> > +		ret = fetch_store_symstring(val + offset, dest, base);
> >  		break;
> >  	default:
> >  		return -EILSEQ;  
> 
> [Severity: Medium]
> Does setting offset to 0 in stage 3 still dereference address 0 when val
> is NULL?
> 
> finalize_fetch_insn() converts the final FETCH_OP_DEREF or
> FETCH_OP_UDEREF of a dereference chain (such as ->pgd in prev->mm->pgd)
> into FETCH_OP_ST_MEM, FETCH_OP_ST_UMEM, FETCH_OP_ST_STRING or
> FETCH_OP_ST_USTRING in stage 3.  When val is 0, val + offset is 0, and
> stage 3 still calls probe_mem_read(), fetch_store_strlen(),
> fetch_store_string() and their user counterparts with address 0.
> 
> On CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE architectures, 0 <
> TASK_SIZE is true, so probe_mem_read(), fetch_store_strlen() and
> fetch_store_string() redirect the NULL kernel pointer read to current's
> user virtual address 0, faulting on every NULL pointer or reading user
> page 0 if mapped.  On s390, where address 0 is mapped to struct lowcore,
> copy_from_kernel_nofault() succeeds and copies lowcore bytes into the
> trace buffer instead of zeroing dest.

Yep, works as designed!

> 
> [Severity: Medium]
> What happens in the FETCH_OP_LP_ARRAY loop below stage 3 when val or
> lval is 0?
> 
> kernel/trace/trace_probe_tmpl.h:process_fetch_insn_bottom() {
>     ...
> 	if (code->op == FETCH_OP_LP_ARRAY) {
> 		if (ret < 0)
> 			ret = 0;
> 		total += ret;
> 		if (++i < code->param) {
> 			code = s3;
> 			if (s3->op != FETCH_OP_ST_STRING &&
> 			    s3->op != FETCH_OP_ST_USTRING) {
> 				dest += s3->size;
> 				val += s3->size;
> 				goto stage3;
> 			}
> 			code--;
> 			val = lval + sizeof(char *);
>     ...
> }
> 
> Because stage 2 no longer returns early on a dereference fault, a NULL or
> faulted array base pointer now enters the FETCH_OP_LP_ARRAY loop with
> val == 0 (or lval == 0).  On the next iteration, val += s3->size or
> val = lval + sizeof(char *) turns 0 into a non-zero value (such as 8), so
> val ? code->offset : 0 and if (val) treat it as a valid pointer and
> dereference i * elem_size + code->offset for the remaining array
> elements.

Ah, I probably should have it short circuit here too on val == 0.

-- Steve


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] tracing/probes: Zero out faulted dereferenced fields
  2026-11-19 21:19   ` Steven Rostedt
@ 2026-11-20  0:10     ` Masami Hiramatsu (Google)
  0 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-20  0:10 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: sashiko-bot, sashiko-reviews, Linux Trace Kernel

On Thu, 19 Nov 2026 16:19:21 -0500, Steven Rostedt <rostedt@goodmis.org> wrote:
> On Thu, 19 Nov 2026 20:50:02 +0000
> sashiko-bot@kernel.org wr
> > 
> > [Severity: Low]
> > Does probe_mem_read() in kernel/trace/trace_uprobe.c need the same
> > memset(dest, 0, size) on error?
> 
> Probably.
> 
> > 
> > kernel/trace/trace_uprobe.c:probe_mem_read() {
> > 	void __user *vaddr = (void __force __user *)src;
> > 
> > 	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
> > }
> > 
> > trace_uprobe.c includes trace_probe_tmpl.h, where
> > process_fetch_insn_bottom() now ignores the return value of
> > probe_mem_read() in stage 2 and relies on it to zero dest on failure.
> > When an unaligned multi-byte read faults across a page boundary,
> > copy_from_user() only zeroes the uncopied trailing bytes, leaving the
> > initial bytes from the first page in val and causing stage 2 to
> > dereference a truncated non-zero address.
> > 
> > > diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
> > > index 8db12f758fdae..8be14c00d66f9 100644
> > > --- a/kernel/trace/trace_probe_tmpl.h
> > > +++ b/kernel/trace/trace_probe_tmpl.h
> > > @@ -127,7 +127,7 @@ 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;
> > >  
> > > @@ -135,25 +135,28 @@ 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)
> > > +				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)
> > > +				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;
> > >  			break;  
> > 
> > [Severity: Medium]
> > Should FETCH_OP_CPU_PTR also check if (val) before calling
> > this_cpu_ptr()?
> > 
> > Now that stage 2 continues after a failed or NULL dereference, an inner
> > dereference in an expression such as this_cpu_ptr(+8($arg1)) or
> > this_cpu_read(+8($arg1)) can reach FETCH_OP_CPU_PTR with val == 0.
> > On SMP kernels, this_cpu_ptr((void __percpu *)0) evaluates to
> > __my_cpu_offset, so subsequent instructions will record or dereference
> > the current CPU's per-CPU base address + offset instead of zero.
> 
> Yeah, I'll update it to not do the per_cpu assignment if val is zero.

Ah, thanks for updating! That sounds good to me.

Thanks,

-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20  0:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 20:29 [PATCH v3] tracing/probes: Zero out faulted dereferenced fields Steven Rostedt
2026-11-19 20:50 ` sashiko-bot
2026-11-19 21:19   ` Steven Rostedt
2026-11-20  0:10     ` Masami Hiramatsu (Google)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox