All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nuoqi Gui" <gnq25@mails.tsinghua.edu.cn>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 2/2] selftests/bpf: Cover scalar addition from rdonly untrusted memory
Date: Tue, 09 Jun 2026 15:08:12 +0000	[thread overview]
Message-ID: <20260609150813.387981F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260609-f01-03-scalar-add-bpf-next-v1-2-e6212e274155@mails.tsinghua.edu.cn>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] The newly added selftest only verifies the rejection of `BPF_ADD` with untrusted pointers, missing coverage for other vulnerable ALU operations.

Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but the verifier incorrectly allows scalar operations (like BPF_SUB, BPF_AND, BPF_XOR) with untrusted pointers, leading to arbitrary kernel memory read/write vulnerabilities.
--

commit f35b72689eaa4c1dd4c68b268d4611d79f672994
Author: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>

selftests/bpf: Cover scalar addition from rdonly untrusted memory

This commit adds a BPF verifier selftest for scalar addition with
rdonly_untrusted_mem. It checks if the verifier properly rejects adding an
untrusted memory value to a scalar destination.

> diff --git a/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c b/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
> index 5b4453747c230..303b8ed3e70b6 100644
> --- a/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
> +++ b/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
[ ... ]
> @@ -77,6 +77,23 @@ int offset_not_tracked(void *ctx)
>  	return s;
>  }
>  
> +SEC("socket")
> +__failure
> +__msg("R1 tried to add from rdonly_untrusted_mem to scalar")
> +__naked void scalar_add_not_ok(void)
> +{
> +	asm volatile ("r1 = 0;"
> +	"r2 = 0;"
> +	"call %[bpf_rdonly_cast];"
> +	"r1 = 0;"
> +	"r1 += r0;"

[Severity: Critical]
This is a pre-existing issue, but the verifier incorrectly allows scalar
operations like subtraction, bitwise AND, and XOR with untrusted pointers,
leading to arbitrary kernel memory read/write vulnerabilities.

In kernel/bpf/verifier.c:adjust_reg_min_max_vals(), the check explicitly
tests for addition:

	if (opcode == BPF_ADD &&
	    base_type(src_reg->type) == PTR_TO_MEM &&
	    (src_reg->type & PTR_UNTRUSTED)) {
		verbose(env, "R%d tried to add from %s to scalar\n", ...);

For other ALU operations, the verifier delegates to
adjust_ptr_min_max_vals(), which returns early:

	if (base_type(ptr_reg->type) == PTR_TO_MEM &&
	    (ptr_reg->type & PTR_UNTRUSTED))
		return 0;

This early return bypasses all opcode validation and leaves the destination
register unmodified as a scalar value with its original bounds. At runtime,
the register receives the result of the pointer arithmetic and can be used
to bypass bounds checks.

Could the verifier be updated to properly reject all invalid ALU operations
on untrusted pointers?

[Severity: Low]
Is it possible to expand this selftest to cover other ALU operations?

Because the verifier code currently ignores operations other than addition,
adding tests like scalar_sub_not_ok or scalar_xor_not_ok would ensure these
operations are also correctly rejected and expose the missing verifier checks.

> +	"r0 = 0;"
> +	"exit;"
> +	:
> +	: __imm(bpf_rdonly_cast)
> +	: __clobber_all);
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260609-f01-03-scalar-add-bpf-next-v1-0-e6212e274155@mails.tsinghua.edu.cn?part=2

      reply	other threads:[~2026-06-09 15:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-09 14:55 [PATCH bpf-next 0/2] bpf: Reject scalar addition from untrusted memory Nuoqi Gui
2026-06-09 14:55 ` [PATCH bpf-next 1/2] " Nuoqi Gui
2026-06-09 15:10   ` sashiko-bot
2026-06-09 15:28   ` bot+bpf-ci
2026-06-09 17:01   ` Eduard Zingerman
2026-06-09 18:21     ` Emil Tsalapatis
2026-06-10 12:16       ` Nuoqi Gui
2026-06-09 14:55 ` [PATCH bpf-next 2/2] selftests/bpf: Cover scalar addition from rdonly " Nuoqi Gui
2026-06-09 15:08   ` 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=20260609150813.387981F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=gnq25@mails.tsinghua.edu.cn \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.