Commit 7a8251f7 authored by Florian Westphal's avatar Florian Westphal Committed by Kleber Sacilotto de Souza

netfilter: x_tables: speed up jump target validation

BugLink: http://bugs.launchpad.net/bugs/1754592

commit f4dc7771 upstream.

The dummy ruleset I used to test the original validation change was broken,
most rules were unreachable and were not tested by mark_source_chains().

In some cases rulesets that used to load in a few seconds now require
several minutes.

sample ruleset that shows the behaviour:

echo "*filter"
for i in $(seq 0 100000);do
        printf ":chain_%06x - [0:0]\n" $i
done
for i in $(seq 0 100000);do
   printf -- "-A INPUT -j chain_%06x\n" $i
   printf -- "-A INPUT -j chain_%06x\n" $i
   printf -- "-A INPUT -j chain_%06x\n" $i
done
echo COMMIT

[ pipe result into iptables-restore ]

This ruleset will be about 74mbyte in size, with ~500k searches
though all 500k[1] rule entries. iptables-restore will take forever
(gave up after 10 minutes)

Instead of always searching the entire blob for a match, fill an
array with the start offsets of every single ipt_entry struct,
then do a binary search to check if the jump target is present or not.

After this change ruleset restore times get again close to what one
gets when reverting 36472341 (~3 seconds on my workstation).

[1] every user-defined rule gets an implicit RETURN, so we get
300k jumps + 100k userchains + 100k returns -> 500k rule entries

Fixes: 36472341 ("netfilter: x_tables: validate targets of jumps")
Reported-by: default avatarJeff Wu <wujiafu@gmail.com>
Tested-by: default avatarJeff Wu <wujiafu@gmail.com>
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
Acked-by: default avatarMichal Kubecek <mkubecek@suse.cz>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarStefan Bader <stefan.bader@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent bde4c169
...@@ -243,6 +243,10 @@ int xt_check_entry_offsets(const void *base, const char *elems, ...@@ -243,6 +243,10 @@ int xt_check_entry_offsets(const void *base, const char *elems,
unsigned int target_offset, unsigned int target_offset,
unsigned int next_offset); unsigned int next_offset);
unsigned int *xt_alloc_entry_offsets(unsigned int size);
bool xt_find_jump_offset(const unsigned int *offsets,
unsigned int target, unsigned int size);
int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto, int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
bool inv_proto); bool inv_proto);
int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto, int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
......
...@@ -378,23 +378,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos) ...@@ -378,23 +378,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos)
return true; return true;
} }
static bool find_jump_target(const struct xt_table_info *t,
const struct arpt_entry *target)
{
struct arpt_entry *iter;
xt_entry_foreach(iter, t->entries, t->size) {
if (iter == target)
return true;
}
return false;
}
/* Figures out from what hook each rule can be called: returns 0 if /* Figures out from what hook each rule can be called: returns 0 if
* there are loops. Puts hook bitmask in comefrom. * there are loops. Puts hook bitmask in comefrom.
*/ */
static int mark_source_chains(const struct xt_table_info *newinfo, static int mark_source_chains(const struct xt_table_info *newinfo,
unsigned int valid_hooks, void *entry0) unsigned int valid_hooks, void *entry0,
unsigned int *offsets)
{ {
unsigned int hook; unsigned int hook;
...@@ -477,10 +466,11 @@ static int mark_source_chains(const struct xt_table_info *newinfo, ...@@ -477,10 +466,11 @@ static int mark_source_chains(const struct xt_table_info *newinfo,
/* This a jump; chase it. */ /* This a jump; chase it. */
duprintf("Jump rule %u -> %u\n", duprintf("Jump rule %u -> %u\n",
pos, newpos); pos, newpos);
if (!xt_find_jump_offset(offsets, newpos,
newinfo->number))
return 0;
e = (struct arpt_entry *) e = (struct arpt_entry *)
(entry0 + newpos); (entry0 + newpos);
if (!find_jump_target(newinfo, e))
return 0;
} else { } else {
/* ... this is a fallthru */ /* ... this is a fallthru */
newpos = pos + e->next_offset; newpos = pos + e->next_offset;
...@@ -652,6 +642,7 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -652,6 +642,7 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
{ {
struct xt_percpu_counter_alloc_state alloc_state = { 0 }; struct xt_percpu_counter_alloc_state alloc_state = { 0 };
struct arpt_entry *iter; struct arpt_entry *iter;
unsigned int *offsets;
unsigned int i; unsigned int i;
int ret = 0; int ret = 0;
...@@ -665,6 +656,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -665,6 +656,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
} }
duprintf("translate_table: size %u\n", newinfo->size); duprintf("translate_table: size %u\n", newinfo->size);
offsets = xt_alloc_entry_offsets(newinfo->number);
if (!offsets)
return -ENOMEM;
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
...@@ -675,7 +669,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -675,7 +669,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
repl->underflow, repl->underflow,
repl->valid_hooks); repl->valid_hooks);
if (ret != 0) if (ret != 0)
break; goto out_free;
if (i < repl->num_entries)
offsets[i] = (void *)iter - entry0;
++i; ++i;
if (strcmp(arpt_get_target(iter)->u.user.name, if (strcmp(arpt_get_target(iter)->u.user.name,
XT_ERROR_TARGET) == 0) XT_ERROR_TARGET) == 0)
...@@ -683,12 +679,13 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -683,12 +679,13 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
} }
duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret); duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
if (ret != 0) if (ret != 0)
return ret; goto out_free;
ret = -EINVAL;
if (i != repl->num_entries) { if (i != repl->num_entries) {
duprintf("translate_table: %u not %u entries\n", duprintf("translate_table: %u not %u entries\n",
i, repl->num_entries); i, repl->num_entries);
return -EINVAL; goto out_free;
} }
/* Check hooks all assigned */ /* Check hooks all assigned */
...@@ -699,17 +696,20 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -699,17 +696,20 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
if (newinfo->hook_entry[i] == 0xFFFFFFFF) { if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
duprintf("Invalid hook entry %u %u\n", duprintf("Invalid hook entry %u %u\n",
i, repl->hook_entry[i]); i, repl->hook_entry[i]);
return -EINVAL; goto out_free;
} }
if (newinfo->underflow[i] == 0xFFFFFFFF) { if (newinfo->underflow[i] == 0xFFFFFFFF) {
duprintf("Invalid underflow %u %u\n", duprintf("Invalid underflow %u %u\n",
i, repl->underflow[i]); i, repl->underflow[i]);
return -EINVAL; goto out_free;
} }
} }
if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
return -ELOOP; ret = -ELOOP;
goto out_free;
}
kvfree(offsets);
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
...@@ -730,6 +730,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, ...@@ -730,6 +730,9 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
return ret; return ret;
} }
return ret;
out_free:
kvfree(offsets);
return ret; return ret;
} }
......
...@@ -454,23 +454,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos) ...@@ -454,23 +454,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos)
return true; return true;
} }
static bool find_jump_target(const struct xt_table_info *t,
const struct ipt_entry *target)
{
struct ipt_entry *iter;
xt_entry_foreach(iter, t->entries, t->size) {
if (iter == target)
return true;
}
return false;
}
/* Figures out from what hook each rule can be called: returns 0 if /* Figures out from what hook each rule can be called: returns 0 if
there are loops. Puts hook bitmask in comefrom. */ there are loops. Puts hook bitmask in comefrom. */
static int static int
mark_source_chains(const struct xt_table_info *newinfo, mark_source_chains(const struct xt_table_info *newinfo,
unsigned int valid_hooks, void *entry0) unsigned int valid_hooks, void *entry0,
unsigned int *offsets)
{ {
unsigned int hook; unsigned int hook;
...@@ -558,10 +547,11 @@ mark_source_chains(const struct xt_table_info *newinfo, ...@@ -558,10 +547,11 @@ mark_source_chains(const struct xt_table_info *newinfo,
/* This a jump; chase it. */ /* This a jump; chase it. */
duprintf("Jump rule %u -> %u\n", duprintf("Jump rule %u -> %u\n",
pos, newpos); pos, newpos);
if (!xt_find_jump_offset(offsets, newpos,
newinfo->number))
return 0;
e = (struct ipt_entry *) e = (struct ipt_entry *)
(entry0 + newpos); (entry0 + newpos);
if (!find_jump_target(newinfo, e))
return 0;
} else { } else {
/* ... this is a fallthru */ /* ... this is a fallthru */
newpos = pos + e->next_offset; newpos = pos + e->next_offset;
...@@ -822,6 +812,7 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -822,6 +812,7 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
{ {
struct xt_percpu_counter_alloc_state alloc_state = { 0 }; struct xt_percpu_counter_alloc_state alloc_state = { 0 };
struct ipt_entry *iter; struct ipt_entry *iter;
unsigned int *offsets;
unsigned int i; unsigned int i;
int ret = 0; int ret = 0;
...@@ -835,6 +826,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -835,6 +826,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
} }
duprintf("translate_table: size %u\n", newinfo->size); duprintf("translate_table: size %u\n", newinfo->size);
offsets = xt_alloc_entry_offsets(newinfo->number);
if (!offsets)
return -ENOMEM;
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
xt_entry_foreach(iter, entry0, newinfo->size) { xt_entry_foreach(iter, entry0, newinfo->size) {
...@@ -844,17 +838,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -844,17 +838,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
repl->underflow, repl->underflow,
repl->valid_hooks); repl->valid_hooks);
if (ret != 0) if (ret != 0)
return ret; goto out_free;
if (i < repl->num_entries)
offsets[i] = (void *)iter - entry0;
++i; ++i;
if (strcmp(ipt_get_target(iter)->u.user.name, if (strcmp(ipt_get_target(iter)->u.user.name,
XT_ERROR_TARGET) == 0) XT_ERROR_TARGET) == 0)
++newinfo->stacksize; ++newinfo->stacksize;
} }
ret = -EINVAL;
if (i != repl->num_entries) { if (i != repl->num_entries) {
duprintf("translate_table: %u not %u entries\n", duprintf("translate_table: %u not %u entries\n",
i, repl->num_entries); i, repl->num_entries);
return -EINVAL; goto out_free;
} }
/* Check hooks all assigned */ /* Check hooks all assigned */
...@@ -865,17 +862,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -865,17 +862,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
if (newinfo->hook_entry[i] == 0xFFFFFFFF) { if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
duprintf("Invalid hook entry %u %u\n", duprintf("Invalid hook entry %u %u\n",
i, repl->hook_entry[i]); i, repl->hook_entry[i]);
return -EINVAL; goto out_free;
} }
if (newinfo->underflow[i] == 0xFFFFFFFF) { if (newinfo->underflow[i] == 0xFFFFFFFF) {
duprintf("Invalid underflow %u %u\n", duprintf("Invalid underflow %u %u\n",
i, repl->underflow[i]); i, repl->underflow[i]);
return -EINVAL; goto out_free;
} }
} }
if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
return -ELOOP; ret = -ELOOP;
goto out_free;
}
kvfree(offsets);
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
...@@ -896,6 +896,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -896,6 +896,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
return ret; return ret;
} }
return ret;
out_free:
kvfree(offsets);
return ret; return ret;
} }
......
...@@ -466,23 +466,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos) ...@@ -466,23 +466,12 @@ static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos)
return true; return true;
} }
static bool find_jump_target(const struct xt_table_info *t,
const struct ip6t_entry *target)
{
struct ip6t_entry *iter;
xt_entry_foreach(iter, t->entries, t->size) {
if (iter == target)
return true;
}
return false;
}
/* Figures out from what hook each rule can be called: returns 0 if /* Figures out from what hook each rule can be called: returns 0 if
there are loops. Puts hook bitmask in comefrom. */ there are loops. Puts hook bitmask in comefrom. */
static int static int
mark_source_chains(const struct xt_table_info *newinfo, mark_source_chains(const struct xt_table_info *newinfo,
unsigned int valid_hooks, void *entry0) unsigned int valid_hooks, void *entry0,
unsigned int *offsets)
{ {
unsigned int hook; unsigned int hook;
...@@ -570,10 +559,11 @@ mark_source_chains(const struct xt_table_info *newinfo, ...@@ -570,10 +559,11 @@ mark_source_chains(const struct xt_table_info *newinfo,
/* This a jump; chase it. */ /* This a jump; chase it. */
duprintf("Jump rule %u -> %u\n", duprintf("Jump rule %u -> %u\n",
pos, newpos); pos, newpos);
if (!xt_find_jump_offset(offsets, newpos,
newinfo->number))
return 0;
e = (struct ip6t_entry *) e = (struct ip6t_entry *)
(entry0 + newpos); (entry0 + newpos);
if (!find_jump_target(newinfo, e))
return 0;
} else { } else {
/* ... this is a fallthru */ /* ... this is a fallthru */
newpos = pos + e->next_offset; newpos = pos + e->next_offset;
...@@ -834,6 +824,7 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -834,6 +824,7 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
{ {
struct xt_percpu_counter_alloc_state alloc_state = { 0 }; struct xt_percpu_counter_alloc_state alloc_state = { 0 };
struct ip6t_entry *iter; struct ip6t_entry *iter;
unsigned int *offsets;
unsigned int i; unsigned int i;
int ret = 0; int ret = 0;
...@@ -847,6 +838,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -847,6 +838,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
} }
duprintf("translate_table: size %u\n", newinfo->size); duprintf("translate_table: size %u\n", newinfo->size);
offsets = xt_alloc_entry_offsets(newinfo->number);
if (!offsets)
return -ENOMEM;
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
xt_entry_foreach(iter, entry0, newinfo->size) { xt_entry_foreach(iter, entry0, newinfo->size) {
...@@ -856,17 +850,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -856,17 +850,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
repl->underflow, repl->underflow,
repl->valid_hooks); repl->valid_hooks);
if (ret != 0) if (ret != 0)
return ret; goto out_free;
if (i < repl->num_entries)
offsets[i] = (void *)iter - entry0;
++i; ++i;
if (strcmp(ip6t_get_target(iter)->u.user.name, if (strcmp(ip6t_get_target(iter)->u.user.name,
XT_ERROR_TARGET) == 0) XT_ERROR_TARGET) == 0)
++newinfo->stacksize; ++newinfo->stacksize;
} }
ret = -EINVAL;
if (i != repl->num_entries) { if (i != repl->num_entries) {
duprintf("translate_table: %u not %u entries\n", duprintf("translate_table: %u not %u entries\n",
i, repl->num_entries); i, repl->num_entries);
return -EINVAL; goto out_free;
} }
/* Check hooks all assigned */ /* Check hooks all assigned */
...@@ -877,17 +874,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -877,17 +874,20 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
if (newinfo->hook_entry[i] == 0xFFFFFFFF) { if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
duprintf("Invalid hook entry %u %u\n", duprintf("Invalid hook entry %u %u\n",
i, repl->hook_entry[i]); i, repl->hook_entry[i]);
return -EINVAL; goto out_free;
} }
if (newinfo->underflow[i] == 0xFFFFFFFF) { if (newinfo->underflow[i] == 0xFFFFFFFF) {
duprintf("Invalid underflow %u %u\n", duprintf("Invalid underflow %u %u\n",
i, repl->underflow[i]); i, repl->underflow[i]);
return -EINVAL; goto out_free;
} }
} }
if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
return -ELOOP; ret = -ELOOP;
goto out_free;
}
kvfree(offsets);
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
...@@ -908,6 +908,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, ...@@ -908,6 +908,9 @@ translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
return ret; return ret;
} }
return ret;
out_free:
kvfree(offsets);
return ret; return ret;
} }
......
...@@ -703,6 +703,56 @@ int xt_check_entry_offsets(const void *base, ...@@ -703,6 +703,56 @@ int xt_check_entry_offsets(const void *base,
} }
EXPORT_SYMBOL(xt_check_entry_offsets); EXPORT_SYMBOL(xt_check_entry_offsets);
/**
* xt_alloc_entry_offsets - allocate array to store rule head offsets
*
* @size: number of entries
*
* Return: NULL or kmalloc'd or vmalloc'd array
*/
unsigned int *xt_alloc_entry_offsets(unsigned int size)
{
unsigned int *off;
off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
if (off)
return off;
if (size < (SIZE_MAX / sizeof(unsigned int)))
off = vmalloc(size * sizeof(unsigned int));
return off;
}
EXPORT_SYMBOL(xt_alloc_entry_offsets);
/**
* xt_find_jump_offset - check if target is a valid jump offset
*
* @offsets: array containing all valid rule start offsets of a rule blob
* @target: the jump target to search for
* @size: entries in @offset
*/
bool xt_find_jump_offset(const unsigned int *offsets,
unsigned int target, unsigned int size)
{
int m, low = 0, hi = size;
while (hi > low) {
m = (low + hi) / 2u;
if (offsets[m] > target)
hi = m;
else if (offsets[m] < target)
low = m + 1;
else
return true;
}
return false;
}
EXPORT_SYMBOL(xt_find_jump_offset);
int xt_check_target(struct xt_tgchk_param *par, int xt_check_target(struct xt_tgchk_param *par,
unsigned int size, u_int8_t proto, bool inv_proto) unsigned int size, u_int8_t proto, bool inv_proto)
{ {
......
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