CVE-2016-4997: Permissions, Privileges, and Access Controls (CWE-264)
value: The compat IPT_SO_SET_REPLACE and IP6T_SO_SET_REPLACE setsockopt implementations in the netfilter subsystem in the Linux kernel before 4.6.3 allow local users to gain privileges or cause a denial of service (memory corruption) by leveraging in-container root access to provide a crafted offset value that triggers an unintended decrement.
CWE description: Weaknesses in this category are related to the management of permissions, privileges, and other security features that are used to perform access control.
CVSS v3 base score: 7.8
Published: 2016-07-03T21:59Z
Repository: https://github.com/torvalds/linux
Language: C
Files changed: include/linux/netfilter/x_tables.h, net/ipv4/netfilter/arp_tables.c, net/ipv4/netfilter/ip_tables.c, net/ipv6/netfilter/ip6_tables.c, net/netfilter/x_tables.c
Commit date: 2016-04-14 00:30:37 +0200
Commit message: 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>

Diff: diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
index 08de48bbe92e3..30cfb1e943fb6 100644
--- a/include/linux/netfilter/x_tables.h
+++ b/include/linux/netfilter/x_tables.h
@@ -240,11 +240,11 @@ void xt_unregister_targets(struct xt_target *target, unsigned int n);
 int xt_register_match(struct xt_match *target);
 void xt_unregister_match(struct xt_match *target);
 int xt_register_matches(struct xt_match *match, unsigned int n);
 void xt_unregister_matches(struct xt_match *match, unsigned int n);
 
-int xt_check_entry_offsets(const void *base,
+int xt_check_entry_offsets(const void *base, const char *elems,
 			   unsigned int target_offset,
 			   unsigned int next_offset);
 
 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
 		   bool inv_proto);
@@ -492,11 +492,11 @@ int xt_compat_match_to_user(const struct xt_entry_match *m,
 int xt_compat_target_offset(const struct xt_target *target);
 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
 				unsigned int *size);
 int xt_compat_target_to_user(const struct xt_entry_target *t,
 			     void __user **dstptr, unsigned int *size);
-int xt_compat_check_entry_offsets(const void *base,
+int xt_compat_check_entry_offsets(const void *base, const char *elems,
 				  unsigned int target_offset,
 				  unsigned int next_offset);
 
 #endif /* CONFIG_COMPAT */
 #endif /* _X_TABLES_H */
diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index ab8952a49bfad..95ed4e454c605 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -590,11 +590,12 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,
 	}
 
 	if (!arp_checkentry(&e->arp))
 		return -EINVAL;
 
-	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
+	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
+				     e->next_offset);
 	if (err)
 		return err;
 
 	/* Check hooks & underflows */
 	for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
@@ -1252,11 +1253,11 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
 	}
 
 	if (!arp_checkentry(&e->arp))
 		return -EINVAL;
 
-	ret = xt_compat_check_entry_offsets(e, e->target_offset,
+	ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
 					    e->next_offset);
 	if (ret)
 		return ret;
 
 	off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index 7d24c872723fd..baab033d74e00 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -752,11 +752,12 @@ check_entry_size_and_hooks(struct ipt_entry *e,
 	}
 
 	if (!ip_checkentry(&e->ip))
 		return -EINVAL;
 
-	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
+	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
+				     e->next_offset);
 	if (err)
 		return err;
 
 	/* Check hooks & underflows */
 	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
@@ -1511,11 +1512,11 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
 	}
 
 	if (!ip_checkentry(&e->ip))
 		return -EINVAL;
 
-	ret = xt_compat_check_entry_offsets(e,
+	ret = xt_compat_check_entry_offsets(e, e->elems,
 					    e->target_offset, e->next_offset);
 	if (ret)
 		return ret;
 
 	off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index 73eee7b5fd60e..6957627c79311 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -764,11 +764,12 @@ check_entry_size_and_hooks(struct ip6t_entry *e,
 	}
 
 	if (!ip6_checkentry(&e->ipv6))
 		return -EINVAL;
 
-	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
+	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
+				     e->next_offset);
 	if (err)
 		return err;
 
 	/* Check hooks & underflows */
 	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
@@ -1523,11 +1524,11 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
 	}
 
 	if (!ip6_checkentry(&e->ipv6))
 		return -EINVAL;
 
-	ret = xt_compat_check_entry_offsets(e,
+	ret = xt_compat_check_entry_offsets(e, e->elems,
 					    e->target_offset, e->next_offset);
 	if (ret)
 		return ret;
 
 	off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
diff --git a/net/netfilter/x_tables.c b/net/netfilter/x_tables.c
index 1cb7a271c0245..e2a6f2a9051b7 100644
--- a/net/netfilter/x_tables.c
+++ b/net/netfilter/x_tables.c
@@ -544,18 +544,21 @@ EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
 struct compat_xt_standard_target {
 	struct compat_xt_entry_target t;
 	compat_uint_t verdict;
 };
 
-/* see xt_check_entry_offsets */
-int xt_compat_check_entry_offsets(const void *base,
+int xt_compat_check_entry_offsets(const void *base, const char *elems,
 				  unsigned int target_offset,
 				  unsigned int next_offset)
 {
+	long size_of_base_struct = elems - (const char *)base;
 	const struct compat_xt_entry_target *t;
 	const char *e = base;
 
+	if (target_offset < size_of_base_struct)
+		return -EINVAL;
+
 	if (target_offset + sizeof(*t) > next_offset)
 		return -EINVAL;
 
 	t = (void *)(e + target_offset);
 	if (t->u.target_size < sizeof(*t))
@@ -575,30 +578,40 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);
 
 /**
  * xt_check_entry_offsets - validate arp/ip/ip6t_entry
  *
  * @base: pointer to arp/ip/ip6t_entry
+ * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
  * @target_offset: the arp/ip/ip6_t->target_offset
  * @next_offset: the arp/ip/ip6_t->next_offset
  *
  * validates that target_offset and next_offset are sane.
  * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
  *
+ * This function does not validate the targets or matches themselves, it
+ * only tests that all the offsets and sizes are correct.
+ *
  * The arp/ip/ip6t_entry structure @base must have passed following tests:
  * - it must point to a valid memory location
  * - base to base + next_offset must be accessible, i.e. not exceed allocated
  *   length.
  *
  * Return: 0 on success, negative errno on failure.
  */
 int xt_check_entry_offsets(const void *base,
+			   const char *elems,
 			   unsigned int target_offset,
 			   unsigned int next_offset)
 {
+	long size_of_base_struct = elems - (const char *)base;
 	const struct xt_entry_target *t;
 	const char *e = base;
 
+	/* target start is within the ip/ip6/arpt_entry struct */
+	if (target_offset < size_of_base_struct)
+		return -EINVAL;
+
 	if (target_offset + sizeof(*t) > next_offset)
 		return -EINVAL;
 
 	t = (void *)(e + target_offset);
 	if (t->u.target_size < sizeof(*t))

Vulnerable code: int xt_check_entry_offsets(const void *base,
int xt_compat_check_entry_offsets(const void *base,
	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
	ret = xt_compat_check_entry_offsets(e, e->target_offset,
	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
	ret = xt_compat_check_entry_offsets(e,
	err = xt_check_entry_offsets(e, e->target_offset, e->next_offset);
	ret = xt_compat_check_entry_offsets(e,
/* see xt_check_entry_offsets */
int xt_compat_check_entry_offsets(const void *base,
Fixed code: int xt_check_entry_offsets(const void *base, const char *elems,
int xt_compat_check_entry_offsets(const void *base, const char *elems,
	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
				     e->next_offset);
	ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
				     e->next_offset);
	ret = xt_compat_check_entry_offsets(e, e->elems,
	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
				     e->next_offset);
	ret = xt_compat_check_entry_offsets(e, e->elems,
int xt_compat_check_entry_offsets(const void *base, const char *elems,
	long size_of_base_struct = elems - (const char *)base;
	if (target_offset < size_of_base_struct)
		return -EINVAL;

 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
 * This function does not validate the targets or matches themselves, it
 * only tests that all the offsets and sizes are correct.
 *
			   const char *elems,
	long size_of_base_struct = elems - (const char *)base;
	/* target start is within the ip/ip6/arpt_entry struct */
	if (target_offset < size_of_base_struct)
		return -EINVAL;