Skip to contentSkip to conversation

x86/hw_breakpoint: Prevent data breakpoints on __per_cpu_offset

PATCH Applied Lai Jiangshan 5 participants 7 messages Feb 4, 2021 – Feb 6, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/hw_breakpoint: Prevent data breakpoints on __per_cpu_offset

From: Lai Jiangshan <[email protected]>

When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU GSBASE value via __per_cpu_offset or pcpu_unit_offsets.

When data breakpoint is set on __per_cpu_offset[cpu] (read-write operation), the specific cpu will be stuck in the infinite #DB loop. RCU will try to send NMI to the specific cpu, but it is not working either since NMI also relies on paranoid_entry().

Fixes eaad981291ee3("x86/entry/64: Introduce the FIND_PERCPU_BASE macro")
---
 arch/x86/kernel/hw_breakpoint.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/kernel/hw_breakpoint.c +14 -0
index 03aa33b58165..bc7493a0736f 100644
@@ -269,6 +269,20 @@ static inline bool within_cpu_entry(unsigned long addr, unsigned long end)
 			CPU_ENTRY_AREA_TOTAL_SIZE))
 		return true;
 
+	/*
+	 * When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU
+	 * GSBASE value via __per_cpu_offset or pcpu_unit_offsets.
+	 */
+#ifdef CONFIG_SMP
+	if (within_area(addr, end, (unsigned long)__per_cpu_offset,
+			sizeof(unsigned long) * nr_cpu_ids))
+		return true;
+#else
+	if (within_area(addr, end, (unsigned long)&pcpu_unit_offsets,
+			sizeof(pcpu_unit_offsets)))
+		return true;
+#endif
+
 	for_each_possible_cpu(cpu) {
 		/* The original rw GDT is being used after load_direct_gdt() */
 		if (within_area(addr, end, (unsigned long)get_cpu_gdt_rw(cpu),
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw

PATCH 2/2 x86/hw_breakpoint: Prevent data breakpoints on cpu_dr7

From: Lai Jiangshan <[email protected]>

When in guest (X86_FEATURE_HYPERVISOR), local_db_save() will read per-cpu cpu_dr7 before clear dr7 register.

local_db_save() is called at the start of exc_debug_kernel(). To avoid recursive #DB, we have to disallow data breakpoints on cpu_dr7.

Fixes 84b6a3491567a("x86/entry: Optimize local_db_save() for virt")
---
 arch/x86/kernel/hw_breakpoint.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/kernel/hw_breakpoint.c +8 -0
index bc7493a0736f..de34dd49d317 100644
@@ -307,6 +307,14 @@ static inline bool within_cpu_entry(unsigned long addr, unsigned long end)
 				(unsigned long)&per_cpu(cpu_tlbstate, cpu),
 				sizeof(struct tlb_state)))
 			return true;
+
+		/*
+		 * When in guest (X86_FEATURE_HYPERVISOR), local_db_save()
+		 * will read per-cpu cpu_dr7 before clear dr7 register.
+		 */
+		if (within_area(addr, end, (unsigned long)&per_cpu(cpu_dr7, cpu),
+				sizeof(cpu_dr7)))
+			return true;
 	}
 
 	return false;
Signature · 1 line
2.19.1.6.gb485710b
merge bot Applied
#2 lore raw

[tip: x86/urgent] x86/debug: Prevent data breakpoints on cpu_dr7

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

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

Commit-ID:     3943abf2dbfae9ea4d2da05c1db569a0603f76da
Gitweb:        https://git.kernel.org/tip/3943abf2dbfae9ea4d2da05c1db569a0603f76da
Author:        Lai Jiangshan <[email protected]>
AuthorDate:    Thu, 04 Feb 2021 23:27:07 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 05 Feb 2021 20:13:12 +01:00

x86/debug: Prevent data breakpoints on cpu_dr7

local_db_save() is called at the start of exc_debug_kernel(), reads DR7 and disables breakpoints to prevent recursion.

When running in a guest (X86_FEATURE_HYPERVISOR), local_db_save() reads the per-cpu variable cpu_dr7 to check whether a breakpoint is active or not before it accesses DR7.

A data breakpoint on cpu_dr7 therefore results in infinite #DB recursion.

Disallow data breakpoints on cpu_dr7 to prevent that.

Fixes 84b6a3491567a("x86/entry: Optimize local_db_save() for virt")
---
 arch/x86/kernel/hw_breakpoint.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/kernel/hw_breakpoint.c +8 -0
index 012ed82..668a4a6 100644
@@ -307,6 +307,14 @@ static inline bool within_cpu_entry(unsigned long addr, unsigned long end)
 				(unsigned long)&per_cpu(cpu_tlbstate, cpu),
 				sizeof(struct tlb_state)))
 			return true;
+
+		/*
+		 * When in guest (X86_FEATURE_HYPERVISOR), local_db_save()
+		 * will read per-cpu cpu_dr7 before clear dr7 register.
+		 */
+		if (within_area(addr, end, (unsigned long)&per_cpu(cpu_dr7, cpu),
+				sizeof(cpu_dr7)))
+			return true;
 	}
 
 	return false;
#3 lore raw
In reply to Lai Jiangshan
Quoted 10 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU GSBASE value via __per_cpu_offset or pcpu_unit_offsets.

When data breakpoint is set on __per_cpu_offset[cpu] (read-write operation), the specific cpu will be stuck in the infinite #DB loop. RCU will try to send NMI to the specific cpu, but it is not working either since NMI also relies on paranoid_entry().

Should we consider having a .percpu..noinstr section and having objtool enforce this?

#4 lore raw
Quoted 11 lines · Andy Lutomirski
Quoted 7 lines · Lai Jiangshan

When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU GSBASE value via __per_cpu_offset or pcpu_unit_offsets.

When data breakpoint is set on __per_cpu_offset[cpu] (read-write operation), the specific cpu will be stuck in the infinite #DB loop. RCU will try to send NMI to the specific cpu, but it is not working either since NMI also relies on paranoid_entry().

Should we consider having a .percpu..noinstr section and having objtool enforce this?

I think so.

Thanks,

tglx

#5 lore raw
Quoted 10 lines · Thomas Gleixner
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Lai Jiangshan

When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU GSBASE value via __per_cpu_offset or pcpu_unit_offsets.

When data breakpoint is set on __per_cpu_offset[cpu] (read-write operation), the specific cpu will be stuck in the infinite #DB loop. RCU will try to send NMI to the specific cpu, but it is not working either since NMI also relies on paranoid_entry().

Quoted 4 lines
Quoted 2 lines

Should we consider having a .percpu..noinstr section and having objtool enforce this?

I think so.

I'll put it on the TODO list somewhere ...

merge bot Applied
#6 lore raw

[tip: x86/urgent] x86/debug: Prevent data breakpoints on __per_cpu_offset

In reply to Lai Jiangshan
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     c4bed4b96918ff1d062ee81fdae4d207da4fa9b0
Gitweb:        https://git.kernel.org/tip/c4bed4b96918ff1d062ee81fdae4d207da4fa9b0
Author:        Lai Jiangshan <[email protected]>
AuthorDate:    Thu, 04 Feb 2021 23:27:06 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 05 Feb 2021 20:13:11 +01:00

x86/debug: Prevent data breakpoints on __per_cpu_offset

When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU GSBASE value via __per_cpu_offset or pcpu_unit_offsets.

When a data breakpoint is set on __per_cpu_offset[cpu] (read-write operation), the specific CPU will be stuck in an infinite #DB loop.

RCU will try to send an NMI to the specific CPU, but it is not working either since NMI also relies on paranoid_entry(). Which means it's undebuggable.

Fixes eaad981291ee3("x86/entry/64: Introduce the FIND_PERCPU_BASE macro")
---
 arch/x86/kernel/hw_breakpoint.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/kernel/hw_breakpoint.c +14 -0
index 6694c0f..012ed82 100644
@@ -269,6 +269,20 @@ static inline bool within_cpu_entry(unsigned long addr, unsigned long end)
 			CPU_ENTRY_AREA_TOTAL_SIZE))
 		return true;
 
+	/*
+	 * When FSGSBASE is enabled, paranoid_entry() fetches the per-CPU
+	 * GSBASE value via __per_cpu_offset or pcpu_unit_offsets.
+	 */
+#ifdef CONFIG_SMP
+	if (within_area(addr, end, (unsigned long)__per_cpu_offset,
+			sizeof(unsigned long) * nr_cpu_ids))
+		return true;
+#else
+	if (within_area(addr, end, (unsigned long)&pcpu_unit_offsets,
+			sizeof(pcpu_unit_offsets)))
+		return true;
+#endif
+
 	for_each_possible_cpu(cpu) {
 		/* The original rw GDT is being used after load_direct_gdt() */
 		if (within_area(addr, end, (unsigned long)get_cpu_gdt_rw(cpu),