* [PATCH net] netdev: prevent accessing NAPI instances from another namespace
@ 2025-01-06 18:01 Jakub Kicinski
2025-01-07 5:30 ` Samudrala, Sridhar
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Jakub Kicinski @ 2025-01-06 18:01 UTC (permalink / raw)
To: davem
Cc: netdev, edumazet, pabeni, Jakub Kicinski, stable, jdamato,
almasrymina, amritha.nambiar, sridhar.samudrala
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: 1287c1ae0fc2 ("netdev-genl: Support setting per-NAPI config values")
Fixes: 27f91aaf49b3 ("netdev-genl: Add netlink framework functions for napi")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
Splitting this into fix per-version is a bit tricky, because we need
to replace the napi_by_id() helper with a better one. I'll send the
stable versions manually.
CC: jdamato@fastly.com
CC: almasrymina@google.com
CC: amritha.nambiar@intel.com
CC: sridhar.samudrala@intel.com
---
net/core/dev.c | 43 +++++++++++++++++++++++++++++-------------
net/core/dev.h | 3 ++-
net/core/netdev-genl.c | 6 ++----
3 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/net/core/dev.c b/net/core/dev.c
index 7c63d97b13c1..e001df4cb486 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -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
@@ -6293,19 +6323,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;
diff --git a/net/core/dev.h b/net/core/dev.h
index aa91eed55a40..08812a025a9b 100644
--- a/net/core/dev.h
+++ b/net/core/dev.h
@@ -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
@@ -269,7 +271,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
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index 125b660004d3..a3bdaf075b6b 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -167,8 +167,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;
@@ -234,7 +232,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 {
@@ -355,7 +353,7 @@ int netdev_nl_napi_set_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_set_config(napi, info);
} else {
--
2.47.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-06 18:01 [PATCH net] netdev: prevent accessing NAPI instances from another namespace Jakub Kicinski
@ 2025-01-07 5:30 ` Samudrala, Sridhar
2025-01-07 13:10 ` Willem de Bruijn
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Samudrala, Sridhar @ 2025-01-07 5:30 UTC (permalink / raw)
To: Jakub Kicinski, davem
Cc: netdev, edumazet, pabeni, stable, jdamato, almasrymina,
amritha.nambiar
On 1/6/2025 10:01 AM, Jakub Kicinski wrote:
> 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: 1287c1ae0fc2 ("netdev-genl: Support setting per-NAPI config values")
> Fixes: 27f91aaf49b3 ("netdev-genl: Add netlink framework functions for napi")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Sridhar Samudrala <sridhar.samudrala@intel.com>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-06 18:01 [PATCH net] netdev: prevent accessing NAPI instances from another namespace Jakub Kicinski
2025-01-07 5:30 ` Samudrala, Sridhar
@ 2025-01-07 13:10 ` Willem de Bruijn
2025-01-07 16:36 ` Jakub Kicinski
2025-01-08 17:24 ` Joe Damato
2025-01-08 19:20 ` patchwork-bot+netdevbpf
3 siblings, 1 reply; 7+ messages in thread
From: Willem de Bruijn @ 2025-01-07 13:10 UTC (permalink / raw)
To: Jakub Kicinski, davem
Cc: netdev, edumazet, pabeni, Jakub Kicinski, stable, jdamato,
almasrymina, amritha.nambiar, sridhar.samudrala
Jakub Kicinski wrote:
> 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: 1287c1ae0fc2 ("netdev-genl: Support setting per-NAPI config values")
> Fixes: 27f91aaf49b3 ("netdev-genl: Add netlink framework functions for napi")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> Splitting this into fix per-version is a bit tricky, because we need
> to replace the napi_by_id() helper with a better one. I'll send the
> stable versions manually.
>
> CC: jdamato@fastly.com
> CC: almasrymina@google.com
> CC: amritha.nambiar@intel.com
> CC: sridhar.samudrala@intel.com
> ---
> net/core/dev.c | 43 +++++++++++++++++++++++++++++-------------
> net/core/dev.h | 3 ++-
> net/core/netdev-genl.c | 6 ++----
> 3 files changed, 34 insertions(+), 18 deletions(-)
>
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 7c63d97b13c1..e001df4cb486 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -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 */
Instead of function comments, invariant checks in code?
Like in dev_get_by_napi_id:
WARN_ON_ONCE(!rcu_read_lock_held());
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-07 13:10 ` Willem de Bruijn
@ 2025-01-07 16:36 ` Jakub Kicinski
2025-01-07 20:31 ` Willem de Bruijn
0 siblings, 1 reply; 7+ messages in thread
From: Jakub Kicinski @ 2025-01-07 16:36 UTC (permalink / raw)
To: Willem de Bruijn
Cc: davem, netdev, edumazet, pabeni, stable, jdamato, almasrymina,
amritha.nambiar, sridhar.samudrala
On Tue, 07 Jan 2025 08:10:36 -0500 Willem de Bruijn wrote:
> > +/* must be called under rcu_read_lock(), as we dont take a reference */
>
> Instead of function comments, invariant checks in code?
>
> Like in dev_get_by_napi_id:
>
> WARN_ON_ONCE(!rcu_read_lock_held());
Can I do it as a follow up? Adding the warning to napi_by_id()
reveals that napi_hash_add() currently walks the list without
holding the RCU lock :)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-07 16:36 ` Jakub Kicinski
@ 2025-01-07 20:31 ` Willem de Bruijn
0 siblings, 0 replies; 7+ messages in thread
From: Willem de Bruijn @ 2025-01-07 20:31 UTC (permalink / raw)
To: Jakub Kicinski, Willem de Bruijn
Cc: davem, netdev, edumazet, pabeni, stable, jdamato, almasrymina,
amritha.nambiar, sridhar.samudrala
Jakub Kicinski wrote:
> On Tue, 07 Jan 2025 08:10:36 -0500 Willem de Bruijn wrote:
> > > +/* must be called under rcu_read_lock(), as we dont take a reference */
> >
> > Instead of function comments, invariant checks in code?
> >
> > Like in dev_get_by_napi_id:
> >
> > WARN_ON_ONCE(!rcu_read_lock_held());
>
> Can I do it as a follow up? Adding the warning to napi_by_id()
> reveals that napi_hash_add() currently walks the list without
> holding the RCU lock :)
Ah I should have noticed that :) Of course, or ignore in this case
then.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-06 18:01 [PATCH net] netdev: prevent accessing NAPI instances from another namespace Jakub Kicinski
2025-01-07 5:30 ` Samudrala, Sridhar
2025-01-07 13:10 ` Willem de Bruijn
@ 2025-01-08 17:24 ` Joe Damato
2025-01-08 19:20 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 7+ messages in thread
From: Joe Damato @ 2025-01-08 17:24 UTC (permalink / raw)
To: Jakub Kicinski
Cc: davem, netdev, edumazet, pabeni, stable, almasrymina,
amritha.nambiar, sridhar.samudrala
On Mon, Jan 06, 2025 at 10:01:36AM -0800, Jakub Kicinski wrote:
> 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: 1287c1ae0fc2 ("netdev-genl: Support setting per-NAPI config values")
> Fixes: 27f91aaf49b3 ("netdev-genl: Add netlink framework functions for napi")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> Splitting this into fix per-version is a bit tricky, because we need
> to replace the napi_by_id() helper with a better one. I'll send the
> stable versions manually.
>
> CC: jdamato@fastly.com
> CC: almasrymina@google.com
> CC: amritha.nambiar@intel.com
> CC: sridhar.samudrala@intel.com
> ---
> net/core/dev.c | 43 +++++++++++++++++++++++++++++-------------
> net/core/dev.h | 3 ++-
> net/core/netdev-genl.c | 6 ++----
> 3 files changed, 34 insertions(+), 18 deletions(-)
Thanks.
Reviewed-by: Joe Damato <jdamato@fastly.com>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net] netdev: prevent accessing NAPI instances from another namespace
2025-01-06 18:01 [PATCH net] netdev: prevent accessing NAPI instances from another namespace Jakub Kicinski
` (2 preceding siblings ...)
2025-01-08 17:24 ` Joe Damato
@ 2025-01-08 19:20 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-01-08 19:20 UTC (permalink / raw)
To: Jakub Kicinski
Cc: davem, netdev, edumazet, pabeni, stable, jdamato, almasrymina,
amritha.nambiar, sridhar.samudrala
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Mon, 6 Jan 2025 10:01:36 -0800 you wrote:
> 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().
>
> [...]
Here is the summary with links:
- [net] netdev: prevent accessing NAPI instances from another namespace
https://git.kernel.org/netdev/net/c/d1cacd747768
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-01-08 19:20 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-06 18:01 [PATCH net] netdev: prevent accessing NAPI instances from another namespace Jakub Kicinski
2025-01-07 5:30 ` Samudrala, Sridhar
2025-01-07 13:10 ` Willem de Bruijn
2025-01-07 16:36 ` Jakub Kicinski
2025-01-07 20:31 ` Willem de Bruijn
2025-01-08 17:24 ` Joe Damato
2025-01-08 19:20 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).