From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796193288; b=ajNpjfYrHlU77+RW5TIKoeimBMCZw2hGSp/Nj5KuEEZYXjipo8u6RgpfZsPiRz+d57/g qRa2IFQ0KvQXpQ7Dn2PuBqbB1bc35XK/hax6VmDA3hSvawSA8ONZe74pD+BSe/TdxjRUo UvyeHleQpSPSunbqitcsFN9C5+j5zEDM6MoIvxm9kTNBPBkfzOI/L8l2+B2FToDBxwzQ6 U7GOSWYHzLewAq+31KdyRorIs4t9rDS31cjlBYoybjGUFF57Wqsmlcva7lFOscDJ5CW1V eOpsmeQKNv88bhSFlKrU+A7+SA98FlroCE388YkBQ3bMRJVukdKkA+8yTxnbLUE8gWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796193288; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=BkaNMw1nNRWOBDLPLZLS9fm4LyhudnCiSgSFr6pv7fk=; b=dxQ0pTn7ATq8HwPDsm1koXCFoAYRLAWcEjY7MWs6vzOR/qRr5y6F1fN93ke0MNX2xJ7c OdncRl2Lt3MWkqt06SydaZ2peXnezUj54eQL8B6Y6VLl1oNapCwMoegUrFQ+mCxyYMtky zhd48YVxlxWpk5ykSfu9aQz03OYhUrADxVaJRTbZpz2eVIVMWjdFvUa5S02UxzbQKMpQC 1K6wwUdo/v1Js6kyCMdYExAtv7grYDUawBSzpFqJfZDiP4ANKnH8/7aXi0ZvjpFx4D7jg cRAV3IgK18eBEwj8lwhhs7iq/PdkuY0ZYQr0H0DkpD+EUOmdZtGyi20A490JDU9jV1g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lu2zlTpP; arc=none smtp.remote-ip=74.125.82.173 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B755606A327 for ; Wed, 2 Dec 2026 06:34:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5B755606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lu2zlTpP Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-386c336eab8so10233359eec.0 for ; Tue, 01 Dec 2026 22:34:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796193287; x=1796798087; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=BkaNMw1nNRWOBDLPLZLS9fm4LyhudnCiSgSFr6pv7fk=; b=lu2zlTpPCbgdqLO3Pn3i2s1S/UmpFR23CF4trLBUzEbJcsQpQZ6wl2ckNF9Qxpw0qY TywXClzHWyxzig+F3L08d1VATlEPorj46a/ZMhAh92EpJ+XW7jn5XFmwkWL0V9/wWeoO XXqGLAhAVMquYnEPpG9vAngmHhlm1CVIxcAVs05NjsKFogFeDUSU00aZXnLMelH7AlBi gjYQ2xDMoZbq+D5a0zfrrn+E12GjVA2JS6pi34m2AgtZrjXrKoH/tjMZVTtLjhEoDnPL cJyXlSunGFpdWsAloK7LXbugk92jfG7XbxCFmG6bkH6ShZjb1kp4LCWKvoCShLUmcHYT bJUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796193287; x=1796798087; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=BkaNMw1nNRWOBDLPLZLS9fm4LyhudnCiSgSFr6pv7fk=; b=Pv7MdMEb983nwNMftCx1AqptQM5k26v7NwaQ/79W/5r28eTiYwZA3wWMDdBlq2sgLw L6qxEObwvfP+rJTIV0Cg4XaeVq+C/CGO7tlal7JOlt+DKo4iq5hoI0gy7jumOSUjO0+r 1CK5c+lhPTUd1s6t6EqXMkabId2WmUUP8GymfOiLe1hjndRUxv8aMVzwE6rys1xHTru2 QcBDuG5xAEcrlNN8lXc85lQ15QvOeGdMUmrmP+TkqzIJA7u8j89TuONypHT8mKu8rBS3 La1ieTTgwtIoUybgzzOaIQhsTjrHnMvAadcIqQbtz5yrK5np7mZS2h29Utug3vkp8V65 m9kg== X-Forwarded-Encrypted: i=1; AL+hErEh56nH7QI9vnxXoHTYwZld3lOTSvUwhU1VBCyC+wMrPPM1wkzzbOPnwb5m36cwfP2gnfKf3y2exhCcHlk=@vger.kernel.org X-Gm-Message-State: APuU4Z9HDb1+580PskFaFy/8PNYESfF9e4R0SDakwJglbZnrKxCt+Vwu E2YG9s+fjU35P1X7C2SC/I2xAKQHo5Wda3+Ba186CQeYHTlUkGJT8xDw X-Gm-Gg: AZFdTZeATbhVh03JWG7EZ4TtK9BbXNZLj8mi46HY/EhWwi549G9RCMlMU9a1TqvhG38 WmlhKlFCNWHYnWOYqQQdKnSDnkvbelt8IsXMxorMspulH0YuqBCDwLEW3yDhrh4HDmJE3+sOANM DvMJPQE5ZewAAQimupD/rri1T/2Ya7kBv0OBaLblrd85Z5OfogYJYW0ZTEhhS4AyALK7tsBsxDP pOfmjWnQrvU3L2z7GP5dMOY3gefGboKUw8we18xZNJcHLsOpuU3FXWTy0DAxno9uxeTiwc5whY4 OULDDBI+vIyZ5b6JVj+MBsAtQh6eeUr6l4fBO813z68ZfsIVt/756CGZqzBVuSKk3Qq+mZhwWQa HhepH58bsp3AboG2nIh7wY43rIRE7sNmXn7rvCmY4yMe0VA91kIhPi0r2yl/nF65F4L7dVgjvYx 9UTtrjHV5E+4DNiqIdL2amqE4fTmTyAdFfhUZOLyzJQC89fN6IRGZ5XgdZmFgEWUrQXErEVTe4j fpH+CixiHqiqqoPumoN4JXzYFL7AUMRsOqPExPlAid4z/ZvI5Dqi4lvTkBAUaFMyIiplIqdd3zB zDoIkgyC16/mtGjTC2rlIxQF8k1w2Lrszq6E8RBgcYoCBBq56kIPH1DB X-Received: by 2002:a05:7301:5098:b0:381:5fe2:a079 with SMTP id 5a478bee46e88-3856c6b010amr38897558eec.1.1796193287181; Tue, 01 Dec 2026 22:34:47 -0800 (PST) Received: from Desktop ([182.161.246.158]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385666e7f59sm121177821eec.6.2026.12.01.22.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 22:34:46 -0800 (PST) From: JunYoung Lee To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, JunYoung Lee , stable@vger.kernel.org Subject: [PATCH] perf/core: Don't expose kernel page sizes to exclude_kernel events Date: Wed, 2 Dec 2026 15:34:33 +0900 Message-ID: <20261202063433.279947-1-ljyoung04@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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). Fixes: 8d97e71811aa ("perf/core: Add PERF_SAMPLE_DATA_PAGE_SIZE") Fixes: 995f088efebe ("perf/core: Add support for PERF_SAMPLE_CODE_PAGE_SIZE") Fixes: a92cbb82c8d3 ("perf/core: allow pte_offset_map() to fail") Cc: stable@vger.kernel.org Signed-off-by: JunYoung Lee --- Affected versions: the page size oracle since v5.11 (8d97e71811aa, 995f088efebe) and the espfix hang since v6.5 (a92cbb82c8d3), up to mainline 693c2e04e3f1 (v7.4-rc5), which this patch is based on. The patch also applies to tip perf/core 86a52e8d105f and to v6.12.118. Testing: - An unprivileged reproducer (uid 65534, no capabilities, perf_event_paranoid=2, KASLR on) was run before and after the patch on v6.12.118 and on a v7.2-rc5 based kernel, in KVM guests on an AMD host. Before: kernel addresses report their leaf sizes, and a probe in the espfix area hangs the CPU. After: every kernel address reports 0, the espfix probes return, and user addresses still report their page size. - With exclude_kernel cleared (as root), a kernel text address still reports 2M after the patch (v7.2-rc5 based kernel). - W=1 build of kernel/events/core.o, defconfig, x86_64 (gcc 13.3) and arm64 (clang 19): no warnings. Not tested: the patched mainline kernel itself was only built, not booted; Intel CPUs; architectures other than x86-64 at run time; other ways of getting a kernel address into a user-mode sample. The reproducer is not posted here; I can send it privately to the maintainers. 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; @@ -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; } base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790 -- 2.56.1