[OpenWrt-Devel] [PATCH libubox v2] blobmsg: blobmsg_parse and blobmsg_parse_array oob read fixes

Petr Štetiar ynezz at true.cz
Mon Jan 20 06:01:22 EST 2020


From: Juraj Vijtiuk <juraj.vijtiuk at sartura.hr>

Fix out of bounds read in blobmsg_parse and blobmsg_check_name. The
out of bounds read happens because blob_attr and blobmsg_hdr have
flexible array members, whose size is 0 in the corresponding sizeofs.
For example the __blob_for_each_attr macro checks whether rem >=
sizeof(struct blob_attr). However, what LibFuzzer discovered was,
if the input data was only 4 bytes, the data would be casted to blob_attr,
and later on blob_data(attr) would be called even though attr->data was empty.
The same issue could appear with data larger than 4 bytes, where data
wasn't empty, but contained only the start of the blobmsg_hdr struct,
and blobmsg_hdr name was empty. The bugs were discovered by fuzzing
blobmsg_parse and blobmsg_array_parse with LibFuzzer.

CC: Luka Perkov <luka.perkov at sartura.hr>
Signed-off-by: Juraj Vijtiuk <juraj.vijtiuk at sartura.hr>
[fixed from, refactored some checks, added fuzz inputs, updated test results]
Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 blobmsg.c                                     |  40 ++++++++++++++----
 tests/cram/test_blob_parse.t                  |   4 ++
 tests/cram/test_blobmsg_parse.t               |  32 ++++++++------
 ...h-a3585b70f1c7ffbdec10f6dadc964336118485c4 | Bin 0 -> 4 bytes
 ...h-b3585b70f1c7ffbdec10f6dadc964336118485c4 | Bin 0 -> 6 bytes
 5 files changed, 56 insertions(+), 20 deletions(-)
 create mode 100644 tests/fuzz/corpus/crash-a3585b70f1c7ffbdec10f6dadc964336118485c4
 create mode 100644 tests/fuzz/corpus/crash-b3585b70f1c7ffbdec10f6dadc964336118485c4

diff --git a/blobmsg.c b/blobmsg.c
index 3e34821cc928..762078436f9b 100644
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -30,16 +30,38 @@ bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
 	return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
 }
 
+static const struct blobmsg_hdr* blobmsg_hdr_from_blob(const struct blob_attr *attr, size_t len)
+{
+	if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr))
+		return NULL;
+
+	return blob_data(attr);
+}
+
+static bool blobmsg_hdr_valid_namelen(const struct blobmsg_hdr *hdr, size_t len)
+{
+	if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr) + blobmsg_namelen(hdr) + 1)
+		return false;
+
+	return true;
+}
+
 static bool blobmsg_check_name(const struct blob_attr *attr, size_t len, bool name)
 {
 	char *limit = (char *) attr + len;
 	const struct blobmsg_hdr *hdr;
 
-	hdr = blob_data(attr);
+	hdr = blobmsg_hdr_from_blob(attr, len);
+	if (!hdr)
+		return false;
+
 	if (name && !hdr->namelen)
 		return false;
 
-	if ((char *) hdr->name + blobmsg_namelen(hdr) > limit)
+	if (name && !blobmsg_hdr_valid_namelen(hdr, len))
+		return false;
+
+	if ((char *) hdr->name + blobmsg_namelen(hdr) + 1 > limit)
 		return false;
 
 	if (blobmsg_namelen(hdr) > (blob_len(attr) - sizeof(struct blobmsg_hdr)))
@@ -73,9 +95,6 @@ bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
 	size_t data_len;
 	int id;
 
-	if (len < sizeof(struct blob_attr))
-		return false;
-
 	if (!blobmsg_check_name(attr, len, name))
 		return false;
 
@@ -170,11 +189,10 @@ int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
 	return 0;
 }
 
-
 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
                   struct blob_attr **tb, void *data, unsigned int len)
 {
-	struct blobmsg_hdr *hdr;
+	const struct blobmsg_hdr *hdr;
 	struct blob_attr *attr;
 	uint8_t *pslen;
 	int i;
@@ -191,7 +209,13 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
 	}
 
 	__blob_for_each_attr(attr, data, len) {
-		hdr = blob_data(attr);
+		hdr = blobmsg_hdr_from_blob(attr, len);
+		if (!hdr)
+			return -1;
+
+		if (!blobmsg_hdr_valid_namelen(hdr, len))
+			return -1;
+
 		for (i = 0; i < policy_len; i++) {
 			if (!policy[i].name)
 				continue;
diff --git a/tests/cram/test_blob_parse.t b/tests/cram/test_blob_parse.t
index 1fd60bc71122..b6cbbaa811b7 100644
--- a/tests/cram/test_blob_parse.t
+++ b/tests/cram/test_blob_parse.t
@@ -68,6 +68,10 @@ check that blob_parse is producing expected results:
   cannot parse cert crash-813f3e68661da09c26d4a87dbb9d5099e92be50f
   cannot parse cert crash-98595faa58ba01d85ba4fd0b109cd3d490b45795
   cannot parse cert crash-98595faa58ba01d85ba4fd0b109cd3d490b45795
+  cannot parse cert crash-a3585b70f1c7ffbdec10f6dadc964336118485c4
+  cannot parse cert crash-a3585b70f1c7ffbdec10f6dadc964336118485c4
+  cannot parse cert crash-b3585b70f1c7ffbdec10f6dadc964336118485c4
+  cannot parse cert crash-b3585b70f1c7ffbdec10f6dadc964336118485c4
   cannot parse cert crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1
   cannot parse cert crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1
   cannot parse cert crash-df9d1243057b27bbad6211e5a23d1cb699028aa2
diff --git a/tests/cram/test_blobmsg_parse.t b/tests/cram/test_blobmsg_parse.t
index 37e09ce33f1a..afed22c0e603 100644
--- a/tests/cram/test_blobmsg_parse.t
+++ b/tests/cram/test_blobmsg_parse.t
@@ -27,37 +27,45 @@ check that blobmsg_parse is producing expected results:
   crash-333757b203a44751d3535f24b05f467183a96d09: blobmsg_parse_array: ... (0)
   crash-333757b203a44751d3535f24b05f467183a96d09: blobmsg_parse: ... (0)
   crash-333757b203a44751d3535f24b05f467183a96d09: blobmsg_parse_array: ... (0)
-  crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse: ... (0)
+  crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse: ... (-1)
   crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse_array: ... (-1)
-  crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse: ... (0)
+  crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse: ... (-1)
   crash-4c4d2c3c9ade5da9347534e290305c3b9760f627: blobmsg_parse_array: ... (-1)
-  crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse: ... (0)
+  crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse: ... (-1)
   crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse_array: ... (-1)
-  crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse: ... (0)
+  crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse: ... (-1)
   crash-5e9937b197c88bf4e7b7ee2612456cad4cb83f5b: blobmsg_parse_array: ... (-1)
-  crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse: ... (0)
+  crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse: ... (-1)
   crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse_array: ... (-1)
-  crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse: ... (0)
+  crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse: ... (-1)
   crash-75b146c4e6fac64d3e62236b27c64b50657bab2a: blobmsg_parse_array: ... (-1)
-  crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse: ... (0)
+  crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse: ... (-1)
   crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse_array: ... (-1)
-  crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse: ... (0)
+  crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse: ... (-1)
   crash-813f3e68661da09c26d4a87dbb9d5099e92be50f: blobmsg_parse_array: ... (-1)
   crash-98595faa58ba01d85ba4fd0b109cd3d490b45795: blobmsg_parse: ... (0)
   crash-98595faa58ba01d85ba4fd0b109cd3d490b45795: blobmsg_parse_array: ... (0)
   crash-98595faa58ba01d85ba4fd0b109cd3d490b45795: blobmsg_parse: ... (0)
   crash-98595faa58ba01d85ba4fd0b109cd3d490b45795: blobmsg_parse_array: ... (0)
+  crash-a3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse: ... (-1)
+  crash-a3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse_array: ... (-1)
+  crash-a3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse: ... (-1)
+  crash-a3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse_array: ... (-1)
+  crash-b3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse: ... (-1)
+  crash-b3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse_array: ... (-1)
+  crash-b3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse: ... (-1)
+  crash-b3585b70f1c7ffbdec10f6dadc964336118485c4: blobmsg_parse_array: ... (-1)
   crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1: blobmsg_parse: ... (0)
   crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1: blobmsg_parse_array: ... (0)
   crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1: blobmsg_parse: ... (0)
   crash-d0f3aa7d60a094b021f635d4edb7807c055a4ea1: blobmsg_parse_array: ... (0)
-  crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse: ... (0)
+  crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse: ... (-1)
   crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse_array: ... (0)
-  crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse: ... (0)
+  crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse: ... (-1)
   crash-df9d1243057b27bbad6211e5a23d1cb699028aa2: blobmsg_parse_array: ... (0)
-  crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse: ... (0)
+  crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse: ... (-1)
   crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse_array: ... (-1)
-  crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse: ... (0)
+  crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse: ... (-1)
   crash-e2fd5ecb3b37926743256f1083f47a07c39e10c2: blobmsg_parse_array: ... (-1)
   valid-blobmsg.bin: blobmsg_parse: MLT (0)
   valid-blobmsg.bin: blobmsg_parse_array: MLT (0)
diff --git a/tests/fuzz/corpus/crash-a3585b70f1c7ffbdec10f6dadc964336118485c4 b/tests/fuzz/corpus/crash-a3585b70f1c7ffbdec10f6dadc964336118485c4
new file mode 100644
index 0000000000000000000000000000000000000000..879ba4da035f961e72cd9ae389b4e29c7d298594
GIT binary patch
literal 4
LcmZQ(U|<0N02BZS

literal 0
HcmV?d00001

diff --git a/tests/fuzz/corpus/crash-b3585b70f1c7ffbdec10f6dadc964336118485c4 b/tests/fuzz/corpus/crash-b3585b70f1c7ffbdec10f6dadc964336118485c4
new file mode 100644
index 0000000000000000000000000000000000000000..12b832b5cd7868045d38706d6dd341e6516e687a
GIT binary patch
literal 6
NcmZQ(U|?oo0000X00#g7

literal 0
HcmV?d00001


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list