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 8BF955883E8; Mon, 31 Aug 2026 13:50:16 +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=1788184218; cv=none; b=cFexBet+SP2lUHbInN9qKpjLbZG5BS63pwUBZHLERW/dVdHi6wOm8fkHWHUTNysXGN2oE42ECzpV2yHWAAuT6F+QGNxiMcF1YNdqM0hPemGmlRcZ/E+FV6c83caPWz8PAC/fgw2FTPuVOgSvEH3VP1c/OYxVihz9R+VQkfCDOWY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788184218; c=relaxed/simple; bh=VH9XsyLhyxrr4dKEaZFdqFO2uykH8/ipt/xUAnAW+PI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CFJc5qG148tyf2o97tkmuw5qeGNiElT/5V9qbvlRnP1W9coS6mqamLLjhAF5Y7jfTGS8aoACNm2f/3s4mSKfgA3bJpBQpy1L6XZqQ6WzRMRFCcvg3mAvwcsi+idoZ1dn9mXMzclfi8lD8sFAwqYpFOI0s6GvkUZs7CM8F55SMgw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=eWY0BLY6; 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="eWY0BLY6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC56E1F00A3F; Mon, 31 Aug 2026 13:50:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1788184216; bh=7o70ImUyFBk4sF2e26TFBItBdy0o5TzfEfSZl2K57hU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eWY0BLY6d0lFgYRt3EWlUAGFfxZfJBsrVXUpHmlqIXHAWqMG+hYqQK4+Y99Mb885T z84LneQGGGlOjJNTEFxuXZjgDxUC4bI/5jMrd7uLSeZuVgl7DhZImd38lDgH/G7CWM yUVW3Poh2b6rA524GBTxBo39IlcG/xSPbE8TBQ+w= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yinhao Hu , Kaiyan Mei , Dongliang Mu , Jiayuan Chen , Daniel Borkmann , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.12 31/99] bpf: Fix use-after-free in offloaded map/prog info fill Date: Mon, 31 Aug 2026 15:34:00 +0200 Message-ID: <20260831133401.564528108@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260831133359.740409777@linuxfoundation.org> References: <20260831133359.740409777@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jiayuan Chen [ Upstream commit a0c584fc18056709c8e047a82a6045d6c209f4ce ] When querying info for an offloaded BPF map or program, bpf_map_offload_info_fill_ns() and bpf_prog_offload_info_fill_ns() obtain the network namespace with get_net(dev_net(offmap->netdev)). However, the associated netdev's netns may be racing with teardown during netns destruction. If the netns refcount has already reached 0, get_net() performs a refcount_t increment on 0, triggering: refcount_t: addition on 0; use-after-free. Although rtnl_lock and bpf_devs_lock ensure the netdev pointer remains valid, they cannot prevent the netns refcount from reaching zero. Fix this by using maybe_get_net() instead of get_net(). maybe_get_net() uses refcount_inc_not_zero() and returns NULL if the refcount is already zero, which causes ns_get_path_cb() to fail and the caller to return -ENOENT -- the correct behavior when the netns is being destroyed. Fixes: 675fc275a3a2d ("bpf: offload: report device information for offloaded programs") Fixes: 52775b33bb507 ("bpf: offload: report device information about offloaded maps") Reported-by: Yinhao Hu Reported-by: Kaiyan Mei Reviewed-by: Dongliang Mu Closes: https://lore.kernel.org/bpf/f0aa3678-79c9-47ae-9e8c-02a3d1df160a@hust.edu.cn/ Signed-off-by: Jiayuan Chen Acked-by: Daniel Borkmann Link: https://lore.kernel.org/r/20260409023733.168050-1-jiayuan.chen@linux.dev Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/offload.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/offload.c b/kernel/bpf/offload.c index 1a4fec330eaac..56115739fcfdd 100644 --- a/kernel/bpf/offload.c +++ b/kernel/bpf/offload.c @@ -444,9 +444,8 @@ static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data) if (aux->offload) { args->info->ifindex = aux->offload->netdev->ifindex; - net = dev_net(aux->offload->netdev); - get_net(net); - ns = &net->ns; + net = maybe_get_net(dev_net(aux->offload->netdev)); + ns = net ? &net->ns : NULL; } else { args->info->ifindex = 0; ns = NULL; @@ -652,9 +651,8 @@ static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data) if (args->offmap->netdev) { args->info->ifindex = args->offmap->netdev->ifindex; - net = dev_net(args->offmap->netdev); - get_net(net); - ns = &net->ns; + net = maybe_get_net(dev_net(args->offmap->netdev)); + ns = net ? &net->ns : NULL; } else { args->info->ifindex = 0; ns = NULL; -- 2.53.0