3.16.37-rc1 review
Subsystems (3)
PATCH 302/305 netfilter: x_tables: do compat validation via translate_table
In reply to Ben Hutchings3.16.37-rc1 review patch. If anyone has any objections, please let me know.
------------------
From: Florian Westphal <[email protected]>
commit 09d9686047dbbe1cf4faa558d3ecc4aae2046054 upstream.
This looks like refactoring, but its also a bug fix.
Problem is that the compat path (32bit iptables, 64bit kernel) lacks a few sanity tests that are done in the normal path.
For example, we do not check for underflows and the base chain policies.
While its possible to also add such checks to the compat path, its more copy&pastry, for instance we cannot reuse check_underflow() helper as e->target_offset differs in the compat case.
Other problem is that it makes auditing for validation errors harder; two places need to be checked and kept in sync.
At a high level 32 bit compat works like this:
1- initial pass over blob:
validate match/entry offsets, bounds checking lookup all matches and targets
do bookkeeping wrt. size delta of 32/64bit structures assign match/target.u.kernel pointer (points at kernel implementation, needed to access ->compatsize etc.)
2- allocate memory according to the total bookkeeping size to
contain the translated ruleset
3- second pass over original blob:
for each entry, copy the 32bit representation to the newly allocated memory. This also does any special match translations (e.g. adjust 32bit to 64bit longs, etc).
4- check if ruleset is free of loops (chase all jumps)
5-first pass over translated blob:
call the checkentry function of all matches and targets.
The alternative implemented by this patch is to drop steps 3&4 from the compat process, the translation is changed into an intermediate step rather than a full 1:1 translate_table replacement.
In the 2nd pass (step #3), change the 64bit ruleset back to a kernel representation, i.e. put() the kernel pointer and restore ->u.user.name .
This gets us a 64bit ruleset that is in the format generated by a 64bit iptables userspace -- we can then use translate_table() to get the 'native' sanity checks.
This has two drawbacks:
1. we re-validate all the match and target entry structure sizes even though compat translation is supposed to never generate bogus offsets.
2. we put and then re-lookup each match and target.
THe upside is that we get all sanity tests and ruleset validations provided by the normal path and can remove some duplicated compat code.
iptables-restore time of autogenerated ruleset with 300k chains of form -A CHAIN0001 -m limit --limit 1/s -j CHAIN0002
-A CHAIN0002 -m limit --limit 1/s -j CHAIN0003
shows no noticeable differences in restore times:
old: 0m30.796s new: 0m31.521s
64bit: 0m25.674s
---
net/ipv4/netfilter/arp_tables.c +22 -88
@@ -1225,19 +1225,17 @@ static inline void compat_release_entry(module_put (t->u.kernel.target->me); } -static inline int +static int check_compat_entry_size_and_hooks (struct compat_arpt_entry *e,struct xt_table_info *newinfo,unsigned int *size,const unsigned char *base, -const unsigned char *limit, -const unsigned int *hook_entries, -const unsigned int *underflows) +const unsigned char *limit) {struct xt_entry_target *t;struct xt_target *target;unsigned int entry_offset; -int ret, off, h; +int ret, off;duprintf ("check_compat_entry_size_and_hooks %p\n" , e); if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
@@ -1282,17 +1280,6 @@ check_compat_entry_size_and_hooks(structif (ret) goto release_target; -/* Check hooks & underflows */ -for (h =0 ; h <NF_ARP_NUMHOOKS ; h++) { -if ((unsigned char *)e - base == hook_entries[h]) - newinfo->hook_entry[h] = hook_entries[h]; -if ((unsigned char *)e - base == underflows[h]) - newinfo->underflow[h] = underflows[h]; - } - -/* Clear counters and comefrom */ -memset (&e->counters,0 ,sizeof (e->counters)); - e->comefrom =0 ;return 0 ; release_target:
@@ -1342,7 +1329,7 @@ static int translate_compat_table(structstruct xt_table_info *newinfo, *info;void *pos, *entry0, *entry1;struct compat_arpt_entry *iter0; -struct arpt_entry *iter1; +struct arpt_replace repl;unsigned int size;int ret =0 ;
@@ -1351,12 +1338,6 @@ static int translate_compat_table(struct size = compatr->size; info->number = compatr->num_entries; -/* Init all hooks to impossible value. */ -for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { - info->hook_entry[i] =0xFFFFFFFF ; - info->underflow[i] =0xFFFFFFFF ; - } -duprintf ("translate_compat_table: size %u\n" , info->size); j =0 ;xt_compat_lock (NFPROTO_ARP );
@@ -1365,9 +1346,7 @@ static int translate_compat_table(struct xt_entry_foreach(iter0, entry0, compatr->size) { ret =check_compat_entry_size_and_hooks (iter0, info, &size, entry0, - entry0 + compatr->size, - compatr->hook_entry, - compatr->underflow); + entry0 + compatr->size);if (ret !=0 ) goto out_unlock; ++j;
@@ -1380,23 +1359,6 @@ static int translate_compat_table(struct goto out_unlock; } -/* Check hooks all assigned */ -for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { -/* Only hooks which are valid */ -if (!(compatr->valid_hooks & (1 << i))) -continue ; -if (info->hook_entry[i] ==0xFFFFFFFF ) { -duprintf ("Invalid hook entry %u %u\n" , - i, info->hook_entry[i]); - goto out_unlock; - } -if (info->underflow[i] ==0xFFFFFFFF ) { -duprintf ("Invalid underflow %u %u\n" , - i, info->underflow[i]); - goto out_unlock; - } - } - ret = -ENOMEM ; newinfo =xt_alloc_table_info (size);if (!newinfo)
@@ -1413,51 +1375,24 @@ static int translate_compat_table(struct xt_entry_foreach(iter0, entry0, compatr->size)compat_copy_entry_from_user (iter0, &pos, &size, newinfo, entry1); + +/* all module references in entry0 are now gone */ +xt_compat_flush_offsets (NFPROTO_ARP );xt_compat_unlock (NFPROTO_ARP ); - ret = -ELOOP ; -if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1)) - goto free_newinfo; - - i =0 ; - xt_entry_foreach(iter1, entry1, newinfo->size) { - ret =check_target (iter1, compatr->name); -if (ret !=0 ) -break ; - ++i; -if (strcmp (arpt_get_target (iter1)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; - } -if (ret) { -/* -* The first i matches need cleanup_entry (calls ->destroy) -* because they had called ->check already. The other j-i -* entries need only release. -*/ -int skip = i; - j -= i; - xt_entry_foreach(iter0, entry0, newinfo->size) { -if (skip-- >0 ) -continue ; -if (j-- ==0 ) -break ; -compat_release_entry (iter0); - } - xt_entry_foreach(iter1, entry1, newinfo->size) { -if (i-- ==0 ) -break ; -cleanup_entry (iter1); - } -xt_free_table_info (newinfo); -return ret; +memcpy (&repl, compatr,sizeof (*compatr)); +for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { + repl.hook_entry[i] = newinfo->hook_entry[i]; + repl.underflow[i] = newinfo->underflow[i]; } -/* And one copy for every other CPU */ -for_each_possible_cpu (i) -if (newinfo->entries[i] && newinfo->entries[i] != entry1) -memcpy (newinfo->entries[i], entry1, newinfo->size); + repl.num_counters =0 ; + repl.counters =NULL ; + repl.size = newinfo->size; + ret =translate_table (newinfo, entry1, &repl); +if (ret) + goto free_newinfo; *pinfo = newinfo; *pentry0 = entry1;
@@ -1466,17 +1401,16 @@ static int translate_compat_table(struct free_newinfo:xt_free_table_info (newinfo); -out: +return ret; +out_unlock: +xt_compat_flush_offsets (NFPROTO_ARP ); +xt_compat_unlock (NFPROTO_ARP ); xt_entry_foreach(iter0, entry0, compatr->size) {if (j-- ==0 )break ;compat_release_entry (iter0); }return ret; -out_unlock: -xt_compat_flush_offsets (NFPROTO_ARP ); -xt_compat_unlock (NFPROTO_ARP ); - goto out; } static int compat_do_replace(struct net *net, void __user *user,
net/ipv4/netfilter/ip_tables.c +28 -123
235 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +22 -123
224 lines not on this page · raw message
net/netfilter/x_tables.c +8 -0
@@ -552,6 +552,7 @@ void xt_compat_match_from_user(struct xtstruct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;int pad, off =xt_compat_match_offset (match);u_int16_t msize = cm->u.user.match_size; +char name[sizeof (m->u.user.name)]; m = *dstptr;memcpy (m, cm,sizeof (*cm));
@@ -565,6 +566,9 @@ void xt_compat_match_from_user(struct xt msize += off; m->u.user.match_size = msize; +strlcpy (name, match->name,sizeof (name)); +module_put (match->me); +strncpy (m->u.user.name, name,sizeof (m->u.user.name)); *size += off; *dstptr += msize;
@@ -782,6 +786,7 @@ void xt_compat_target_from_user(struct xstruct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;int pad, off =xt_compat_target_offset (target);u_int16_t tsize = ct->u.user.target_size; +char name[sizeof (t->u.user.name)]; t = *dstptr;memcpy (t, ct,sizeof (*ct));
@@ -795,6 +800,9 @@ void xt_compat_target_from_user(struct x tsize += off; t->u.user.target_size = tsize; +strlcpy (name, target->name,sizeof (name)); +module_put (target->me); +strncpy (t->u.user.name, name,sizeof (t->u.user.name)); *size += off; *dstptr += tsize;
PATCH 303/305 netfilter: x_tables: introduce and use xt_copy_counters_from_user
In reply to Ben Hutchings3.16.37-rc1 review patch. If anyone has any objections, please let me know.
------------------
From: Florian Westphal <[email protected]>
commit d7591f0c41ce3e67600a982bab6989ef0f07b3ce upstream.
The three variants use same copy&pasted code, condense this into a helper and use that.
Make sure info.name is 0-terminated.
--- include/linux/netfilter/x_tables.h | 3 ++ net/ipv4/netfilter/arp_tables.c | 48 +++---------------------- net/ipv4/netfilter/ip_tables.c | 48 +++---------------------- net/ipv6/netfilter/ip6_tables.c | 49 +++---------------------- net/netfilter/x_tables.c | 74 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 130 deletions(-)
include/linux/netfilter/x_tables.h +3 -0
@@ -248,6 +248,9 @@ int xt_check_match(struct xt_mtchk_paramint xt_check_target (struct xt_tgchk_param *,unsigned int size,u_int8_t proto,bool inv_proto); +void *xt_copy_counters_from_user (const void __user *user,unsigned int len, +struct xt_counters_info *info,bool compat); + struct xt_table *xt_register_table(struct net *net, const struct xt_table *table, struct xt_table_info *bootstrap,
net/ipv4/netfilter/arp_tables.c +5 -43
68 lines not on this page · raw message
net/ipv4/netfilter/ip_tables.c +5 -43
68 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +5 -44
68 lines not on this page · raw message
net/netfilter/x_tables.c +74 -0
80 lines not on this page · raw message
PATCH 304/305 netfilter: ensure number of counters is >0 in do_replace()
In reply to Ben Hutchings3.16.37-rc1 review patch. If anyone has any objections, please let me know.
------------------
From: Dave Jones <[email protected]>
commit 1086bbe97a074844188c6c988fa0b1a98c3ccbb9 upstream.
After improving setsockopt() coverage in trinity, I started triggering vmalloc failures pretty reliably from this code path:
warn_alloc_failed+0xe9/0x140 __vmalloc_node_range+0x1be/0x270 vzalloc+0x4b/0x50 __do_replace+0x52/0x260 [ip_tables] do_ipt_set_ctl+0x15d/0x1d0 [ip_tables] nf_setsockopt+0x65/0x90 ip_setsockopt+0x61/0xa0 raw_setsockopt+0x16/0x60 sock_common_setsockopt+0x14/0x20 SyS_setsockopt+0x71/0xd0
It turns out we don't validate that the num_counters field in the struct we pass in from userspace is initialized.
The same problem also exists in ebtables, arptables, ipv6, and the compat variants.
--- net/bridge/netfilter/ebtables.c | 4 ++++ net/ipv4/netfilter/arp_tables.c | 6 ++++++ net/ipv4/netfilter/ip_tables.c | 6 ++++++ net/ipv6/netfilter/ip6_tables.c | 6 ++++++ 4 files changed, 22 insertions(+)
net/bridge/netfilter/ebtables.c +4 -0
@@ -1105,6 +1105,8 @@ static int do_replace(struct net *net, creturn -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct ebt_counter ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; tmp.name[sizeof (tmp.name) -1 ] =0 ;
@@ -2150,6 +2152,8 @@ static int compat_copy_ebt_replace_from_return -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct ebt_counter ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ;memcpy (repl, &tmp, offsetof(struct ebt_replace , hook_entry));
net/ipv4/netfilter/arp_tables.c +6 -0
@@ -1082,6 +1082,9 @@ static int do_replace(struct net *net, c/* overflow check */ if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
@@ -1392,6 +1395,9 @@ static int compat_do_replace(struct netreturn -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
net/ipv4/netfilter/ip_tables.c +6 -0
@@ -1268,6 +1268,9 @@ do_replace(struct net *net, const void _/* overflow check */ if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
@@ -1669,6 +1672,9 @@ compat_do_replace(struct net *net, voidreturn -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
net/ipv6/netfilter/ip6_tables.c +6 -0
@@ -1278,6 +1278,9 @@ do_replace(struct net *net, const void _/* overflow check */ if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
@@ -1672,6 +1675,9 @@ compat_do_replace(struct net *net, voidreturn -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ; +if (tmp.num_counters ==0 ) +return -EINVAL ; + tmp.name[sizeof (tmp.name)-1 ] =0 ; newinfo =xt_alloc_table_info (tmp.size);
Re: [PATCH 3.16 304/305] netfilter: ensure number of counters is >0 in do_replace()
Quoted 7 lines · Ben Hutchings
3.16.37-rc1 review patch. If anyone has any objections, please let me know.
------------------
From: Dave Jones <[email protected]>
commit
1086bbe97a074844188c6c988fa0b1a98c3ccbb9upstream.
Make sure you grab the follow-up patch in d26e2c9ffa385dd1b646f43c1397ba12af9ed431
Dave
Re: [PATCH 3.16 304/305] netfilter: ensure number of counters is >0 in do_replace()
Quoted 11 lines · Dave Jones
On Sat, Aug 13, 2016 at 06:42:51PM +0100, Ben Hutchings wrote:
> 3.16.37-rc1 review patch. If anyone has any objections, please let me know.
>
> ------------------
>
> From: Dave Jones <[email protected]>
>
> commit1086bbe97a074844188c6c988fa0b1a98c3ccbb9upstream.Make sure you grab the follow-up patch in
d26e2c9ffa385dd1b646f43c1397ba12af9ed431
Yes, that's the next in the series, thanks.
Ben.
Signature · 3 lines
Ben Hutchings Make three consecutive correct guesses and you will be considered an expert.
PATCH 305/305 Revert "netfilter: ensure number of counters is >0 in do_replace()"
In reply to Ben Hutchings3.16.37-rc1 review patch. If anyone has any objections, please let me know.
------------------
From: Bernhard Thaler <[email protected]>
commit d26e2c9ffa385dd1b646f43c1397ba12af9ed431 upstream.
This partially reverts commit 1086bbe97a07 ("netfilter: ensure number of counters is >0 in do_replace()") in net/bridge/netfilter/ebtables.c.
Setting rules with ebtables does not work any more with 1086bbe97a07 place.
There is an error message and no rules set in the end.
e.g.
~# ebtables -t nat -A POSTROUTING --src 12:34:56:78:9a:bc -j DROP Unable to update the kernel. Two possible causes:
1. Multiple ebtables programs were executing simultaneously. The ebtables userspace tool doesn't by default support multiple ebtables programs
running
Reverting the ebtables part of 1086bbe97a07 makes this work again.
--- net/bridge/netfilter/ebtables.c | 4 ---- 1 file changed, 4 deletions(-)
net/bridge/netfilter/ebtables.c +0 -4
@@ -1105,8 +1105,6 @@ static int do_replace(struct net *net, creturn -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct ebt_counter ))return -ENOMEM ; -if (tmp.num_counters ==0 ) -return -EINVAL ; tmp.name[sizeof (tmp.name) -1 ] =0 ;
@@ -2152,8 +2150,6 @@ static int compat_copy_ebt_replace_from_return -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct ebt_counter ))return -ENOMEM ; -if (tmp.num_counters ==0 ) -return -EINVAL ;memcpy (repl, &tmp, offsetof(struct ebt_replace , hook_entry));