From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Amery Hung <ameryhung@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>, Tejun Heo <tj@kernel.org>,
kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf-next v4 7/8] bpf: Support multiple __uninit kfunc output arguments
Date: Fri, 18 Sep 2026 07:29:01 +0200 [thread overview]
Message-ID: <20260918052906.12226-8-memxor@gmail.com> (raw)
In-Reply-To: <20260918052906.12226-1-memxor@gmail.com>
A single output descriptor cannot retain the initialization ranges of two
__uninit memory arguments. Track raw-mode eligibility and the definite
output size separately for each ABI slot, so a variable-size output leaves
independent constant-size outputs intact.
Use the shared argument-checking path to record outputs for both helpers
and kfuncs. With per-slot tracking, neither needs the single-output
prototype restriction. Initialize every recorded output after checking all
arguments, skipping empty slots before looking up their register state.
Use the resolved BTF parameter when looking up __uninit for stack liveness.
A preceding by-value parameter can consume multiple ABI slots, so its slot
number cannot index the BTF parameter array.
Suggested-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
include/linux/bpf_verifier.h | 10 ++---
kernel/bpf/verifier.c | 79 +++++++++++-------------------------
2 files changed, 28 insertions(+), 61 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 6ff1c227d298..9ddbb20ec1e9 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1547,13 +1547,13 @@ struct ref_obj_desc {
};
/*
- * A memory argument a call fills in. The verifier allows the stack to be uninitialized if
- * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access()
- * after all arguments have been checked.
+ * Memory arguments a call fills in, indexed by argument slot. The verifier allows the
+ * stack to be uninitialized if the range is a known constant. Stack slots are marked as
+ * STACK_MISC by check_mem_access() after all arguments have been checked.
*/
struct arg_raw_mem_desc {
- u8 argno; /* One-based ABI argument slot; zero means no output. */
- int size;
+ u16 mask;
+ int size[MAX_BPF_FUNC_ARGS];
};
/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d327b5356d53..4ba7929f6d7e 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6987,7 +6987,9 @@ static int check_stack_range_initialized(
*/
bool allow_poison = access_size < 0 || clobber;
/* The call will initialize the memory; uninitialized stack allowed */
- bool raw_mode = meta && meta->arg_raw_mem.argno == arg_slot_from_argno(argno) + 1;
+ u32 arg_slot = arg_slot_from_argno(argno);
+ bool raw_mode = meta && arg_slot < MAX_BPF_FUNC_ARGS &&
+ (meta->arg_raw_mem.mask & BIT(arg_slot));
access_size = abs(access_size);
@@ -7029,7 +7031,7 @@ static int check_stack_range_initialized(
}
if (raw_mode) {
- meta->arg_raw_mem.size = access_size;
+ meta->arg_raw_mem.size[arg_slot] = access_size;
return 0;
}
@@ -7221,9 +7223,8 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,
* raw mode so that the program is required to initialize all
* the memory that the helper could just partially fill up.
*/
- if (!tnum_is_const(size_reg->var_off) &&
- meta->arg_raw_mem.argno == arg_slot_from_argno(mem_argno) + 1)
- meta->arg_raw_mem.argno = 0;
+ if (!tnum_is_const(size_reg->var_off))
+ meta->arg_raw_mem.mask &= ~BIT(arg_slot_from_argno(mem_argno));
if (reg_smin(size_reg) < 0) {
verbose(env, "%s min value is negative, either use unsigned or 'var &= const'\n",
@@ -8158,21 +8159,6 @@ static bool arg_type_is_mem_size(enum bpf_arg_type type)
return type == ARG_MEM_SIZE || type == ARG_MEM_SIZE_OR_ZERO;
}
-static bool arg_type_is_raw_mem(enum bpf_arg_type type)
-{
- /*
- * A map value output buffer (e.g. bpf_map_pop_elem) is also a raw
- * (uninitialized) memory argument, and like ARG_PTR_TO_MEM it may be
- * passed as a PTR_TO_STACK that reaches check_stack_range_initialized().
- * A kfunc's struct pointer remains ARG_PTR_TO_BTF_ID until call argument
- * checking resolves it to generic memory, so include it in proto validation.
- */
- return (base_type(type) == ARG_PTR_TO_MEM ||
- base_type(type) == ARG_PTR_TO_MAP_VALUE ||
- base_type(type) == ARG_PTR_TO_BTF_ID) &&
- type & MEM_UNINIT;
-}
-
static bool arg_type_is_release(enum bpf_arg_type type)
{
return type & OBJ_RELEASE;
@@ -8951,7 +8937,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 p
if ((arg_type & MEM_UNINIT) &&
(base_type(arg_type) == ARG_PTR_TO_MEM ||
base_type(arg_type) == ARG_PTR_TO_MAP_VALUE))
- meta->arg_raw_mem.argno = slot + 1;
+ meta->arg_raw_mem.mask |= BIT(slot);
if (bpf_register_is_null(reg) && type_may_be_null(arg_type)) {
err = mark_arg_precision(env, argno);
@@ -9566,24 +9552,28 @@ static int mark_raw_stack(struct bpf_verifier_env *env, struct bpf_call_arg_meta
int insn_idx)
{
struct bpf_func_state *caller = cur_func(env);
- struct bpf_reg_state *reg;
- u32 slot = meta->arg_raw_mem.argno - 1;
+ u32 slot;
int i, err;
- if (!meta->arg_raw_mem.size)
- return 0;
- reg = get_func_arg_reg(caller, cur_regs(env), slot);
-
/*
* Validate every argument before initializing outputs: an input argument
* may alias an output buffer. Use the normal stack-write checks to discard
* stale spills and preserve the rules for special stack objects.
*/
- for (i = 0; i < meta->arg_raw_mem.size; i++) {
- err = check_mem_access(env, insn_idx, reg, argno_from_arg(slot + 1), i, BPF_B,
- BPF_WRITE, -1, false, false);
- if (err)
- return err;
+ for (slot = 0; slot < MAX_BPF_FUNC_ARGS; slot++) {
+ struct bpf_reg_state *reg;
+ argno_t argno = argno_from_arg(slot + 1);
+
+ if (!meta->arg_raw_mem.size[slot])
+ continue;
+ reg = get_func_arg_reg(caller, cur_regs(env), slot);
+
+ for (i = 0; i < meta->arg_raw_mem.size[slot]; i++) {
+ err = check_mem_access(env, insn_idx, reg, argno, i, BPF_B,
+ BPF_WRITE, -1, false, false);
+ if (err)
+ return err;
+ }
}
return 0;
@@ -9881,24 +9871,6 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
return -EINVAL;
}
-static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
-{
- bool seen = false;
- int i;
-
- for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) {
- if (fn->arg_type[i] == ARG_UNUSED)
- break;
- if (!arg_type_is_raw_mem(fn->arg_type[i]))
- continue;
- if (seen)
- return false;
- seen = true;
- }
-
- return true;
-}
-
static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg)
{
bool is_fixed = fn->arg_type[arg] & MEM_FIXED_SIZE;
@@ -10025,7 +9997,6 @@ static int check_func_proto(struct bpf_verifier_env *env, const struct bpf_func_
struct bpf_call_arg_meta *meta)
{
return check_arg_prog_aux(env, fn) &&
- check_raw_mode_ok(fn) &&
check_arg_pair_ok(fn) &&
check_mem_arg_rw_flag_ok(fn) &&
check_proto_release_reg(fn, meta) &&
@@ -13097,10 +13068,6 @@ static int gen_kfunc_arg_proto(struct bpf_verifier_env *env, struct bpf_call_arg
return -ENOTSUPP;
}
- if (!check_raw_mode_ok(proto)) {
- verbose(env, "multiple __uninit buffers are not supported\n");
- return -EINVAL;
- }
return check_arg_prog_aux(env, proto) ? 0 : -EINVAL;
}
@@ -13897,7 +13864,7 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *
/* KF_ITER_NEW kfuncs initialize the iterator state at arg 0 */
if (arg == 0 && meta.kfunc_flags & KF_ITER_NEW)
return -size;
- if (is_kfunc_arg_uninit(btf, &args[arg]))
+ if (is_kfunc_arg_uninit(btf, &args[i]))
return -size;
return size;
}
--
2.53.0
next prev parent reply other threads:[~2026-09-18 5:29 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 5:28 [PATCH bpf-next v4 0/8] Fix generic __uninit kfunc output buffers Kumar Kartikeya Dwivedi
2026-09-18 5:28 ` [PATCH bpf-next v4 1/8] selftests/bpf: Allow privileged preparation for capability tests Kumar Kartikeya Dwivedi
2026-09-18 5:28 ` [PATCH bpf-next v4 2/8] bpf: Record raw memory arguments during argument checking Kumar Kartikeya Dwivedi
2026-09-18 6:35 ` bot+bpf-ci
2026-09-18 16:24 ` Eduard Zingerman
2026-09-18 5:28 ` [PATCH bpf-next v4 3/8] bpf: Check read access for initialized writable memory arguments Kumar Kartikeya Dwivedi
2026-09-18 5:50 ` sashiko-bot
2026-09-18 17:46 ` Eduard Zingerman
2026-09-18 18:01 ` Amery Hung
2026-09-18 5:28 ` [PATCH bpf-next v4 4/8] selftests/bpf: Cover helper memory access permissions Kumar Kartikeya Dwivedi
2026-09-18 6:17 ` bot+bpf-ci
2026-09-18 5:28 ` [PATCH bpf-next v4 5/8] bpf: Fix generic __uninit kfunc output buffers Kumar Kartikeya Dwivedi
2026-09-18 6:35 ` bot+bpf-ci
2026-09-18 17:50 ` Eduard Zingerman
2026-09-18 5:29 ` [PATCH bpf-next v4 6/8] selftests/bpf: Cover generic __uninit output initialization Kumar Kartikeya Dwivedi
2026-09-18 5:29 ` Kumar Kartikeya Dwivedi [this message]
2026-09-18 5:29 ` [PATCH bpf-next v4 8/8] selftests/bpf: Cover __uninit kfunc output argument slots Kumar Kartikeya Dwivedi
2026-09-18 6:35 ` bot+bpf-ci
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=20260918052906.12226-8-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=ameryhung@gmail.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=kernel-team@meta.com \
--cc=kkd@meta.com \
--cc=tj@kernel.org \
/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