Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.201 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100209; b=qEhfGBUvZtLcE3KLhyfqOJ04+YK0vNRf7cp53TH7LtfbXWQc6KTifZeqlKfl5XWpluby e6/beQIRFffE/aHmDZqgJQR9H8ggjewm4KZ5FzaE2W+JJNCu7pRfPtuK/lIAdcyb7GdAh Zf82z2Z9AP5vdKmGEYZr7ijxxALdL3E30bjPDO2sxX/cRbEcT4Zop36wTASaE1O/X/hW5 nHRxkt3nzfZGviRN3JzCCIwDSNWlTAX0FMWLtFmsoD1jf374nRm4O+8Mao06NMAJc8iL4 I2wGEPbfl3xHr5HzC1g5eRydrxsK4YqRDoBEVqsmyeV/SFImB1Zaepy1y4hHDkEAOAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100209; 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=y11meqM+wQ1iKijaf7aoAPUw97OJi4sz+TYeOptC8fI=; b=mIWNydvIkuJsfuIPHosn0hZQxDy0b70z1fQ2qaESMQ7PKifTfBboQ3vWKITQS3Ipkac3 OUZcW0gu3jglrIpgb8XBx4yGZWiLQEFgM610oM/WiFx7KBAt8v7/blNGNXqxD2xZZ9dTV iXiin+A8rUM07KKxvZWj+B6L7qNpFZsaFv9VqcRpCkB4qfBAwG/omwA9Bmd7LOiAbJbp/ pqrKs3nQ8r1JQ+497p5aIe113YfiBAgjsQHUYENy1EskrwHFuoAsU6y4607y7Hvts+aiO zskJ91G8EMYq9dQKDcivVipSZNRQ//PKuZ52x7hSBFaZeQeDbS20jTNOFOgL9Vh764A== 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=Nvie6Ldd; arc=none smtp.remote-ip=74.125.231.201 Received: from mail-oi2-f9.google.com (mail-oi2-f9.google.com [74.125.231.201]) (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 A5ECD606A31A for ; Tue, 1 Dec 2026 04:43:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A5ECD606A31A 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=Nvie6Ldd Received: by mail-oi2-f9.google.com with SMTP id 5614622812f47-57cc80785e0so2918415b6e.1 for ; Mon, 30 Nov 2026 20:43:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100208; x=1796705008; 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=y11meqM+wQ1iKijaf7aoAPUw97OJi4sz+TYeOptC8fI=; b=Nvie6LddDlAUac3Ib+nP90BZOoskqT3DuMCNGBfFcV9iWweOVf3QARsZ9sAx5KZyk1 sNMnBDRvBWGx//Be0k9FmCRebxzai+0VVdpCZFd5rf33h2MRvDTkrcguYNErqCjAZ9c5 ZOYO9cdB9Qez4QFs4dkDg1SefdsRK2C7i9Nuz7JUtM8+7DQ5+TMVXTSBEnHxRNaY1lKT 4YMrwzavkdCRRFytSn5P7zM6FY3X7bQyxF8Gb/ow/Mq3IsHp2Mblmwzy2Pky9LU1vcPj Ex1GqJZodlyxZn0OKGE35uktZSWUg5ufThMyU3DikOWUa4DAh2aCehsjPVFfCMXylbnC jykA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100208; x=1796705008; 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=y11meqM+wQ1iKijaf7aoAPUw97OJi4sz+TYeOptC8fI=; b=nN4cfBGVzR83S50CLMfuSx7ytjgdTxoCSJltZoWzCicU4+B/MRklWcqe6cz2T9HmoA Wdx3n6gilNPmCf6dAWa6wh2s/z4/CIaLGsQ7y2AM9zwrxhjPQ75eEnaJUFb2PfoiiZ1m cPce/XKeNgwTKyZZ3RyamWG4/E5iT2n3aDG6si4/QfbIj7S73H0STXv7B/GzSx743dJu e1m21sD/y1JHFhi8fq99NIMY7Be+urRPXVLxyVapdOcyd18bYCLh5c5UP3Uyv53IAqwf xF2hWSC4aYQxjpvfEhZoKVEG0Vb3Bx6UR1YHZXQU1n2hjXxNZihRZgjEWEMu0QZENkWh RIOw== X-Gm-Message-State: APuU4Z9CSIs4QRZpty76u5Upja09RzdBInMaAJ1DnIB41gcmIobnsmvX Dk6ezsLtlRU0L3ni0Jn+iCftkCpQfzFsv2wLZdmk9Rsn2En1YQyCYXOdatJ77yQZERk= X-Gm-Gg: AZFdTZdftXK9S0omcD2RcXRJU+bHGNN7QUzxRXCfh+jdWppQNJEyXr9n9GQWo1SULE7 6tZMaotUKxGlfWDuSSeLlAJ2FzKZkYgLuhtfvgYEaaTDPzsKjnGT8m4u7OnVkr2LmJNRoaCxB4t 8Fa/vQWwQwcPm/QfqOD+YxSKbaVRVkbwLEpIPu9Ofskhl1Vrua0oIQtolZuNsnUWrwBxoJBOCnG +8a3r9jkzyCF6baEjE3AXdCRo8Xae80IzPfsBPSlk6zDdB5f1XFHy9X3dvPHxU0UZW7bkgrKKgO b0BuM3xS5hT3ltOWuT2pfYuaAmUYI1mfk+naNkjYY/7yecbFAPzHEBhvc2rq3z8pos6LSgbE0Y/ 9Y4rg0ZCga1U55tXqsTWwgX+9DWYk6DshyFPAK6Ion1vOBhu+KD5x83/TCdUAw3nHk70DxQ9Frz ES2WqswKgP3RQJePewWADcFcgBFCdTnbSbMAh4gGOiZMbi1rFGPi4NvrF3vBaEDKl44MovxyqhN CTTFsDxhyahxMgawDOal34wZuefdZF9A1TlI3wyKqPXcQHs21Sw3aIF1WcsZsYONYXUG6dXYq6s +A+jDMhwB43EoHMCL+CKsx3WT6TNKx6i9WdFQo1SggzTB9yj6aoUIYfYvhcRc81SwuHE62YSNsv 5kapPnkujkyUcS8zWPOH/qyEYjDcJa16hwmVh X-Received: by 2002:a05:6808:191a:b0:4d5:291b:b850 with SMTP id 5614622812f47-57a0c4b956amr36703968b6e.54.1796100206599; Mon, 30 Nov 2026 20:43:26 -0800 (PST) Received: from localhost ([2a03:2880:10ff:4::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57a31928e6dsm25632974b6e.2.2026.11.30.20.43.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:43:24 -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 65/73] bpf: Emit the stub, the entry and the remainder of a cleanup pad Date: Tue, 1 Dec 2026 05:39:32 +0100 Message-ID: <2017e4f48d394a7bdc4ab1d90744b47dab9cd0dc.1796099738.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=54833; h=from:subject:message-id; bh=CKgpaC1nPR19Oy6WC6mZZoPUKEzq6wkiJMx3s54t8pw=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb7FJ+6QzGw5XTpEw2LR+W9HxBP3lz5Y82jp/7iSxnK2K XdrrO0pZGMS4GGTFFFlK/u9jMj5R+TvQdhk3zBxWJpAhDFycAjCRDZ8Y/pdvEn6aeiqXXWjx4jaZj1 kTJxxwm7eF8ZXdUda22NjIz+YMPxm3uWxee1ts39k33VMN4tU2vJzYdmevUdkSPat9D6sv3mYEAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the verifier can check a cleanup pad for a cancelled frame, but it emits none of the code around the pad. Emit the code of the listing in "bpf: Find the landing pads of a program that can only clean up": the stub, the entry, the remainder, and the second landing pad of a site that a cancelled frame can reach. No program gets any of it until bpf_find_cleanup_pads() is called. In a program without bpf_throw(), with V for the kernel's value: stub: cancelled = 0 rest: ax = cancelled gotol pad if ax == 0 goto +3 entry: cancelled = 1 r0 = V r0 = 0 r2 = 0 gotol pad cancel_exit unwind_exit The "cancelled" word is written only on the two edges that take a frame from the normal path into a landing pad of the program: - The callee left with unwind_exit. The unwind_goto after the call leads to a stub, which clears the word and jumps to the program's landing pad. A JIT takes the target of unwind_goto from the offset of the insn alone, so no JIT changes. - The callee was cancelled. The cancel_goto of the call leads to the entry and not to a generated pad. The entry sets the word without reading it first. In a program with bpf_throw() it also stores R0, the value of the cancellation, in the value word. Nothing on the normal path writes the word, and a function does not clear it at its first insn. What a function pays on the normal path is the word itself and the stack frame that it needs: a function that had no stack gets one. Two kinds of code are different: - A call in pad code can be made by a frame that is cancelled already. Its unwind_goto gets no stub. A stub would clear the word, the frame would leave with the program's unwind_exit, and a landing pad of the program in a caller could catch the cancellation. Its entry tests the word and stores only if it is 0, so the first cancellation decides what the program returns. - A mixed function clears the word at its first insn, with one store per call. It has no stub, and all its entries test. There code that reads the word can be reached from the first insn without passing a stub or an entry. The store stands before the first insn, where a jump back to that insn does not reach it. An unwind_exit that a cancelled frame can reach becomes a jump to the remainder. There a cancelled frame runs the landing pad that the verifier generated for the unwind_exit, and a frame that the program unwinds runs unwind_exit as before. A site that a frame can reach both cancelled and not has two landing pads and one target. The target is a test of the word with both pads after it, unless the two have the same insns. That includes an access to arena at which nothing is held. Such accesses share one landing pad per function, which loads V, and a cancelled frame needs the value of its cancellation, so the access gets an unwind_goto of its own. The word is never read before it is written. Only pad code reads it: an entry after a call in pad code, the test before the two landing pads of a site, and the remainder. In a function that is not mixed, a frame gets from the normal path into pad code only through a stub or an entry that stores. This matters because a frame finds in the word what an earlier frame left there, and code that reads 1 treats the frame as cancelled. So every unwind_goto of the program whose target is not dead code gets a stub, unless its call is in pad code, also where the program's landing pad is no cleanup pad: a typed catch that does not match joins pad code. The emission fails the load with a verifier bug unless each unwind_goto has a stub exactly where it needs one, every reader of the word is pad code, and the main pass saw no cancelled frame take an unwind_goto that has a stub or reach an entry that does not test. Code with the same insns is shared, as landing pads are, except for an entry, which jumps to its own cleanup pad. The new code has no nospec barrier, like the landing pads: a program with unwind_goto is loaded with CAP_PERFMON. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 39 ++- kernel/bpf/fixups.c | 601 ++++++++++++++++++++++++++++------- kernel/bpf/pads.c | 90 +++++- kernel/bpf/verifier.c | 54 +--- 4 files changed, 621 insertions(+), 163 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 97892d66274e..93147a22c81e 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -649,6 +649,13 @@ struct bpf_pad_plan { u8 asked_slots; u8 nr_ents; bool ordered; /* see order_ents() */ + /* BPF_SITE_CANCEL_ENTRY: its entry need not test the "cancelled" word: collect_pads() */ + bool untested; + /* + * BPF_SITE_CANCEL_ENTRY: a frame that was cancelled already reached the entry, so its call + * is in pad code. collect_pads() checks that against the control flow graph. + */ + bool cancelled_visit; struct bpf_pad_ent *ents; /* oldest first. The pad releases the last one first. */ /* before[i]: bits of the entries that entry i must stand before, see put_before() */ u8 before[BPF_PAD_MAX_ENTS]; @@ -738,6 +745,9 @@ struct bpf_pad_note { u8 entry: 1; }; +/* 'clear_stub' of an unwind_goto that gets no stub */ +#define BPF_CLEAR_STUB_NONE U32_MAX + /* The plans and the notes of an insn in the snapshot of a pass, see bpf_pads_move() */ struct bpf_pads_moved { struct bpf_pad_plan *plans; @@ -1403,6 +1413,11 @@ struct bpf_insn_aux_data { u64 fault_unwinds:1; /* in the final program a fault goes to 'fault_pad' of the subprog */ u64 unwind_goto_follows:1; /* the verifier added an unwind_goto after the insn */ u64 cancel_goto_follows:1; /* the verifier added a cancel_goto after the insn */ + /* + * An unwind_goto of the program: a cancelled frame took its jump, so its call is in pad + * code. collect_stubs() checks that the control flow graph says the same. + */ + u64 cancelled_takes:1; ); /* what one pass gives to the next pass, see bpf_flow_new_pass() */ @@ -1429,6 +1444,11 @@ struct bpf_insn_aux_data { /* it can read or write the frame of a caller, in a call with a known callee */ u32 reads_outer:1; u32 writes_outer:1; + /* + * An unwind_goto of the program: 1 + the offset of its stub in the pads of the subprog, see + * collect_stubs(). BPF_CLEAR_STUB_NONE: no stub, its call is in pad code. 0: not decided. + */ + u32 clear_stub; /* * Per-instruction summary of stack slots in the current frame * that this instruction may write, may read, and that are live before it. @@ -1585,8 +1605,9 @@ struct bpf_subprog_info { */ 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. + * The "cancelled" word: 1 in a frame that runs a cleanup pad for a cancellation, 0 in one + * that runs it for the program's own unwinding. Only the stubs and the entries of the + * function write it, or the first insn of a mixed function. */ s16 cancelled_off; /* @@ -1783,6 +1804,15 @@ static inline void bpf_subprog_set_stuck(struct bpf_subprog_info *sub, enum bpf_ sub->stuck_insn = insn_idx; } +/* + * Does the first insn of @sub clear its "cancelled" word? Only in a mixed function, see + * bpf_pad_entry_insns(). In any other function the stubs and the entries write the word. + */ +static inline bool bpf_subprog_clears_cancelled(const struct bpf_subprog_info *sub) +{ + return sub->cancelled_off && sub->cleanup_pads_mixed; +} + /* The stack depth of @sub with its hidden slots and words */ static inline u32 bpf_stack_depth_with_slots(const struct bpf_subprog_info *sub) { @@ -2603,7 +2633,10 @@ void bpf_pads_note_causes(struct bpf_verifier_env *env); bool bpf_pad_can_be_shared(const struct bpf_pad_plan *plan); int bpf_hidden_slots_fit(struct bpf_verifier_env *env); int bpf_pad_slot_notes(struct bpf_verifier_env *env, int s, const struct bpf_pad_note **notes); -int bpf_pad_entry_insns(const struct bpf_pad_note **notes, int nr, struct bpf_insn *buf); +int bpf_pad_entry_insns(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, + int nr, struct bpf_insn *buf); +int bpf_pad_cancel_entry_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, + int subprog, struct bpf_insn *buf); int bpf_pad_insns_before(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, int nr, const struct bpf_pad_note *note, struct bpf_insn *buf); int bpf_pad_insns_after(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index b00a011fa66c..75d3349624d1 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -613,18 +613,17 @@ static int emit_unwind_exit(struct bpf_verifier_env *env, int s, struct bpf_insn } /* - * The landing pad of a site in subprog @s. The only exit of a pad is its last insn. Nothing in a - * pad is hardened against a cpu that mispredicts: only a program that is loaded with CAP_PERFMON - * gets one, see frame_cannot_unwind(). - * The main program returns what the exception callback makes of the value. The pad has no index - * yet: bpf_insert_sites() sets the offset of the call. The callback is not called a second time: - * its unwind_exit gets to the exit of the pad like its exit does, and so does its cancel_exit. - * unwind_goto and cancel_goto say so, to bpf_unwind_target() and bpf_cancel_target(): a return - * address in a program without an entry is a lost unwind_exit or cancel_exit. Only a callback that - * can leave that way gets the insn, and such a program needs a JIT that knows it already. A pad of - * any other program is plain BPF: the interpreter and the other JITs run it. - * bpf_mark_subprogs_that_unwind() ran, so it is known which callback that is. - * Returns the number of insns, BPF_PAD_MAX_INSNS + 2 at most. + * Emit the landing pad of @plan, a site in subprog @s. In a program with an exception callback, a + * pad of the main program passes its value to the callback and returns what the callback returns: + * + * the releases, r1 = value bpf_pad_insns() + * call exception_cb bpf_insert_sites() sets the offset + * unwind_goto 1f if the callback can leave with unwind_exit + * cancel_goto 1f if the callback can leave with cancel_exit + * 1: exit + * + * A pad has no nospec barrier: only a program that is loaded with CAP_PERFMON gets one, see + * frame_cannot_unwind(). Returns the number of insns, at most BPF_PAD_MAX_INSNS + 4. */ static int emit_pad(struct bpf_verifier_env *env, int s, const struct bpf_pad_plan *plan, struct bpf_insn *buf, struct bpf_insn_aux_data *aux) @@ -781,7 +780,10 @@ static struct bpf_pad_plan *unvisited_plan(struct bpf_verifier_env *env, int i, return plan; } -/* The landing pads that go after a subprog: their insns, and where each pad ends */ +/* + * The insns that go after the last insn of a subprog: its stubs, see collect_stubs(), then its + * landing pads. 'ends' has the end of each pad in 'insns'. + */ struct pad_buf { struct bpf_insn *insns; int *ends; @@ -792,6 +794,7 @@ struct pad_buf { int max_cnt; /* the room of 'insns' */ int nr; /* pads */ int cnt; /* insns */ + int stubs; /* insns of the stubs, which come before the first pad */ }; /* Make room in @pads for @n more insns. The buffer is kept from one subprog to the next. */ @@ -817,7 +820,7 @@ static int reserve_pad_insns(struct pad_buf *pads, int n) */ static int add_pad(struct pad_buf *pads, const struct bpf_insn *new, int n, bool share) { - int k, start = 0; + int k, start = pads->stubs; void *mem; u64 key = 0; @@ -856,57 +859,354 @@ static int add_pad(struct pad_buf *pads, const struct bpf_insn *new, int n, bool } /* - * The landing pads of the sites of subprog @s go after its last insn. Sites whose pads have the - * same insns share one. @new and @new_aux have room for a pad. Returns the pads in @pads, which - * has the memory of the subprog before, and the number of their insns. + * A unit is the code that one kind of site at one insn leads to. A site can have two records of + * what the frame holds: 'plan' for a frame that is not cancelled when it gets there, 'cancelled' + * for a frame that is, in a cleanup pad (BPF_SITE_CANCELLED). With one record the unit is its + * landing pad. With both, the "cancelled" word of the frame selects the pad, unless the pads have + * the same insns: + * + * ax = cancelled + * if ax == 0 goto 1f + * the pad of 'cancelled' + * 1: the pad of 'plan' + */ +struct pad_unit { + struct bpf_pad_plan *plan; + struct bpf_pad_plan *cancelled; +}; + +/* The most insns that a unit has */ +#define PAD_UNIT_MAX_INSNS (2 * (BPF_PAD_MAX_INSNS + 4) + 3) + +/* + * Do the two records of @unit give the same landing pad? Both pads are emitted into @buf to + * compare them, and their aux data goes to @scratch. A pad that frees an object of the program + * is never the same as another: the type of the object is not in the insns. + */ +static bool unit_pads_are_same(struct bpf_verifier_env *env, int s, const struct pad_unit *unit, + struct bpf_insn *buf, struct bpf_insn_aux_data *scratch) +{ + int len, other; + + if (!bpf_pad_can_be_shared(unit->plan) || !bpf_pad_can_be_shared(unit->cancelled)) + return false; + len = emit_pad(env, s, unit->cancelled, buf, scratch); + other = emit_pad(env, s, unit->plan, buf + len, scratch + len); + return other == len && !memcmp(buf, buf + len, len * sizeof(*buf)); +} + +/* Does the insn at @i have a unit for sites of @base? If so, it is in @unit. */ +static bool site_unit(struct bpf_verifier_env *env, int i, enum bpf_site_kind base, + struct pad_unit *unit) +{ + const struct bpf_insn_aux_data *aux = &env->insn_aux_data[i]; + + unit->cancelled = NULL; + unit->plan = bpf_pad_plan(aux, base); + if (unit->plan && !plan_has_pad(env, i, unit->plan)) + unit->plan = NULL; + if (base >= BPF_SITE_CANCEL_ENTRY) + return unit->plan; + unit->cancelled = bpf_pad_plan(aux, base | BPF_SITE_CANCELLED); + if (unit->cancelled && !plan_has_pad(env, i, unit->cancelled)) + unit->cancelled = NULL; + return unit->plan || unit->cancelled; +} + +/* + * Emit @unit, the code for a site of kind @base in subprog @s: landing pads, see struct pad_unit, + * except for two kinds. + * + * BPF_SITE_CANCEL_ENTRY is the entry of a cleanup pad, where the cancel_goto of the call leads: + * + * bpf_pad_cancel_entry_insns() cancelled = 1, r0 = 0 + * gotol pad bpf_insert_sites() sets the offset + * + * BPF_SITE_REMAINDER is where an unwind_exit that a cancelled frame can reach jumps instead: + * + * ax = cancelled + * if ax == 0 goto 1f + * the pad of the record releases what the frame still holds, cancel_exit + * 1: unwind_exit the program's own unwinding leaves as before + * + * @aux gets the aux data of the insns, @scratch is for unit_pads_are_same(). Returns the number + * of insns, at most PAD_UNIT_MAX_INSNS. + */ +static int emit_unit(struct bpf_verifier_env *env, int s, enum bpf_site_kind base, + const struct pad_unit *unit, struct bpf_insn *buf, + struct bpf_insn_aux_data *aux, struct bpf_insn_aux_data *scratch) +{ + int cancelled_off = env->subprog_info[s].cancelled_off; + int n = 0, len; + + if (base == BPF_SITE_CANCEL_ENTRY) { + n += bpf_pad_cancel_entry_insns(env, unit->plan, s, buf + n); + buf[n++] = BPF_JMP32_A(0); + return n; + } + if (base != BPF_SITE_REMAINDER && + (!(unit->plan && unit->cancelled) || unit_pads_are_same(env, s, unit, buf, scratch))) + return emit_pad(env, s, unit->plan ?: unit->cancelled, buf, aux); + buf[n++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, BPF_REG_FP, cancelled_off); + buf[n++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, 0); + len = emit_pad(env, s, unit->cancelled ?: unit->plan, buf + n, aux + n); + buf[n - 1].off = len; + n += len; + if (base == BPF_SITE_REMAINDER) { + buf[n++] = BPF_RAW_INSN(BPF_JMP | BPF_EXIT, 0, BPF_UNWIND_EXIT, 0, 0); + return n; + } + return n + emit_pad(env, s, unit->plan, buf + n, aux + n); +} + +/* Copy what the insns of a pad need in their aux data: a map, the type of an object to free */ +static void copy_pad_aux(struct bpf_insn_aux_data *to, const struct bpf_insn_aux_data *from) +{ + to->map_index = from->map_index; + to->map_off = from->map_off; + to->kptr_struct_meta = from->kptr_struct_meta; +} + +/* Can @unit share its insns with an equal unit? Not an entry: it jumps to its own cleanup pad. */ +static bool unit_can_be_shared(enum bpf_site_kind base, const struct pad_unit *unit) +{ + return base != BPF_SITE_CANCEL_ENTRY && + (!unit->plan || bpf_pad_can_be_shared(unit->plan)) && + (!unit->cancelled || bpf_pad_can_be_shared(unit->cancelled)); +} + +/* + * Emit the stubs of subprog @s as the first insns of @pads. A stub clears the "cancelled" word + * where the program's own unwinding takes a frame into a landing pad of the program, so that pad + * code does not take the frame for a cancelled one: + * + * call foo + * unwind_goto stub the program has: unwind_goto pad + * ... + * stub: cancelled = 0 + * gotol pad fill_stub() sets the offset + * + * A function that has the word and is not mixed gets one stub for each landing pad that is not + * dead code. That includes a pad that is no cleanup pad: a typed catch that does not match joins + * pad code. An unwind_goto after a call in pad code keeps its target. A cancelled frame can make + * that call, and with its word cleared it would leave with unwind_exit, which a caller can catch. + * + * 'clear_stub' of each unwind_goto keeps the decision. Returns how many of them got a stub. + */ +static int collect_stubs(struct bpf_verifier_env *env, int s, struct pad_buf *pads) +{ + const struct bpf_subprog_info *sub = &env->subprog_info[s]; + struct bpf_insn_aux_data *aux = env->insn_aux_data; + int i, to, users = 0, len = sub[1].start - sub->start; + const struct bpf_insn *insn; + /* per insn of the subprog: 1 + the start of the stub that jumps to the insn, or 0 */ + u32 *stub_of; + + if (!sub->cancelled_off || sub->cleanup_pads_mixed) + return 0; + stub_of = kvcalloc(len, sizeof(*stub_of), BPF_VERIFIER_GFP); + if (!stub_of) + return -ENOMEM; + for (i = sub->start + 1; i < sub[1].start; i++) { + insn = &env->prog->insnsi[i]; + if (!bpf_is_unwind_goto(insn)) + continue; + to = i + 1 + bpf_jmp_offset(insn); + if (!aux[to].seen) + continue; + if (aux[i - 1].cleanup_pad_code) { + aux[i].clear_stub = BPF_CLEAR_STUB_NONE; + continue; + } + /* a stub must not be where the main pass saw a cancelled frame take the jump */ + if (verifier_bug_if(aux[i].cancelled_takes, env, + "unwind_goto at insn %d of func#%d gets a stub in pad code", + i, s)) { + users = -EFAULT; + break; + } + if (!stub_of[to - sub->start]) { + if (reserve_pad_insns(pads, 2)) { + users = -ENOMEM; + break; + } + pads->insns[pads->cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, + sub->cancelled_off, 0); + pads->insns[pads->cnt++] = BPF_JMP32_A(0); + stub_of[to - sub->start] = pads->cnt - 1; + } + aux[i].clear_stub = stub_of[to - sub->start]; + users++; + } + kvfree(stub_of); + pads->stubs = pads->cnt; + return users; +} + +/* + * The stubs of subprog @s are in the program, from insn @pads on. Make the stub of the program's + * unwind_goto at insn @i jump to the target of that insn. insert_unwind_gotos() then makes the + * insn jump to the stub. + */ +static int fill_stub(struct bpf_verifier_env *env, int s, int i, int pads) +{ + const struct bpf_subprog_info *sub = &env->subprog_info[s]; + struct bpf_insn clear = BPF_ST_MEM(BPF_DW, BPF_REG_FP, sub->cancelled_off, 0); + u32 clear_stub = env->insn_aux_data[i].clear_stub; + struct bpf_insn *stub; + int at, off; + + if (!clear_stub || clear_stub == BPF_CLEAR_STUB_NONE) + return 0; + at = pads + clear_stub - 1; + if (verifier_bug_if(!sub->cancelled_off || sub->cleanup_pads_mixed || at + 2 > sub[1].start || + !bpf_is_unwind_goto(&env->prog->insnsi[i]), env, + "insn %d of func#%d has a stub", i, s)) + return -EFAULT; + stub = &env->prog->insnsi[at]; + off = i + 1 + bpf_jmp_offset(&env->prog->insnsi[i]) - (at + 2); + /* a stub is the store and a jump that has no offset yet, or the offset of this target */ + if (verifier_bug_if(memcmp(stub, &clear, sizeof(clear)) || + stub[1].code != (BPF_JMP32 | BPF_JA) || + (stub[1].imm && stub[1].imm != off), env, + "the stub of insn %d is not at insn %d", i, at)) + return -EFAULT; + stub[1].imm = off; + return 0; +} + +/* + * Collect in @pads what goes after the last insn of subprog @s: its stubs, then one unit per site, + * see struct pad_unit. Units with the same insns are shared. 'pad' of each record gets the start + * of its unit, and @stubbed the number of unwind_gotos that have a stub. @new and @new_aux are + * scratch for one unit. Returns the number of insns. */ static int collect_pads(struct bpf_verifier_env *env, int s, struct bpf_insn *new, - struct bpf_insn_aux_data *new_aux, struct pad_buf *pads) + struct bpf_insn_aux_data *new_aux, struct pad_buf *pads, int *stubbed) { - int last = env->subprog_info[s + 1].start - 1, i, n, pad; + const struct bpf_subprog_info *sub = &env->subprog_info[s]; struct bpf_insn_aux_data *aux = env->insn_aux_data; struct bpf_pad_plan *plan, *shared = NULL; - - pads->nr = pads->cnt = 0; - for (i = env->subprog_info[s].start; i <= last; i++) { + int i, n, pad, base, last = sub[1].start - 1; + struct pad_unit unit; + bool unwinds, cancelled; + + pads->nr = pads->cnt = pads->stubs = 0; + n = collect_stubs(env, s, pads); + if (n < 0) + return n; + *stubbed = n; + for (i = sub->start; i <= last; i++) { + if (aux[i].pad_plans && bpf_must_abort()) + return -EINTR; if (aux[i].loop_guard && !unvisited_plan(env, i, BPF_SITE_GUARD)) return -ENOMEM; /* - * Nothing is held there: the same. There are many such accesses. They get no insn, - * a bit in the entry that they have in the exception table tells, and the pad of - * the first one serves all of the function, see bpf_arena_fault(). + * The accesses to arena at which nothing is held get no unwind_goto. A bit in their + * exception table entries sends a fault to the pad of the first of them, see + * bpf_arena_fault(). An access that a cancelled frame can reach too gets an + * unwind_goto: it needs a unit of two pads. */ - aux[i].fault_unwinds = aux[i].fault_empty && !aux[i].fault_skips; - if (aux[i].fault_unwinds && !shared) { - shared = unvisited_plan(env, i, BPF_SITE_FAULT); - if (!shared) + plan = bpf_pad_plan(&aux[i], BPF_SITE_FAULT | BPF_SITE_CANCELLED); + cancelled = plan && bpf_site_is_used(env, i, plan); + unwinds = aux[i].fault_empty && !aux[i].fault_skips; + aux[i].fault_unwinds = unwinds && !cancelled; + if (unwinds && (cancelled || !shared)) { + plan = unvisited_plan(env, i, BPF_SITE_FAULT); + if (!plan) return -ENOMEM; + shared = cancelled ? shared : plan; } - for (plan = aux[i].pad_plans; plan; plan = plan->next) { - if (!plan_has_pad(env, i, plan)) + for (base = 0; aux[i].pad_plans && base <= BPF_SITE_REMAINDER; base++) { + if (!site_unit(env, i, base, &unit)) continue; /* bpf_pads_settle() settled every site that the program keeps */ - if (verifier_bug_if(!plan->used || plan->conflict, env, - "the landing pad of insn %d was not settled", i)) + if (verifier_bug_if((unit.plan && (!unit.plan->used || unit.plan->conflict)) || + (unit.cancelled && + (!unit.cancelled->used || unit.cancelled->conflict)), + env, "the landing pad of insn %d was not settled", i)) return -EFAULT; - n = emit_pad(env, s, plan, new, new_aux); - pad = add_pad(pads, new, n, bpf_pad_can_be_shared(plan)); + /* these units read or write the "cancelled" word */ + if (verifier_bug_if((unit.cancelled || base >= BPF_SITE_CANCEL_ENTRY) && + !sub->cancelled_off, + env, "func#%d has no hidden word for insn %d", s, i)) + return -EFAULT; + /* + * An entry tests the "cancelled" word only if its call is in pad code or + * the function is mixed. Decide it while 'cleanup_pad_code' matches the + * insns, for both emissions of the entry. + */ + if (base == BPF_SITE_CANCEL_ENTRY) + unit.plan->untested = !sub->cleanup_pads_mixed && + !aux[i].cleanup_pad_code; + /* + * The main pass saw a cancelled frame reach this entry. Without the test + * the entry would overwrite what the first entry of the frame saved. + */ + if (verifier_bug_if(base == BPF_SITE_CANCEL_ENTRY && unit.plan->untested && + unit.plan->cancelled_visit, env, + "the entry behind insn %d of func#%d does not test K", + i, s)) + return -EFAULT; + /* + * A pad for cancelled frames and a remainder rely on the "cancelled" + * word. They have to be in pad code, where a stub or an entry has written + * the word, unless the function clears it at its first insn. + */ + if (verifier_bug_if(!bpf_subprog_clears_cancelled(sub) && + !aux[i].cleanup_pad_code && + (unit.cancelled || base == BPF_SITE_REMAINDER), + env, "insn %d of func#%d reads K outside of pad code", + i, s)) + return -EFAULT; + n = emit_unit(env, s, base, &unit, new, new_aux, new_aux); + pad = add_pad(pads, new, n, unit_can_be_shared(base, &unit)); if (pad < 0) return pad; - plan->pad = pad; + if (unit.plan) + unit.plan->pad = pad; + if (unit.cancelled) + unit.cancelled->pad = pad; } } return pads->cnt; } -/* The plan of the site of @kind at insn @i if it has unwind_goto or cancel_goto after it */ -static const struct bpf_pad_plan *goto_plan(struct bpf_verifier_env *env, int i, - enum bpf_site_kind kind) +/* + * Where the unit of the site of kind @base at insn @i starts, counted from the first insn that is + * appended to the subprog. -1 if there is none, or if an access to arena uses the shared pad. + */ +static int unit_pad(struct bpf_verifier_env *env, int i, enum bpf_site_kind base) { - const struct bpf_insn_aux_data *aux = &env->insn_aux_data[i]; - const struct bpf_pad_plan *plan = bpf_pad_plan(aux, kind); + struct pad_unit unit; - return plan && bpf_site_is_used(env, i, plan) && !aux->fault_unwinds ? plan : NULL; + if (!site_unit(env, i, base, &unit) || + (base == BPF_SITE_FAULT && env->insn_aux_data[i].fault_unwinds)) + return -1; + return (unit.plan ?: unit.cancelled)->pad; +} + +/* + * Set @stub to where the stub of the program's unwind_goto at insn @i of subprog @s starts, + * counted like unit_pad(), or to -1 if the insn keeps its target. It is a verifier bug if + * collect_stubs() decided nothing for an insn whose target is not dead code, in a function that + * gets stubs: a frame would enter pad code with a stale "cancelled" word. + */ +static int goto_stub(struct bpf_verifier_env *env, int s, int i, int *stub) +{ + const struct bpf_subprog_info *sub = &env->subprog_info[s]; + const struct bpf_insn_aux_data *aux = env->insn_aux_data; + bool wanted = sub->cancelled_off && !sub->cleanup_pads_mixed; + u32 clear_stub = aux[i].clear_stub; + bool seen; + + seen = aux[i + 1 + bpf_jmp_offset(&env->prog->insnsi[i])].seen; + if (verifier_bug_if(wanted ? seen && !clear_stub : clear_stub, env, + "unwind_goto at insn %d of func#%d has %s stub", i, s, + clear_stub ? "a" : "no")) + return -EFAULT; + *stub = clear_stub && clear_stub != BPF_CLEAR_STUB_NONE ? clear_stub - 1 : -1; + return 0; } /* @@ -920,54 +1220,62 @@ static const struct bpf_pad_plan *goto_plan(struct bpf_verifier_env *env, int i, * cancel_goto cancel_pad unwind_goto pad unwind_goto P of the program * cancel_goto cancel_pad cancel_goto cancel_pad * - * unwind_goto of the program takes the form that has 32 bits there: a jump that is taken around - * its target would put two insns between it and cancel_goto. Where no state got to that unwind_goto - * it is dead code, and a trap is going to stand in its place, or nothing at all: cancel_goto goes - * in front of it, behind the call. + * At a call with a cleanup pad, cancel_goto leads to the entry of that pad, and P leads to its + * stub if it has one. If no state reached P, it is dead code and cancel_goto goes before it. + * + * Both insns are exception table entries of the insn before them. They are appended to that insn, + * so nothing gets between them and a jump to the next insn skips them. Both have a 32 bit offset, + * also P here: with 16 bits the patch list puts jumps after an insn whose target is too far. */ -static int insert_unwind_gotos(struct bpf_verifier_env *env, const int *added) +static int insert_unwind_gotos(struct bpf_verifier_env *env, const int *added, const int *stubbed) { struct bpf_insn unwind_goto = BPF_RAW_INSN(BPF_JMP32 | BPF_JCOND, 0, BPF_UNWIND_GOTO, 0, 0); struct bpf_insn cancel_goto = BPF_RAW_INSN(BPF_JMP32 | BPF_JCOND, 0, BPF_CANCEL_GOTO, 0, 0); struct bpf_patch_list *pl = &env->patch_list; - const struct bpf_pad_plan *plan, *cancel_plan; + int i, s, pads, err, pad, cancel_pad, stub, stubs; const struct bpf_subprog_info *sub; const struct bpf_insn_patch *p; struct bpf_insn *insn; - int i, s, pads, err; + bool seen; for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; pads = sub[1].start - added[s]; + stubs = 0; for (i = sub->start; added[s] && i < pads; i++) { - plan = goto_plan(env, i, BPF_SITE_CALL) ?: goto_plan(env, i, BPF_SITE_FAULT); - cancel_plan = goto_plan(env, i, BPF_SITE_CANCEL_CALL); - if (!plan && !cancel_plan) + /* so far every unwind_goto before the pads is the program's own */ + insn = &env->prog->insnsi[i + 1]; + stub = -1; + err = bpf_is_unwind_goto(insn) ? goto_stub(env, s, i + 1, &stub) : 0; + if (err) + return err; + pad = cancel_pad = -1; + if (env->insn_aux_data[i].pad_plans) { + pad = unit_pad(env, i, BPF_SITE_CALL); + if (pad < 0) + pad = unit_pad(env, i, BPF_SITE_FAULT); + cancel_pad = unit_pad(env, i, BPF_SITE_CANCEL_ENTRY); + /* cancel_goto leads to an entry or to a generated pad */ + if (verifier_bug_if(cancel_pad >= 0 && + unit_pad(env, i, BPF_SITE_CANCEL_CALL) >= 0, env, + "cancel_goto of insn %d has two pads", i)) + return -EFAULT; + if (cancel_pad < 0) + cancel_pad = unit_pad(env, i, BPF_SITE_CANCEL_CALL); + } + if (pad < 0 && cancel_pad < 0 && stub < 0) continue; if (bpf_must_abort()) return -EINTR; - insn = &env->prog->insnsi[i + 1]; - if (cancel_plan && bpf_is_unwind_goto(insn) && - env->insn_aux_data[i + 1].seen) { - /* the program has unwind_goto there: no plan for unwind_exit */ - unwind_goto.imm = 0; - insn = __bpf_patch_list_add(pl, env->prog, i + 1, &unwind_goto, 1); - if (IS_ERR(insn)) - return PTR_ERR(insn); - bpf_patch_list_jump(pl, insn, - i + 2 + bpf_jmp_offset(&env->prog->insnsi[i + 1]), - 0); - err = bpf_patch_list_append(env, i + 1, &cancel_goto, 1); - if (err) - return err; - p = &pl->patches[pl->cnt - 1]; - bpf_patch_list_jump(pl, &pl->insns[p->start + p->len - 1], - pads + cancel_plan->pad, 0); - env->insn_aux_data[i].cancel_goto_follows = true; - continue; - } + /* + * Is the program's unwind_goto live code? A state reaches the insn when the + * callee returns, not only when the callee leaves with unwind_exit or a + * cancellation enters the cleanup pad. cancel_goto goes after a live + * unwind_goto, and right after the call otherwise. + */ + seen = bpf_is_unwind_goto(insn) && env->insn_aux_data[i + 1].seen; /* what is appended to the site is not dead code, the next insn may be */ - if (plan) { + if (pad >= 0) { err = bpf_patch_list_append(env, i, &unwind_goto, 1); if (err) return err; @@ -975,18 +1283,41 @@ static int insert_unwind_gotos(struct bpf_verifier_env *env, const int *added) env->insn_aux_data[i].unwind_goto_follows = true; p = &pl->patches[pl->cnt - 1]; bpf_patch_list_jump(pl, &pl->insns[p->start + p->len - 1], - pads + plan->pad, 0); + pads + pad, 0); } - if (cancel_plan) { + if (cancel_pad >= 0 && !seen) { err = bpf_patch_list_append(env, i, &cancel_goto, 1); if (err) return err; env->insn_aux_data[i].cancel_goto_follows = true; p = &pl->patches[pl->cnt - 1]; bpf_patch_list_jump(pl, &pl->insns[p->start + p->len - 1], - pads + cancel_plan->pad, 0); + pads + cancel_pad, 0); } + if (stub < 0 && !(cancel_pad >= 0 && seen)) + continue; + /* the program's unwind_goto in its 32 bit form, to its stub or target */ + unwind_goto.imm = 0; + insn = __bpf_patch_list_add(pl, env->prog, i + 1, &unwind_goto, 1); + if (IS_ERR(insn)) + return PTR_ERR(insn); + bpf_patch_list_jump(pl, insn, stub >= 0 ? pads + stub : + i + 2 + bpf_jmp_offset(&env->prog->insnsi[i + 1]), 0); + stubs += stub >= 0; + if (cancel_pad < 0 || !seen) + continue; + err = bpf_patch_list_append(env, i + 1, &cancel_goto, 1); + if (err) + return err; + p = &pl->patches[pl->cnt - 1]; + bpf_patch_list_jump(pl, &pl->insns[p->start + p->len - 1], + pads + cancel_pad, 0); + env->insn_aux_data[i].cancel_goto_follows = true; } + if (verifier_bug_if(stubs != stubbed[s], env, + "%d unwind_goto of func#%d go to a stub, %d have one", + stubs, s, stubbed[s])) + return -EFAULT; } return bpf_patch_list_commit(env); } @@ -1001,15 +1332,16 @@ static int retarget_may_gotos(struct bpf_verifier_env *env, const int *added) { struct bpf_patch_list *pl = &env->patch_list; const struct bpf_subprog_info *sub; - const struct bpf_pad_plan *plan; struct bpf_insn *insn; - int i, s; + int i, s, pad; for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; for (i = sub->start; added[s] && i < sub[1].start - added[s]; i++) { - plan = bpf_pad_plan(&env->insn_aux_data[i], BPF_SITE_MAY_GOTO); - if (!plan || !bpf_site_is_used(env, i, plan)) + if (!env->insn_aux_data[i].pad_plans) + continue; + pad = unit_pad(env, i, BPF_SITE_MAY_GOTO); + if (pad < 0) continue; if (bpf_must_abort()) return -EINTR; @@ -1017,7 +1349,7 @@ static int retarget_may_gotos(struct bpf_verifier_env *env, const int *added) insn = __bpf_patch_list_add(pl, env->prog, i, &env->prog->insnsi[i], 1); if (IS_ERR(insn)) return PTR_ERR(insn); - bpf_patch_list_jump(pl, insn, sub[1].start - added[s] + plan->pad, 0); + bpf_patch_list_jump(pl, insn, sub[1].start - added[s] + pad, 0); } } return bpf_patch_list_commit(env); @@ -1049,9 +1381,10 @@ static int insert_slot_stores(struct bpf_verifier_env *env, struct bpf_insn *buf for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; - if (!sub->slots || !added[s]) + /* a mixed function clears its "cancelled" word at the entry too */ + if ((!sub->slots && !bpf_subprog_clears_cancelled(sub)) || !added[s]) continue; - nr = bpf_pad_slot_notes(env, s, notes); + nr = sub->slots ? bpf_pad_slot_notes(env, s, notes) : 0; if (nr < 0) return nr; entry = sub->start + is_arena_sp_adj(&env->prog->insnsi[sub->start]); @@ -1060,7 +1393,7 @@ static int insert_slot_stores(struct bpf_verifier_env *env, struct bpf_insn *buf /* the patches are queued in the order of the insns */ n = 0; if (i == entry && i > sub->start) - n = bpf_pad_entry_insns(notes, nr, buf); + n = bpf_pad_entry_insns(sub, notes, nr, buf); /* the stores after a call follow the program's unwind_goto */ call = i - 1 - (i > sub->start && bpf_is_unwind_goto(insn - 1)); if (call >= (int)sub->start && !bpf_is_unwind_goto(insn)) @@ -1073,7 +1406,8 @@ static int insert_slot_stores(struct bpf_verifier_env *env, struct bpf_insn *buf last = n ? bpf_patch_list_add(env, i, buf, n + 1) : NULL; if (IS_ERR(last)) return PTR_ERR(last); - n = i == entry && i == sub->start ? bpf_pad_entry_insns(notes, nr, buf) : 0; + n = i == entry && i == sub->start ? + bpf_pad_entry_insns(sub, notes, nr, buf) : 0; err = n ? bpf_patch_list_insert(env, i, buf, n) : 0; if (err) return err; @@ -1104,21 +1438,24 @@ static int insert_slot_stores(struct bpf_verifier_env *env, struct bpf_insn *buf */ int bpf_insert_sites(struct bpf_verifier_env *env) { + struct bpf_insn unwind_goto = BPF_RAW_INSN(BPF_JMP32 | BPF_JCOND, 0, BPF_UNWIND_GOTO, 0, 0); struct bpf_insn patch[2] = { BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0, 0) }; - struct bpf_insn_aux_data *aux = env->insn_aux_data, *pad_aux; + struct bpf_insn_aux_data *aux = env->insn_aux_data, *pad_aux, *unit_aux; struct bpf_insn *new, *insn; - const struct bpf_pad_plan *plan; struct pad_buf pads = {}; struct bpf_subprog_info *sub; - int i, s, cnt, err = 0; - int *added; + int i, k, s, n, cnt, base, err = 0; + struct pad_unit unit; + int *added, *stubbed; - /* how many insns the pads after the subprog have */ + /* per subprog: the insns appended to it, and how many of its unwind_gotos have a stub */ added = kvzalloc_objs(int, env->subprog_cnt, BPF_VERIFIER_GFP); - new = kvzalloc_objs(*new, max(BPF_PAD_MAX_INSNS + 2, BPF_PAD_MAX_SLOT_INSNS), + stubbed = kvzalloc_objs(int, env->subprog_cnt, BPF_VERIFIER_GFP); + new = kvzalloc_objs(*new, max(PAD_UNIT_MAX_INSNS, BPF_PAD_MAX_SLOT_INSNS + 1), BPF_VERIFIER_GFP); - pad_aux = kvzalloc_objs(*pad_aux, BPF_PAD_MAX_INSNS + 2, BPF_VERIFIER_GFP); - if (!added || !new || !pad_aux) { + pad_aux = kvzalloc_objs(*pad_aux, PAD_UNIT_MAX_INSNS, BPF_VERIFIER_GFP); + unit_aux = kvzalloc_objs(*unit_aux, PAD_UNIT_MAX_INSNS, BPF_VERIFIER_GFP); + if (!added || !stubbed || !new || !pad_aux || !unit_aux) { err = -ENOMEM; goto out; } @@ -1129,7 +1466,7 @@ int bpf_insert_sites(struct bpf_verifier_env *env) env->subprog_info[0].can_cancel; for (s = 0; s < env->subprog_cnt && !err; s++) { sub = &env->subprog_info[s]; - cnt = collect_pads(env, s, new, pad_aux, &pads); + cnt = collect_pads(env, s, new, pad_aux, &pads, &stubbed[s]); if (cnt < 0) { err = cnt; break; @@ -1142,19 +1479,33 @@ int bpf_insert_sites(struct bpf_verifier_env *env) } added[s] = cnt; for (i = sub->start; i < sub[1].start; i++) { - if (!aux[i].loop_guard) + if (aux[i].loop_guard) { + /* A jump to itself is a back edge. It goes through may_goto too. */ + patch[1] = env->prog->insnsi[i]; + insn = bpf_patch_list_add(env, i, patch, 2); + if (IS_ERR(insn)) { + err = PTR_ERR(insn); + goto out; + } + /* however far the pad is, see bpf_patch_list_jump() */ + bpf_patch_list_jump(&env->patch_list, insn - 1, sub[1].start, + cnt - unit_pad(env, i, BPF_SITE_GUARD)); + } + if (!aux[i].clear_stub || aux[i].clear_stub == BPF_CLEAR_STUB_NONE) continue; - /* A jump to itself is a back edge. It goes through may_goto too. */ - patch[1] = env->prog->insnsi[i]; - insn = bpf_patch_list_add(env, i, patch, 2); + /* + * The program's unwind_goto gets its 32 bit form before the pads go in. + * fill_stub() then reads its target, which has to be the landing pad. With + * 16 bits the patch list can make the insn jump to a gotol of its own. + */ + insn = __bpf_patch_list_add(&env->patch_list, env->prog, i, &unwind_goto, + 1); if (IS_ERR(insn)) { err = PTR_ERR(insn); goto out; } - /* however far the pad is, see bpf_patch_list_jump() */ - plan = bpf_pad_plan(&aux[i], BPF_SITE_GUARD); - bpf_patch_list_jump(&env->patch_list, insn - 1, sub[1].start, - cnt - plan->pad); + bpf_patch_list_jump(&env->patch_list, insn, + i + 1 + bpf_jmp_offset(&env->prog->insnsi[i]), 0); } if (cnt) err = bpf_patch_list_append(env, i - 1, pads.insns, cnt); @@ -1167,17 +1518,41 @@ int bpf_insert_sites(struct bpf_verifier_env *env) for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; for (i = sub->start; added[s] && i < sub[1].start - added[s]; i++) { - for (plan = aux[i].pad_plans; plan; plan = plan->next) { - if (!plan_has_pad(env, i, plan)) + err = aux[i].pad_plans && bpf_must_abort() ? -EINTR : + fill_stub(env, s, i, sub[1].start - added[s]); + if (err) + goto out; + for (base = 0; aux[i].pad_plans && base <= BPF_SITE_REMAINDER; base++) { + if (!site_unit(env, i, base, &unit)) continue; - cnt = sub[1].start - added[s] + plan->pad; - emit_pad(env, s, plan, new, &aux[cnt]); - if (plan->kind == BPF_SITE_FAULT && aux[i].fault_unwinds) + cnt = sub[1].start - added[s] + (unit.plan ?: unit.cancelled)->pad; + n = emit_unit(env, s, base, &unit, new, unit_aux, pad_aux); + /* + * Emit the unit a second time, for its aux data. It has to give the + * insns that are in the program. The aux data is copied after that + * check: a unit that differs could be longer than the one there. + */ + if (verifier_bug_if(cnt + n > sub[1].start || + memcmp(new, &env->prog->insnsi[cnt], + n * sizeof(*new)), env, + "the pad of insn %d is not the one at insn %d", + i, cnt)) { + err = -EFAULT; + goto out; + } + for (k = 0; k < n; k++) + copy_pad_aux(&aux[cnt + k], &unit_aux[k]); + memset(unit_aux, 0, n * sizeof(*unit_aux)); + insn = &env->prog->insnsi[i]; + if (base == BPF_SITE_FAULT && aux[i].fault_unwinds) sub->fault_pad = cnt; - /* no call is made, see check_kfunc_call() */ - if (bpf_site_base(plan->kind) == BPF_SITE_THROW) - env->prog->insnsi[i] = BPF_RAW_INSN(BPF_JMP32 | BPF_JA, 0, 0, 0, - cnt - i - 1); + /* bpf_throw() is not called. The remainder replaces unwind_exit. */ + if (base == BPF_SITE_THROW || base == BPF_SITE_REMAINDER) + *insn = BPF_JMP32_A(cnt - i - 1); + /* the entry jumps to the target of the program's unwind_goto */ + if (base == BPF_SITE_CANCEL_ENTRY) + env->prog->insnsi[cnt + n - 1].imm = + i + 2 + bpf_jmp_offset(insn + 1) - (cnt + n); } } /* a pad calls no subprog but the exception callback, see emit_pad() */ @@ -1187,12 +1562,14 @@ int bpf_insert_sites(struct bpf_verifier_env *env) i - 1; } err = insert_slot_stores(env, new, added); - err = err ?: insert_unwind_gotos(env, added); + err = err ?: insert_unwind_gotos(env, added, stubbed); err = err ?: retarget_may_gotos(env, added); out: kvfree(added); + kvfree(stubbed); kvfree(new); kvfree(pad_aux); + kvfree(unit_aux); kvfree(pads.insns); kvfree(pads.ends); kvfree(pads.index.slots); diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 7907f59068be..e888589d4cc5 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -112,15 +112,38 @@ * * A landing pad of the program is a CLEANUP PAD if it can only clean up and continue unwinding, * like a C++ destructor or Rust drop glue, see bpf_find_cleanup_pads(). A cancellation runs the - * cleanup pads of the frames that it leaves. + * cleanup pads of the frames that it leaves. The frame remembers in a hidden word, the "cancelled" + * word, whether the kernel cancels it or the program unwinds it: + * + * what the program has what the verifier makes of it + * call foo call foo + * unwind_goto pad unwind_goto stub + * ... cancel_goto entry + * pad: call dtor ... + * unwind_exit pad: call dtor + * gotol rest + * stub: cancelled = 0 <- foo left with unwind_exit + * gotol pad + * entry: cancelled = 1 <- foo was cancelled + * r0 = 0 + * gotol pad + * rest: if cancelled == 0 goto +N + * release what the frame still holds + * cancel_exit + * unwind_exit * * In the main pass, enter_cleanup_pad() in verifier.c marks the frame as cancelled and verifies the * cleanup pad a second time for it, with 0 in R0. A cancelled frame never returns to its caller. It - * leaves at the unwind_exit of the pad, which is a site for it (BPF_SITE_REMAINDER), or at + * leaves at the unwind_exit of the pad, through the remainder at 'rest' (BPF_SITE_REMAINDER), or at * a site inside the pad, where its states have a record and a pad of their own * (BPF_SITE_CANCELLED). A cleanup pad that a cancellation can reach is entered, or the program is * rejected. * + * Only the stub and the entry write the "cancelled" word, and only pad code reads it, see + * find_pad_code(). A call in pad code can be made by a frame that is cancelled already, so its + * unwind_goto gets no stub, and its entry tests the word before it stores. A mixed function, in + * which an insn is pad code and on the normal path as well, clears the word at its first insn. + * * A frame can be in fewer sections than it was called in. Its pad enters the sections that the * frame left, so that the caller is in the sections of its call wherever the callee is cancelled. * Here grow() is called with preemption disabled, and its load from arena can fault. The fault @@ -1505,6 +1528,8 @@ struct bpf_pad_plan *bpf_pad_enter(struct bpf_verifier_env *env, if (!plan) return ERR_PTR(-ENOMEM); plan->visited = true; + /* the frame is cancelled already, so the entry has to test: collect_pads() checks */ + plan->cancelled_visit |= bpf_frame_is_cancelled(frame); if (plan->conflict) return plan; if (env->has_throw && frame->regs[BPF_REG_0].type != SCALAR_VALUE) @@ -3121,11 +3146,15 @@ int bpf_pad_slot_notes(struct bpf_verifier_env *env, int s, const struct bpf_pad } /* - * The entry of a function: all words of its hidden slots are 0. The frame holds nothing of its own, - * and a pad reads all slots, also at a site that is before the calls that fill them. But for the - * slot of a reference that the call hands over: it takes the argument register. + * Emit the stores at the start of a function. All words of its hidden slots are cleared: the frame + * holds nothing yet, and a pad reads all slots, also at a site before the calls that fill them. The + * slot of a reference that the call hands over takes the argument register instead. A mixed + * function also clears its "cancelled" word here, see find_pad_code(): there code that reads the + * word can be reached from the first insn without passing a stub or an entry. Returns the number of + * insns. */ -int bpf_pad_entry_insns(const struct bpf_pad_note **notes, int nr, struct bpf_insn *buf) +int bpf_pad_entry_insns(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, + int nr, struct bpf_insn *buf) { int i, k, n = 0; @@ -3139,6 +3168,50 @@ int bpf_pad_entry_insns(const struct bpf_pad_note **notes, int nr, struct bpf_in buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, notes[i]->off - k * BPF_REG_SIZE, 0); } + if (bpf_subprog_clears_cancelled(sub)) + buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, sub->cancelled_off, 0); + return n; +} + +/* + * Emit the entry of a cleanup pad, see enter_cleanup_pad() in verifier.c, except its last insn, + * which jumps to the cleanup pad. Returns the number of insns. + * + * ax = K behind a call in pad code: + * if ax != 0 goto 1f the kernel unwinds the frame already + * K = 1 + * *(u64 *)(r10 - W) = r0 in a program with bpf_throw(): the value + * 1: r0 = 0 + * + * The first entry of a frame decides: K stays 1 until the frame is left, and the value that the + * program ends with is the one that the unwinding began with, whatever unwinds on the way. The + * pad gets 0 in R0, where unwind_exit of the program leaves what the program threw: the value of + * the kernel is none of its business, and a pad that takes R0 for an object of the program finds + * none. + * Only a call in pad code can be the second entry of a frame: a frame that the kernel unwinds runs + * nothing else, see bpf_find_cleanup_pads(). So only the entry of such a call tests K. Behind any + * other call the entry is the first one, 'untested' of its plan, and stores without the two insns + * of the test: it must not read the word, which nothing has written on that way. In a mixed + * function no call is known to be such a one, every entry tests, and the entry of the function + * clears K for them. + * Returns the number of insns. + */ +int bpf_pad_cancel_entry_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, + int subprog, struct bpf_insn *buf) +{ + const struct bpf_subprog_info *sub = &env->subprog_info[subprog]; + int n = 0; + + if (!plan->untested) { + buf[n++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, BPF_REG_FP, sub->cancelled_off); + buf[n++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 0); + } + buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, sub->cancelled_off, 1); + if (env->has_throw) + buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, sub->value_off); + if (!plan->untested) + buf[1].off = n - 2; + buf[n++] = BPF_MOV64_IMM(BPF_REG_0, 0); return n; } @@ -3752,8 +3825,9 @@ static int take_value_word(struct bpf_verifier_env *env, struct bpf_subprog_info } /* - * @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. + * Give @sub the hidden words that the entries of its cleanup pads store to, see + * bpf_pad_cancel_entry_insns(): the "cancelled" word, the save words, and in a program with + * bpf_throw() the value word. Returns 0, 1 if the stack has no room, or a negative errno. */ static int take_cancelled_words(struct bpf_verifier_env *env, struct bpf_subprog_info *sub) { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 31b1bac012fa..8386bc4df747 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20069,6 +20069,9 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, err = enter_cleanup_pad(env); if (err) return err; + } else if (bpf_frame_is_cancelled(cur_func(env))) { + /* A cancelled frame takes it: no stub for it, see collect_stubs(). */ + cur_aux(env)->cancelled_takes = true; } *insn_idx += insn->off; return 0; @@ -22715,47 +22718,18 @@ static int check_unwound_arrival(struct bpf_verifier_env *env) } /* - * The kernel unwound what the call before this insn called, and the program has unwind_goto here - * whose landing pad ONLY RESUMES, see bpf_find_cleanup_pads(): it runs what the program wants done - * when a frame is left, a destructor of C++ or Rust, and cannot end the unwinding. So it is not - * passed by. cancel_goto behind the call goes to the ENTRY that the verifier emits, and from there - * to the pad. + * The callee of the call before this insn was cancelled, and the program has an unwind_goto here + * that leads to a cleanup pad: a landing pad of the program that can only clean up and continue + * unwinding, such as a C++ destructor, see bpf_find_cleanup_pads(). A cancellation runs such a + * pad. At run time the cancel_goto after the call goes to an entry that sets the "cancelled" word + * of the frame, puts 0 into R0 and jumps to the pad, see the head comment of kernel/bpf/pads.c. * - * call foo call foo - * unwind_goto pad unwind_goto pad - * ... cancel_goto entry - * pad: r6 = r0 ... - * call dtor pad: r6 = r0 - * r0 = r6 call dtor - * unwind_exit r0 = r6 - * gotol rest - * ... - * entry: ax = K - * if ax != 0 goto +1 - * K = 1 - * r0 = 0 - * gotol pad - * rest: ax = K - * if ax == 0 goto +3 - * r0 = V - * r2 = 0 - * cancel_exit - * unwind_exit - * - * The current frame is FORCED from here on, and never anything else again: - * - The pad is verified once more, with what the frame holds when the kernel unwinds the callee, - * and with 0 in R0. An error on that way is an error of the program like any other. - * - A forced state and one of the normal path are never joined: the mark is in the join key. - * - No insn that returns to the caller can be reached from the pad, so the only ways out are - * unwind_exit of the program and the sites of the frame, see leave_cancelled_frame() and - * unwind_frame(). Both leave with cancel_exit and the value of the kernel. The program cannot - * go on, and it cannot change what it ends with. - * - What unwinds in the pad itself does not change that either. unwind_exit of a callee is - * taken where the program has unwind_goto for it, and the kernel that unwinds a callee - * enters the landing pad of that call as it did this one: K is set, and it stays set. - * - * At run time K says the same as the mark: a hidden word of the frame that is 0 from the entry of - * the function on and that only the entry of a pad sets, see bpf_pad_entry_insns(). + * Here the frame is marked BPF_FRAME_CANCELLED and gets 0 in R0, and the caller takes the jump. So + * the cleanup pad is verified a second time, for the cancelled frame. The mark is never cleared, + * and the verifier does not merge two states that differ in it. No insn that returns to the caller + * can be reached from a cleanup pad, so the frame leaves at the unwind_exit of the pad, see + * leave_cancelled_frame(), or at a site inside the pad, see unwind_frame(). Both leave with + * cancel_exit and the value of the cancellation. * * Returns PROCESS_BPF_EXIT if the state ends. */ -- 2.53.1-Meta