Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp3213955lqo; Tue, 21 May 2024 09:53:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPBeawWpHplg1BLIVnZ8bs1p2xXO4lsc+dIlJqMSOODK9gbuFt7DzTGyGz03u8AKY1srIKRX9OufSV0tL+SEu72j1BJfhbQDUlvLLdkQ== X-Google-Smtp-Source: AGHT+IFcOv48TbafQb7etorryM7wAvYgtLshDqwilu73l0RXq5Ru4kB45UkZ20gh0kNy90xQa7sG X-Received: by 2002:a17:902:dac8:b0:1f2:f4c8:e0d5 with SMTP id d9443c01a7336-1f2f4c93179mr108122855ad.0.1716310411405; Tue, 21 May 2024 09:53:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716310411; cv=pass; d=google.com; s=arc-20160816; b=Cd9gVcoaYcU8fKn2nqOUYOBePGdk//a3MfxVt1IReLyC33+DA+hHbs80C5kHWi3GFW lxkxlfwy7R75DZE+lBsJu4eLZUCNS8h9GzKKe+aZCV6rZOsAkE1rlRX322vTerNJtNYZ mULC1FeW42spx6PYjxCvWXWJuKhFRNlJdnVlLjdeNh5hsa900hxpJUmYHVDFwjo443Kj HX81DDOFXwrvipHWuKFDyQSIrTPH07Xh78CroRwKPcig9EEhagtN/fY78zeT+0oUXpu8 urJ1WPJ331k40MF3979GS8ddGFaIkrF7qpBmUWLuxlst3+kZtevzhf6SD1QFrVK3K4HZ vvfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=kUfTujR33N1wGIzFWyd3IGvA2YqCHOAb96ZV/yApJRQ=; fh=5Npu/5CNVTzchQKgdEzKJztYNla8k/BRiBL8Ftr9+kY=; b=Foio3nIILGSCfRtIUFIZQP4jQZL4hXbrEw3KHyQ78ICaTWBZWMMoiyKgIoEFE3ta2i ICRsVJr1+rkCkkOQZVwU83YIpowTW/XwdBELmxUKEMLPtutJZ7qV+mC0/Y6VDNHih4+p LGOnlcOvWe0kAmQRN/KGc2B28AyaWi0CV6TfnLGBnFbfOVnIc6/d+T18KxgUOmrbcX+y xsKGNd/WqNXWJB0stYoiIFPq8ZV7Q3fJlJdjiRW91sxQvkKUS3T904KP7/IDkMVbhUsd 1aIB12nnjPfG+YohA0EyO/LoY4CA0XFk5/RHciJQpRYLENmOD7FSPUGK+vz6Jqb6eCl+ 3iVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="kqfsDjR/"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-185232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185232-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f307dc0058si17335715ad.448.2024.05.21.09.53.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 09:53:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="kqfsDjR/"; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-185232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185232-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A7785B226BB for ; Tue, 21 May 2024 16:51:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A65DD1482F1; Tue, 21 May 2024 16:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kqfsDjR/" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 644DB3B1A1 for ; Tue, 21 May 2024 16:51:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716310282; cv=none; b=alO0p4XV0E/lNcQCtnadKNTt6ArK76y2Po53hTYMECuIOySYwJfHAdudiehytnSaOxviqSNTLmKp6jQ+Hrx+8Ap2kpisDQu9CqZUkhaTdf9DukrR+nK6GyFLiWoDpMcrrnpcIV8ZRQ9mVcdR63KswzD2o0mIsATZDu6PVeCfntU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716310282; c=relaxed/simple; bh=CQhX3rHzsitnHPfXKQHoTLwMmtFJKqDCMNaUjS6EnNA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=YjuPQ4vEkJQQadb6wBi0wgvTXTBeWrWsz0hVAK4vsl1vBBF3usI8d1qFyVcLpPNNHKbcQRks9gOirovhtMLSF86Hb9NCfjitOyGAHNfyOVG+n7P1dmNEx95LDl1sN/3F5J8C5+ncs2L8lCuEl//ekjXkeorXKBe5McJrL77/nkk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kqfsDjR/; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc647f65573so28252138276.2 for ; Tue, 21 May 2024 09:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716310280; x=1716915080; 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=kUfTujR33N1wGIzFWyd3IGvA2YqCHOAb96ZV/yApJRQ=; b=kqfsDjR/2+71WblNqgOHbu6WvgolPFFZXzwadOTz+ixKpdE1fmO8LljJVw4PvgZLM+ eA8vpInbpFeTvBaVBrdb9OiXiGDk10Wy7LJeNv4lb/k7+RzcbSJi7StK/WQxKA2A4Sxo K6ifexhv/tjEQZ37Vvm31UGFWfxmIPwJHutOpUvegUgBn6fzZu6UCFEQGhrbo/hgI3CX 3JV4KdYM8R/BWxH3kdmmWPNeGMoggqPudhuiLkLbIcUaZ/BnMr/CMXRxe3vBWlHiFFfT Aa55AlMn5YlFfYu05io32p3339wr0T7PFj9cfRYd70wl0ux8GV2zBQFsS2K5qZB7/N7g GsNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716310280; x=1716915080; 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=kUfTujR33N1wGIzFWyd3IGvA2YqCHOAb96ZV/yApJRQ=; b=cnSn79econr+YT5nZgUDFFo+QL8X/SSRIuVNrJfH+ZHSO0bCZSJWYUWYb2JDH38YVa C4xT0xQeQXjBJ+g9YZCAdIPnHR4+d74duvweQxBqBDy23XfOQLZGDNr1++HGkZVubnwF fvX7MHRAcPG59viQpkInlw5T31VxgugjpdKo/tB+wBTeUHMWAiVHzTTauMx+wfjroWu9 ifJgglbWIurVe9Qbvqaq0/iV9ax5eWseXgvl06ZMtg6I+6IthFn27H4YcvZxMXcFnj3s qHnbi6zVsc9Fo2bBZaa6B2Sj81rav0mFQ1H6QSgDihhJCF7yxKr3t+PGN2YXlubrwmu3 xNkQ== X-Forwarded-Encrypted: i=1; AJvYcCX7mYEy6OZrmB3K0lrBn9jYV9jpdtdN0It6b39Q6p663UTvG6wQQD8sPR1rS87hMHD3kg5NpXv0fLBdw/Y8L0I9T2uKocvS7kH4bRvy X-Gm-Message-State: AOJu0YwkMn1BUz8+HaYZJ9vNt5BHF+DtxpVBVXaVS+yoQfYGPvblH8AT dIfsJDJQYFdSM9P24Vju1i8/OPL4fhBzYp4jUumyFO4NwU98nAAOuKEuWnZOEJqH3txleTbFm1S diRerxA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:8533:b29a:936d:651a]) (user=irogers job=sendgmr) by 2002:a25:7486:0:b0:de5:2325:72a1 with SMTP id 3f1490d57ef6-dee4f192850mr8133573276.4.1716310280333; Tue, 21 May 2024 09:51:20 -0700 (PDT) Date: Tue, 21 May 2024 09:51:07 -0700 In-Reply-To: <20240521165109.708593-1-irogers@google.com> Message-Id: <20240521165109.708593-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240521165109.708593-1-irogers@google.com> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Subject: [PATCH v1 1/3] perf maps: Fix use after free in __maps__fixup_overlap_and_insert From: Ian Rogers To: "Steinar H . Gunderson" , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" In the case 'before' and 'after' are broken out from pos, maps_by_address may be changed by __maps__insert, as such it needs re-reading. Don't ignore the return value from __maps_insert. Fixes: 659ad3492b91 ("perf maps: Switch from rbtree to lazily sorted array for addresses") Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 16b39db594f4..eaada3e0f5b4 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -741,7 +741,6 @@ static unsigned int first_ending_after(struct maps *maps, const struct map *map) */ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) { - struct map **maps_by_address; int err = 0; FILE *fp = debug_file(); @@ -749,12 +748,12 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) if (!maps__maps_by_address_sorted(maps)) __maps__sort_by_address(maps); - maps_by_address = maps__maps_by_address(maps); /* * Iterate through entries where the end of the existing entry is * greater-than the new map's start. */ for (unsigned int i = first_ending_after(maps, new); i < maps__nr_maps(maps); ) { + struct map **maps_by_address = maps__maps_by_address(maps); struct map *pos = maps_by_address[i]; struct map *before = NULL, *after = NULL; @@ -821,8 +820,10 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) /* Maps are still ordered, go to next one. */ i++; if (after) { - __maps__insert(maps, after); + err = __maps__insert(maps, after); map__put(after); + if (err) + goto out_err; if (!maps__maps_by_address_sorted(maps)) { /* * Sorting broken so invariants don't @@ -851,7 +852,7 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) check_invariants(maps); } /* Add the map. */ - __maps__insert(maps, new); + err = __maps__insert(maps, new); out_err: return err; } -- 2.45.0.rc1.225.g2a3ae87e7f-goog