blobmsg: blobmsg_parse and blobmsg_parse_array oob read fixes

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@sartura.hr>
Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr>
[refactored some checks, added fuzz inputs, adjusted unit test results]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
This commit is contained in:
Juraj Vijtiuk 2020-01-12 12:26:18 +01:00 committed by Petr Štetiar
parent 5c0faaf4f5
commit 43a103ff17
5 changed files with 56 additions and 20 deletions

View file

@ -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;

View file

@ -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

View file

@ -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)