add blobmsg validation function
authorFelix Fietkau <nbd@openwrt.org>
Sun, 23 Jan 2011 18:55:41 +0000 (19:55 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 23 Jan 2011 18:55:41 +0000 (19:55 +0100)
blob.h
blobmsg.c
blobmsg.h

diff --git a/blob.h b/blob.h
index 401a355..c9364e9 100644 (file)
--- a/blob.h
+++ b/blob.h
@@ -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: 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);
 }
 {
        return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
 }
index 25b72ae..cc6b2f3 100644 (file)
--- a/blobmsg.c
+++ b/blobmsg.c
 
 #include "blobmsg.h"
 
 
 #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)
 {
 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 != 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])
                                return -1;
 
                        if (tb[i])
index b50cca9..3085d00 100644 (file)
--- a/blobmsg.h
+++ b/blobmsg.h
@@ -65,6 +65,7 @@ static inline int blobmsg_data_len(struct blob_attr *attr)
        return blob_len(attr) - (end - start);
 }
 
        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);
 
 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
                   struct blob_attr **tb, void *data, int len);