]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
tools: bpftool: Fix json dump crash on powerpc
authorJiri Olsa <jolsa@redhat.com>
Fri, 5 Jul 2019 12:10:31 +0000 (14:10 +0200)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 14 Aug 2019 09:18:49 +0000 (11:18 +0200)
BugLink: https://bugs.launchpad.net/bugs/1839036
[ Upstream commit aa52bcbe0e72fac36b1862db08b9c09c4caefae3 ]

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: 107f041212c1 ("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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
tools/bpf/bpftool/jit_disasm.c

index 1551d3918d4cdc7c73e9b0e5f952740b6c6d40b9..86376f923d28568eadb254c18d26d4eee451baf8 100644 (file)
@@ -10,6 +10,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>
@@ -51,11 +53,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] == ' ')
@@ -68,11 +72,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;
 }