16502d
From c4685b85f61f28ea0d5cf4e41cb8feaa5193d9dd Mon Sep 17 00:00:00 2001
16502d
From: "Alan T. DeKok" <aland@freeradius.org>
16502d
Date: Tue, 27 Jun 2017 21:49:20 -0400
16502d
Subject: [PATCH] FR-GV-301 - handle malformed WiMAX attributes
16502d
16502d
---
16502d
 src/lib/radius.c         | 177 ++++++++++++++++++++++++++++++++++-------------
16502d
 src/tests/unit/wimax.txt |  12 ++++
16502d
 2 files changed, 142 insertions(+), 47 deletions(-)
16502d
16502d
diff --git a/src/lib/radius.c b/src/lib/radius.c
16502d
index 7114e1650..ea4cbf06b 100644
16502d
--- a/src/lib/radius.c
16502d
+++ b/src/lib/radius.c
16502d
@@ -3229,7 +3229,7 @@ static ssize_t data2vp_extended(TALLOC_CTX *ctx, RADIUS_PACKET *packet,
16502d
 	return end - data;
16502d
 }
16502d
 
16502d
-/** Convert a Vendor-Specific WIMAX to vps
16502d
+/** Convert a Vendor-Specific WIMAX to VPs
16502d
  *
16502d
  * @note Called ONLY for Vendor-Specific
16502d
  */
16502d
@@ -3241,25 +3241,54 @@ static ssize_t data2vp_wimax(TALLOC_CTX *ctx,
16502d
 			     VALUE_PAIR **pvp)
16502d
 {
16502d
 	ssize_t rcode;
16502d
-	size_t fraglen;
16502d
-	bool last_frag;
16502d
+	size_t wimax_len;
16502d
+	bool more;
16502d
 	uint8_t *head, *tail;
16502d
-	uint8_t const *frag, *end;
16502d
+	uint8_t const *attr, *end;
16502d
 	DICT_ATTR const *child;
16502d
 
16502d
-	if (attrlen < 8) return -1;
16502d
+	/*
16502d
+	 *	data = VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
16502d
+	 */
16502d
+
16502d
+	/*
16502d
+	 *	Not enough room for WiMAX Vendor + Wimax attr + length
16502d
+	 *	+ continuation, it's a bad attribute.
16502d
+	 */
16502d
+	if (attrlen < 8) {
16502d
+	raw:		
16502d
+		/*
16502d
+		 *	It's not a Vendor-Specific, it's unknown...
16502d
+		 */
16502d
+		child = dict_unknown_afrom_fields(ctx, PW_VENDOR_SPECIFIC, 0);
16502d
+		if (!child) {
16502d
+			fr_strerror_printf("Internal sanity check %d", __LINE__);
16502d
+			return -1;
16502d
+		}
16502d
+
16502d
+		rcode = data2vp(ctx, packet, original, secret, child,
16502d
+				data, attrlen, attrlen, pvp);
16502d
+		if (rcode < 0) return rcode;
16502d
+		return attrlen;
16502d
+	}
16502d
 
16502d
-	if (((size_t) (data[5] + 4)) != attrlen) return -1;
16502d
+	if (data[5] < 3) goto raw;		/* WiMAX-Length is too small */
16502d
 
16502d
 	child = dict_attrbyvalue(data[4], vendor);
16502d
-	if (!child) return -1;
16502d
+	if (!child) goto raw;
16502d
 
16502d
+	/*
16502d
+	 *	No continued data, just decode the attribute in place.
16502d
+	 */
16502d
 	if ((data[6] & 0x80) == 0) {
16502d
+		if ((data[5] + 4) != attrlen) goto raw; /* WiMAX attribute doesn't fill Vendor-Specific */
16502d
+
16502d
 		rcode = data2vp(ctx, packet, original, secret, child,
16502d
 				data + 7, data[5] - 3, data[5] - 3,
16502d
 				pvp);
16502d
-		if (rcode < 0) return -1;
16502d
-		return 7 + rcode;
16502d
+
16502d
+		if ((rcode < 0) || (((size_t) rcode + 7) != attrlen)) goto raw; /* didn't decode all of the data */
16502d
+		return attrlen;
16502d
 	}
16502d
 
16502d
 	/*
16502d
@@ -3268,61 +3297,115 @@ static ssize_t data2vp_wimax(TALLOC_CTX *ctx,
16502d
 	 *	MUST be all of the same VSA, WiMAX, and WiMAX-attr.
16502d
 	 *
16502d
 	 *	The first fragment doesn't have a RADIUS attribute
16502d
-	 *	header, so it needs to be treated a little special.
16502d
+	 *	header.
16502d
 	 */
16502d
-	fraglen = data[5] - 3;
16502d
-	frag = data + attrlen;
16502d
+	wimax_len = 0;
16502d
+	attr = data + 4;
16502d
 	end = data + packetlen;
16502d
-	last_frag = false;
16502d
 
16502d
-	while (frag < end) {
16502d
-		if (last_frag ||
16502d
-		    (frag[0] != PW_VENDOR_SPECIFIC) ||
16502d
-		    (frag[1] < 9) ||		       /* too short for wimax */
16502d
-		    ((frag + frag[1]) > end) ||		/* overflow */
16502d
-		    (memcmp(frag + 2, data, 4) != 0) || /* not wimax */
16502d
-		    (frag[6] != data[4]) || /* not the same wimax attr */
16502d
-		    ((frag[7] + 6) != frag[1])) { /* doesn't fill the attr */
16502d
-			end = frag;
16502d
-			break;
16502d
-		}
16502d
+	while (attr < end) {
16502d
+		/*
16502d
+		 *	Not enough room for Attribute + length +
16502d
+		 *	continuation, it's bad.
16502d
+		 */
16502d
+		if ((end - attr) < 3) goto raw;
16502d
 
16502d
-		last_frag = ((frag[8] & 0x80) == 0);
16502d
+		/*
16502d
+		 *	Must have non-zero data in the attribute.
16502d
+		 */
16502d
+		if (attr[1] <= 3) goto raw;
16502d
 
16502d
-		fraglen += frag[7] - 3;
16502d
-		frag += frag[1];
16502d
-	}
16502d
+		/*
16502d
+		 *	If the WiMAX attribute overflows the packet,
16502d
+		 *	it's bad.
16502d
+		 */
16502d
+		if ((attr + attr[1]) > end) goto raw;
16502d
 
16502d
-	head = tail = malloc(fraglen);
16502d
-	if (!head) return -1;
16502d
+		/*
16502d
+		 *	Check the continuation flag.
16502d
+		 */
16502d
+		more = ((attr[2] & 0x80) != 0);
16502d
+
16502d
+		/*
16502d
+		 *	Or, there's no more data, in which case we
16502d
+		 *	shorten "end" to finish at this attribute.
16502d
+		 */
16502d
+		if (!more) end = attr + attr[1];
16502d
+
16502d
+		/*
16502d
+		 *	There's more data, but we're at the end of the
16502d
+		 *	packet.  The attribute is malformed!
16502d
+		 */
16502d
+		if (more && ((attr + attr[1]) == end)) goto raw;
16502d
+
16502d
+		/*
16502d
+		 *	Add in the length of the data we need to
16502d
+		 *	concatenate together.
16502d
+		 */
16502d
+		wimax_len += attr[1] - 3;
16502d
+
16502d
+		/*
16502d
+		 *	Go to the next attribute, and stop if there's
16502d
+		 *	no more.
16502d
+		 */
16502d
+		attr += attr[1];
16502d
+		if (!more) break;
16502d
+
16502d
+		/*
16502d
+		 *	data = VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
16502d
+		 *
16502d
+		 *	attr = Vendor-Specific VSA-Length VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
16502d
+		 *
16502d
+		 */
16502d
+
16502d
+		/*
16502d
+		 *	No room for Vendor-Specific + length +
16502d
+		 *	Vendor(4) + attr + length + continuation + data
16502d
+		 */
16502d
+		if ((end - attr) < 9) goto raw;
16502d
+
16502d
+		if (attr[0] != PW_VENDOR_SPECIFIC) goto raw;
16502d
+		if (attr[1] < 9) goto raw;
16502d
+		if ((attr + attr[1]) > end) goto raw;
16502d
+		if (memcmp(data, attr + 2, 4) != 0) goto raw; /* not WiMAX Vendor ID */
16502d
+
16502d
+		if (attr[1] != (attr[7] + 6)) goto raw; /* WiMAX attr doesn't exactly fill the VSA */
16502d
+
16502d
+		if (data[4] != attr[6]) goto raw; /* different WiMAX attribute */
16502d
+
16502d
+		/*
16502d
+		 *	Skip over the Vendor-Specific header, and
16502d
+		 *	continue with the WiMAX attributes.
16502d
+		 */
16502d
+		attr += 6;
16502d
+	}
16502d
 
16502d
 	/*
16502d
-	 *	And again, but faster and looser.
16502d
-	 *
16502d
-	 *	We copy the first fragment, followed by the rest of
16502d
-	 *	the fragments.
16502d
+	 *	No data in the WiMAX attribute, make a "raw" one.
16502d
 	 */
16502d
-	frag = data;
16502d
+	if (!wimax_len) goto raw;
16502d
 
16502d
-	memcpy(tail, frag + 4 + 3, frag[4 + 1] - 3);
16502d
-	tail += frag[4 + 1] - 3;
16502d
-	frag += attrlen;	/* should be frag[1] - 7 */
16502d
+	head = tail = malloc(wimax_len);
16502d
+	if (!head) return -1;
16502d
 
16502d
 	/*
16502d
-	 *	frag now points to RADIUS attributes
16502d
+	 *	Copy the data over, this time trusting the attribute
16502d
+	 *	contents.
16502d
 	 */
16502d
-	do {
16502d
-		memcpy(tail, frag + 2 + 4 + 3, frag[2 + 4 + 1] - 3);
16502d
-		tail += frag[2 + 4 + 1] - 3;
16502d
-		frag += frag[1];
16502d
-	} while (frag < end);
16502d
+	attr = data;
16502d
+	while (attr < end) {
16502d
+		memcpy(tail, attr + 4 + 3, attr[4 + 1] - 3);
16502d
+		tail += attr[4 + 1] - 3;
16502d
+		attr += 4 + attr[4 + 1]; /* skip VID+WiMax header */
16502d
+		attr += 2;		 /* skip Vendor-Specific header */
16502d
+	}
16502d
 
16502d
-	VP_HEXDUMP("wimax fragments", head, fraglen);
16502d
+	VP_HEXDUMP("wimax fragments", head, wimax_len);
16502d
 
16502d
 	rcode = data2vp(ctx, packet, original, secret, child,
16502d
-			head, fraglen, fraglen, pvp);
16502d
+			head, wimax_len, wimax_len, pvp);
16502d
 	free(head);
16502d
-	if (rcode < 0) return rcode;
16502d
+	if (rcode < 0) goto raw;
16502d
 
16502d
 	return end - data;
16502d
 }
16502d
diff --git a/src/tests/unit/wimax.txt b/src/tests/unit/wimax.txt
16502d
index 191b37e25..6e373d59a 100644
16502d
--- a/src/tests/unit/wimax.txt
16502d
+++ b/src/tests/unit/wimax.txt
16502d
@@ -7,6 +7,12 @@ data 1a 0e 00 00 60 b5 01 08 00 01 05 31 2e 30
16502d
 decode -
16502d
 data WiMAX-Release = "1.0"
16502d
 
16502d
+decode 1a 08 00 00 60 b5 01 02
16502d
+data Attr-26 = 0x000060b50102
16502d
+
16502d
+decode 1a 0a 00 00 60 b5 01 04 00 01
16502d
+data Attr-26.24757.1 = 0x01
16502d
+
16502d
 encode WiMAX-Accounting-Capabilities = 1
16502d
 data 1a 0c 00 00 60 b5 01 06 00 02 03 01
16502d
 
16502d
@@ -143,3 +149,9 @@ data 1a ff 00 00 60 b5 01 f9 80 01 ff 45 45 45 45 45 45 45 45 45 45 45 45 45 45
16502d
 
16502d
 decode -
16502d
 data WiMAX-Release = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", WiMAX-Idle-Mode-Notification-Cap = Supported
16502d
+
16502d
+#
16502d
+#  Continuation is set, but there's no continued data.
16502d
+decode 1a 0b 00 00 60 b5 31 05 80 00 00
16502d
+data Attr-26 = 0x000060b53105800000
16502d
+
16502d
-- 
16502d
2.13.2
16502d