Skip to content

Commit 3647234

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: x_tables: validate targets of jumps
When we see a jump also check that the offset gets us to beginning of a rule (an ipt_entry). The extra overhead is negible, even with absurd cases. 300k custom rules, 300k jumps to 'next' user chain: [ plus one jump from INPUT to first userchain ]: Before: real 0m24.874s user 0m7.532s sys 0m16.076s After: real 0m27.464s user 0m7.436s sys 0m18.840s Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent f24e230 commit 3647234

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

net/ipv4/netfilter/arp_tables.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ static inline bool unconditional(const struct arpt_entry *e)
367367
memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
368368
}
369369

370+
static bool find_jump_target(const struct xt_table_info *t,
371+
const struct arpt_entry *target)
372+
{
373+
struct arpt_entry *iter;
374+
375+
xt_entry_foreach(iter, t->entries, t->size) {
376+
if (iter == target)
377+
return true;
378+
}
379+
return false;
380+
}
381+
370382
/* Figures out from what hook each rule can be called: returns 0 if
371383
* there are loops. Puts hook bitmask in comefrom.
372384
*/
@@ -460,6 +472,10 @@ static int mark_source_chains(const struct xt_table_info *newinfo,
460472
/* This a jump; chase it. */
461473
duprintf("Jump rule %u -> %u\n",
462474
pos, newpos);
475+
e = (struct arpt_entry *)
476+
(entry0 + newpos);
477+
if (!find_jump_target(newinfo, e))
478+
return 0;
463479
} else {
464480
/* ... this is a fallthru */
465481
newpos = pos + e->next_offset;

net/ipv4/netfilter/ip_tables.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,18 @@ ipt_do_table(struct sk_buff *skb,
443443
#endif
444444
}
445445

446+
static bool find_jump_target(const struct xt_table_info *t,
447+
const struct ipt_entry *target)
448+
{
449+
struct ipt_entry *iter;
450+
451+
xt_entry_foreach(iter, t->entries, t->size) {
452+
if (iter == target)
453+
return true;
454+
}
455+
return false;
456+
}
457+
446458
/* Figures out from what hook each rule can be called: returns 0 if
447459
there are loops. Puts hook bitmask in comefrom. */
448460
static int
@@ -540,6 +552,10 @@ mark_source_chains(const struct xt_table_info *newinfo,
540552
/* This a jump; chase it. */
541553
duprintf("Jump rule %u -> %u\n",
542554
pos, newpos);
555+
e = (struct ipt_entry *)
556+
(entry0 + newpos);
557+
if (!find_jump_target(newinfo, e))
558+
return 0;
543559
} else {
544560
/* ... this is a fallthru */
545561
newpos = pos + e->next_offset;

net/ipv6/netfilter/ip6_tables.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ ip6t_do_table(struct sk_buff *skb,
455455
#endif
456456
}
457457

458+
static bool find_jump_target(const struct xt_table_info *t,
459+
const struct ip6t_entry *target)
460+
{
461+
struct ip6t_entry *iter;
462+
463+
xt_entry_foreach(iter, t->entries, t->size) {
464+
if (iter == target)
465+
return true;
466+
}
467+
return false;
468+
}
469+
458470
/* Figures out from what hook each rule can be called: returns 0 if
459471
there are loops. Puts hook bitmask in comefrom. */
460472
static int
@@ -552,6 +564,10 @@ mark_source_chains(const struct xt_table_info *newinfo,
552564
/* This a jump; chase it. */
553565
duprintf("Jump rule %u -> %u\n",
554566
pos, newpos);
567+
e = (struct ip6t_entry *)
568+
(entry0 + newpos);
569+
if (!find_jump_target(newinfo, e))
570+
return 0;
555571
} else {
556572
/* ... this is a fallthru */
557573
newpos = pos + e->next_offset;

0 commit comments

Comments
 (0)