Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100081; b=NCwAMCN/li0xsq2nFa1V8aFXIfKC8Hch2KBs4+sp3wDynVXBY0bmmbcNixb+gUUl127Y VdH6qGKgYIWDL8HDD5L1gegsjkoZ1Zsi1jeksJ94cYWsTWOmObmw3bC3PN+f25I7eieUm dGF5dGGTrJ81JQyTU01cheASWzqdyeQ/gVT0Cw3S0h1CI375AV1HTN4ucbAk3FR/pnnBP MpwRIqYfjwaXryom8uOwF350lWpQk/PnCYksMB+ij137+6yFEmizWS/NP34iF+8gQA8r7 XPbLK1F5/b1y8jwkodYEGZdyimrIa1fHHaHa2Btuqt1ZF1hSSsWMDEfiJzOyM/nInuw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100081; 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=VmkNbVWnI61MMN9/rogjQaO0Ik11y1lYk8/3YAWWhrg=; b=XVmd/wWTmrITPMPe1w/4D2zXO3Mpb7Bc82c7KBcQtiexH9olhEqzG6e5dc1Jh32caggA vL0UvslEcuieeAnJ6Ilp+B00cN/Q3sz6YjkZNAlPrSklhE4blWweesrslppL617z7XyZH QxZ7Q0aFYwSd+zd8A/G+7qjwAu51d5NAf0OVQIpCshi3ScdSRCTaSRGyDXp6TS5Py+VHX /HuRsQgyFyh2WsrT0eB0QN21CQYjmH+w588m2j4PLvueYGX363f/dkvLxt2jxqFSattnc 56QwE24K2LSEh4Y0ISjlz8LrYcyTqna+nBKOBXbR/l+7g+sQ8iVZL2HXb5bw08Hnjhg== 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=MbgvsdjF; arc=none smtp.remote-ip=74.125.231.139 Received: from mail-oo2-f11.google.com (mail-oo2-f11.google.com [74.125.231.139]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 439AC606A31A for ; Tue, 1 Dec 2026 04:41:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 439AC606A31A 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=MbgvsdjF Received: by mail-oo2-f11.google.com with SMTP id 006d021491bc7-71b7aac1211so2140516eaf.0 for ; Mon, 30 Nov 2026 20:41:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100080; x=1796704880; 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=VmkNbVWnI61MMN9/rogjQaO0Ik11y1lYk8/3YAWWhrg=; b=MbgvsdjFKiPmiitRPOjBaX6hxDubxSH0uusawWm0pKzI7n8BBHqI2IsAGM7BXzmD9M 7wrYHepiheVPX/D/NdAGzLMmaeOFoicYbY39gehoy4giuwwfM7UAeeyVCtbPKtGqNLHc Afcewbxcx+mOgTZITnpyGdEvuAnK451ufKTFBwS2TfittBuoi0AnGWG9O6VRcihRBia/ /VG8AApLs0i8EfGQPtFFoX1uT0rOZTclCZazg4TcTfejGgTKfi5brnBMR9xf436hs65w O3TSxfWlvKHADHpspHxUWVhiJNVUw/I+PoUcTIacVNny5MpTywoQiClE15049e4NN132 fR2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100080; x=1796704880; 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=VmkNbVWnI61MMN9/rogjQaO0Ik11y1lYk8/3YAWWhrg=; b=e9iB7OiQKbstOh0hkbVx6rSAQb7uQJMH4HUjZxAJ/mj0G8fFaIs85p5z97/ZVVEiQk I4rPTkocYP3sbhwjb783M7r1hbz5kSKvvzwhe4W2esh5DMGlBrw26Bwsz46eYnYbZxKe 5LAJ4zACTAKPOObdEPS3d+mPWnKaUmA60FTn3NN6/a6IjsWOGVi456pkQnkquDQW18+q MV/JoJta8tdgnpKTqrbRIn8NqApv90KkEjTwvDjQ/78LvIG7svZDgjO+a3aP7N4qxh+0 fg7JyxHJHkp9RNjldNszmQNJ1rg0rNOog4PJWTMEEVJdZYSeHSuhtQAz91rlerGQcpt/ DeTQ== X-Gm-Message-State: APuU4Z/xIHaSHI3Jx7meXnuJ6VDnso9ddsXWIfpQXMTtYf+MWrCvFzVv L9SN71GwDWN5iCm6gMr3QNfIPbW64IG4QhiSzfxCRi7/sII0AyTOvQWOWQtIAEX4B+A= X-Gm-Gg: AZFdTZedluBr3f6t0OJT/T4Ux0MFQoJEN9eWEWtbJPShnU/LXsct76RWtWI8tBrrqaL C3K5eup8jao3XwXfw7Vf3svvWNIDWyUrhvhXgTV8Ibe/1SvCnWUg5/VDmLi+ae44i4Mx7BYq5ZK 9DOrPkWwl12Ud+dwdw8AnA/4I/2RE1ZexlwiocT/vfxKh/+2tZDobHoRcFwWtfD9UICvt1/ovtk u1j9laWdq0EWztVXG0T8nrkIbiK38q5Gwt+tWlwxXWnylb9k0YlXG416f7C6MyriE0WA6Xp6+V0 fSER+DJSmaxpb5AMeCR1qS4yAOjPH2bbUGUhnK1gQfxrfm0p3z6Y+dmA9239cri8yVfo+pJ0Zi1 ZRp2eZ3rN6dxhRhueLzJ4V95lpk0qoovMM5zfJSnFcMEs+TJq++XdPMaKfnj4I4Knc1Rxk6cjDW elO/Axx8jd6fT9t2NTenGIb78e+0Gir9dLhLWq6qstYShDaDMeea+ocS/hSkkYqodNwUsfNGl98 EEROT8KnAcsynnvUlNs8rHquCNofK76ne3F5fxaKA3lFwYzuhkcW3PHzvUAjpB3XOAawyK2ExsO DEK//Qc1WTRP88BVY7crC1yWMto0TOlSeESAgj82YYgDKkyQ+TAIEkHj8E3HV0H3IXrSf0elQaw i80hAAxW1RqHXOmOnuiQEVdnmGNO2MkLvfm3Qdp3P8kDtyL7tdv/wdodNUA== X-Received: by 2002:a05:6820:c31b:b0:70e:2096:8070 with SMTP id 006d021491bc7-71b39482d41mr24739431eaf.35.1796100080368; Mon, 30 Nov 2026 20:41:20 -0800 (PST) Received: from localhost ([2a03:2880:10ff:43::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863168f6d9asm26640214a34.17.2026.11.30.20.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:19 -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 26/73] bpf: Use hidden slots for a pointer that has no location at a site Date: Tue, 1 Dec 2026 05:38:53 +0100 Message-ID: <883a9ff65366f6b2c07484aa438929846ba5aec2.1796099737.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=9024; i=memxor@gmail.com; h=from:subject:message-id; bh=gP1/cCCRptG8KOTER3fBpTbE7Qzi9W/MWI8Ri3dW1Es=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb95u/YcLp32bULe45bPC0jmeXqxbHHeuuz6hPHDhKnfz +xsiOkpZGMS4GGTFFFlK/u9jMj5R+TvQdhk3zBxWJpAhDFycAjAR3VeMDM/Z598MjJ64c6VWnH7rDA FRD+sH+3NXPODy4bh64YmwzmWGf/Zr98/cYnn+4fk/t59cPa521+x35fONrP93iMlcY3uwxJoDAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit From: Alexei Starovoitov Hidden slots help where a landing pad cannot tell whether a pointer is held. Currently they do not help where the pad cannot find the pointer, because no register or stack slot of the frame holds it at the site: t = bpf_task_from_pid(1); /* and a NULL check */ t = wait(t); /* can be cancelled, returns t */ bpf_task_release(t); When the program takes t from R0 after the call, t is in R1 alone at the call. If the kernel cancels the program in wait(), the landing pad after the call has to release t, but the call has clobbered R1-R5. The program is rejected. (With clang this shape takes a wait() that the compiler does not look into, or asm. Otherwise clang keeps t in R6 over the call.) A hidden slot does not depend on where the program keeps the pointer. So let a pointer without a location ask for hidden slots as well. '+' marks what the verifier adds, and ax is BPF_REG_AX: + *(u64 *)(r10 - 8) = 0 entry call bpf_task_from_pid + *(u64 *)(r10 - 8) = r0 acquire ... r1 = r0 call wait t is in r1 alone + unwind_goto pad r1 = r0 + ax = *(u64 *)(r10 - 8); if ax != r1 goto +1 release + *(u64 *)(r10 - 8) = 0 call bpf_task_release ... + pad: r1 = *(u64 *)(r10 - 8); if r1 == 0 goto +1 + call bpf_task_release + r0 = 2; exit TC_ACT_SHOT (tc) A function gets hidden slots for this only if none of its insns acquires an iterator or a dynptr, see acquires_stack_obj(). The hidden slot of such an object holds its address in the stack of the program, and the landing pad destroys what is at that address. To the verifier an iterator and a dynptr are references like a pointer, so a state that holds a task without a location may stand for a state that holds an iterator at fp-8, which the program may have overwritten since. In a function that acquires neither, such a reference is always a pointer, and its slot holds it. A function that cannot have hidden slots for references is still rejected. Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 1 + kernel/bpf/pads.c | 56 +++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 7027734c315e..ae407b6a14ab 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -560,6 +560,7 @@ struct bpf_pad_plan { bool failed; /* a state made the conflict an error of the pass */ u8 sections[BPF_PAD_NR_SECTIONS]; /* how many of each class the frame has entered */ u8 sections_differ; /* bit per class: the states differ in the number */ + u8 no_place; u8 nr_ents; bool ordered; /* see order_ents() */ struct bpf_pad_ent *ents; /* oldest first. The pad releases the last one first. */ diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index a9a2b56364ab..7dbb09ce6c2b 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -1098,6 +1098,12 @@ static int visit_refs(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, if (held_ent(plan, i)) continue; err = ref_ent(env, &new, st, insn_idx, ref); + /* no location: a hidden slot may hold it, see plan_needs_slots() */ + if (ref->type == REF_TYPE_PTR && + (err == BPF_CONFLICT_NO_PLACE || err == BPF_CONFLICT_CALLER_SAVED)) { + plan->no_place = err; + continue; + } if (err) return err; ent = match_ent(plan, &new, !pass); @@ -1617,6 +1623,7 @@ static int settle_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) if (!err && plan->sections_differ) err = BPF_CONFLICT_SECTIONS_DIFFER; + err = err ?: plan->no_place; for (i = 0; !err && i < plan->nr_ents; i++) err = settle_ent(env, plan, &plan->ents[i]); for (class = 0; !err && class < BPF_PAD_NR_SECTIONS; class++) { @@ -1628,16 +1635,38 @@ static int settle_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) } /* - * The groups of which @plan has something that its pad cannot tell whether it is held. None for a - * site that the program does not have. + * Does an insn of @sub acquire an iterator or a dynptr? If none does, a reference of its frame that + * has no location is always a pointer, and the hidden slot of the call that returned it holds it. + * Otherwise it can stand for an object in the stack that the program has overwritten. + */ +static bool acquires_stack_obj(struct bpf_verifier_env *env, const struct bpf_subprog_info *sub) +{ + const struct bpf_pad_note *note; + int i; + + for (i = sub->start; i < sub[1].start; i++) { + note = env->insn_aux_data[i].pad_note; + if (note && note->acquires && note->ent.class > BPF_PAD_REF) + return true; + } + return false; +} + +/* + * The groups of which @plan has something that its pad cannot tell whether it is held, or where. + * None for a site that the program does not have. + * @all_ptrs: no insn of the function acquires an iterator or a dynptr. */ -static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan) +static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, + bool all_ptrs) { u8 groups = plan->sections_differ; int i, err; if (!plan->used || plan->conflict) return 0; + if (plan->no_place && all_ptrs) + groups |= BIT(BPF_PAD_GROUP_REFS); for (i = 0; i < plan->nr_ents; i++) { err = settle_ent(env, plan, &plan->ents[i]); if (err == BPF_CONFLICT_NOT_ALWAYS_HELD || err == BPF_CONFLICT_CALLER_SAVED) @@ -1659,13 +1688,14 @@ static bool plan_has_slots(const struct bpf_pad_plan *plan) } /* - * The pad of @plan releases what the frame holds of @group through the hidden slots of the @nr - * calls @notes and in no other way. A slot is an entry that is tested. For the order of a pad, see - * enum pad_turn, the slots of sections go before all entries, those of references after - * them, those of locks and irq flags after all entries. + * Make the pad of @plan release what the frame holds of @group through the hidden slots of the @nr + * calls in @notes, and in no other way. Each slot becomes a release that is tested for 0. To fit + * the order of enum pad_turn, the slots of sections go before all other releases, those of + * references after them, and those of locks and irq flags last. @all_ptrs: the slots hold every + * reference of the frame. Returns 0 or -ENOMEM. */ static int plan_take_slots(struct bpf_pad_plan *plan, int group, struct bpf_pad_note **notes, - int nr) + int nr, bool all_ptrs) { struct bpf_pad_ent new, *ent; int i, k, pos = 0; @@ -1678,6 +1708,8 @@ static int plan_take_slots(struct bpf_pad_plan *plan, int group, struct bpf_pad_ plan->sections[group] = 0; plan->sections_differ &= ~BIT(group); } + if (all_ptrs) + plan->no_place = 0; if (group == BPF_PAD_GROUP_LOCKS) pos = plan->nr_ents; while (group == BPF_PAD_GROUP_REFS && pos < plan->nr_ents && @@ -1710,6 +1742,7 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) struct bpf_subprog_info *sub = &env->subprog_info[s]; struct bpf_insn_aux_data *aux = env->insn_aux_data; struct bpf_pad_note *note, *notes[BPF_PAD_MAX_ENTS]; + bool all_ptrs = group == BPF_PAD_GROUP_REFS && !acquires_stack_obj(env, sub); int i, j, err, left, nr = 0, slots = 0, words = 0; struct bpf_pad_plan *plan; @@ -1745,7 +1778,8 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) } for (i = sub->start; i < sub[1].start; i++) { for (plan = aux[i].pad_plans; plan; plan = plan->next) { - err = plan_has_slots(plan) ? plan_take_slots(plan, group, notes, nr) : 0; + err = plan_has_slots(plan) ? + plan_take_slots(plan, group, notes, nr, all_ptrs) : 0; if (err) return err; } @@ -1766,6 +1800,7 @@ static int settle_slots(struct bpf_verifier_env *env) struct bpf_subprog_info *sub; struct bpf_pad_plan *plan; int i, s, err; + bool all_ptrs; u8 need; for (s = 0; s < env->subprog_cnt; s++) { @@ -1774,13 +1809,14 @@ static int settle_slots(struct bpf_verifier_env *env) } for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; + all_ptrs = !acquires_stack_obj(env, sub); do { if (bpf_must_abort()) return -EINTR; need = 0; for (i = sub->start; i < sub[1].start; i++) for (plan = aux[i].pad_plans; plan; plan = plan->next) - need |= plan_needs_slots(env, plan); + need |= plan_needs_slots(env, plan, all_ptrs); need &= ~sub->no_slots; err = need ? subprog_take_slots(env, s, __ffs(need)) : 0; if (err < 0) -- 2.53.1-Meta