Skip to content

Commit 1086bbe

Browse files
kernelslackerummakynes
authored andcommitted
netfilter: ensure number of counters is >0 in do_replace()
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 <davej@codemonkey.org.uk> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 3bfe049 commit 1086bbe

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

net/bridge/netfilter/ebtables.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,8 @@ static int do_replace(struct net *net, const void __user *user,
11171117
return -ENOMEM;
11181118
if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
11191119
return -ENOMEM;
1120+
if (tmp.num_counters == 0)
1121+
return -EINVAL;
11201122

11211123
tmp.name[sizeof(tmp.name) - 1] = 0;
11221124

@@ -2159,6 +2161,8 @@ static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
21592161
return -ENOMEM;
21602162
if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
21612163
return -ENOMEM;
2164+
if (tmp.num_counters == 0)
2165+
return -EINVAL;
21622166

21632167
memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
21642168

net/ipv4/netfilter/arp_tables.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,9 @@ static int do_replace(struct net *net, const void __user *user,
10751075
/* overflow check */
10761076
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
10771077
return -ENOMEM;
1078+
if (tmp.num_counters == 0)
1079+
return -EINVAL;
1080+
10781081
tmp.name[sizeof(tmp.name)-1] = 0;
10791082

10801083
newinfo = xt_alloc_table_info(tmp.size);
@@ -1499,6 +1502,9 @@ static int compat_do_replace(struct net *net, void __user *user,
14991502
return -ENOMEM;
15001503
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
15011504
return -ENOMEM;
1505+
if (tmp.num_counters == 0)
1506+
return -EINVAL;
1507+
15021508
tmp.name[sizeof(tmp.name)-1] = 0;
15031509

15041510
newinfo = xt_alloc_table_info(tmp.size);

net/ipv4/netfilter/ip_tables.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
12621262
/* overflow check */
12631263
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
12641264
return -ENOMEM;
1265+
if (tmp.num_counters == 0)
1266+
return -EINVAL;
1267+
12651268
tmp.name[sizeof(tmp.name)-1] = 0;
12661269

12671270
newinfo = xt_alloc_table_info(tmp.size);
@@ -1809,6 +1812,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
18091812
return -ENOMEM;
18101813
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
18111814
return -ENOMEM;
1815+
if (tmp.num_counters == 0)
1816+
return -EINVAL;
1817+
18121818
tmp.name[sizeof(tmp.name)-1] = 0;
18131819

18141820
newinfo = xt_alloc_table_info(tmp.size);

net/ipv6/netfilter/ip6_tables.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
12751275
/* overflow check */
12761276
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
12771277
return -ENOMEM;
1278+
if (tmp.num_counters == 0)
1279+
return -EINVAL;
1280+
12781281
tmp.name[sizeof(tmp.name)-1] = 0;
12791282

12801283
newinfo = xt_alloc_table_info(tmp.size);
@@ -1822,6 +1825,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
18221825
return -ENOMEM;
18231826
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
18241827
return -ENOMEM;
1828+
if (tmp.num_counters == 0)
1829+
return -EINVAL;
1830+
18251831
tmp.name[sizeof(tmp.name)-1] = 0;
18261832

18271833
newinfo = xt_alloc_table_info(tmp.size);

0 commit comments

Comments
 (0)