blobmsg: fix attrs iteration in the blobmsg_check_array_len()

Starting with 75e300aeec ("blobmsg: fix wrong payload len passed from
blobmsg_check_array") blobmsg_check_array_len() gets *blob* length
passed as argument. It cannot be used with __blobmsg_for_each_attr()
which expects *data* length.

Use blobmsg_for_each_attr() which calculates *data* length on its own.

The same bug was already reported in the past and there was fix attempt
in the commit cd75136b13 ("blobmsg: fix wrong payload len passed from
blobmsg_check_array"). That change made blobmsg_check_attr_len() calls
fail however.

This is hopefully the correct & complete fix:
1. blobmsg_check_array_len() gets *blob* length
2. It calls blobmsg_check_attr_len() which requires *blob* length
3. It uses blobmsg_for_each_attr() which gets *data* length

This fixes iterating over random memory treated as attrs. That was
resulting in check failing randomly for totally correct blobs. It's
critical e.g. for procd project with its instance_fill_array() failing
and procd not starting services.

Fixes: 75e300aeec ("blobmsg: fix wrong payload len passed from blobmsg_check_array")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
This commit is contained in:
Rafał Miłecki 2020-05-23 13:18:51 +02:00 committed by Felix Fietkau
parent eeddf22d9c
commit 5e75160f48

View file

@ -117,16 +117,18 @@ int blobmsg_check_array(const struct blob_attr *attr, int type)
return blobmsg_check_array_len(attr, type, blob_len(attr)); return blobmsg_check_array_len(attr, type, blob_len(attr));
} }
int blobmsg_check_array_len(const struct blob_attr *attr, int type, size_t len) int blobmsg_check_array_len(const struct blob_attr *attr, int type,
size_t blob_len)
{ {
struct blob_attr *cur; struct blob_attr *cur;
size_t rem;
bool name; bool name;
int size = 0; int size = 0;
if (type > BLOBMSG_TYPE_LAST) if (type > BLOBMSG_TYPE_LAST)
return -1; return -1;
if (!blobmsg_check_attr_len(attr, false, len)) if (!blobmsg_check_attr_len(attr, false, blob_len))
return -1; return -1;
switch (blobmsg_type(attr)) { switch (blobmsg_type(attr)) {
@ -140,11 +142,11 @@ int blobmsg_check_array_len(const struct blob_attr *attr, int type, size_t len)
return -1; return -1;
} }
__blobmsg_for_each_attr(cur, attr, len) { blobmsg_for_each_attr(cur, attr, rem) {
if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type) if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
return -1; return -1;
if (!blobmsg_check_attr_len(cur, name, len)) if (!blobmsg_check_attr_len(cur, name, rem))
return -1; return -1;
size++; size++;