Skip to contentSkip to conversation

tools bpftool: Fix json dump crash on powerpc

PATCH In review Jiri Olsa 5 participants 8 messages Jul 4, 2019 – Jul 9, 2019

Subsystems (4)
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 tools bpftool: Fix json dump crash on powerpc

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, so there's no point to return error in case of allocation failure.

---
 tools/bpf/bpftool/jit_disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/bpf/bpftool/jit_disasm.c +7 -4
index 3ef3093560ba..05fa6dc970f8 100644
@@ -11,6 +11,8 @@
  * Licensed under the GNU General Public License, version 2.0 (GPLv2)
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
 #include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -44,11 +46,13 @@ static int fprintf_json(void *out, const char *fmt, ...)
 	char *s;
 
 	va_start(ap, fmt);
+	if (vasprintf(&s, fmt, ap) < 0)
+		return 0;
+	va_end(ap);
+
 	if (!oper_count) {
 		int i;
 
-		s = va_arg(ap, char *);
-
 		/* Strip trailing spaces */
 		i = strlen(s) - 1;
 		while (s[i] == ' ')
@@ -61,11 +65,10 @@ static int fprintf_json(void *out, const char *fmt, ...)
 	} else if (!strcmp(fmt, ",")) {
 		   /* Skip */
 	} else {
-		s = va_arg(ap, char *);
 		jsonw_string(json_wtr, s);
 		oper_count++;
 	}
-	va_end(ap);
+	free(s);
 	return 0;
 }
 
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw

2019-07-04 10:58 UTC+0200 ~ Jiri Olsa <[email protected]>

Quoted 24 lines

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Quoted 6 lines

Please note that libopcodes code does not check return values from fprintf callback, so there's no point to return error in case of allocation failure.

Looks good to me, thank you for the fix!

#2 lore raw
In reply to Jiri Olsa
Quoted 20 lines · Jiri Olsa

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)
Quoted 7 lines

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, so there's no point to return error in case of allocation failure.

Well, it doesn't check it today, it may perhaps do it in the future? Let's flip the question - since it doesn't check it today, why not propagate the error? :) We should stay close to how fprintf would behave, IMHO.

Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")
Quoted 2 lines
#3 lore raw

PATCH v2 1/1 tools bpftool: Fix json dump crash on powerpc

Quoted 29 lines · Jakub Kicinski
Quoted 20 lines · Jiri Olsa

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)
Quoted 7 lines

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, so there's no point to return error in case of allocation failure.

Quoted 6 lines

Well, it doesn't check it today, it may perhaps do it in the future? Let's flip the question - since it doesn't check it today, why not propagate the error? :) We should stay close to how fprintf would behave, IMHO.

Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")

ok fair enough, v2 attached

thanks,
jirka

---
Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error.

---
 tools/bpf/bpftool/jit_disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/bpf/bpftool/jit_disasm.c +7 -4
index 3ef3093560ba..bfed711258ce 100644
@@ -11,6 +11,8 @@
  * Licensed under the GNU General Public License, version 2.0 (GPLv2)
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
 #include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -44,11 +46,13 @@ static int fprintf_json(void *out, const char *fmt, ...)
 	char *s;
 
 	va_start(ap, fmt);
+	if (vasprintf(&s, fmt, ap) < 0)
+		return -1;
+	va_end(ap);
+
 	if (!oper_count) {
 		int i;
 
-		s = va_arg(ap, char *);
-
 		/* Strip trailing spaces */
 		i = strlen(s) - 1;
 		while (s[i] == ' ')
@@ -61,11 +65,10 @@ static int fprintf_json(void *out, const char *fmt, ...)
 	} else if (!strcmp(fmt, ",")) {
 		   /* Skip */
 	} else {
-		s = va_arg(ap, char *);
 		jsonw_string(json_wtr, s);
 		oper_count++;
 	}
-	va_end(ap);
+	free(s);
 	return 0;
 }
 
Signature · 1 line
2.21.0
Reviewed-by Quentin Monnet Reviewed-by on v2 1/1
#4 lore raw
Quoted 24 lines · Jiri Olsa

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Quoted 6 lines

Please note that libopcodes code does not check return values from fprintf callback, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error.

Thanks, let me repost all the tags (Quentin, please shout if you're not ok with this :)):

Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")
#5 lore raw

2019-07-05 10:24 UTC-0700 ~ Jakub Kicinski <[email protected]>

Quoted 29 lines · Jiri Olsa
Quoted 28 lines

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error.

Quoted 5 lines
Quoted 2 lines

Thanks, let me repost all the tags (Quentin, please shout if you're not ok with this :)):

I confirm it's all good for me, thanks :)

Quoted 5 lines
Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")
maintainer
#6 lore raw
Quoted 30 lines · Quentin Monnet

2019-07-05 10:24 UTC-0700 ~ Jakub Kicinski <[email protected]>

Quoted 29 lines · Jiri Olsa
Quoted 28 lines

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error.

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines

Thanks, let me repost all the tags (Quentin, please shout if you're not ok with this :)):

I confirm it's all good for me, thanks :)

Quoted 3 lines
Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")

Given merge window coming up, I've applied this to bpf-next, thanks everyone!

P.s.: Jiri, please repost full/proper patch next time instead of inline reply.

#7 lore raw
Quoted 39 lines · Daniel Borkmann
Quoted 38 lines · Quentin Monnet

2019-07-05 10:24 UTC-0700 ~ Jakub Kicinski <[email protected]>

Quoted 29 lines · Jiri Olsa
Quoted 28 lines

Michael reported crash with by bpf program in json mode on powerpc:

  # bpftool prog -p dump jited id 14
  [{
        "name": "0xd00000000a9aa760",
        "insns": [{
                "pc": "0x0",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x4",
                "operation": "nop",
                "operands": [null
                ]
            },{
                "pc": "0x8",
                "operation": "mflr",
  Segmentation fault (core dumped)

The code is assuming char pointers in format, which is not always true at least for powerpc. Fixing this by dumping the whole string into buffer based on its format.

Please note that libopcodes code does not check return values from fprintf callback, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error.

Quoted 5 lines
Quoted 2 lines

Thanks, let me repost all the tags (Quentin, please shout if you're not ok with this :)):

I confirm it's all good for me, thanks :)

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Fixes 107f041212c1 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command")

Given merge window coming up, I've applied this to bpf-next, thanks everyone!

P.s.: Jiri, please repost full/proper patch next time instead of inline reply.

will do, thanks
jirka