Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2184981rda; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEP0mrKhWSVGVmg16V8h9U4asiqlpN9FnMwhoavydh5gJLiYXEXxUZrMcjaOlFejcycqGGD X-Received: by 2002:a17:90a:183:b0:27f:bd9e:5a15 with SMTP id 3-20020a17090a018300b0027fbd9e5a15mr5189354pjc.28.1698186291138; Tue, 24 Oct 2023 15:24:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186291; cv=none; d=google.com; s=arc-20160816; b=Q+NYAaTdYXdBbNR3It/ZBPWtkr4mYzccnf7qYaKNtEMCojX7tIrkZpjGxxJvj9eTSV uUD7HBZHvk2WXY99dAlbQe8LIiQGafsVM22IW5nQm6oamdJXShlH0N2da9VPmnzG3rvy /RxN4/F4KfYm2azLvjkXmlrAP4Q7IXbBOZEIgPSq/h7B6pwCu0fTzlR6h++CwHTugzXS Ztp8m3p2ONtDtOPjfLs7DzLlf1670OftH7/Wp186r/tss2OaDBLN/KnwG0uA+tIKc4Ml EN0PWMOQbwOSNr2vz9JKGmbGMyi5fc/RaOWAjENB75MFCOxPTjPDUgyZQcZKDU5eum8k Wp1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=uP1Gj1jo6OfWasXlL19cijcHECfRWs93ftfWWCvTQBVHOwg+94IevQ9kNwyb1F1Rx6 t6dwfTxxvXlaoSyWJCSGHTOxDL8zCYcl21LbQXkKsEMLUvKfX1V63JTeun7oBxurMfS7 oHoiBy8faYyMOxgsi0WbN58JhhW06xBeJ95SbyO2nG/T4zeIQr9GgKyJ4KhsbgPAiH/0 lRJD7Gu4orlGdN8wB5eaLV9Q0V6029c49UxBkJ2GZnqvRRzYi4QrIdOHE2BsWhBvr2UV S+V4Nlux+kV948bj3dCIPn6QAx7F/+TxdEaJKBkSBlAQ5tpOFjj37SpVlEh5Jtg9lQyR 8Y5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VAYKbecQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id mq12-20020a17090b380c00b00277517b42dasi9428086pjb.35.2023.10.24.15.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:24:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VAYKbecQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 063BB802A9C3; Tue, 24 Oct 2023 15:24:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344446AbjJXWYY (ORCPT + 99 others); Tue, 24 Oct 2023 18:24:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344431AbjJXWYU (ORCPT ); Tue, 24 Oct 2023 18:24:20 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 490E010CB for ; Tue, 24 Oct 2023 15:24:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da040c021aeso1140818276.3 for ; Tue, 24 Oct 2023 15:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186257; x=1698791057; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; b=VAYKbecQNlVTg/B52zPDUaaiURDyjiKy7S+6RIJOxJpJ0eHhouwdTSRRBJ27EJULRu dxqgDSMBuegaaEh2V9wyGFRlyBqDnas82lSaTbvD97QErmN4VbCrwd0kjJRS7TO3x9FG tVNhfEvHEB2Z/CQ6oMCpKUWDBVFqU090UkqLf+jv2vfjskvOUE8FlHbCKTT2mZsnQ+CN kegQ5T4b6AhmuMBRT431gSOe7l5kezCOzc/yHc/ZJmidQMGqyLqsQAIlmn4DT6WXgMhg osWbRexaBrpnCVGgOf22q7eEulXwowZ8Lmy7vwtcI9C+cz0kdMMFoWER/jYCV8iUL5cC 4KLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186257; x=1698791057; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=soqORP6tbK7HSPMdiExWiSFnufIqC4NXz+eAMSjew4k=; b=sleI/+GYK6CKPllGJMxQ6AGn6XD0RtnpwbU9MtV8TxctYRR+gYO/0sCv64cjDnbHJG q/DdNpSJxKKrN/Qb9WzLtbnELFZrzPRixKa7VYrgKzn0kTCnYlteQ80SVlkvvd9UT1qu IXUCMmP/sJwUTVN2Tqgrz7GRGWRs3Bhy2kmTWlNRthv4NdVZjSmYym1/ivzbyUvmw+Il rpQYIoL8VgBZ0ZQ3zYUd4loBgSt9D/tVBQ38/Z/tN27E+4JeCy7PzGwplp++6gKHdfBC s68wspFjR7R+RVL5TLlVNCK0m0bUlgVcm22AY3w4U1P+sYLaqBFNILhcIvA0Z7DTFzUP 9Law== X-Gm-Message-State: AOJu0YyFE3VJTigjuMj6UgYGtr41NybZjrb6Xqw/nh5aoT7oNMCafwpS GuKtHNJTv1e4hf7a8Tq5VOgA7dJ8rPZS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:324b:0:b0:d9b:f88c:ca2b with SMTP id y72-20020a25324b000000b00d9bf88cca2bmr258271yby.11.1698186257401; Tue, 24 Oct 2023 15:24:17 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:05 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-3-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 02/50] perf machine: Avoid out of bounds LBR memory read From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:24:47 -0700 (PDT) Running perf top with address sanitizer and "--call-graph=lbr" fails due to reading sample 0 when no samples exist. Add a guard to prevent this. Fixes: e2b23483eb1d ("perf machine: Factor out lbr_callchain_add_lbr_ip()") Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index addfae2f63ef..e0e2c4a943e4 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2622,16 +2622,18 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread, save_lbr_cursor_node(thread, cursor, i); } - /* Add LBR ip from first entries.to */ - ip = entries[0].to; - flags = &entries[0].flags; - *branch_from = entries[0].from; - err = add_callchain_ip(thread, cursor, parent, - root_al, &cpumode, ip, - true, flags, NULL, - *branch_from); - if (err) - return err; + if (lbr_nr > 0) { + /* Add LBR ip from first entries.to */ + ip = entries[0].to; + flags = &entries[0].flags; + *branch_from = entries[0].from; + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + true, flags, NULL, + *branch_from); + if (err) + return err; + } return 0; } -- 2.42.0.758.gaed0368e0e-goog