Skip to contentSkip to conversation

lib/bpf: Fix bytecode-file parsing

PATCH In review Phil Sutter 3 participants 5 messages Aug 29, 2017 – Sep 4, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/bpf: Fix bytecode-file parsing

The signedness of char type is implementation dependent, and there are architectures on which it is unsigned by default. In that case, the check whether fgetc() returned EOF failed because the return value was assigned an (unsigned) char variable prior to comparison with EOF (which is defined to -1). Fix this by using int as type for 'c' variable, which also matches the declaration of fgetc().

While being at it, fix the parser logic to correctly handle multiple empty lines and consecutive whitespace and tab characters to further improve the parser's robustness. Note that this will still detect double separator characters, so doesn't soften up the parser too much.

Fixes 3da3ebfca85b8 ("bpf: Make bytecode-file reading a little more robust")
Cc Daniel Borkmann <[email protected]>
---
 lib/bpf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
lib/bpf.c +5 -2
index 0bd0a95eafe6c..77eb8ee27114f 100644
@@ -208,8 +208,9 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
 
 	if (from_file) {
 		size_t tmp_len, op_len = sizeof("65535 255 255 4294967295,");
-		char *tmp_string, *pos, c, c_prev = ' ';
+		char *tmp_string, *pos, c_prev = ' ';
 		FILE *fp;
+		int c;
 
 		tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
 		tmp_string = pos = calloc(1, tmp_len);
@@ -228,18 +229,20 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
 			case '\n':
 				if (c_prev != ',')
 					*(pos++) = ',';
+				c_prev = ',';
 				break;
 			case ' ':
 			case '\t':
 				if (c_prev != ' ')
 					*(pos++) = c;
+				c_prev = ' ';
 				break;
 			default:
 				*(pos++) = c;
+				c_prev = c;
 			}
 			if (pos - tmp_string == tmp_len)
 				break;
-			c_prev = c;
 		}
 
 		if (!feof(fp)) {
Signature · 1 line
2.13.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Phil Sutter

The signedness of char type is implementation dependent, and there are architectures on which it is unsigned by default. In that case, the check whether fgetc() returned EOF failed because the return value was assigned an (unsigned) char variable prior to comparison with EOF (which is defined to -1). Fix this by using int as type for 'c' variable, which also matches the declaration of fgetc().

Quoted 8 lines

While being at it, fix the parser logic to correctly handle multiple empty lines and consecutive whitespace and tab characters to further improve the parser's robustness. Note that this will still detect double separator characters, so doesn't soften up the parser too much.

Fixes 3da3ebfca85b8 ("bpf: Make bytecode-file reading a little more robust")
Cc Daniel Borkmann <[email protected]>

Definitely ack on the EOF bug:

[...]

Quoted 9 lines
lib/bpf.c +1 -0
@@ -228,18 +229,20 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
  			case '\n':
  				if (c_prev != ',')
  					*(pos++) = ',';
+				c_prev = ',';
  				break;
  			case ' ':
  			case '\t':
  				if (c_prev != ' ')

lib/bpf.c static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len, · line 242

Quoted 10 lines
lib/bpf.c +2 -1
  					*(pos++) = c;
+				c_prev = ' ';
  				break;
  			default:
  				*(pos++) = c;
+				c_prev = c;
  			}
  			if (pos - tmp_string == tmp_len)
  				break;
-			c_prev = c;

I don't really have a strong opinion on this, but the logic for normalizing here is getting a bit convoluted. Is your use case for making the parser more robust mainly so you can just use the -ddd output from tcpdump for cBPF w/o piping through tr? But even that shouldn't give multiple empty lines afaik, no?

#2 lore raw

Hi Daniel,

Quoted 1 line · Daniel Borkmann

On 08/29/2017 05:09 PM, Phil Sutter wrote:

[...]

Quoted 17 lines
Quoted 17 lines
lib/bpf.c +3 -0
@@ -228,18 +229,20 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
  			case '\n':
  				if (c_prev != ',')
  					*(pos++) = ',';
+				c_prev = ',';
  				break;
  			case ' ':
  			case '\t':
  				if (c_prev != ' ')
  					*(pos++) = c;
+				c_prev = ' ';
  				break;
  			default:
  				*(pos++) = c;
+				c_prev = c;
  			}
  			if (pos - tmp_string == tmp_len)
Quoted 8 lines

lib/bpf.c static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len, · line 242

Quoted 2 lines
lib/bpf.c +0 -1
  				break;
-			c_prev = c;

I don't really have a strong opinion on this, but the logic for normalizing here is getting a bit convoluted. Is your use case for making the parser more robust mainly so you can just use the -ddd output from tcpdump for cBPF w/o piping through tr? But even that shouldn't give multiple empty lines afaik, no?

Well, using tcpdump output was functional before already. I just noticed that if I add an empty line to the end of bytecode-file, it will fail and I didn't like that. Then while searching for the EOF issue, I noticed that the parser logic above is a bit faulty in that it will treat different characters equally but doesn't make sure c_prev will be assigned only one of them. So apart from the added robustness, it really fixes an inconsistency in the parsing logic.

Cheers, Phil

#3 lore raw
Quoted 2 lines · Phil Sutter
Quoted 1 line · Daniel Borkmann

On 08/29/2017 05:09 PM, Phil Sutter wrote:

[...]

Quoted 7 lines
Quoted 6 lines

I don't really have a strong opinion on this, but the logic for normalizing here is getting a bit convoluted. Is your use case for making the parser more robust mainly so you can just use the -ddd output from tcpdump for cBPF w/o piping through tr? But even that shouldn't give multiple empty lines afaik, no?

Quoted 7 lines

Well, using tcpdump output was functional before already. I just noticed that if I add an empty line to the end of bytecode-file, it will fail and I didn't like that. Then while searching for the EOF issue, I noticed that the parser logic above is a bit faulty in that it will treat different characters equally but doesn't make sure c_prev will be assigned only one of them. So apart from the added robustness, it really fixes an inconsistency in the parsing logic.

Ok, fine by me.

#4 lore raw
In reply to Phil Sutter
Quoted 7 lines · On Tue, 29 Aug 2017 17:09:45 +0200 Phil Sutter <[email protected]> wrote:

The signedness of char type is implementation dependent, and there are architectures on which it is unsigned by default. In that case, the check whether fgetc() returned EOF failed because the return value was assigned an (unsigned) char variable prior to comparison with EOF (which is defined to -1). Fix this by using int as type for 'c' variable, which also matches the declaration of fgetc().

Quoted 8 lines

While being at it, fix the parser logic to correctly handle multiple empty lines and consecutive whitespace and tab characters to further improve the parser's robustness. Note that this will still detect double separator characters, so doesn't soften up the parser too much.

Fixes 3da3ebfca85b8 ("bpf: Make bytecode-file reading a little more robust")
Cc Daniel Borkmann <[email protected]>

Looks fine applied.

Although I think only Android is using unsigned for char type at this point.