mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>,
	ast@kernel.org,  daniel@iogearbox.net, andrii@kernel.org
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	Kumar Kartikeya Dwivedi	 <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	Yonghong Song	 <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>, Shuah Khan	 <shuah@kernel.org>,
	Shenghao Yuan <shenghaoyuan0928@163.com>,
	Yazhou Tang	 <tangyazhou518@outlook.com>,
	Matt Bobrowski <mattbobrowski@google.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
		linux-kselftest@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: add tests for PTR_TO_FLOW_KEYS constant offset bounds
Date: Thu, 04 Jun 2026 19:23:41 -0700	[thread overview]
Message-ID: <3a45b9648a59651ce4206e839050d2f3d76325cd.camel@gmail.com> (raw)
In-Reply-To: <20260604180730.2518088-3-gnq25@mails.tsinghua.edu.cn>

On Fri, 2026-06-05 at 02:07 +0800, Nuoqi Gui wrote:
> Add verifier tests covering constant pointer arithmetic on a
> PTR_TO_FLOW_KEYS register, which regressed with commit 022ac0750883
> ("bpf: use reg->var_off instead of reg->off for pointers"): an
> out-of-bounds offset introduced as flow_keys += K and then dereferenced
> at insn->off 0 was accepted, while the equivalent flow_keys + K direct
> offset was rejected.
> 
> The tests check that:
>  - in-bounds constant arithmetic on the keys pointer is still accepted,
>  - an out-of-bounds offset introduced via constant arithmetic is rejected
>    for both read and write, with the same diagnostic as the direct
>    insn->off form.
> 
> Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn>
> ---
>  .../selftests/bpf/prog_tests/verifier.c       |  2 +
>  .../selftests/bpf/progs/verifier_flow_keys.c  | 77 +++++++++++++++++++
>  2 files changed, 79 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/verifier_flow_keys.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
> index 219ff2969868..dae26dda3782 100644
> --- a/tools/testing/selftests/bpf/prog_tests/verifier.c
> +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
> @@ -38,6 +38,7 @@
>  #include "verifier_div0.skel.h"
>  #include "verifier_div_mod_bounds.skel.h"
>  #include "verifier_div_overflow.skel.h"
> +#include "verifier_flow_keys.skel.h"
>  #include "verifier_global_subprogs.skel.h"
>  #include "verifier_global_ptr_args.skel.h"
>  #include "verifier_gotol.skel.h"
> @@ -189,6 +190,7 @@ void test_verifier_direct_stack_access_wraparound(void) { RUN(verifier_direct_st
>  void test_verifier_div0(void)                 { RUN(verifier_div0); }
>  void test_verifier_div_mod_bounds(void)       { RUN(verifier_div_mod_bounds); }
>  void test_verifier_div_overflow(void)         { RUN(verifier_div_overflow); }
> +void test_verifier_flow_keys(void)            { RUN(verifier_flow_keys); }
>  void test_verifier_global_subprogs(void)      { RUN(verifier_global_subprogs); }
>  void test_verifier_global_ptr_args(void)      { RUN(verifier_global_ptr_args); }
>  void test_verifier_gotol(void)                { RUN(verifier_gotol); }
> diff --git a/tools/testing/selftests/bpf/progs/verifier_flow_keys.c b/tools/testing/selftests/bpf/progs/verifier_flow_keys.c
> new file mode 100644
> index 000000000000..512e5d1d2665
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/verifier_flow_keys.c
> @@ -0,0 +1,77 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Constant-offset bounds checks for PTR_TO_FLOW_KEYS pointer arithmetic. */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include "bpf_misc.h"
> +
> +/* sizeof(struct bpf_flow_keys) is well under 4096, so +0x1000 is OOB. */
> +
> +SEC("flow_dissector")
> +__description("flow_keys: in-bounds constant pointer arithmetic accepted")
> +__success
> +__naked void flow_keys_const_inbounds(void)
> +{
> +	asm volatile ("					\
> +	r1 = *(u64 *)(r1 + %[flow_keys]);		\
> +	r1 += 8;					\
> +	r0 = *(u64 *)(r1 + 0);				\
> +	r0 = 0;						\
> +	exit;						\
> +"	:
> +	: __imm_const(flow_keys, offsetof(struct __sk_buff, flow_keys))
> +	: __clobber_all);
> +}
> +
> +SEC("flow_dissector")
> +__description("flow_keys: OOB via constant pointer arithmetic rejected")
> +__failure __msg("invalid access to flow keys off=4096 size=8")
> +__naked void flow_keys_const_oob_read(void)
> +{
> +	asm volatile ("					\
> +	r1 = *(u64 *)(r1 + %[flow_keys]);		\
> +	r1 += 4096;					\
> +	r0 = *(u64 *)(r1 + 0);				\
> +	r0 = 0;						\
> +	exit;						\
> +"	:
> +	: __imm_const(flow_keys, offsetof(struct __sk_buff, flow_keys))
> +	: __clobber_all);
> +}
> +
> +SEC("flow_dissector")
> +__description("flow_keys: OOB write via constant pointer arithmetic rejected")
> +__failure __msg("invalid access to flow keys off=4096 size=8")
> +__naked void flow_keys_const_oob_write(void)
> +{
> +	asm volatile ("					\
> +	r1 = *(u64 *)(r1 + %[flow_keys]);		\
> +	r1 += 4096;					\
> +	r2 = 0;						\
> +	*(u64 *)(r1 + 0) = r2;				\
> +	r0 = 0;						\
> +	exit;						\
> +"	:
> +	: __imm_const(flow_keys, offsetof(struct __sk_buff, flow_keys))
> +	: __clobber_all);
> +}
> +
> +/* Equivalent OOB expressed directly in insn->off; this form was always
> + * rejected and is kept to show both forms now share one diagnostic.
> + */
> +SEC("flow_dissector")
> +__description("flow_keys: OOB via insn->off rejected")
> +__failure __msg("invalid access to flow keys off=4096 size=8")
> +__naked void flow_keys_insn_off_oob(void)
> +{
> +	asm volatile ("					\
> +	r1 = *(u64 *)(r1 + %[flow_keys]);		\
> +	r0 = *(u64 *)(r1 + 4096);			\
> +	r0 = 0;						\
> +	exit;						\
> +"	:
> +	: __imm_const(flow_keys, offsetof(struct __sk_buff, flow_keys))
> +	: __clobber_all);
> +}
> +
> +char _license[] SEC("license") = "GPL";

Could you please also add a test with a truly varying offset?
Like below:

__naked void flow_keys_var_read(void)
{
	asm volatile ("					\
	r6 = r1;					\
	call %[bpf_get_prandom_u32];			\
	r0 &= 0xFFFF;					\
	r1 = *(u64 *)(r6 + %[flow_keys]);		\
	r1 += r0;					\
	r0 = *(u64 *)(r1 + 0);				\
	r0 = 0;						\
	exit;						\
"	:
	: __imm_const(flow_keys, offsetof(struct __sk_buff, flow_keys)),
	  __imm(bpf_get_prandom_u32)
	: __clobber_all);
}

  parent reply	other threads:[~2026-06-05  2:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260604180730.2518088-1-gnq25@mails.tsinghua.edu.cn>
2026-06-04 18:07 ` [PATCH bpf-next v2 1/2] bpf, verifier: fold reg->var_off into PTR_TO_FLOW_KEYS bounds check Nuoqi Gui
2026-06-04 18:45   ` bot+bpf-ci
2026-06-05  2:22   ` Eduard Zingerman
2026-06-05  4:36     ` gnq25
2026-06-04 18:07 ` [PATCH bpf-next v2 2/2] selftests/bpf: add tests for PTR_TO_FLOW_KEYS constant offset bounds Nuoqi Gui
2026-06-04 18:45   ` bot+bpf-ci
2026-06-05  2:23   ` Eduard Zingerman [this message]
2026-06-05  4:39     ` gnq25

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=3a45b9648a59651ce4206e839050d2f3d76325cd.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=emil@etsalapatis.com \
    --cc=gnq25@mails.tsinghua.edu.cn \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mattbobrowski@google.com \
    --cc=memxor@gmail.com \
    --cc=shenghaoyuan0928@163.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=tangyazhou518@outlook.com \
    --cc=yonghong.song@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

all inboxes | Powered by JetHome®