Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100041; b=GaR0jXo3Kv6V2ZO6h7peBnPtx5euj7LdCLwImlMNgg/eSgYpHb2Y3DJpKmArFcr6AxH6 fVRbO0BB+lSL/5nSprMtlgqLN6721VNWPfS0pcXK2y+Vvi77/N31i7Np3q5zSRPrDaDUq +U/lx/DFUucDP5ZuHbNO0h+AlaRzkamcd3KJj58b2eqt5XsvsBooJuRYbgRSw+Q9efetc CYNIBW5dqzX3bJvo0fH2hzETx+o8r/rc+E8JIjjpCEVaZSB4W4NqbnpAk2ueOKbSChtKH S31ZjcEKfZNrXueam9jo73lX44mAsb+O43lbBgNPt8/P+/kSe+Rkh4e1bM0ZaO53aIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100041; 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=E/fYZ6ZnKzML7ybeOh+K7ezlEihdMD9E8OW6QdKxc9k=; b=r5Bf6rSQhaAw7mLjR8Ju+IrTYH8TFMOlBSwDv+zJ4YnduSCaqNSDsOGSe8HD/JbUMZst HaJt/ubAbjZEQEBlwqPvYtnp5FXHXLdCNdHgY2yRilyexP+z3nITbreNW0W1TTUVtzKae xWWB5GSlN29mNFttNI4d4ZDWRNJNm+tOHqRhtMOUMf6TZy9O7C9trQt8/l33rgac1oeW1 uebNh55YCCD2y7gJzagiYocfRFaEz0VHrc4J8aCFZN+97Gl3rn1fN/cxpE67P/0KOiZ59 OXm98OxTo+JbarENAB4i95AEsDGi23yNTw5fXlA+iwM0SyAHXIwqcuy7JUil+ta2Xbw== 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=xsyslYav; arc=none smtp.remote-ip=74.125.231.195 Received: from mail-oi2-f3.google.com (mail-oi2-f3.google.com [74.125.231.195]) (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 1CDFE606A31A for ; Tue, 1 Dec 2026 04:40:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1CDFE606A31A 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=xsyslYav Received: by mail-oi2-f3.google.com with SMTP id 5614622812f47-4af85d1cb34so1049265b6e.0 for ; Mon, 30 Nov 2026 20:40:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100039; x=1796704839; 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=E/fYZ6ZnKzML7ybeOh+K7ezlEihdMD9E8OW6QdKxc9k=; b=xsyslYaveR3PZVXqs95g0WS5AK/DGWSTgaVtp8OkKC9LQ8gN62dmFctB1v9+svHOuo cMWFfyTAb7u7Hmad/6yEStPqioaIVRQBszhptlD0UCQyDkfwwlNHgfVBOIl4bSXMkXPj CFMoWvwAkcA9Tzt+82jiaIgLo5eVgYxLnltadTaQTDFkQSzUeVfjZImoFt1pDPiaDcFx SnGMHzvWeixrs3z4fp36DOQdjzoqBof2eCAG4t9TXgySFHLdPRJ3Yr8rCTQXe8eSAQ8I cxiU/B2PLD0nnil0cOHH6RhHO4dcxShQbCWLaLWrU1UI1SJoifDTRQdViVuLZidQepgS phWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100039; x=1796704839; 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=E/fYZ6ZnKzML7ybeOh+K7ezlEihdMD9E8OW6QdKxc9k=; b=znwfFNJxxeglSzDRRs45JOj/yDcyOspSfe6LEHDjh8HNdmwz2SO+n54GLYtTMe5gO8 XCtGp9uKu7ZeTWG2K4SIOXw6plD88Eps175koKN2OCCd4wJ+eeOtF/mWc2TfnpngsfGM bFTAAVqP9+5BSGufQu8UUuIhPTDuclaOyYFNzkIpw2tuRJnr4CvDP3C7Ers5iPvWq59I xeu+QR9K7xiWw03kPd6bFC73Aw8GgWQXdqSmleWeWjgHg9Mr9KJOtri4eGoFPbt0R4rm jUqHHbg7KC6Y4eB/ExLfVgWaW726eA7r1mIQ8HD7Q16BohA3lSwZlhC1S7oerhu+Ut2b gqng== X-Gm-Message-State: APuU4Z+ijpMg/qz2UunD2OLrVFBzhY+tocbz0prDBGF4iviJPHJh8FAF b6GM4EbaBe0VXYixmCpCz32rOW204yzgm6Ha0m3+pNuObfA0RzrTuwD8WT6wn1DQ8n0= X-Gm-Gg: AZFdTZeFxsAa/Pqc8YFyTayLvb3bfrSmSidsKWoEggEyM8j+Lb/uz+qTUQ5p3mL7lt0 OfjWPhl2JX2xWeCRMsT4MOg9PjJ/7GgtrffBD2rB8BqUypYl3D7N9ujS+HgS+h3iUyqtnj78KO9 jwraJzHTSXkiOl3qphx0XFyQ+qeTh32GylRxboKH1EVzR/p4JtAV/YpBDXGc6KYR8K3w/XTQOT3 92x7PvlG5aH2Mkay3U4s9wZ0r/NzOH/1LcQqSup7+EavmPsdQSK5VDmaOGxo+vdw/b305o7hR/M OVkqtWO096YeB/4eQlzOjzOEQTuAjeBH+oyGQejKwQmXrdLBJvi6eqlgYPiU6etwze+n/a+5jUF 2KymHbVhwzR0KnGRGPXvVH7XEWCoR4J+U6afI19xuf2tvvGMp+qya8Q5GkR5erJSzNsJOYC9O69 fCfEU+YWVMNMpIFMrp8EeZCVWc6w4YnffudaKqm8XEPL6qV0+/zayd2jZlp8mEin4a+KDzfgjll 7Pzuhv1sxqVAey2HNiXdYTPN1xTi5SMkQLNI2olT/Rfg563h30VELos13E237A4QcFniqw/1JFZ Hr4Wpsd4oc+ub88yOkxVvVDLA6ugpq9LvQPpIW51/PLy3Lnbh83McSD4Vbp55LJnlidpHu+M8fN JQ+ppBKuq4dH9rpDwATpSjsV1Msdm8RQ0fyt/ X-Received: by 2002:a05:6871:c7c3:b0:494:116a:b0f6 with SMTP id 586e51a60fabf-4bc8434fa4fmr21874576fac.21.1796100038638; Mon, 30 Nov 2026 20:40:38 -0800 (PST) Received: from localhost ([2a03:2880:10ff:41::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4bcf0794755sm30199982fac.10.2026.11.30.20.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:40:37 -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 14/73] bpf: Release locks and irq flags in landing pads Date: Tue, 1 Dec 2026 05:38:41 +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=38549; i=memxor@gmail.com; h=from:subject:message-id; bh=7T9UrCCIx+/5aqj4K/CFMXWLksoF1AuoJPfrbDi8TZg=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb46LVpQme06F2vk7nq4/kr81ZioGlCu8YV74Ns+k6hGL uGBHKQuDGBeDrJgiS8n/fUzGJyp/B9ou44aZw8oEMoSBi1MAJtJwieGv7KZ08bh9aWFc9xY/+7dJTf J5X3H6EoYLwQcmtVepSb4/x/A/9WRhvunZV4I/dz6oMEt8yKvax9DEvv/srGdScqVF4QmcAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit From: Alexei Starovoitov Currently a landing pad can only leave sections. In a frame that holds a lock or saved irq flags, a may_goto of the verifier still leaves the loop: bpf_spin_lock(&v->lock); do { i = v->idx; } while (i >= 4); r = v->t[i]; <- rejected: i can be anything bpf_spin_unlock(&v->lock); Let the pad release what bpf_spin_lock(), bpf_res_spin_lock(), bpf_res_spin_lock_irqsave() and bpf_local_irq_save() acquired: pad: r1 = r6 call bpf_spin_unlock r0 = 0; exit The release takes the address of the lock or of the irq flags. The pad loads it from a location: a register or a stack slot of the program that holds the address at the site, plus a constant. lock R6-R9 or a stack slot with a pointer r1 = r6 into the map value or the object r1 += 8 a global variable r1 = lock ll irq flags the stack of the frame r1 = r10 r1 += -16 the stack of a caller: R6-R9 or a r1 = r7 stack slot with a pointer to them A location has to be live at the site, because later passes reuse dead registers and remove dead stores. The slot of a bpf_fastcall spill is not a location either: its store can be removed too. All states at a site have to hold the same locks and irq flags, acquired in the same order and each with a location in common. A pad releases the youngest first. Otherwise the verifier cannot generate the pad, and the program is rejected. The log says, for example, "conflict: no place has the handle on all paths". A later patch lifts the case where no location holds the address on all paths. What changes for programs that load today: where may_goto left a loop under a lock or with irq flags saved and the program continued, the program now ends, or is rejected as above. In a frame that holds a reference, may_goto still leaves the loop. Signed-off-by: Alexei Starovoitov Co-developed-by: Kumar Kartikeya Dwivedi Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 51 +++- kernel/bpf/fixups.c | 55 +++- kernel/bpf/pads.c | 470 ++++++++++++++++++++++++++++++++++- kernel/bpf/verifier.c | 54 +++- 4 files changed, 598 insertions(+), 32 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index e9db40421411..d40bd855c539 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -382,6 +382,8 @@ struct bpf_reference_state { */ int insn_idx; u32 diag_id; /* finds the acquire in the report of a leak, it is not renumbered */ + /* the insn that brought it into its frame: insn_idx, or a call that returned with it */ + int origin; union { /* For REF_TYPE_PTR */ struct { @@ -458,6 +460,11 @@ enum bpf_pad_class { BPF_PAD_RCU, /* bpf_rcu_read_lock() */ BPF_PAD_PREEMPT, /* bpf_preempt_disable() */ BPF_PAD_NR_SECTIONS, + /* Each of the others is an entry of refs[], and its release takes a pointer */ + BPF_PAD_IRQ = BPF_PAD_NR_SECTIONS, /* bpf_local_irq_save() */ + BPF_PAD_RES_LOCK, /* bpf_res_spin_lock() */ + BPF_PAD_RES_LOCK_IRQ, /* bpf_res_spin_lock_irqsave(): two of refs[], one release */ + BPF_PAD_SPIN_LOCK, /* bpf_spin_lock() */ }; /* Why the verifier cannot generate one landing pad for all states that reach a site */ @@ -466,11 +473,47 @@ enum bpf_pad_conflict { BPF_CONFLICT_SECTIONS_DIFFER, BPF_CONFLICT_TOO_MUCH, BPF_CONFLICT_NO_RELEASE, + BPF_CONFLICT_NOT_ALWAYS_HELD, + BPF_CONFLICT_NO_PLACE, + BPF_CONFLICT_ORDER_DIFFERS, }; -/* The limits of a landing pad. The 3 of BPF_PAD_MAX_INSNS are the insns that leave the frame. */ +/* + * The limits of a landing pad. BPF_PAD_MAX_INSNS: a release is two insns per argument and the + * call. The 3 are the insns that leave the frame. + */ #define BPF_PAD_MAX_SECTIONS 8 -#define BPF_PAD_MAX_INSNS (BPF_PAD_NR_SECTIONS * BPF_PAD_MAX_SECTIONS + 3) +#define BPF_PAD_MAX_ENTS 8 +#define BPF_PAD_MAX_ARGS 2 +#define BPF_PAD_MAX_INSNS (BPF_PAD_MAX_ENTS * (2 * BPF_PAD_MAX_ARGS + 1) + \ + BPF_PAD_NR_SECTIONS * BPF_PAD_MAX_SECTIONS + 3) +#define BPF_PAD_MAX_CANDS 6 + +/* The kinds of location */ +enum bpf_place_loc { + BPF_PLACE_REG, /* a register. R10: the pointer is an address in the stack */ + BPF_PLACE_SPILL, /* 8 bytes of the stack of the frame */ + BPF_PLACE_MAP_VALUE, /* the address of the only value of a map */ +}; + +/* A location: where a landing pad finds the pointer that a release takes */ +struct bpf_pad_place { + u8 loc; /* enum bpf_place_loc */ + s16 at; /* number of the register | offset from R10 | index in used_maps */ + s32 delta; /* the location has the pointer plus 'delta' */ +}; + +/* One release of a landing pad: a resource of the frame, the call and where its arguments are */ +struct bpf_pad_ent { + u32 origin; /* the lowest 'origin' of the references that it stands for */ + int ref; /* its index in refs[] of the state that is being recorded, or -1 */ + struct bpf_insn release; /* the call that releases it */ + u8 class; /* enum bpf_pad_class */ + /* per argument of the call: the locations that all states share. The pad uses the first. */ + u8 nr_cands[BPF_PAD_MAX_ARGS]; + struct bpf_pad_place cand[BPF_PAD_MAX_ARGS][BPF_PAD_MAX_CANDS]; +}; + /* * The plan of a site: what a frame holds when it leaves there, and so what the landing pad * releases. bpf_pad_visit() records each state whose frame leaves at the site. One landing pad @@ -482,6 +525,8 @@ struct bpf_pad_plan { u8 conflict; /* enum bpf_pad_conflict */ bool visited; u8 sections[BPF_PAD_NR_SECTIONS]; /* how many of each class the frame has entered */ + u8 nr_ents; + struct bpf_pad_ent *ents; /* oldest first. The pad releases the last one first. */ /* the pad starts that many insns after the last insn of the subprog, once it is emitted */ u32 pad; }; @@ -1954,7 +1999,7 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, int insn_idx, enum bpf_site_kind kind); int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, int subprog, - struct bpf_insn *buf); + struct bpf_insn *buf, struct bpf_insn_aux_data *aux); void bpf_pad_log(struct bpf_verifier_env *env, int insn_idx, const struct bpf_pad_plan *plan); int bpf_pads_settle(struct bpf_verifier_env *env); bool bpf_pads_conflict(const struct bpf_insn_aux_data *aux); diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 0df3b5630b0b..154f9569b132 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -604,21 +604,29 @@ static int emit_unwind_exit(struct bpf_verifier_env *env, int s, struct bpf_insn * Returns the number of insns, BPF_PAD_MAX_INSNS + 1 at most. */ static int emit_pad(struct bpf_verifier_env *env, int s, const struct bpf_pad_plan *plan, - struct bpf_insn *buf) + struct bpf_insn *buf, struct bpf_insn_aux_data *aux) { int n = 0; - n += bpf_pad_insns(env, plan, s, buf + n); + n += bpf_pad_insns(env, plan, s, buf + n, aux + n); buf[n++] = unwind_exit_insn(env, s); return n; } +/* Does the site of @plan at an insn with @aux get a landing pad after its subprog? */ +static bool plan_has_pad(const struct bpf_insn_aux_data *aux, const struct bpf_pad_plan *plan) +{ + /* bpf_insert_recursion_checks() emits the pad of the check */ + return plan->kind != BPF_SITE_RECURSION && aux->loop_guard && aux->guard_unwinds; +} + /* * The landing pads of the sites of subprog @s go after its last insn. Sites whose pads have the - * same insns share one. @new has room for a pad. Returns the insns in @pads and their number. + * same insns share one. @new and @new_aux have room for a pad. Returns the insns in @pads and + * their number. */ static int collect_pads(struct bpf_verifier_env *env, int s, struct bpf_insn *new, - struct bpf_insn **pads) + struct bpf_insn_aux_data *new_aux, struct bpf_insn **pads) { int last = env->subprog_info[s + 1].start - 1, cnt = 0, i, n, pad, len; struct bpf_insn_aux_data *aux = env->insn_aux_data; @@ -632,10 +640,9 @@ static int collect_pads(struct bpf_verifier_env *env, int s, struct bpf_insn *ne if (!bpf_pad_plan_get(env, i, BPF_SITE_GUARD)) return -ENOMEM; for (plan = aux[i].pad_plans; plan; plan = plan->next) { - /* bpf_insert_recursion_checks() emits the pad of the check */ - if (plan->kind == BPF_SITE_RECURSION) + if (!plan_has_pad(&aux[i], plan)) continue; - n = emit_pad(env, s, plan, new); + n = emit_pad(env, s, plan, new, new_aux); for (pad = 0; pad < cnt; pad += len) { for (len = 1; buf[pad + len - 1].code != (BPF_JMP | BPF_EXIT); len++) @@ -677,23 +684,29 @@ static int collect_pads(struct bpf_verifier_env *env, int s, struct bpf_insn *ne * - The guard of a jump goes where the jump goes. * - What is live where the guard at the head of a loop goes is live at the head, * see bpf_head_guard_target(). - * - A landing pad reads no register. + * - A landing pad reads places, which are live before the insn. */ int bpf_insert_sites(struct bpf_verifier_env *env) { 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; + struct bpf_insn_aux_data *aux = env->insn_aux_data, *pad_aux; struct bpf_insn *pads = NULL, *new, *insn; const struct bpf_pad_plan *plan; struct bpf_subprog_info *sub; int i, s, cnt, err = 0; + int *added; + /* how many insns the pads after the subprog have */ + added = kvzalloc_objs(int, env->subprog_cnt, BPF_VERIFIER_GFP); new = kvzalloc_objs(*new, BPF_PAD_MAX_INSNS + 1, BPF_VERIFIER_GFP); - if (!new) - return -ENOMEM; + pad_aux = kvzalloc_objs(*pad_aux, BPF_PAD_MAX_INSNS + 1, BPF_VERIFIER_GFP); + if (!added || !new || !pad_aux) { + err = -ENOMEM; + goto out; + } for (s = 0; s < env->subprog_cnt && !err; s++) { sub = &env->subprog_info[s]; - cnt = collect_pads(env, s, new, &pads); + cnt = collect_pads(env, s, new, pad_aux, &pads); if (cnt < 0) { err = cnt; break; @@ -704,6 +717,7 @@ int bpf_insert_sites(struct bpf_verifier_env *env) err = -EFAULT; break; } + added[s] = cnt; for (i = sub->start; i < sub[1].start; i++) { if (!aux[i].loop_guard) continue; @@ -726,10 +740,25 @@ int bpf_insert_sites(struct bpf_verifier_env *env) if (cnt) err = bpf_patch_list_append(env, i - 1, pads, cnt); } + err = err ?: bpf_patch_list_commit(env); + if (err) + goto out; + /* the pads are in the program now, and their insns have aux data */ + aux = env->insn_aux_data; + 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(&aux[i], plan)) + emit_pad(env, s, plan, new, + &aux[sub[1].start - added[s] + plan->pad]); + } out: + kvfree(added); kvfree(new); + kvfree(pad_aux); kvfree(pads); - return err ?: bpf_patch_list_commit(env); + return err; } /* diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 26bba616ee50..1f6b084259fc 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -37,7 +37,16 @@ * the references, and the registers and stack slots that are live at the site. * * A pad releases what its own frame holds and nothing of the callers. For RCU and preempt sections - * the record has a count. A frame that holds anything else is not cancelled. + * the record has a count. Every other resource is one release of the record (struct bpf_pad_ent): a + * call, and for each argument a LOCATION (struct bpf_pad_place). A location is a register or a + * stack slot of the program that holds the pointer at the site, plus a constant offset. For a frame + * that holds a lock at R6 the pad is 'r1 = r6; call bpf_spin_unlock; r0 = 0; exit'. A pointer into + * a map value is a location for the lock in that value, and R10 is one for an object in the stack + * of the frame. A location has to be live at the site, because later passes reuse dead registers + * and remove dead stores, see find_cands(). + * + * All states at a site have to share a location for each release, and the pad has to know whether + * the resource is held. Only the program counter tells the pad, so all states have to hold it. */ #include #include @@ -50,24 +59,36 @@ BTF_ID_LIST(pad_kfuncs) BTF_ID(func, bpf_rcu_read_unlock) BTF_ID(func, bpf_preempt_enable) +BTF_ID(func, bpf_local_irq_restore) +BTF_ID(func, bpf_res_spin_unlock) +BTF_ID(func, bpf_res_spin_unlock_irqrestore) + +static struct bpf_insn helper_call(enum bpf_func_id func_id) +{ + return BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, func_id); +} /* The insn of a pad that releases one resource of @class */ static struct bpf_insn release_call(enum bpf_pad_class class) { + if (class == BPF_PAD_SPIN_LOCK) + return helper_call(BPF_FUNC_spin_unlock); return BPF_CALL_KFUNC(0, pad_kfuncs[class]); } /* - * A pad is going to call what @insn calls, if the program can call that. The passes that follow the - * emission look for the descriptor of a kfunc, which the program gets here. - * Returns the reason of a conflict. + * Check that the program may call what @insn calls, and add the kfunc descriptor that the passes + * after the emission look up. Returns 0, BPF_CONFLICT_NO_RELEASE or -ENOMEM. */ static int get_release(struct bpf_verifier_env *env, const struct bpf_insn *insn) { + const struct bpf_func_proto *fn; u32 log_level = env->log.level; struct bpf_call_arg_meta meta; int err; + if (bpf_helper_call(insn)) + return bpf_get_helper_proto(env, insn->imm, &fn) ? BPF_CONFLICT_NO_RELEASE : 0; env->log.level = 0; err = bpf_add_kfunc_call(env, insn->imm, insn->off); err = err ?: bpf_fetch_kfunc_arg_meta(env, insn->imm, insn->off, &meta); @@ -111,6 +132,7 @@ static void free_plans(struct bpf_pad_plan *plan) for (; plan; plan = next) { next = plan->next; + kfree(plan->ents); kfree(plan); } } @@ -173,6 +195,341 @@ static u32 frame_sections(const struct bpf_verifier_state *st, enum bpf_pad_clas return st->active_preempt_locks - frame->entry_preempt_locks; } +static int ref_class(const struct bpf_reference_state *ref) +{ + switch (ref->type) { + case REF_TYPE_IRQ: + return BPF_PAD_IRQ; + case REF_TYPE_LOCK: + return BPF_PAD_SPIN_LOCK; + case REF_TYPE_RES_LOCK: + return BPF_PAD_RES_LOCK; + case REF_TYPE_RES_LOCK_IRQ: + return BPF_PAD_RES_LOCK_IRQ; + default: + return -ENOENT; + } +} + +/* Returns the offset of the lock of @class in the type that @rec describes, or -ENOENT */ +static int lock_off(const struct btf_record *rec, enum bpf_pad_class class) +{ + if (class == BPF_PAD_SPIN_LOCK) + return btf_record_has_field(rec, BPF_SPIN_LOCK) ? rec->spin_lock_off : -ENOENT; + return btf_record_has_field(rec, BPF_RES_SPIN_LOCK) ? rec->res_spin_lock_off : -ENOENT; +} + +/* + * Add (@loc, @at) to @cand if @reg, which is there, points at a constant offset into the map value + * or the object that the lock @ref is in, as process_spin_lock() asks of an unlock. + */ +static void add_lock_cand(struct bpf_pad_place *cand, int *nr, const struct bpf_reg_state *reg, + const struct bpf_reference_state *ref, enum bpf_place_loc loc, int at) +{ + const struct btf_struct_meta *meta; + const struct btf_record *rec; + s64 delta; + int off; + + if (*nr == BPF_PAD_MAX_CANDS || reg->id != ref->id || !tnum_is_const(reg->var_off)) + return; + if (reg->type == PTR_TO_MAP_VALUE && reg->map_ptr == ref->ptr) { + rec = reg->map_ptr->record; + } else if (reg->type == (PTR_TO_BTF_ID | MEM_ALLOC) && reg->btf == ref->ptr) { + meta = btf_find_struct_meta(reg->btf, reg->btf_id); + rec = meta ? meta->record : NULL; + } else { + return; + } + off = lock_off(rec, ref_class(ref)); + delta = (s64)reg->var_off.value - off; + if (off < 0 || delta < -S32_MAX || delta > S32_MAX) + return; + cand[(*nr)++] = (struct bpf_pad_place){ .loc = loc, .at = at, .delta = delta }; +} + +/* Add (@loc, @at) to @cand if @reg points to the stack of the frame of @obj at a constant offset */ +static void add_obj_cand(struct bpf_pad_place *cand, int *nr, const struct bpf_reg_state *reg, + const struct bpf_reg_state *obj, enum bpf_place_loc loc, int at) +{ + s64 delta = reg->var_off.value - obj->var_off.value; + + if (*nr == BPF_PAD_MAX_CANDS || reg->type != PTR_TO_STACK || reg->frameno != obj->frameno || + !tnum_is_const(reg->var_off) || delta < -S32_MAX || delta > S32_MAX) + return; + cand[(*nr)++] = (struct bpf_pad_place){ .loc = loc, .at = at, .delta = delta }; +} + +static void add_cand(struct bpf_pad_place *cand, int *nr, const struct bpf_reg_state *reg, + const struct bpf_reference_state *ref, const struct bpf_reg_state *obj, + enum bpf_place_loc loc, int at) +{ + if (obj) + add_obj_cand(cand, nr, reg, obj, loc, at); + else + add_lock_cand(cand, nr, reg, ref, loc, at); +} + +/* + * The places of the current frame of @st that have the address of the lock @ref at @insn_idx: R6-R9 + * and stack slots. If @ref is an object in the stack of a caller @obj is its address. + * A register or a stack slot that is not live there is no place: the passes that follow use + * such a register, remove the store to such a slot, and states that differ in it are the same to + * the main pass. Nor is a slot of the spills around bpf_fastcall calls: they are removed unless the + * main pass sees another access to the slot, and it does not see the pad. + */ +static int find_cands(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + int insn_idx, const struct bpf_reference_state *ref, + const struct bpf_reg_state *obj, struct bpf_pad_place *cand) +{ + const struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; + const struct bpf_func_state *frame = st->frame[st->curframe]; + int fastcall_off = env->subprog_info[frame->subprogno].fastcall_stack_off; + const struct bpf_stack_state *slot; + int i, off, nr = 0; + + for (i = BPF_REG_6; i < BPF_REG_FP; i++) + if (aux->live_regs_before & BIT(i)) + add_cand(cand, &nr, &frame->regs[i], ref, obj, BPF_PLACE_REG, i); + for (i = 0; i < bpf_stack_nr_slots(frame); i++) { + slot = bpf_stack_slot(frame, i); + off = -(i + 1) * BPF_REG_SIZE; + if (test_bit(i, aux->live_stack_before) && bpf_spill_size(slot) == BPF_REG_SIZE && + off >= fastcall_off) + add_cand(cand, &nr, &slot->spilled_ptr, ref, obj, BPF_PLACE_SPILL, off); + } + return nr; +} + +/* The candidates for the address of the lock @ref: a constant address, then find_cands() */ +static int find_lock_cands(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + int insn_idx, const struct bpf_reference_state *ref, + struct bpf_pad_place *cand) +{ + const struct bpf_map *map; + int i, off, nr = 0; + u64 addr; + + /* a lock in a global variable has no id: the only value of an array, a constant address */ + for (i = 0; !ref->id && i < env->used_map_cnt; i++) { + map = env->used_maps[i]; + off = lock_off(map->record, ref_class(ref)); + if (map != ref->ptr || map->map_type != BPF_MAP_TYPE_ARRAY || off < 0 || + map->ops->map_direct_value_addr(map, &addr, off)) + continue; + cand[nr++] = (struct bpf_pad_place){ .loc = BPF_PLACE_MAP_VALUE, .at = i, + .delta = -off }; + } + return ref->id ? find_cands(env, st, insn_idx, ref, NULL, cand) : nr; +} + +/* Is slot @spi of @frame the irq flags @ref? */ +static bool slot_is_obj(const struct bpf_func_state *frame, int spi, + const struct bpf_reference_state *ref) +{ + const struct bpf_reg_state *reg = &bpf_stack_slot(frame, spi)->spilled_ptr; + + switch (bpf_stack_slot(frame, spi)->slot_type[BPF_REG_SIZE - 1]) { + case STACK_IRQ_FLAG: + return reg->id == ref->id; + } + return false; +} + +/* + * If @ref is an object in the stack: the places that have its address at @insn_idx. R10 if the + * current frame of @st has the object. If a caller has it, what find_cands() finds: the function + * got a pointer and made the object there, like the constructor of a guard. + * *@obj: the first slot of the object. + */ +static int find_obj_cands(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + int insn_idx, const struct bpf_reference_state *ref, + struct bpf_pad_place *cand, const struct bpf_stack_state **obj) +{ + const struct bpf_func_state *frame; + struct bpf_reg_state addr; + int f, i, nr = 0; + + for (f = st->curframe; f >= 0 && !nr; f--) { + frame = st->frame[f]; + for (i = 0; i < bpf_stack_nr_slots(frame) && nr < BPF_PAD_MAX_CANDS; i++) { + if (!slot_is_obj(frame, i, ref)) + continue; + *obj = nr ? *obj : bpf_stack_slot(frame, i); + if (f == st->curframe) { + cand[nr++] = (struct bpf_pad_place){ .loc = BPF_PLACE_REG, + .at = BPF_REG_FP, + .delta = (i + 1) * BPF_REG_SIZE }; + continue; + } + addr = frame->regs[BPF_REG_FP]; + addr.var_off = tnum_const(-(i + 1) * BPF_REG_SIZE); + return find_cands(env, st, insn_idx, ref, &addr, cand); + } + } + return nr; +} + +/* Copy the candidates for argument @arg that @ent and @new share to @cand. Returns their number. */ +static int common_cands(const struct bpf_pad_ent *ent, const struct bpf_pad_ent *new, int arg, + struct bpf_pad_place *cand) +{ + const struct bpf_pad_place *old = ent->cand[arg], *place = new->cand[arg]; + int i, j, n = 0; + + for (i = 0; i < ent->nr_cands[arg]; i++) { + for (j = 0; j < new->nr_cands[arg]; j++) { + if (old[i].loc != place[j].loc || old[i].at != place[j].at || + old[i].delta != place[j].delta) + continue; + cand[n++] = old[i]; + break; + } + } + return n; +} + +/* + * Fill @ent with the release of @ref, one of refs[] of @st, for a pad at @insn_idx: the call that + * releases it, and the candidate locations of its arguments in the current frame. Returns 0, the + * enum bpf_pad_conflict that says why the pad cannot be generated, or a negative errno. + */ +static int ref_ent(struct bpf_verifier_env *env, struct bpf_pad_ent *ent, + const struct bpf_verifier_state *st, int insn_idx, + const struct bpf_reference_state *ref) +{ + const struct bpf_stack_state *obj = NULL; + int class = ref_class(ref); + + if (verifier_bug_if(class < 0, env, "no landing pad releases reference type %d", ref->type)) + return -EFAULT; + memset(ent, 0, sizeof(*ent)); + ent->origin = ref->origin; + ent->class = class; + ent->release = release_call(class); + if (class == BPF_PAD_IRQ) + ent->nr_cands[0] = find_obj_cands(env, st, insn_idx, ref, ent->cand[0], &obj); + else + ent->nr_cands[0] = find_lock_cands(env, st, insn_idx, ref, ent->cand[0]); + if (class == BPF_PAD_RES_LOCK_IRQ) { + /* the same insn saved irq flags, the next of refs[]: the second argument */ + if (verifier_bug_if(ref + 1 == st->refs + st->acquired_refs || + ref[1].type != REF_TYPE_IRQ || ref[1].insn_idx != ref->insn_idx, + env, "no irq flags after the lock of insn %d", ref->insn_idx)) + return -EFAULT; + ent->nr_cands[1] = find_obj_cands(env, st, insn_idx, ref + 1, ent->cand[1], &obj); + if (!ent->nr_cands[1]) + return BPF_CONFLICT_NO_PLACE; + } + return ent->nr_cands[0] ? 0 : BPF_CONFLICT_NO_PLACE; +} + +/* Returns the release of @plan that refs[@ref] of the current state was matched with, or NULL */ +static struct bpf_pad_ent *held_ent(const struct bpf_pad_plan *plan, int ref) +{ + int i; + + for (i = 0; i < plan->nr_ents; i++) + if (plan->ents[i].ref == ref) + return &plan->ents[i]; + return NULL; +} + +/* + * Returns a release of @plan that can stand for @new too, or NULL: no other reference of the + * current state is matched with it, the same call releases both, and they share a candidate for + * each argument. With @origin the same insn brought both to the frame. + */ +static struct bpf_pad_ent *match_ent(const struct bpf_pad_plan *plan, const struct bpf_pad_ent *new, + bool origin) +{ + struct bpf_pad_place cand[BPF_PAD_MAX_CANDS]; + struct bpf_pad_ent *ent; + int i, arg; + + for (i = 0; i < plan->nr_ents; i++) { + ent = &plan->ents[i]; + if (ent->ref >= 0 || (origin && ent->origin != new->origin) || + memcmp(&ent->release, &new->release, sizeof(ent->release))) + continue; + for (arg = 0; arg < BPF_PAD_MAX_ARGS; arg++) + if (new->nr_cands[arg] && !common_cands(ent, new, arg, cand)) + break; + if (arg == BPF_PAD_MAX_ARGS) + return ent; + } + return NULL; +} + +/* @new is one more entry of @plan. Returns it, or NULL if there is no memory. */ +static struct bpf_pad_ent *add_ent(struct bpf_pad_plan *plan, const struct bpf_pad_ent *new) +{ + if (!plan->ents) + plan->ents = kzalloc_objs(*plan->ents, BPF_PAD_MAX_ENTS, BPF_VERIFIER_GFP); + if (!plan->ents) + return NULL; + plan->ents[plan->nr_ents] = *new; + return &plan->ents[plan->nr_ents++]; +} + +/* + * The resources with a handle that the current frame of @st acquired: refs[] from + * entry_refs on, oldest first. Which entry is one of them? What a pad does with an entry is a call + * with what it finds at a place, so that is what an entry is, like a variable with a destructor. It + * does not matter which insn acquired what is there. Where that leaves a choice the origin decides. + * Every visit has to hold every entry. + * Locks and irq flags have to be in refs[] in the order of the plan. + * Returns the reason of a conflict. + */ +static int visit_refs(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, + const struct bpf_verifier_state *st, int insn_idx) +{ + const struct bpf_reference_state *ref; + struct bpf_pad_ent new, *ent; + int i, arg, pass, pos, err; + + for (i = 0; i < plan->nr_ents; i++) + plan->ents[i].ref = -1; + for (pass = 0; pass < 2; pass++) { + for (i = st->frame[st->curframe]->entry_refs; i < st->acquired_refs; i++) { + ref = &st->refs[i]; + /* irq flags that a lock saved are an argument of its release */ + if (i && ref[-1].type == REF_TYPE_RES_LOCK_IRQ) + continue; + if (held_ent(plan, i)) + continue; + err = ref_ent(env, &new, st, insn_idx, ref); + if (err) + return err; + ent = match_ent(plan, &new, !pass); + if (!ent && !pass) + continue; + if (!ent && plan->visited) + return BPF_CONFLICT_NOT_ALWAYS_HELD; + if (!ent && plan->nr_ents == BPF_PAD_MAX_ENTS) + return BPF_CONFLICT_TOO_MUCH; + ent = ent ?: add_ent(plan, &new); + if (!ent) + return -ENOMEM; + ent->ref = i; + for (arg = 0; arg < BPF_PAD_MAX_ARGS; arg++) + ent->nr_cands[arg] = common_cands(ent, &new, arg, ent->cand[arg]); + } + } + for (i = st->frame[st->curframe]->entry_refs, pos = 0; i < st->acquired_refs; i++) { + ent = held_ent(plan, i); + if (!ent) + continue; + if (ent - plan->ents < pos) + return BPF_CONFLICT_ORDER_DIFFERS; + pos = ent - plan->ents; + } + for (i = 0; i < plan->nr_ents; i++) + if (plan->ents[i].ref < 0) + return BPF_CONFLICT_NOT_ALWAYS_HELD; + return 0; +} + /* * The current frame of @st is about to be cancelled at the site of @kind at @insn_idx. Add what it * holds to the record of the site, also if it holds nothing. Returns the record, whose 'conflict' @@ -189,12 +546,17 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, { struct bpf_pad_plan *plan; u32 class, nr; + int err; plan = bpf_pad_plan_get(env, insn_idx, kind); if (!plan) return ERR_PTR(-ENOMEM); if (plan->conflict) return plan; + err = visit_refs(env, plan, st, insn_idx); + if (err < 0) + return ERR_PTR(err); + plan->conflict = err; for (class = 0; class < BPF_PAD_NR_SECTIONS; class++) { nr = frame_sections(st, class); if (nr > BPF_PAD_MAX_SECTIONS) @@ -208,14 +570,66 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, } /* - * The insns of the landing pad of @plan, a site in @subprog, but for the last one, which leaves the - * frame. Returns their number, less than BPF_PAD_MAX_INSNS. + * R@reg = the handle that is at @place. Returns the number of insns. + */ +static int load_handle(struct bpf_verifier_env *env, struct bpf_insn *buf, + struct bpf_insn_aux_data *aux, int reg, const struct bpf_pad_place *place) +{ + const struct bpf_map *map; + u64 addr = 0; + int n = 0; + + switch (place->loc) { + case BPF_PLACE_REG: + buf[n++] = BPF_MOV64_REG(reg, place->at); + break; + case BPF_PLACE_SPILL: + buf[n++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_FP, place->at); + break; + case BPF_PLACE_MAP_VALUE: + /* what check_and_resolve_insns() makes of such an insn */ + map = env->used_maps[place->at]; + map->ops->map_direct_value_addr(map, &addr, -place->delta); + addr -= place->delta; + aux[n].map_index = place->at; + aux[n].map_off = -place->delta; + buf[n++] = BPF_RAW_INSN(BPF_LD | BPF_DW | BPF_IMM, reg, BPF_PSEUDO_MAP_VALUE, 0, + (u32)addr); + buf[n++] = BPF_RAW_INSN(0, 0, 0, 0, addr >> 32); + return n; + } + if (place->delta) + buf[n++] = BPF_ALU64_IMM(BPF_ADD, reg, -place->delta); + return n; +} + +/* The insns that release the entries of @plan, the youngest first */ +static int release_ents(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, + struct bpf_insn *buf, struct bpf_insn_aux_data *aux) +{ + const struct bpf_pad_ent *ent; + int i, arg, n = 0; + + for (i = plan->nr_ents - 1; i >= 0; i--) { + ent = &plan->ents[i]; + for (arg = 0; arg < BPF_PAD_MAX_ARGS && ent->nr_cands[arg]; arg++) + n += load_handle(env, &buf[n], &aux[n], BPF_REG_1 + arg, &ent->cand[arg][0]); + buf[n++] = ent->release; + } + return n; +} + +/* + * Emit the insns of the pad of @plan, a site in @subprog, except the last one, which leaves the + * frame. The main pass does not see these insns, so the aux data that later passes look up for them + * goes to @aux, one element per insn. Returns the number of insns, at most BPF_PAD_MAX_INSNS. */ int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, int subprog, - struct bpf_insn *buf) + struct bpf_insn *buf, struct bpf_insn_aux_data *aux) { - int class, i, n = 0; + int class, i, n; + n = release_ents(env, plan, buf, aux); for (class = 0; class < BPF_PAD_NR_SECTIONS; class++) for (i = 0; i < plan->sections[class]; i++) buf[n++] = release_call(class); @@ -229,8 +643,10 @@ int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, static int settle_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) { struct bpf_insn release; - int class, err = plan->conflict; + int i, class, err = plan->conflict; + for (i = 0; !err && i < plan->nr_ents; i++) + err = get_release(env, &plan->ents[i].release); for (class = 0; !err && class < BPF_PAD_NR_SECTIONS; class++) { release = release_call(class); if (plan->sections[class]) @@ -239,6 +655,36 @@ static int settle_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) return err; } +/* 'spin_lock@5(r6-8)': what insn 5 brought to the frame, the arguments of its release */ +static void print_ent(struct bpf_verifier_env *env, const struct bpf_pad_ent *ent) +{ + static const char * const class[] = { + [BPF_PAD_IRQ] = "irq", + [BPF_PAD_RES_LOCK] = "res_lock", + [BPF_PAD_RES_LOCK_IRQ] = "res_lock_irq", + [BPF_PAD_SPIN_LOCK] = "spin_lock", + }; + const struct bpf_pad_place *place; + int arg; + + verbose(env, "%s@%d(", class[ent->class], ent->origin); + for (arg = 0; arg < BPF_PAD_MAX_ARGS && ent->nr_cands[arg]; arg++) { + place = &ent->cand[arg][0]; + verbose(env, "%s", arg ? ", " : ""); + if (place->loc == BPF_PLACE_SPILL) + verbose(env, "*fp%d", place->at); + else if (place->loc == BPF_PLACE_MAP_VALUE) + verbose(env, "map#%d", place->at); + else if (place->at == BPF_REG_FP) + verbose(env, "fp"); + else + verbose(env, "r%d", place->at); + if (place->delta) + verbose(env, "%+d", -place->delta); + } + verbose(env, ") "); +} + /* Print what the pad of the site at insn @i releases, or why it cannot be generated */ static void print_plan(struct bpf_verifier_env *env, int i, const struct bpf_pad_plan *plan) { @@ -250,9 +696,15 @@ static void print_plan(struct bpf_verifier_env *env, int i, const struct bpf_pad [BPF_CONFLICT_SECTIONS_DIFFER] = "the number of sections differs", [BPF_CONFLICT_TOO_MUCH] = "too much is held", [BPF_CONFLICT_NO_RELEASE] = "the program can call nothing that releases a resource", + [BPF_CONFLICT_NOT_ALWAYS_HELD] = "a resource is held on some paths only", + [BPF_CONFLICT_NO_PLACE] = "no place has the handle on all paths", + [BPF_CONFLICT_ORDER_DIFFERS] = "the order of the releases differs", }; + int j; verbose(env, "landing pad of %s at %d: ", kind[plan->kind], i); + for (j = plan->nr_ents - 1; !plan->conflict && j >= 0; j--) + print_ent(env, &plan->ents[j]); if (plan->conflict) verbose(env, "conflict: %s\n", why[plan->conflict]); else diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d8c2d239867a..a1c4415266ed 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1547,6 +1547,7 @@ static struct bpf_reference_state *acquire_reference_state(struct bpf_verifier_e if (err) return NULL; state->refs[new_ofs].insn_idx = insn_idx; + state->refs[new_ofs].origin = insn_idx; return &state->refs[new_ofs]; } @@ -11874,8 +11875,10 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal err = resize_reference_state(caller, exit->acquired_refs); if (err) return err; - for (i = nheld; i < exit->acquired_refs; i++) + for (i = nheld; i < exit->acquired_refs; i++) { caller->refs[i] = exit->refs[i]; + caller->refs[i].origin = call_idx; + } caller->held = exit->held; nregs = bpf_ret_reg_pair(env, callee->subprogno) ? 2 : 1; @@ -12623,6 +12626,9 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) bpf_diag_mod_end(env); } hide_returned_stack_ptrs(env, nregs, caller->regs); + /* To the caller, the call acquired what the callee still holds. */ + for (i = callee->entry_refs; i < state->acquired_refs; i++) + state->refs[i].origin = callee->callsite; } /* Invalidate callee-local dynptrs and their slices before the frame goes away. */ @@ -21601,7 +21607,7 @@ static bool frame_holds_resources(const struct bpf_verifier_state *st) * * False when it cannot: * - This frame acquired something itself, and there is no landing pad for that: the verifier - * emits one for sections only. + * emits none for references. * - There is a call of the subprog that unwind_goto doesn't follow. That is every * program that is compiled without unwinding. * - A helper that calls a callback doesn't expect it to unwind. @@ -21616,6 +21622,7 @@ static bool state_can_unwind(struct bpf_verifier_env *env, const struct bpf_veri { const struct bpf_func_state *frame = st->frame[st->curframe]; const struct bpf_subprog_info *sub = &env->subprog_info[frame->subprogno]; + int i; /* unwind_frame() needs an insn at which the state leaves the function. */ if (sub->exit_idx == U32_MAX) @@ -21623,8 +21630,9 @@ static bool state_can_unwind(struct bpf_verifier_env *env, const struct bpf_veri /* A landing pad has no nospec barrier, so only a loader with CAP_PERFMON gets one. */ if (!bpf_may_unwind(env)) return false; - if (st->acquired_refs != frame->entry_refs) - return false; + for (i = frame->entry_refs; i < st->acquired_refs; i++) + if (st->refs[i].type == REF_TYPE_PTR) + return false; if (frame->in_callback_fn) return false; return frame_is_top(st) || sub->bail_unwinds || or_exit; @@ -21737,17 +21745,49 @@ static int check_loop_guard(struct bpf_verifier_env *env) return err; } +/* Irq flags, @id, are released. A caller may have the object: it is none any more. */ +static void unmark_stack_obj(struct bpf_verifier_env *env, int id) +{ + u32 mask = 1 << STACK_IRQ_FLAG; + struct bpf_stack_state *slot; + struct bpf_func_state *frame; + struct bpf_reg_state *reg; + + bpf_for_each_reg_in_vstate_mask(env->cur_state, frame, reg, slot, mask, ({ + if (slot && reg->id == id) { + bpf_mark_reg_not_init(env, reg); + memset(slot->slot_type, STACK_INVALID, sizeof(slot->slot_type)); + } + })); +} + /* * The current frame is left at a site, and its landing pad releases what the frame acquired since * its entry. Do to the current state what the calls of the pad do to it, with the functions that - * such a call of the program ends up in. The callers may have pointers that are good as long as a - * lock or a section is held. + * such a call of the program ends up in, and unmark_stack_obj(). The callers may have pointers that + * are good as long as a lock or a section is held. */ static int release_frame_resources(struct bpf_verifier_env *env) { struct bpf_verifier_state *st = env->cur_state; struct bpf_func_state *frame = cur_func(env); - + u32 locks = st->active_locks; + struct bpf_reference_state *ref; + int id, err; + + while (st->acquired_refs > frame->entry_refs) { + ref = &st->refs[st->acquired_refs - 1]; + id = ref->id; + if (ref->type == REF_TYPE_IRQ) + err = release_irq_state(env, ref->id); + else + err = release_lock_state(env, ref->type, ref->id, ref->ptr); + if (verifier_bug_if(err, env, "the youngest of refs[] cannot be released")) + return -EFAULT; + unmark_stack_obj(env, id); + } + if (st->active_locks != locks) + invalidate_non_owning_refs(env); st->active_rcu_locks = frame->entry_rcu_locks; st->active_preempt_locks = frame->entry_preempt_locks; if (!in_rcu_cs(env)) -- 2.53.1-Meta