Control-flow Enforcement: Indirect Branch Tracking
Subsystems (7)
v12 cover Control-flow Enforcement: Indirect Branch Tracking
Control-flow Enforcement (CET) is a new Intel processor feature that blocks return/jump-oriented programming attacks. Details are in "Intel 64 and IA-32 Architectures Software Developer's Manual" [1].
This is the second part of CET and enables Indirect Branch Tracking (IBT). It is built on top of the shadow stack series.
Changes in v12:
- Replace obj file list with $(vobjs) $(vobjs32) in VDSO Makefile.
- Disable vsyscall emulation only when it is attempted.
- Split out ptrace patch.
[1] Intel 64 and IA-32 Architectures Software Developer's Manual:
https://software.intel.com/en-us/download/intel-64-and-ia-32- architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4
[2] Indirect Branch Tracking patches v11.
https://lkml.kernel.org/r/[email protected]/
H.J. Lu (3):
x86/cet/ibt: Update arch_prctl functions for Indirect Branch Tracking x86/vdso/32: Add ENDBR32 to __kernel_vsyscall entry point x86/vdso: Insert endbr32/endbr64 to vDSO
Yu-cheng Yu (5):
x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking x86/cet/ibt: User-mode Indirect Branch Tracking support x86/cet/ibt: Handle signals for Indirect Branch Tracking x86/cet/ibt: ELF header parsing for Indirect Branch Tracking
x86: Disallow vsyscall emulation when CET is enabled
arch/x86/Kconfig | 18 ++++++ arch/x86/entry/vdso/Makefile | 4 ++ arch/x86/entry/vdso/vdso32/system_call.S | 3 + arch/x86/entry/vsyscall/vsyscall_64.c | 9 +++ arch/x86/include/asm/cet.h | 3 + arch/x86/include/asm/disabled-features.h | 8 ++- arch/x86/kernel/cet.c | 60 ++++++++++++++++++- arch/x86/kernel/cet_prctl.c | 8 ++- arch/x86/kernel/cpu/common.c | 17 ++++++ arch/x86/kernel/fpu/signal.c | 8 ++- arch/x86/kernel/process_64.c | 8 +++ .../arch/x86/include/asm/disabled-features.h | 8 ++- 12 files changed, 146 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.0
PATCH v12 1/8 x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Introduce Kconfig option X86_INTEL_BRANCH_TRACKING_USER.
Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
---
v10:
- Change build-time CET check to config depends on.
arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/Kconfig +16 -0
@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when + the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say y + config EFI bool "EFI runtime service support" depends on ACPI
Signature · 1 line
2.21.0
Randy Dunlap
reviewerRe: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Hi,
If you do another version of this:
Quoted 29 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +5
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection)
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 10 lines
arch/x86/Kconfig +10
-0+ select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when + the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say y
If unsure, say y.
Quoted 5 lines
arch/x86/Kconfig +1
-0+ config EFI bool "EFI runtime service support" depends on ACPI
Signature · 1 line
~Randy
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 38 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 33 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when
Quoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the Kconfig.
But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 49 lines · Pavel Machek
Quoted 38 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 33 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Quoted 7 lines
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
CET enabled kernel runs on all x86-64 processors. All my machines are running the same CET enabled kernel binary.
Signature · 1 line
H.J.
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Hi!
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 9 lines
arch/x86/Kconfig +9
-0+ help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when + the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.
Quoted 7 lines
Quoted 4 lines
But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
CET enabled kernel runs on all x86-64 processors. All my machines are running the same CET enabled kernel binary.
I believe that.
But enabling CET in kernel is useless on Core 2 Duo machine, right?
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 23 lines · Pavel Machek
Hi!
Quoted 20 lines
Quoted 19 lines
Quoted 11 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 9 lines
arch/x86/Kconfig +9
-0+ help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when + the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
Quoted 7 lines
Quoted 2 lines
CET enabled kernel runs on all x86-64 processors. All my machines are running the same CET enabled kernel binary.
I believe that.
But enabling CET in kernel is useless on Core 2 Duo machine, right?
This is very important for CET kernel to run on Core 2 Duo machine. Otherwise, a distro needs to provide 2 kernel binaries, one for CET CPU and one for non-CET CPU.
Signature · 1 line
H.J.
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
In reply to Pavel MachekQuoted 48 lines · Pavel Machek
Quoted 38 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 33 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Quoted 6 lines
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
I will revise the wording if there is another version. But a CET-capable kernel can run on legacy systems. We have been testing that combination.
Yu-cheng
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 50 lines · Yu-cheng
Quoted 49 lines · Pavel Machek
Quoted 36 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 31 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.
Quoted 6 lines
Quoted 3 lines
But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
I will revise the wording if there is another version. But a CET-capable kernel can run on legacy systems. We have been testing that combination.
Yes, but enabling CET is unneccessary overhead on older systems. And Kconfig is great place to explain that.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 56 lines · Pavel Machek
Quoted 54 lines · Yu-cheng
Quoted 52 lines · Pavel Machek
Quoted 36 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 31 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
Quoted 6 lines
Quoted 2 lines
I will revise the wording if there is another version. But a CET-capable kernel can run on legacy systems. We have been testing that combination.
Yes, but enabling CET is unneccessary overhead on older systems. And
Kconfigis great place to explain that.
I can't tell any visible CET kernel overhead on my non-CET machines.
Signature · 1 line
H.J.
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 57 lines · H.J. Lu
Quoted 56 lines · Pavel Machek
Quoted 54 lines · Yu-cheng
Quoted 52 lines · Pavel Machek
Quoted 36 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 31 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
I will revise the wording if there is another version. But a CET-capable kernel can run on legacy systems. We have been testing that combination.
Yes, but enabling CET is unneccessary overhead on older systems. And
Kconfigis great place to explain that.I can't tell any visible CET kernel overhead on my non-CET machines.
I assume you are not a troll but you sound a bit like one.
Please list kernel size before and after enablingX86_INTEL_CET option(s).
That's the overhead I'm talking about, and that's why Kconfig should explain what machines this is useful on.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
In reply to Pavel MachekQuoted 55 lines · Pavel Machek
Quoted 54 lines · Yu-cheng
Quoted 52 lines · Pavel Machek
Quoted 36 lines · Randy Dunlap
Hi,
If you do another version of this:
Quoted 31 lines · Yu-cheng Yu
Introduce
KconfigoptionX86_INTEL_BRANCH_TRACKING_USER.Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
Signed-off-by Yu-cheng Yu <[email protected]>---
v10:
- Change build-time CET check to config depends on.arch/x86/Kconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/x86/Kconfig +9
-0@@ -1963,6 +1963,22 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + depends on $(cc-option,-fcf-protection) + select X86_INTEL_CET + help + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active whenQuoted 8 lines
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1980
Quoted 6 lines
arch/x86/Kconfig +6
-0+ the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say yIf unsure, say y.
Actually, it would be "If unsure, say Y.", to be consistent with the rest of the
Kconfig.But I wonder if Yes by default is good idea. Only very new CPUs will support this, right? Are they even available at the market? Should the help text say "if your CPU is Whatever Lake or newer, ...." :-) ?
Quoted 5 lines
Quoted 2 lines
I will revise the wording if there is another version. But a CET-capable kernel can run on legacy systems. We have been testing that combination.
Yes, but enabling CET is unneccessary overhead on older systems. And
Kconfigis great place to explain that.
Maybe I'll add:
If the kernel is to be used only on older systems that do not support IBT, and the size of the binary is important, you can save 900 KB by disabling this feature.
Otherwise, if unsure, say Y.
How is that?
Thanks,
Yu-cheng
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1967
Quoted 2 lines · Yu-cheng
arch/x86/Kconfig +2
-0+config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode"
Take the "Intel " and "INTEL_" out, please. It will only cause us all pain later if some of our x86 compatriots decide to implement this.
Quoted 5 lines
If the kernel is to be used only on older systems that do not support IBT, and the size of the binary is important, you can save 900 KB by disabling this feature.
Otherwise, if unsure, say Y.
900k seems like a *lot*. Where the heck does that come from?
Also, comments like that don't age very well. Consider:
Support for this feature is only known to be present on Intel processors released in 2020 or later. This feature is also known to increase kernel text size substantially.
If unsure, say N.
The 900KB is probably wrong today in a lot of configurations, and will; only get *more* wrong over time.
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 15 lines · Dave Hansen
Review
Quoted 2 lines · Yu-cheng
(quoted diff) +2
-0+config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode"Take the "Intel " and "
INTEL_" out, please. It will only cause us all pain later if some of our x86 compatriots decide to implement this.Quoted 5 lines
If the kernel is to be used only on older systems that do not support IBT, and the size of the binary is important, you can save 900 KB by disabling this feature.
Otherwise, if unsure, say Y.
900k seems like a *lot*. Where the heck does that come from?
Quoted 8 lines
Also, comments like that don't age very well. Consider:
Support for this feature is only known to be present on Intel processors released in 2020 or later. This feature is also known to increase kernel text size substantially.
If unsure, say N.
Thanks!
Quoted 3 lines
The 900KB is probably wrong today in a lot of configurations, and will; only get *more* wrong over time.
I was talking about the vmlinux file, and probably should have said bzImage size, which has 14 KB increase when CET is enabled.
Yu-cheng
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 5 lines · Yu-cheng
Quoted 2 lines
The 900KB is probably wrong today in a lot of configurations, and will; only get *more* wrong over time.
I was talking about the vmlinux file, and probably should have said bzImage size, which has 14 KB increase when CET is enabled.
Well, vmlinux size is important too. 1 page of vmlinux size means one fewer page of memory available for real use.
I would really encourage you when you write to try to be specific and use as much plain language as possible without being verbose. Most people understand things like "this feature increases kernel text size".
I wouldn't expect most folks who can type "make oldconfig; make
install" to understands the difference between vmlinux and bzImage.
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Yu-cheng
Quoted 2 lines
The 900KB is probably wrong today in a lot of configurations, and will; only get *more* wrong over time.
I was talking about the vmlinux file, and probably should have said bzImage size, which has 14 KB increase when CET is enabled.
Quoted 9 lines
Well, vmlinux size is important too. 1 page of vmlinux size means one fewer page of memory available for real use.
I would really encourage you when you write to try to be specific and use as much plain language as possible without being verbose. Most people understand things like "this feature increases kernel text size".
I wouldn't expect most folks who can type "make oldconfig; make
install" to understands the difference between vmlinux and bzImage.
Ok, thanks!
Yu-cheng
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
In reply to Dave HansenHi!
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode"Take the "Intel " and "
INTEL_" out, please. It will only cause us all pain later if some of our x86 compatriots decide to implement this.
Are other x86 manufacturers legally allowed to implement that?
Quoted 8 lines
Quoted 5 lines
If the kernel is to be used only on older systems that do not support IBT, and the size of the binary is important, you can save 900 KB by disabling this feature.
Otherwise, if unsure, say Y.
900k seems like a *lot*. Where the heck does that come from?
Quoted 7 lines
Also, comments like that don't age very well. Consider:
Support for this feature is only known to be present on Intel processors released in 2020 or later. This feature is also known to increase kernel text size substantially.
If unsure, say N.
That is much better, thanks.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 1/8] x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Quoted 5 lines · Pavel Machek
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode"Take the "Intel " and "
INTEL_" out, please. It will only cause us all pain later if some of our x86 compatriots decide to implement this.Are other x86 manufacturers legally allowed to implement that?
I don't know and frankly don't want to know.
We've seen AMD and Intel both implement plenty of features from the other. Protection Keys for Userspace is the most recent example.
Unless there's evidence to the contrary, I think the best thing for the kernel is to assume that any feature can show up on any vendor's CPU.
PATCH v12 2/8 x86/cet/ibt: User-mode Indirect Branch Tracking support
In reply to Yu-cheng YuIntroduce user-mode Indirect Branch Tracking (IBT) support. Update setup routines to include IBT.
---
v10:
- Change no_cet_ibt to no_user_ibt.
v9:
- Change cpu_feature_enabled() to static_cpu_has().
v2:
- Change noibt to no_cet_ibt.
arch/x86/include/asm/cet.h | 3 ++ arch/x86/include/asm/disabled-features.h | 8 ++++- arch/x86/kernel/cet.c | 33 +++++++++++++++++++ arch/x86/kernel/cpu/common.c | 17 ++++++++++ .../arch/x86/include/asm/disabled-features.h | 8 ++++- 5 files changed, 67 insertions(+), 2 deletions(-)
arch/x86/include/asm/cet.h +3 -0
@@ -15,6 +15,7 @@ struct cet_status {unsigned long shstk_base;unsigned long shstk_size; unsigned int locked:1; + unsigned int ibt_enabled:1; };#ifdef CONFIG_X86_INTEL_CET
@@ -26,6 +27,8 @@ void cet_free_shstk(struct task_struct *p);int cet_verify_rstor_token (bool ia32,unsigned long ssp,unsigned long *new_ssp);void cet_restore_signal (struct sc_ext *sc);int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc); +int cet_setup_ibt (void ); +void cet_disable_ibt (void );#else static inline int prctl_cet (int option,u64 arg2) {return -EINVAL ; } static inline int cet_setup_thread_shstk(struct task_struct *p,
arch/x86/include/asm/disabled-features.h +7 -1
@@ -62,6 +62,12 @@#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31))#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -83,7 +89,7 @@#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)#endif /* _ASM_X86_DISABLED_FEATURES_H */
arch/x86/kernel/cet.c +33 -0
@@ -13,6 +13,8 @@#include <linux/uaccess.h> #include <linux/sched/signal.h> #include <linux/compat.h> +#include <linux/vmalloc.h> +#include <linux/bitops.h> #include <asm/msr.h> #include <asm/user.h> #include <asm/fpu/internal.h>
@@ -341,3 +343,34 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext)return 0 ; } + +int cet_setup_ibt (void ) +{ +u64 msr_val; + +if (!static_cpu_has (X86_FEATURE_IBT )) +return -EOPNOTSUPP ; + +start_update_msrs (); +rdmsrl (MSR_IA32_U_CET , msr_val); + msr_val |= (CET_ENDBR_EN |CET_NO_TRACK_EN ); +wrmsrl (MSR_IA32_U_CET , msr_val); +end_update_msrs (); + current->thread.cet.ibt_enabled =1 ; +return 0 ; +} + +void cet_disable_ibt (void ) +{ +u64 msr_val; + +if (!static_cpu_has (X86_FEATURE_IBT )) +return ; + +start_update_msrs (); +rdmsrl (MSR_IA32_U_CET , msr_val); + msr_val &=CET_SHSTK_EN ; +wrmsrl (MSR_IA32_U_CET , msr_val); +end_update_msrs (); + current->thread.cet.ibt_enabled =0 ; +}
arch/x86/kernel/cpu/common.c +17 -0
@@ -536,6 +536,23 @@ static __init int setup_disable_shstk(char *s)__setup ("no_user_shstk" , setup_disable_shstk);#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +static __init int setup_disable_ibt (char *s) +{ +/* require an exact match without trailing characters */ +if (s[0 ] !='\0' ) +return 0 ; + +if (!boot_cpu_has (X86_FEATURE_IBT )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_IBT ); +pr_info ("x86: 'no_user_ibt' specified, disabling user Branch Tracking\n" ); +return 1 ; +} +__setup ("no_user_ibt" , setup_disable_ibt); +#endif +/* * Some CPU features depend on higher CPUID levels, which may not always * be available due to CPUID level capping or broken virtualization
tools/arch/x86/include/asm/disabled-features.h +7 -1
@@ -62,6 +62,12 @@#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31))#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -83,7 +89,7 @@#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)#endif /* _ASM_X86_DISABLED_FEATURES_H */
Signature · 1 line
2.21.0
PATCH v12 3/8 x86/cet/ibt: Handle signals for Indirect Branch Tracking
In reply to Yu-cheng YuAn indirect CALL/JMP moves the indirect branch tracking (IBT) state machine to WAIT_ENDBR status until the instruction reaches an ENDBR opcode. If the CALL/JMP does not reach an ENDBR opcode, the processor raises a control- protection fault. WAIT_ENDBR status can be read from MSR_IA32_U_CET.
WAIT_ENDBR is cleared for signal handling, and restored for sigreturn.
IBT state machine is described in Intel SDM Vol. 1, Sec. 18.3.
---
v9:
- Fix missing WAIT_ENDBR in signal handling.
arch/x86/kernel/cet.c | 27 +++++++++++++++++++++++++-- arch/x86/kernel/fpu/signal.c | 8 +++++--- 2 files changed, 30 insertions(+), 5 deletions(-)
arch/x86/kernel/cet.c +25 -2
@@ -295,6 +295,13 @@ void cet_restore_signal(struct sc_ext *sc_ext) msr_val |=CET_SHSTK_EN ; } +if (cet->ibt_enabled) { + msr_val |= (CET_ENDBR_EN |CET_NO_TRACK_EN ); + +if (sc_ext->wait_endbr) + msr_val |=CET_WAIT_ENDBR ; + } +if (test_thread_flag (TIF_NEED_FPU_LOAD )) cet_user_state->user_cet = msr_val;else
@@ -335,9 +342,25 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext) sc_ext->ssp = new_ssp; } -if (ssp) { +if (ssp || cet->ibt_enabled) { +start_update_msrs (); -wrmsrl (MSR_IA32_PL3_SSP , ssp); + +if (ssp) +wrmsrl (MSR_IA32_PL3_SSP , ssp); + +if (cet->ibt_enabled) { +u64 r; + +rdmsrl (MSR_IA32_U_CET , r); + +if (r &CET_WAIT_ENDBR ) { + sc_ext->wait_endbr =1 ; + r &= ~CET_WAIT_ENDBR ; +wrmsrl (MSR_IA32_U_CET , r); + } + } +end_update_msrs (); }
arch/x86/kernel/fpu/signal.c +5 -3
@@ -57,7 +57,8 @@ int save_cet_to_sigframe(int ia32, void __user *fp, unsigned long restorer) {int err =0 ; -if (!current->thread.cet.shstk_size) +if (!current->thread.cet.shstk_size && + !current->thread.cet.ibt_enabled)return 0 ;if (fp) {
@@ -89,7 +90,8 @@ static int get_cet_from_sigframe(int ia32, void __user *fp, struct sc_ext *ext)memset (ext,0 ,sizeof (*ext)); -if (!current->thread.cet.shstk_size) +if (!current->thread.cet.shstk_size && + !current->thread.cet.ibt_enabled)return 0 ;if (fp) {
@@ -577,7 +579,7 @@ static unsigned long fpu__alloc_sigcontext_ext(unsigned long sp)* sigcontext_ext is at: fpu + fpu_user_xstate_size + * FP_XSTATE_MAGIC2_SIZE, then aligned to 8. */ -if (cet->shstk_size) +if (cet->shstk_size || cet->ibt_enabled) sp -= (sizeof (struct sc_ext ) +8 );return sp;
Signature · 1 line
2.21.0
PATCH v12 4/8 x86/cet/ibt: ELF header parsing for Indirect Branch Tracking
In reply to Yu-cheng YuUpdate arch_setup_elf_property() for Indirect Branch Tracking.
---
v9:
- Change cpu_feature_enabled() to static_cpu_has().
arch/x86/Kconfig | 2 ++ arch/x86/kernel/process_64.c | 8 ++++++++ 2 files changed, 10 insertions(+)
arch/x86/Kconfig +2 -0
@@ -1969,6 +1969,8 @@ config X86_INTEL_BRANCH_TRACKING_USER depends on CPU_SUP_INTEL && X86_64 depends on $(cc-option,-fcf-protection) select X86_INTEL_CET + select ARCH_USE_GNU_PROPERTY + select ARCH_BINFMT_ELF_STATE help Indirect Branch Tracking (IBT) provides protection against CALL-/JMP-oriented programming attacks. It is active when
arch/x86/kernel/process_64.c +8 -0
@@ -866,6 +866,14 @@ int arch_setup_elf_property(struct arch_elf_state *state) r = cet_setup_shstk(); } +if (r <0 ) +return r; + +if (static_cpu_has (X86_FEATURE_IBT )) { +if (state->gnu_property &GNU_PROPERTY_X86_FEATURE_1_IBT ) + r =cet_setup_ibt (); + } +return r; }#endif
Signature · 1 line
2.21.0
PATCH v12 5/8 x86/cet/ibt: Update arch_prctl functions for Indirect Branch Tracking
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
Update ARCH_X86_CET_STATUS and ARCH_X86_CET_DISABLE for Indirect Branch Tracking.
--- arch/x86/kernel/cet_prctl.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cet_prctl.c +7 -1
@@ -22,6 +22,9 @@ static int copy_status_to_user(struct cet_status *cet, u64 arg2) buf[2 ] = (u64 )cet->shstk_size; } +if (cet->ibt_enabled) + buf[0 ] |=GNU_PROPERTY_X86_FEATURE_1_IBT ; +return copy_to_user ((u64 __user *)arg2, buf,sizeof (buf)); }
@@ -42,7 +45,8 @@ int prctl_cet(int option, u64 arg2)if (option ==ARCH_X86_CET_STATUS )return copy_status_to_user (cet, arg2); -if (!static_cpu_has (X86_FEATURE_SHSTK )) +if (!static_cpu_has (X86_FEATURE_SHSTK ) && + !static_cpu_has (X86_FEATURE_IBT ))return -EOPNOTSUPP ;switch (option) {
@@ -56,6 +60,8 @@ int prctl_cet(int option, u64 arg2)return -EINVAL ;if (features &GNU_PROPERTY_X86_FEATURE_1_SHSTK )cet_disable_shstk (); +if (features &GNU_PROPERTY_X86_FEATURE_1_IBT ) +cet_disable_ibt ();return 0 ;case ARCH_X86_CET_LOCK :
Signature · 1 line
2.21.0
PATCH v12 6/8 x86/vdso/32: Add ENDBR32 to __kernel_vsyscall entry point
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
Add ENDBR32 to __kernel_vsyscall entry point.
--- arch/x86/entry/vdso/vdso32/system_call.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/entry/vdso/vdso32/system_call.S +3 -0
@@ -14,6 +14,9 @@ ALIGN __kernel_vsyscall: CFI_STARTPROC +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + endbr32 +#endif /* * Reshuffle regs so that all of any of the entry instructions * will preserve enough state.
Signature · 1 line
2.21.0
PATCH v12 7/8 x86/vdso: Insert endbr32/endbr64 to vDSO
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
When Indirect Branch Tracking (IBT) is enabled, vDSO functions may be called indirectly, and must have ENDBR32 or ENDBR64 as the first instruction. The compiler must support -fcf-protection=branch so that it can be used to compile vDSO.
---
v12:
- Replace object file list with $(vobjs) $(vobjs32).
arch/x86/entry/vdso/Makefile | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/entry/vdso/Makefile +4 -0
@@ -94,6 +94,10 @@ endif $(vobjs): KBUILD_CFLAGS := $(filter-out $(GCC_PLUGINS_CFLAGS) $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS)) $(CFL) +ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +$(vobjs) $(vobjs32): KBUILD_CFLAGS += -fcf-protection=branch +endif + # # vDSO code runs in userspace and -pg doesn't help with profiling anyway. #
Signature · 1 line
2.21.0
PATCH v12 8/8 x86: Disallow vsyscall emulation when CET is enabled
In reply to Yu-cheng YuEmulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
---
v12:
- Disable vsyscall emulation only when it is attempted (vs. at compile time).
arch/x86/entry/vsyscall/vsyscall_64.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/entry/vsyscall/vsyscall_64.c +9 -0
@@ -150,6 +150,15 @@ bool emulate_vsyscall(unsigned long error_code,WARN_ON_ONCE (address != regs->ip); +#ifdef CONFIG_X86_INTEL_CET +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +warn_bad_vsyscall (KERN_INFO , regs, +"vsyscall attempted with cet enabled" ); +return false; + } +#endif +if (vsyscall_mode ==NONE ) {warn_bad_vsyscall (KERN_INFO , regs,"vsyscall attempted with vsyscall=none" );
Signature · 1 line
2.21.0
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 9 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 8 lines · Pavel Machek
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Quoted 9 lines
Quoted 3 lines
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
Nothing will be broken. CET enabled applications don't use/need
vsyscall emulation.
Signature · 1 line
H.J.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 9 lines · H.J. Lu
Quoted 8 lines · Pavel Machek
Quoted 7 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
How does this "enhance security"?
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
Nothing will be broken. CET enabled applications don't use/need
vsyscall emulation.
Hi H.J.,
Could you explain your logic a bit more thoroughly, please?
I also suspect that Pavel was confused by your changelog where you said that you do this when "CET is enabled". Does enabled in this context mean:
1. Just CET support compiled in, or
2. Compiled in and on CET hardware, or
3. Compiled in to the kernel enabled in the app and running on CET hardware?
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 17 lines · Dave Hansen
Quoted 15 lines · H.J. Lu
Quoted 14 lines · Pavel Machek
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
Quoted 6 lines
Quoted 2 lines
Nothing will be broken. CET enabled applications don't use/need
vsyscall emulation.Hi H.J.,
Could you explain your logic a bit more thoroughly, please?
Here is my CET slides for LPC 2020:
which may have answers for most questions.
Quoted 6 lines
I also suspect that Pavel was confused by your changelog where you said that you do this when "CET is enabled". Does enabled in this context mean:
1. Just CET support compiled in, or
2. Compiled in and on CET hardware, or
3. Compiled in to the kernel enabled in the app and running on CET hardware?
CET is enabled only in a process if
1. All components are CET enabled, and
2. CPU supports CET, and
3. Kernel supports CET.
Signature · 1 line
H.J.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 5 lines
Here is my CET slides for LPC 2020:
which may have answers for most questions.
Hi H.J.,
I know you're not super familiar with our kernel development process, which might be why you've been repeatedly referring folks to your presentations. Our efforts here on the kernel mailing list are to ensure that we have a nice standalone changelog in the kernel forever. Great as your presentation might be, it isn't a part of the changelog.
I'm not asking these questions because I need them explained to me. I'm asking because I need Yu-cheng to ensure he has the answers in his changelog when the patches are merged.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
In reply to Pavel MachekQuoted 7 lines · Pavel Machek
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Quoted 8 lines
Quoted 3 lines
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
I would revise the wording if there is another version. What this patch does is:
If an application is compiled for CET and the system supports it, then the application cannot do vsyscall emulation. Earlier we allow the emulation, and had a patch that fixes the shadow stack and endbr for the emulation code. Since newer programs mostly do no do the emulation, we changed the patch do block it when attempted.
This patch would not block any legacy applications or any applications on older machines.
Yu-cheng
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 20 lines · Yu-cheng
Quoted 7 lines · Pavel Machek
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Quoted 8 lines
Quoted 3 lines
How does this "enhance security"?
What is the connection between vsyscall emulation and CET?
Boom.
We don't break compatibility by default, and you should not tell people to enable CET by default if you plan to do this.
I would revise the wording if there is another version. What this patch does is:
Quoted 8 lines
If an application is compiled for CET and the system supports it, then the application cannot do vsyscall emulation. Earlier we allow the emulation, and had a patch that fixes the shadow stack and endbr for the emulation code. Since newer programs mostly do no do the emulation, we changed the patch do block it when attempted.
This patch would not block any legacy applications or any applications on older machines.
Aha, makes sense, sorry for the noise.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Andy Lutomirski
maintainerRe: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
In reply to Yu-cheng YuQuoted 19 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Signed-off-by Yu-cheng Yu <[email protected]>---
v12:
- Disable vsyscall emulation only when it is attempted (vs. at compile time).arch/x86/entry/vsyscall/vsyscall_64.c | 9 +++++++++ 1 file changed, 9 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +0
-0
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 159
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +7
-0@@ -150,6 +150,15 @@ bool emulate_vsyscall(unsigned long error_code,WARN_ON_ONCE (address != regs->ip); +#ifdef CONFIG_X86_INTEL_CET +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +warn_bad_vsyscall (KERN_INFO , regs, +"vsyscall attempted with cet enabled" ); +return false; + }
Nope, try again. Having IBT on does *not* mean that every library in the process knows that we have indirect branch tracking. The legacy bitmap exists for a reason. Also, I want a way to flag programs as not using the vsyscall page, but that flag should not be called CET. And a process with vsyscalls off should not be able to read the vsyscall page, and /proc/self/maps should be correct.
So you have some choices:
1. Drop this patch and make it work.
2. Add a real per-process vsyscall control. Either make it depend on vsyscall=xonly and wire it up correctly or actually make it work correctly with vsyscall=emulate.
NAK to any hacks in this space. Do it right or don't do it at all.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 40 lines · Andy Lutomirski
Quoted 19 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Signed-off-by Yu-cheng Yu <[email protected]>---
v12:
- Disable vsyscall emulation only when it is attempted (vs. at compile time).arch/x86/entry/vsyscall/vsyscall_64.c | 9 +++++++++ 1 file changed, 9 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +0
-0arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 159
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +7
-0@@ -150,6 +150,15 @@ bool emulate_vsyscall(unsigned long error_code,WARN_ON_ONCE (address != regs->ip); +#ifdef CONFIG_X86_INTEL_CET +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +warn_bad_vsyscall (KERN_INFO , regs, +"vsyscall attempted with cet enabled" ); +return false; + }Nope, try again. Having IBT on does *not* mean that every library in the process knows that we have indirect branch tracking. The legacy bitmap exists for a reason. Also, I want a way to flag programs as not using the vsyscall page, but that flag should not be called CET. And a process with vsyscalls off should not be able to read the vsyscall page, and
/proc/self/mapsshould be correct.So you have some choices:
Quoted 8 lines
1. Drop this patch and make it work.
2. Add a real per-process vsyscall control. Either make it depend on vsyscall=xonly and wire it up correctly or actually make it work correctly with vsyscall=emulate.
NAK to any hacks in this space. Do it right or don't do it at all.
We can drop this patch, and bring back the previous patch that fixes up shadow stack and ibt. That makes vsyscall emulation work correctly, and does not force the application to do anything different from what is working now. I will post the previous patch as a reply to this thread so that people can make comments on it.
Yu-cheng
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 8 lines · Yu-cheng
Quoted 7 lines · Andy Lutomirski
Quoted 6 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Signed-off-by Yu-cheng Yu <[email protected]>
[...]
Quoted 19 lines
Quoted 10 lines
Nope, try again. Having IBT on does *not* mean that every library in the process knows that we have indirect branch tracking. The legacy bitmap exists for a reason. Also, I want a way to flag programs as not using the vsyscall page, but that flag should not be called CET. And a process with vsyscalls off should not be able to read the vsyscall page, and
/proc/self/mapsshould be correct.So you have some choices:
Quoted 8 lines
1. Drop this patch and make it work.
2. Add a real per-process vsyscall control. Either make it depend on vsyscall=xonly and wire it up correctly or actually make it work correctly with vsyscall=emulate.
NAK to any hacks in this space. Do it right or don't do it at all.
Quoted 7 lines
We can drop this patch, and bring back the previous patch that fixes up shadow stack and ibt. That makes vsyscall emulation work correctly, and does not force the application to do anything different from what is working now. I will post the previous patch as a reply to this thread so that people can make comments on it.
Yu-cheng
Here is the patch:
------
From dfdee39c795ee5dcee2c77f6ba344a61f4d8124b Mon Sep 17 00:00:00 2001
From: Yu-cheng Yu <[email protected]>
Date: Thu, 29 Nov 2018 14:15:38 -0800
Subject: [PATCH 34/43] x86/vsyscall/64: Fixup Shadow Stack and Indirect Branch
Tracking for vsyscall emulation
Vsyscall entry points are effectively branch targets. Mark them with ENDBR64 opcodes. When emulating the RET instruction, unwind the shadow stack and reset IBT state machine.
--- arch/x86/entry/vsyscall/vsyscall_64.c | 29 +++++++++++++++++++++++ arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++ arch/x86/entry/vsyscall/vsyscall_trace.h | 1 + 3 files changed, 39 insertions(+)
(quoted diff) +0 -0
b/arch/x86/entry/vsyscall/vsyscall_64.c
index 44c33103a955..0131c9f7f9c5 100644
arch/x86/entry/vsyscall/vsyscall_64.c +29 -0
@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> #define CREATE_TRACE_POINTS #include "vsyscall_trace.h"
@@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate (); + +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif + +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif +fpregs_unlock (); + }return true; sigsegv:
(quoted diff) +0 -0
b/arch/x86/entry/vsyscall/vsyscall_emu_64.S
index 2e203f3a25a7..040696333457 100644
arch/x86/entry/vsyscall/vsyscall_emu_64.S +9 -0
@@ -17,16 +17,25 @@ __PAGE_ALIGNED_DATA .type __vsyscall_page, @object __vsyscall_page: +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + endbr64 +#endif mov $__NR_gettimeofday, %rax syscall ret .balign 1024, 0xcc +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + endbr64 +#endif mov $__NR_time, %rax syscall ret .balign 1024, 0xcc +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + endbr64 +#endif mov $__NR_getcpu, %rax syscall ret
(quoted diff) +0 -0
b/arch/x86/entry/vsyscall/vsyscall_trace.h
index 3c3f9765a85c..7aa2101ada44 100644
arch/x86/entry/vsyscall/vsyscall_trace.h +1 -0
@@ -25,6 +25,7 @@ TRACE_EVENT(emulate_vsyscall,#endif #undef TRACE_INCLUDE_PATH +#undef TRACE_INCLUDE_FILE#define TRACE_INCLUDE_PATH ../../arch/x86/entry/vsyscall/ #define TRACE_INCLUDE_FILE vsyscall_trace#include <trace/define_trace.h>
Signature · 1 line
2.21.0
Andy Lutomirski
maintainerRe: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 76 lines · Yu-cheng Yu
Quoted 8 lines · Yu-cheng
Quoted 7 lines · Andy Lutomirski
Quoted 6 lines · Yu-cheng Yu
Emulation of the legacy vsyscall page is required by some programs built before 2013. Newer programs after 2013 don't use it. Disable vsyscall emulation when Control-flow Enforcement (CET) is enabled to enhance security.
Signed-off-by Yu-cheng Yu <[email protected]>[...]
Quoted 19 lines
Quoted 10 lines
Nope, try again. Having IBT on does *not* mean that every library in the process knows that we have indirect branch tracking. The legacy bitmap exists for a reason. Also, I want a way to flag programs as not using the vsyscall page, but that flag should not be called CET. And a process with vsyscalls off should not be able to read the vsyscall page, and
/proc/self/mapsshould be correct.So you have some choices:
Quoted 8 lines
1. Drop this patch and make it work.
2. Add a real per-process vsyscall control. Either make it depend on vsyscall=xonly and wire it up correctly or actually make it work correctly with vsyscall=emulate.
NAK to any hacks in this space. Do it right or don't do it at all.
Quoted 7 lines
We can drop this patch, and bring back the previous patch that fixes up shadow stack and ibt. That makes vsyscall emulation work correctly, and does not force the application to do anything different from what is working now. I will post the previous patch as a reply to this thread so that people can make comments on it.
Yu-cheng
Here is the patch:
------
From
dfdee39c795ee5dcee2c77f6ba344a61f4d8124bMon Sep 17 00:00:00 2001
From: Yu-cheng Yu <[email protected]>
Date: Thu, 29 Nov 2018 14:15:38 -0800
Subject: [PATCH 34/43] x86/vsyscall/64: Fixup Shadow Stack and Indirect Branch
Tracking for vsyscall emulationVsyscall entry points are effectively branch targets. Mark them with ENDBR64 opcodes. When emulating the RET instruction, unwind the shadow stack and reset IBT state machine.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/entry/vsyscall/vsyscall_64.c | 29 +++++++++++++++++++++++ arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++ arch/x86/entry/vsyscall/vsyscall_trace.h | 1 + 3 files changed, 39 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> #define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();
Wouldn't this be nicer if you operated on the memory image, not the registers?
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif
Seems reasonable on first glance.
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif
What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 3 lines · Andy Lutomirski
Quoted 2 lines · Yu-cheng Yu
On Mon, 2020-09-21 at 09:22 -0700, Yu, Yu-cheng wrote:
[...]
Quoted 35 lines
Quoted 35 lines
Here is the patch:
------
From
dfdee39c795ee5dcee2c77f6ba344a61f4d8124bMon Sep 17 00:00:00 2001
From: Yu-cheng Yu <[email protected]>
Date: Thu, 29 Nov 2018 14:15:38 -0800
Subject: [PATCH 34/43] x86/vsyscall/64: Fixup Shadow Stack and Indirect Branch
Tracking for vsyscall emulationVsyscall entry points are effectively branch targets. Mark them with ENDBR64 opcodes. When emulating the RET instruction, unwind the shadow stack and reset IBT state machine.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/entry/vsyscall/vsyscall_64.c | 29 +++++++++++++++++++++++ arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++ arch/x86/entry/vsyscall/vsyscall_trace.h | 1 + 3 files changed, 39 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h>
Quoted 16 lines
Quoted 4 lines
arch/x86/entry/vsyscall/vsyscall_64.c +0
-0#define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();Wouldn't this be nicer if you operated on the memory image, not the registers?
Do you mean writing to the XSAVES area?
Quoted 12 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 9 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif Seems reasonable on first glance.
Quoted 14 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
At this point, the MSR's value can still be valid or is already saved to memory. If we are going to write to memory, then the MSR must be saved first. So I chose to do __fpregs_load_activate() and write the MSR.
Maybe we can check the address before writing it to the MSR?
Yu-cheng
Andy Lutomirski
maintainerRe: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 60 lines · Yu-cheng
Quoted 3 lines · Andy Lutomirski
Quoted 2 lines · Yu-cheng Yu
On Mon, 2020-09-21 at 09:22 -0700, Yu, Yu-cheng wrote:
[...]
Quoted 35 lines
Quoted 35 lines
Here is the patch:
------
From
dfdee39c795ee5dcee2c77f6ba344a61f4d8124bMon Sep 17 00:00:00 2001
From: Yu-cheng Yu <[email protected]>
Date: Thu, 29 Nov 2018 14:15:38 -0800
Subject: [PATCH 34/43] x86/vsyscall/64: Fixup Shadow Stack and Indirect Branch
Tracking for vsyscall emulationVsyscall entry points are effectively branch targets. Mark them with ENDBR64 opcodes. When emulating the RET instruction, unwind the shadow stack and reset IBT state machine.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/entry/vsyscall/vsyscall_64.c | 29 +++++++++++++++++++++++ arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++ arch/x86/entry/vsyscall/vsyscall_trace.h | 1 + 3 files changed, 39 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> Quoted 16 lines
Quoted 4 lines
arch/x86/entry/vsyscall/vsyscall_64.c +0
-0#define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();Wouldn't this be nicer if you operated on the memory image, not the registers?
Quoted 1 line
Do you mean writing to the XSAVES area?
Yes.
Quoted 28 lines
Quoted 13 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif Seems reasonable on first glance.
Quoted 14 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
Quoted 5 lines
At this point, the MSR's value can still be valid or is already saved to memory. If we are going to write to memory, then the MSR must be saved first. So I chose to do
__fpregs_load_activate()and write the MSR.Maybe we can check the address before writing it to the MSR?
Performance is almost irrelevant here, and the writing-to-XSAVES-area approach should have the benefit that the exception handling and signaling happens for free.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 97 lines · Andy Lutomirski
Quoted 60 lines · Yu-cheng
Quoted 3 lines · Andy Lutomirski
Quoted 2 lines · Yu-cheng Yu
On Mon, 2020-09-21 at 09:22 -0700, Yu, Yu-cheng wrote:
[...]
Quoted 35 lines
Quoted 35 lines
Here is the patch:
------
From
dfdee39c795ee5dcee2c77f6ba344a61f4d8124bMon Sep 17 00:00:00 2001
From: Yu-cheng Yu <[email protected]>
Date: Thu, 29 Nov 2018 14:15:38 -0800
Subject: [PATCH 34/43] x86/vsyscall/64: Fixup Shadow Stack and Indirect Branch
Tracking for vsyscall emulationVsyscall entry points are effectively branch targets. Mark them with ENDBR64 opcodes. When emulating the RET instruction, unwind the shadow stack and reset IBT state machine.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/entry/vsyscall/vsyscall_64.c | 29 +++++++++++++++++++++++ arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++ arch/x86/entry/vsyscall/vsyscall_trace.h | 1 + 3 files changed, 39 insertions(+)arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> Quoted 16 lines
Quoted 4 lines
arch/x86/entry/vsyscall/vsyscall_64.c +0
-0#define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();Wouldn't this be nicer if you operated on the memory image, not the registers?
Quoted 1 line
Do you mean writing to the XSAVES area?
Yes.
Quoted 32 lines
Quoted 13 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif Seems reasonable on first glance.
Quoted 14 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
At this point, the MSR's value can still be valid or is already saved to memory. If we are going to write to memory, then the MSR must be saved first. So I chose to do
__fpregs_load_activate()and write the MSR.
Quoted 6 lines
Quoted 1 line
Maybe we can check the address before writing it to the MSR?
Performance is almost irrelevant here, and the writing-to-XSAVES-area approach should have the benefit that the exception handling and signaling happens for free.
Ok, I will change it.
Thanks,
Yu-cheng
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
In reply to Andy LutomirskiQuoted 42 lines · Andy Lutomirski
Quoted 17 lines · Yu-cheng Yu
arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> #define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();Wouldn't this be nicer if you operated on the memory image, not the registers?
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif Seems reasonable on first glance.
Quoted 13 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
#GP, the SSP MSRs do canonical checks.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 57 lines · Sean Christopherson
Quoted 42 lines · Andy Lutomirski
Quoted 17 lines · Yu-cheng Yu
arch/x86/entry/vsyscall/vsyscall_64.c +3
-0@@ -38,6 +38,9 @@#include <asm/fixmap.h> #include <asm/traps.h> #include <asm/paravirt.h> +#include <asm/fpu/xstate.h> +#include <asm/fpu/types.h> +#include <asm/fpu/internal.h> #define CREATE_TRACE_POINTS #include "vsyscall_trace.h" @@ -286,6 +289,32 @@ bool emulate_vsyscall(unsigned long error_code,/* Emulate a ret instruction. */ arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 299
Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0regs->ip = caller; regs->sp +=8 ; + +if (current->thread.cet.shstk_size || + current->thread.cet.ibt_enabled) { +u64 r; + +fpregs_lock (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +__fpregs_load_activate ();Wouldn't this be nicer if you operated on the memory image, not the registers?
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 307
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +/* Fixup branch tracking */ +if (current->thread.cet.ibt_enabled) { +rdmsrl (MSR_IA32_U_CET , r); +wrmsrl (MSR_IA32_U_CET , r & ~CET_WAIT_ENDBR ); + } +#endif Seems reasonable on first glance.
Quoted 13 lines
arch/x86/entry/vsyscall/vsyscall_64.c bool emulate_vsyscall(unsigned long error_code, · line 315
Quoted 8 lines
arch/x86/entry/vsyscall/vsyscall_64.c +8
-0+ +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +/* Unwind shadow stack. */ +if (current->thread.cet.shstk_size) { +rdmsrl (MSR_IA32_PL3_SSP , r); +wrmsrl (MSR_IA32_PL3_SSP , r +8 ); + } +#endif What happens if the result is noncanonical? A quick skim of the SDM didn't find anything. This latter issue goes away if you operate on the memory image, though -- writing a bogus value is just fine, since the FP restore will handle it.
Quoted 1 line
#GP, the SSP MSRs do canonical checks.
I think I'll add a check here for (r + 8) >= TASK_SIZE_MAX. It is better than getting a fault.
Thanks,
Yu-cheng
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 2 lines · Yu-cheng
I think I'll add a check here for (r + 8) >=
TASK_SIZE_MAX. It is better than getting a fault.
There's also wrmsr_safe().
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Yu-cheng
I think I'll add a check here for (r + 8) >=
TASK_SIZE_MAX. It is better than getting a fault.There's also
wrmsr_safe().
Yes, thanks.
Since I am going to change this to:
fpu__prepare_write(), then write to the XSAVES area.
The kernel does not expect XRSTORS to fail ("Bad FPU state detected..." message). So maybe still check the address first.
Andy Lutomirski
maintainerRe: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 8 lines · Yu-cheng
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Yu-cheng
I think I'll add a check here for (r + 8) >=
TASK_SIZE_MAX. It is better than getting a fault.There's also
wrmsr_safe().
Quoted 8 lines
Yes, thanks.
Since I am going to change this to:
fpu__prepare_write(), then write to the XSAVES area.The kernel does not expect XRSTORS to fail ("Bad FPU state detected..." message). So maybe still check the address first.
Surely there are plenty of ways to use ptrace() to poke garbage into the FPU state. We should be able to handle this type of failure somewhat gracefully.
Re: [PATCH v12 8/8] x86: Disallow vsyscall emulation when CET is enabled
Quoted 11 lines · Andy Lutomirski
Quoted 10 lines · Yu-cheng
Quoted 5 lines · Dave Hansen
Quoted 2 lines · Yu-cheng
I think I'll add a check here for (r + 8) >=
TASK_SIZE_MAX. It is better than getting a fault.There's also
wrmsr_safe().Yes, thanks.
Since I am going to change this to:
Quoted 7 lines
Quoted 4 lines
fpu__prepare_write(), then write to the XSAVES area.The kernel does not expect XRSTORS to fail ("Bad FPU state detected..." message). So maybe still check the address first.
Surely there are plenty of ways to use
ptrace()to poke garbage into the FPU state. We should be able to handle this type of failure somewhat gracefully.
Yeah, agreed. I'd much rather make XRSTORS able to #GP gracefully than teach the kernel exhaustively about every possible error condition it can encounter.
We *might* want to do something like to preserve the warning if the task hasn't been ptrace'd, or had the memory buffer written to directly or tainted in another way.