Skip to content

Commit cfdca13

Browse files
Florian Westphalgregkh
authored andcommitted
netfilter: x_tables: add and use xt_check_entry_offsets
commit 7d35812c3214afa5b37a675113555259cfd67b98 upstream. Currently arp/ip and ip6tables each implement a short helper to check that the target offset is large enough to hold one xt_entry_target struct and that t->u.target_size fits within the current rule. Unfortunately these checks are not sufficient. To avoid adding new tests to all of ip/ip6/arptables move the current checks into a helper, then extend this helper in followup patches. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 611d408 commit cfdca13

5 files changed

Lines changed: 41 additions & 32 deletions

File tree

include/linux/netfilter/x_tables.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ void xt_unregister_match(struct xt_match *target);
239239
int xt_register_matches(struct xt_match *match, unsigned int n);
240240
void xt_unregister_matches(struct xt_match *match, unsigned int n);
241241

242+
int xt_check_entry_offsets(const void *base,
243+
unsigned int target_offset,
244+
unsigned int next_offset);
245+
242246
int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
243247
bool inv_proto);
244248
int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,

net/ipv4/netfilter/arp_tables.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -496,19 +496,10 @@ static int mark_source_chains(const struct xt_table_info *newinfo,
496496

497497
static inline int check_entry(const struct arpt_entry *e)
498498
{
499-
const struct xt_entry_target *t;
500-
501499
if (!arp_checkentry(&e->arp))
502500
return -EINVAL;
503501

504-
if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
505-
return -EINVAL;
506-
507-
t = arpt_get_target_c(e);
508-
if (e->target_offset + t->u.target_size > e->next_offset)
509-
return -EINVAL;
510-
511-
return 0;
502+
return xt_check_entry_offsets(e, e->target_offset, e->next_offset);
512503
}
513504

514505
static inline int check_target(struct arpt_entry *e, const char *name)

net/ipv4/netfilter/ip_tables.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -590,20 +590,10 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)
590590
static int
591591
check_entry(const struct ipt_entry *e)
592592
{
593-
const struct xt_entry_target *t;
594-
595593
if (!ip_checkentry(&e->ip))
596594
return -EINVAL;
597595

598-
if (e->target_offset + sizeof(struct xt_entry_target) >
599-
e->next_offset)
600-
return -EINVAL;
601-
602-
t = ipt_get_target_c(e);
603-
if (e->target_offset + t->u.target_size > e->next_offset)
604-
return -EINVAL;
605-
606-
return 0;
596+
return xt_check_entry_offsets(e, e->target_offset, e->next_offset);
607597
}
608598

609599
static int

net/ipv6/netfilter/ip6_tables.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -602,20 +602,10 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)
602602
static int
603603
check_entry(const struct ip6t_entry *e)
604604
{
605-
const struct xt_entry_target *t;
606-
607605
if (!ip6_checkentry(&e->ipv6))
608606
return -EINVAL;
609607

610-
if (e->target_offset + sizeof(struct xt_entry_target) >
611-
e->next_offset)
612-
return -EINVAL;
613-
614-
t = ip6t_get_target_c(e);
615-
if (e->target_offset + t->u.target_size > e->next_offset)
616-
return -EINVAL;
617-
618-
return 0;
608+
return xt_check_entry_offsets(e, e->target_offset, e->next_offset);
619609
}
620610

621611
static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)

net/netfilter/x_tables.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,40 @@ int xt_compat_match_to_user(const struct xt_entry_match *m,
540540
EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
541541
#endif /* CONFIG_COMPAT */
542542

543+
/**
544+
* xt_check_entry_offsets - validate arp/ip/ip6t_entry
545+
*
546+
* @base: pointer to arp/ip/ip6t_entry
547+
* @target_offset: the arp/ip/ip6_t->target_offset
548+
* @next_offset: the arp/ip/ip6_t->next_offset
549+
*
550+
* validates that target_offset and next_offset are sane.
551+
*
552+
* The arp/ip/ip6t_entry structure @base must have passed following tests:
553+
* - it must point to a valid memory location
554+
* - base to base + next_offset must be accessible, i.e. not exceed allocated
555+
* length.
556+
*
557+
* Return: 0 on success, negative errno on failure.
558+
*/
559+
int xt_check_entry_offsets(const void *base,
560+
unsigned int target_offset,
561+
unsigned int next_offset)
562+
{
563+
const struct xt_entry_target *t;
564+
const char *e = base;
565+
566+
if (target_offset + sizeof(*t) > next_offset)
567+
return -EINVAL;
568+
569+
t = (void *)(e + target_offset);
570+
if (target_offset + t->u.target_size > next_offset)
571+
return -EINVAL;
572+
573+
return 0;
574+
}
575+
EXPORT_SYMBOL(xt_check_entry_offsets);
576+
543577
int xt_check_target(struct xt_tgchk_param *par,
544578
unsigned int size, u_int8_t proto, bool inv_proto)
545579
{

0 commit comments

Comments
 (0)