Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Steven Rostedt" <rostedt@goodmis.org>,
	"Linux Trace Kernel" <linux-trace-kernel@vger.kernel.org>
Subject: Re: [PATCH v5] tracing/probes: Zero out faulted dereferenced fields
Date: Fri, 20 Nov 2026 20:39:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-226078@kernel.org> (raw)
In-Reply-To: <20261120152432.0dc2bdf0@gandalf.local.home>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tracing/probes: stage 3 dereferences 0x0 in
  process_fetch_insn_bottom()
--

commit 6464703716b264ea3aea879ca95045129fa2a909
Author: Steven Rostedt <rostedt@goodmis.org>

tracing/probes: Zero out faulted dereferenced fields

This commit introduces do_probe_mem_read() and do_probe_mem_read_user() to
zero out the destination buffer on read faults, and updates
process_fetch_insn_bottom() to continue to stage 3 with val set to 0 rather
than returning early when a dereference fails or encounters NULL.

> diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h
> index 8db12f758fdae..3e9dea89e90ca 100644
> --- a/kernel/trace/trace_probe_tmpl.h
> +++ b/kernel/trace/trace_probe_tmpl.h
> @@ -121,13 +121,37 @@ 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;
> +}
> +
>  /* 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;
>  
> @@ -135,25 +159,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);
> @@ -161,19 +190,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 = 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 +216,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 + 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;

[Severity: Medium]
Does stage 3 still attempt to read from virtual address 0x0 when val is
0?

finalize_fetch_insn() converts the final dereference in a chain (such as
ppgd=(task_struct)prev->mm->pgd) into FETCH_OP_ST_MEM, FETCH_OP_ST_UMEM,
FETCH_OP_ST_STRING, or FETCH_OP_ST_USTRING.  When val is 0 entering
stage 3 (either because a pointer in stage 2 was NULL or because a
stage 2 read faulted and zeroed val), setting offset = 0 passes address
0x0 to the stage 3 helpers instead of skipping the memory read.

This causes three problems when val is 0 in stage 3:

1. If address 0x0 is readable, stage 3 records data from 0x0 instead of
zeroing the field or recording a fault.  On s390, kernel address 0x0 is
mapped to S390_lowcore, so copy_from_kernel_nofault() succeeds and copies
lowcore bytes into dest.  On architectures with
CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE (such as x86 and arm64),
probe_mem_read(), fetch_store_strlen(), and fetch_store_string() check
(unsigned long)src < TASK_SIZE and redirect address 0x0 to user space,
reading from user page 0 if mapped.  For FETCH_OP_ST_SYMSTR, a stage 2
fault zeroes val and formats 0x0 instead of (fault).

2. On every NULL pointer (such as prev->mm == NULL on kernel thread
switches in sched_switch) or after any stage 2 fault, stage 3 triggers an
unnecessary hardware page fault and exception-table fixup at 0x0 (or one
per element in a FETCH_OP_LP_ARRAY loop).

3. In stage 2, offset = val ? code->offset : 0 is only used inside
if (val) blocks where val is already non-zero, and in stage 3, offset is
computed twice without val or code changing in between.

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

      reply	other threads:[~2026-11-20 20:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 20:24 [PATCH v5] tracing/probes: Zero out faulted dereferenced fields Steven Rostedt
2026-11-20 20:39 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-226078@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox