Article View: linux.kernel.netdev
Article #1095[PATCH net] ipv4: use RCU protection in inet_netconf_get_devconf()
From: Eric Dumazet
Date: Fri, 09 Oct 2026 07:44
Date: Fri, 09 Oct 2026 07:44
108 lines
8810 bytes
8810 bytes
From: Eric Dumazet <edumazet@google.com>
inet_netconf_get_devconf() runs without RTNL. It uses dev_get_by_index()
and in_dev_get() to pin the device and its in_device while the reply is
allocated and filled.
in_dev_get() is not safe for a lockless caller. Since commit 9d40c84cf5bc
("net: devinet: Reduce refcount before grace period"), inetdev_destroy()
drops the final reference before the RCU grace period. A reader can load
dev->ip_ptr, then call refcount_inc() on a zero refcount after
in_dev_free_rcu() has been queued, and keep using the in_device once it
has been freed.
None of these references are needed. An in_device is freed after an RCU
grace period, ipv4_devconf is embedded in it, and
inet_netconf_fill_devconf() does not sleep.
Allocate the skb first, then perform the lookup and the fill under
rcu_read_lock(), using dev_get_by_index_rcu() and __in_dev_get_rcu(),
like inet_netconf_dump_devconf() already does.
Fixes: bbcf91053bb6 ("inet: do not use RTNL in inet_netconf_get_devconf()")
Reported-by: Baul Lee <baul.lee@xbow.com>
Closes: https://lore.kernel.org/netdev/20260815172032.79740-1-baul.lee@xbow.com/
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Cen Zhang <zzzccc427@gmail.com>
---
net/ipv4/devinet.c | 35 +++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 5b6b11c943e453742b4893f1c9bd9e70d3d37a6f..f9a361621f74b420276006b13de94926e13af18e 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -2379,8 +2379,8 @@ static int inet_netconf_get_devconf(struct sk_buff *in_skb,
struct net *net = sock_net(in_skb->sk);
struct nlattr *tb[NETCONFA_MAX + 1];
const struct ipv4_devconf *devconf;
- struct in_device *in_dev = NULL;
- struct net_device *dev = NULL;
+ struct in_device *in_dev;
+ struct net_device *dev;
struct sk_buff *skb;
int ifindex;
int err;
@@ -2392,7 +2392,13 @@ static int inet_netconf_get_devconf(struct sk_buff *in_skb,
if (!tb[NETCONFA_IFINDEX])
return -EINVAL;
+ skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
+ if (!skb)
+ return -ENOBUFS;
+
ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
+
+ rcu_read_lock();
switch (ifindex) {
case NETCONFA_IFINDEX_ALL:
devconf = net->ipv4.devconf_all;
@@ -2402,36 +2408,29 @@ static int inet_netconf_get_devconf(struct sk_buff *in_skb,
break;
default:
err = -ENODEV;
- dev = dev_get_by_index(net, ifindex);
- if (dev)
- in_dev = in_dev_get(dev);
+ dev = dev_get_by_index_rcu(net, ifindex);
+ if (!dev)
+ goto out_unlock;
+ in_dev = __in_dev_get_rcu(dev);
if (!in_dev)
- goto errout;
+ goto out_unlock;
devconf = &in_dev->cnf;
break;
}
- err = -ENOBUFS;
- skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
- if (!skb)
- goto errout;
-
err = inet_netconf_fill_devconf(skb, ifindex, devconf,
NETLINK_CB(in_skb).portid,
nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
NETCONFA_ALL);
+out_unlock:
+ rcu_read_unlock();
if (err < 0) {
/* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
WARN_ON(err == -EMSGSIZE);
kfree_skb(skb);
- goto errout;
+ return err;
}
- err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
-errout:
- if (in_dev)
- in_dev_put(in_dev);
- dev_put(dev);
- return err;
+ return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
}
static int inet_netconf_dump_devconf(struct sk_buff *skb,
--
2.53.0
Technical Details
Message-ID:
<20261009074405.727999-1-edumazet@kernel.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-343493-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-343493-linux-kernel-netdev=dewy.ru From: edumazet@kernel.org (Eric Dumazet) Newsgroups: linux.kernel.netdev Subject: [PATCH net] ipv4: use RCU protection in inet_netconf_get_devconf() Date: Fri, 9 Oct 2026 09:44:05 +0200 Organization: Usenet Network Message-ID: <20261009074405.727999-1-edumazet@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1057005"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:JTpv/McTXMnZEYbyIXWc5GnDvZc= sha256:2OBQSqpOovP/rg0YMl1kQj4iv7Fcoclof5hMDN+IdpU= sha1:NN5cTDW5x2o0dfi8Umm7AXfvs3E= sha256:/9JEiVhuDaJ0Mp/a48KayfhyyNtZH+hwC599SW+old4= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 89CCC10F5EF; Fri, 9 Oct 2026 10:44:28 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.641 X-Spam-Status: No, score=-3.641 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.241, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sGKCN4F6vAIT; Fri, 9 Oct 2026 10:44:25 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-343493-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 1F8E310F5AC; Fri, 9 Oct 2026 10:44:20 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sin.lore.kernel.org (Postfix) with ESMTP id B4BB72A19D; Fri, 9 Oct 2026 07:44:15 +0000 (UTC) X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FB4947D461; Fri, 9 Oct 2026 07:44:14 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R44qMcgl" X-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 082EC457E56 for <netdev@vger.kernel.org>; Fri, 9 Oct 2026 07:44:12 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791531854; cv=none; b=jt5hNYatXoS9WzfJcpbERPXASnjot+PAIOmK9EOvS3F253raCtBWomsXQ225JuLLZ/4DhNl1vNHbQH4FJPHgZlsNIC0PPkGHXDYR4vbK3vKyPOQYN6h5XegL0BXd/HrrPSxthbY8B4YjdoErRQv6ScHbVhnywn0jKrr1nCUaSNM= X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791531854; c=relaxed/simple; bh=m1WmAY5g1ebSDH4cjX37xA1Lx8rITb4V2u+6EUWh+AE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GAoN5Ix6uADd5ipXqA/nvDr3/A3x0HjDXGii9sLDm60mZwbdJW4uZoDFNIhmu2+p4OWk251L5iZUENWpPPAUE8dd0Cz+CkeCHAgiT9mPblYv0FepwJETi4Kb/O1gaec951/O3s0ZbzUL22eNfvl4VFB0iVI8RWL7aW2FwmAbBBA= X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R44qMcgl; arc=none smtp.client-ip=100.103.45.18 X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDAC11F000FF; Fri, 9 Oct 2026 07:44:08 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1791531852; bh=7kfQmmz2VEfOe+stV4PsQ8pwfY4vcNZihIKqHxDLvkc=; h=From:To:Cc:Subject:Date; b=R44qMcglzHELOSIy23JWOybaYdrwIb30QyGGKplMGX7ih6WJreCMjmqH66ANF8+9v rj8c6j256i1bk+EusM9xkoZB8c+vHeDz5QakcAniaC0nZPB0rKRlnv8jrJs7Tr7+h7 EsDPqwFfu8Qtjt5dcIVvo0zBGb4EqEUNf8KYwL4SeFtkwQ0XWBFZjbIO5/JJfzCL5L O7acGfZSygVpdoVhWeZO2npznOzuEZzIV16kRg296xZsQHF9vKd83extvCJ8uOnO38 Zjg9NTQ+UncBrtn/LVN9k1kqhEJrSwGdgD0cIt7VEErFFg23PxcqbOS+IBa34mM0xO 3Q484rtZv8kUw== X-Cc: Simon Horman <horms@kernel.org>, David Ahern <dsahern@kernel.org>, Ido Schimmel <idosch@nvidia.com>, Kuniyuki Iwashima <kuniyu@google.com>, netdev@vger.kernel.org, Eric Dumazet <edumazet@google.com>, Baul Lee <baul.lee@xbow.com>, Cen Zhang <zzzccc427@gmail.com> X-Mailer: git-send-email 2.53.0 X-Precedence: bulk X-Mailing-List: netdev@vger.kernel.org X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>