netfilter: nf_tables: fix device name and prefix match in hook lookup
Subsystems (2)
PATCH v2 1/1 netfilter: nf_tables: fix device name and prefix match in hook lookup
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of min() to calculate the length to match.
Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +24 -3
@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }
@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ; } +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ; +if (hook->ifnamelen != this->ifnamelen) +continue ; +if (!strncmp (hook->ifname, this->ifname, hook->ifnamelen)) { +if (hook->flags &NFT_HOOK_REMOVE ) +continue ; + +return hook; + } + } + +return NULL ; +} + static int nf_tables_parse_netdev_hooks(struct net *net, const struct nlattr *attr, struct list_head *hook_list,
@@ -3257,7 +3278,7 @@ static int nft_delchain_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &chain_hook.list, list) { - hook =nft_hook_list_find (&basechain->hook_list, this); + hook =nft_hook_list_find_strict (&basechain->hook_list, this);if (!hook) { err = -ENOENT ; goto err_chain_del_hook;
@@ -9383,7 +9404,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &flowtable_hook.list, list) { - hook =nft_hook_list_find (&flowtable->hook_list, this); + hook =nft_hook_list_find_strict (&flowtable->hook_list, this);if (!hook) { err = -ENOENT ; goto err_flowtable_del_hook;
Signature · 1 line
2.55.0
Pablo Neira Ayuso
maintainerHi Fernando,
Quoted 9 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
netdev maintainers prefer follow ups for LLM reported issues.
Please, provide an incremental patch for your v1 that we can take in the next PR round.
Thanks!
Quoted 70 lines
Reported-by Wei Fang <[email protected]>Closes https://lore.kernel.org/netfilter-devel/CANE+tVrDeNCHQVmsqkV2ozeBqyE3GtRDMhZgsg1bhw10yGNTRQ@mail.gmail.com/ open hereFixes 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +24
-3@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ; } +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ; +if (hook->ifnamelen != this->ifnamelen) +continue ; +if (!strncmp (hook->ifname, this->ifname, hook->ifnamelen)) { +if (hook->flags &NFT_HOOK_REMOVE ) +continue ; + +return hook; + } + } + +return NULL ; +} + static int nf_tables_parse_netdev_hooks(struct net *net, const struct nlattr *attr, struct list_head *hook_list,@@ -3257,7 +3278,7 @@ static int nft_delchain_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &chain_hook.list, list) { - hook =nft_hook_list_find (&basechain->hook_list, this); + hook =nft_hook_list_find_strict (&basechain->hook_list, this);if (!hook) { err = -ENOENT ; goto err_chain_del_hook;@@ -9383,7 +9404,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &flowtable_hook.list, list) { - hook =nft_hook_list_find (&flowtable->hook_list, this); + hook =nft_hook_list_find_strict (&flowtable->hook_list, this);if (!hook) { err = -ENOENT ; goto err_flowtable_del_hook;Signature · 1 line
2.55.0
Quoted 13 lines · Pablo Neira Ayuso
Hi Fernando,
Quoted 9 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
Quoted 5 lines
netdev maintainers prefer follow ups for LLM reported issues.
Please, provide an incremental patch for your v1 that we can take in the next PR round.
Oh really? Even in this situation? Will do once the PR is accepted. The patch won't be much different of course.
Thanks!
Quoted 72 lines
Thanks!
Quoted 70 lines
Reported-by Wei Fang <[email protected]>Closes https://lore.kernel.org/netfilter-devel/CANE+tVrDeNCHQVmsqkV2ozeBqyE3GtRDMhZgsg1bhw10yGNTRQ@mail.gmail.com/ open hereFixes 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +24
-3@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ; } +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ; +if (hook->ifnamelen != this->ifnamelen) +continue ; +if (!strncmp (hook->ifname, this->ifname, hook->ifnamelen)) { +if (hook->flags &NFT_HOOK_REMOVE ) +continue ; + +return hook; + } + } + +return NULL ; +} + static int nf_tables_parse_netdev_hooks(struct net *net, const struct nlattr *attr, struct list_head *hook_list,@@ -3257,7 +3278,7 @@ static int nft_delchain_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &chain_hook.list, list) { - hook =nft_hook_list_find (&basechain->hook_list, this); + hook =nft_hook_list_find_strict (&basechain->hook_list, this);if (!hook) { err = -ENOENT ; goto err_chain_del_hook;@@ -9383,7 +9404,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,return err;list_for_each_entry (this, &flowtable_hook.list, list) { - hook =nft_hook_list_find (&flowtable->hook_list, this); + hook =nft_hook_list_find_strict (&flowtable->hook_list, this);if (!hook) { err = -ENOENT ; goto err_flowtable_del_hook;Signature · 1 line
2.55.0
Pablo Neira Ayuso
maintainerQuoted 14 lines · Fernando Fernandez Mancera
Quoted 13 lines · Pablo Neira Ayuso
Hi Fernando,
Quoted 9 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
Quoted 7 lines
Quoted 5 lines
netdev maintainers prefer follow ups for LLM reported issues.
Please, provide an incremental patch for your v1 that we can take in the next PR round.
Oh really? Even in this situation?
Yes, this helps reduce pressure on the mailing list.
Quoted 2 lines
Will do once the PR is accepted. The patch won't be much different of course.
I guess so, it will be just a simple rebase.
Thanks
Phil Sutter
reviewerQuoted 33 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
Reported-by Wei Fang <[email protected]>Closes https://lore.kernel.org/netfilter-devel/CANE+tVrDeNCHQVmsqkV2ozeBqyE3GtRDMhZgsg1bhw10yGNTRQ@mail.gmail.com/ open hereFixes 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +1
-1@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ;
net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2467
Quoted 10 lines
net/netfilter/nf_tables_api.c +8
-0} +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ;
I don't think this is necessary: The first ifnamelen chars of both hooks' ifname either contain the NUL-char or not. If both ifnamelen values are identical, the following strncmp will only match if either both are prefixes or both are not.
(I realized my initial approach using max() won't work when comparing "eth0\0" (non-prefix) to "eth0" (prefix) as the latter is followed by NUL-chars as well.)
net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2469
Quoted 2 lines
net/netfilter/nf_tables_api.c +2
-0+if (hook->ifnamelen != this->ifnamelen) +continue ;
Given the above, this could become 'if (strict && hook-ifnamelen !=...)' in nft_hook_list_find with extra parameter, up to you.
Thanks, Phil
Quoted 54 lines · Phil Sutter
Quoted 33 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
Reported-by Wei Fang <[email protected]>Closes https://lore.kernel.org/netfilter-devel/CANE+tVrDeNCHQVmsqkV2ozeBqyE3GtRDMhZgsg1bhw10yGNTRQ@mail.gmail.com/ open hereFixes 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +1
-1@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ;net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2467
Quoted 10 lines
net/netfilter/nf_tables_api.c +8
-0} +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ;I don't think this is necessary: The first ifnamelen chars of both hooks' ifname either contain the NUL-char or not. If both ifnamelen values are identical, the following strncmp will only match if either both are prefixes or both are not.
(I realized my initial approach using
max()won't work when comparing "eth0\0" (non-prefix) to "eth0" (prefix) as the latter is followed by NUL-chars as well.)
Quoted 6 lines
net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2469
Quoted 2 lines
net/netfilter/nf_tables_api.c +2
-0+if (hook->ifnamelen != this->ifnamelen) +continue ;Given the above, this could become 'if (strict && hook-ifnamelen !=...)' in
nft_hook_list_findwith extra parameter, up to you.
Hm, that is true. I am sending a v3 following this method as it is simpler.
Pablo, feel free to pick up either this v2 or the v3 I am sending. Both are fine to me.
Thank you Phil and Pablo!
Pablo Neira Ayuso
maintainerHi Fernando,
Quoted 60 lines · Fernando Fernandez Mancera
Quoted 52 lines · Phil Sutter
Quoted 31 lines · Fernando Fernandez Mancera
Currently, a netdev chain or flowtable hooked to a device prefix can be unintentionally deleted by a control-plane request targeting an exact device name or even a shorter one due to the usage of
min()to calculate the length to match.Fix this by making sure an exact device match never matches a prefix and that both the target and the candidate have the same length during delete operation. The add and update paths retain the existing overlap matching to prevent a single device from matching multiple hooks.
Reported-by Wei Fang <[email protected]>Closes https://lore.kernel.org/netfilter-devel/CANE+tVrDeNCHQVmsqkV2ozeBqyE3GtRDMhZgsg1bhw10yGNTRQ@mail.gmail.com/ open hereFixes 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nf_tables_api.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)net/netfilter/nf_tables_api.c +1
-1@@ -1978,7 +1978,7 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)return -ENOSPC ; } -static bool hook_is_prefix (struct nft_hook *hook) +static bool hook_is_prefix (const struct nft_hook *hook) {return strlen (hook->ifname) >= hook->ifnamelen; }net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2466
Quoted 10 lines
net/netfilter/nf_tables_api.c +8
-0@@ -2457,6 +2457,27 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,return NULL ; } +static struct nft_hook *nft_hook_list_find_strict (struct list_head *hook_list, +const struct nft_hook *this) +{ +struct nft_hook *hook; + +list_for_each_entry (hook, hook_list, list) { +if (hook_is_prefix (hook) !=hook_is_prefix (this)) +continue ;I don't think this is necessary: The first ifnamelen chars of both hooks' ifname either contain the NUL-char or not. If both ifnamelen values are identical, the following strncmp will only match if either both are prefixes or both are not.
(I realized my initial approach using
max()won't work when comparing "eth0\0" (non-prefix) to "eth0" (prefix) as the latter is followed by NUL-chars as well.)Quoted 6 lines
net/netfilter/nf_tables_api.c static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, · line 2468
Quoted 2 lines
net/netfilter/nf_tables_api.c +2
-0+if (hook->ifnamelen != this->ifnamelen) +continue ;Given the above, this could become 'if (strict && hook-ifnamelen !=...)' in
nft_hook_list_findwith extra parameter, up to you.
Quoted 4 lines
Hm, that is true. I am sending a v3 following this method as it is simpler.
Pablo, feel free to pick up either this v2 or the v3 I am sending. Both are fine to me.
I can wait for a few hours before the next PR if you'd like to explore the v3.
Thanks.