From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384899; b=jbMAaMQ4mAM8veJnDHKPXhHEDqJZ8bbOfs2jKNbCLbmngq0u8X+1LGM/+8wdb5STFh2d 90r00WlJFQTrFEr+bPa37bA7iBh/4ogMl0MCuG2e2r6a/lBAA3rsYq2EEB0jb+MmfBEqo 6ElSHoHxWJOmu1yfmGfk04CZqPxHEqrFf+ZO1tzdI4oTDPWcy6e+iyo3GPTzpk+t7FoMz RRfC5624Gf3tsmJGcQIzPMBLSzvRnsnUdwIlU6AGzwGwRvwPonxOnODBc68ZPA5VdBgqV UwtTkNB6weOCtLYTrhAfOoNiBgKSCQECWQI+Jc/+jmJ8oAYK3i8Fxe6ub2omO5oMJHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384899; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=1/XoKx4s3++dn2YyDdnswWx8hs73nrdRsRNp79pgG5I=; b=xKIDmnOk+uuvxfLjvTwwefIUf6PuIDNgCdlUVgbqmHlP3SNZBZdjcJuCu7y2+HZ0KXDJ FNloY6NBpGst/MdvA3YrgY4GxG1QmZYSwm4UL6TeD3rSXK2/B9FAxry/VYqQ8p2LLc9rw xHVzu1qFEwTP3HdP2BRIvqujfTTdKeEbV3O4zkKKbCvDcjrsIAojh9u8qqsWDIAOd9pYp 5zgUXFsWHxjzklqakL7FVeXMdLdwW1AOiYhiUgrMwoUIWOSA7TPzv2FnzZ/dbRZi/iSXz z9dbyI7aka3hIFam3yYpDIDB9jBkCr5MXLoGoq3P1uRBzFVbTyJCHcwldVex4/ztmPQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gVyHvZGi; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4739E606A32D; Fri, 4 Dec 2026 11:48:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gVyHvZGi Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1BC11F00897; Fri, 4 Dec 2026 11:48:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384899; bh=1/XoKx4s3++dn2YyDdnswWx8hs73nrdRsRNp79pgG5I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gVyHvZGi9dHqgu8RdU/cr5MIToVhbZlkHYmuF0oDSKiPTZ4/AgXNDynvZDmeIpxC7 cBnxP/AnfXP4mOcqg/Prtzq3CYIhmo4MOytZezbvknKZoado7qCmzsry2hmcZWIxnW la1571gyab9aOMC+ScDeUYQWB5vdsViYtlbgQfos= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.6 0341/1167] bpf: Dont converge a loop on an iterator that was created anew Date: Fri, 4 Dec 2026 18:56:08 +0900 Message-ID: <20261204100710.876152843@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 6f3ee3516305da3043f1787e3890381a54151730 ] When the verifier reaches bpf_iter_*_next() in a state that is equal to a state that is still being explored it concludes that the loop converged, since an active iterator will be drained eventually. states_equal() matches ids through idmap, so the same is concluded for an iterator that was destroyed and created again since the old state: bpf_iter_num_new(&it, 0, 10); loop: if (bpf_ktime_get_ns()) goto remake; next: if (!bpf_iter_num_next(&it)) goto out; goto loop; remake: bpf_iter_num_destroy(&it); bpf_iter_num_new(&it, 0, 10); goto next; Such prog is accepted when the call of bpf_iter_num_next() is the first prune point after bpf_iter_num_new() and it never returns. Otherwise it's rejected with "infinite loop detected". Require the iterator to have the same id in both states. Fixes: 06accc8779c1 ("bpf: add support for open-coded iterator loops") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20260924233130.1213812-8-alexei.starovoitov@gmail.com [ sashal: Renamed the upstream states.c/bpf_is_state_visited() code to verifier.c/is_state_visited(), compared ref_obj_id instead of id, and added a NULL guard for old_iter when spi is out of range. ] Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 5d28bd3ece833..a72d196bcbd29 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17050,8 +17050,9 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) */ if (is_iter_next_insn(env, insn_idx)) { if (states_equal(env, &sl->state, cur, true)) { - struct bpf_func_state *cur_frame; + struct bpf_func_state *cur_frame, *old_frame; struct bpf_reg_state *iter_state, *iter_reg; + struct bpf_reg_state *old_iter; int spi; cur_frame = cur->frame[cur->curframe]; @@ -17065,7 +17066,17 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) */ spi = __get_spi(iter_reg->off + iter_reg->var_off.value); iter_state = &func(env, iter_reg)->stack[spi].spilled_ptr; - if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) { + old_frame = sl->state.frame[iter_reg->frameno]; + old_iter = spi < old_frame->allocated_stack / BPF_REG_SIZE ? + &old_frame->stack[spi].spilled_ptr : NULL; + /* + * states_equal() matches ids through idmap. + * The loop converged only if it's the same + * iterator. The one that was destroyed and + * created again starts from the beginning. + */ + if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE && + old_iter && iter_state->ref_obj_id == old_iter->ref_obj_id) { update_loop_entry(cur, &sl->state); goto hit; } -- 2.53.0