Skip to contentSkip to conversation

x86: entry: flush the cache if syscall error

RFC PATCH In review Kristen Carlson Accardi 5 participants 12 messages Sep 10, 2018 – Sep 12, 2018

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

RFC PATCH 1/1 x86: entry: flush the cache if syscall error

This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache.

---
 arch/x86/Kconfig        |  8 ++++++++
 arch/x86/entry/common.c | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
arch/x86/Kconfig +8 -0
index c5ff296bc5d1..8a67642ff9fe 100644
@@ -445,6 +445,14 @@ config RETPOLINE
 	  code are eliminated. Since this includes the syscall entry path,
 	  it is not entirely pointless.
 
+config SYSCALL_FLUSH
+	bool "Clear L1 Cache on syscall errors"
+	default y
+	help
+	  Select to allow the L1 cache to be cleared upon return of
+	  an error code from a syscall. This will reduce the likelyhood of
+	  speculative execution style attacks on syscalls.
+
 config INTEL_RDT
 	bool "Intel Resource Director Technology support"
 	default n
arch/x86/entry/common.c +20 -0
index 3b2490b81918..77beff541013 100644
@@ -268,6 +268,22 @@ __visible inline void syscall_return_slowpath(struct pt_regs *regs)
 	prepare_exit_to_usermode(regs);
 }
 
+__visible inline void l1_cache_flush(struct pt_regs *regs)
+{
+	if (IS_ENABLED(CONFIG_SYSCALL_FLUSH)) {
+		if (regs->ax == 0 || regs->ax == -EAGAIN ||
+		    regs->ax == -EEXIST || regs->ax == -ENOENT ||
+		    regs->ax == -EXDEV || regs->ax == -ETIMEDOUT ||
+		    regs->ax == -ENOTCONN || regs->ax == -EINPROGRESS)
+			return;
+
+		if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
+			wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
+			return;
+		}
+	}
+}
+
 #ifdef CONFIG_X86_64
 __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
 {
@@ -290,6 +306,8 @@ __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
 		regs->ax = sys_call_table[nr](regs);
 	}
 
+	l1_cache_flush(regs);
+
 	syscall_return_slowpath(regs);
 }
 #endif
@@ -338,6 +356,8 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
 #endif /* CONFIG_IA32_EMULATION */
 	}
 
+	l1_cache_flush(regs);
+
 	syscall_return_slowpath(regs);
 }
 
Signature · 1 line
2.14.4
#1 lore raw
Quoted 3 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache.

What kind of performance impact does this have on a process that e.g. attempts to access a large number of paths to which it has no access, and what is the impact on the hyperthread?

(You may want to also CC the X86 maintainers (especially Andy Lutomirski) and LKML on this series relatively early - people on the kernel-hardening list tend to have a different focus compared to the relevant maintainers, so there's some feedback that you'll probably only get once you submit this to places other than the kernel-hardening@ list.)

Quoted 11 lines
---
 arch/x86/Kconfig        |  8 ++++++++
 arch/x86/entry/common.c | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
arch/x86/Kconfig +0 -0
index c5ff296bc5d1..8a67642ff9fe 100644

arch/x86/Kconfig config RETPOLINE · line 454

Quoted 10 lines
arch/x86/Kconfig +7 -0
@@ -445,6 +445,14 @@ config RETPOLINE
          code are eliminated. Since this includes the syscall entry path,
          it is not entirely pointless.
 
+config SYSCALL_FLUSH
+       bool "Clear L1 Cache on syscall errors"
+       default y
+       help
+         Select to allow the L1 cache to be cleared upon return of
+         an error code from a syscall. This will reduce the likelyhood of
+         speculative execution style attacks on syscalls.

s/L1/L1D/ ?

Quoted 11 lines
arch/x86/Kconfig +0 -0
 config INTEL_RDT
        bool "Intel Resource Director Technology support"
        default n
arch/x86/entry/common.c +0 -0
index 3b2490b81918..77beff541013 100644
@@ -268,6 +268,22 @@ __visible inline void syscall_return_slowpath(struct pt_regs *regs)
        prepare_exit_to_usermode(regs);
 }
 

arch/x86/entry/common.c __visible inline void syscall_return_slowpath(struct pt_regs *regs) · line 280

Quoted 10 lines
arch/x86/entry/common.c +10 -0
+__visible inline void l1_cache_flush(struct pt_regs *regs)
+{
+       if (IS_ENABLED(CONFIG_SYSCALL_FLUSH)) {
+               if (regs->ax == 0 || regs->ax == -EAGAIN ||
+                   regs->ax == -EEXIST || regs->ax == -ENOENT ||
+                   regs->ax == -EXDEV || regs->ax == -ETIMEDOUT ||
+                   regs->ax == -ENOTCONN || regs->ax == -EINPROGRESS)
+                       return;
+
+               if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {

Style nit: Maybe merge this condition into the first if() ?

Quoted 30 lines
arch/x86/entry/common.c +10 -1
+                       wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
+                       return;
+               }
+       }
+}
+
 #ifdef CONFIG_X86_64
 __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
 {
@@ -290,6 +306,8 @@ __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
                regs->ax = sys_call_table[nr](regs);
        }
 
+       l1_cache_flush(regs);
+
        syscall_return_slowpath(regs);
 }
 #endif
@@ -338,6 +356,8 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
 #endif /* CONFIG_IA32_EMULATION */
        }
 
+       l1_cache_flush(regs);
+
        syscall_return_slowpath(regs);
 }
 
--

2.14.4

#4 lore raw
In reply to Kristen Carlson Accardi
Quoted 15 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache.

---
 arch/x86/Kconfig        |  8 ++++++++
 arch/x86/entry/common.c | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
arch/x86/Kconfig +0 -0
index c5ff296bc5d1..8a67642ff9fe 100644

arch/x86/Kconfig config RETPOLINE · line 450

Quoted 6 lines
arch/x86/Kconfig +3 -0
@@ -445,6 +445,14 @@ config RETPOLINE
 	  code are eliminated. Since this includes the syscall entry path,
 	  it is not entirely pointless.
 
+config SYSCALL_FLUSH
+	bool "Clear L1 Cache on syscall errors"
+	default y

"y" normally is only for "your machine will not boot without this option", and I don't think that's the case here :)

arch/x86/Kconfig config RETPOLINE · line 454

Quoted 4 lines
arch/x86/Kconfig +4 -0
+	help
+	  Select to allow the L1 cache to be cleared upon return of
+	  an error code from a syscall. This will reduce the likelyhood of
+	  speculative execution style attacks on syscalls.

Shouldn't this help text refer to the fact that this needs CPU support for this type of functionality?

I like the idea, as a "gadget" normally only is used when an out-of-bands check happens, which implies someone could be trying to do something "bad", nice job.

thanks,

greg k-h

#2 lore raw
In reply to Jann Horn
Quoted 11 lines · Jann Horn
Quoted 5 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

What kind of performance impact does this have on a process that e.g. attempts to access a large number of paths to which it has no access, and what is the impact on the hyperthread?

This will undoubtedly have a performance impact - I can certainly run some benchmarks to find out just how much in this scenario.

Quoted 7 lines

(You may want to also CC the X86 maintainers (especially Andy Lutomirski) and LKML on this series relatively early - people on the kernel-hardening list tend to have a different focus compared to the relevant maintainers, so there's some feedback that you'll probably only get once you submit this to places other than the kernel-hardening@ list.)

Thank you yes, I admit I decided to try to float this idea past a friendlier audience first :).

Quoted 16 lines
Quoted 16 lines
---
 arch/x86/Kconfig        |  8 ++++++++
 arch/x86/entry/common.c | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
arch/x86/Kconfig +0 -0
index c5ff296bc5d1..8a67642ff9fe 100644
@@ -445,6 +445,14 @@ config RETPOLINE
          code are eliminated. Since this includes the syscall
 entry path,
          it is not entirely pointless.
 
Quoted 10 lines
Quoted 8 lines
arch/x86/Kconfig +6 -0
+config SYSCALL_FLUSH
+       bool "Clear L1 Cache on syscall errors"
+       default y
+       help
+         Select to allow the L1 cache to be cleared upon return of
+         an error code from a syscall. This will reduce the

likelyhood of
+ speculative execution style attacks on syscalls.

s/L1/L1D/ ?

I can change this - the documentation for this msr mentioned that on some processors the icache might be impacted as well - think that's worth mentioning?

Quoted 60 lines
Quoted 14 lines

config INTEL_RDT
bool "Intel Resource Director Technology support" default n

arch/x86/entry/common.c +1 -0
index 3b2490b81918..77beff541013 100644
@@ -268,6 +268,22 @@ __visible inline void
 syscall_return_slowpath(struct pt_regs *regs)
        prepare_exit_to_usermode(regs);
 }
 
+__visible inline void l1_cache_flush(struct pt_regs *regs)

arch/x86/entry/common.c __visible inline void · line 281

Quoted 10 lines
arch/x86/entry/common.c +9 -1
+{
+       if (IS_ENABLED(CONFIG_SYSCALL_FLUSH)) {
+               if (regs->ax == 0 || regs->ax == -EAGAIN ||
+                   regs->ax == -EEXIST || regs->ax == -ENOENT ||
+                   regs->ax == -EXDEV || regs->ax == -ETIMEDOUT ||
+                   regs->ax == -ENOTCONN || regs->ax ==
-EINPROGRESS)
+                       return;
+
+               if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {

Style nit: Maybe merge this condition into the first if() ?

Quoted 33 lines
arch/x86/entry/common.c +6 -0
+                       wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
+                       return;
+               }
+       }
+}
+
 #ifdef CONFIG_X86_64
 __visible void do_syscall_64(unsigned long nr, struct pt_regs

*regs)
{

arch/x86/entry/common.c +2 -0
@@ -290,6 +306,8 @@ __visible void do_syscall_64(unsigned long nr,
 struct pt_regs *regs)
                regs->ax = sys_call_table[nr](regs);
        }
 
+       l1_cache_flush(regs);
+
        syscall_return_slowpath(regs);
 }

#endif

arch/x86/entry/common.c +2 -1
@@ -338,6 +356,8 @@ static __always_inline void
 do_syscall_32_irqs_on(struct pt_regs *regs)
 #endif /* CONFIG_IA32_EMULATION */
        }
 
+       l1_cache_flush(regs);
+
        syscall_return_slowpath(regs);
 }
 
--

2.14.4

#5 lore raw
In reply to Greg KH
Quoted 23 lines · Greg KH
Quoted 21 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

---
 arch/x86/Kconfig        |  8 ++++++++
 arch/x86/entry/common.c | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
arch/x86/Kconfig +0 -0
index c5ff296bc5d1..8a67642ff9fe 100644
@@ -445,6 +445,14 @@ config RETPOLINE
 	  code are eliminated. Since this includes the syscall
 entry path,
 	  it is not entirely pointless.
 
Quoted 6 lines

arch/x86/Kconfig config RETPOLINE · line 450

Quoted 3 lines
arch/x86/Kconfig +3 -0
+config SYSCALL_FLUSH
+	bool "Clear L1 Cache on syscall errors"
+	default y

"y" normally is only for "your machine will not boot without this option", and I don't think that's the case here :)

true. :). I wish there was a way to indicate that this option provided enhanced security so that people could just select all of these features at once and I could make it select based on that option.

Quoted 11 lines

arch/x86/Kconfig config RETPOLINE · line 454

Quoted 7 lines
arch/x86/Kconfig +2 -0
+	help
+	  Select to allow the L1 cache to be cleared upon return

of

arch/x86/Kconfig +1 -0
+	  an error code from a syscall. This will reduce the

likelyhood of

arch/x86/Kconfig +1 -0
+	  speculative execution style attacks on syscalls.

Shouldn't this help text refer to the fact that this needs CPU support
for this type of functionality?

OK, I will change it.

Quoted 9 lines

I like the idea, as a "gadget" normally only is used when an out-of-bands check happens, which implies someone could be trying to do
something "bad", nice job.

thanks,

greg k-h

Thanks!

#6 lore raw
In reply to Kristen Carlson Accardi
Quoted 3 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache.

Which L1 cache? There's no guarantee the task stayed on the same CPU...

- Eric

#3 lore raw
In reply to Kristen C Accardi
Quoted 8 lines · Kristen C Accardi
Quoted 7 lines · Jann Horn
Quoted 5 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
arch/x86/Kconfig +6 -0
+config SYSCALL_FLUSH
+       bool "Clear L1 Cache on syscall errors"
+       default y
+       help
+         Select to allow the L1 cache to be cleared upon return of
+         an error code from a syscall. This will reduce the

likelyhood of
+ speculative execution style attacks on syscalls.

Quoted 5 lines
Quoted 1 line

s/L1/L1D/ ?

I can change this - the documentation for this msr mentioned that on some processors the icache might be impacted as well - think that's worth mentioning?

Ah, whoops, I didn't realize that it isn't as clear-cut as "just flush L1D". I should've looked for the documentation first... nevermind, I guess. I don't want to turn this into a documentation bikeshed.

#10 lore raw
In reply to Kristen Carlson Accardi
Quoted 3 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache.

How much protection does this provide, given that it e.g. doesn't flush L2/L3 and doesn't prevent data leakage through hyperthreading and cache coherency? Is an L2/L3-based attack expected to be harder than an L1D-based one?

#7 lore raw
In reply to Eric Biggers
Quoted 10 lines · Eric Biggers
Quoted 5 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

Which L1 cache? There's no guarantee the task stayed on the same CPU...

While this is true, it is unlikely that the task switched CPUs for this type of flow (i.e. an error path, presumably caught early-ish), and worst case this would just mean we were wiping the wrong cache. I can add a comment to indicate this scenario.

#11 lore raw
In reply to Jann Horn
Quoted 8 lines · Jann Horn
Quoted 5 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

Quoted 4 lines

How much protection does this provide, given that it e.g. doesn't flush L2/L3 and doesn't prevent data leakage through hyperthreading and cache coherency? Is an L2/L3-based attack expected to be harder than an L1D-based one?

My reasoning here is that L2/L3 caches can be partitioned using something like CAT (maybe), but L1 cannot. So IMO L1 is the case that needs coverage. Also, while this doesn't address a specific exploit, the idea is that attacks on data in L1D are more common, and the performance penalty for L2/L3 flushes would be too high without a specific exploit in mind.

#8 lore raw
In reply to Kristen C Accardi
Quoted 9 lines · Kristen C Accardi
Quoted 8 lines · Eric Biggers
Quoted 5 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks on data
left behind by system calls. If we have an error returned from a syscall,
flush the L1 cache.

Quoted 5 lines
Quoted 2 lines

Which L1 cache? There's no guarantee the task stayed on the same CPU...

While this is true, it is unlikely that the task switched CPUs for this type of flow (i.e. an error path, presumably caught early-ish),

How you do know it's unlikely? What degrees of freedom might an attacker have in controlling this?

Quoted 3 lines

worst case this would just mean we were wiping the wrong cache. I can add a comment to indicate this scenario.

IOW, the protection may be useless?

- Eric

#9 lore raw
Quoted 23 lines · Eric Biggers
Quoted 11 lines · Kristen C Accardi
Quoted 10 lines · Eric Biggers
Quoted 7 lines · Kristen Carlson Accardi

This patch aims to make it harder to perform cache timing attacks
on data
left behind by system calls. If we have an error returned from a
syscall,
flush the L1 cache.

Quoted 6 lines
Quoted 2 lines

Which L1 cache? There's no guarantee the task stayed on the same CPU...

While this is true, it is unlikely that the task switched CPUs for this
type of flow (i.e. an error path, presumably caught early-ish),

How you do know it's unlikely? What degrees of freedom might an attacker have
in controlling this?

Quoted 6 lines
Quoted 4 lines

worst case this would just mean we were wiping the wrong cache. I can
add a comment to indicate this scenario.

IOW, the protection may be useless?

If the task gets moved to a different CPU, won't that completely foil a timing attack?

In other words, this protection would protect against an attack on the same CPU, and is unnecessary when a task switches CPUs?

What am I missing?

Signature · 1 line
All Rights Reversed.