netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH nf 09/17] netfilter: x_tables: validate all offsets and sizes in a rule
Date: Fri,  1 Apr 2016 14:17:29 +0200	[thread overview]
Message-ID: <1459513057-30652-10-git-send-email-fw@strlen.de> (raw)
In-Reply-To: <1459513057-30652-1-git-send-email-fw@strlen.de>

Validate that all matches (if any) add up to the beginning of
the target and that each match covers at least the base structure size.

The compat path should be able to safely re-use the function
as the structures only differ in alignment; added a
BUILD_BUG_ON just in case we have an arch that adds padding as well.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 net/netfilter/x_tables.c | 81 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 76 insertions(+), 5 deletions(-)

diff --git a/net/netfilter/x_tables.c b/net/netfilter/x_tables.c
index e2a6f2a..f9aa971 100644
--- a/net/netfilter/x_tables.c
+++ b/net/netfilter/x_tables.c
@@ -416,6 +416,47 @@ int xt_check_match(struct xt_mtchk_param *par,
 }
 EXPORT_SYMBOL_GPL(xt_check_match);
 
+/** xt_check_entry_match - check that matches end before start of target
+ *
+ * @match: beginning of xt_entry_match
+ * @target: beginning of this rules target (alleged end of matches)
+ * @alignment: alignment requirement of match structures
+ *
+ * Validates that all matches add up to the beginning of the target,
+ * and that each match covers at least the base structure size.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int xt_check_entry_match(const char *match, const char *target,
+				const size_t alignment)
+{
+	const struct xt_entry_match *pos;
+	int length = target - match;
+
+	if (length == 0) /* no matches */
+		return 0;
+
+	pos = (struct xt_entry_match *)match;
+	do {
+		if ((unsigned long)pos % alignment)
+			return -EINVAL;
+
+		if (length < (int)sizeof(struct xt_entry_match))
+			return -EINVAL;
+
+		if (pos->u.match_size < sizeof(struct xt_entry_match))
+			return -EINVAL;
+
+		if (pos->u.match_size > length)
+			return -EINVAL;
+
+		length -= pos->u.match_size;
+		pos = ((void *)((char *)(pos) + (pos)->u.match_size));
+	} while (length > 0);
+
+	return 0;
+}
+
 #ifdef CONFIG_COMPAT
 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
 {
@@ -571,7 +612,14 @@ int xt_compat_check_entry_offsets(const void *base, const char *elems,
 	    target_offset + sizeof(struct compat_xt_standard_target) != next_offset)
 		return -EINVAL;
 
-	return 0;
+	/* compat_xt_entry match has less strict aligment requirements,
+	 * otherwise they are identical.  In case of padding differences
+	 * we need to add compat version of xt_check_entry_match.
+	 */
+	BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
+
+	return xt_check_entry_match(elems, base + target_offset,
+				    __alignof__(struct compat_xt_entry_match));
 }
 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
 #endif /* CONFIG_COMPAT */
@@ -584,17 +632,39 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);
  * @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.
+ * validates that target_offset and next_offset are sane and that all
+ * match sizes (if any) align with the target offset.
  *
  * This function does not validate the targets or matches themselves, it
- * only tests that all the offsets and sizes are correct.
+ * only tests that all the offsets and sizes are correct, that all
+ * match structures are aligned, and that the last structure ends where
+ * the target structure begins.
+ *
+ * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
  *
  * 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.
  *
+ * A well-formed entry looks like this:
+ *
+ * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
+ * e->elems[]-----'                              |               |
+ *                matchsize                      |               |
+ *                                matchsize      |               |
+ *                                               |               |
+ * target_offset---------------------------------'               |
+ * next_offset---------------------------------------------------'
+ *
+ * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
+ *          This is where matches (if any) and the target reside.
+ * target_offset: beginning of target.
+ * next_offset: start of the next rule; also: size of this rule.
+ * Since targets have a minimum size, target_offset + minlen <= next_offset.
+ *
+ * Every match stores its size, sum of sizes must not exceed target_offset.
+ *
  * Return: 0 on success, negative errno on failure.
  */
 int xt_check_entry_offsets(const void *base,
@@ -624,7 +694,8 @@ int xt_check_entry_offsets(const void *base,
 	    target_offset + sizeof(struct xt_standard_target) != next_offset)
 		return -EINVAL;
 
-	return 0;
+	return xt_check_entry_match(elems, base + target_offset,
+				    __alignof__(struct xt_entry_match));
 }
 EXPORT_SYMBOL(xt_check_entry_offsets);
 
-- 
2.7.3


  parent reply	other threads:[~2016-04-01 12:17 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-01 12:17 [PATCH nf 00/17] netfilter: xtables: stricter ruleset validation Florian Westphal
2016-04-01 12:17 ` [PATCH nf 01/17] netfilter: x_tables: don't move to non-existent next rule Florian Westphal
2016-04-01 12:17 ` [PATCH nf 02/17] netfilter: x_tables: validate targets of jumps Florian Westphal
2016-04-01 12:24   ` Jan Engelhardt
2016-04-01 12:17 ` [PATCH nf 03/17] netfilter: x_tables: add and use xt_check_entry_offsets Florian Westphal
2016-04-01 12:17 ` [PATCH nf 04/17] netfilter: x_tables: kill check_entry helper Florian Westphal
2016-04-01 12:17 ` [PATCH nf 05/17] netfilter: x_tables: assert minimum target size Florian Westphal
2016-04-01 12:17 ` [PATCH nf 06/17] netfilter: x_tables: add compat version of xt_check_entry_offsets Florian Westphal
2016-04-01 12:17 ` [PATCH nf 07/17] netfilter: x_tables: check standard target size too Florian Westphal
2016-04-01 12:17 ` [PATCH nf 08/17] netfilter: x_tables: check for bogus target offset Florian Westphal
2016-04-01 12:17 ` Florian Westphal [this message]
2016-04-01 12:17 ` [PATCH nf 10/17] netfilter: ip_tables: simplify translate_compat_table args Florian Westphal
2016-04-01 12:17 ` [PATCH nf 11/17] netfilter: ip6_tables: " Florian Westphal
2016-04-01 12:17 ` [PATCH nf 12/17] netfilter: arp_tables: " Florian Westphal
2016-04-01 12:17 ` [PATCH nf 13/17] netfilter: x_tables: xt_compat_match_from_user doesn't need a retval Florian Westphal
2016-04-01 12:17 ` [PATCH nf 14/17] netfilter: x_tables: do compat validation via translate_table Florian Westphal
2016-04-01 12:17 ` [PATCH nf 15/17] netfilter: x_tables: remove obsolete overflow check for compat case too Florian Westphal
2016-04-01 12:17 ` [PATCH nf 16/17] netfilter: x_tables: remove obsolete check Florian Westphal
2016-04-01 12:17 ` [PATCH nf 17/17] netfilter: x_tables: introduce and use xt_copy_counters_from_user Florian Westphal
2016-04-01 12:52   ` kbuild test robot
2016-04-01 13:06   ` kbuild test robot
2016-04-01 13:33   ` kbuild test robot
2016-04-01 13:37 ` [PATCH v2 " Florian Westphal
2016-04-08 11:58 ` [PATCH nf 00/17] netfilter: xtables: stricter ruleset validation Pablo Neira Ayuso
2016-04-08 11:59   ` Florian Westphal
2016-04-12 21:54     ` Pablo Neira Ayuso
2016-04-13 22:33 ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1459513057-30652-10-git-send-email-fw@strlen.de \
    --to=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).