]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_ematch.c
ematch: fix possible snprintf overflow
[mirror_iproute2.git] / tc / m_ematch.c
1 /*
2 * m_ematch.c Extended Matches
3 *
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <stdarg.h>
22 #include <errno.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "m_ematch.h"
27
28 #define EMATCH_MAP "/etc/iproute2/ematch_map"
29
30 static struct ematch_util *ematch_list;
31
32 /* export to bison parser */
33 int ematch_argc;
34 char **ematch_argv;
35 char *ematch_err;
36 struct ematch *ematch_root;
37
38 static int begin_argc;
39 static char **begin_argv;
40
41 static inline void map_warning(int num, char *kind)
42 {
43 fprintf(stderr,
44 "Error: Unable to find ematch \"%s\" in %s\n" \
45 "Please assign a unique ID to the ematch kind the suggested " \
46 "entry is:\n" \
47 "\t%d\t%s\n",
48 kind, EMATCH_MAP, num, kind);
49 }
50
51 static int lookup_map(__u16 num, char *dst, int len, const char *file)
52 {
53 int err = -EINVAL;
54 char buf[512];
55 FILE *fd = fopen(file, "r");
56
57 if (fd == NULL)
58 return -errno;
59
60 while (fgets(buf, sizeof(buf), fd)) {
61 char namebuf[512], *p = buf;
62 int id;
63
64 while (*p == ' ' || *p == '\t')
65 p++;
66 if (*p == '#' || *p == '\n' || *p == 0)
67 continue;
68
69 if (sscanf(p, "%d %s", &id, namebuf) != 2) {
70 fprintf(stderr, "ematch map %s corrupted at %s\n",
71 file, p);
72 goto out;
73 }
74
75 if (id == num) {
76 if (dst)
77 strncpy(dst, namebuf, len - 1);
78 err = 0;
79 goto out;
80 }
81 }
82
83 err = -ENOENT;
84 out:
85 fclose(fd);
86 return err;
87 }
88
89 static int lookup_map_id(char *kind, int *dst, const char *file)
90 {
91 int err = -EINVAL;
92 char buf[512];
93 FILE *fd = fopen(file, "r");
94
95 if (fd == NULL)
96 return -errno;
97
98 while (fgets(buf, sizeof(buf), fd)) {
99 char namebuf[512], *p = buf;
100 int id;
101
102 while (*p == ' ' || *p == '\t')
103 p++;
104 if (*p == '#' || *p == '\n' || *p == 0)
105 continue;
106
107 if (sscanf(p, "%d %s", &id, namebuf) != 2) {
108 fprintf(stderr, "ematch map %s corrupted at %s\n",
109 file, p);
110 goto out;
111 }
112
113 if (!strcasecmp(namebuf, kind)) {
114 if (dst)
115 *dst = id;
116 err = 0;
117 goto out;
118 }
119 }
120
121 err = -ENOENT;
122 *dst = 0;
123 out:
124 fclose(fd);
125 return err;
126 }
127
128 static struct ematch_util *get_ematch_kind(char *kind)
129 {
130 static void *body;
131 void *dlh;
132 char buf[256];
133 struct ematch_util *e;
134
135 for (e = ematch_list; e; e = e->next) {
136 if (strcmp(e->kind, kind) == 0)
137 return e;
138 }
139
140 snprintf(buf, sizeof(buf), "em_%s.so", kind);
141 dlh = dlopen(buf, RTLD_LAZY);
142 if (dlh == NULL) {
143 dlh = body;
144 if (dlh == NULL) {
145 dlh = body = dlopen(NULL, RTLD_LAZY);
146 if (dlh == NULL)
147 return NULL;
148 }
149 }
150
151 snprintf(buf, sizeof(buf), "%s_ematch_util", kind);
152 e = dlsym(dlh, buf);
153 if (e == NULL)
154 return NULL;
155
156 e->next = ematch_list;
157 ematch_list = e;
158
159 return e;
160 }
161
162 static struct ematch_util *get_ematch_kind_num(__u16 kind)
163 {
164 char name[513];
165
166 if (lookup_map(kind, name, sizeof(name), EMATCH_MAP) < 0)
167 return NULL;
168
169 return get_ematch_kind(name);
170 }
171
172 static int parse_tree(struct nlmsghdr *n, struct ematch *tree)
173 {
174 int index = 1;
175 struct ematch *t;
176
177 for (t = tree; t; t = t->next) {
178 struct rtattr *tail = NLMSG_TAIL(n);
179 struct tcf_ematch_hdr hdr = { .flags = t->relation };
180
181 if (t->inverted)
182 hdr.flags |= TCF_EM_INVERT;
183
184 addattr_l(n, MAX_MSG, index++, NULL, 0);
185
186 if (t->child) {
187 __u32 r = t->child_ref;
188
189 addraw_l(n, MAX_MSG, &hdr, sizeof(hdr));
190 addraw_l(n, MAX_MSG, &r, sizeof(r));
191 } else {
192 int num = 0, err;
193 char buf[64];
194 struct ematch_util *e;
195
196 if (t->args == NULL)
197 return -1;
198
199 strncpy(buf, (char *) t->args->data, sizeof(buf)-1);
200 e = get_ematch_kind(buf);
201 if (e == NULL) {
202 fprintf(stderr, "Unknown ematch \"%s\"\n",
203 buf);
204 return -1;
205 }
206
207 err = lookup_map_id(buf, &num, EMATCH_MAP);
208 if (err < 0) {
209 if (err == -ENOENT)
210 map_warning(e->kind_num, buf);
211 return err;
212 }
213
214 hdr.kind = num;
215 if (e->parse_eopt(n, &hdr, t->args->next) < 0)
216 return -1;
217 }
218
219 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
220 }
221
222 return 0;
223 }
224
225 static int flatten_tree(struct ematch *head, struct ematch *tree)
226 {
227 int i, count = 0;
228 struct ematch *t;
229
230 for (;;) {
231 count++;
232
233 if (tree->child) {
234 for (t = head; t->next; t = t->next);
235 t->next = tree->child;
236 count += flatten_tree(head, tree->child);
237 }
238
239 if (tree->relation == 0)
240 break;
241
242 tree = tree->next;
243 }
244
245 for (i = 0, t = head; t; t = t->next, i++)
246 t->index = i;
247
248 for (t = head; t; t = t->next)
249 if (t->child)
250 t->child_ref = t->child->index;
251
252 return count;
253 }
254
255 int em_parse_error(int err, struct bstr *args, struct bstr *carg,
256 struct ematch_util *e, char *fmt, ...)
257 {
258 va_list a;
259
260 va_start(a, fmt);
261 vfprintf(stderr, fmt, a);
262 va_end(a);
263
264 if (ematch_err)
265 fprintf(stderr, ": %s\n... ", ematch_err);
266 else
267 fprintf(stderr, "\n... ");
268
269 while (ematch_argc < begin_argc) {
270 if (ematch_argc == (begin_argc - 1))
271 fprintf(stderr, ">>%s<< ", *begin_argv);
272 else
273 fprintf(stderr, "%s ", *begin_argv);
274 begin_argv++;
275 begin_argc--;
276 }
277
278 fprintf(stderr, "...\n");
279
280 if (args) {
281 fprintf(stderr, "... %s(", e->kind);
282 while (args) {
283 fprintf(stderr, "%s", args == carg ? ">>" : "");
284 bstr_print(stderr, args, 1);
285 fprintf(stderr, "%s%s", args == carg ? "<<" : "",
286 args->next ? " " : "");
287 args = args->next;
288 }
289 fprintf(stderr, ")...\n");
290
291 }
292
293 if (e == NULL) {
294 fprintf(stderr,
295 "Usage: EXPR\n" \
296 "where: EXPR := TERM [ { and | or } EXPR ]\n" \
297 " TERM := [ not ] { MATCH | '(' EXPR ')' }\n" \
298 " MATCH := module '(' ARGS ')'\n" \
299 " ARGS := ARG1 ARG2 ...\n" \
300 "\n" \
301 "Example: a(x y) and not (b(x) or c(x y z))\n");
302 } else
303 e->print_usage(stderr);
304
305 return -err;
306 }
307
308 static inline void free_ematch_err(void)
309 {
310 if (ematch_err) {
311 free(ematch_err);
312 ematch_err = NULL;
313 }
314 }
315
316 extern int ematch_parse(void);
317
318 int parse_ematch(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
319 {
320 begin_argc = ematch_argc = *argc_p;
321 begin_argv = ematch_argv = *argv_p;
322
323 if (ematch_parse()) {
324 int err = em_parse_error(EINVAL, NULL, NULL, NULL,
325 "Parse error");
326 free_ematch_err();
327 return err;
328 }
329
330 free_ematch_err();
331
332 /* undo look ahead by parser */
333 ematch_argc++;
334 ematch_argv--;
335
336 if (ematch_root) {
337 struct rtattr *tail, *tail_list;
338
339 struct tcf_ematch_tree_hdr hdr = {
340 .nmatches = flatten_tree(ematch_root, ematch_root),
341 .progid = TCF_EM_PROG_TC
342 };
343
344 tail = NLMSG_TAIL(n);
345 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
346 addattr_l(n, MAX_MSG, TCA_EMATCH_TREE_HDR, &hdr, sizeof(hdr));
347
348 tail_list = NLMSG_TAIL(n);
349 addattr_l(n, MAX_MSG, TCA_EMATCH_TREE_LIST, NULL, 0);
350
351 if (parse_tree(n, ematch_root) < 0)
352 return -1;
353
354 tail_list->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail_list;
355 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
356 }
357
358 *argc_p = ematch_argc;
359 *argv_p = ematch_argv;
360
361 return 0;
362 }
363
364 static int print_ematch_seq(FILE *fd, struct rtattr **tb, int start,
365 int prefix)
366 {
367 int n, i = start;
368 struct tcf_ematch_hdr *hdr;
369 int dlen;
370 void *data;
371
372 for (;;) {
373 if (tb[i] == NULL)
374 return -1;
375
376 dlen = RTA_PAYLOAD(tb[i]) - sizeof(*hdr);
377 data = (void *) RTA_DATA(tb[i]) + sizeof(*hdr);
378
379 if (dlen < 0)
380 return -1;
381
382 hdr = RTA_DATA(tb[i]);
383
384 if (hdr->flags & TCF_EM_INVERT)
385 fprintf(fd, "NOT ");
386
387 if (hdr->kind == 0) {
388 __u32 ref;
389
390 if (dlen < sizeof(__u32))
391 return -1;
392
393 ref = *(__u32 *) data;
394 fprintf(fd, "(\n");
395 for (n = 0; n <= prefix; n++)
396 fprintf(fd, " ");
397 if (print_ematch_seq(fd, tb, ref + 1, prefix + 1) < 0)
398 return -1;
399 for (n = 0; n < prefix; n++)
400 fprintf(fd, " ");
401 fprintf(fd, ") ");
402
403 } else {
404 struct ematch_util *e;
405
406 e = get_ematch_kind_num(hdr->kind);
407 if (e == NULL)
408 fprintf(fd, "[unknown ematch %d]\n",
409 hdr->kind);
410 else {
411 fprintf(fd, "%s(", e->kind);
412 if (e->print_eopt(fd, hdr, data, dlen) < 0)
413 return -1;
414 fprintf(fd, ")\n");
415 }
416 if (hdr->flags & TCF_EM_REL_MASK)
417 for (n = 0; n < prefix; n++)
418 fprintf(fd, " ");
419 }
420
421 switch (hdr->flags & TCF_EM_REL_MASK) {
422 case TCF_EM_REL_AND:
423 fprintf(fd, "AND ");
424 break;
425
426 case TCF_EM_REL_OR:
427 fprintf(fd, "OR ");
428 break;
429
430 default:
431 return 0;
432 }
433
434 i++;
435 }
436
437 return 0;
438 }
439
440 static int print_ematch_list(FILE *fd, struct tcf_ematch_tree_hdr *hdr,
441 struct rtattr *rta)
442 {
443 int err = -1;
444 struct rtattr **tb;
445
446 tb = malloc((hdr->nmatches + 1) * sizeof(struct rtattr *));
447 if (tb == NULL)
448 return -1;
449
450 if (hdr->nmatches > 0) {
451 if (parse_rtattr_nested(tb, hdr->nmatches, rta) < 0)
452 goto errout;
453
454 fprintf(fd, "\n ");
455 if (print_ematch_seq(fd, tb, 1, 1) < 0)
456 goto errout;
457 }
458
459 err = 0;
460 errout:
461 free(tb);
462 return err;
463 }
464
465 int print_ematch(FILE *fd, const struct rtattr *rta)
466 {
467 struct rtattr *tb[TCA_EMATCH_TREE_MAX+1];
468 struct tcf_ematch_tree_hdr *hdr;
469
470 if (parse_rtattr_nested(tb, TCA_EMATCH_TREE_MAX, rta) < 0)
471 return -1;
472
473 if (tb[TCA_EMATCH_TREE_HDR] == NULL) {
474 fprintf(stderr, "Missing ematch tree header\n");
475 return -1;
476 }
477
478 if (tb[TCA_EMATCH_TREE_LIST] == NULL) {
479 fprintf(stderr, "Missing ematch tree list\n");
480 return -1;
481 }
482
483 if (RTA_PAYLOAD(tb[TCA_EMATCH_TREE_HDR]) < sizeof(*hdr)) {
484 fprintf(stderr, "Ematch tree header size mismatch\n");
485 return -1;
486 }
487
488 hdr = RTA_DATA(tb[TCA_EMATCH_TREE_HDR]);
489
490 return print_ematch_list(fd, hdr, tb[TCA_EMATCH_TREE_LIST]);
491 }
492
493 struct bstr *bstr_alloc(const char *text)
494 {
495 struct bstr *b = calloc(1, sizeof(*b));
496
497 if (b == NULL)
498 return NULL;
499
500 b->data = strdup(text);
501 if (b->data == NULL) {
502 free(b);
503 return NULL;
504 }
505
506 b->len = strlen(text);
507
508 return b;
509 }
510
511 unsigned long bstrtoul(const struct bstr *b)
512 {
513 char *inv = NULL;
514 unsigned long l;
515 char buf[b->len+1];
516
517 memcpy(buf, b->data, b->len);
518 buf[b->len] = '\0';
519
520 l = strtoul(buf, &inv, 0);
521 if (l == ULONG_MAX || inv == buf)
522 return ULONG_MAX;
523
524 return l;
525 }
526
527 void bstr_print(FILE *fd, const struct bstr *b, int ascii)
528 {
529 int i;
530 char *s = b->data;
531
532 if (ascii)
533 for (i = 0; i < b->len; i++)
534 fprintf(fd, "%c", isprint(s[i]) ? s[i] : '.');
535 else {
536 for (i = 0; i < b->len; i++)
537 fprintf(fd, "%02x", s[i]);
538 fprintf(fd, "\"");
539 for (i = 0; i < b->len; i++)
540 fprintf(fd, "%c", isprint(s[i]) ? s[i] : '.');
541 fprintf(fd, "\"");
542 }
543 }
544
545 void print_ematch_tree(const struct ematch *tree)
546 {
547 const struct ematch *t;
548
549 for (t = tree; t; t = t->next) {
550 if (t->inverted)
551 printf("NOT ");
552
553 if (t->child) {
554 printf("(");
555 print_ematch_tree(t->child);
556 printf(")");
557 } else {
558 struct bstr *b;
559
560 for (b = t->args; b; b = b->next)
561 printf("%s%s", b->data, b->next ? " " : "");
562 }
563
564 if (t->relation == TCF_EM_REL_AND)
565 printf(" AND ");
566 else if (t->relation == TCF_EM_REL_OR)
567 printf(" OR ");
568 }
569 }