Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang
@ 2026-12-02 11:59 JunYoung Lee
  2026-12-02 11:59 ` [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events JunYoung Lee
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: JunYoung Lee @ 2026-12-02 11:59 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark, linux-perf-users, linux-kernel, JunYoung Lee

Patch 1 stops exclude_kernel events from getting a kernel IP, which
PERF_SAMPLE_CODE_PAGE_SIZE turned into a KASLR leak. Patch 2 fixes the
endless page table walk on a bad pmd (espfix), which events allowed to
profile the kernel can still hit.

v1: https://lore.kernel.org/all/20261202063433.279947-1-ljyoung04@gmail.com/

Changes in v2:
- Filter the kernel IP in perf_instruction_pointer(), where data->ip is
  set, instead of in perf_get_page_size() (Peter).
- Split the bad pmd fix into its own patch (Peter).
- Shorter changelogs.

On data->addr: I found no way on x86 to get a kernel address into an
exclude_kernel sample. Breakpoint addresses are checked when the event
is created, and user mode faults on kernel addresses don't emit the
page-fault software event. On riscv and powerpc that event seems to be
emitted before the kernel address check, so a kernel data->addr may be
possible there. I haven't tested this. Should it be fixed in those
fault handlers, or with a generic check?

JunYoung Lee (2):
  perf/core: Don't report a kernel IP for exclude_kernel events
  perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size()

 kernel/events/core.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
-- 
2.56.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events
  2026-12-02 11:59 [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang JunYoung Lee
@ 2026-12-02 11:59 ` JunYoung Lee
  2026-12-02 12:16   ` sashiko-bot
  2026-12-02 11:59 ` [PATCH v2 2/2] perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size() JunYoung Lee
  2026-12-02 12:16 ` [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: JunYoung Lee @ 2026-12-02 11:59 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark, linux-perf-users, linux-kernel, JunYoung Lee, stable

perf_instruction_pointer() hides the IP of an exclude_kernel event only
when the sampled frame is not in user mode. A user mode frame can still
carry a kernel IP: a data breakpoint on the memory operand of an indirect
"jmp *mem" traps after the jump, so the user mode trap frame holds the
kernel jump target.

PERF_SAMPLE_CODE_PAGE_SIZE then reports the page size of that kernel
address, which lets an unprivileged user find the kernel image and the
direct map despite KASLR.

Also return 0 when the IP is a kernel address.

The problem was found with the help of an AI coding assistant (Claude).

Fixes: 995f088efebe ("perf/core: Add support for PERF_SAMPLE_CODE_PAGE_SIZE")
Cc: stable@vger.kernel.org
Signed-off-by: JunYoung Lee <ljyoung04@gmail.com>
---
Changes in v2:
- Filter the kernel IP in perf_instruction_pointer() instead of in
  perf_get_page_size() (Peter).
- Move the bad pmd fix to its own patch, 2/2 (Peter).
- Shorten the changelog.

Tested on 693c2e04e3f1 with the kernelCTF LTS config, KASLR on, in a
KVM guest on an AMD host, as uid 65534 with perf_event_paranoid=2:
- Kernel addresses: samples now carry ip=0 and code_page_size=0, and a
  scan of the kernel image and direct map windows finds nothing.
- User addresses: IP and page size unchanged.
- cpu-clock with exclude_kernel: all 7390 samples have a user IP and 4K.
- exclude_kernel=0 as root: _stext still reports 2M.

Kernels without the generic perf_instruction_pointer() (for example
v6.12.y) need the same check where perf_prepare_sample() sets data->ip.

 kernel/events/core.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919ac..4c7f173d7 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7858,6 +7858,8 @@ unsigned long perf_misc_flags(struct perf_event *event,
 unsigned long perf_instruction_pointer(struct perf_event *event,
 				       struct pt_regs *regs)
 {
+	unsigned long ip;
+
 	/*
 	 * Hardware skid can lead to a scenario where a PMI is
 	 * delivered after the CPU has already entered kernel mode.
@@ -7870,8 +7872,18 @@ unsigned long perf_instruction_pointer(struct perf_event *event,
 			0 : perf_guest_get_ip();
 	}
 
-	return event->attr.exclude_kernel && !user_mode(regs) ?
-		0 : perf_arch_instruction_pointer(regs);
+	ip = perf_arch_instruction_pointer(regs);
+
+	/*
+	 * A user mode frame can still carry a kernel IP, for example when a
+	 * debug trap is taken right after a jump to a kernel address, before
+	 * the instruction fetch faults.
+	 */
+	if (event->attr.exclude_kernel &&
+	    (!user_mode(regs) || ip >= TASK_SIZE_MAX))
+		return 0;
+
+	return ip;
 }
 
 u64 __weak perf_reg_value(struct pt_regs *regs, int idx)

base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
-- 
2.56.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size()
  2026-12-02 11:59 [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang JunYoung Lee
  2026-12-02 11:59 ` [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events JunYoung Lee
@ 2026-12-02 11:59 ` JunYoung Lee
  2026-12-02 12:16 ` [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: JunYoung Lee @ 2026-12-02 11:59 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark, linux-perf-users, linux-kernel, JunYoung Lee, stable

perf_get_pgtable_size() hands pte_offset_map() a copy of the pmd. For a
bad pmd, such as an x86-64 espfix entry, pte_offset_map() fails and only
clears the copy, so the walk reads the same entry again and retries
forever with interrupts disabled. A PERF_SAMPLE_CODE_PAGE_SIZE sample
with an IP in the espfix area hangs the CPU this way.

Return 0 for a bad pmd instead.

Fixes: a92cbb82c8d3 ("perf/core: allow pte_offset_map() to fail")
Cc: stable@vger.kernel.org
Signed-off-by: JunYoung Lee <ljyoung04@gmail.com>
---
Changes in v2:
- Split out of v1 into its own patch (Peter).

Tested on 693c2e04e3f1 (kernelCTF LTS config, KVM guest, AMD host) with
a root exclude_kernel=0 breakpoint event probing the 128 espfix slots:
- With 1/2 only: the probe of the populated slot never returns, the
  console fills with "bad pmd" messages and the guest stops answering.
- With 1/2 and 2/2: all probes return with code_page_size=0 and no
  "bad pmd" message appears.

 kernel/events/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4c7f173d7..dbede0eab 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8710,6 +8710,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() fails on a bad pmd and only clears our local
+	 * copy, so retrying would read the same entry again and loop
+	 * forever with interrupts disabled.
+	 */
+	if (pmd_bad(pmd))
+		return 0;
+
 	ptep = pte_offset_map(&pmd, addr);
 	if (!ptep)
 		goto again;

-- 
2.56.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events
  2026-12-02 11:59 ` [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events JunYoung Lee
@ 2026-12-02 12:16   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-02 12:16 UTC (permalink / raw)
  To: JunYoung Lee; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf/core: perf_instruction_pointer() leaks kernel page size
  via guest IP
--

commit a820ba38e41084effb8fa61a40e0099cbe14de3a
Author: JunYoung Lee <ljyoung04@gmail.com>

perf/core: Don't report a kernel IP for exclude_kernel events

This commit returns 0 from perf_instruction_pointer() on exclude_kernel
events when the sampled IP is >= TASK_SIZE_MAX, preventing a user mode
trap frame with a kernel IP from exposing kernel code page sizes via
PERF_SAMPLE_CODE_PAGE_SIZE.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4ab3919ac78e9..4c7f173d716a3 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -7858,6 +7858,8 @@ unsigned long perf_misc_flags(struct perf_event *event,
>  unsigned long perf_instruction_pointer(struct perf_event *event,
>  				       struct pt_regs *regs)
>  {
> +	unsigned long ip;
> +
>  	/*
>  	 * Hardware skid can lead to a scenario where a PMI is
>  	 * delivered after the CPU has already entered kernel mode.
> @@ -7870,8 +7872,18 @@ unsigned long perf_instruction_pointer(struct perf_event *event,
>  			0 : perf_guest_get_ip();
>  	}
>  
> -	return event->attr.exclude_kernel && !user_mode(regs) ?
> -		0 : perf_arch_instruction_pointer(regs);
> +	ip = perf_arch_instruction_pointer(regs);
> +
> +	/*
> +	 * A user mode frame can still carry a kernel IP, for example when a
> +	 * debug trap is taken right after a jump to a kernel address, before
> +	 * the instruction fetch faults.
> +	 */
> +	if (event->attr.exclude_kernel &&
> +	    (!user_mode(regs) || ip >= TASK_SIZE_MAX))
> +		return 0;
> +
> +	return ip;
>  }

[Severity: Medium]
Can the should_sample_guest() branch still return a kernel address >=
TASK_SIZE_MAX on an exclude_kernel event?

An unprivileged user can open a PERF_SAMPLE_CODE_PAGE_SIZE event with
exclude_kernel=1 and exclude_guest=0, and run a KVM vCPU in guest user
mode (PERF_GUEST_USER) with a guest IP >= TASK_SIZE_MAX.
perf_instruction_pointer() then returns perf_guest_get_ip() without the
ip >= TASK_SIZE_MAX check, and perf_prepare_sample() passes data->ip to
perf_get_page_size(), which walks current->mm on the host and reports the
host kernel mapping's page size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202115902.586760-1-ljyoung04@gmail.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang
  2026-12-02 11:59 [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang JunYoung Lee
  2026-12-02 11:59 ` [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events JunYoung Lee
  2026-12-02 11:59 ` [PATCH v2 2/2] perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size() JunYoung Lee
@ 2026-12-02 12:16 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-02 12:16 UTC (permalink / raw)
  To: JunYoung Lee; +Cc: linux-perf-users

Sashiko has finished reviewing this 2-patch series:

- Patch 2: no issues found.
- Patch 1 ("perf/core: Don't report a kernel IP for exclude_kern..."): 1
  potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 1.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202115902.586760-1-ljyoung04@gmail.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02 12:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 11:59 [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang JunYoung Lee
2026-12-02 11:59 ` [PATCH v2 1/2] perf/core: Don't report a kernel IP for exclude_kernel events JunYoung Lee
2026-12-02 12:16   ` sashiko-bot
2026-12-02 11:59 ` [PATCH v2 2/2] perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size() JunYoung Lee
2026-12-02 12:16 ` [PATCH v2 0/2] perf/core: Fix kernel page size leak and espfix hang sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox