Skip to contentSkip to conversation

perf/x86: Check data address for IBS software filter

PATCH Applied Namhyung Kim 5 participants 9 messages Mar 17, 2025

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf/x86: Check data address for IBS software filter

IBS software filter was to filter kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel more or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +7 -0
index 7b52b8e3a185157f..8b3b76fad587b3ff 100644
@@ -1286,6 +1286,13 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}
+
 	/*
 	 * rip recorded by IbsOpRip will not be consistent with rsp and rbp
 	 * recorded as part of interrupt regs. Thus we need to use rip from
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
merge bot Applied
#1 lore raw
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     b0be17d8108bf3448a58be319d085155a128cf3a
Gitweb:        https://git.kernel.org/tip/b0be17d8108bf3448a58be319d085155a128cf3a
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 01:10:58 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 10:04:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel mode or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +7 -0
index e7a8b87..24985c7 100644
@@ -1147,6 +1147,13 @@ fail:
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}
+
 	/*
 	 * rip recorded by IbsOpRip will not be consistent with rsp and rbp
 	 * recorded as part of interrupt regs. Thus we need to use rip from
maintainer
#2 lore raw
Quoted 36 lines · On Mon, Mar 17, 2025 at 09:15:05AM -0000, tip-bot2 for Namhyung Kim wrote:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     b0be17d8108bf3448a58be319d085155a128cf3a
Gitweb:        https://git.kernel.org/tip/b0be17d8108bf3448a58be319d085155a128cf3a
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 01:10:58 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 10:04:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel mode or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +0 -0
index e7a8b87..24985c7 100644

arch/x86/events/amd/ibs.c fail: · line 1155

Quoted 9 lines
arch/x86/events/amd/ibs.c +6 -0
@@ -1147,6 +1147,13 @@ fail:
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}

Did anyone build this?

arch/x86/events/amd/ibs.c: In function ‘perf_ibs_handle_irq’:
arch/x86/events/amd/ibs.c:1291:63: error: macro "access_ok" requires 2 arguments, but only 1 given
1291 | event->attr.exclude_kernel && !access_ok(data.addr)) {
| ^
In file included from ./arch/x86/include/asm/uaccess.h:25,

                 from ./include/linux/uaccess.h:12,
                 from ./include/linux/sched/task.h:13,
                 from ./include/linux/sched/signal.h:9,
                 from ./include/linux/ptrace.h:7,
                 from ./include/uapi/asm-generic/bpf_perf_event.h:4,
                 from ./arch/x86/include/generated/uapi/asm/bpf_perf_event.h:1,
                 from ./include/uapi/linux/bpf_perf_event.h:11,
                 from ./include/linux/perf_event.h:18,
                 from arch/x86/events/amd/ibs.c:9:
./include/asm-generic/access_ok.h:45: note: macro "access_ok" defined here
   45 | #define access_ok(addr, size) likely(__access_ok(addr, size))
      | 

arch/x86/events/amd/ibs.c:1291:44: error: ‘access_ok’ undeclared (first use in this function)
1291 | event->attr.exclude_kernel && !access_ok(data.addr)) {
| ^~~~~~~~~
arch/x86/events/amd/ibs.c:1291:44: note: each undeclared identifier is reported only once for each function it appears in make[5]: *** [scripts/Makefile.build:207: arch/x86/events/amd/ibs.o] Error 1
make[4]: *** [scripts/Makefile.build:465: arch/x86/events/amd] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:465: arch/x86/events] Error 2
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [scripts/Makefile.build:465: arch/x86] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/mnt/kernel/kernel/6th/linux/Makefile:1997: .] Error 2
make: *** [Makefile:251: __sub-make] Error 2

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#3 lore raw
Quoted 40 lines · Borislav Petkov
Quoted 39 lines · On Mon, Mar 17, 2025 at 09:15:05AM -0000, tip-bot2 for Namhyung Kim wrote:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     b0be17d8108bf3448a58be319d085155a128cf3a
Gitweb:        https://git.kernel.org/tip/b0be17d8108bf3448a58be319d085155a128cf3a
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 01:10:58 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 10:04:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel mode or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +0 -0
index e7a8b87..24985c7 100644
@@ -1147,6 +1147,13 @@ fail:
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
Quoted 8 lines

arch/x86/events/amd/ibs.c fail: · line 1155

Quoted 6 lines
arch/x86/events/amd/ibs.c +6 -0
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}

Did anyone build this?

Oops, sorry about this. I fixed it locally but sent the one before adding the change. Will send v2.

Thanks,
Namhyung

Quoted 37 lines

arch/x86/events/amd/ibs.c: In function ‘perf_ibs_handle_irq’:
arch/x86/events/amd/ibs.c:1291:63: error: macro "access_ok" requires 2 arguments, but only 1 given
1291 | event->attr.exclude_kernel && !access_ok(data.addr)) {
| ^
In file included from ./arch/x86/include/asm/uaccess.h:25,

                 from ./include/linux/uaccess.h:12,
                 from ./include/linux/sched/task.h:13,
                 from ./include/linux/sched/signal.h:9,
                 from ./include/linux/ptrace.h:7,
                 from ./include/uapi/asm-generic/bpf_perf_event.h:4,
                 from ./arch/x86/include/generated/uapi/asm/bpf_perf_event.h:1,
                 from ./include/uapi/linux/bpf_perf_event.h:11,
                 from ./include/linux/perf_event.h:18,
                 from arch/x86/events/amd/ibs.c:9:
./include/asm-generic/access_ok.h:45: note: macro "access_ok" defined here
   45 | #define access_ok(addr, size) likely(__access_ok(addr, size))
      | 

arch/x86/events/amd/ibs.c:1291:44: error: ‘access_ok’ undeclared (first use in this function)
1291 | event->attr.exclude_kernel && !access_ok(data.addr)) {
| ^~~~~~~~~
arch/x86/events/amd/ibs.c:1291:44: note: each undeclared identifier is reported only once for each function it appears in make[5]: *** [scripts/Makefile.build:207: arch/x86/events/amd/ibs.o] Error 1
make[4]: *** [scripts/Makefile.build:465: arch/x86/events/amd] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:465: arch/x86/events] Error 2
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [scripts/Makefile.build:465: arch/x86] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/mnt/kernel/kernel/6th/linux/Makefile:1997: .] Error 2
make: *** [Makefile:251: __sub-make] Error 2

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#4 lore raw
In reply to tip-bot2 for Namhyung Kim
Quoted 36 lines · On Mon, Mar 17, 2025 at 09:15:05AM -0000, tip-bot2 for Namhyung Kim wrote:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     b0be17d8108bf3448a58be319d085155a128cf3a
Gitweb:        https://git.kernel.org/tip/b0be17d8108bf3448a58be319d085155a128cf3a
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 01:10:58 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 10:04:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel mode or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +0 -0
index e7a8b87..24985c7 100644

arch/x86/events/amd/ibs.c fail: · line 1152

Quoted 6 lines
arch/x86/events/amd/ibs.c +3 -0
@@ -1147,6 +1147,13 @@ fail:
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {

If only you'd looked at all the other filter code :/ everybody uses kernel_ip() helper for this, not access_ok().

Quoted 7 lines
arch/x86/events/amd/ibs.c +4 -0
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}
+
 	/*
 	 * rip recorded by IbsOpRip will not be consistent with rsp and rbp
 	 * recorded as part of interrupt regs. Thus we need to use rip from
author maintainer
#5 lore raw

Hi Peter,

Quoted 40 lines · Peter Zijlstra
Quoted 39 lines · On Mon, Mar 17, 2025 at 09:15:05AM -0000, tip-bot2 for Namhyung Kim wrote:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     b0be17d8108bf3448a58be319d085155a128cf3a
Gitweb:        https://git.kernel.org/tip/b0be17d8108bf3448a58be319d085155a128cf3a
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 01:10:58 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 10:04:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in PMI handler. It checks the instruction address in the IBS register to determine if it was in the kernel mode or not.

But it turns out that it's possible to report a kernel data address even if the instruction address belongs to the user space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data address like IDT while holding the faulting instruction's RIP. To prevent an information leak, it should double check if the data address in PERF_SAMPLE_DATA is in the kernel space as well.

---
 arch/x86/events/amd/ibs.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/events/amd/ibs.c +0 -0
index e7a8b87..24985c7 100644
@@ -1147,6 +1147,13 @@ fail:
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
Quoted 6 lines

arch/x86/events/amd/ibs.c fail: · line 1152

Quoted 3 lines
arch/x86/events/amd/ibs.c +3 -0
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {

If only you'd looked at all the other filter code :/ everybody uses kernel_ip() helper for this, not access_ok().

I thought it also needs __KERNEL_CS but now I see it only checks the address. Will change in v2.

Thanks,
Namhyung

Quoted 8 lines
Quoted 8 lines
arch/x86/events/amd/ibs.c +4 -0
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}
+
 	/*
 	 * rip recorded by IbsOpRip will not be consistent with rsp and rbp
 	 * recorded as part of interrupt regs. Thus we need to use rip from
#6 lore raw
In reply to Namhyung Kim

Hi Namhyung,

arch/x86/events/amd/ibs.c static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) · line 1294

Quoted 10 lines
arch/x86/events/amd/ibs.c +6 -0
@@ -1286,6 +1286,13 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}

Can this move up where it checks perf_exclude_event()? Use ibs_data.regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)] instead of data.addr.

Thanks,
Ravi

author maintainer
#7 lore raw

Hi Ravi,

Quoted 6 lines · Ravi Bangoria

Hi Namhyung,

Quoted 4 lines
arch/x86/events/amd/ibs.c +0 -0
@@ -1286,6 +1286,13 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
Quoted 10 lines

arch/x86/events/amd/ibs.c static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) · line 1294

Quoted 6 lines
arch/x86/events/amd/ibs.c +6 -0
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}

Can this move up where it checks perf_exclude_event()? Use ibs_data.regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)] instead of data.addr.

Sure, will do.

Thanks,
Namhyung

author maintainer
#8 lore raw
Quoted 20 lines · Namhyung Kim

Hi Ravi,

Quoted 6 lines · Ravi Bangoria

Hi Namhyung,

Quoted 4 lines
arch/x86/events/amd/ibs.c +0 -0
@@ -1286,6 +1286,13 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	if (perf_ibs == &perf_ibs_op)
 		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
 
Quoted 10 lines

arch/x86/events/amd/ibs.c static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) · line 1294

Quoted 6 lines
arch/x86/events/amd/ibs.c +6 -0
+	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
+	    (event->attr.sample_type & PERF_SAMPLE_ADDR) &&
+	    event->attr.exclude_kernel && !access_ok(data.addr)) {
+		throttle = perf_event_account_interrupt(event);
+		goto out;
+	}

Can this move up where it checks perf_exclude_event()? Use ibs_data.regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)] instead of data.addr.

Quoted 1 line

Sure, will do.

Hmm.. but I think it also needs to check op_data3_dc_lin_addr_valid bit. Can we move up the perf_ibs_parse_ld_st_data() too and check data.addr?

Thanks,
Namhyung