From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F8A8C0650E for ; Thu, 4 Jul 2019 08:59:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 545762189E for ; Thu, 4 Jul 2019 08:59:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562230740; bh=eWfUFK2rumsdI3sZ6IVoNVpTFOlbyD9a1DcPiJSxtBA=; h=From:To:Cc:Subject:Date:List-ID:From; b=iQbxa9VaOlD5litGk3e3lrce+KP0Sb2kupgIqhW4WTRBs1NJEQTThU4gJHBzgNjFT RtJnIubTn7LYGCw0uXvZnMbk3EILAuJeUPH09gP8lOucC0IZf6EW5e18rgIyFs6GgZ YZwWxax+RfnoNi7mgOIaYOH47+01B15uL68lBARc= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727095AbfGDI7A (ORCPT ); Thu, 4 Jul 2019 04:59:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:17397 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726993AbfGDI67 (ORCPT ); Thu, 4 Jul 2019 04:58:59 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2CD8530BCCE2; Thu, 4 Jul 2019 08:58:59 +0000 (UTC) Received: from krava.brq.redhat.com (unknown [10.43.17.81]) by smtp.corp.redhat.com (Postfix) with ESMTP id 903B888F04; Thu, 4 Jul 2019 08:58:57 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: Michael Petlan , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau Subject: [PATCH] tools bpftool: Fix json dump crash on powerpc Date: Thu, 4 Jul 2019 10:58:56 +0200 Message-Id: <20190704085856.17502-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 04 Jul 2019 08:58:59 +0000 (UTC) Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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. Reported-by: Michael Petlan Signed-off-by: Jiri Olsa --- tools/bpf/bpftool/jit_disasm.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index 3ef3093560ba..05fa6dc970f8 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -11,6 +11,8 @@ * Licensed under the GNU General Public License, version 2.0 (GPLv2) */ +#define _GNU_SOURCE +#include #include #include #include @@ -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; } -- 2.21.0