Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.74 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100018; b=1HWVCxi7n/prDamWBRvLTRyV2h4krL6onvGOwvGLQzJ9ZLiJHYw4gHfA3atuT1ju4v1s 2gpAXKUSZy+ssL905SgTQWYsiCkI8VmExLLLYk4A+1Nt919D5dH+F6m2d83AQuYxmJ53n LRId9qYJ7B3DUT8e38qy4IrpZQliEKsoUF4Fuqhot5+JoTJ8QeapgURgy/FgswZgmTJFa 82fHNx97fsaF8h5DutZNiZ5BRNvOnA77bCAU7W+UNrxG+X/WnvDLvA8eyvRwctikmQpKW k+TQpDhuit+8iqjJuzUZGovcRfla2J4J4aidL0zmhd0tUiRJEGMyjBP+pwTXqpqxBLA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100018; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=7sReh3VxtY2wHUyg3LOwzJJ5HqyE/k+Ri4qgkeqHgxQ=; b=WUfStbGDElEQWUy5+26rVybiW0h8+euhjRprApsBHg1V42rzU/wJCaxsM8cHoeywYvFb lAhANFgJhGLTdjuzZF6Q0cZawT93VyZrDQ6rSCVyGgUDbE6rwZq9kS0j8oPTtVe32vamy YBhPTb9oNAWEtNVNeCCemLix23fJ6ObovUQNX3ggSqX6RzHmIYLIZeFQAvJuProh9pNsd iEWMde3UCC3SL7MYjZXjbtyxdd1ljNCJTD1EDSGlxoGmtr5H3idx2N3WPCXG3I1bMp2EA SvojHZ3S8H3G1o5VpeeSf/Eoy2MEjRJ72LzRlwHDUyueihZPfUm65kz4qisWXngK7DA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=vmTfdyge; arc=none smtp.remote-ip=74.125.231.74 Received: from mail-oa2-f10.google.com (mail-oa2-f10.google.com [74.125.231.74]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 589B9606A31A for ; Tue, 1 Dec 2026 04:40:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 589B9606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=vmTfdyge Received: by mail-oa2-f10.google.com with SMTP id 586e51a60fabf-4ae5a7c2df9so8537327fac.1 for ; Mon, 30 Nov 2026 20:40:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100016; x=1796704816; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7sReh3VxtY2wHUyg3LOwzJJ5HqyE/k+Ri4qgkeqHgxQ=; b=vmTfdygeTana/IcCQb3tTt23PW9QdKpGkDoSLrYhiTChnzPop5p3iWD51DDIoGDUce jDjYMf2BPZoQBEBtYJ9f+eKp9CT++aKTDlXYvnVMEf57lwhmAGSDPYkPok29p47GQBSR +pCOyxvH1X2cUihIiR76U+aLag9tRgmAaYXWZ+grGgADbW4JQ9S7DQbHDBX/fnIWNG7w T1BAxJj7UeOX1HRKsEsRYz9/X9kZTDIKsc5vEagTMdv4LwEMrQl0W7zw12tgoTAzFm8a z4Z8hG1j7GLRXN7DJLTbDgxaeQRPfMLXancap9D5NWtBganRi3hK7E2Lc/Y5ixsIQ/zw NUXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100016; x=1796704816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=7sReh3VxtY2wHUyg3LOwzJJ5HqyE/k+Ri4qgkeqHgxQ=; b=iEUrEFHVkxtFvpFckz8SUGobVZsEIb//67MALvtvRgdivfth0vNn2WIrvDwWWVrUFU nVni8oiIZ2YsaFFhxtOwP+YrmcZN16kxmr3KjnESWDJCtgg5aAyHAtcMpsNXOzeGQZ53 yRrnGrvayCQS5LG9YevuLz6Ppa2Hd9cNY1pzs2i0xU1d3Qr+LBuhK9TVf0sLBYRvqD2C AHJGDnrvLdvO7N7jA24cD+waMzaYBc1HzlrcIvqEqXAF+G4PSdvuhl+V37BnE4h1N50s +URaybVfp3mQsIXPqJMqJGVTeTGMiBzDx4r2ehp1KwK2W1B1+e2DyWg302Uj888V7nIm tY1A== X-Gm-Message-State: APuU4Z+Oal86/QxsvsHI4VLUZ18qJxWjwRWwEOsMeKzupWUHM4ZWpbNy 2gZtitNbs0jEYp0pGE3AFMe8IirEZfCmpH7CbqaIEIGQ85KnJhaT8W1k0OPc+xrcB5k= X-Gm-Gg: AZFdTZc4mTTJXKsXzjU4sbtwSfj02Rgfb6xVLrXggfjMgpOVRZnFwDsDunSiGtKU1OC 0fbV8Eb/r+0hfuhzVCksTksNnj+Qgr1De8uFtagz6ruiYxvAGw5UxcQkpGK7/eANnQGVcS4Eber luas8icU2EAdHc6pvwrddG32aa20bUTTYzcyO1BwiUZoaQsY0i4YcY6vhg2967XdQORrEBjNpRA 1wtTKLJt0+5Q62pjOGLl1Im4/eAml1ABQiknFQUoIy/3HTYg4Bi8SM8xEB8DuS75aFurUZm6+UT Pfvg02aaFhdVG5Ekd+eW+FB097kKbLe77W6IJAEFyxStcGK797/epnC3GMKYxN7Ldhuv6YtuSCb xS8SzU3AyYVY6e/6aCXJBYrVP7xsFRT/a/Oz4eW8i3MuOXxbVouI9h0WRutxOXYEtvRSVOWnl1a SmI2QrhvJgO7kLIL4A1YwragYcw/VaoQX2WSqYyCNEBGh3s+mooOTanB8xkGbgAbU5LCzWoj0GF aGgbD15EYun892XNeHoMdiHTN8cOBKVL3ZDCT6LWLGaGeifiy8IiaXs2vj5adxkAeRh/vBsopVz q24peBuHaxlosqYYU3VoBrAoGgkyAEjv0z9OOZJCdgI7J6a5GH8S7Ee+voSoMoEiHQHuftpBN9M xlUAruub4DLYCCXvYMVw8Ey04zeOrfAgpH0Fm X-Received: by 2002:a05:6820:1391:b0:71b:8969:9d1d with SMTP id 006d021491bc7-71b8969a594mr15196834eaf.31.1796100016062; Mon, 30 Nov 2026 20:40:16 -0800 (PST) Received: from localhost ([2a03:2880:10ff:53::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380bdff9sm24977676eaf.11.2026.11.30.20.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:40:14 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 07/73] bpf: Let a callback unwind out of a helper that has an unwind label Date: Tue, 1 Dec 2026 05:38:34 +0100 Message-ID: X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=22433; h=from:subject:message-id; bh=RqTkKb7fBtuWKCgxckc8p6I/3YuMGqvuE4uLHyN9Gpk=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb3a1QpZcF8NXRhk5k6f+9m+6lv31fDjzHZvRBk+7GsuN X2s6SlkYxLgYZMUUWUr+72MyPlH5O9B2GTfMHFYmkCEMXJwCMBHpBEaGbws7nAq3xhb2Lp4uMdPkma /YhMmLZtTFOm85xhTA3C9TyPDfb9OPh5tLt5SHBc4P2vf3vqrd84mnM5ZOSBLJq+FJ2pvHBAA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the verifier rejects a callback that reaches unwind_exit with "callback cannot unwind". A helper that calls its callback with bpf_call_cb() can now pass the unwinding on: it leaves from its unwind label to the unwind_goto after its own call, with the R0 of the callback. Treat the call of a helper or kfunc whose callback can unwind like the call of a subprog that can unwind: unwind_goto may follow the call, and it has to follow when the callback can unwind. Here cb is the callback of bpf_loop(): call bpf_loop <- 1. enters cb 3. visited again unwind_goto +1 <- 4. taken exit r0 += 41 <- R0 is 5 here exit cb: r0 = 5 unwind_exit <- 2. back to the call, marked unwound The verifier models the call of a helper with a callback as a loop: each time the callback exits, the state goes back to the call insn. A state that leaves the callback with unwind_exit goes back too, marked state->unwound, because the helper has effects of its own on its way out: bpf_rbtree_add(), for example, takes the node from the program whichever way it leaves. On this second visit the call starts no further iteration, applies the effects of the helper and keeps R0 as the callback left it. The marked state then takes the unwind_goto. check_cb_unwind() checks the call where the verifier enters a callback that can unwind, in this order: - No unwind_goto follows the call: "no unwind_goto after the call of bpf_loop()", -EINVAL. - The kernel has no unwind labels (BPF_HAS_CB_PADS is 0): "callback cannot unwind: the helpers have no pads", -ENOTSUPP. This comes second, so that a missing unwind_goto gets the same message on every kernel. - This helper has no unwind label: "callback cannot unwind", -EINVAL. helper_has_cb_pad() and kfunc_has_cb_pad() list those that have one. Both lists are empty, so every such program is still rejected. The code at the target of unwind_goto gets R0 in the state that the callback unwound with, as after the call of a subprog. The helper passes that value on bit for bit, so the range of a callback's return value does not apply to it, and a pointer keeps its type and its reference. What a callback can leave in R0 this way it can already store through callback_ctx. These cases need care: - The dynptr of bpf_user_ringbuf_drain() and the key of an array element live in the frame of the helper. The exit of the callback invalidates them and everything derived from them, in R0 too. - A callback passes on R0 alone, whatever its prototype says. R2 stays the caller's, because the second visit of the call reads an argument there. - A callback can unwind without an unwind_exit of its own. The check of the stack at the start of a recursive subprog leaves with unwind_exit when unwind_goto follows every call of the subprog that the verifier found before the main pass (bail_unwinds). The call of a helper that takes its callback from an array of function addresses in read-only data is not among them. So check_cb_unwind() asks the new bpf_subprog_unwinds(): can_unwind, or recursive with bail_unwinds. - No speculative state gets to unwind_exit of a callback. Only a load without CAP_PERFMON has such states, and its program has no unwind_exit. One hole is known and stays open. The vma that bpf_find_vma() gives its callback is valid only under the mmap lock, and a callback that unwinds with the vma in R0 passes it to code that runs without the lock. A callback can do the same today when it stores the vma through callback_ctx. Either way the program holds a PTR_TO_BTF_ID without a trust flag, and loads through it are fault-safe and can return stale data. One fix has to close both, and it is not part of this series. Liveness, the backward slice from an unproven insn and constant folding learn that R0 flows through a call with a callback when unwind_goto follows the call. Co-developed-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 10 ++ include/uapi/linux/bpf.h | 3 +- kernel/bpf/const_fold.c | 9 ++ kernel/bpf/liveness.c | 7 + kernel/bpf/precision.c | 12 +- kernel/bpf/verifier.c | 147 +++++++++++++++--- tools/include/uapi/linux/bpf.h | 3 +- .../selftests/bpf/progs/verifier_unwind.c | 2 +- 8 files changed, 168 insertions(+), 25 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index b8617d364f9c..117c3a3f7cbb 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1234,6 +1234,16 @@ struct bpf_subprog_info { u16 max_out_stack_arg_cnt; }; +/* + * The subprog can leave with unwind_exit, so a helper that calls it needs an unwind label, see + * check_cb_unwind(). A recursive subprog counts before it has the insn: its stack check gets one + * after the main pass. + */ +static inline bool bpf_subprog_unwinds(const struct bpf_subprog_info *sub) +{ + return sub->can_unwind || (sub->recursive && sub->bail_unwinds); +} + static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub) { if (sub->stack_args_from_code) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 28904c6f0787..95b41ae6584d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -63,7 +63,8 @@ enum bpf_mul_variant { enum bpf_cond_pseudo_jmp { BPF_MAY_GOTO = 0, /* - * Follows a call of a bpf subprog. Taken when the subprog left with + * Follows a call of a bpf subprog, callx, or a call of a helper or kfunc + * that calls a callback. Taken when the subprog or the callback left with * BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. */ BPF_UNWIND_GOTO = 1, diff --git a/kernel/bpf/const_fold.c b/kernel/bpf/const_fold.c index 3310a55afa3b..1524166cf6db 100644 --- a/kernel/bpf/const_fold.c +++ b/kernel/bpf/const_fold.c @@ -311,6 +311,15 @@ int bpf_compute_const_regs(struct bpf_verifier_env *env) for (r = BPF_REG_0; r < ARRAY_SIZE(aux->const_reg_vals); r++) { struct const_arg_info *c = &ci[r]; + /* + * The revisit of a call with a callback that unwound + * carries the r0 of the callback, see check_helper_call(): + * r0 is no constant of that insn. + */ + if (r == BPF_REG_0 && i + 1 < insn_cnt && + bpf_is_sync_callback_calling_insn(&insns[i]) && + bpf_is_unwind_goto(&insns[i + 1])) + continue; switch (c->state) { case CONST_ARG_CONST: { u64 val = c->val; diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c index 2c93798e1f15..f6456e5be5fd 100644 --- a/kernel/bpf/liveness.c +++ b/kernel/bpf/liveness.c @@ -2832,6 +2832,13 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env, */ if (bpf_is_callx(insn)) use = dst; + /* + * A callback that unwinds goes back to the call of its helper, see + * check_helper_call(). R0 of the callback has to get through the call to + * the target of the unwind_goto, so it is live before the call. + */ + if (bpf_is_sync_callback_calling_insn(insn) && bpf_is_unwind_goto(insn + 1)) + use |= r0; break; default: def = 0; diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c index 33b180b35c88..f2ef763684f4 100644 --- a/kernel/bpf/precision.c +++ b/kernel/bpf/precision.c @@ -767,7 +767,7 @@ static bool compute_insn_deps(struct bpf_verifier_env *env, unsigned long *masks u16 use = aux->use_regs, def = aux->def_regs, args = 0; bool changed = false, stores, reads, loads, call, held_after; struct taken_funcs *taken = &env->precision->taken; - u8 class = BPF_CLASS(insn->code), needs; + u8 class = BPF_CLASS(insn->code), needs, cb_ret; u16 ptrs = 0; int t, callee; @@ -818,8 +818,14 @@ static bool compute_insn_deps(struct bpf_verifier_env *env, unsigned long *masks reads = is_seed; needs = ret[callee]; } else if (callee > 0) { - reads |= compute_call_deps(env, masks, ret, callee, needs & ~RET_REGS, &held, - &changed); + /* + * The callback returns to the helper, so R0 after the call is the helper's. With an + * unwind_goto after the call it can also be R0 of a callback that unwound, see + * check_cb_unwind(). + */ + cb_ret = bpf_is_unwind_goto(insn + 1) ? BIT(BPF_REG_0) : 0; + reads |= compute_call_deps(env, masks, ret, callee, needs & ~(RET_REGS & ~cb_ret), + &held, &changed); needs = ret[callee]; } else if (bpf_is_callx(insn) || aux->calls_callback) { changed |= needs & ~taken->needs; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f10f3660208f..4b65f537668c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -3363,6 +3364,8 @@ static int record_callx_edge(struct bpf_verifier_env *env, int caller, int calle * 'call foo; unwind_goto pad' goes to the pad when foo left with unwind_exit. * JITs don't emit a branch for it. unwind_exit finds the pad by its return * address. So unwind_goto has to follow the call and nothing may jump to it. + * The call can be of a helper or a kfunc that calls a callback: it leaves the + * way the callback does, see check_cb_unwind(). */ static int check_unwind_insns(struct bpf_verifier_env *env) { @@ -3388,8 +3391,10 @@ static int check_unwind_insns(struct bpf_verifier_env *env) if (!bpf_is_unwind_goto(insn)) continue; seen = true; - if (!i || !(bpf_pseudo_call(insn - 1) || bpf_is_callx(insn - 1))) { - verbose(env, "unwind_goto at insn %d doesn't follow a call of a subprog\n", + if (!i || !(bpf_pseudo_call(insn - 1) || bpf_is_callx(insn - 1) || + bpf_is_sync_callback_calling_insn(insn - 1))) { + verbose(env, + "unwind_goto at insn %d doesn't follow a call of a subprog or of a helper with a callback\n", i); return -EINVAL; } @@ -11448,6 +11453,66 @@ static void leave_callback(struct bpf_func_state *caller) leave_loops(caller, caller->loop_stack_cnt - 1); } +/* The helper calls its callback with a pad, see bpf_call_cb(). None yet. */ +static bool helper_has_cb_pad(enum bpf_func_id func_id) +{ + return false; +} + +/* The kfunc calls its callback with a pad. None yet. */ +static bool kfunc_has_cb_pad(u32 btf_id) +{ + return false; +} + +static bool insn_has_cb_pad(struct bpf_insn *insn) +{ + return (bpf_helper_call(insn) && helper_has_cb_pad(insn->imm)) || + (bpf_pseudo_kfunc_call(insn) && kfunc_has_cb_pad(insn->imm)); +} + +/* + * A sync callback is called by a helper or a kfunc, a C frame between the + * callback and the BPF code that called it. unwind_exit of the callback returns + * to the pad of its caller when there is one, and a helper or a kfunc that + * calls a callback has one where BPF_HAS_CB_PADS and insn_has_cb_pad(): it + * calls the callback with bpf_call_cb(), drops what it holds when the callback + * unwinds and leaves to the pad of its own caller with the r0 of the callback. + * So a call with such a callback that can unwind is a call of something that + * can unwind, and unwind_goto has to follow it. + * + * A callback has no unwind_exit and unwinds all the same when it is recursive + * and unwind_goto follows every call of it that mark_recursive_subprogs() saw: + * its check of the recursion leaves with unwind_exit then, see + * check_recursion_guard(). The call of a helper that got the callback from a + * table of functions is not one of those calls. It is known here. + */ +static int check_cb_unwind(struct bpf_verifier_env *env, struct bpf_insn *insn, + int insn_idx, int subprog) +{ + if (!bpf_subprog_unwinds(&env->subprog_info[subprog])) + return 0; + if (!bpf_is_unwind_goto(insn + 1)) { + if (bpf_helper_call(insn)) + verbose(env, "no unwind_goto after the call of %s()\n", + func_id_name(insn->imm)); + else + verbose(env, "no unwind_goto after the call of the kfunc at insn %d\n", + insn_idx); + return -EINVAL; + } + /* The helpers have unwind labels only with asm goto outputs, see bpf_call_cb(). */ + if (!BPF_HAS_CB_PADS) { + verbose(env, "callback cannot unwind: the helpers have no pads\n"); + return -ENOTSUPP; + } + if (!insn_has_cb_pad(insn)) { + verbose(env, "callback cannot unwind\n"); + return -EINVAL; + } + return 0; +} + static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *insn, int insn_idx, int subprog, set_callee_state_fn set_callee_state_cb) @@ -11457,6 +11522,11 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins int err; caller = state->frame[state->curframe]; + /* The callback unwound. This second visit only applies the effects of the helper. */ + if (state->unwound) { + leave_callback(caller); + return 0; + } err = btf_check_subprog_call(env, subprog); if (err == -EFAULT) return err; @@ -11527,6 +11597,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins else env->subprog_info[subprog].is_other_cb = true; + err = check_cb_unwind(env, insn, insn_idx, subprog); + if (err) + return err; + /* for callback functions enqueue entry to callback and * proceed with next instruction within current frame. */ @@ -11685,10 +11759,10 @@ static int call_func_on_its_own(struct bpf_verifier_env *env, int subprog, int i * A void function can leave a pointer to the stack in r0. The caller cannot read it. That holds * for the stack of a caller too: for stack liveness a call returns no pointer to the stack. */ -static void hide_returned_stack_ptrs(struct bpf_verifier_env *env, int subprog, +static void hide_returned_stack_ptrs(struct bpf_verifier_env *env, u32 nregs, struct bpf_reg_state *regs) { - u32 i, nregs = bpf_ret_reg_pair(env, subprog) ? 2 : 1; + u32 i; for (i = 0; i < nregs; i++) { if (regs[ret_regs[i]].type != PTR_TO_STACK) @@ -11706,7 +11780,8 @@ static int exit_func_on_its_own(struct bpf_verifier_env *env) struct bpf_func_state *callee = state->frame[0]; int err; - hide_returned_stack_ptrs(env, callee->subprogno, callee->regs); + hide_returned_stack_ptrs(env, bpf_ret_reg_pair(env, callee->subprogno) ? 2 : 1, + callee->regs); state->unwound |= bpf_is_unwind_exit(&env->prog->insnsi[env->insn_idx]); /* Invalidate callee-local dynptrs and their slices before the frame goes away. */ @@ -12463,20 +12538,30 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) struct bpf_verifier_state *state = env->cur_state; struct bpf_func_state *caller, *callee; struct bpf_reg_state *r0; + bool unwinds; u32 i, nregs; int err; callee = state->frame[state->curframe]; r0 = &callee->regs[BPF_REG_0]; - nregs = bpf_ret_reg_pair(env, callee->subprogno) ? 2 : 1; + /* + * A callback passes on R0 alone, whatever its prototype says, both when it returns to + * the helper and when it unwinds through the unwind label of the helper. R2 stays the + * caller's, because the second visit of the call reads the arguments. + */ + nregs = !callee->in_callback_fn && bpf_ret_reg_pair(env, callee->subprogno) ? 2 : 1; caller = state->frame[state->curframe - 1]; - if (bpf_is_unwind_exit(&env->prog->insnsi[*insn_idx]) || state->unwound) { - /* the helper or the kfunc that called it doesn't expect that */ - if (callee->in_callback_fn) { - verbose(env, "callback cannot unwind\n"); - return -EINVAL; - } + unwinds = bpf_is_unwind_exit(&env->prog->insnsi[*insn_idx]) || state->unwound; + if (unwinds) { + /* + * A callback returns into the helper that called it, and to the + * pad of the helper when the helper has one. check_cb_unwind() + * has seen to that, and to the unwind_goto after the call of + * the helper, where the helper leaves to right away, with the + * r0 of the callback as it is: the pad gets its state, as the + * pad after a subprog call does. + */ if (!bpf_is_unwind_goto(&env->prog->insnsi[callee->callsite + 1])) { verbose(env, "unwind_exit, but no unwind_goto after the call at insn %d\n", callee->callsite); @@ -12484,7 +12569,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) } state->unwound = true; } - if (callee->in_callback_fn) { + if (callee->in_callback_fn && !unwinds) { /* the helper continues after the barrier of its call */ if (state->speculative) return -EACCES; @@ -12527,7 +12612,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) caller->regs[regno] = callee->regs[regno]; bpf_diag_mod_end(env); } - hide_returned_stack_ptrs(env, callee->subprogno, caller->regs); + hide_returned_stack_ptrs(env, nregs, caller->regs); } /* Invalidate callee-local dynptrs and their slices before the frame goes away. */ @@ -12545,6 +12630,11 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) /* for callbacks like bpf_loop or bpf_for_each_map_elem go back to callsite, * there function call logic would reschedule callback visit. */ + /* + * A callback that unwound goes back to the call too, because the helper has effects of + * its own, such as releasing what it consumed. For a state that is marked unwound the + * call starts no further iteration, and R0 stays what the callback unwound with. + */ if (callee->in_callback_fn) *insn_idx = callee->callsite; else @@ -13011,7 +13101,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn const struct bpf_func_proto *fn = NULL; enum bpf_return_type ret_type; enum bpf_type_flag ret_flag; - struct bpf_reg_state *regs; + struct bpf_reg_state *regs, unwound_r0; struct bpf_call_arg_meta meta; const char *operation; int insn_idx = *insn_idx_p; @@ -13261,12 +13351,20 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn /* reset caller saved regs */ bpf_diag_record_caller_saved(env, regs); - bpf_diag_mod_begin(env, ®s[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE); + /* The callback unwound: the helper leaves with the R0 of the callback, so keep R0. */ + if (env->cur_state->unwound) + unwound_r0 = regs[BPF_REG_0]; + else + bpf_diag_mod_begin(env, ®s[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE); for (i = 0; i < CALLER_SAVED_REGS; i++) { bpf_mark_reg_not_init(env, ®s[caller_saved[i]]); check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); } invalidate_outgoing_stack_args(env, cur_func(env)); + if (env->cur_state->unwound) { + regs[BPF_REG_0] = unwound_r0; + goto unwound; + } /* update return register (already marked as written above) */ ret_type = fn->ret_type; @@ -13453,7 +13551,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn return err; bpf_diag_mod_end(env); - +unwound: err = check_map_func_compatibility(env, meta.map.ptr, func_id); if (err) return err; @@ -15827,7 +15925,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, { bool sleepable, rcu_lock, rcu_unlock, preempt_disable, preempt_enable; enum bpf_prog_type prog_type = resolve_prog_type(env->prog); - struct bpf_reg_state *regs = cur_regs(env); + struct bpf_reg_state *regs = cur_regs(env), unwound_r0; const char *func_name, *ptr_type_name; const struct btf_type *t, *ptr_type; struct bpf_call_arg_meta meta; @@ -16116,13 +16214,23 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } bpf_diag_record_caller_saved(env, regs); - bpf_diag_mod_begin(env, ®s[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE); + /* The callback unwound: keep its R0, see check_helper_call(). */ + if (env->cur_state->unwound) { + unwound_r0 = regs[BPF_REG_0]; + ret_nregs = 0; + } else { + bpf_diag_mod_begin(env, ®s[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE); + } for (i = 0; i < CALLER_SAVED_REGS; i++) { u32 regno = caller_saved[i]; bpf_mark_reg_not_init(env, ®s[regno]); } invalidate_outgoing_stack_args(env, cur_func(env)); + if (env->cur_state->unwound) { + regs[BPF_REG_0] = unwound_r0; + goto unwound; + } /* Check return type */ t = btf_type_skip_modifiers(desc_btf, meta.func_proto->type, NULL); @@ -16316,6 +16424,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } +unwound: if (is_kfunc_dynptr_write(&meta) ? dynptr_write_changes_pkt_data(meta.dynptr_write_type) : bpf_is_kfunc_pkt_changing(&meta)) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 28904c6f0787..95b41ae6584d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -63,7 +63,8 @@ enum bpf_mul_variant { enum bpf_cond_pseudo_jmp { BPF_MAY_GOTO = 0, /* - * Follows a call of a bpf subprog. Taken when the subprog left with + * Follows a call of a bpf subprog, callx, or a call of a helper or kfunc + * that calls a callback. Taken when the subprog or the callback left with * BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. */ BPF_UNWIND_GOTO = 1, diff --git a/tools/testing/selftests/bpf/progs/verifier_unwind.c b/tools/testing/selftests/bpf/progs/verifier_unwind.c index be3992ed8eed..6fd8e56485ed 100644 --- a/tools/testing/selftests/bpf/progs/verifier_unwind.c +++ b/tools/testing/selftests/bpf/progs/verifier_unwind.c @@ -929,7 +929,7 @@ __description("unwind: callback unwinds") __arch_x86_64 __arch_arm64 __load_if_JITed() -__failure __msg("callback cannot unwind") +__failure __msg("no unwind_goto after the call of bpf_loop()") __naked int callback_unwinds(void) { asm volatile ( -- 2.53.1-Meta