Skip to content

Commit 3a90f09

Browse files
kuba-moomehmetb0
authored andcommitted
netdev: prevent accessing NAPI instances from another namespace
BugLink: https://bugs.launchpad.net/bugs/2106770 commit d1cacd7 upstream. 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: Greg Kroah-Hartman <gregkh@linuxfoundation.org> CVE-2025-21659 Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent f43e49b commit 3a90f09

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

net/core/dev.c

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

736+
/* must be called under rcu_read_lock(), as we dont take a reference */
737+
static struct napi_struct *napi_by_id(unsigned int napi_id)
738+
{
739+
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
740+
struct napi_struct *napi;
741+
742+
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
743+
if (napi->napi_id == napi_id)
744+
return napi;
745+
746+
return NULL;
747+
}
748+
749+
/* must be called under rcu_read_lock(), as we dont take a reference */
750+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id)
751+
{
752+
struct napi_struct *napi;
753+
754+
napi = napi_by_id(napi_id);
755+
if (!napi)
756+
return NULL;
757+
758+
if (WARN_ON_ONCE(!napi->dev))
759+
return NULL;
760+
if (!net_eq(net, dev_net(napi->dev)))
761+
return NULL;
762+
763+
return napi;
764+
}
765+
736766
/**
737767
* __dev_get_by_name - find a device by its name
738768
* @net: the applicable net namespace
@@ -6159,19 +6189,6 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
61596189
}
61606190
EXPORT_SYMBOL(napi_complete_done);
61616191

6162-
/* must be called under rcu_read_lock(), as we dont take a reference */
6163-
struct napi_struct *napi_by_id(unsigned int napi_id)
6164-
{
6165-
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6166-
struct napi_struct *napi;
6167-
6168-
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6169-
if (napi->napi_id == napi_id)
6170-
return napi;
6171-
6172-
return NULL;
6173-
}
6174-
61756192
#if defined(CONFIG_NET_RX_BUSY_POLL)
61766193

61776194
static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)

net/core/dev.h

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

2424
extern int netdev_flow_limit_table_len;
2525

26+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id);
27+
2628
#ifdef CONFIG_PROC_FS
2729
int __init dev_proc_init(void);
2830
#else
@@ -148,5 +150,4 @@ void xdp_do_check_flushed(struct napi_struct *napi);
148150
static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
149151
#endif
150152

151-
struct napi_struct *napi_by_id(unsigned int napi_id);
152153
#endif

net/core/netdev-genl.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
162162
void *hdr;
163163
pid_t pid;
164164

165-
if (WARN_ON_ONCE(!napi->dev))
166-
return -EINVAL;
167165
if (!(napi->dev->flags & IFF_UP))
168166
return 0;
169167

@@ -214,7 +212,7 @@ int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info)
214212
rtnl_lock();
215213
rcu_read_lock();
216214

217-
napi = napi_by_id(napi_id);
215+
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
218216
if (napi) {
219217
err = netdev_nl_napi_fill_one(rsp, napi, info);
220218
} else {

0 commit comments

Comments
 (0)