Skip to contentSkip to conversation

KAISER fixlets..

PATCH In review Peter Zijlstra 5 participants 15 messages Nov 27, 2017 – Nov 28, 2017

Subsystems (4)
lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

cover KAISER fixlets..

Thomas asked me to post the various patches I did today so he doesn't need to go dig them out the various email threads and make em pretty.

They're all tested in so far that they booted and managed to build the next kernel.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#1 lore raw

PATCH 1/5 x86/mm/kaiser: Alternative ESPFIX

Change the asm to do the CR3 switcheroo so we can remove the magic mappings.

Since RDI is unused after SWAPGS we can use it as a scratch reg for SWITCH_TO_KERNEL. And once we've computed the new RSP (in RAX) we no longer need RDI and can again use it as scratch reg for SWITCH_TO_USER.

---
 arch/x86/entry/entry_64.S   |   11 ++++++++---
 arch/x86/kernel/espfix_64.c |   10 ++--------
 2 files changed, 10 insertions(+), 11 deletions(-)
arch/x86/entry/entry_64.S +8 -3
@@ -825,7 +825,9 @@ ENTRY(native_iret)
 	 */
 
 	pushq	%rdi				/* Stash user RDI */
-	SWAPGS
+	SWAPGS					/* to kernel GS */
+	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
+
 	movq	PER_CPU_VAR(espfix_waddr), %rdi
 	movq	%rax, (0*8)(%rdi)		/* user RAX */
 	movq	(1*8)(%rsp), %rax		/* user RIP */
@@ -841,7 +843,6 @@ ENTRY(native_iret)
 	/* Now RAX == RSP. */
 
 	andl	$0xffff0000, %eax		/* RAX = (RSP & 0xffff0000) */
-	popq	%rdi				/* Restore user RDI */
 
 	/*
 	 * espfix_stack[31:16] == 0.  The page tables are set up such that
@@ -852,7 +853,11 @@ ENTRY(native_iret)
 	 * still points to an RO alias of the ESPFIX stack.
 	 */
 	orq	PER_CPU_VAR(espfix_stack), %rax
-	SWAPGS
+
+	SWITCH_TO_USER_CR3 scratch_reg=%rdi	/* to user CR3 */
+	SWAPGS					/* to user GS */
+	popq	%rdi				/* Restore user RDI */
+
 	movq	%rax, %rsp
 	UNWIND_HINT_IRET_REGS offset=8
 
arch/x86/kernel/espfix_64.c +2 -8
@@ -61,8 +61,8 @@
 #define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO)
 
 /* This contains the *bottom* address of the espfix stack */
-DEFINE_PER_CPU_USER_MAPPED(unsigned long, espfix_stack);
-DEFINE_PER_CPU_USER_MAPPED(unsigned long, espfix_waddr);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned long, espfix_stack);
+DEFINE_PER_CPU_READ_MOSTLY(unsigned long, espfix_waddr);
 
 /* Initialization mutex - should this be a spinlock? */
 static DEFINE_MUTEX(espfix_init_mutex);
@@ -226,10 +226,4 @@ void init_espfix_ap(int cpu)
 	per_cpu(espfix_stack, cpu) = addr;
 	per_cpu(espfix_waddr, cpu) = (unsigned long)stack_page
 				      + (addr & ~PAGE_MASK);
-	/*
-	 * _PAGE_GLOBAL is not really required.  This is not a hot
-	 * path, but we do it here for consistency.
-	 */
-	kaiser_add_mapping((unsigned long)stack_page, PAGE_SIZE,
-			__PAGE_KERNEL | _PAGE_GLOBAL);
 }

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#2 lore raw

Re: [PATCH 1/5] x86/mm/kaiser: Alternative ESPFIX

Quoted 7 lines · Peter Zijlstra

Change the asm to do the CR3 switcheroo so we can remove the magic mappings.

Since RDI is unused after SWAPGS we can use it as a scratch reg for SWITCH_TO_KERNEL. And once we've computed the new RSP (in RAX) we no longer need RDI and can again use it as scratch reg for SWITCH_TO_USER.

Forgot to note; this passes tools/testing/selftests/x86/sigreturn_64.

Quoted 1 line

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#3 lore raw

Re: [PATCH 1/5] x86/mm/kaiser: Alternative ESPFIX

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra

Change the asm to do the CR3 switcheroo so we can remove the magic mappings.

Since RDI is unused after SWAPGS we can use it as a scratch reg for SWITCH_TO_KERNEL. And once we've computed the new RSP (in RAX) we no longer need RDI and can again use it as scratch reg for SWITCH_TO_USER.

This definitely looks like the right thing. Either I missed something obvious before, or Andy's entry rework made this much more obviously correct to do the simple thing here.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#4 lore raw

PATCH 2/5 x86/mm/kaiser: Add a banner

In reply to Peter Zijlstra

So we can more easily see if the shiny got enabled.

---
 arch/x86/mm/kaiser.c |    2 ++
 1 file changed, 2 insertions(+)
arch/x86/mm/kaiser.c +2 -0
@@ -425,6 +425,8 @@ void __init kaiser_init(void)
 	if (!kaiser_enabled)
 		return;
 
+	printk("All your KAISER are belong to us\n");
+
 	kaiser_init_all_pgds();
 
 	for_each_possible_cpu(cpu) {
maintainer
#5 lore raw

Re: [PATCH 2/5] x86/mm/kaiser: Add a banner

Quoted 10 lines · Peter Zijlstra

So we can more easily see if the shiny got enabled.

---
 arch/x86/mm/kaiser.c |    2 ++
 1 file changed, 2 insertions(+)

arch/x86/mm/kaiser.c void __init kaiser_init(void) · line 429

Quoted 5 lines
arch/x86/mm/kaiser.c +2 -0
@@ -425,6 +425,8 @@ void __init kaiser_init(void)
        if (!kaiser_enabled)
                return;
 
+       printk("All your KAISER are belong to us\n");
+

All your incomprehensible academic names are belong to us.

On a serious note, can we please banish the name KAISER from all the user-facing bits? No one should be setting a boot option that has a name based on an academic project called "Kernel Address Isolation to have Side-channels Efficiently Removed". We're not efficiently removing side channels. The side channels are still very much there. Heck, the series as currently presented doesn't even rescue kASLR. It could*, if we were to finish the work that I mostly started and completely banish all the normal kernel mappings from the shadow** tables. We're rather inefficiently (and partially!) mitigating the fact that certain CPU designers have had their heads up their collective arses for *years* and have failed to pay attention to numerous academic papers documenting that fact.

Let's call the user facing bits "separate user pagetables". If we want to make it conditioned on a future cpu cap called X86_BUG_REALLY_DUMB_SIDE_CHANNELS, great, assuming a better CPU ever shows up. But please let's not make users look up WTF "KAISER" means.

* No one ever documented the %*!& side channels AFAIK, so everything we're talking about here is mostly speculation.

** The word "shadow" needs to die, too. I know what shadow page tables are, and they have *nothing* to do with KAISER.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [PATCH 2/5] x86/mm/kaiser: Add a banner

Quoted 37 lines · Andy Lutomirski
Quoted 10 lines · Peter Zijlstra

So we can more easily see if the shiny got enabled.

---
 arch/x86/mm/kaiser.c |    2 ++
 1 file changed, 2 insertions(+)

arch/x86/mm/kaiser.c void __init kaiser_init(void) · line 429

Quoted 5 lines
arch/x86/mm/kaiser.c +2 -0
@@ -425,6 +425,8 @@ void __init kaiser_init(void)
        if (!kaiser_enabled)
                return;
 
+       printk("All your KAISER are belong to us\n");
+

All your incomprehensible academic names are belong to us.

On a serious note, can we please banish the name KAISER from all the user-facing bits? No one should be setting a boot option that has a name based on an academic project called "Kernel Address Isolation to have Side-channels Efficiently Removed". We're not efficiently removing side channels. The side channels are still very much there. Heck, the series as currently presented doesn't even rescue kASLR. It could*, if we were to finish the work that I mostly started and completely banish all the normal kernel mappings from the shadow** tables. We're rather inefficiently (and partially!) mitigating the fact that certain CPU designers have had their heads up their collective arses for *years* and have failed to pay attention to numerous academic papers documenting that fact.

Let's call the user facing bits "separate user pagetables". If we want to make it conditioned on a future cpu cap called X86_BUG_REALLY_DUMB_SIDE_CHANNELS, great, assuming a better CPU ever shows up. But please let's not make users look up WTF "KAISER" means.

Quoted 5 lines

* No one ever documented the %*!& side channels AFAIK, so everything we're talking about here is mostly speculation.

** The word "shadow" needs to die, too. I know what shadow page tables are, and they have *nothing* to do with KAISER.

+1. Somebody please rename KAISER and shadow page tables for more clarity.

To fix KASLR I think we need to move (at least parts of) .entry.text, .irqentry.text, and .entry_trampoline into their own fixed section(s). Is there anything else missing?

Signature · 1 line
Josh
maintainer
#7 lore raw

Re: [PATCH 2/5] x86/mm/kaiser: Add a banner

Quoted 44 lines · Josh Poimboeuf
Quoted 37 lines · Andy Lutomirski
Quoted 10 lines · Peter Zijlstra

So we can more easily see if the shiny got enabled.

---
 arch/x86/mm/kaiser.c |    2 ++
 1 file changed, 2 insertions(+)

arch/x86/mm/kaiser.c void __init kaiser_init(void) · line 429

Quoted 5 lines
arch/x86/mm/kaiser.c +2 -0
@@ -425,6 +425,8 @@ void __init kaiser_init(void)
        if (!kaiser_enabled)
                return;
 
+       printk("All your KAISER are belong to us\n");
+

All your incomprehensible academic names are belong to us.

On a serious note, can we please banish the name KAISER from all the user-facing bits? No one should be setting a boot option that has a name based on an academic project called "Kernel Address Isolation to have Side-channels Efficiently Removed". We're not efficiently removing side channels. The side channels are still very much there. Heck, the series as currently presented doesn't even rescue kASLR. It could*, if we were to finish the work that I mostly started and completely banish all the normal kernel mappings from the shadow** tables. We're rather inefficiently (and partially!) mitigating the fact that certain CPU designers have had their heads up their collective arses for *years* and have failed to pay attention to numerous academic papers documenting that fact.

Let's call the user facing bits "separate user pagetables". If we want to make it conditioned on a future cpu cap called X86_BUG_REALLY_DUMB_SIDE_CHANNELS, great, assuming a better CPU ever shows up. But please let's not make users look up WTF "KAISER" means.

Quoted 5 lines

* No one ever documented the %*!& side channels AFAIK, so everything we're talking about here is mostly speculation.

** The word "shadow" needs to die, too. I know what shadow page tables are, and they have *nothing* to do with KAISER.

Quoted 6 lines

+1. Somebody please rename KAISER and shadow page tables for more clarity.

To fix KASLR I think we need to move (at least parts of) .entry.text, .irqentry.text, and .entry_trampoline into their own fixed section(s). Is there anything else missing?

We need to completely eliminate anything that maps normal kernel addresses into the usermode tables.

Quoted 3 lines

--
Josh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#8 lore raw

Re: [PATCH 2/5] x86/mm/kaiser: Add a banner

In reply to Andy Lutomirski
Quoted 2 lines · Andy Lutomirski

** The word "shadow" needs to die, too. I know what shadow page tables are, and they have *nothing* to do with KAISER.

ACK to that. Calling them "shadow" is mishandling an already overloaded term.

Let's call them the user page tables as we call the other the kernel page tables already. Nicely balanced.

Signature · 10 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author maintainer
#9 lore raw

PATCH 3/5 x86/mm/kaiser: Revert ("Map the entry stack variables")

In reply to Peter Zijlstra

Because its not needed. The entry_SYSCALL_64 code isn't ran when KAISER is enabled, we use entry_SYSCALL_64_trampoline instead.

---
 arch/x86/kernel/cpu/common.c |    2 +-
 arch/x86/kernel/process_64.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/common.c +1 -1
@@ -1515,7 +1515,7 @@ EXPORT_PER_CPU_SYMBOL(__preempt_count);
  * the top of the kernel stack.  Use an extra percpu variable to track the
  * top of the kernel stack directly.
  */
-DEFINE_PER_CPU_USER_MAPPED(unsigned long, cpu_current_top_of_stack) =
+DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
 	(unsigned long)&init_thread_union + THREAD_SIZE;
 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
 
arch/x86/kernel/process_64.c +1 -1
@@ -59,7 +59,7 @@
 #include <asm/unistd_32_ia32.h>
 #endif
 
-__visible DEFINE_PER_CPU_USER_MAPPED(unsigned long, rsp_scratch);
+__visible DEFINE_PER_CPU(unsigned long, rsp_scratch);
 
 /* Prints also some state that isn't saved in the pt_regs */
 void __show_regs(struct pt_regs *regs, int all)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#10 lore raw

PATCH 4/5 x86/mm/kaiser: Remove superfluous SWITCH_TO_KERNEL

In reply to Peter Zijlstra

We never use this code-path with KAISER enabled.

Fixes ("Prepare the x86/entry assembly code for entry/exit CR3 switching")
---
 arch/x86/entry/entry_64.S |    8 --------
 1 file changed, 8 deletions(-)
arch/x86/entry/entry_64.S +0 -8
@@ -201,14 +201,6 @@ ENTRY(entry_SYSCALL_64)
 
 	swapgs
 	movq	%rsp, PER_CPU_VAR(rsp_scratch)
-
-	/*
-	 * The kernel CR3 is needed to map the process stack, but we
-	 * need a scratch register to be able to load CR3.  %rsp is
-	 * clobberable right now, so use it as a scratch register.
-	 * %rsp will look crazy here for a couple instructions.
-	 */
-	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
 
 	/* Construct struct pt_regs on stack */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#11 lore raw

Re: [PATCH 4/5] x86/mm/kaiser: Remove superfluous SWITCH_TO_KERNEL

Quoted 1 line · Peter Zijlstra

We never use this code-path with KAISER enabled.

...

arch/x86/entry/entry_64.S ENTRY(entry_SYSCALL_64) · line 204

Quoted 13 lines
arch/x86/entry/entry_64.S +0 -8
@@ -201,14 +201,6 @@ ENTRY(entry_SYSCALL_64)
 
 	swapgs
 	movq	%rsp, PER_CPU_VAR(rsp_scratch)
-
-	/*
-	 * The kernel CR3 is needed to map the process stack, but we
-	 * need a scratch register to be able to load CR3.  %rsp is
-	 * clobberable right now, so use it as a scratch register.
-	 * %rsp will look crazy here for a couple instructions.
-	 */
-	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp

What's the mechanism that we use to switch between the two versions of the SYSCALL entry? It wasn't obvious from some grepping.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#12 lore raw

Re: [PATCH 4/5] x86/mm/kaiser: Remove superfluous SWITCH_TO_KERNEL

Quoted 5 lines · Dave Hansen
Quoted 1 line · Peter Zijlstra

We never use this code-path with KAISER enabled.

...

Quoted 2 lines
(quoted diff) +0 -0
@@ -201,14 +201,6 @@ ENTRY(entry_SYSCALL_64)
 
Quoted 14 lines

arch/x86/entry/entry_64.S ENTRY(entry_SYSCALL_64) · line 204

Quoted 11 lines
arch/x86/entry/entry_64.S +0 -8
 	swapgs
 	movq	%rsp, PER_CPU_VAR(rsp_scratch)
-
-	/*
-	 * The kernel CR3 is needed to map the process stack, but we
-	 * need a scratch register to be able to load CR3.  %rsp is
-	 * clobberable right now, so use it as a scratch register.
-	 * %rsp will look crazy here for a couple instructions.
-	 */
-	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp

What's the mechanism that we use to switch between the two versions of the SYSCALL entry? It wasn't obvious from some grepping.

the next patch, the code in tip will in fact never use this code.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author maintainer
#13 lore raw

PATCH 5/5 x86/mm/kaiser: Disable the SYSCALL-64 trampoline along with KAISER

In reply to Peter Zijlstra

If we don't use KAISER we don't need the trampoline, use the old entry code.

---
 arch/x86/kernel/cpu/common.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +4 -1
@@ -1442,7 +1442,10 @@ void syscall_init(void)
 		(entry_SYSCALL_64_trampoline - _entry_trampoline);
 
 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
-	wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
+	if (kaiser_enabled)
+		wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
+	else
+		wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
 
 #ifdef CONFIG_IA32_EMULATION
 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#14 lore raw

Re: [PATCH 5/5] x86/mm/kaiser: Disable the SYSCALL-64 trampoline along with KAISER

arch/x86/kernel/cpu/common.c void syscall_init(void) · line 1448

Quoted 11 lines · Peter Zijlstra
arch/x86/kernel/cpu/common.c +4 -1
@@ -1442,7 +1442,10 @@ void syscall_init(void)
 		(entry_SYSCALL_64_trampoline - _entry_trampoline);
 
 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
-	wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
+	if (kaiser_enabled)
+		wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
+	else
+		wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);

Heh, ask and ye shall receive, I guess.

We do need a Documentation/ update now. For this, and other things. I'll put something together.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>