Commit 76f0a94e authored by Patrick McHardy's avatar Patrick McHardy Committed by Chris Wright

[PATCH] NETFILTER: ip_tables: fix module refcount leaks in compat error paths

Based on patch by myself with additional fixes from Dmitry Mishin <dim@openvz.org>.
Signed-off-by: default avatarDmitry Mishin <dim@openvz.org>
Acked-by: default avatarVasily Averin <vvs@openvz.org>
Acked-by: default avatarKirill Korotaev <dev@openvz.org>
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarChris Wright <chrisw@sous-sol.org>
parent 5b9322ae
...@@ -1537,7 +1537,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e, ...@@ -1537,7 +1537,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
ret = IPT_MATCH_ITERATE(e, compat_check_calc_match, name, &e->ip, ret = IPT_MATCH_ITERATE(e, compat_check_calc_match, name, &e->ip,
e->comefrom, &off, &j); e->comefrom, &off, &j);
if (ret != 0) if (ret != 0)
goto out; goto cleanup_matches;
t = ipt_get_target(e); t = ipt_get_target(e);
target = try_then_request_module(xt_find_target(AF_INET, target = try_then_request_module(xt_find_target(AF_INET,
...@@ -1547,7 +1547,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e, ...@@ -1547,7 +1547,7 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
if (IS_ERR(target) || !target) { if (IS_ERR(target) || !target) {
duprintf("check_entry: `%s' not found\n", t->u.user.name); duprintf("check_entry: `%s' not found\n", t->u.user.name);
ret = target ? PTR_ERR(target) : -ENOENT; ret = target ? PTR_ERR(target) : -ENOENT;
goto out; goto cleanup_matches;
} }
t->u.kernel.target = target; t->u.kernel.target = target;
...@@ -1574,7 +1574,10 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e, ...@@ -1574,7 +1574,10 @@ check_compat_entry_size_and_hooks(struct ipt_entry *e,
(*i)++; (*i)++;
return 0; return 0;
out: out:
module_put(t->u.kernel.target->me);
cleanup_matches:
IPT_MATCH_ITERATE(e, cleanup_match, &j); IPT_MATCH_ITERATE(e, cleanup_match, &j);
return ret; return ret;
} }
...@@ -1597,18 +1600,16 @@ static inline int compat_copy_match_from_user(struct ipt_entry_match *m, ...@@ -1597,18 +1600,16 @@ static inline int compat_copy_match_from_user(struct ipt_entry_match *m,
ret = xt_check_match(match, AF_INET, dm->u.match_size - sizeof(*dm), ret = xt_check_match(match, AF_INET, dm->u.match_size - sizeof(*dm),
name, hookmask, ip->proto, name, hookmask, ip->proto,
ip->invflags & IPT_INV_PROTO); ip->invflags & IPT_INV_PROTO);
if (ret)
return ret;
if (m->u.kernel.match->checkentry if (!ret && m->u.kernel.match->checkentry
&& !m->u.kernel.match->checkentry(name, ip, match, dm->data, && !m->u.kernel.match->checkentry(name, ip, match, dm->data,
dm->u.match_size - sizeof(*dm), dm->u.match_size - sizeof(*dm),
hookmask)) { hookmask)) {
duprintf("ip_tables: check failed for `%s'.\n", duprintf("ip_tables: check failed for `%s'.\n",
m->u.kernel.match->name); m->u.kernel.match->name);
return -EINVAL; ret = -EINVAL;
} }
return 0; return ret;
} }
static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr, static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
...@@ -1630,7 +1631,7 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr, ...@@ -1630,7 +1631,7 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
ret = IPT_MATCH_ITERATE(e, compat_copy_match_from_user, dstptr, size, ret = IPT_MATCH_ITERATE(e, compat_copy_match_from_user, dstptr, size,
name, &de->ip, de->comefrom); name, &de->ip, de->comefrom);
if (ret) if (ret)
goto out; goto err;
de->target_offset = e->target_offset - (origsize - *size); de->target_offset = e->target_offset - (origsize - *size);
t = ipt_get_target(e); t = ipt_get_target(e);
target = t->u.kernel.target; target = t->u.kernel.target;
...@@ -1653,22 +1654,22 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr, ...@@ -1653,22 +1654,22 @@ static int compat_copy_entry_from_user(struct ipt_entry *e, void **dstptr,
name, e->comefrom, e->ip.proto, name, e->comefrom, e->ip.proto,
e->ip.invflags & IPT_INV_PROTO); e->ip.invflags & IPT_INV_PROTO);
if (ret) if (ret)
goto out; goto err;
ret = -EINVAL; ret = -EINVAL;
if (t->u.kernel.target == &ipt_standard_target) { if (t->u.kernel.target == &ipt_standard_target) {
if (!standard_check(t, *size)) if (!standard_check(t, *size))
goto out; goto err;
} else if (t->u.kernel.target->checkentry } else if (t->u.kernel.target->checkentry
&& !t->u.kernel.target->checkentry(name, de, target, && !t->u.kernel.target->checkentry(name, de, target,
t->data, t->u.target_size - sizeof(*t), t->data, t->u.target_size - sizeof(*t),
de->comefrom)) { de->comefrom)) {
duprintf("ip_tables: compat: check failed for `%s'.\n", duprintf("ip_tables: compat: check failed for `%s'.\n",
t->u.kernel.target->name); t->u.kernel.target->name);
goto out; goto err;
} }
ret = 0; ret = 0;
out: err:
return ret; return ret;
} }
...@@ -1682,7 +1683,7 @@ translate_compat_table(const char *name, ...@@ -1682,7 +1683,7 @@ translate_compat_table(const char *name,
unsigned int *hook_entries, unsigned int *hook_entries,
unsigned int *underflows) unsigned int *underflows)
{ {
unsigned int i; unsigned int i, j;
struct xt_table_info *newinfo, *info; struct xt_table_info *newinfo, *info;
void *pos, *entry0, *entry1; void *pos, *entry0, *entry1;
unsigned int size; unsigned int size;
...@@ -1700,21 +1701,21 @@ translate_compat_table(const char *name, ...@@ -1700,21 +1701,21 @@ translate_compat_table(const char *name,
} }
duprintf("translate_compat_table: size %u\n", info->size); duprintf("translate_compat_table: size %u\n", info->size);
i = 0; j = 0;
xt_compat_lock(AF_INET); xt_compat_lock(AF_INET);
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = IPT_ENTRY_ITERATE(entry0, total_size, ret = IPT_ENTRY_ITERATE(entry0, total_size,
check_compat_entry_size_and_hooks, check_compat_entry_size_and_hooks,
info, &size, entry0, info, &size, entry0,
entry0 + total_size, entry0 + total_size,
hook_entries, underflows, &i, name); hook_entries, underflows, &j, name);
if (ret != 0) if (ret != 0)
goto out_unlock; goto out_unlock;
ret = -EINVAL; ret = -EINVAL;
if (i != number) { if (j != number) {
duprintf("translate_compat_table: %u not %u entries\n", duprintf("translate_compat_table: %u not %u entries\n",
i, number); j, number);
goto out_unlock; goto out_unlock;
} }
...@@ -1773,6 +1774,7 @@ translate_compat_table(const char *name, ...@@ -1773,6 +1774,7 @@ translate_compat_table(const char *name,
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
out: out:
IPT_ENTRY_ITERATE(entry0, total_size, cleanup_entry, &j);
return ret; return ret;
out_unlock: out_unlock:
compat_flush_offsets(); compat_flush_offsets();
......
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