From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 19B0431196D for ; Wed, 19 Nov 2025 12:33:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763555615; cv=none; b=SwUTIpefj9T2wCBamVChcQBwvNmBeGuMiquHuuk5TrFLcU8xSOGZ2Y4sk9JPKRICsGqwMcrWuRe+VD9v41/vJIhlp2G/Dka01hTm41s5i7d68/CZvj/aHYBBCOPx3OeswYoenPMATQsyfUIO9yViqDMqgwjh2JM/ubZr1ZwHHC4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763555615; c=relaxed/simple; bh=GFLtMROpQw9A+PEPUreRaLxQRhO+LyU0XUmlP/JZmBw=; h=Message-ID:Date:MIME-Version:Subject:To:References:Cc:From: In-Reply-To:Content-Type; b=BAGYgvPMnqHUhYVPrRbHl221swtYYZz+rXq77F+3QWwwYzGKYOXFvi6kbt4WLNeVHn1CLuain8PeUgfcmzIx4UZOzcLeNJMMi641x7W9HKegAAx+5yZgS9fWByc2qcp9Yxv+tC+0iAHiUO3OL+9d8WhqLZuwI5PkNPru9WbkVIQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=QSpa9RMk; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="QSpa9RMk" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-42b387483bbso5359104f8f.1 for ; Wed, 19 Nov 2025 04:33:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1763555611; x=1764160411; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=tVOK/gZwGH/qAUHEfz1NMpjUaPUGidGp5/ImMSGMGpc=; b=QSpa9RMk2Gawp1omHaxLWpUlbRDwxPSY7OBZsnsiJJdGj59IB4vILkfOA0L85qnnTR TnNGaUCbX7hIxsQQSb1Sl1NbNoOT3fL+bHnsGNuT27kxEvygBUZXTUZf9vkUCTXaZ4wT JyWaEK0VIdP5l3oZnL1N5dd0KXDTLFtdiaGFiViWB8QA0ngHLOtLnfnaGFzr7JKUdM08 VZ9A2JXpuaaMrOJVF2csFXPH6ImNgV6v+0gb4PuS1YekS4PuIxo0xuHtc4BSfDMC4ri2 cRRjgYBF+OFeWgjgvdIH8VI29YGsropgWtGcRk7bvhUg2KN6fTyo9dnCDwYlCF15UrdO Z4YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763555611; x=1764160411; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tVOK/gZwGH/qAUHEfz1NMpjUaPUGidGp5/ImMSGMGpc=; b=Cy3kozfe7uIQ5A+hJ4+f6ocFImYDCllpxqzWcJGUp5OaAIYv7JES35ODArNvVtoIDj zad0nZtq9qlMj8kaUfT6T4vEmVC76i6fs02+azf9pgJKHQcYL0zMtyivTpIzNGwRVOSG RoYmwD4HoFBZlBPr25eahNchdCZQXRXhHOAuulMce+uZwbM9JZ3Waf9I5TQCSMPHIV41 q44HUof//hsmJDTk7DVHfDvtI2R4HwSWWnv/XWFMwgz/MANmFMIG4VtwesGDrKX803lS bErxTwPZzjhZmACc6zk53oVnhskXuVGzFxwBrk8LeA7CBd/UXs1A4Yly45/u7WJbOY8o n/QQ== X-Forwarded-Encrypted: i=1; AJvYcCXJh77gHVbn6zWrZrLxiUhqmkRKJ2aVq3Py2dlOmz3ZLwVcznldmlUABSiJc5cK/DkTHXm0Lzn7ROBBl4E=@vger.kernel.org X-Gm-Message-State: AOJu0YwFxaJnXKGetwx9C0kEUgMWpjr62e0iSoAKqrKN84bZKUhMW92f ApPgxR7xL57xHRoqZ3fjjs6RJNDJYqdNPKQMxxIvDZmFkSBY7wj6X5FO72s6KR3U7JU= X-Gm-Gg: ASbGncuSGmgzd6dW3sEm1fnxI5Io8yGRkWcfhKyeQXpEZxtrUBfpDkTYX4u+I95IZW/ Flz+ZTbzLe5hvpcowWgyI9qsc+KJ9rjafxvR/MMEogIVBXTBaeVzUs77gGDw/sZDTNl9KTG+0xp ed+0Qc+pnN56+Gqd8ow9QMT9icmNnNsaE3FanU4w7Q1UEvNQRz+g9MXod8OdpHyPeYKLYgIk8Ri miSYCgyJ8hPgKBLPkSZE3jtpvZlz5/9d8Az2/w3gm/mt6O4Rue6mp68DSgDNGDEslrfRUI0jjmv 9x2OegnutsFSXS16H9ZAEwakQmNHg7oZ5Z2cut7D0ikNmwFUwUv++Y+cxzIWtLa7DRShM95Ip5n BHeffxLourbvQTcM71/YxFxKyivZkKBHXAxERrXem7U9aU+meH499XQsJv7lZ4TcPIWtiZjwFAX nD12l8456YsT5MbQz/NkbWYyR7hD0= X-Google-Smtp-Source: AGHT+IHrVclBtR9HfIguZZfrK6wQUJjCLe6CJI/4hqbAdl79ZudAPdTK9Mp+h1vilglUyVf/wTs04g== X-Received: by 2002:a05:6000:2509:b0:42b:3e0a:649e with SMTP id ffacd0b85a97d-42b59328f9fmr20589260f8f.13.1763555610867; Wed, 19 Nov 2025 04:33:30 -0800 (PST) Received: from [192.168.1.3] ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42b53e85627sm39778524f8f.16.2025.11.19.04.33.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Nov 2025 04:33:30 -0800 (PST) Message-ID: Date: Wed, 19 Nov 2025 12:33:29 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/2] perf maps: Avoid RC_CHK use after free To: Ian Rogers References: <20251119050555.2017758-1-irogers@google.com> Content-Language: en-US Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Adrian Hunter , German Gomez , Ravi Bangoria , Christophe Leroy , Chun-Tse Shao , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot From: James Clark In-Reply-To: <20251119050555.2017758-1-irogers@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 19/11/2025 5:05 am, Ian Rogers wrote: > The case of __maps__fixup_overlap_and_insert where the "new" maps > covers existing mappings can create a use-after-free with reference > count checking enabled. The issue is that "pos" holds a map pointer > from maps_by_address that is put from maps_by_address but then used to > look for a map in maps_by_name (the compared map is now a > use-after-free). The issue stems from using maps__remove which redoes > some of the searches already done by __maps__fixup_overlap_and_insert, > so optimize the code (by avoiding repeated searches) and avoid the > use-after-free by inlining the appropriate removal code. > > Reported-by: kernel test robot > Closes: https://lore.kernel.org/oe-lkp/202511141407.f9edcfa6-lkp@intel.com > Signed-off-by: Ian Rogers Reviewed-by: James Clark > --- > tools/perf/util/maps.c | 22 ++++++++++++++++++++-- > 1 file changed, 20 insertions(+), 2 deletions(-) > > diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c > index 779f6230130a..c321d4f4d846 100644 > --- a/tools/perf/util/maps.c > +++ b/tools/perf/util/maps.c > @@ -931,8 +931,9 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) > return err; > } else { > struct map *next = NULL; > + unsigned int nr_maps = maps__nr_maps(maps); > > - if (i + 1 < maps__nr_maps(maps)) > + if (i + 1 < nr_maps) > next = maps_by_address[i + 1]; > > if (!next || map__start(next) >= map__end(new)) { > @@ -953,7 +954,24 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) > check_invariants(maps); > return err; > } > - __maps__remove(maps, pos); > + /* > + * pos fully covers the previous mapping so remove > + * it. The following is an inlined version of > + * maps__remove that reuses the already computed > + * indices. > + */ > + map__put(maps_by_address[i]); > + memmove(&maps_by_address[i], > + &maps_by_address[i + 1], > + (nr_maps - i - 1) * sizeof(*maps_by_address)); > + > + if (maps_by_name) { > + map__put(maps_by_name[ni]); > + memmove(&maps_by_name[ni], > + &maps_by_name[ni + 1], > + (nr_maps - ni - 1) * sizeof(*maps_by_name)); > + } > + --RC_CHK_ACCESS(maps)->nr_maps; > check_invariants(maps); > /* > * Maps are ordered but no need to increase `i` as the