BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Amery Hung" <ameryhung@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 20/23] selftests/bpf: Test kfunc packet memory direct writes
Date: Fri, 11 Sep 2026 22:36:24 +0000	[thread overview]
Message-ID: <20260911223624.CB4321F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260911220415.1396439-21-ameryhung@gmail.com>

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

Pre-existing issues:
- [High] Passing packet data to a kfunc expecting a struct causes unaligned memory accesses, which panics the kernel on strict-alignment architectures.
- [Medium] The verifier unconditionally treats all `ARG_PTR_TO_MEM` kfunc arguments as writable, causing unnecessary packet unsharing (cloning) for read-only kfuncs.
--

commit 4fcd6afbde5f30a90a43ed9c7692746d4f5d5555
Author: Amery Hung <ameryhung@gmail.com>

selftests/bpf: Test kfunc packet memory direct writes

A kfunc memory argument may be backed by packet data. Exercise this
with bpf_skb_ct_lookup(), which writes errors through its opts memory
argument. Verify that the verifier marks the program as directly
writing packet data, causing TC to emit its writable-skb prologue.

> diff --git a/tools/testing/selftests/bpf/progs/verifier_kfunc_packet_access.c b/tools/testing/selftests/bpf/progs/verifier_kfunc_packet_access.c
> new file mode 100644
> index 0000000000000..88009566d92f9
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/verifier_kfunc_packet_access.c
[ ... ]
> +int kfunc_packet_write(struct __sk_buff *skb)
> +{
> +	void *data_end = (void *)(long)skb->data_end;
> +	void *data = (void *)(long)skb->data;
> +	struct bpf_sock_tuple tuple = {};
> +	struct nf_conn *nfct;
> +
> +	if (data + sizeof(struct bpf_ct_opts) > data_end)
> +		return 0;
> +
> +	/* An invalid tuple size makes bpf_skb_ct_lookup() write opts->error. */
> +	nfct = bpf_skb_ct_lookup(skb, &tuple, 1, data, sizeof(struct bpf_ct_opts));

[Severity: High]
This is a pre-existing issue, but will this test cause a kernel panic on
strict-alignment architectures?

The verifier's get_kfunc_arg_type() assigns ARG_PTR_TO_MEM | MEM_FIXED_SIZE
for kfunc struct arguments but fails to set MEM_ALIGNED. This allows BPF
programs to pass unaligned pointers like skb->data to kfuncs expecting structs.

When this selftest intentionally passes data as the opts parameter, the kfunc's
underlying C code in __bpf_nf_ct_lookup() dereferences the struct fields
directly (e.g., READ_ONCE(opts->netns_id)). On strict-alignment architectures
like SPARC or older ARM, this unaligned 32-bit read causes a hardware exception
and panics the kernel. Executing this selftest acts as a concrete panic vector.

[Severity: Medium]
This is a pre-existing issue, but does the verifier unconditionally treat all
ARG_PTR_TO_MEM kfunc arguments as writable?

While this selftest verifies the writable-skb prologue, the verifier's
get_kfunc_arg_type() never sets MEM_RDONLY based on the BTF const modifier.
Additionally, check_func_arg() forcefully applies BPF_READ | BPF_WRITE via
the if (meta->btf) condition.

Consequently, any memory argument passed to a kfunc is treated as writable.
When packet data (PTR_TO_PACKET) is passed to a purely read-only kfunc, the
verifier sets env->seen_direct_write = true, unnecessarily triggering the
bpf_unclone_prologue(). This forces a packet reallocation and copy for every
packet at runtime, degrading performance.

> +	if (nfct)
> +		bpf_ct_release(nfct);
> +	return 0;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260911220415.1396439-1-ameryhung@gmail.com?part=20

  reply	other threads:[~2026-09-11 22:36 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-11 22:03 [PATCH bpf-next v2 00/23] Unify helper and kfunc argument checks Amery Hung
2026-09-11 22:03 ` [PATCH bpf-next v2 01/23] bpf: Pass call metadata through shared " Amery Hung
2026-09-11 22:03 ` [PATCH bpf-next v2 02/23] bpf: Address check_func_arg() arguments by argno Amery Hung
2026-09-11 22:03 ` [PATCH bpf-next v2 03/23] bpf: Only compare func_id against BPF_FUNC_* for helper calls Amery Hung
2026-09-11 22:03 ` [PATCH bpf-next v2 04/23] bpf: Only compare func_id against kfunc BTF IDs for kfunc calls Amery Hung
2026-09-11 22:03 ` [PATCH bpf-next v2 05/23] bpf: Clarify unused and scalar function argument types Amery Hung
2026-09-11 22:19   ` sashiko-bot
2026-09-11 22:03 ` [PATCH bpf-next v2 06/23] bpf: Unify kfunc argument kinds with enum bpf_arg_type Amery Hung
2026-09-11 22:26   ` sashiko-bot
2026-09-11 22:03 ` [PATCH bpf-next v2 07/23] bpf: Classify kfunc arguments the verifier ignores Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 08/23] bpf: Align helper and kfunc ARG_PTR_TO_PROG_AUX handling Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 09/23] bpf: Set OBJ_RELEASE when generating kfunc argument types Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 10/23] bpf: Set MEM_UNINIT and dynptr subtypes when generating kfunc arg types Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 11/23] bpf: Set MEM_RCU when generating kfunc argument types Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 12/23] bpf: Resolve BTF ID of ARG_PTR_TO_BTF_ID in kfunc bpf_func_proto Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 13/23] bpf: Resolve ARG_PTR_TO_MEM | MEM_FIXED_SIZE size " Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 14/23] bpf: Consolidate runtime argument type resolution Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 15/23] bpf: Consolidate nullable argument validation Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 16/23] bpf: Drop redundant BTF pointer helper write rejection Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 17/23] bpf: Consolidate helper and kfunc PTR_TO_BTF_ID argument matching Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 18/23] bpf: Admit kfunc argument registers through check_reg_type() Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 19/23] bpf: Consolidate function call pkt_access validation Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 20/23] selftests/bpf: Test kfunc packet memory direct writes Amery Hung
2026-09-11 22:36   ` sashiko-bot [this message]
2026-09-11 22:04 ` [PATCH bpf-next v2 21/23] bpf: Consolidate release argument validation Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 22/23] selftests/bpf: Test nullable per-CPU kptr identity after exchange Amery Hung
2026-09-11 22:04 ` [PATCH bpf-next v2 23/23] bpf: Check helper and kfunc arguments in one path Amery Hung
2026-09-12  3:20 ` [PATCH bpf-next v2 00/23] Unify helper and kfunc argument checks patchwork-bot+netdevbpf

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=20260911223624.CB4321F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ameryhung@gmail.com \
    --cc=bpf@vger.kernel.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