Skip to content

Commit d1cacd7

Browse files
committed
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: [email protected] Fixes: 1287c1a ("netdev-genl: Support setting per-NAPI config values") Fixes: 27f91aa ("netdev-genl: Add netlink framework functions for napi") Reviewed-by: Sridhar Samudrala <[email protected]> Reviewed-by: Joe Damato <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent db78475 commit d1cacd7

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
@@ -753,6 +753,36 @@ int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
753753
}
754754
EXPORT_SYMBOL_GPL(dev_fill_forward_path);
755755

756+
/* must be called under rcu_read_lock(), as we dont take a reference */
757+
static struct napi_struct *napi_by_id(unsigned int napi_id)
758+
{
759+
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
760+
struct napi_struct *napi;
761+
762+
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
763+
if (napi->napi_id == napi_id)
764+
return napi;
765+
766+
return NULL;
767+
}
768+
769+
/* must be called under rcu_read_lock(), as we dont take a reference */
770+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id)
771+
{
772+
struct napi_struct *napi;
773+
774+
napi = napi_by_id(napi_id);
775+
if (!napi)
776+
return NULL;
777+
778+
if (WARN_ON_ONCE(!napi->dev))
779+
return NULL;
780+
if (!net_eq(net, dev_net(napi->dev)))
781+
return NULL;
782+
783+
return napi;
784+
}
785+
756786
/**
757787
* __dev_get_by_name - find a device by its name
758788
* @net: the applicable net namespace
@@ -6293,19 +6323,6 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
62936323
}
62946324
EXPORT_SYMBOL(napi_complete_done);
62956325

6296-
/* must be called under rcu_read_lock(), as we dont take a reference */
6297-
struct napi_struct *napi_by_id(unsigned int napi_id)
6298-
{
6299-
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6300-
struct napi_struct *napi;
6301-
6302-
hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6303-
if (napi->napi_id == napi_id)
6304-
return napi;
6305-
6306-
return NULL;
6307-
}
6308-
63096326
static void skb_defer_free_flush(struct softnet_data *sd)
63106327
{
63116328
struct sk_buff *skb, *next;

net/core/dev.h

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

2323
extern int netdev_flow_limit_table_len;
2424

25+
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id);
26+
2527
#ifdef CONFIG_PROC_FS
2628
int __init dev_proc_init(void);
2729
#else
@@ -269,7 +271,6 @@ void xdp_do_check_flushed(struct napi_struct *napi);
269271
static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
270272
#endif
271273

272-
struct napi_struct *napi_by_id(unsigned int napi_id);
273274
void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu);
274275

275276
#define XMIT_RECURSION_LIMIT 8

net/core/netdev-genl.c

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

170-
if (WARN_ON_ONCE(!napi->dev))
171-
return -EINVAL;
172170
if (!(napi->dev->flags & IFF_UP))
173171
return 0;
174172

@@ -234,7 +232,7 @@ int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info)
234232
rtnl_lock();
235233
rcu_read_lock();
236234

237-
napi = napi_by_id(napi_id);
235+
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
238236
if (napi) {
239237
err = netdev_nl_napi_fill_one(rsp, napi, info);
240238
} else {
@@ -355,7 +353,7 @@ int netdev_nl_napi_set_doit(struct sk_buff *skb, struct genl_info *info)
355353
rtnl_lock();
356354
rcu_read_lock();
357355

358-
napi = napi_by_id(napi_id);
356+
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
359357
if (napi) {
360358
err = netdev_nl_napi_set_config(napi, info);
361359
} else {

0 commit comments

Comments
 (0)