naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

Blame SOURCES/0005-bpf-Make-bytecode-file-reading-a-little-more-robust.patch

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