BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <almagui@kernel.org>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org
Cc: andrey.grodzovsky@crowdstrike.com, eddyz87@gmail.com,
	memxor@gmail.com, martin.lau@linux.dev, song@kernel.org,
	yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com,
	ihor.solodrai@linux.dev, john.fastabend@gmail.com,
	mason@kernel.org, bpf@vger.kernel.org,
	Alan Maguire <almagui@kernel.org>
Subject: [PATCH v2 bpf-next 1/5] bpf: Allow loads of the low-order bytes of context slots on big-endian
Date: Wed, 25 Nov 2026 16:58:41 +0000	[thread overview]
Message-ID: <20261125165845.45806-2-almagui@kernel.org> (raw)
In-Reply-To: <20261125165845.45806-1-almagui@kernel.org>

The context of LSM, struct_ops and tracing programs other than iterators
is an array of u64 slots, and arguments and return values smaller than
8 bytes are in the low-order bytes of theirs. btf_ctx_access() only
allows loads at the start of a slot: on little-endian, a load of fewer
than 8 bytes there reads the argument (truncated to the size of the
load), but on big-endian, it reads the high-order bytes instead, and the
argument can't be loaded on its own.

On big-endian, also allow loads of part of a slot that end at its end,
i.e. that read its low-order bytes. Such a load reads the same bytes as
a load of the same size at the start of the slot does on little-endian,
so the bounds the verifier assumes for it, such as the range of the
return value of LSM programs, which is kept in the 32-bit bounds of
4-byte loads, hold. Loads at other offsets within a slot stay rejected,
as these bounds wouldn't hold for them, and pointers can still only be
loaded as a whole.

Loads at the start of a slot stay allowed, so that no program that loads
today is rejected, and little-endian is unaffected. Iterators are
excluded: their context is a struct whose fields, such as the uid of
struct bpf_iter__tcp, are at the start of their 8 bytes, followed by
padding. As struct_ops programs keep the index of their member in
expected_attach_type, the program type is checked too.

This lets programs that describe their context with a struct, such as
struct bpf_func_args___<flavor>, which places such arguments at the end
of their slot on big-endian, read them there.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Assisted-by: LLM
Signed-off-by: Alan Maguire <almagui@kernel.org>
---
 kernel/bpf/btf.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9a1ad4..04f12225ec08 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7503,10 +7503,22 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	u32 nr_args, arg;
 	int i, ret;
 
+	/*
+	 * Loads start at an 8-byte boundary: the start of a u64 slot, or of a
+	 * field of an iterator's context struct. Arguments and return values
+	 * smaller than 8 bytes are in the low-order bytes of their slot, which
+	 * on big-endian are at its end, so loads ending there are allowed too,
+	 * except for iterators, whose fields are followed by padding.
+	 */
 	if (off % 8) {
-		bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
-			tname, off);
-		return false;
+		bool iter = prog->type == BPF_PROG_TYPE_TRACING &&
+			    prog->expected_attach_type == BPF_TRACE_ITER;
+
+		if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) || iter || off % 8 != 8 - size) {
+			bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
+				tname, off);
+			return false;
+		}
 	}
 	arg = btf_ctx_arg_idx(btf, t, off);
 	args = (const struct btf_param *)(t + 1);
-- 
2.56.0


  reply	other threads:[~2026-11-25 16:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 16:58 [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-25 16:58 ` Alan Maguire [this message]
2026-11-25 16:58 ` [PATCH v2 bpf-next 2/5] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-25 17:16   ` sashiko-bot
2026-11-25 16:58 ` [PATCH v2 bpf-next 3/5] bpf: " Alan Maguire
2026-11-25 16:58 ` [PATCH v2 bpf-next 4/5] selftests/bpf: Test " Alan Maguire
2026-11-25 16:58 ` [PATCH v2 bpf-next 5/5] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
2026-11-25 17:37   ` bot+bpf-ci
2026-11-25 22:49 ` [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Andrii Nakryiko
2026-11-26  4:02   ` Alexei Starovoitov

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=20261125165845.45806-2-almagui@kernel.org \
    --to=almagui@kernel.org \
    --cc=andrey.grodzovsky@crowdstrike.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --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