Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.138 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100093; b=SSyU6ZjM5/HWUkpqRN0OVMunUugjx+Gp8zIHIXmjULvt1J7UKfp/kwgMhKaR2mqrXyKj XiO3zNq2wEFSISDlM6JCsmzF8Dhscz3A1NPTD6OMk4ap/mi5B2AcHKHRLBstpZTfZSmiC TEycHE0Z2MhRX168ptk7NfEvLAn8uBXyjv9qgJhZgBHbvyRmjmJHjaA3sIqZubOfX2myK gX8qKLXuTS2ElETfId1dzSfI4Akm8adRUHzq99YReFGWTRKaqtgX8lbrlX5idst9n50E5 F/kAUcgn1yOqOL9QVvp8oPxPbbbE6VMP2R+FQspwMAkJgD2k38JkXKfWDkKoFVAVxHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100093; 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=Qf6msurXA4VpXYJmr3yyLdakcx+MyOSGtaoEZRqP5FY=; b=C2puFGxgACGP8JL6CLuzY5mbr2TDqbDMA1ylwEVYNGjFxdA5s3dSCsyGExF/ZMAGahlQ OgRiMe2MkPM2XipzMiqvx7MNL6w7vytlira2UehKzEm1IFdHKnRJdXXQxGkv1OtdQvGxg QTET+oj2yyQ7CQx8i4mes3ZQoK+utXfd51lSlOtwHWfPFGnZtErzM4YfTJ8KBOvouIcl9 qoaCSmMmw/k9Sx2Ba3O6H9Cnk3c/nRRehOGcemnJsNztZTWWyu+VIa24E1FknR7F5dFCV 8pCOWTJtrxQ1XOyguBiNMf3Xt6xWl+qqRzIvXA49JQmnXC36n7KxbyKCYGRHIxSw48Q== 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=Y8bY9SCM; arc=none smtp.remote-ip=74.125.231.138 Received: from mail-oo2-f10.google.com (mail-oo2-f10.google.com [74.125.231.138]) (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 90E2C606A31A for ; Tue, 1 Dec 2026 04:41:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90E2C606A31A 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=Y8bY9SCM Received: by mail-oo2-f10.google.com with SMTP id 46e09a7af769-862ac6a951dso4925084a34.1 for ; Mon, 30 Nov 2026 20:41:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100091; x=1796704891; 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=Qf6msurXA4VpXYJmr3yyLdakcx+MyOSGtaoEZRqP5FY=; b=Y8bY9SCMoPrfFp40M3jquv8oEIAvWBjPQgYFDaCxEFrpcNOtP9EOrHejO9Gh4wI9HT ofizYq0Nu1KFiBV9HEjylILY9gP+/vkGsn2O/jmmoUzoU/vjBqaaSRjti7sG4Aww/h8K VfCt5YONX2k0NQLHfyxrc99Qmu0QgH9Mhy42HcmngKSbb5CWgXq3H4BA60eeoozJKC9D OltX50+vWsVlSxCiQhJVQd6jGgo/5MjNs7Qxuc/8X8mD+nQvbBNetI1q3uhmmp/K2E3k t1qODZfIvFEXz6EoPYgF07SntasCVvFRNGKsd0E9uywjG+YBF/zhkXsXPlOwiz3wRYHh TFNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100091; x=1796704891; 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=Qf6msurXA4VpXYJmr3yyLdakcx+MyOSGtaoEZRqP5FY=; b=SW+bIEr8rVh0E11VQvGcts49yQSs/j1XvRObcL+91NE/hH7HEo4epcwR6/YFrGYnip 471b1rV9jQ1eYTFC1L+Ho2CgUsIoSgCimgl/DbxMvQxvMLNApmuACWNEbWhzwKsC5VQm Vx+M60NrdXV7S35VyPKwmv2VEUlR0OmT8IgFjSQtfQ9nM7LcfBASMu+aNV0DRMa180fC rxi8VPQYUrHbF1/fTDdElN7MWFD5McBGzRXbszFTk7KnMr2C419DhrRsRUDT5Wm+4voF 6hiYbC7NPw5nd7DS3aEXEsrEd8oERsqFKCgayt+q6UdC0xQT7ckyt2cGTwpmnbqZsg0D mR/w== X-Gm-Message-State: APuU4Z9tM8ZUgMQqj9UZHY/+6gMDoSi4FvV7h8OG1/8Qu6Ivo5/+VH2t OSbm0u0z+z1wGNHyq1qKSTCDjBnRipZnk6mME4Oq1Y6SBfUmIPdig7mmFv7CrbqaUzc= X-Gm-Gg: AZFdTZebudDEdAqAVqZlfsfVcCxPjB99EkrC9oDKhfL6Ar9A2nDYdHV+KTW6Lrewqi8 JmMC9zXY+DrwcshpfGVPpE1l/2sAtYyVRdqMuDH0WGwgSwYH8K3dU5F8InVyaEtGc2QLvfUSkxX UQ32/heW71V9HulfBe0VTRCVDZflz8RePm0hLhSJacsEDbClSElVyxNTqACnA3iI2svfP/QKaSY wZxQwYXuGOGFxK/1bWPVgHadkUeUBTyqi9SqoJ/Ln5Xluj+VoYYtTBCcBXdavnxvRU3chR4FrAY xFKRC4otnmGUelL5HJpNZKfLSo4kxAB0iDkmt1jUsYOdZ3rbkS+Re064JFHQtbPEG2IRO7F4r/m lE1xJIaaKsazUfj3TWbvHCidyZ6sLfYx3i5yGjBcCnsS3FYZVzDFJq7djzHuB808j167B0o+lgv FdyBEjmHH2pd/mBHfDcBRZ+bg6eey73BndVceZDQpiSlI4qY9xFrEuG91TXNtdriVOnHmsBgwus xVhBZFTwOWZ/nNiCYmAZjR97vltCv7+DZLA5RvbV6pqeAWqg8ZaNO9w0tzkDW+f95Zcgzw4rmU5 ym1WOlKUs2Ejdg70WYZrgABzirXCDWdowUeNrUkuqPKrQtgLvheptjVmJf7mVIwVFBsRyCJmbUp U7hiHO1v2OFrd+KyydWdFVhfcVYxWvTbg X-Received: by 2002:a05:6820:83c7:20b0:71a:fb85:d05e with SMTP id 006d021491bc7-71b392aab2dmr20721459eaf.10.1796100091089; Mon, 30 Nov 2026 20:41:31 -0800 (PST) Received: from localhost ([2a03:2880:10ff::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4bc5b07d57csm30295539fac.1.2026.11.30.20.41.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:29 -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 29/73] bpf: Let a callee hold a reference that its call hands over Date: Tue, 1 Dec 2026 05:38:56 +0100 Message-ID: <8b9397f5cbbf2090d16e197ef9ca822494fdff8d.1796099737.git.memxor@gmail.com> 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=31203; h=from:subject:message-id; bh=ZMOAz8gTmQm787xdP9chr7ni62wQd4XnpoZV5PHPBPw=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb55ptxJDRJ6cv9T2/zkOtpFhWtJz4zxsg7d1iv0+we68 Z2dHKQuDGBeDrJgiS8n/fUzGJyp/B9ou44aZw8oEMoSBi1MAJrJWhOGvZLDo7L/bBVa8Oh+zqoBN2I 5hrX+35LLji/Mq1soKNMnFMTLMZmRvE3200tz+D8PxW52NWcW2E9KEt7rNtdnL8prh/lEOAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently a reference that a function passes to a static subprog stays with the frame of the caller, and a landing pad releases what its own frame holds. A caller that passes a pointer to a subprog which releases it keeps no copy: static void consume(struct task_struct *t) { while (!ready) ; bpf_task_release(t); } t = bpf_task_from_pid(1); /* and a NULL check */ consume(t); When the loop is out of time, the landing pad of the call has to release t, and no register or stack slot of the caller has t any more. A hidden slot of the caller cannot have it either, because no store of the caller follows the release in consume(). If consume() can be cancelled after its release as well, the pad of the call cannot even tell whether t is still held. Either way the program is rejected, and no pass that keeps more apart changes that. The subprog has the pointer until it releases it, and each of its sites has a landing pad of its own. So let the call hand the reference over: the callee owns it from its entry, as long as the call lasts. The landing pads of the callee release it, and if the callee returns with it, the caller holds it again. Nothing is added at run time. The pad of the loop above is pad: r1 = r6 call bpf_task_release r0 = 0 unwind_exit and the pad of the call releases nothing. In the main pass, hand_over_refs() moves the reference to the frame of the callee, and such a call is verified in the frames of its caller. A hand-over can cost a location: a caller that keeps the pointer for the path on which the callee does not release it has one today, and the callee may have none. A program that loads without a hand-over has to load as it did, with the same landing pads. So a hand-over is tried only where the program would be rejected: - The main pass records which functions release a reference that their caller holds and passed to them in an argument register. - When a pass fails at the landing pad of a call because no location holds a pointer, or because nothing tells whether a reference is held, the callees of that call that release an argument ask for it. - When the load would end in a reject, each function that asked owns its argument from the next pass on, see hand_over_more(), and the passes continue. A call does not hand over a pointer that can be NULL, an iterator or a dynptr, a reference that was made from another one or that another one was made from, or an argument on the stack. It hands nothing over to a recursive subprog or to a callback. These cases are still rejected: - The callee has no location for the pointer at one of its sites, or gets to a site with the reference on some paths only. - The callee or one of its callers needs hidden slots for references. No store follows a reference from one frame to another, so where a call hands over, no function on the stack gets such slots. - The landing pad of the callee would release under a lock of the caller what the program could not release there. - The callee leaves with an unwind_exit of the program while it holds the reference. The caller holds it again and has no location for it. Signed-off-by: Kumar Kartikeya Dwivedi --- Documentation/bpf/verifier.rst | 72 ++++++++++++++++- include/linux/bpf_verifier.h | 18 +++++ kernel/bpf/join.c | 4 + kernel/bpf/pads.c | 143 +++++++++++++++++++++++++++++++-- kernel/bpf/verifier.c | 107 ++++++++++++++++++++++-- 5 files changed, 330 insertions(+), 14 deletions(-) diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst index 21c92b7e829a..03bf363b3d30 100644 --- a/Documentation/bpf/verifier.rst +++ b/Documentation/bpf/verifier.rst @@ -848,8 +848,9 @@ static, in frames at each call, with the the state at the exit A function that is checked *on its own* has a state without the frames of its callers. A static function is checked *in frames* if it can change what its callers see: it gets a pointer to the stack, tests a pointer for the caller, -moves packet data, or releases what the caller holds. A callback is always in -frames. See ``func_is_on_its_own()``. If only the exit state shows such a +moves packet data, releases what the caller holds, or owns a reference that its +call hands over, see `Calls that release for the caller`_. A callback is always +in frames. See ``func_is_on_its_own()``. If only the exit state shows such a change, the call is checked again in frames. An error can ask for frames too, see IN_FRAMES in `Partition`_. @@ -976,6 +977,16 @@ things: tells. If some do not, the pad tests the location for NULL, as the program does. The location has to be 0 in the states that do not hold it. +Where a site has no such location or no such test, the function gets *hidden +slots*: stack slots that the verifier adds below the stack of the function, +where no access of the program gets. A store after each call that acquires +fills a slot, a store at each call that releases clears it, and the landing +pads of the function release what they find in the slots. These stores are on +the normal path, so hidden slots are the fallback, and a function gets them +only for the kind of resource that needs them: RCU sections, preempt sections, +locks and irq flags, or references. Where no store can follow what happens to a +resource, the function cannot have slots for that kind. + If the verifier cannot generate one pad for all states at a site, that is an error of the pass, like an unproven insn: a later pass that keeps more states apart may not have it. If the error stays, the program is rejected, and the log @@ -983,6 +994,49 @@ says why:: landing pad of may_goto at 7: conflict: the number of sections differs +Calls that release for the caller +--------------------------------- + +A static function can release a reference of its caller, and the caller keeps +no copy of the pointer. When the kernel cancels the program in the callee, the +landing pad of the call has no location for the pointer. A hidden slot of the +caller does not help, because no store of the caller follows the release. + +The call can *hand* the reference *over*: the callee owns it from its entry, as +long as the call lasts. The landing pads of the callee release it, and if the +callee returns with it, the caller holds it again, see ``hand_over_refs()``:: + + t = bpf_task_from_pid(pid); consume: + consume(t); r6 = r1 + 1: ... + static void consume(...) may_goto pad + { if r0 == 0 goto 1b + do { ... } while (!p); r1 = r6 + bpf_task_release(t); call bpf_task_release + } exit + pad: r1 = r6 + call bpf_task_release + r0 = 0 + unwind_exit + +The landing pad of the call releases nothing, and nothing is added at run time. +A call hands over a pointer in one of ``R1`` - ``R5`` that the callee reads and +that cannot be NULL. It does not hand over an iterator, a dynptr, a reference +that was made from another one, or one that another one was made from. It hands +nothing over to a recursive function or to a callback. + +The program is still rejected if the callee has no location for the pointer at +one of its sites, if a landing pad of the callee would release under a lock of +the caller what the program could not release there, or if the callee leaves +with ``unwind_exit`` while it owns the reference. + +Where a call hands over, the caller and the functions above it get no hidden +slots for references: a slot of the caller would keep the pointer that the +callee releases. The callee gets none either. + +A hand-over is tried only where the program would be rejected without it, see +`All passes`_. + After an error ============== @@ -1222,6 +1276,16 @@ The last answer is no in three cases: - No partition adds something. - The budget is used up. +One more step comes before the REJECT, see ``hand_over_more()``. If the last +pass with an error failed at a landing pad because of a reference that a callee +releases for its caller, the passes continue with the limit of the load, and +calls hand over what that pass asks for. + +`Calls that release for the caller`_ explains the hand-over. This is no +partition: it changes which function releases a reference. So it comes last, +and a program that loads without it loads as before, with the same landing +pads. + A pass that is over the limit of the load ends it too. The next chapter explains "pessimized". @@ -1551,10 +1615,14 @@ gap a value of a join that is in neither state good pass a pass without errors guess a choice that can be wrong and cannot accept an unsafe program +hand over a call gives a reference of the caller to the callee, which + owns it as long as the call lasts header the insn where each trip of a loop starts held references, locks, dynptrs, iterators, irq flags helped after a partition the first unproven insn of the last pass has no error, and no old error came back +hidden slot a stack slot that the verifier adds to a function, it + records what the frame holds for the landing pads id the name of a set of xregs that hold copies of one pointer imprecise a state of the pass possibly has values that no path has in frames a function is checked with the frames of its callers diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index b4b19cc605df..a7ea1433f323 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -746,6 +746,11 @@ struct bpf_func_state { struct bpf_pad_arg call_args[MAX_BPF_FUNC_REG_ARGS]; ); bool no_stack_arg_load; + /* + * Per argument register: 1 + the index in refs[] of a reference of the caller that the + * call can hand over, see bpf_pad_handed_ref(). 0 if none. Not compared between states. + */ + u8 arg_refs[MAX_BPF_FUNC_REG_ARGS]; /* * Unique diagnostic identity for this function invocation. Frame depth is * reused after returns, while this ID is preserved across state clones. @@ -1493,6 +1498,16 @@ struct bpf_subprog_info { u8 no_slots: 4; /* the groups that it has hidden slots for. Each bpf_pads_settle() decides again. */ u8 slots: 4; + /* + * Bits by argument register, R1 first, see bpf_pad_handed_ref(). + * releases_args: it, or a function below it, released a reference that its caller held and + * passed in the register, in some pass so far. + * asks_args: of those, the arguments that the last pass with an error wants handed over. + * owns_args: its call hands the reference over, in every pass from now on. + */ + u8 releases_args; + u8 asks_args; + u8 owns_args; /* its hidden slots and words take that many words of 8 bytes below its stack */ u8 hidden_words; u8 code_stack_args; @@ -2188,6 +2203,8 @@ void bpf_pad_no_slots(struct bpf_verifier_env *env, enum bpf_pad_group group); void bpf_pad_note_acquire(struct bpf_verifier_env *env, const struct bpf_reg_state *reg); void bpf_pad_note_release(struct bpf_verifier_env *env, int idx); void bpf_pad_note_args(struct bpf_func_state *callee, const struct bpf_func_state *caller); +int bpf_pad_handed_ref(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + const struct bpf_func_state *caller, int subprog, int regno); size_t bpf_pad_note_free(struct bpf_pad_note *note); void bpf_pad_note_section(struct bpf_verifier_env *env, enum bpf_pad_class class, bool enter); void bpf_pad_note_return(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, @@ -2202,6 +2219,7 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, int subprog, struct bpf_insn *buf, struct bpf_insn_aux_data *aux); void bpf_pad_log(struct bpf_verifier_env *env, int insn_idx, const struct bpf_pad_plan *plan); +void bpf_pad_log_handed_over(struct bpf_verifier_env *env, int subprog, u8 args); void bpf_pads_mark_used(struct bpf_verifier_env *env); int bpf_pads_settle(struct bpf_verifier_env *env); bool bpf_pads_conflict(const struct bpf_insn_aux_data *aux); diff --git a/kernel/bpf/join.c b/kernel/bpf/join.c index caaf711570a4..c6c250231aca 100644 --- a/kernel/bpf/join.c +++ b/kernel/bpf/join.c @@ -218,6 +218,10 @@ static void join_func(struct join *j, struct bpf_func_state *dst, struct bpf_fun bpf_stack_arg(dst, src, i) ? bpf_stack_arg(src, dst, i) : NULL); dst->no_stack_arg_load |= src->no_stack_arg_load; dst->callback_depth = min(dst->callback_depth, src->callback_depth); + /* an argument keeps its reference only if both states agree on it, see hand_over_refs() */ + for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) + if (dst->arg_refs[i] != src->arg_refs[i]) + dst->arg_refs[i] = 0; } /* diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 01c5c23f8c5c..f9e959b6a3df 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -105,6 +105,13 @@ * A subprog can acquire or release for its caller, as lock() and unlock() functions do. Where the * caller has hidden slots, the call stands for what the subprog does, like the call of a kfunc. The * stores are in the caller, after the call, see bpf_pad_note_return(). + * + * A call can also HAND a reference OVER: the callee owns it from its entry, as long as the call + * lasts, and the pads of the callee release it. This serves a caller that passes a pointer to a + * subprog which releases it, and keeps no copy. Take 'consume(t)', where consume() waits in a loop + * and then calls bpf_task_release(t): the pad of the loop in consume() releases t, and the pad + * after the call of consume() releases nothing. A hand-over is tried only where the program would + * be rejected without it, see bpf_pad_handed_ref() and hand_over_more() in verifier.c. */ #include #include @@ -1451,6 +1458,34 @@ void bpf_pad_no_slots(struct bpf_verifier_env *env, enum bpf_pad_group group) env->subprog_info[st->frame[f]->subprogno].no_slots |= BIT(group); } +/* + * The current insn releases refs[@idx], a reference that a caller of its frame holds. Record in + * 'releases_args' each function, from the callee of that caller down to this frame, that got the + * pointer in an argument which its call could hand over, see bpf_pad_handed_ref(). Such a function + * can own the argument from the next pass on, see note_causes(). A function that got the pointer in + * another way ends the walk. + */ +static void released_arg_of_caller(struct bpf_verifier_env *env, int idx) +{ + const struct bpf_verifier_state *st = env->cur_state; + const struct bpf_func_state *frame; + int f, k; + + for (f = st->curframe; f >= 0 && st->frame[f]->entry_refs > idx; f--) + ; + /* the function is verified separately: no frame of the state holds the reference */ + if (f < 0) + return; + for (f++; f <= st->curframe; f++) { + frame = st->frame[f]; + for (k = 0; k < MAX_BPF_FUNC_REG_ARGS && frame->arg_refs[k] != idx + 1; k++) + ; + if (k == MAX_BPF_FUNC_REG_ARGS) + return; + env->subprog_info[frame->subprogno].releases_args |= BIT(k); + } +} + /* * The current insn leaves a section of @class that a caller of its frame entered. From here on the * function is in one section less than it was called in, which no hidden slot can say, so it gets @@ -1645,10 +1680,12 @@ void bpf_pad_note_release(struct bpf_verifier_env *env, int idx) if (env->pad_releases) return; if (idx < frame->entry_refs) { - if (group == BPF_PAD_GROUP_LOCKS) + if (group == BPF_PAD_GROUP_LOCKS) { took_lock_of_caller(env, idx); - else + } else { bpf_pad_no_slots(env, group); + released_arg_of_caller(env, idx); + } return; } obj = find_obj(st, ref, &addr); @@ -1714,6 +1751,41 @@ void bpf_pad_note_args(struct bpf_func_state *callee, const struct bpf_func_stat } } +/* + * A call of @subprog out of @caller, a frame of @st, has a pointer in R@regno whose reference + * @caller holds: its index in refs[], which is what the call can HAND OVER, see the comment at the + * top. -1 if there is none, or if it is not one that changes frames without more: + * - A pointer that can be NULL: nothing is held where it is. + * - An object in the stack, a reference that was made from another one, and one that another + * one was made from: they are released in the order of refs[]. + * - @subprog is recursive: the check at its entry takes a frame that holds something for one + * that got there by a jump, see check_recursion_guard() in verifier.c. + */ +int bpf_pad_handed_ref(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + const struct bpf_func_state *caller, int subprog, int regno) +{ + const struct bpf_subprog_info *sub = &env->subprog_info[subprog]; + const struct bpf_reg_state *reg = &caller->regs[regno]; + u32 i, idx, end = st->acquired_refs; + struct bpf_reg_state addr; + + if (!(env->insn_aux_data[sub->start].live_regs_before & BIT(regno)) || sub->recursive || + !reg->id || reg->type == SCALAR_VALUE || type_may_be_null(reg->type)) + return -1; + /* @caller cannot hand over what a frame below it acquired */ + if (caller->frameno < st->curframe) + end = st->frame[caller->frameno + 1]->entry_refs; + for (idx = caller->entry_refs; idx < end; idx++) + if (st->refs[idx].type == REF_TYPE_PTR && st->refs[idx].id == reg->id) + break; + if (idx >= end || st->refs[idx].parent_id || find_obj(st, &st->refs[idx], &addr)) + return -1; + for (i = 0; i < st->acquired_refs; i++) + if (ref_is_child(st, i, idx)) + return -1; + return idx; +} + /* What is known about the lower half of R0 at a return: BPF_RET_ZERO, NONZERO or UNKNOWN */ static u8 ret_class(const struct bpf_reg_state *r0) { @@ -2166,6 +2238,32 @@ static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_pl return groups; } +/* + * Is the pad of @plan impossible because of a reference, BPF_PAD_GROUP_REFS: no location holds the + * pointer, or nothing tells whether it is held? 'no_place' is always about a pointer. Otherwise the + * reason is that of the first release at which settle_plan() stopped. + */ +static bool conflict_of_ref(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan) +{ + int i; + + switch (plan->conflict) { + case BPF_CONFLICT_NO_PLACE: + return plan->no_place; + case BPF_CONFLICT_CALLER_SAVED: + if (plan->no_place) + return true; + fallthrough; + case BPF_CONFLICT_NOT_ALWAYS_HELD: + for (i = 0; i < plan->nr_ents; i++) + if (settle_ent(env, plan, &plan->ents[i]) == plan->conflict) + return class_group(plan->ents[i].class) == BPF_PAD_GROUP_REFS; + return false; + default: + return false; + } +} + /* The slot of bpf_res_spin_lock_irqsave() has a second word, for the irq flags */ static int slot_words(const struct bpf_pad_note *note) { @@ -2668,9 +2766,32 @@ static void print_plan(struct bpf_verifier_env *env, int i, const struct bpf_pad plan->sections[BPF_PAD_PREEMPT]); } +/* + * Log that @subprog owns the references that its calls hand over in the argument registers @args, + * see bpf_pad_handed_ref(). The log of an error about a reference at one of its sites says so, + * because no insn of the function acquired what its pad has to release. + */ +void bpf_pad_log_handed_over(struct bpf_verifier_env *env, int subprog, u8 args) +{ + int k; + + for (k = 0; k < MAX_BPF_FUNC_REG_ARGS; k++) + if (args & BIT(k)) + verbose(env, "func#%d holds the reference that its call hands over in R%d\n", + subprog, BPF_REG_1 + k); +} + /* Log what the pad of the site of @plan at @insn_idx releases, or why it cannot be generated */ void bpf_pad_log(struct bpf_verifier_env *env, int insn_idx, const struct bpf_pad_plan *plan) { + const struct bpf_subprog_info *sub = bpf_find_containing_subprog(env, insn_idx); + + if (plan->conflict) { + /* a release that the pad may not make is one of a pointer or of an object */ + if (sub->owns_args && (plan->conflict == BPF_CONFLICT_NO_RELEASE || + conflict_of_ref(env, plan))) + bpf_pad_log_handed_over(env, sub - env->subprog_info, sub->owns_args); + } print_plan(env, insn_idx, plan); } @@ -2790,12 +2911,22 @@ static bool jump_leaves_loop(struct bpf_verifier_env *env, int h, int n) * * Kept apart, neither call is cancelled. The calls do not say what to keep apart, because their * arguments are constants, so the causes are recorded in the callee. + * + * No pass that keeps more apart gives the frame of the call a place for a pointer that it passed + * on and kept no copy of, or a way to tell whether the callee has released it. Where that is the + * conflict, see conflict_of_ref(), and the callee is one that releases what the call could hand + * over, the callee asks for it, see hand_over_more() in verifier.c. + * @insn_idx: the site of @plan. */ -static void note_causes(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan) +static void note_causes(struct bpf_verifier_env *env, int insn_idx, const struct bpf_pad_plan *plan) { struct bpf_subprog_info *sub = env->subprog_info; int s; + if (plan->kind == BPF_SITE_CALL && conflict_of_ref(env, plan)) { + bpf_pad_for_each_callee(plan, s) + sub[s].asks_args |= sub[s].releases_args; + } if (plan->kind != BPF_SITE_CALL) return; bpf_pad_for_each_callee(plan, s) @@ -2895,12 +3026,14 @@ void bpf_pads_note_causes(struct bpf_verifier_env *env) const struct bpf_pad_plan *plan; int i, s; - for (s = 0; s < env->subprog_cnt; s++) + for (s = 0; s < env->subprog_cnt; s++) { env->subprog_info[s].below_conflict = false; + env->subprog_info[s].asks_args = 0; + } for (i = 0; i < env->prog->len; i++) for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) if (plan->failed || (plan->used && plan->conflict)) - note_causes(env, plan); + note_causes(env, i, plan); note_causes_below(env); /* The caller checks the limits before the next pass. This is for a load that ends here. */ bpf_check_limits(env); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 34238772cc7a..602d058dc107 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1624,13 +1624,23 @@ static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx) static void release_reference_state(struct bpf_verifier_state *state, int idx) { - int last_idx, fr; + struct bpf_func_state *frame; + int last_idx, fr, i; size_t rem; /* the frames that were entered with it don't hold it ever since */ - for (fr = 0; fr <= state->curframe; fr++) - if (idx < state->frame[fr]->entry_refs) - state->frame[fr]->entry_refs--; + for (fr = 0; fr <= state->curframe; fr++) { + frame = state->frame[fr]; + if (idx < frame->entry_refs) + frame->entry_refs--; + /* arg_refs[] has 1 + an index of refs[]: clear the released one, shift the rest */ + for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { + if (frame->arg_refs[i] == idx + 1) + frame->arg_refs[i] = 0; + else if (frame->arg_refs[i] > idx + 1) + frame->arg_refs[i]--; + } + } /* IRQ state requires the relative ordering of elements remaining the * same, since it relies on the refs array to behave as a stack, so that @@ -11843,6 +11853,10 @@ static bool func_is_on_its_own(struct bpf_verifier_env *env, struct bpf_verifier return false; if (arg_needs_caller(env, state, sub, &caller->regs[i])) return false; + /* A call that hands a reference over is verified in the frames of its caller. */ + if ((sub->owns_args & BIT(i - BPF_REG_1)) && + bpf_pad_handed_ref(env, state, caller, subprog, i) >= 0) + return false; } /* callx of one function of a set asks before it is checked that the arguments are there */ for (i = 0; i < bpf_in_stack_arg_cnt(sub) && i < caller->out_stack_arg_cnt; i++) { @@ -12059,6 +12073,41 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal return 0; } +/* + * The call that made the current frame of @state has pointers in its argument registers whose + * references the caller holds, see bpf_pad_handed_ref(). The frame notes each of them, for + * bpf_pad_note_release(). Where the function owns the argument, see hand_over_more(), the call + * HANDS the reference OVER, see kernel/bpf/pads.c: its entry goes to the end of refs[], where what + * a frame acquires is, and the frame of the callee was entered without it. All that looks at + * refs[] takes the reference for one of the callee from here on: its landing pads release it, and + * when it returns with the reference the caller holds it again, as it holds what a callee acquired + * and returned with. The id stays, so the registers of every frame mean what they meant, and the + * locks and irq flags stay in their order. + * No store follows a reference from one frame to another: the functions on the stack get no hidden + * slots for references. + */ +static void hand_over_refs(struct bpf_verifier_env *env, struct bpf_verifier_state *state) +{ + const struct bpf_func_state *caller = state->frame[state->curframe - 1]; + struct bpf_func_state *callee = state->frame[state->curframe]; + u8 owns = env->subprog_info[callee->subprogno].owns_args; + struct bpf_reference_state ref; + int i, idx; + + for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { + idx = bpf_pad_handed_ref(env, state, caller, callee->subprogno, BPF_REG_1 + i); + if (idx < 0) + continue; + callee->arg_refs[i] = idx < U8_MAX ? idx + 1 : 0; + if (!(owns & BIT(i))) + continue; + ref = state->refs[idx]; + release_reference_state(state, idx); + state->refs[state->acquired_refs++] = ref; + bpf_pad_no_slots(env, BPF_PAD_GROUP_REFS); + } +} + /* * Call of a static subprog. Unless the callee is verified on its own it is * verified in the context of the caller, hence set up a new frame and @@ -12107,6 +12156,7 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog, err = setup_func_entry(env, subprog, *insn_idx, set_callee_state, state); if (err) return err; + hand_over_refs(env, state); bpf_diag_record_scrub(env, &caller->regs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED); clear_caller_saved_regs(env, caller->regs); @@ -24228,10 +24278,14 @@ static int check_funcs(struct bpf_verifier_env *env, struct bpf_snapshot *initia u32 level = env->log.level; int i, ret; - /* see settle_late_unwinds() */ - for (i = 1; i < env->subprog_cnt; i++) + for (i = 1; i < env->subprog_cnt; i++) { + /* did_unwind and did_cancel of a global subprog go to the next pass. */ if (bpf_subprog_is_global(env, i)) initial->subprogs[i].did_unwind |= env->subprog_info[i].did_unwind; + /* releases_args adds up. asks_args is that of the last pass with an error. */ + initial->subprogs[i].releases_args |= env->subprog_info[i].releases_args; + initial->subprogs[i].asks_args = env->subprog_info[i].asks_args; + } restore_snapshot(env, initial); kvfree(env->callx_edges); kvfree(env->callx_index.slots); @@ -24387,6 +24441,34 @@ static int log_failing_path(struct bpf_verifier_env *env, struct bpf_snapshot *i return ret; } +/* + * No pass is going to keep more apart, and the last one that had an error had a conflict that a + * function which releases what its call could hand over asks for: 'asks_args', see note_causes() + * in pads.c. From the next pass on the function owns such a reference while the call lasts, see + * hand_over_refs(), and its landing pads release what the frame of the call has no place for. + * That is no partition. It changes which frame releases, in every pass that follows, and a + * program that loads without it could load with other landing pads, or not at all: a callee + * that keeps no copy has no place either. So it is tried where the program would be rejected + * and nowhere else: what loads without it loads as it did. + * A function only ever owns more, so this comes to an end. + * Returns true if a function owns more than before. + */ +static bool hand_over_more(struct bpf_verifier_env *env, struct bpf_snapshot *initial) +{ + bool more = false; + u8 args; + int s; + + for (s = 1; s < env->subprog_cnt; s++) { + args = env->subprog_info[s].asks_args & ~initial->subprogs[s].owns_args; + initial->subprogs[s].owns_args |= args; + more |= args; + if (env->log.level & BPF_LOG_LEVEL2) + bpf_pad_log_handed_over(env, s, args); + } + return more; +} + /* * pass 0: join all numbers * | @@ -24419,6 +24501,10 @@ static int log_failing_path(struct bpf_verifier_env *env, struct bpf_snapshot *i * The log has the failing path. If there is none, or if it has a guess and the error of a pass * is real, the log has the basic block of an error. Below level 2 the later passes and the * path-sensitive runs write no log. The one that gives the log runs once more. + * + * Where the answer would be REJECT and the last pass with an error had a conflict that a call + * which hands a reference to its callee can settle, the callee owns the reference from the next + * pass on, see hand_over_more(), and the passes go on with what is kept apart. */ static int do_check_all(struct bpf_verifier_env *env) { @@ -24428,7 +24514,7 @@ static int do_check_all(struct bpf_verifier_env *env) struct bpf_snapshot *initial, *good_pass = NULL; int ret, more, path_err = 0, err = 0; bool guess = false, real; - u64 max_cost = 0; + u64 max_cost = 0, load_max_cost = env->max_cost; initial = save_snapshot(env, NULL); if (!initial || bpf_precision_init(env)) { @@ -24527,6 +24613,13 @@ static int do_check_all(struct bpf_verifier_env *env) if (!last) continue; reject: + /* There is more to try: the passes continue, with the limit of the load. */ + if (!limit && hand_over_more(env, initial)) { + path_err = 0; + guess = logged = false; + env->max_cost = load_max_cost; + continue; + } if (logged || !quiet || (limit && !env->pass)) break; /* Run a pass with an error once more, for the log. It has a limit of its own. */ -- 2.53.1-Meta