From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=90.155.50.34 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796208312; b=z+0NzQaabrsdqX2eyq4gem+VnGC8IHj7TE1HsTWFPFYJIt5+mcfe2anfo7sx5BywOSqM yPSMb/InxFHFjYqx2mYOO8PV0ImQOwa+oExTcxElkFiBEb8TRWrfshBOQJVK21yxYNpKL Na+eYcsUYJKO8gfyCZtVEDUeO6e4dqxncKBQgjegBd4c6HCKYxSe7mrT1ONmNmnXYbuTq lEMkfVGFxRBOa0JkrMdj3Sc4QiWT1bFVwhSiFTGF4TROaRXY8KIltjJS8UG6yG0KQqIgi vrUI8bQXveS/2w/qg2863zVKijjXZdTIAb8Lac4bnzFHiUWlZcutsCmMUqjB1DaOXHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796208312; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=1pehrSATNGGUSGh/y4BX70Nq5r07orLVICKjEcGjeSU=; b=KuabSUGq6f4NqPlYOGhXPjq6qO/5ec/ELeQDLpNXk1/Qo0KAmyGeMMJef8Zs7qkmwvqN uQ9MWDVx3CzEwtCZmi5filCIG0HdHdkxZwr53xNIaTLhHiLYf4WjUPUMeBjcDtJL0j2dg Jn8RefQafyl1D5ENJdEwk4Tk/ZqtLqj+/pw1B5AbpY1lsIMWYFE8s1WeJRHMlkqAVoWq+ aMhlBDcqEOoa2z4ogQMdY/sAYCY2rv8IUkjfzGcpT5b3nE8wEQFu8ka8JdHBWuIm2+Frk jQmm0/FEZM19FKXwC++FyHYvEHTkfGCXai/my50EY2QRbBEiXj2TVOkb427yjAYzFbg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=l5ri2Zrh; arc=none smtp.remote-ip=90.155.50.34 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11D3A606A33F; Wed, 2 Dec 2026 10:45:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 11D3A606A33F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=l5ri2Zrh DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=1pehrSATNGGUSGh/y4BX70Nq5r07orLVICKjEcGjeSU=; b=l5ri2ZrhLlCTEiNss65KeNOZkL T8Oe0gmh0LgaR2zVuPDPmWQaIeFSi/3TBobesisf1qHReIkB/YBqTCR7BRczjk/EOSrAR/Cxn2kIc poEaxDNnWIl3KcsBjuG/zL22zfk++sn43L3Gfsw3lGu0sYNXJiJc4kvE8eskMjNEv+Q7KtO2gXYlD 8t6UpfQGak2ndBp1qGxZ/o02fVyqJRdTE7TZnCMSRfr2iPveyv9gJ3/FDHmjceXD4wmT+tfQReXA0 5zWmuH97gNeD6/unjOb2Ka5fQHDJB0HNsJJwdj5NJJgGKV+1wKeHw7WcqulDG+jSYwmAQrTH4XbIa n8gsoOwQ==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYhpc-00000008ZGO-2lz8; Wed, 02 Dec 2026 10:45:00 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id A6B0B300428; Wed, 02 Dec 2026 11:44:56 +0100 (CET) Date: Wed, 2 Dec 2026 11:44:56 +0100 From: Peter Zijlstra To: JunYoung Lee Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] perf/core: Don't expose kernel page sizes to exclude_kernel events Message-ID: <20261202104456.GH2883950@noisy.programming.kicks-ass.net> References: <20261202063433.279947-1-ljyoung04@gmail.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261202063433.279947-1-ljyoung04@gmail.com> On Wed, Dec 02, 2026 at 03:34:33PM +0900, JunYoung Lee wrote: > PERF_SAMPLE_CODE_PAGE_SIZE and PERF_SAMPLE_DATA_PAGE_SIZE make > perf_prepare_sample() call perf_get_page_size() on data->ip and > data->addr. perf_get_page_size() walks the page tables of the current mm > (or init_mm) and returns the leaf size (0, 4K, 2M or 1G) of the mapping. > The address comes from the sample and is not range checked. > > With the default perf_event_paranoid=2, a user without CAP_PERFMON has > to set exclude_kernel, because perf_event_open() calls > perf_allow_kernel() whenever exclude_kernel is clear. The overflow path > then drops a sample only if exclude_kernel is set and user_mode(regs) is > false, so it checks the mode of the sampled frame, not the sampled > address. A user-mode frame can still carry a kernel address: a hardware > data breakpoint on the memory operand of an indirect "jmp *mem" traps > after the jump, so the user-mode trap frame already holds the kernel > jump target in RIP. data->ip is then a kernel address and its page size > is written into the sample. > > Reading back the leaf size of chosen kernel addresses is a KASLR oracle. > As an unprivileged user (uid 65534, no capabilities, > perf_event_paranoid=2) this located the kernel image base and the bases > of the direct map, the vmalloc area and vmemmap in about one second, > matching the values root can read. Reproduced on v6.12.118 and on a > v7.2-rc5 based kernel. > > The same unchecked address can also hang a CPU on x86-64. The espfix > PMD entries are not writable, so pmd_bad() is true for them. For an > address in the espfix area pte_offset_map() rejects the pmd, prints > "bad pmd" and returns NULL, and perf_get_pgtable_size() retries. The > entry never changes, so the retry never ends, and it runs with > interrupts disabled. With the same unprivileged setup this flooded the > console with "bad pmd" messages and produced soft lockup and RCU stall > reports with the CPU stuck in this loop. > > Make perf_get_page_size() return 0 for an address at or above > TASK_SIZE_MAX when the event has exclude_kernel set. Such an event is > not allowed to profile the kernel. Events with exclude_kernel cleared > passed perf_allow_kernel() at open time and still get kernel page sizes, > and user addresses are not affected. > > Also make perf_get_pgtable_size() return 0 for a bad pmd instead of > retrying. pte_offset_map() rejects a bad pmd every time, so the retry > cannot make progress, also for events that may profile the kernel. > > The problem was found, and this patch drafted, with the help of an AI > coding assistant (Claude). This is a cumbersome long Changelog for what is essentially a simple patch. > kernel/events/core.c | 23 ++++++++++++++++++++--- > 1 file changed, 20 insertions(+), 3 deletions(-) > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 4ab3919ac..8c93d4bac 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -8698,6 +8698,14 @@ static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) > if (pmd_leaf(pmd)) > return pmd_leaf_size(pmd); > > + /* > + * pte_offset_map() rejects a bad pmd (for example an espfix entry) > + * every time, so the retry below would loop forever with interrupts > + * disabled. Report size 0 instead. > + */ > + if (pmd_bad(pmd)) > + return 0; > + > ptep = pte_offset_map(&pmd, addr); > if (!ptep) > goto again; This is a separate issue and should therefore be a separate patch. > @@ -8711,7 +8719,7 @@ static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) > return size; > } > > -static u64 perf_get_page_size(unsigned long addr) > +static u64 perf_get_page_size(struct perf_event *event, unsigned long addr) > { > struct mm_struct *mm; > unsigned long flags; > @@ -8720,6 +8728,15 @@ static u64 perf_get_page_size(unsigned long addr) > if (!addr) > return 0; > > + /* > + * Do not reveal kernel mappings to an event that may not profile the > + * kernel. The overflow path checks user_mode(regs), not the sampled > + * value, and a user-mode frame can still carry a kernel address, for > + * example after a data breakpoint trap on an indirect jump. > + */ > + if (event->attr.exclude_kernel && addr >= TASK_SIZE_MAX) > + return 0; > + > /* > * Software page-table walkers must disable IRQs, > * which prevents any tear down of the page tables. > @@ -8968,12 +8985,12 @@ void perf_prepare_sample(struct perf_sample_data *data, > * but the value will not dump to the userspace. > */ > if (filtered_sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) { > - data->data_page_size = perf_get_page_size(data->addr); > + data->data_page_size = perf_get_page_size(event, data->addr); > data->sample_flags |= PERF_SAMPLE_DATA_PAGE_SIZE; > } > > if (filtered_sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) { > - data->code_page_size = perf_get_page_size(data->ip); > + data->code_page_size = perf_get_page_size(event, data->ip); > data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE; > } I'm thinking this is doing things at the wrong level. If exclude_kernel then neither data->addr nor data->ip should be kernel to begin with. You're now randomly fixing up one consumer of that data.