Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10417272ybi; Wed, 24 Jul 2019 22:51:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqzHL6QvGhRkqPsLeaKlzbGV2ktyIic97L8YOGLxjMDSewoHtOFQjBou6kSO7VyzaOwYfmPJ X-Received: by 2002:a17:902:4222:: with SMTP id g31mr13957270pld.41.1564033873528; Wed, 24 Jul 2019 22:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564033873; cv=none; d=google.com; s=arc-20160816; b=J+2sssH49eqjHhojyIgmgU1XlY3wI2OQrK9Xanl2/AdsAWQNZRZqQM32E7EBbKPsba C1vT8p5jmeTdZSbfr6IPZlN5XpdLxr1fncFGz1wyqGeNAG/OYf0k4Ot/Xgymp6Q6KD2z 6KQsBPt1Dom3YPSraoCIsWy7D/YY6dJ8l7x8mVe7lmbfyp9DIBZKxRXC4DS/WM8PEKL4 kioP5Gt/q8L+INz3CauNy8F6ImheSnsGqFNac8llAPGILAb24HaBekniztNHY2KddhNp ydYrNESXWRU1IK8r0PMtVy5SjmbOtLIUrmkTtoJNK2tCkahPJO7n8lDphnNboZVTs5ck 6DPA== 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=MwRccX2MBD2s53mippQSnbtUA99Ul/BghITn/o0J/7s=; b=VgF5Yvo2oF4U/BHpk1OPVE1y5u69SWMNsIs8qvgTvtSvYRyjLv8mbYapoA+w6KbBdM nJz5pYa4b74qbVRnxR4glQACX3AQ9xV8OCbTIZo9cLECT7NONmdY5l+vSGYOpRykS9J9 Unw7F6PGevzn1Qdloia25b3IsT1YKLd4e7zthBoA3sRfgMM+/tqJrfecosnDc1MmEk34 3xKdEvEGUEr3gXKLfETL8KracoiGhvt1xncmvBowtiFdSVprmG83Gpv5BONC5KsynlMK 5rXay04JEygNqnfjX6bsxwHF31KnfJ68JpkPkUECQZhVN9mZa6Zn6fyHtTgIo04xNAVA RItw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=XXwWy+39; 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 h5si15558774pls.280.2019.07.24.22.50.58; Wed, 24 Jul 2019 22:51:13 -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=XXwWy+39; 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 S2388020AbfGXXpN (ORCPT + 99 others); Wed, 24 Jul 2019 19:45:13 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:55823 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387818AbfGXXpJ (ORCPT ); Wed, 24 Jul 2019 19:45:09 -0400 Received: by mail-pf1-f202.google.com with SMTP id i26so29528733pfo.22 for ; Wed, 24 Jul 2019 16:45:09 -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=MwRccX2MBD2s53mippQSnbtUA99Ul/BghITn/o0J/7s=; b=XXwWy+39F2zSYwp+44oymQtRU9nfFSYpzoyOW3zyX87oNyHUSpeayXgtkJDadzqAKj xw0YEiesKneQ/x27MqvOSqM81x/32LffuS8z8PQIT8CEilihzPKm1GoA052p698xdVrT iSkjwFx1v2YstV0a8sUkjWm9RLInR5TgKMcaIsMJcLLTXK7CkyBTMD4QhW9DxFfXjpxm WFn0xRaLgZlWqwKD1DVYu2wXEXaUpOzr85lYBz9Rtq/bk6M26ASqX0OWsGHsHpj8Vqz5 3zHQ00GMq4GV4l7OmHFkZvtEW4COcOudGLDNFk6z1X+KmpmJJP22tRBq3LQHIO2D6rNz fxEA== 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=MwRccX2MBD2s53mippQSnbtUA99Ul/BghITn/o0J/7s=; b=J5RcLSpb8q9wNcAkNM+pNNPaIE8WCBZhNoymRRAmJz/Ia9gMJ3MlGd/JYEMc5xAafj /oCkc8Od2BwJxoEavTqDFkJHwwJ5sjueBKvVG9KQo5Mi4f8qXt1MmxENxPaDerXteh8g FM61wwCL3JTXm6zy9zBg0x+JT+uZW0gPfGz/J59aJ/2In11eJ1RutRrItRvKV+VFaaBV QRWc9KZpoR0KwXToLLgZVK7aVsQYsWCuxcLkWBghqMkhU9s0Nv8Bxa8Q2JcS88damo7s VZhzdlZq0Uu61G8CpLqPmDVoDYqRtf0EYdBv5M2bs59u42+Ln3M/RK78WhbkelZyh9Xi svjQ== X-Gm-Message-State: APjAAAVtuAi4WIA0fLVfPX9knjY0Oebx8SZQZuUvUfk68NyItiwwCQFN rBLnB64chYBYsuyZs7R3aV32P1Ip X-Received: by 2002:a65:57ca:: with SMTP id q10mr86291825pgr.52.1564011908747; Wed, 24 Jul 2019 16:45:08 -0700 (PDT) Date: Wed, 24 Jul 2019 16:44:59 -0700 In-Reply-To: <20190724234500.253358-1-nums@google.com> Message-Id: <20190724234500.253358-3-nums@google.com> Mime-Version: 1.0 References: <20190724234500.253358-1-nums@google.com> X-Mailer: git-send-email 2.22.0.657.g960e92d24f-goog Subject: [PATCH 2/3] 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..d8bfb561bc35 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)); + 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.657.g960e92d24f-goog