blobmsg: fix length in blobmsg_check_array
[oweals/libubox.git] / blobmsg.c
1 /*
2  * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include "blobmsg.h"
17
18 static const int blob_type[__BLOBMSG_TYPE_LAST] = {
19         [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
20         [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
21         [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
22         [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
23         [BLOBMSG_TYPE_DOUBLE] = BLOB_ATTR_DOUBLE,
24         [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
25         [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
26 };
27
28 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
29 {
30         return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
31 }
32
33 static const struct blobmsg_hdr* blobmsg_hdr_from_blob(const struct blob_attr *attr, size_t len)
34 {
35         if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr))
36                 return NULL;
37
38         return blob_data(attr);
39 }
40
41 static bool blobmsg_hdr_valid_namelen(const struct blobmsg_hdr *hdr, size_t len)
42 {
43         if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr) + blobmsg_namelen(hdr) + 1)
44                 return false;
45
46         return true;
47 }
48
49 static bool blobmsg_check_name(const struct blob_attr *attr, size_t len, bool name)
50 {
51         char *limit = (char *) attr + len;
52         const struct blobmsg_hdr *hdr;
53
54         hdr = blobmsg_hdr_from_blob(attr, len);
55         if (!hdr)
56                 return false;
57
58         if (name && !hdr->namelen)
59                 return false;
60
61         if (name && !blobmsg_hdr_valid_namelen(hdr, len))
62                 return false;
63
64         if ((char *) hdr->name + blobmsg_namelen(hdr) + 1 > limit)
65                 return false;
66
67         if (blobmsg_namelen(hdr) > (blob_len(attr) - sizeof(struct blobmsg_hdr)))
68                 return false;
69
70         if (hdr->name[blobmsg_namelen(hdr)] != 0)
71                 return false;
72
73         return true;
74 }
75
76 static const char* blobmsg_check_data(const struct blob_attr *attr, size_t len, size_t *data_len)
77 {
78         char *limit = (char *) attr + len;
79         const char *data;
80
81         *data_len = blobmsg_data_len(attr);
82         if (*data_len > blob_raw_len(attr))
83                 return NULL;
84
85         data = blobmsg_data(attr);
86         if (data + *data_len > limit)
87                 return NULL;
88
89         return data;
90 }
91
92 bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
93 {
94         const char *data;
95         size_t data_len;
96         int id;
97
98         if (!blobmsg_check_name(attr, len, name))
99                 return false;
100
101         id = blob_id(attr);
102         if (id > BLOBMSG_TYPE_LAST)
103                 return false;
104
105         if (!blob_type[id])
106                 return true;
107
108         data = blobmsg_check_data(attr, len, &data_len);
109         if (!data)
110                 return false;
111
112         return blob_check_type(data, data_len, blob_type[id]);
113 }
114
115 int blobmsg_check_array(const struct blob_attr *attr, int type)
116 {
117         return blobmsg_check_array_len(attr, type, blob_raw_len(attr));
118 }
119
120 int blobmsg_check_array_len(const struct blob_attr *attr, int type,
121                             size_t blob_len)
122 {
123         struct blob_attr *cur;
124         size_t rem;
125         bool name;
126         int size = 0;
127
128         if (type > BLOBMSG_TYPE_LAST)
129                 return -1;
130
131         if (!blobmsg_check_attr_len(attr, false, blob_len))
132                 return -1;
133
134         switch (blobmsg_type(attr)) {
135         case BLOBMSG_TYPE_TABLE:
136                 name = true;
137                 break;
138         case BLOBMSG_TYPE_ARRAY:
139                 name = false;
140                 break;
141         default:
142                 return -1;
143         }
144
145         blobmsg_for_each_attr(cur, attr, rem) {
146                 if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
147                         return -1;
148
149                 if (!blobmsg_check_attr_len(cur, name, rem))
150                         return -1;
151
152                 size++;
153         }
154
155         return size;
156 }
157
158 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
159 {
160         return blobmsg_check_array(attr, type) >= 0;
161 }
162
163 bool blobmsg_check_attr_list_len(const struct blob_attr *attr, int type, size_t len)
164 {
165         return blobmsg_check_array_len(attr, type, len) >= 0;
166 }
167
168 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
169                         struct blob_attr **tb, void *data, unsigned int len)
170 {
171         struct blob_attr *attr;
172         int i = 0;
173
174         memset(tb, 0, policy_len * sizeof(*tb));
175         __blob_for_each_attr(attr, data, len) {
176                 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
177                     blob_id(attr) != policy[i].type)
178                         continue;
179
180                 if (!blobmsg_check_attr_len(attr, false, len))
181                         return -1;
182
183                 if (tb[i])
184                         continue;
185
186                 tb[i++] = attr;
187                 if (i == policy_len)
188                         break;
189         }
190
191         return 0;
192 }
193
194 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
195                   struct blob_attr **tb, void *data, unsigned int len)
196 {
197         const struct blobmsg_hdr *hdr;
198         struct blob_attr *attr;
199         uint8_t *pslen;
200         int i;
201
202         memset(tb, 0, policy_len * sizeof(*tb));
203         if (!data || !len)
204                 return -EINVAL;
205         pslen = alloca(policy_len);
206         for (i = 0; i < policy_len; i++) {
207                 if (!policy[i].name)
208                         continue;
209
210                 pslen[i] = strlen(policy[i].name);
211         }
212
213         __blob_for_each_attr(attr, data, len) {
214                 hdr = blobmsg_hdr_from_blob(attr, len);
215                 if (!hdr)
216                         return -1;
217
218                 if (!blobmsg_hdr_valid_namelen(hdr, len))
219                         return -1;
220
221                 for (i = 0; i < policy_len; i++) {
222                         if (!policy[i].name)
223                                 continue;
224
225                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
226                             blob_id(attr) != policy[i].type)
227                                 continue;
228
229                         if (blobmsg_namelen(hdr) != pslen[i])
230                                 continue;
231
232                         if (!blobmsg_check_attr_len(attr, true, len))
233                                 return -1;
234
235                         if (tb[i])
236                                 continue;
237
238                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
239                                 continue;
240
241                         tb[i] = attr;
242                 }
243         }
244
245         return 0;
246 }
247
248
249 static struct blob_attr *
250 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
251 {
252         struct blob_attr *attr;
253         struct blobmsg_hdr *hdr;
254         int attrlen, namelen;
255         char *pad_start, *pad_end;
256
257         if (!name)
258                 name = "";
259
260         namelen = strlen(name);
261         attrlen = blobmsg_hdrlen(namelen) + payload_len;
262         attr = blob_new(buf, type, attrlen);
263         if (!attr)
264                 return NULL;
265
266         attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
267         hdr = blob_data(attr);
268         hdr->namelen = cpu_to_be16(namelen);
269
270         memcpy(hdr->name, name, namelen);
271         hdr->name[namelen] = '\0';
272
273         pad_end = *data = blobmsg_data(attr);
274         pad_start = (char *) &hdr->name[namelen];
275         if (pad_start < pad_end)
276                 memset(pad_start, 0, pad_end - pad_start);
277
278         return attr;
279 }
280
281 static inline int
282 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
283 {
284         return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
285 }
286
287
288 void *
289 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
290 {
291         struct blob_attr *head;
292         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
293         unsigned long offset = attr_to_offset(buf, buf->head);
294         void *data;
295
296         if (!name)
297                 name = "";
298
299         head = blobmsg_new(buf, type, name, 0, &data);
300         if (!head)
301                 return NULL;
302         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
303         buf->head = head;
304         return (void *)offset;
305 }
306
307 __attribute__((format(printf, 3, 0)))
308 int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
309 {
310         va_list arg2;
311         char cbuf;
312         char *sbuf;
313         int len, ret;
314
315         va_copy(arg2, arg);
316         len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
317         va_end(arg2);
318
319         if (len < 0)
320                 return -1;
321
322         sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
323         if (!sbuf)
324                 return -1;
325
326         ret = vsnprintf(sbuf, len + 1, format, arg);
327         if (ret < 0)
328                 return -1;
329
330         blobmsg_add_string_buffer(buf);
331
332         return ret;
333 }
334
335 __attribute__((format(printf, 3, 4)))
336 int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
337 {
338         va_list ap;
339         int ret;
340
341         va_start(ap, format);
342         ret = blobmsg_vprintf(buf, name, format, ap);
343         va_end(ap);
344
345         return ret;
346 }
347
348 void *
349 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
350 {
351         struct blob_attr *attr;
352         void *data_dest;
353
354         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
355         if (!attr)
356                 return NULL;
357
358         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
359         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
360
361         return data_dest;
362 }
363
364 void *
365 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
366 {
367         struct blob_attr *attr = blob_next(buf->head);
368         int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
369         int required = maxlen - (buf->buflen - offset);
370
371         if (required <= 0)
372                 goto out;
373
374         if (!blob_buf_grow(buf, required))
375                 return NULL;
376         attr = blob_next(buf->head);
377
378 out:
379         return blobmsg_data(attr);
380 }
381
382 void
383 blobmsg_add_string_buffer(struct blob_buf *buf)
384 {
385         struct blob_attr *attr;
386         int len, attrlen;
387
388         attr = blob_next(buf->head);
389         len = strlen(blobmsg_data(attr)) + 1;
390
391         attrlen = blob_raw_len(attr) + len;
392         blob_set_raw_len(attr, attrlen);
393         blob_fill_pad(attr);
394
395         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
396 }
397
398 int
399 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
400                   const void *data, unsigned int len)
401 {
402         struct blob_attr *attr;
403         void *data_dest;
404
405         attr = blobmsg_new(buf, type, name, len, &data_dest);
406         if (!attr)
407                 return -1;
408
409         if (len > 0)
410                 memcpy(data_dest, data, len);
411
412         return 0;
413 }