Netfilter updates for net
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter updates for net
Hello,
The following patchset contains Netfilter fixes for net:
1) Fix use-after-free in ipset bitmap destroy path, from Cong Wang.
2) Missing init netns in entry cleanup path of arp_tables, from Florian Westphal.
3) Fix WARN_ON in set destroy path due to missing cleanup on transaction error.
4) Incorrect netlink sanity check in tunnel, from Florian Westphal.
5) Missing sanity check for erspan version netlink attribute, also from Florian.
6) Remove WARN in nft_request_module() that can be triggered from userspace, from Florian Westphal.
7) Memleak in NFTA_HOOK_DEVS netlink parser, from Dan Carpenter.
8) List poison from commit path for flowtables that are added and deleted in the same batch, from Florian Westphal.
9) Fix NAT ICMP packet corruption, from Eyal Birger.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Thank you.
----------------------------------------------------------------
The following changes since commit c9f53049d4a842db6bcd76f597759a0ef5f65c86:
MAINTAINERS: update my email address (2020-01-11 14:33:39 -0800)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD
for you to fetch changes up to 61177e911dad660df86a4553eb01c95ece2f6a82:
netfilter: nat: fix ICMP header corruption on ICMP errors (2020-01-16 15:08:25 +0100)
----------------------------------------------------------------
Cong Wang (1):
netfilter: fix a use-after-free in mtype_destroy()
Dan Carpenter (1):
netfilter: nf_tables: fix memory leak in nf_tables_parse_netdev_hooks()
Eyal Birger (1):
netfilter: nat: fix ICMP header corruption on ICMP errors
Florian Westphal (5):
netfilter: arp_tables: init netns pointer in xt_tgdtor_param struct
netfilter: nft_tunnel: fix null-attribute check
netfilter: nft_tunnel: ERSPAN_VERSION must not be null
netfilter: nf_tables: remove WARN and add NLA_STRING upper limits
netfilter: nf_tables: fix flowtable list del corruption
Pablo Neira Ayuso (1):
netfilter: nf_tables: store transaction list locally while requesting module
net/ipv4/netfilter/arp_tables.c | 19 ++++++++-------- net/netfilter/ipset/ip_set_bitmap_gen.h | 2 +- net/netfilter/nf_nat_proto.c | 13 +++++++++++ net/netfilter/nf_tables_api.c | 39 ++++++++++++++++++++++----------- net/netfilter/nft_tunnel.c | 5 ++++- 5 files changed, 54 insertions(+), 24 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/9 netfilter: fix a use-after-free in mtype_destroy()
From: Cong Wang <[email protected]>
map->members is freed by ip_set_free() right before using it in mtype_ext_cleanup() again. So we just have to move it down.
--- net/netfilter/ipset/ip_set_bitmap_gen.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipset/ip_set_bitmap_gen.h +1 -1
@@ -60,9 +60,9 @@ mtype_destroy(struct ip_set *set)if (SET_WITH_TIMEOUT (set))del_timer_sync (&map->gc); -ip_set_free (map->members);if (set->dsize && set->extensions &IPSET_EXT_DESTROY )mtype_ext_cleanup (set); +ip_set_free (map->members);ip_set_free (map); set->data =NULL ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/9 netfilter: arp_tables: init netns pointer in xt_tgdtor_param struct
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
An earlier commit (1b789577f655060d98d20e,
"netfilter: arp_tables: init netns pointer in xt_tgchk_param struct") fixed missing net initialization for arptables, but turns out it was incomplete. We can get a very similar struct net NULL deref during error unwinding:
general protection fault: 0000 [#1] PREEMPT SMP KASAN RIP: 0010:xt_rateest_put+0xa1/0x440 net/netfilter/xt_RATEEST.c:77 xt_rateest_tg_destroy+0x72/0xa0 net/netfilter/xt_RATEEST.c:175 cleanup_entry net/ipv4/netfilter/arp_tables.c:509 [inline] translate_table+0x11f4/0x1d80 net/ipv4/netfilter/arp_tables.c:587 do_replace net/ipv4/netfilter/arp_tables.c:981 [inline] do_arpt_set_ctl+0x317/0x650 net/ipv4/netfilter/arp_tables.c:1461
Also init the netns pointer in xt_tgdtor_param struct.
--- net/ipv4/netfilter/arp_tables.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
net/ipv4/netfilter/arp_tables.c +10 -9
@@ -496,12 +496,13 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,return 0 ; } -static inline void cleanup_entry (struct arpt_entry *e) +static void cleanup_entry (struct arpt_entry *e,struct net *net) {struct xt_tgdtor_param par;struct xt_entry_target *t; t =arpt_get_target (e); + par.net = net; par.target = t->u.kernel.target; par.targinfo = t->data; par.family =NFPROTO_ARP ;
@@ -584,7 +585,7 @@ static int translate_table(struct net *net, xt_entry_foreach(iter, entry0, newinfo->size) {if (i-- ==0 )break ; -cleanup_entry (iter); +cleanup_entry (iter, net); }return ret; }
@@ -927,7 +928,7 @@ static int __do_replace(struct net *net, const char *name,/* Decrease module usage counts and free resource */ loc_cpu_old_entry = oldinfo->entries; xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size) -cleanup_entry (iter); +cleanup_entry (iter, net);xt_free_table_info (oldinfo); if (copy_to_user(counters_ptr, counters,
@@ -990,7 +991,7 @@ static int do_replace(struct net *net, const void __user *user, free_newinfo_untrans: xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) -cleanup_entry (iter); +cleanup_entry (iter, net); free_newinfo:xt_free_table_info (newinfo);return ret;
@@ -1287,7 +1288,7 @@ static int compat_do_replace(struct net *net, void __user *user, free_newinfo_untrans: xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) -cleanup_entry (iter); +cleanup_entry (iter, net); free_newinfo:xt_free_table_info (newinfo);return ret;
@@ -1514,7 +1515,7 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *lenreturn ret; } -static void __arpt_unregister_table (struct xt_table *table) +static void __arpt_unregister_table (struct net *net,struct xt_table *table) {struct xt_table_info *private;void *loc_cpu_entry;
@@ -1526,7 +1527,7 @@ static void __arpt_unregister_table(struct xt_table *table)/* Decrease module usage counts and free resources */ loc_cpu_entry = private->entries; xt_entry_foreach(iter, loc_cpu_entry, private->size) -cleanup_entry (iter); +cleanup_entry (iter, net);if (private->number > private->initial_entries)module_put (table_owner);xt_free_table_info (private);
@@ -1566,7 +1567,7 @@ int arpt_register_table(struct net *net, ret =nf_register_net_hooks (net, ops,hweight32 (table->valid_hooks));if (ret !=0 ) { -__arpt_unregister_table (new_table); +__arpt_unregister_table (net, new_table); *res =NULL ; }
@@ -1581,7 +1582,7 @@ void arpt_unregister_table(struct net *net, struct xt_table *table, const struct nf_hook_ops *ops) {nf_unregister_net_hooks (net, ops,hweight32 (table->valid_hooks)); -__arpt_unregister_table (table); +__arpt_unregister_table (net, table); }/* The built-in targets: standard (NULL) and error. */
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/9 netfilter: nf_tables: store transaction list locally while requesting module
In reply to Pablo Neira AyusoThis patch fixes a WARN_ON in nft_set_destroy() due to missing set reference count drop from the preparation phase. This is triggered by the module autoload path. Do not exercise the abort path from nft_request_module() while preparation phase cleaning up is still pending.
WARNING: CPU: 3 PID: 3456 at net/netfilter/nf_tables_api.c:3740 nft_set_destroy+0x45/0x50 [nf_tables] [...] CPU: 3 PID: 3456 Comm: nft Not tainted 5.4.6-arch3-1 #1 RIP: 0010:nft_set_destroy+0x45/0x50 [nf_tables] Code: e8 30 eb 83 c6 48 8b 85 80 00 00 00 48 8b b8 90 00 00 00 e8 dd 6b d7 c5 48 8b 7d 30 e8 24 dd eb c5 48 89 ef 5d e9 6b c6 e5 c5 <0f> 0b c3 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 8b 7f 10 e9 52 RSP: 0018:ffffac4f43e53700 EFLAGS: 00010202 RAX: 0000000000000001 RBX: ffff99d63a154d80 RCX: 0000000001f88e03 RDX: 0000000001f88c03 RSI: ffff99d6560ef0c0 RDI: ffff99d63a101200 RBP: ffff99d617721de0 R08: 0000000000000000 R09: 0000000000000318 R10: 00000000f0000000 R11: 0000000000000001 R12: ffffffff880fabf0 R13: dead000000000122 R14: dead000000000100 R15: ffff99d63a154d80 FS: 00007ff3dbd5b740(0000) GS:ffff99d6560c0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00001cb5de6a9000 CR3: 000000016eb6a004 CR4: 00000000001606e0 Call Trace: __nf_tables_abort+0x3e3/0x6d0 [nf_tables] nft_request_module+0x6f/0x110 [nf_tables] nft_expr_type_request_module+0x28/0x50 [nf_tables] nf_tables_expr_parse+0x198/0x1f0 [nf_tables] nft_expr_init+0x3b/0xf0 [nf_tables] nft_dynset_init+0x1e2/0x410 [nf_tables] nf_tables_newrule+0x30a/0x930 [nf_tables] nfnetlink_rcv_batch+0x2a0/0x640 [nfnetlink] nfnetlink_rcv+0x125/0x171 [nfnetlink] netlink_unicast+0x179/0x210 netlink_sendmsg+0x208/0x3d0 sock_sendmsg+0x5e/0x60 ____sys_sendmsg+0x21b/0x290
Update comment on the code to describe the new behaviour.
--- net/netfilter/nf_tables_api.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +10 -9
@@ -564,23 +564,21 @@ __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) }/* -* Loading a module requires dropping mutex that guards the -* transaction. -* We first need to abort any pending transactions as once -* mutex is unlocked a different client could start a new -* transaction. It must not see any 'future generation' -* changes * as these changes will never happen. +* Loading a module requires dropping mutex that guards the transaction. +* A different client might race to start a new transaction meanwhile. Zap the +* list of pending transaction and then restore it once the mutex is grabbed +* again. Users of this function return EAGAIN which implicitly triggers the +* transaction abort path to clean up the list of pending transactions. */ #ifdef CONFIG_MODULES -static int __nf_tables_abort (struct net *net); -static void nft_request_module (struct net *net,const char *fmt, ...) {char module_name[MODULE_NAME_LEN ]; +LIST_HEAD (commit_list);va_list args;int ret; -__nf_tables_abort (net); +list_splice_init (&net->nft.commit_list, &commit_list);va_start (args, fmt); ret =vsnprintf (module_name,MODULE_NAME_LEN , fmt, args);
@@ -591,6 +589,9 @@ static void nft_request_module(struct net *net, const char *fmt, ...)mutex_unlock (&net->nft.commit_mutex);request_module ("%s" , module_name);mutex_lock (&net->nft.commit_mutex); + +WARN_ON_ONCE (!list_empty (&net->nft.commit_list)); + list_splice(&commit_list, &net->nft.commit_list); }#endif
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/9 netfilter: nft_tunnel: fix null-attribute check
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
else we get null deref when one of the attributes is missing, both must be non-null.
--- net/netfilter/nft_tunnel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_tunnel.c +1 -1
@@ -76,7 +76,7 @@ static int nft_tunnel_get_init(const struct nft_ctx *ctx,struct nft_tunnel *priv =nft_expr_priv (expr);u32 len; -if (!tb[NFTA_TUNNEL_KEY ] && +if (!tb[NFTA_TUNNEL_KEY ] || !tb[NFTA_TUNNEL_DREG ])return -EINVAL ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/9 netfilter: nft_tunnel: ERSPAN_VERSION must not be null
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
--- net/netfilter/nft_tunnel.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nft_tunnel.c +3 -0
@@ -266,6 +266,9 @@ static int nft_tunnel_obj_erspan_init(const struct nlattr *attr,if (err <0 )return err; +if (!tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION ]) +return -EINVAL ; + version =ntohl (nla_get_be32 (tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION ]));switch (version) {case ERSPAN_VERSION :
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 6/9 netfilter: nf_tables: remove WARN and add NLA_STRING upper limits
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
This WARN can trigger because some of the names fed to the module autoload function can be of arbitrary length.
Remove the WARN and add limits for all NLA_STRING attributes.
--- net/netfilter/nf_tables_api.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +9 -4
@@ -22,6 +22,8 @@#include <net/net_namespace.h> #include <net/sock.h> +#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) +static LIST_HEAD (nf_tables_expressions);static LIST_HEAD (nf_tables_objects);static LIST_HEAD (nf_tables_flowtables);
@@ -583,7 +585,7 @@ static void nft_request_module(struct net *net, const char *fmt, ...)va_start (args, fmt); ret =vsnprintf (module_name,MODULE_NAME_LEN , fmt, args);va_end (args); -if (WARN (ret >=MODULE_NAME_LEN ,"truncated: '%s' (len %d)" , module_name, ret)) +if (ret >=MODULE_NAME_LEN )return ;mutex_unlock (&net->nft.commit_mutex);
@@ -1242,7 +1244,8 @@ static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { .len = NFT_CHAIN_MAXNAMELEN - 1 }, [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, - [NFTA_CHAIN_TYPE] = { .type = NLA_STRING }, + [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, + .len = NFT_MODULE_AUTOLOAD_LIMIT }, [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, };
@@ -2356,7 +2359,8 @@ static const struct nft_expr_type *nft_expr_type_get(struct net *net, }static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX +1 ] = { - [NFTA_EXPR_NAME ] = { .type =NLA_STRING }, + [NFTA_EXPR_NAME ] = { .type =NLA_STRING , + .len =NFT_MODULE_AUTOLOAD_LIMIT }, [NFTA_EXPR_DATA ] = { .type =NLA_NESTED }, };
@@ -4199,7 +4203,8 @@ static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { [NFTA_SET_ELEM_USERDATA ] = { .type =NLA_BINARY , .len = NFT_USERDATA_MAXLEN }, [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, - [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING }, + [NFTA_SET_ELEM_OBJREF ] = { .type =NLA_STRING , + .len = NFT_OBJ_MAXNAMELEN - 1 }, };static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX +1 ] = {
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 7/9 netfilter: nf_tables: fix memory leak in nf_tables_parse_netdev_hooks()
In reply to Pablo Neira AyusoFrom: Dan Carpenter <[email protected]>
Syzbot detected a leak in nf_tables_parse_netdev_hooks(). If the hook already exists, then the error handling doesn't free the newest "hook".
--- net/netfilter/nf_tables_api.c | 1 + 1 file changed, 1 insertion(+)
net/netfilter/nf_tables_api.c +1 -0
@@ -1680,6 +1680,7 @@ static int nf_tables_parse_netdev_hooks(struct net *net, goto err_hook; }if (nft_hook_list_find (hook_list, hook)) { +kfree (hook); err = -EEXIST ; goto err_hook; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 8/9 netfilter: nf_tables: fix flowtable list del corruption
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
syzbot reported following crash:
list_del corruption, ffff88808c9bb000->prev is LIST_POISON2 (dead000000000122) [..]
Call Trace: __list_del_entry include/linux/list.h:131 [inline] list_del_rcu include/linux/rculist.h:148 [inline] nf_tables_commit+0x1068/0x3b30 net/netfilter/nf_tables_api.c:7183 [..]
The commit transaction list has:
NFT_MSG_NEWTABLENFT_MSG_NEWFLOWTABLENFT_MSG_DELFLOWTABLENFT_MSG_DELTABLE
A missing generation check during DELTABLE processing causes it to queue the DELFLOWTABLE operation a second time, so we corrupt the list here:
case NFT_MSG_DELFLOWTABLE:
list_del_rcu(&nft_trans_flowtable(trans)->list);
nf_tables_flowtable_notify(&trans->ctx,because we have two different DELFLOWTABLE transactions for the same flowtable. We then call list_del_rcu() twice for the same flowtable->list.
The object handling seems to suffer from the same bug so add a generation check too and only queue delete transactions for flowtables/objects that are still active in the next generation.
--- net/netfilter/nf_tables_api.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +6 -0
@@ -1048,12 +1048,18 @@ static int nft_flush_table(struct nft_ctx *ctx) }list_for_each_entry_safe (flowtable, nft, &ctx->table->flowtables, list) { +if (!nft_is_active_next (ctx->net, flowtable)) +continue ; + err =nft_delflowtable (ctx, flowtable);if (err <0 ) goto out; }list_for_each_entry_safe (obj, ne, &ctx->table->objects, list) { +if (!nft_is_active_next (ctx->net, obj)) +continue ; + err =nft_delobj (ctx, obj);if (err <0 ) goto out;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 9/9 netfilter: nat: fix ICMP header corruption on ICMP errors
In reply to Pablo Neira AyusoFrom: Eyal Birger <[email protected]>
Commit 8303b7e8f018 ("netfilter: nat: fix spurious connection timeouts") made nf_nat_icmp_reply_translation() use icmp_manip_pkt() as the l4 manipulation function for the outer packet on ICMP errors.
However, icmp_manip_pkt() assumes the packet has an 'id' field which is not correct for all types of ICMP messages.
This is not correct for ICMP error packets, and leads to bogus bytes being written the ICMP header, which can be wrongfully regarded as 'length' bytes by RFC 4884 compliant receivers.
Fix by assigning the 'id' field only for ICMP messages that have this semantic.
--- net/netfilter/nf_nat_proto.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
net/netfilter/nf_nat_proto.c +13 -0
@@ -233,6 +233,19 @@ icmp_manip_pkt(struct sk_buff *skb,return false; hdr = (struct icmphdr *)(skb->data + hdroff); +switch (hdr->type) { +case ICMP_ECHO : +case ICMP_ECHOREPLY : +case ICMP_TIMESTAMP : +case ICMP_TIMESTAMPREPLY : +case ICMP_INFO_REQUEST : +case ICMP_INFO_REPLY : +case ICMP_ADDRESS : +case ICMP_ADDRESSREPLY : +break ; +default : +return true; + }inet_proto_csum_replace2 (&hdr->checksum, skb, hdr->un.echo.id, tuple->src.u.icmp.id, false); hdr->un.echo.id = tuple->src.u.icmp.id;
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Thu, 16 Jan 2020 20:50:35 +0100
Quoted 1 line
The following patchset contains Netfilter fixes for net:
...
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Pulled, thanks Pablo.