Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100201; b=yg6Iqc/ADhzuUuSuwqwK1lX+SmaquR/EOpW4REurfI4GK9U1H5b+qNnhovp4l5FHljJg ZLkRLSDr4ApDzw/GP/6ZqkTsCsxJp6NogCf41sev1UMLONJoXQVfRjjOtKIdeqRzr7/8i 2N5FEboqJ1ZzPI6ii0zlO3YSSNijDeYsl4HPryXajK4krCOmHU7ZIrw73NIYT5F+/F53K kOGm8pL5aLCXgS7zIIPU9/PPZ3HhALzBRI3/bn39n6GNRQR2eWejXkB1hA38d8rmwccxB B9XjbAnVL3WiWW9C2nJaC5KjCbPJxs6JMweexpczHGfCsA2qX4QxEhQYDKcJA2avrFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100201; 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=DpvohMmsNdksqRdvBz8jr1h5qJvvVn2puN2E51AG+zs=; b=gvVoUruw45GUvyzcQdKXY+VIUjtOtq70z5aaebtheOCYFX/HHNnLbVg/Q2R54ZBX2tj0 c/uCAyejjI21HMPoiFrUUzGyWfb5JeLkK6LvtRJMBips6DudRpCNMC4thRIvC9E5fUXwQ PhpCm35Wgx9qrAW/O8yhiTDFR136ErSNwPpFbbe67nDLbYvu7yUUYOzjf6lJzzfjkGzqf x/nI2c+hHxZTNaRk6wv2Vqjhvl1N99pzsLQhX73d13UTAf8DeGKLjUuJkDCoiq+M+aMlm Mp45DBsMjR/7f3nX16Zfh5degejpec1pJkCjrLo4IaYegEwM4/asY9aQJ4AWRlvyAUA== 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=d3RZFZM5; arc=none smtp.remote-ip=74.125.231.139 Received: from mail-oo2-f11.google.com (mail-oo2-f11.google.com [74.125.231.139]) (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 B56E6606A32C for ; Tue, 1 Dec 2026 04:43:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B56E6606A32C 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=d3RZFZM5 Received: by mail-oo2-f11.google.com with SMTP id 006d021491bc7-71baaabbbb1so726784eaf.1 for ; Mon, 30 Nov 2026 20:43:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100200; x=1796705000; 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=DpvohMmsNdksqRdvBz8jr1h5qJvvVn2puN2E51AG+zs=; b=d3RZFZM5EsPkblNMeDesQHjl9Nh+JdTWxIstlI8hNdnmHMj80fYr1SPf4QM5qMtb+V baSPxh5Gh+tAHkvA/2RCffmaGza6PKapVjbZpOkFpujgrl2UJ+FVCyr+H3jHjjt+Z42v JoC8Y7REIx8KusxdBXI2qjPAzIL5qdZ2NvjG+HjqlEIXrkDWrcPH89kukvEox2KtZIit PeeK/IS94l8Ca4Z1sm+a0/Nkhi80ZhujDroAYvgSM0LKJSkpAzZ0pnynQbMkbAQ8q9XV w0fihCxXGiu/MYXiOCU/hBJVWdIberwAZ+GkM4Lry2xjLholKX7dd56avOmck555ETdt 7i4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100200; x=1796705000; 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=DpvohMmsNdksqRdvBz8jr1h5qJvvVn2puN2E51AG+zs=; b=UEc6tCAkxS6wQgxZmSRkP3jNDif+rGES6smoF2xRiZRspgfo/Gp+8aAsTHNsB6rFL+ avop2vlpWXR6Z8zMKyknPHW38y3CiZV6NaIfhSCvWq8b79rKHEu7ynG8oL7fAjL/wfjB XsNfiRNy6VbWuqW2zo/Ztw2UCCor11EdETWNS9Ta9dX4A0eZFCSuvIBTkgJQTYSZYwXf G9RcLaPQPPDzgBQjE3XqAtEBxuKsqs+WeVYWVY8DFRow2+tMWmEbmQY3ILNR5pontcoX RUtutlYKGrRYi06uyWleeNnRBu7hf2B+pO+iAsg1NZP12EL3QaJQDnKR4exmx17g8upY yj/Q== X-Gm-Message-State: APuU4Z/QdzKm3/CFZodpAekXrKa7TlHIcIynD16yY3h/OFQIpkFA9KGZ 1tGkF1NuV5C1uA/yHDHJGdFfjeJ/8jxb1gqm/vB9yPn0MEo25YfkiSrF1XNJQDsH/Vo= X-Gm-Gg: AZFdTZfKAlNrpEscvhK82Fkbb0n3/0ao3YTh0uhEgxukpVqhHQ/4wGq4EmUhJT7WDJ2 PMkA79JzYlN62XY+RVnWNob8YZurF4yjV2H3NcqJ6hBrmPNxyDvTurVNcIyHq5+oK+3T515UrJz 4dZmJvqKik8ZELChiz4D2s6qYmPo/99dzI4rucrY8yl90CNF6GyEdqyCOD/AIGdmvxowZoqwTh+ UiGvIgrFUxp2hA9DqVgVZwBx+GTgykC3Pzfy7oI8rV5QL9erEguyfHmEjyNaL9C8m+aITQjKrCx k69a1YPY20iZgvKtC/AXb6iiBuasyJfsCRJzVD8IqcCdBWXQluPqUK07q7BPbEIzERonRwMjuG1 6VPQ+eiZbyU7dFPBjxtpipki5MYaK+PeO/2uBwS1h6heYUcZzhYCrgZZlbS3ESvfLzI5JOGQGZu DzzSBOkuiUyizUGiIxAheXxjulZNIP4cQ3xY+4ISaZogx1IbMiUCe7XFN0hNeP+l0n9G/HP5q/T 4LXR0vi/E+BASKtRV+nAq9Nj4k6fssC+bH1vqnwSiDF0CSx3GZj4CM/Yk4ogmSkSrdUDZkINs7b +DH6C3GftE80oVkXNLEbAAoLKkL8T594nGyN5TgapGMzY07Snzc5bKSwadUpUkSHzmu9y041S3m mxFShkTNzWYZ9FFSAkrPP54y+/LL8IcitUrTR X-Received: by 2002:a05:6820:7089:10b0:6d8:9568:840a with SMTP id 006d021491bc7-71b39530417mr22134238eaf.46.1796100199333; Mon, 30 Nov 2026 20:43:19 -0800 (PST) Received: from localhost ([2a03:2880:10ff:41::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b3806fc45sm23832432eaf.8.2026.11.30.20.43.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:43:17 -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 63/73] bpf: Reject a program whose cleanup pad a cancellation cannot enter Date: Tue, 1 Dec 2026 05:39:30 +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=35328; h=from:subject:message-id; bh=juBK8xNB+Wlt2lqPyF8wuHl3iwWBGLNWt37KoxpJPwY=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb7HZ5u+yMmxmBV5ThBP+Xgu6FdlRv2Ly3xjBqaq/50x0 udrcUcrCIMbFICumyFLyfx+T8YnK34G2y7hh5rAygQxh4OIUgIm4LmH4n37v04XYe2KZ4tErcwpfJO 69sPXqTDOjogsXEu3VX53+e4/hN/v5ljU3Sw6vu3lD5HHyeo55X5Q/frj0Z6GVA0On9unKa4wA X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit A cleanup pad that a cancellation can reach is entered, or the program is rejected. The verifier never skips one silently: a destructor that does not run shows only when the kernel cancels the program, under load, as a lock word in arena that stays locked. Two things are missing for that rule: the hidden words that a cancelled frame needs, and a log that names the cleanup pad that a rejection is about. Give every function in which a cancellation enters a cleanup pad its "cancelled" word, and in a program with bpf_throw() the value word. take_cleanup_pad_words() takes them after all other hidden words, so they never take the room of a word that the program needs in order to load. A function whose stack has no room for them rejects the program. A landing pad for a cancelled frame asks for no hidden slots, because a path that only a cancellation takes must not cost the normal path a store. It does use the hidden slots that the function has for another site. So code that needs a hidden slot, such as a loop in an RCU section that only one path enters, rejects the program when a cancelled frame can reach it. state_is_in_cleanup_pad() tells a state that has a cancelled frame, or that runs a function which such a frame called. Its calls hand no reference over and clear no hidden slot of a caller. So what such a state releases of a caller costs the functions on the stack their hidden slots for that kind of resource. bpf_pad_log() now prints a line before the conflict that says what a cleanup pad has to do with it, for example (one line in the log): the kernel would enter the landing pad at 18 of the call at 11: what the pad leaves at unwind_exit at 27 cannot be released A frame does not know which entry it came through. So the log names the first cleanup pad of the function that reaches the insn on the control flow graph and counts the others, in a walk that runs for the log only and is not charged. Where a cleanup pad may have released or overwritten what a caller holds, the log names the callee and says "may", because it is not known which pad did it. At the unwind_exit of a cleanup pad only R0 is live. So the remainder can release sections, a lock at a constant address, an object in the stack of the frame, a pointer in R0 and what is in hidden slots. A pointer in R6-R9 that the cleanup pad does not release still rejects the program. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 15 ++ kernel/bpf/pads.c | 448 +++++++++++++++++++++++++++++------ kernel/bpf/verifier.c | 11 +- 3 files changed, 399 insertions(+), 75 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 1e87200119bc..aa204e909579 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -626,6 +626,9 @@ struct bpf_pad_plan { bool visited; bool used; /* the program keeps the site, see bpf_pads_mark_used() */ bool failed; /* a state made the conflict an error of the pass */ + /* for bpf_pad_log(): a state came from a cancelled frame or below one, and one did not */ + bool from_cleanup_pad; + bool from_normal; /* * How many sections of each class the frame has entered since its call. Less than 0: it * has left that many sections of its callers, and the pad enters them again. @@ -642,6 +645,8 @@ struct bpf_pad_plan { /* enum bpf_pad_conflict: no location has a pointer. No error if a hidden slot has it. */ u8 no_place; u8 no_irq_place; /* the same for irq flags in the stack of a caller */ + /* the groups that it asked hidden slots for, bits by enum bpf_pad_group: settle_slots() */ + u8 asked_slots; u8 nr_ents; bool ordered; /* see order_ents() */ struct bpf_pad_ent *ents; /* oldest first. The pad releases the last one first. */ @@ -1577,6 +1582,11 @@ struct bpf_subprog_info { * The value word: its landing pads keep the value there over their calls. */ s16 value_off; + /* + * The "cancelled" word of a function in which a cancellation enters a cleanup pad: 0 from + * the first insn of the function on, 1 once the frame is cancelled. + */ + s16 cancelled_off; /* * The pending word: an argument waits there over the call of a subprog, the address of irq * flags that the subprog saves or a pointer that it releases, see bpf_pad_note_return() @@ -1709,6 +1719,11 @@ struct bpf_subprog_info { u16 left_at; /* in this pass it cannot have hidden slots for these groups, bits by enum bpf_pad_group */ u8 no_slots: 4; + /* + * Of those, the groups that it lost because a state in a cleanup pad for a cancellation + * released something of a caller. For the log, see bpf_pad_log(). + */ + u8 no_slots_cleanup_pad: 4; /* the groups that it has hidden slots for. Each bpf_pads_settle() decides again. */ u8 slots: 4; /* diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 8bcde2582519..01292cff77c9 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -1346,11 +1346,30 @@ static int visit_refs(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, return 0; } -/* @sub gets no hidden slots for @groups, for what @st did */ +/* + * Does @st have a cancelled frame, or does it run a function that a cancelled frame called and that + * is verified separately? See 'cancelled' of struct bpf_func_state. + */ +static bool state_is_in_cleanup_pad(const struct bpf_verifier_state *st) +{ + int i; + + for (i = 0; i <= st->curframe && st->frame[i]; i++) + if (st->frame[i]->cancelled) + return true; + return false; +} + +/* + * @sub gets no hidden slots for @groups because of what @st did. If @st is in a cleanup pad, record + * that for the log, see bpf_pad_log(). + */ static void sub_no_slots(struct bpf_subprog_info *sub, const struct bpf_verifier_state *st, u8 groups) { sub->no_slots |= groups; + if (state_is_in_cleanup_pad(st)) + sub->no_slots_cleanup_pad |= groups; } /* @@ -1423,6 +1442,11 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, return ERR_PTR(-ENOMEM); /* the frame is cancelled before it returns to the stores after its call */ took_locks_no_slots(env, st, st->frame[st->curframe]->took_locks); + /* for the log of a rejection, see bpf_pad_log() */ + if (state_is_in_cleanup_pad(st)) + plan->from_cleanup_pad = true; + else + plan->from_normal = true; if (plan->conflict) return plan; err = visit_refs(env, plan, st, insn_idx); @@ -1574,6 +1598,22 @@ 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 a reference of a caller, and no call clears a hidden slot for it, see + * bpf_pad_no_slots(). If the state is in a cleanup pad, record that for the log, see bpf_pad_log(). + */ +static void released_of_caller(struct bpf_verifier_env *env, enum bpf_pad_group group) +{ + const struct bpf_verifier_state *st = env->cur_state; + int f; + + bpf_pad_no_slots(env, group); + if (!state_is_in_cleanup_pad(st)) + return; + for (f = 0; f <= st->curframe; f++) + env->subprog_info[st->frame[f]->subprogno].no_slots_cleanup_pad |= 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 @@ -1650,20 +1690,15 @@ bool bpf_pad_reg_is_handle(const struct bpf_reg_state *reg, const struct bpf_ref } /* - * The current insn releases refs[@idx], a pointer that a caller of its frame holds. If the call - * that this caller made had the pointer in an argument register, see hand_over_refs() in - * verifier.c, the call is what clears the hidden slot of the caller when the callee returns, as - * it is for a lock, see took_lock_of_caller(): the callee notes the register, 'took_arg'. Nothing - * else tells the slot at run time: the register waits in the pending word of the caller, and the - * store behind the call looks for the slot that has the same, see bpf_pad_note_return(). - * The callee can be left before it returns, by itself or because a frame below it is unwound. - * Then the slot is not cleared and the reference is gone: see bpf_pad_note_return(). - * False if the call cannot do that: no frame of this state holds the reference, no argument had - * the pointer at offset 0, the call took another reference before, or a callback is on the way, - * which returns to a helper. - * And false until the program would be rejected without it: these are stores on the normal path, - * and a call that hands the reference over needs none in its caller, see try_more() in - * verifier.c. + * The current insn releases refs[@idx], a pointer that a caller of its frame holds. If the call of + * that caller had the pointer in an argument register, the call can clear the hidden slot of the + * caller when the callee returns, as it does for a lock: the callee records the register in + * 'took_arg', see bpf_pad_note_return(). + * + * Returns false if the call cannot do that: no frame of this state holds the reference, the state + * is in a cleanup pad, no argument had the pointer at offset 0, the callee has released another + * argument, or a callback is in between. Also false until try_more() in verifier.c turns this on + * ('takes_refs'): these are stores on the normal path, and a hand-over needs none. */ static bool took_ref_of_caller(struct bpf_verifier_env *env, int idx) { @@ -1673,7 +1708,7 @@ static bool took_ref_of_caller(struct bpf_verifier_env *env, int idx) for (f = st->curframe - 1; f > 0 && st->frame[f]->entry_refs > idx; f--) ; - if (f < 0 || st->frame[f]->entry_refs > idx) + if (f < 0 || st->frame[f]->entry_refs > idx || state_is_in_cleanup_pad(st)) return false; callee = st->frame[f + 1]; for (k = 0; k < MAX_BPF_FUNC_REG_ARGS && callee->arg_refs[k] != idx + 1; k++) @@ -1910,7 +1945,7 @@ void bpf_pad_note_release(struct bpf_verifier_env *env, int idx) if (note) note->for_caller = true; } else { - bpf_pad_no_slots(env, group); + released_of_caller(env, group); } released_arg_of_caller(env, idx); return; @@ -1985,6 +2020,7 @@ void bpf_pad_note_args(struct bpf_func_state *callee, const struct bpf_func_stat * - 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[]. + * - Any reference in a state that the kernel has brought into a landing pad of the program. * - @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. */ @@ -1997,7 +2033,8 @@ int bpf_pad_handed_ref(struct bpf_verifier_env *env, const struct bpf_verifier_s 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)) + state_is_in_cleanup_pad(st) || !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) @@ -2722,6 +2759,13 @@ static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_pl if (!plan->used || plan->conflict) return 0; + /* + * A cancelled frame asks for none either: a path that only a cancellation takes must not + * cost the normal path a store. Its pads do use the slots that the function has for another + * site, see plan_take_slots(). + */ + if (bpf_site_is_cancelled(plan->kind) || plan->kind == BPF_SITE_CANCEL_ENTRY) + return 0; if (plan->no_place && all_ptrs) groups |= BIT(BPF_PAD_GROUP_REFS); if (plan->no_irq_place) @@ -2734,6 +2778,39 @@ static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_pl return groups; } +/* + * Returns the groups whose hidden slots would have made the pad of @plan possible: the groups that + * its 'conflict' is about, of those that it asked for in settle_slots() and did not get + */ +static u8 conflict_groups(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan) +{ + u8 groups = 0; + int i; + + switch (plan->conflict) { + case BPF_CONFLICT_SECTIONS_DIFFER: + groups = plan->sections_differ; + break; + case BPF_CONFLICT_NO_PLACE: + groups = BIT(BPF_PAD_GROUP_REFS); + if (plan->no_irq_place) + groups |= BIT(BPF_PAD_GROUP_LOCKS); + break; + case BPF_CONFLICT_NOT_ALWAYS_HELD: + case BPF_CONFLICT_CALLER_SAVED: + for (i = 0; i < plan->nr_ents; i++) + if (settle_ent(env, plan, &plan->ents[i]) == plan->conflict) + groups |= BIT(class_group(plan->ents[i].class)); + /* 'no_place' of the record can be the same reason, see settle_plan() */ + if (plan->no_place) + groups |= BIT(BPF_PAD_GROUP_REFS); + break; + default: + break; + } + return groups & plan->asked_slots; +} + /* * 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 @@ -2769,7 +2846,8 @@ static int slot_words(const struct bpf_pad_note *note) /* Does the pad of @plan run in a frame that has set up its hidden slots? */ static bool plan_has_slots(const struct bpf_pad_plan *plan) { - return bpf_site_base(plan->kind) != BPF_SITE_RECURSION && !plan->conflict; + return bpf_site_base(plan->kind) != BPF_SITE_RECURSION && + plan->kind != BPF_SITE_CANCEL_ENTRY && !plan->conflict; } /* Does @plan have room for @slots releases instead of those that it has of @group? */ @@ -2925,9 +3003,11 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) if (pend_off) pend_off = sub->pend_off ?: -(int)bpf_stack_depth_with_slots(sub) - ++words * BPF_REG_SIZE; + /* a cancelled frame without room does not cost the function its slots, see below */ for (i = sub->start; i < sub[1].start; i++) for (plan = aux[i].pad_plans; plan; plan = plan->next) if (plan->used && plan_has_slots(plan) && + !bpf_site_is_cancelled(plan->kind) && !plan_has_room(plan, group, slots)) return 0; sub->hidden_words += words; @@ -2940,7 +3020,7 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) sub->pend_off = pend_off; for (i = sub->start; i < sub[1].start; i++) { for (plan = aux[i].pad_plans; plan; plan = plan->next) { - /* the program does not have its site */ + /* not a site that the program keeps, or a cancelled frame */ if (plan_has_slots(plan) && !plan_has_room(plan, group, slots)) plan->conflict = BPF_CONFLICT_SLOTS_FULL; err = plan_has_slots(plan) ? @@ -2967,13 +3047,14 @@ static int settle_slots(struct bpf_verifier_env *env) struct bpf_pad_plan *plan; int i, s, err; bool all_ptrs; - u8 need; + u8 need, asks; for (s = 0; s < env->subprog_cnt; s++) { env->subprog_info[s].slots = 0; env->subprog_info[s].hidden_words = 0; env->subprog_info[s].value_off = 0; env->subprog_info[s].no_value_word = false; + env->subprog_info[s].cancelled_off = 0; env->subprog_info[s].pend_off = 0; } for (s = 0; s < env->subprog_cnt; s++) { @@ -2983,9 +3064,14 @@ static int settle_slots(struct bpf_verifier_env *env) if (bpf_must_abort()) return -EINTR; need = 0; - for (i = sub->start; i < sub[1].start; i++) - for (plan = aux[i].pad_plans; plan; plan = plan->next) - need |= plan_needs_slots(env, plan, all_ptrs); + for (i = sub->start; i < sub[1].start; i++) { + for (plan = aux[i].pad_plans; plan; plan = plan->next) { + asks = plan_needs_slots(env, plan, all_ptrs); + /* for the log, see conflict_groups() */ + plan->asked_slots |= asks; + need |= asks; + } + } need &= ~sub->no_slots; err = need ? subprog_take_slots(env, s, __ffs(need)) : 0; if (err < 0) @@ -3330,6 +3416,197 @@ static void print_plan(struct bpf_verifier_env *env, int i, const struct bpf_pad plan->sections[BPF_PAD_PREEMPT]); } +/* + * Returns a bitmap of the insns from which insn @at can be reached in its function, on the control + * flow graph, @at included. NULL if there is no memory. The walk is for the log only and is not + * charged, so it follows each edge of the function once, backwards from @at. + */ +static unsigned long *insns_that_reach(struct bpf_verifier_env *env, int at) +{ + const struct bpf_subprog_info *sub = bpf_find_containing_subprog(env, at); + unsigned long *reach = bitmap_zalloc(env->prog->len, BPF_VERIFIER_GFP); + /* an edge, and 1 + the edge before it that goes to the same insn */ + struct { u32 from, prev; } *edges = NULL, *edge; + /* per insn: 1 + the last edge that goes to it. And the insns to walk back from. */ + u32 *last, *todo; + struct bpf_iarray *succ; + int i, p, t, nr = 0, top = 0; + + last = kvcalloc(env->prog->len, sizeof(*last), BPF_VERIFIER_GFP); + todo = kvmalloc_array(env->prog->len, sizeof(*todo), BPF_VERIFIER_GFP); + if (!reach || !last || !todo) + goto err; + for (p = sub->postorder_start; p < sub[1].postorder_start; p++) { + i = env->cfg.insn_postorder[p]; + succ = bpf_insn_successors(env, i); + iarray_for_each(t, succ) { + edge = bpf_table_next(edges, nr); + if (!edge) + goto err; + *edge = (typeof(*edge)){ i, last[t] }; + last[t] = ++nr; + } + } + __set_bit(at, reach); + todo[top++] = at; + while (top) { + t = todo[--top]; + for (p = last[t]; p; p = edges[p - 1].prev) { + i = edges[p - 1].from; + if (!__test_and_set_bit(i, reach)) + todo[top++] = i; + } + } +out: + kvfree(edges); + kvfree(last); + kvfree(todo); + return reach; +err: + bitmap_free(reach); + reach = NULL; + goto out; +} + +/* Returns the first insn of the program's landing pad of the call at @call_idx */ +static int pad_of_call(struct bpf_verifier_env *env, int call_idx) +{ + return call_idx + 2 + bpf_jmp_offset(&env->prog->insnsi[call_idx + 1]); +} + +/* + * Returns the first call of @sub after which a cancellation enters a cleanup pad, or -1, and sets + * *@more to the number of other such calls. With @reach only the cleanup pads whose first insn is + * in that bitmap count, see insns_that_reach(). When a pass is over, the entries that the program + * keeps are known ('used'). Until then the entries that a state went through are taken. + */ +static int first_entered_pad(struct bpf_verifier_env *env, const struct bpf_subprog_info *sub, + const unsigned long *reach, int *more) +{ + const struct bpf_insn_aux_data *aux = env->insn_aux_data; + const struct bpf_pad_plan *plan; + int i, used, first = -1; + + for (used = 1; used >= 0 && first < 0; used--) { + *more = 0; + for (i = sub->start; sub->has_cleanup_pads && i < sub[1].start; i++) { + plan = bpf_pad_plan(&aux[i], BPF_SITE_CANCEL_ENTRY); + if (!plan || !(used ? plan->used : plan->visited)) + continue; + if (reach && !test_bit(pad_of_call(env, i), reach)) + continue; + if (first < 0) + first = i; + else + (*more)++; + } + } + return first; +} + +/* + * Start the log line of an error that comes from a cancellation entering a cleanup pad of @sub. It + * names the first such cleanup pad, see first_entered_pad(). Returns false if there is none. + */ +static bool print_entered_pad(struct bpf_verifier_env *env, const struct bpf_subprog_info *sub, + const unsigned long *reach) +{ + int more, call = first_entered_pad(env, sub, reach, &more); + + if (call < 0) + return false; + verbose(env, "the kernel would enter the landing pad at %d of the call at %d", + pad_of_call(env, call), call); + if (more) + verbose(env, ", and %d more of func#%d", more, (int)(sub - env->subprog_info)); + verbose(env, ": "); + return true; +} + +/* + * Can a cleanup pad be the reason for @conflict in a frame above its own? It can if its code + * releases a lock or leaves a section of a caller on some paths only, or writes to the stack of a + * caller, where irq flags or an iterator are. The other reasons do not depend on a callee. + */ +static bool pad_can_cause(enum bpf_pad_conflict conflict) +{ + switch (conflict) { + case BPF_CONFLICT_SECTIONS_DIFFER: + case BPF_CONFLICT_PLAN_FULL: + case BPF_CONFLICT_SLOTS_FULL: + case BPF_CONFLICT_NOT_ALWAYS_HELD: + case BPF_CONFLICT_NO_PLACE: + case BPF_CONFLICT_ORDER_DIFFERS: + case BPF_CONFLICT_CALLER_SAVED: + return true; + default: + return false; + } +} + +/* + * The conflict of @plan, a site at insn @i, is an error. The kernel enters a landing pad of the + * program that only resumes or the program is rejected: where such a pad has a part in the + * conflict the log says so, in front of the line of the conflict. It names a pad only where that + * pad is known to have the part. + * - The plan is that of an entry: it has no value to keep. + * - The frame is one that the kernel unwinds through such a pad: it has no plan where the pad + * ends, or at a site in pad code. A frame does not know which entry it came through: the + * first pad of the function that gets to the insn on the control flow graph is named, and + * the others that do are counted. + * - States got to the site out of such a pad, in a function that the pad calls. Its frame + * does not know the pad. If states of the normal path got there as well, the function is + * "also" called from the pad: the conflict may be one without the pad. + * - What came back to the call of the plan left a frame below through such a pad, which may + * have taken from what the frame of the call holds, or written to its stack. Which pad it + * was is not known, and a pad on the way may have done nothing of the kind: the function + * that came back is named, the pad is in it or below it. + * - Such a pad released what a caller holds, and no function on the stack gets hidden slots + * for that, see released_of_caller(): the plan needed them for it. + */ +static void print_cleanup_pad_conflict(struct bpf_verifier_env *env, int i, + const struct bpf_pad_plan *plan) +{ + const struct bpf_subprog_info *sub = bpf_find_containing_subprog(env, i); + unsigned long *reach; + int s, c = -1; + + if (!bpf_verifier_log_needed(&env->log)) + return; + if (plan->kind == BPF_SITE_CANCEL_ENTRY) { + verbose(env, "the kernel would enter the landing pad at %d of the call at %d: its entry has no value to keep: %s\n", + pad_of_call(env, i), i, conflict_why[plan->conflict]); + return; + } + if (bpf_site_is_cancelled(plan->kind)) { + reach = insns_that_reach(env, i); + if (reach && print_entered_pad(env, sub, reach)) { + if (plan->kind == BPF_SITE_REMAINDER) + verbose(env, "what the pad leaves at unwind_exit at %d cannot be released\n", + i); + else + verbose(env, "a site in its code has no plan\n"); + } + bitmap_free(reach); + return; + } + if (plan->from_cleanup_pad) + verbose(env, "func#%d is %scalled from a landing pad of the program that the kernel would enter\n", + (int)(sub - env->subprog_info), plan->from_normal ? "also " : ""); + if (!pad_can_cause(plan->conflict)) + return; + if (bpf_site_is_call(plan->kind) && (sites_below(env, plan) & BIT(BPF_SITE_REMAINDER))) + bpf_pad_for_each_callee(plan, s) + if (c < 0 && (env->subprog_info[s].left_at & BIT(BPF_SITE_REMAINDER))) + c = s; + if (c >= 0) + verbose(env, "the kernel would enter a landing pad of the program in or below func#%d: it may release or overwrite what a caller holds at the call at %d\n", + c, i); + else if (sub->no_slots_cleanup_pad & conflict_groups(env, plan)) + verbose(env, "the kernel would enter a landing pad of the program: what its code releases of a caller may keep func#%d from getting hidden slots\n", + (int)(sub - env->subprog_info)); +} + /* * 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, @@ -3352,12 +3629,15 @@ void bpf_pad_log(struct bpf_verifier_env *env, int insn_idx, const struct bpf_pa if (plan->conflict) { print_sites_below(env, insn_idx, plan); + print_cleanup_pad_conflict(env, insn_idx, plan); /* 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); + /* the line above has said why the entry cannot be generated */ + if (plan->kind != BPF_SITE_CANCEL_ENTRY || !plan->conflict) + print_plan(env, insn_idx, plan); } /* @@ -3471,6 +3751,60 @@ static int take_value_word(struct bpf_verifier_env *env, struct bpf_subprog_info return 0; } +/* + * @sub gets the hidden word K, and in a program with bpf_throw() the word for the value: the entry + * of a landing pad stores both. Returns 1 if the stack has no room. + */ +static int take_cancelled_words(struct bpf_verifier_env *env, struct bpf_subprog_info *sub) +{ + int err; + + sub->hidden_words++; + err = bpf_hidden_slots_fit(env); + if (err <= 0) { + sub->hidden_words--; + return err ?: 1; + } + sub->cancelled_off = -(int)bpf_stack_depth_with_slots(sub); + if (!env->has_throw) + return 0; + err = take_value_word(env, sub); + return err < 0 ? err : !!err; +} + +/* + * Give the functions in which a cancellation enters a cleanup pad their hidden words, see + * take_cancelled_words(). They are taken after all other hidden words, so that they never take the + * room of a word that the program needs in order to load. A function whose stack has no room for + * them rejects the program. + */ +static int take_cleanup_pad_words(struct bpf_verifier_env *env) +{ + struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_subprog_info *sub; + struct bpf_pad_plan *plan; + int i, s, err; + bool enters; + + for (s = 0; s < env->subprog_cnt; s++) { + sub = &env->subprog_info[s]; + enters = false; + for (i = sub->start; sub->has_cleanup_pads && !enters && i < sub[1].start; i++) { + plan = bpf_pad_plan(&aux[i], BPF_SITE_CANCEL_ENTRY); + enters = plan && plan->used; + } + err = enters ? take_cancelled_words(env, sub) : 0; + if (err < 0) + return err; + if (!err) + continue; + print_entered_pad(env, sub, NULL); + verbose(env, "no room in the stack for the hidden words of func#%d\n", s); + return -EINVAL; + } + return 0; +} + /* * Mark the records of the sites that the program keeps ('used'). The main pass cancels states at * more sites than that: the first pass takes the edge of may_goto in every loop that nothing else @@ -3489,17 +3823,12 @@ void bpf_pads_mark_used(struct bpf_verifier_env *env) } /* - * A pass that counts is over: all states have visited the plans. A state whose visit made a - * conflict ended at the site, see unwind_frame(): as an unproven insn if the program has the site - * for sure. A conflict that shows only now, at a site that the program has, is an error of the - * pass as well, and its site is an unproven insn for the next one, see - * bpf_find_unproven_after_pass(). - * That holds for every plan, also for those that are there because the kernel enters landing pads - * of the program: the entry of such a pad, a site that a frame gets to while the kernel unwinds it - * through the pad, unwind_exit where the pad ends, and a pad of the verifier in a caller that gets - * what such a frame left. What the program does in its pad is its own business. What the verifier - * adds around it has to be right, or the program does not load: no landing pad is passed by - * because the verifier has no plan. + * A pass ended without an error, and all its states have been recorded at the sites. For each site + * that the program keeps, decide whether its pad can be generated, and give the functions their + * hidden slots and hidden words. A site without a pad and without another way to continue is an + * error of the pass, see plan_rejects(), and an unproven insn for the next pass, see + * bpf_find_unproven_after_pass(). That holds for the records around a cleanup pad as well: a + * cleanup pad that a cancellation can reach is entered, or the program is rejected. */ int bpf_pads_settle(struct bpf_verifier_env *env) { @@ -3537,9 +3866,7 @@ int bpf_pads_settle(struct bpf_verifier_env *env) /* the record of an entry is no record of a pad: nothing to settle */ if (plan->kind != BPF_SITE_CANCEL_ENTRY) { err = settle_plan(env, plan); - if (!err && - (plan_saves_value(env, plan, sub - env->subprog_info) || - (bpf_site_is_cancelled(plan->kind) && env->has_throw))) + if (!err && plan_saves_value(env, plan, sub - env->subprog_info)) err = take_value_word(env, sub); if (err < 0) return err; @@ -3576,8 +3903,13 @@ int bpf_pads_settle(struct bpf_verifier_env *env) bpf_pad_log(env, at, reject); return -EINVAL; } + if (env->faults_late) + return 0; + err = take_cleanup_pad_words(env); + if (err) + return err; /* all accesses of the program to its stack are known: the hidden words go below them */ - for (s = 0; !env->faults_late && s < env->subprog_cnt; s++) { + for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; if (sub->hidden_words) bpf_stack_take_hidden_words(sub); @@ -3705,32 +4037,12 @@ static void ask_below_cleanup_pads(struct bpf_verifier_env *env) } /* - * The pass has an error, and the kernel entered landing pads of the program in it: a state went - * through the entry of a call, 'visited' of its plan, see bpf_pad_enter(). The kernel may have got - * into such a pad only because calls were verified together: the function of count() above with a - * landing pad behind its call. Kept apart nothing comes back unwound to that call, no state gets - * into the pad, and what such a state had a part in is gone. That can be any error, and neither - * the state nor the plan that has it needs to know of the pad: - * - a conflict where the pad ends, at a site in its code, or in a caller that gets what the - * pad left; - * - an error or a conflict in what its code calls, also in a callback that it only arms; - * - a conflict at a call of the normal path, in a function that the code of the pad calls as - * well: the numbers that the pad passes take the trip count from its loop, and may_goto is in - * the code for every call of the function; - * - hidden slots that a function does not get because of what the code of the pad releases. - * So there is one rule: whatever the error is, every function in which the kernel entered a pad - * is marked. The calls behind which it did are causes, and so is what makes their callees unwind, - * see note_causes_below(). The marks say what the next pass keeps apart and nothing else: no - * verdict rests on them. - * They are those of this pass, and every pass that has an error makes them again while the - * kernel still gets into a pad: the causes have to be there for each step of - * bpf_partition_more() in turn, until the calls are apart. They are no step of their own. A pass - * after which nothing more is kept apart is the last one with them as without them, and the - * limit on the cost of a load that is rejected is the same, see do_check_all(). - * A program that is rejected for another reason pays for them: for the dependencies of the calls - * of the marked functions, in every pass, and where only calls that are verified together get - * into the pad, for keeping those apart. An error behind such calls depends on the loop of their - * callee from then on, and costs what an error that reads the result of that loop costs. + * The pass has an error, and a cancellation entered a cleanup pad in it: a state went through the + * entry of a call, see bpf_pad_enter(). That may be so only because two calls are verified + * together, like the calls of count() above with a cleanup pad after them. The error can then be + * anywhere, and the pass that keeps the two calls apart has none. So after a pass with any error, + * mark every function in which a state entered a cleanup pad, for note_causes_below(). No verdict + * rests on the marks. Only a load that is rejected pays for them, in load time. */ static void note_entered(struct bpf_verifier_env *env) { @@ -3976,7 +4288,9 @@ static bool insn_returns(const struct bpf_insn *insn) * it is that no insn which it can get to returns to its caller: it is left at unwind_exit or at a * site, with cancel_exit, and nowhere else. See enter_cleanup_pad() in verifier.c. * - * The answer is a property of the insns, the same in every pass: 'cleanup_pad' of unwind_goto. + * The answer is a property of the insns, the same in every pass: 'cleanup_pad' of unwind_goto. The + * kernel enters each of these pads when it unwinds what the call in front of it calls, in a + * function that can unwind, or the program is rejected: see bpf_pads_settle(). * Dead branches are gone from the graph, see bpf_prune_dead_branches(). */ int bpf_find_cleanup_pads(struct bpf_verifier_env *env) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 834d0b0a9ea9..31b1bac012fa 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -22586,14 +22586,9 @@ static int unwind_frame(struct bpf_verifier_env *env, struct bpf_verifier_state stuck = plan->conflict == BPF_CONFLICT_CANNOT_UNWIND || plan->conflict == BPF_CONFLICT_NO_EXIT; /* - * There is no other way out. Not for may_goto of the program, an access to arena - * and a call above them either, but in a subprog that cannot unwind, and whether - * it is one may be found later in the pass. Then this error was none, see - * do_check_all(). - * It is an error also where a landing pad of the program that the kernel entered - * has a part in it: what came back to the call left a frame below through such - * a pad, or the state got here out of one, in a function that the pad calls. - * The kernel enters the pad or the program is rejected. + * A missing landing pad is an error, unless the frame cannot be cancelled and the + * site can be skipped. If the function turns out later in the pass to be one that + * cannot be cancelled, do_check_all() drops the error. */ if (plan->conflict && certain && (bpf_pad_must_unwind(env, plan) || !stuck)) { bpf_pad_log(env, insn_idx, plan); -- 2.53.1-Meta