|
|
e9acfd |
From 91cda136ef27402256dbf85434374b43ab52d932 Mon Sep 17 00:00:00 2001
|
|
|
e9acfd |
From: Phil Sutter <psutter@redhat.com>
|
|
|
e9acfd |
Date: Fri, 11 Aug 2017 11:15:30 +0200
|
|
|
e9acfd |
Subject: [PATCH] bpf: Make bytecode-file reading a little more robust
|
|
|
e9acfd |
|
|
|
e9acfd |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1477491
|
|
|
e9acfd |
Upstream Status: iproute2.git commit 3da3ebfca85b8
|
|
|
e9acfd |
|
|
|
e9acfd |
commit 3da3ebfca85b8f1e8252b898453d8cb383c5c398
|
|
|
e9acfd |
Author: Phil Sutter <phil@nwl.cc>
|
|
|
e9acfd |
Date: Wed Aug 2 14:57:56 2017 +0200
|
|
|
e9acfd |
|
|
|
e9acfd |
bpf: Make bytecode-file reading a little more robust
|
|
|
e9acfd |
|
|
|
e9acfd |
bpf_parse_string() will now correctly handle:
|
|
|
e9acfd |
|
|
|
e9acfd |
- Extraneous whitespace,
|
|
|
e9acfd |
- OPs on multiple lines and
|
|
|
e9acfd |
- overlong file names.
|
|
|
e9acfd |
|
|
|
e9acfd |
The added feature of allowing to have OPs on multiple lines (like e.g.
|
|
|
e9acfd |
tcpdump prints them) is rather a side effect of fixing detection of
|
|
|
e9acfd |
malformed bytecode files having random content on a second line, like
|
|
|
e9acfd |
e.g.:
|
|
|
e9acfd |
|
|
|
e9acfd |
| 4,40 0 0 12,21 0 1 2048,6 0 0 262144,6 0 0 0
|
|
|
e9acfd |
| foobar
|
|
|
e9acfd |
|
|
|
e9acfd |
Cc: Daniel Borkmann <daniel@iogearbox.net>
|
|
|
e9acfd |
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
|
|
e9acfd |
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
|
|
|
e9acfd |
---
|
|
|
e9acfd |
lib/bpf.c | 32 ++++++++++++++++++++++++--------
|
|
|
e9acfd |
1 file changed, 24 insertions(+), 8 deletions(-)
|
|
|
e9acfd |
|
|
|
e9acfd |
diff --git a/lib/bpf.c b/lib/bpf.c
|
|
|
e9acfd |
index 04ee1ab..73dac5c 100644
|
|
|
e9acfd |
--- a/lib/bpf.c
|
|
|
e9acfd |
+++ b/lib/bpf.c
|
|
|
e9acfd |
@@ -160,11 +160,11 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
|
|
|
e9acfd |
|
|
|
e9acfd |
if (from_file) {
|
|
|
e9acfd |
size_t tmp_len, op_len = sizeof("65535 255 255 4294967295,");
|
|
|
e9acfd |
- char *tmp_string, *last;
|
|
|
e9acfd |
+ char *tmp_string, *pos, c, c_prev = ' ';
|
|
|
e9acfd |
FILE *fp;
|
|
|
e9acfd |
|
|
|
e9acfd |
tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
|
|
|
e9acfd |
- tmp_string = calloc(1, tmp_len);
|
|
|
e9acfd |
+ tmp_string = pos = calloc(1, tmp_len);
|
|
|
e9acfd |
if (tmp_string == NULL)
|
|
|
e9acfd |
return -ENOMEM;
|
|
|
e9acfd |
|
|
|
e9acfd |
@@ -175,17 +175,33 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
|
|
|
e9acfd |
return -ENOENT;
|
|
|
e9acfd |
}
|
|
|
e9acfd |
|
|
|
e9acfd |
- if (!fgets(tmp_string, tmp_len, fp)) {
|
|
|
e9acfd |
+ while ((c = fgetc(fp)) != EOF) {
|
|
|
e9acfd |
+ switch (c) {
|
|
|
e9acfd |
+ case '\n':
|
|
|
e9acfd |
+ if (c_prev != ',')
|
|
|
e9acfd |
+ *(pos++) = ',';
|
|
|
e9acfd |
+ break;
|
|
|
e9acfd |
+ case ' ':
|
|
|
e9acfd |
+ case '\t':
|
|
|
e9acfd |
+ if (c_prev != ' ')
|
|
|
e9acfd |
+ *(pos++) = c;
|
|
|
e9acfd |
+ break;
|
|
|
e9acfd |
+ default:
|
|
|
e9acfd |
+ *(pos++) = c;
|
|
|
e9acfd |
+ }
|
|
|
e9acfd |
+ if (pos - tmp_string == tmp_len)
|
|
|
e9acfd |
+ break;
|
|
|
e9acfd |
+ c_prev = c;
|
|
|
e9acfd |
+ }
|
|
|
e9acfd |
+
|
|
|
e9acfd |
+ if (!feof(fp)) {
|
|
|
e9acfd |
free(tmp_string);
|
|
|
e9acfd |
fclose(fp);
|
|
|
e9acfd |
- return -EIO;
|
|
|
e9acfd |
+ return -E2BIG;
|
|
|
e9acfd |
}
|
|
|
e9acfd |
|
|
|
e9acfd |
fclose(fp);
|
|
|
e9acfd |
-
|
|
|
e9acfd |
- last = &tmp_string[strlen(tmp_string) - 1];
|
|
|
e9acfd |
- if (*last == '\n')
|
|
|
e9acfd |
- *last = 0;
|
|
|
e9acfd |
+ *pos = 0;
|
|
|
e9acfd |
|
|
|
e9acfd |
*need_release = true;
|
|
|
e9acfd |
*bpf_string = tmp_string;
|
|
|
e9acfd |
--
|
|
|
e9acfd |
1.8.3.1
|
|
|
e9acfd |
|