From 43a103ff17ee5872669f8712606578c90c14591d Mon Sep 17 00:00:00 2001 From: Juraj Vijtiuk Date: Sun, 12 Jan 2020 12:26:18 +0100 Subject: blobmsg: blobmsg_parse and blobmsg_parse_array oob read fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Jo-Philipp Wich Signed-off-by: Juraj Vijtiuk [refactored some checks, added fuzz inputs, adjusted unit test results] Signed-off-by: Petr Štetiar --- blobmsg.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'blobmsg.c') diff --git a/blobmsg.c b/blobmsg.c index 3e34821..7620784 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; -- cgit v1.2.3