Skip to content

Commit aa52bcb

Browse files
Jiri Olsaborkmann
authored andcommitted
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, but as per Jakub suggestion returning -1 on allocation failure so we do the best effort to propagate the error. Fixes: 107f041 ("tools: bpftool: add JSON output for `bpftool prog dump jited *` command") Reported-by: Michael Petlan <mpetlan@redhat.com> Signed-off-by: Jiri Olsa <jolsa@kernel.org> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent ba95c74 commit aa52bcb

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

tools/bpf/bpftool/jit_disasm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Licensed under the GNU General Public License, version 2.0 (GPLv2)
1212
*/
1313

14+
#define _GNU_SOURCE
15+
#include <stdio.h>
1416
#include <stdarg.h>
1517
#include <stdint.h>
1618
#include <stdio.h>
@@ -44,11 +46,13 @@ static int fprintf_json(void *out, const char *fmt, ...)
4446
char *s;
4547

4648
va_start(ap, fmt);
49+
if (vasprintf(&s, fmt, ap) < 0)
50+
return -1;
51+
va_end(ap);
52+
4753
if (!oper_count) {
4854
int i;
4955

50-
s = va_arg(ap, char *);
51-
5256
/* Strip trailing spaces */
5357
i = strlen(s) - 1;
5458
while (s[i] == ' ')
@@ -61,11 +65,10 @@ static int fprintf_json(void *out, const char *fmt, ...)
6165
} else if (!strcmp(fmt, ",")) {
6266
/* Skip */
6367
} else {
64-
s = va_arg(ap, char *);
6568
jsonw_string(json_wtr, s);
6669
oper_count++;
6770
}
68-
va_end(ap);
71+
free(s);
6972
return 0;
7073
}
7174

0 commit comments

Comments
 (0)