add blobmsg validation function

This commit is contained in:
Felix Fietkau 2011-01-23 19:55:41 +01:00
parent 63cea8dcb7
commit 71f0be5e11
3 changed files with 23 additions and 8 deletions

2
blob.h
View file

@ -141,7 +141,7 @@ blob_id(struct blob_attr *attr)
* blob_len: returns the length of the attribute's payload
*/
static inline unsigned int
blob_len(struct blob_attr *attr)
blob_len(const struct blob_attr *attr)
{
return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
}

View file

@ -15,6 +15,26 @@
#include "blobmsg.h"
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
{
const struct blobmsg_hdr *hdr;
if (blob_len(attr) < sizeof(struct blobmsg_hdr))
return false;
hdr = (void *) attr->data;
if (!hdr->namelen && name)
return false;
if (hdr->namelen > blob_len(attr))
return false;
if (hdr->name[hdr->namelen] != 0)
return false;
return true;
}
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
struct blob_attr **tb, void *data, int len)
{
@ -45,13 +65,7 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
if (hdr->namelen != pslen[i])
continue;
if (!hdr->namelen)
return -1;
if (sizeof(*attr) + blobmsg_hdrlen(hdr->namelen) > blob_pad_len(attr))
return -1;
if (hdr->name[hdr->namelen] != 0)
if (!blobmsg_check_attr(attr, true))
return -1;
if (tb[i])

View file

@ -65,6 +65,7 @@ static inline int blobmsg_data_len(struct blob_attr *attr)
return blob_len(attr) - (end - start);
}
bool blobmsg_check_attr(const struct blob_attr *attr, bool name);
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
struct blob_attr **tb, void *data, int len);