Skip to contentSkip to conversation

perf/x86: Check data address for IBS software filter

PATCH Applied Namhyung Kim 4 participants 8 messages Mar 17, 2025 – Mar 21, 2025

Subsystems (2)
lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 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.

---
v2)
* fix a build error (Boris)
* use kernel_ip() instead (Peter)
* combine sw filter checks (Ravi)

 arch/x86/events/amd/ibs.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/events/amd/ibs.c +6 -4
index 7b52b8e3a185157f..fbe10b469e8b03d5 100644
@@ -1267,8 +1267,13 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 		regs.flags |= PERF_EFLAGS_EXACT;
 	}
 
+	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) &&
-	    perf_exclude_event(event, &regs)) {
+	    (perf_exclude_event(event, &regs) ||
+	     ((data.sample_flags & PERF_SAMPLE_ADDR) &&
+	      event->attr.exclude_kernel && kernel_ip(data.addr)))) {
 		throttle = perf_event_account_interrupt(event);
 		goto out;
 	}
@@ -1283,9 +1288,6 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 		perf_sample_save_raw_data(&data, event, &raw);
 	}
 
-	if (perf_ibs == &perf_ibs_op)
-		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
-
 	/*
 	 * 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:     65a99264f5e5a2bcc8c905f7b2d633e8991672ac
Gitweb:        https://git.kernel.org/tip/65a99264f5e5a2bcc8c905f7b2d633e8991672ac
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Mon, 17 Mar 2025 09:37:55 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Mon, 17 Mar 2025 23:37:31 +01:00

perf/x86: Check data address for IBS software filter

The IBS software filter is filtering kernel samples for regular users in the PMI handler. It checks the instruction address in the IBS register to determine if it was in 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 user-space. Matteo Rizzo found that when an instruction raises an exception, IBS can report some kernel data addresses 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.

[ mingo: Clarified the changelog ]

---
 arch/x86/events/amd/ibs.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/events/amd/ibs.c +6 -4
index e7a8b87..c465005 100644
@@ -1128,8 +1128,13 @@ fail:
 		regs.flags |= PERF_EFLAGS_EXACT;
 	}
 
+	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) &&
-	    perf_exclude_event(event, &regs)) {
+	    (perf_exclude_event(event, &regs) ||
+	     ((data.sample_flags & PERF_SAMPLE_ADDR) &&
+	      event->attr.exclude_kernel && kernel_ip(data.addr)))) {
 		throttle = perf_event_account_interrupt(event);
 		goto out;
 	}
@@ -1144,9 +1149,6 @@ fail:
 		perf_sample_save_raw_data(&data, event, &raw);
 	}
 
-	if (perf_ibs == &perf_ibs_op)
-		perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
-
 	/*
 	 * 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
#2 lore raw
In reply to Namhyung Kim

Hi Namhyung,

Quoted 10 lines · Namhyung Kim

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.

PERF_SAMPLE_RAW can also leak kernel data address. How about:

arch/x86/events/amd/ibs.c +20 -1
@@ -1159,6 +1159,25 @@ static int perf_ibs_get_offset_max(struct perf_ibs *perf_ibs,
 	return 1;
 }
 
+static bool perf_ibs_swfilt_discard(struct perf_ibs *perf_ibs, struct perf_event *event,
+				    struct pt_regs *regs, struct perf_ibs_data *ibs_data)
+{
+	union ibs_op_data3 op_data3;
+
+	if (perf_exclude_event(event, regs))
+		return true;
+
+	if (perf_ibs != &perf_ibs_op || !event->attr.exclude_kernel)
+		return false;
+
+	op_data3.val = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSOPDATA3)];
+
+	/* Prevent leaking kernel 'data' addresses to unprivileged users. */
+	return unlikely(event->attr.sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_RAW) &&
+			op_data3.dc_lin_addr_valid &&
+			kernel_ip(ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)]));
+}
+
 static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 {
 	struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
@@ -1268,7 +1287,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	}
 
 	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
-	    perf_exclude_event(event, &regs)) {
+	    perf_ibs_swfilt_discard(perf_ibs, event, &regs, &ibs_data)) {
 		throttle = perf_event_account_interrupt(event);
 		goto out;
 	}
Signature · 2 lines
Thanks,
Ravi
author maintainer
#3 lore raw

Hi Ravi,

Quoted 7 lines · Ravi Bangoria

Hi Namhyung,

Quoted 4 lines · Namhyung Kim

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.

Quoted 8 lines
Quoted 6 lines

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.

PERF_SAMPLE_RAW can also leak kernel data address. How about:

Thanks for your review.

I think RAW is different as it requires perf_event_paranoid == -1. This is normally not allowed to regular users and having this means you can profile kernel with detailed tracepoints info already.

Thanks,
Namhyung

Quoted 42 lines
arch/x86/events/amd/ibs.c +20 -1
@@ -1159,6 +1159,25 @@ static int perf_ibs_get_offset_max(struct perf_ibs *perf_ibs,
 	return 1;
 }
 
+static bool perf_ibs_swfilt_discard(struct perf_ibs *perf_ibs, struct perf_event *event,
+				    struct pt_regs *regs, struct perf_ibs_data *ibs_data)
+{
+	union ibs_op_data3 op_data3;
+
+	if (perf_exclude_event(event, regs))
+		return true;
+
+	if (perf_ibs != &perf_ibs_op || !event->attr.exclude_kernel)
+		return false;
+
+	op_data3.val = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSOPDATA3)];
+
+	/* Prevent leaking kernel 'data' addresses to unprivileged users. */
+	return unlikely(event->attr.sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_RAW) &&
+			op_data3.dc_lin_addr_valid &&
+			kernel_ip(ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)]));
+}
+
 static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 {
 	struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
@@ -1268,7 +1287,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 	}
 
 	if ((event->attr.config2 & IBS_SW_FILTER_MASK) &&
-	    perf_exclude_event(event, &regs)) {
+	    perf_ibs_swfilt_discard(perf_ibs, event, &regs, &ibs_data)) {
 		throttle = perf_event_account_interrupt(event);
 		goto out;
 	}
Signature · 2 lines
Thanks,
Ravi
#4 lore raw

Hi Namhyung,

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

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.

Quoted 5 lines
Quoted 1 line

PERF_SAMPLE_RAW can also leak kernel data address. How about:

Thanks for your review.

I think RAW is different as it requires perf_event_paranoid == -1.

IBS allows PERF_SAMPLE_RAW irrespective of perf_event_paranoid. e.g.:

  $ cat /proc/sys/kernel/perf_event_paranoid
  2
  $ ./perf record -e ibs_op/swfilt=1/u --raw-samples -- make
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 1.371 MB perf.data (3957 samples) ]
  $ ./perf script -D | egrep -A2 "LdOp 1.*DcLinAddrValid 1" | egrep "IbsDCLinAd:\s*f"
  IbsDCLinAd:     fffffe00000000e8

We have two options:
1) Restrict IBS + PERF_SAMPLE_RAW to privilege users.
2) Remove all sensitive information from raw register dump before passing it to userspace. (Kernel data addresses and all physical addresses are the only sensitive info I suppose?).

2 is better IMO since it will allow unprivileged user to use IBS with full potential. wdyt?

Thanks,
Ravi

author maintainer
#5 lore raw
Quoted 24 lines · Ravi Bangoria

Hi Namhyung,

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

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.

Quoted 5 lines
Quoted 1 line

PERF_SAMPLE_RAW can also leak kernel data address. How about:

Thanks for your review.

I think RAW is different as it requires perf_event_paranoid == -1.

IBS allows PERF_SAMPLE_RAW irrespective of perf_event_paranoid. e.g.:

  $ cat /proc/sys/kernel/perf_event_paranoid
  2
Quoted 6 lines
  $ ./perf record -e ibs_op/swfilt=1/u --raw-samples -- make
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 1.371 MB perf.data (3957 samples) ]
  $ ./perf script -D | egrep -A2 "LdOp 1.*DcLinAddrValid 1" | egrep "IbsDCLinAd:\s*f"
  IbsDCLinAd:     fffffe00000000e8

Oh, I thought it was enforced in the core layer but it turns out that it's checked only by tracepoint events.

Quoted 9 lines

We have two options:
1) Restrict IBS + PERF_SAMPLE_RAW to privilege users.
2) Remove all sensitive information from raw register dump before passing it to userspace. (Kernel data addresses and all physical addresses are the only sensitive info I suppose?).

2 is better IMO since it will allow unprivileged user to use IBS with full potential. wdyt?

I'm slightly inclined to #1 for simplicity and safety, but #2 is fine to me as well.

Thanks,
Namhyung

#6 lore raw
Quoted 11 lines · Namhyung Kim
Quoted 8 lines

We have two options:
1) Restrict IBS + PERF_SAMPLE_RAW to privilege users.
2) Remove all sensitive information from raw register dump before passing it to userspace. (Kernel data addresses and all physical addresses are the only sensitive info I suppose?).

2 is better IMO since it will allow unprivileged user to use IBS with full potential. wdyt?

I'm slightly inclined to #1 for simplicity and safety, but #2 is fine to me as well.

I'd prefer #2 for superior usability, if it doesn't get too complicated.

Thanks,

Ingo

#7 lore raw

Hi Namhyung, Ingo,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

We have two options:
1) Restrict IBS + PERF_SAMPLE_RAW to privilege users.
2) Remove all sensitive information from raw register dump before passing it to userspace. (Kernel data addresses and all physical addresses are the only sensitive info I suppose?).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

2 is better IMO since it will allow unprivileged user to use IBS with full potential. wdyt?

I'm slightly inclined to #1 for simplicity and safety, but #2 is fine to me as well.

I'd prefer #2 for superior usability, if it doesn't get too complicated.

Thank you for your opinion.

I've posted a v3 based on 2nd option:
https://lore.kernel.org/r/[email protected]

@Ingo, can you please revert v2 and apply v3? (provided @Namhyung is okay with it).

Thanks,
Ravi