Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp5495667ybi; Tue, 30 Jul 2019 22:11:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqyOwOsAwZMGUGHN1J3YMR50PukR4bK+T0vprxh4EzqQ+7F6JsV3qWvgfvjgu8lr+iez3v5x X-Received: by 2002:a17:90a:26a1:: with SMTP id m30mr975873pje.59.1564549904119; Tue, 30 Jul 2019 22:11:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564549904; cv=none; d=google.com; s=arc-20160816; b=RVOG7smLMC5yYBEdCT302rqdBDwjwB1Dk8FSR9Y60J4lX3DbzgmOEpoNSRIiVNXhrK N3lgAOOkEMFtcMuPc1XJJSwOu7/1ivox5FFQSee+vRKeJCLLVMArjxSDJawN9mmAClYb XKqal019gekUQ/uoCVBdpYqRhnCqVm5lYZ5xdyu0kttZAUwwqHfbEEMhwy5vdTk4PDKv yCoBQNDn1kQZJqDj52Bl9WswRQcYlaylggQCw6M+Y94r+EDXJIvScZ0UbiYooLI0M3MT sdXmCmMUtZCeM4gQULubDKv9JEvU2V7qkvuQIzmTg+ywBg3XEqbdXYwYf9U3NeFYP9ML vzMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=BApBF9lDyDUHtKy92pMwude8bSQOUkXYIpIspp3IQpA=; b=oPxslT3cWbiGD88om7wqLd6QBYoqzlOhxHtCOhl0E9R7FhzhfctfXMU/9eQrw1Eh2O SnzdupI2O7ousi4dZL3HBy1KGqvppQ8QtNZyCgouULbVnbpeDDwk93qZkrmh8MuQpypG GdrkLdbSM7cCX+CaPFqQ1QiYaO8UCPz0Y4e2TIFx9F+p7OwSKrkQ/XgWQGJGlq1RUxZx Ww+k2GOms0qYR+/u8fW2BYzMFTPrNyIofOfds9Y5NvBO0+zsJxj3flb4Dok0gmy9wCYU snDOwqbQ1FUGhEfaqzlPaSxcw/gyiow6uX7KzBMy24o5r/7srr6GGvIBL2Z8Xtx6PwuB T6jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EQEb+Jko; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 124si31516779pgb.11.2019.07.30.22.11.29; Tue, 30 Jul 2019 22:11:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EQEb+Jko; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726856AbfGaAkh (ORCPT + 99 others); Tue, 30 Jul 2019 20:40:37 -0400 Received: from mail-vk1-f202.google.com ([209.85.221.202]:50695 "EHLO mail-vk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726668AbfGaAkh (ORCPT ); Tue, 30 Jul 2019 20:40:37 -0400 Received: by mail-vk1-f202.google.com with SMTP id p196so28658328vke.17 for ; Tue, 30 Jul 2019 17:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BApBF9lDyDUHtKy92pMwude8bSQOUkXYIpIspp3IQpA=; b=EQEb+JkoCSxT4aLpwLuHjX/vwl8+Y5yqjAP9jWQbUxf5GHiW1S2JZvjEsZ10Q+eLOR 5z29NQVjneEdlpoPyLpPkcqAl5p8cFacARBYXR1z8HOVLzX3dIete7sgazGKuXnoxB+h l3UT4vT6MgU4yQmQpKmSE7xRMogz+slMR76BWuiKwEa7QK3CSn2atU5JtacA6VGRAtWp 1U1OoAXxfAFOc1j8QLZX0Reazdmj6mWykhiRlLBLDGIRE9EWXzUhdjXDqe6LpKfqwzaT VpMcFliKUFgSdABibDpwnSPEJ989GGLRuHH+LBYnGuW6ph4tPiCxHW7oriL4hHPd8gTG j4aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BApBF9lDyDUHtKy92pMwude8bSQOUkXYIpIspp3IQpA=; b=sAO8oyMB2G4U88v4rPKZl+5XytaYm9pmR0aRQELp1BDGU5h5zIi92OPcqMHgPzNus/ qMecDD36w0vnAASdWwdQF3nYd4x6Ck7ujHkMsdHSAexKNcg2NdrNAJ+l4xeUNbY6tcZG suw1BoYo4Jn7DEcbaX5XdmQRPBZtCaz+Ih4pDTvCrxs3qt/vwi21bXrs9AdyaLROZgYi zsydkVD8TDeWOBgfk4i20Hv8JGfjby8BP4DGOJ1sIBjJOH3qcUimPMpaE3sTJSNMS+h9 aDEg0onWmeCMxqPDbKf4KG3rk5NswGEV6tdp9n6AYz9ChZvtqs30Z9zD1XbFroRosjBs mvIw== X-Gm-Message-State: APjAAAVA1Axocz9Cr5XOLCXtSHm4+qpWJFCfdC6SQIApJt8DtDCbDgGy 2tFkp1YWRlkBeLwr8WfvKt9Y3Os2 X-Received: by 2002:a1f:f48f:: with SMTP id s137mr8990000vkh.10.1564533635841; Tue, 30 Jul 2019 17:40:35 -0700 (PDT) Date: Tue, 30 Jul 2019 17:40:32 -0700 In-Reply-To: <20190726192859.GG20482@kernel.org> Message-Id: <20190731004032.74676-1-nums@google.com> Mime-Version: 1.0 References: <20190726192859.GG20482@kernel.org> X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH v2] Fix annotate.c use of uninitialized value error From: Numfor Mbiziwo-Tiapo To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, songliubraving@fb.com, mbd@fb.com Cc: linux-kernel@vger.kernel.org, irogers@google.com, eranian@google.com, Numfor Mbiziwo-Tiapo Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Our local MSAN (Memory Sanitizer) build of perf throws a warning that comes from the "dso__disassemble_filename" function in "tools/perf/util/annotate.c" when running perf record. The warning stems from the call to readlink, in which "build_id_path" was being read into "linkname". Since readlink does not null terminate, an uninitialized memory access would later occur when "linkname" is passed into the strstr function. This is simply fixed by null-terminating "linkname" after the call to readlink. To reproduce this warning, build perf by running: make -C tools/perf CLANG=1 CC=clang EXTRA_CFLAGS="-fsanitize=memory\ -fsanitize-memory-track-origins" (Additionally, llvm might have to be installed and clang might have to be specified as the compiler - export CC=/usr/bin/clang) then running: tools/perf/perf record -o - ls / | tools/perf/perf --no-pager annotate\ -i - --stdio Please see the cover letter for why false positive warnings may be generated. Signed-off-by: Numfor Mbiziwo-Tiapo --- tools/perf/util/annotate.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 70de8f6b3aee..e1b075b52dce 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1627,6 +1627,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil char *build_id_filename; char *build_id_path = NULL; char *pos; + int len; if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) @@ -1655,10 +1656,16 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) dirname(build_id_path); - if (dso__is_kcore(dso) || - readlink(build_id_path, linkname, sizeof(linkname)) < 0 || - strstr(linkname, DSO__NAME_KALLSYMS) || - access(filename, R_OK)) { + if (dso__is_kcore(dso)) + goto fallback; + + len = readlink(build_id_path, linkname, sizeof(linkname) - 1); + if (len < 0) + goto fallback; + + linkname[len] = '\0'; + if (strstr(linkname, DSO__NAME_KALLSYMS) || + access(filename, R_OK)) { fallback: /* * If we don't have build-ids or the build-id file isn't in the -- 2.22.0.709.g102302147b-goog