Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.133 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100129; b=zbZ0a/wzqYji7HS+s3dkJMcEH1lAWKI8HG6JlAHnNXcY1toP6flEy48vhkpi1LjzrAwB LwXzCMeOP9ctUqS+wtxt5qabOhfe6UcAQrdOOdhfj0U8yg3rGJ+zEIQmuq2f0aGUx5hzH hxmunpC6yX7XsB8vTd5lRvt/2BPYt/9+y39EVw3f17UO9GRNsNCETVENwgdp5B01LPrHG OseA/7vFQMge3usNxMgvsZ385JecF7n/7G6XlGemPGjHpQj/L5TZdSb+AC7HGxmcWUfXS seRjCJmaj6+lF5ieFg6N7LT0GocVQhphudnR0EmWpRV/YdgdSX46Fud89AU0LU5wTQQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100129; 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=oX4X4jQdwfJzCgU1LMWIqgTAw8VBXlXxg7qqavBTBik=; b=Fv37QAfSdUTPSdoVgvLKiDpnyPkWpxmnkV+CSvUj6saIJTY/u+Vll/x2bJv+ob9/jhaH zc0xvB5l5MxY+JK186xC0fxq3V+v7zQ4so/tr/SBJh9BRyvgjDqAWCN2GG/Z4nS2LhNzJ JMuNJRK7vo7YCpwBB/jAfu9Ruj8Bs0yM6hhBLNTtpSE3D7wWo6D7CZTM5z4GYKspD+ceb S/ETMTWKec53p7VHOglV7s8UnbG7l5jPVVP9ZJlD0cCJjE4T0OmOp7YsgMt/yg2pUHkeR 9OvrguVLWua5ezUFFCv7HH1IdXjV6iobCU8T+kcJoZjz23KK/V85j8AlwLTGBw5OYYg== 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=a8OWwPzl; arc=none smtp.remote-ip=74.125.231.133 Received: from mail-oo2-f5.google.com (mail-oo2-f5.google.com [74.125.231.133]) (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 EB491606A31A for ; Tue, 1 Dec 2026 04:42:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB491606A31A 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=a8OWwPzl Received: by mail-oo2-f5.google.com with SMTP id 46e09a7af769-86203d56413so1793647a34.1 for ; Mon, 30 Nov 2026 20:42:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100128; x=1796704928; 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=oX4X4jQdwfJzCgU1LMWIqgTAw8VBXlXxg7qqavBTBik=; b=a8OWwPzlpRda0IFFXZ5gPo1KlQvoso0T3svMrDgIXCJNuwJnWDx+Yh0BejAcsoUCpj C4Jdju6vECjdBpbVSAZsblPYaEN1UxYSP7dkrtzX7TfLo67oNyfnMm91DGxGVM9t0S5b AgLtGuQp15moGQLZz+sq4rK6eb7FL6+1T2fjG/+sKLhPRYKoePXXDNPdl3SRzr/KCays A+unDEz2k9TWyAraedgDnVfnK56OoHwop/rhwR41cOLWo5OlQNzbroVATtyhDVDtrk0d hla0GqnBucU5zjUpsZ85OPiKmyH4tQMVBr4Ay6Tr/839lC5qlQBlF7qTMn53HFbCByXb 8dug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100128; x=1796704928; 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=oX4X4jQdwfJzCgU1LMWIqgTAw8VBXlXxg7qqavBTBik=; b=zABAUTeuS70EX2uonFmYENmAERF2dKrVXbKK3XEDiVjWh21i12FNlQ4LxoUo6COjuQ 7LpMYHOodGSsZ/hOGBcZUEEiGRb+MbIWsgSn63yEcHS7fFCmPrfOo27/RcVsOdhAFxrY U0IPdzU1XLt/sSiO4w9uY0Pbv3S5fO1NpRXT3huPIkldbjwZ9nVd5+teHfRNx2H8H3aO z06CAsgYgEflZS3yQWfbu7wIakR/AuZtKMGQXpvdzf/0J/+sVhcIvcQZyLNyCZHRc5Mr QhcG+lOJNbENpNNjj9jeZ9D/oe17qPAe2XhcP9atkjX9UIJoC4vsVIIuIqpICUzAavi4 2ZtA== X-Gm-Message-State: APuU4Z8NSrIDydQxuo1j8xn+cy83uR4hiVgM4y6Ykrnp9TYW9qmt8z3k 0zydxLDmdA6WhVOma5WpoqDU0lXHwZ9FrvEb2rXZfZyGQX1mWJds1gOYdCf7nCJM9Gg= X-Gm-Gg: AZFdTZfqrrIEha9bCVPZCSbi4xX1YKcZHZLMcuScSfYv4P9x86Ob/5+Czgi8dh8Jbqh TTy5Op72fMV2So8lARGY8NImvMXbY7K8QXUN+j8ShT3fiUU1Yung7oQOR1u/ymfyC80Q5dRNkWV pMR25a4YzY+Wjosv8QTK5ffQLvydPk5M5wXVOzv1gCRbqdbWBxY1T/oOgB4guqZeaRtF+FLWf4K zrpkbKsO2v13f9yaIBnx9olwvI+puctGgVZIzvNxnZoGltW0PcqafEyvij+feQ78D5ApYgLWNZO e9mKynKfq9wOKpOMEirisVs3FpEbQ8/kWK6wdMxmZ+ByauFjFN3GA/gAoauF8PD16qtIXbRXVmA aLuaUmiSXuxFfTPTJMHO96K9YsPIAVnimJaev5Rzho9VVcP6w5i9QreZ2RcvZeZn3U9qAhsevwi GJS+cAHiG5kXytB4+6392byfgt6zJn6RQaNeXKsmXVTATtdqDHj0RL0j4uD/vBH/UpIZ6ehV+v5 k4ymsifCIueLL0zZjCfoDCeUDvSfEjtO2m6EX4t8KspfuwtRvo04tEYs+QqR9U/tHnD4hvU17aH fQoSjt4imFq7NnwFNcX5GilljiL+OD9DAuutMuF5d20kBk4QJ4rSvOo2KfSbW2wNj3yOL715rgP Zvrl1UkJctk0sAbRL6paf2LRvoFYAjascTagQ+A== X-Received: by 2002:a05:6830:411a:b0:862:9f13:2530 with SMTP id 46e09a7af769-863157a7edamr42097405a34.16.1796100127873; Mon, 30 Nov 2026 20:42:07 -0800 (PST) Received: from localhost ([2a03:2880:10ff:5b::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863168bdaf7sm26170383a34.5.2026.11.30.20.42.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:42:06 -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 41/73] bpf: Keep a reference of the caller that a function finds NULL Date: Tue, 1 Dec 2026 05:39:08 +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=8678; h=from:subject:message-id; bh=ttlPlHcEewkHOzbXyaIMg2xMQHct2RHFI63V3EP4LIk=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb0G56DZ/6ef7HA+vO7M9duXMK0cSeROF+NYHyEg3y/QU 7J3XUcrCIMbFICumyFLyfx+T8YnK34G2y7hh5rAygQxh4OIUgImcM2RkYNhW+b2Le0dxydlPO7i32b 4saJhVfZL5c9HK3IX3tD4KdDEyLK+slDPqmfa22aDAKuxLJv/e32rp+zqZ3tWFXvmn2ZbICgA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit A function that is verified separately from its callers, as a recursive function is, has the references of its caller in its state but none of the caller's registers: static struct pair *throw_if_null(struct pair *p, long depth) { if (!p) bpf_throw(0); if (depth) throw_if_null(p, depth - 1); return p; } Currently, when such a function finds a pointer of the caller to be NULL, the verifier drops the caller's reference from the state and verifies the call once more in the frames of the caller. With a recursion of unknown depth it runs out of frames, and the program is rejected. The function above loads today only because bpf_throw() ends its path in the verifier. Once bpf_throw() cancels the program through landing pads, a state comes back to the caller from the path with NULL. Keep the reference instead, and mark it on the path with NULL, see found_null and ref_of_caller(). The verifier compares the mark, so such a state stays apart from the states that lack it. No register of the function holds the pointer after the test, so the function cannot release the reference. What follows depends on how the function is exited: - If it is cancelled, the state goes to the caller with the reference held and without the mark. The landing pad of the caller tests the pointer for NULL before it releases it. The call is not verified a second time. - If it returns, the caller may rely on what the function found. The call is verified once more in the frames of the caller, as before. Two cases need care: - A landing pad of the program after the call starts with the reference held and a pointer that can be NULL, also on the path on which the function found NULL. It has to test the pointer itself. - The verifier still rejects a throw while a reference is held, so check_reference_leak() skips a reference that has the mark at a throw. For programs that load today, a landing pad after a call whose callee is cancelled on the path with NULL now sees a pointer that can be NULL. Until now the call was verified once more with the pointer known to be NULL. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 9 ++++- kernel/bpf/verifier.c | 71 ++++++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index e53a9f8618f6..4582dddd5e28 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -386,10 +386,15 @@ struct bpf_reference_state { /* the insn that brought it into its frame: insn_idx, or a call that returned with it */ int origin; union { - /* For REF_TYPE_PTR */ + /* For REF_TYPE_PTR. Compared and hashed as 8 bytes, see same_refs(): no holes. */ struct { int parent_id; - u32 ctx_arg; /* it came with that __ref argument, the first is 1 */ + u16 ctx_arg; /* it came with that __ref argument, the first is 1 */ + /* + * A function that is verified separately found the pointer to be NULL, + * and its caller holds the reference, see ref_of_caller() + */ + u16 found_null; }; /* Use to keep track of the source object of a lock, to ensure * it matches on unlock. diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 9b000754ae2f..6ede9f5d6aa1 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12033,6 +12033,22 @@ static u32 returned_id(u32 id) return bpf_id_is_xreg(id) ? BPF_RETURNED_ID(id) : id; } +/* + * Did the function find a pointer to be NULL whose reference @caller holds? @caller waits at the + * call and does not know that yet, see ref_of_caller(). + */ +static bool found_null_of_caller(const struct bpf_verifier_state *caller, + const struct bpf_verifier_state *exit) +{ + u32 i; + + for (i = 0; i < caller->acquired_refs; i++) + if (exit->refs[i].type == REF_TYPE_PTR && exit->refs[i].found_null && + !caller->refs[i].found_null) + return true; + return false; +} + /* * @caller waits at the call at @call_idx. @exit is the state at the exit of the * function. Make @caller the state after the call. @@ -12044,23 +12060,28 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal const struct bpf_func_state *callee = exit->frame[0]; u32 i, nregs, nheld = caller->acquired_refs; struct bpf_reg_state *reg; + bool found_null; int err; caller->speculative |= exit->speculative; /* * Keep only what the function held all the time. The state at its exit looks the same if * the function released a lock and acquired it again. Else verify the function in the - * instance of the caller. The caller has non-owning references only under a lock, which - * is one of what it holds. + * instance of the caller. Do the same if the function invalidated non-owning references + * and the caller holds anything: it has such references only under a lock. And do the + * same if the function returns after it found a pointer of the caller to be NULL. */ + found_null = callee->entry_refs == nheld && !exit->unwound && + found_null_of_caller(caller, exit); if (callee->entry_refs < nheld || (exit->invalidated_non_owning && nheld) || callee->entry_rcu_locks < caller->active_rcu_locks || - callee->entry_preempt_locks < caller->active_preempt_locks) { + callee->entry_preempt_locks < caller->active_preempt_locks || found_null) { if (exit->speculative) return -EACCES; if (env->log.level & BPF_LOG_LEVEL2) - verbose(env, "func#%d releases what its caller holds at insn %d\n", - callee->subprogno, call_idx); + verbose(env, "func#%d %s what its caller holds at insn %d\n", + callee->subprogno, found_null ? "finds NULL" : "releases", + call_idx); env->subprog_info[callee->subprogno].releases_inherited = true; return BPF_EXIT_NEEDS_CALLER; } @@ -13136,6 +13157,9 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exi for (i = 0; i < state->acquired_refs; i++) { if (state->refs[i].type != REF_TYPE_PTR) continue; + /* found NULL on this path, see ref_of_caller(): a throw leaks nothing */ + if (exception_exit && state->refs[i].found_null) + continue; /* Allow struct_ops programs to return a referenced kptr back to * kernel. Type checks are performed later in check_return_code. */ @@ -19499,6 +19523,25 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state, } } +/* + * Returns the entry of the reference @id if the caller of frame 0 holds it. + * + * A function that is verified separately has the references of its caller in refs[], but not the + * caller's registers. When it finds such a pointer to be NULL, the reference stays, marked + * found_null. If the function returns, the call is verified once more in the frames of the caller, + * see bpf_flow_return(). If it is cancelled or unwinds, the caller holds the reference without the + * mark, and the landing pad after the call has to test the pointer for NULL. + */ +static struct bpf_reference_state *ref_of_caller(struct bpf_verifier_state *st, u32 id) +{ + u32 i; + + for (i = 0; i < st->frame[0]->entry_refs; i++) + if (st->refs[i].type == REF_TYPE_PTR && st->refs[i].id == id) + return &st->refs[i]; + return NULL; +} + /* The logic is similar to find_good_pkt_pointers(), both could eventually * be folded together at some point. */ @@ -19507,14 +19550,20 @@ static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, { struct bpf_func_state *state = vstate->frame[vstate->curframe]; struct bpf_reg_state *regs = state->regs, *reg; + struct bpf_reference_state *ref; u32 id = regs[regno].id; - if (is_null && find_reference_state(vstate, id)) - /* regs[regno] is in the " == NULL" branch. - * No one could have freed the reference state before - * doing the NULL check. - */ - WARN_ON_ONCE(__release_reference_nomark(vstate, id)); + if (is_null && find_reference_state(vstate, id)) { + ref = ref_of_caller(vstate, id); + if (ref) + ref->found_null = true; + else + /* regs[regno] is in the " == NULL" branch. + * No one could have freed the reference state before + * doing the NULL check. + */ + WARN_ON_ONCE(__release_reference_nomark(vstate, id)); + } bpf_for_each_reg_in_vstate(vstate, state, reg, ({ mark_ptr_or_null_reg(state, reg, id, is_null); -- 2.53.1-Meta