From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E5F551A723; Wed, 30 Sep 2026 18:28:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790792881; cv=none; b=PYpphkg/i6Yc6Irg56NVyI0ZDoVlfX4GvIjB4dZaSDuylpOt1h12lr9HHwHukBDKHyp1bsO6N/0FNvKlE/130haylUXvo9gZdfc1/+nWzCi9rEnzG+WT/QNAa4DjM/QSXkIuuU7sEY9EMyK+hdunaXrBO376h6cWL70pPuI4feI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790792881; c=relaxed/simple; bh=5MdYc8oi/Dcua7nOd8T2BYPh+fvBNpZRnad+7zAeLeM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TU67FmqnZrUFDAY/bUBympUvDksCjfFZ5flOfCurN545+LEk0WyGB6E/ziEBWed2aOtO9ktN6MC8Uu8v6XgkFuksn5cvHiA2gK8YYh5I4GctRzE4tlF009gNKKAkiCr9SYlVsom1F6FYYIQb7HYsuOY/X3aKGESlnTC7cEWEKTM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=WJ7/Gr6P; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="WJ7/Gr6P" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE23A1F000FF; Wed, 30 Sep 2026 18:27:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1790792880; bh=rhsMwXNjNwWvG8lPblydmJ2Y6KY6FUEirJ3ax27Ivys=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WJ7/Gr6PfVRSBEIrXbZhZLNFsGrvulTXafOmiJ7mczsP3EOwYvXot/GA8KUPRdgDG tE16Zh8KQ8AMqLdoWcsu+CjuPPhFVVgBIOGIWp+dL0K6I4dFM8MVHrxUdNypxQlrZW ewxpy9QSsJXNy6kyFUrDONuFX6+nAX78Ld0gZ98E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Xiang Mei , Weiming Shi , Andrii Nakryiko , Sasha Levin Subject: [PATCH 6.18 057/395] bpf: Skip unsettled links in link iterator Date: Wed, 30 Sep 2026 17:25:19 +0200 Message-ID: <20260930152341.872347061@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260930152340.591469096@linuxfoundation.org> References: <20260930152340.591469096@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Weiming Shi [ Upstream commit 50e80e2bb5e2be8515205b9c496b9640ddefa434 ] bpf_link_prime() inserts a link into link_idr before anon_inode_getfile() succeeds and before bpf_link_settle() publishes the ID in link->id. bpf_link_by_id() treats such an ID-zero link as unsettled, but the link iterator takes a reference without this check. If anon_inode_getfile() then fails, the creator removes the ID and frees its still-private link directly. The iterator is left with a dangling reference and its next bpf_link_put() accesses freed memory. Treat ID-zero entries as transient in bpf_link_get_curr_or_next(), just as bpf_link_by_id() does. BUG: KASAN: slab-use-after-free in bpf_link_put Write of size 8 by task exp/384 Call Trace: bpf_link_put kernel/bpf/syscall.c:3372 bpf_link_seq_next kernel/bpf/link_iter.c:33 bpf_seq_read kernel/bpf/bpf_iter.c:158 vfs_read fs/read_write.c:572 ksys_read fs/read_write.c:716 do_syscall_64 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe arch/x86/entry/entry_64.S:121 Kernel panic - not syncing: KASAN: panic_on_warn set ... Fixes: 9f8836127308 ("bpf: Add bpf_link iterator") Reported-by: Xiang Mei Signed-off-by: Weiming Shi Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20260914170206.170723-2-bestswngs@gmail.com Signed-off-by: Sasha Levin --- kernel/bpf/syscall.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index bab472152d64d..dec993c90dc33 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5941,7 +5941,10 @@ struct bpf_link *bpf_link_get_curr_or_next(u32 *id) again: link = idr_get_next(&link_idr, id); if (link) { - link = bpf_link_inc_not_zero(link); + if (link->id) + link = bpf_link_inc_not_zero(link); + else + link = ERR_PTR(-EAGAIN); if (IS_ERR(link)) { (*id)++; goto again; -- 2.53.0