[PATCH net] Revert netfilter: ensure number of counters is 0 in do_replace()

2015-06-01 Thread Pablo Neira Ayuso
From: Bernhard Thaler bernhard.tha...@wvnet.at

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.

Signed-off-by: Bernhard Thaler bernhard.tha...@wvnet.at
Signed-off-by: Pablo Neira Ayuso pa...@netfilter.org
---
 net/bridge/netfilter/ebtables.c |4 
 1 file changed, 4 deletions(-)

diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
index 24c7c96..91180a7 100644
--- a/net/bridge/netfilter/ebtables.c
+++ b/net/bridge/netfilter/ebtables.c
@@ -1117,8 +1117,6 @@ static int do_replace(struct net *net, const void __user 
*user,
return -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;
 
@@ -2161,8 +2159,6 @@ static int compat_copy_ebt_replace_from_user(struct 
ebt_replace *repl,
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));
 
-- 
1.7.10.4

--
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/3] netfilter: ensure number of counters is 0 in do_replace()

2015-05-22 Thread Pablo Neira Ayuso
From: Dave Jones da...@codemonkey.org.uk

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.

Signed-off-by: Dave Jones da...@codemonkey.org.uk
Signed-off-by: Pablo Neira Ayuso pa...@netfilter.org
---
 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(+)

diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
index 91180a7..24c7c96 100644
--- a/net/bridge/netfilter/ebtables.c
+++ b/net/bridge/netfilter/ebtables.c
@@ -1117,6 +1117,8 @@ static int do_replace(struct net *net, const void __user 
*user,
return -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;
 
@@ -2159,6 +2161,8 @@ static int compat_copy_ebt_replace_from_user(struct 
ebt_replace *repl,
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));
 
diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index 13bfe84..a612007 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -1075,6 +1075,9 @@ static int do_replace(struct net *net, const void __user 
*user,
/* 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);
@@ -1499,6 +1502,9 @@ static int compat_do_replace(struct net *net, void __user 
*user,
return -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);
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index c69db7f..2d0e265 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -1262,6 +1262,9 @@ do_replace(struct net *net, const void __user *user, 
unsigned int len)
/* 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);
@@ -1809,6 +1812,9 @@ compat_do_replace(struct net *net, void __user *user, 
unsigned int len)
return -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);
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index 1a732a1..62f5b0d 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -1275,6 +1275,9 @@ do_replace(struct net *net, const void __user *user, 
unsigned int len)
/* 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);
@@ -1822,6 +1825,9 @@ compat_do_replace(struct net *net, void __user *user, 
unsigned int len)
return -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);
-- 
1.7.10.4

--
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: netfilter: ensure number of counters is 0 in do_replace()

2015-05-21 Thread Pablo Neira Ayuso
On Tue, May 19, 2015 at 08:55:17PM -0400, Dave Jones wrote:
 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.

Applied.

This also applies to -stable kernels:

3.2.x
3.4.x
3.10.x
3.12.x
3.14.x
3.18.x
4.0.x

so after some testing and a little while, I'll pass this on.

Thanks.
--
To unsubscribe from this list: send the line unsubscribe netdev in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html