Skip to content

Commit

Permalink
netdev: prevent accessing NAPI instances from another namespace
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jakub Kicinski authored and Greg Kroah-Hartman committed Jan 17, 2025
1 parent c08d7fc commit b683ba0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
43 changes: 30 additions & 13 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,36 @@ int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
}
EXPORT_SYMBOL_GPL(dev_fill_forward_path);

/* must be called under rcu_read_lock(), as we dont take a reference */
static struct napi_struct *napi_by_id(unsigned int napi_id)
{
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
struct napi_struct *napi;

hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
if (napi->napi_id == napi_id)
return napi;

return NULL;
}

/* must be called under rcu_read_lock(), as we dont take a reference */
struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id)
{
struct napi_struct *napi;

napi = napi_by_id(napi_id);
if (!napi)
return NULL;

if (WARN_ON_ONCE(!napi->dev))
return NULL;
if (!net_eq(net, dev_net(napi->dev)))
return NULL;

return napi;
}

/**
* __dev_get_by_name - find a device by its name
* @net: the applicable net namespace
Expand Down Expand Up @@ -6291,19 +6321,6 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
}
EXPORT_SYMBOL(napi_complete_done);

/* must be called under rcu_read_lock(), as we dont take a reference */
struct napi_struct *napi_by_id(unsigned int napi_id)
{
unsigned int hash = napi_id % HASH_SIZE(napi_hash);
struct napi_struct *napi;

hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
if (napi->napi_id == napi_id)
return napi;

return NULL;
}

static void skb_defer_free_flush(struct softnet_data *sd)
{
struct sk_buff *skb, *next;
Expand Down
3 changes: 2 additions & 1 deletion net/core/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct sd_flow_limit {

extern int netdev_flow_limit_table_len;

struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id);

#ifdef CONFIG_PROC_FS
int __init dev_proc_init(void);
#else
Expand Down Expand Up @@ -146,7 +148,6 @@ void xdp_do_check_flushed(struct napi_struct *napi);
static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
#endif

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

#define XMIT_RECURSION_LIMIT 8
Expand Down
4 changes: 1 addition & 3 deletions net/core/netdev-genl.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
void *hdr;
pid_t pid;

if (WARN_ON_ONCE(!napi->dev))
return -EINVAL;
if (!(napi->dev->flags & IFF_UP))
return 0;

Expand Down Expand Up @@ -216,7 +214,7 @@ int netdev_nl_napi_get_doit(struct sk_buff *skb, struct genl_info *info)
rtnl_lock();
rcu_read_lock();

napi = napi_by_id(napi_id);
napi = netdev_napi_by_id(genl_info_net(info), napi_id);
if (napi) {
err = netdev_nl_napi_fill_one(rsp, napi, info);
} else {
Expand Down

0 comments on commit b683ba0

Please sign in to comment.