Commit c80600e6 authored by Florian Westphal's avatar Florian Westphal Committed by Brad Figg

netfilter: x_tables: validate e->target_offset early

We should check that e->target_offset is sane before
mark_source_chains gets called since it will fetch the target entry
for loop detection.
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
CVE-2016-3134
(cherry picked from commit bdf533de)
BugLink: https://bugs.launchpad.net/bugs/1555338Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
Acked-by: default avatarTim Gardner <tim.gardner@canonical.com>
Signed-off-by: default avatarBrad Figg <brad.figg@canonical.com>
parent 6223a2a2
...@@ -483,14 +483,12 @@ static int mark_source_chains(const struct xt_table_info *newinfo, ...@@ -483,14 +483,12 @@ static int mark_source_chains(const struct xt_table_info *newinfo,
return 1; return 1;
} }
static inline int check_entry(const struct arpt_entry *e, const char *name) static inline int check_entry(const struct arpt_entry *e)
{ {
const struct xt_entry_target *t; const struct xt_entry_target *t;
if (!arp_checkentry(&e->arp)) { if (!arp_checkentry(&e->arp))
duprintf("arp_tables: arp check failed %p %s.\n", e, name);
return -EINVAL; return -EINVAL;
}
if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset) if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
return -EINVAL; return -EINVAL;
...@@ -531,10 +529,6 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size) ...@@ -531,10 +529,6 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
struct xt_target *target; struct xt_target *target;
int ret; int ret;
ret = check_entry(e, name);
if (ret)
return ret;
e->counters.pcnt = xt_percpu_counter_alloc(); e->counters.pcnt = xt_percpu_counter_alloc();
if (IS_ERR_VALUE(e->counters.pcnt)) if (IS_ERR_VALUE(e->counters.pcnt))
return -ENOMEM; return -ENOMEM;
...@@ -585,6 +579,7 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e, ...@@ -585,6 +579,7 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,
unsigned int valid_hooks) unsigned int valid_hooks)
{ {
unsigned int h; unsigned int h;
int err;
if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 || if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
(unsigned char *)e + sizeof(struct arpt_entry) >= limit) { (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
...@@ -599,6 +594,10 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e, ...@@ -599,6 +594,10 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,
return -EINVAL; return -EINVAL;
} }
err = check_entry(e);
if (err)
return err;
/* Check hooks & underflows */ /* Check hooks & underflows */
for (h = 0; h < NF_ARP_NUMHOOKS; h++) { for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
if (!(valid_hooks & (1 << h))) if (!(valid_hooks & (1 << h)))
...@@ -1255,7 +1254,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e, ...@@ -1255,7 +1254,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
} }
/* For purposes of check_entry casting the compat entry is fine */ /* For purposes of check_entry casting the compat entry is fine */
ret = check_entry((struct arpt_entry *)e, name); ret = check_entry((struct arpt_entry *)e);
if (ret) if (ret)
return ret; return ret;
......
...@@ -579,14 +579,12 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net) ...@@ -579,14 +579,12 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)
} }
static int static int
check_entry(const struct ipt_entry *e, const char *name) check_entry(const struct ipt_entry *e)
{ {
const struct xt_entry_target *t; const struct xt_entry_target *t;
if (!ip_checkentry(&e->ip)) { if (!ip_checkentry(&e->ip))
duprintf("ip check failed %p %s.\n", e, name);
return -EINVAL; return -EINVAL;
}
if (e->target_offset + sizeof(struct xt_entry_target) > if (e->target_offset + sizeof(struct xt_entry_target) >
e->next_offset) e->next_offset)
...@@ -676,10 +674,6 @@ find_check_entry(struct ipt_entry *e, struct net *net, const char *name, ...@@ -676,10 +674,6 @@ find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
struct xt_mtchk_param mtpar; struct xt_mtchk_param mtpar;
struct xt_entry_match *ematch; struct xt_entry_match *ematch;
ret = check_entry(e, name);
if (ret)
return ret;
e->counters.pcnt = xt_percpu_counter_alloc(); e->counters.pcnt = xt_percpu_counter_alloc();
if (IS_ERR_VALUE(e->counters.pcnt)) if (IS_ERR_VALUE(e->counters.pcnt))
return -ENOMEM; return -ENOMEM;
...@@ -751,6 +745,7 @@ check_entry_size_and_hooks(struct ipt_entry *e, ...@@ -751,6 +745,7 @@ check_entry_size_and_hooks(struct ipt_entry *e,
unsigned int valid_hooks) unsigned int valid_hooks)
{ {
unsigned int h; unsigned int h;
int err;
if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 || if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
(unsigned char *)e + sizeof(struct ipt_entry) >= limit) { (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
...@@ -765,6 +760,10 @@ check_entry_size_and_hooks(struct ipt_entry *e, ...@@ -765,6 +760,10 @@ check_entry_size_and_hooks(struct ipt_entry *e,
return -EINVAL; return -EINVAL;
} }
err = check_entry(e);
if (err)
return err;
/* Check hooks & underflows */ /* Check hooks & underflows */
for (h = 0; h < NF_INET_NUMHOOKS; h++) { for (h = 0; h < NF_INET_NUMHOOKS; h++) {
if (!(valid_hooks & (1 << h))) if (!(valid_hooks & (1 << h)))
...@@ -1516,7 +1515,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e, ...@@ -1516,7 +1515,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
} }
/* For purposes of check_entry casting the compat entry is fine */ /* For purposes of check_entry casting the compat entry is fine */
ret = check_entry((struct ipt_entry *)e, name); ret = check_entry((struct ipt_entry *)e);
if (ret) if (ret)
return ret; return ret;
......
...@@ -591,14 +591,12 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net) ...@@ -591,14 +591,12 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)
} }
static int static int
check_entry(const struct ip6t_entry *e, const char *name) check_entry(const struct ip6t_entry *e)
{ {
const struct xt_entry_target *t; const struct xt_entry_target *t;
if (!ip6_checkentry(&e->ipv6)) { if (!ip6_checkentry(&e->ipv6))
duprintf("ip_tables: ip check failed %p %s.\n", e, name);
return -EINVAL; return -EINVAL;
}
if (e->target_offset + sizeof(struct xt_entry_target) > if (e->target_offset + sizeof(struct xt_entry_target) >
e->next_offset) e->next_offset)
...@@ -689,10 +687,6 @@ find_check_entry(struct ip6t_entry *e, struct net *net, const char *name, ...@@ -689,10 +687,6 @@ find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
struct xt_mtchk_param mtpar; struct xt_mtchk_param mtpar;
struct xt_entry_match *ematch; struct xt_entry_match *ematch;
ret = check_entry(e, name);
if (ret)
return ret;
e->counters.pcnt = xt_percpu_counter_alloc(); e->counters.pcnt = xt_percpu_counter_alloc();
if (IS_ERR_VALUE(e->counters.pcnt)) if (IS_ERR_VALUE(e->counters.pcnt))
return -ENOMEM; return -ENOMEM;
...@@ -763,6 +757,7 @@ check_entry_size_and_hooks(struct ip6t_entry *e, ...@@ -763,6 +757,7 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
unsigned int valid_hooks) unsigned int valid_hooks)
{ {
unsigned int h; unsigned int h;
int err;
if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 || if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
(unsigned char *)e + sizeof(struct ip6t_entry) >= limit) { (unsigned char *)e + sizeof(struct ip6t_entry) >= limit) {
...@@ -777,6 +772,10 @@ check_entry_size_and_hooks(struct ip6t_entry *e, ...@@ -777,6 +772,10 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
return -EINVAL; return -EINVAL;
} }
err = check_entry(e);
if (err)
return err;
/* Check hooks & underflows */ /* Check hooks & underflows */
for (h = 0; h < NF_INET_NUMHOOKS; h++) { for (h = 0; h < NF_INET_NUMHOOKS; h++) {
if (!(valid_hooks & (1 << h))) if (!(valid_hooks & (1 << h)))
...@@ -1528,7 +1527,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e, ...@@ -1528,7 +1527,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
} }
/* For purposes of check_entry casting the compat entry is fine */ /* For purposes of check_entry casting the compat entry is fine */
ret = check_entry((struct ip6t_entry *)e, name); ret = check_entry((struct ip6t_entry *)e);
if (ret) if (ret)
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment