Skip to content

Commit 3a2c083

Browse files
committed
netdev: prevent accessing NAPI instances from another namespace
JIRA: https://issues.redhat.com/browse/RHEL-77816 CVE: CVE-2025-21659 Upstream Status: net.git commit d1cacd7 Conflicts: - net/core/dev.h, conflict as we don't have upstream commits b9495b5 ("net: move kick_defer_list_purge() to net/core/dev.h"), ecefbc0 ("net: softnet_data: Make xmit per task"), 2fe50a4 ("net: move dev_xmit_recursion() helpers to net/core/dev.h") commit d1cacd7 Author: Jakub Kicinski <kuba@kernel.org> Date: Mon Jan 6 10:01:36 2025 -0800 netdev: prevent accessing NAPI instances from another namespace The NAPI IDs were not fully exposed to user space prior to the netlink API, so they were never namespaced. The netlink API must ensure that at the very least NAPI instance belongs to the same netns as the owner of the genl sock. napi_by_id() can become static now, but it needs to move because of dev_get_by_napi_id(). Cc: stable@vger.kernel.org Fixes: 1287c1a ("netdev-genl: Support setting per-NAPI config values") Fixes: 27f91aa ("netdev-genl: Add netlink framework functions for napi") Reviewed-by: Sridhar Samudrala <sridhar.samudrala@intel.com> Reviewed-by: Joe Damato <jdamato@fastly.com> Link: https://patch.msgid.link/20250106180137.1861472-1-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Davide Caratti <dcaratti@redhat.com>
1 parent c414205 commit 3a2c083

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

net/core/dev.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,36 @@ int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
748748
}
749749
EXPORT_SYMBOL_GPL(dev_fill_forward_path);
750750

751+
/* must be called under rcu_read_lock(), as we dont take a reference */
752+
static struct napi_struct *napi_by_id(unsigned int napi_id)
753+
{
754+
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
755+
struct napi_struct *napi;
756+
757+
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
758+
if (napi->napi_id == napi_id)
759+
return napi;
760+
761+
return NULL;
762+
}
763+
764+
/* must be called under rcu_read_lock(), as we dont take a reference */
765+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id)
766+
{
767+
struct napi_struct *napi;
768+
769+
napi = napi_by_id(napi_id);
770+
if (!napi)
771+
return NULL;
772+
773+
if (WARN_ON_ONCE(!napi->dev))
774+
return NULL;
775+
if (!net_eq(net, dev_net(napi->dev)))
776+
return NULL;
777+
778+
return napi;
779+
}
780+
751781
/**
752782
* __dev_get_by_name - find a device by its name
753783
* @net: the applicable net namespace
@@ -6217,19 +6247,6 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
62176247
}
62186248
EXPORT_SYMBOL(napi_complete_done);
62196249

6220-
/* must be called under rcu_read_lock(), as we dont take a reference */
6221-
struct napi_struct *napi_by_id(unsigned int napi_id)
6222-
{
6223-
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6224-
struct napi_struct *napi;
6225-
6226-
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6227-
if (napi->napi_id == napi_id)
6228-
return napi;
6229-
6230-
return NULL;
6231-
}
6232-
62336250
static void skb_defer_free_flush(struct softnet_data *sd)
62346251
{
62356252
struct sk_buff *skb, *next;

net/core/dev.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct sd_flow_limit {
2424

2525
extern int netdev_flow_limit_table_len;
2626

27+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id);
28+
2729
#ifdef CONFIG_PROC_FS
2830
int __init dev_proc_init(void);
2931
#else
@@ -233,5 +235,4 @@ static inline void netdev_set_gro_flush_timeout(struct net_device *netdev,
233235

234236
int rps_cpumask_housekeeping(struct cpumask *mask);
235237

236-
struct napi_struct *napi_by_id(unsigned int napi_id);
237238
#endif

net/core/netdev-genl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
165165
void *hdr;
166166
pid_t pid;
167167

168-
if (WARN_ON_ONCE(!napi->dev))
169-
return -EINVAL;
170168
if (!(napi->dev->flags & IFF_UP))
171169
return 0;
172170

@@ -228,7 +226,7 @@ int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info)
228226
rtnl_lock();
229227
rcu_read_lock();
230228

231-
napi = napi_by_id(napi_id);
229+
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
232230
if (napi) {
233231
err = netdev_nl_napi_fill_one(rsp, napi, info);
234232
} else {
@@ -337,7 +335,7 @@ int netdev_nl_napi_set_doit(struct sk_buff *skb, struct genl_info *info)
337335
rtnl_lock();
338336
rcu_read_lock();
339337

340-
napi = napi_by_id(napi_id);
338+
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
341339
if (napi) {
342340
err = netdev_nl_napi_set_config(napi, info);
343341
} else {

0 commit comments

Comments
 (0)