Skip to content

Commit ce683e5

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: x_tables: check for bogus target offset
We're currently asserting that targetoff + targetsize <= nextoff. Extend it to also check that targetoff is >= sizeof(xt_entry). Since this is generic code, add an argument pointing to the start of the match/target, we can then derive the base structure size from the delta. We also need the e->elems pointer in a followup change to validate matches. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 7ed2abd commit ce683e5

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void xt_unregister_match(struct xt_match *target);
242242
int xt_register_matches(struct xt_match *match, unsigned int n);
243243
void xt_unregister_matches(struct xt_match *match, unsigned int n);
244244

245-
int xt_check_entry_offsets(const void *base,
245+
int xt_check_entry_offsets(const void *base, const char *elems,
246246
unsigned int target_offset,
247247
unsigned int next_offset);
248248

@@ -494,7 +494,7 @@ void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
494494
unsigned int *size);
495495
int xt_compat_target_to_user(const struct xt_entry_target *t,
496496
void __user **dstptr, unsigned int *size);
497-
int xt_compat_check_entry_offsets(const void *base,
497+
int xt_compat_check_entry_offsets(const void *base, const char *elems,
498498
unsigned int target_offset,
499499
unsigned int next_offset);
500500

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,
592592
if (!arp_checkentry(&e->arp))
593593
return -EINVAL;
594594

595-
err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
595+
err = xt_check_entry_offsets(e, e->elems, e->target_offset,
596+
e->next_offset);
596597
if (err)
597598
return err;
598599

@@ -1254,7 +1255,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
12541255
if (!arp_checkentry(&e->arp))
12551256
return -EINVAL;
12561257

1257-
ret = xt_compat_check_entry_offsets(e, e->target_offset,
1258+
ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
12581259
e->next_offset);
12591260
if (ret)
12601261
return ret;

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ check_entry_size_and_hooks(struct ipt_entry *e,
754754
if (!ip_checkentry(&e->ip))
755755
return -EINVAL;
756756

757-
err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
757+
err = xt_check_entry_offsets(e, e->elems, e->target_offset,
758+
e->next_offset);
758759
if (err)
759760
return err;
760761

@@ -1513,7 +1514,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
15131514
if (!ip_checkentry(&e->ip))
15141515
return -EINVAL;
15151516

1516-
ret = xt_compat_check_entry_offsets(e,
1517+
ret = xt_compat_check_entry_offsets(e, e->elems,
15171518
e->target_offset, e->next_offset);
15181519
if (ret)
15191520
return ret;

‎net/ipv6/netfilter/ip6_tables.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
766766
if (!ip6_checkentry(&e->ipv6))
767767
return -EINVAL;
768768

769-
err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
769+
err = xt_check_entry_offsets(e, e->elems, e->target_offset,
770+
e->next_offset);
770771
if (err)
771772
return err;
772773

@@ -1525,7 +1526,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
15251526
if (!ip6_checkentry(&e->ipv6))
15261527
return -EINVAL;
15271528

1528-
ret = xt_compat_check_entry_offsets(e,
1529+
ret = xt_compat_check_entry_offsets(e, e->elems,
15291530
e->target_offset, e->next_offset);
15301531
if (ret)
15311532
return ret;

‎net/netfilter/x_tables.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,14 +546,17 @@ struct compat_xt_standard_target {
546546
compat_uint_t verdict;
547547
};
548548

549-
/* see xt_check_entry_offsets */
550-
int xt_compat_check_entry_offsets(const void *base,
549+
int xt_compat_check_entry_offsets(const void *base, const char *elems,
551550
unsigned int target_offset,
552551
unsigned int next_offset)
553552
{
553+
long size_of_base_struct = elems - (const char *)base;
554554
const struct compat_xt_entry_target *t;
555555
const char *e = base;
556556

557+
if (target_offset < size_of_base_struct)
558+
return -EINVAL;
559+
557560
if (target_offset + sizeof(*t) > next_offset)
558561
return -EINVAL;
559562

@@ -577,12 +580,16 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);
577580
* xt_check_entry_offsets - validate arp/ip/ip6t_entry
578581
*
579582
* @base: pointer to arp/ip/ip6t_entry
583+
* @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
580584
* @target_offset: the arp/ip/ip6_t->target_offset
581585
* @next_offset: the arp/ip/ip6_t->next_offset
582586
*
583587
* validates that target_offset and next_offset are sane.
584588
* Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
585589
*
590+
* This function does not validate the targets or matches themselves, it
591+
* only tests that all the offsets and sizes are correct.
592+
*
586593
* The arp/ip/ip6t_entry structure @base must have passed following tests:
587594
* - it must point to a valid memory location
588595
* - base to base + next_offset must be accessible, i.e. not exceed allocated
@@ -591,12 +598,18 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);
591598
* Return: 0 on success, negative errno on failure.
592599
*/
593600
int xt_check_entry_offsets(const void *base,
601+
const char *elems,
594602
unsigned int target_offset,
595603
unsigned int next_offset)
596604
{
605+
long size_of_base_struct = elems - (const char *)base;
597606
const struct xt_entry_target *t;
598607
const char *e = base;
599608

609+
/* target start is within the ip/ip6/arpt_entry struct */
610+
if (target_offset < size_of_base_struct)
611+
return -EINVAL;
612+
600613
if (target_offset + sizeof(*t) > next_offset)
601614
return -EINVAL;
602615

0 commit comments

Comments
 (0)