RFC Dynamic mitigations
Get series with b4
b4 am [email protected]Versions newest first
- v1 RFC Latest56 patches 115 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/56 | Documentation/admin-guide: Add documentationDocumentation/ Documentation/ | +76 | 5 replies | |
| 2/56 | x86/Kconfig: Add CONFIG_DYNAMIC_MITIGATIONSarch/ | +12 | 2 replies | |
| 3/56 | cpu: Reset global mitigationsinclude/ kernel/ | +21 | 5 replies | |
| 4/56 | x86/bugs: Reset spectre_v1 mitigationsarch/ arch/ | +19 | 5 replies | |
| 5/56 | x86/bugs: Reset spectre_v2 mitigationsarch/ | +19 | 13 replies | |
| 6/56 | x86/bugs: Reset retbleed mitigationsarch/ | +15 | 0 replies | |
| 7/56 | x86/bugs: Reset spectre_v2_user mitigationsarch/ | +13 | 12 replies | |
| 8/56 | x86/bugs: Reset SSB mitigationsarch/ | +24 | 3 replies | |
| 9/56 | x86/bugs: Reset L1TF mitigationsarch/ | +10 | 0 replies | |
| 10/56 | x86/bugs: Reset MDS mitigationsarch/ | +11 | 0 replies | |
| 11/56 | x86/bugs: Reset MMIO mitigationsarch/ | +11 | 2 replies | |
| 12/56 | x86/bugs: Reset SRBDS mitigationsarch/ | +12 | 0 replies | |
| 13/56 | x86/bugs: Reset SRSO mitigationsarch/ | +16 | 0 replies | |
| 14/56 | x86/bugs: Reset GDS mitigationsarch/ | +13 | 2 replies | |
| 15/56 | x86/bugs: Reset BHI mitigationsarch/ | +12 | 2 replies | |
| 16/56 | x86/bugs: Reset ITS mitigationarch/ | +12 | 0 replies | |
| 17/56 | x86/bugs: Reset TSA mitigationsarch/ | +12 | 0 replies | |
| 18/56 | x86/bugs: Reset VMSCAPE mitigationsarch/ | +10 | 0 replies | |
| 19/56 | x86/bugs: Define bugs_smt_disable()arch/ | +14 | 0 replies | |
| 20/56 | x86/bugs: Move bugs.c logic out of .init sectionarch/ | +156 | 3 replies | |
| 21/56 | x86/callthunks: Move logic out of .initarch/ | +6 | 0 replies | |
| 22/56 | cpu: Move mitigation logic out of .initkernel/ | +6 | 0 replies | |
| 23/56 | x86/vmlinux.lds: Move alternative sectionsarch/ | +49 | 0 replies | |
| 24/56 | x86/vmlinux.lds: Move altinstr_aux conditionallyarch/ | +6 | 0 replies | |
| 25/56 | x86/vmlinux.lds: Define __init_alt_endarch/ arch/ | +6 | 0 replies | |
| 26/56 | module: Save module ELF infokernel/ | +1 | 0 replies | |
| 27/56 | x86/mm: Conditionally free alternative sectionsarch/ arch/ | +12 | 0 replies | |
| 28/56 | stop_machine: Add stop_machine_nmi()include/ kernel/ | +107 | 2 replies | |
| 29/56 | x86/apic: Add self-NMI supportarch/ | +7 | 0 replies | |
| 30/56 | x86/nmi: Add support for stop_machine_nmi()arch/ | +4 | 0 replies | |
| 31/56 | x86/alternative: Prepend nops with retpolinesarch/ | +24 | 11 replies | |
| 32/56 | x86/alternative: Add module paramarch/ arch/ arch/ +1 more | +20 | 0 replies | |
| 33/56 | x86/alternative: Avoid re-patching init codearch/ | +33 | 0 replies | |
| 34/56 | x86/alternative: Save old bytes for alternativesarch/ arch/ arch/ | +44 | 10 replies | |
| 35/56 | x86/alternative: Save old bytes for retpolinesarch/ arch/ arch/ | +43 | 0 replies | |
| 36/56 | x86/alternative: Do not recompute len on re-patcharch/ | +6 | 0 replies | |
| 37/56 | x86/alternative: Reset alternativesarch/ arch/ | +83 | 0 replies | |
| 38/56 | x86/callthunks: Reset callthunksarch/ arch/ arch/ | +81 | 0 replies | |
| 39/56 | x86/sync_core: Add sync_core_nmi_safe()arch/ | +14 | 0 replies | |
| 40/56 | x86/alternative: Use sync_core_nmi_safe()arch/ | +5 | 16 replies | |
| 41/56 | static_call: Add update_all_static_calls()arch/ include/ kernel/ | +26 | 0 replies | |
| 42/56 | module: Make memory writeable for re-patchinginclude/ kernel/ | +39 | 0 replies | |
| 43/56 | module: Update alternativesinclude/ kernel/ | +48 | 0 replies | |
| 44/56 | x86/module: Update alternativesarch/ arch/ | +62 | 0 replies | |
| 45/56 | x86/alternative: Use boot_cpu_has in ITS codearch/ | +7 | 0 replies | |
| 46/56 | x86/alternative: Add ITS re-patching supportarch/ arch/ | +114 | 0 replies | |
| 47/56 | x86/module: Add ITS re-patch support for modulesarch/ | +32 | 0 replies | |
| 48/56 | x86/bugs: Move code for updating speculation MSRsarch/ arch/ arch/ +1 more | +13 | 0 replies | |
| 49/56 | x86/fpu: Qualify warning in os_xsavearch/ | +1 | 0 replies | |
| 50/56 | x86/alternative: Add re-patch supportarch/ arch/ | +137 | 2 replies | |
| 51/56 | cpu: Parse string of mitigation optionsinclude/ kernel/ | +36 | 0 replies | |
| 52/56 | x86/bugs: Support parsing mitigation optionsarch/ arch/ | +58 | 2 replies | |
| 53/56 | drivers/cpu: Re-patch mitigations through sysfsDocumentation/ drivers/ include/ | +124 | 2 replies | |
| 54/56 | x86/debug: Create debugfs interface to x86_capabilitiesarch/ | +59 | 0 replies | |
| 55/56 | x86/debug: Show return thunk in debugfsarch/ | +44 | 2 replies | |
| 56/56 | x86/debug: Show static branch config in debugfsarch/ | +32 | 0 replies |
Reviews trailers per patch
2 of 56 patches have a Reviewed-by or Acked-by.
| Patch | Nikolay Borisov | Josh Poimboeuf | Pawan Gupta |
|---|---|---|---|
| 1/56 | |||
| 2/56 | |||
| 3/56 | |||
| 4/56 | |||
| 5/56 | A-b | R-b | |
| 6/56 | |||
| 7/56 | |||
| 8/56 | |||
| 9/56 | |||
| 10/56 | |||
| 11/56 | |||
| 12/56 | |||
| 13/56 | |||
| 14/56 | |||
| 15/56 | |||
| 16/56 | |||
| 17/56 | |||
| 18/56 | |||
| 19/56 | |||
| 20/56 | |||
| 21/56 | |||
| 22/56 | |||
| 23/56 | |||
| 24/56 | |||
| 25/56 | |||
| 26/56 | |||
| 27/56 | |||
| 28/56 | |||
| 29/56 | |||
| 30/56 | |||
| 31/56 | |||
| 32/56 | |||
| 33/56 | |||
| 34/56 | |||
| 35/56 | |||
| 36/56 | |||
| 37/56 | |||
| 38/56 | |||
| 39/56 | |||
| 40/56 | |||
| 41/56 | |||
| 42/56 | |||
| 43/56 | |||
| 44/56 | |||
| 45/56 | |||
| 46/56 | |||
| 47/56 | |||
| 48/56 | |||
| 49/56 | |||
| 50/56 | |||
| 51/56 | |||
| 52/56 | |||
| 53/56 | |||
| 54/56 | |||
| 55/56 | R-b | ||
| 56/56 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.
Cover letter [RFC PATCH 00/56] Dynamic mitigations
Dynamic mitigations enables changing the kernel CPU security mitigations at runtime without a reboot/kexec.
Previously, mitigation choices had to be made on the kernel cmdline. With this feature an administrator can select new mitigation choices by writing a sysfs file, after which the kernel will re-patch itself based on the new mitigations.
As the performance cost of CPU mitigations can be significant, selecting the right set of mitigations is important to achieve the correct balance of performance/security.
Use
---
As described in the supplied documentation file, new mitigations are selected by writing cmdline options to a new sysfs file. Only cmdline options related to mitigations are recognized via this interface. All previous mitigation-related cmdline options are ignored and selections are done based on the new options.
Examples:
echo "mitigations=off" > /sys/devices/system/cpu/mitigations echo "spectre_v2=retpoline tsa=off" > /sys/devices/system/cpu/mitigations
There are several use cases that will benefit from dynamic mitigations:
Use Cases
---------
1. Runtime Policy
Some workflows rely on booting a generic kernel before customizing the system. cloud-init is a popular example of this where a VM is started typically with default settings and then is customized based on a customer-provided configuration file.
As flows like this rely on configuring the system after boot, they currently cannot customize the mitigation policy. With dynamic mitigations, this configuration information can be augmented to include security policy information.
For example, a cloud VM which runs only trusted workloads likely does not need any CPU security mitigations applied. But as this policy information is not known at boot time, the kernel will be booted with unnecessary mitigations enabled. With dynamic mitigations, these mitigations can be disabled during boot after policy information is retrieved, improving performance.
2. Mitigation Changes
Sometimes there are needs to change the mitigation settings in light of new security findings. For example, AMD-SB-1036 advised of a security issue with a spectre v2 mitigation and advised using a different one instead.
With dynamic mitigations, such changes can be made without a reboot/kexec which minimizes disruption in environments which cannot easily tolerate such an event.
3. Mitigation Testing
Being able to quickly change between different mitigation settings without having to restart applications is beneficial when conducting mitigation development and testing.
Note that some bugs have multiple mitigation options, which may have varying performance impacts. Being able to quickly switch between them makes evaluating such options easier.
Implementation Details
----------------------
Re-patching the kernel is expected to be a very rare operation and is done under very big hammers. All tasks are put into the freezer and the re-patching is then done under the (new) stop_machine_nmi() routine.
To re-patch the kernel, it is first reverted back to its compile-time state. The original bytes from alternatives, retpolines, etc. are saved during boot so they can later be used to restore the original kernel image. After that, the kernel is patched based on the new feature flags.
This simplifies the re-patch process as restoring the original kernel image is relatively straightforward. In other words, instead of having to re-patch from mitigation A to mitigation B directly, we first restore the original image and then patch from that to mitigation B, similar to if the system had booted with mitigation B selected originally.
Performance
-----------
Testing so far has demonstrated that re-patching takes ~50ms on an AMD EPYC 7713 running a typical Ubuntu kernel with around 100 modules loaded.
Guide to Patch Series
---------------------
As this series is rather lengthy, this may help with understanding it:
Patches 3-18 focus on "resetting" mitigations. Every bug that may set feature flags, MSRs, static branches, etc. now has matching "reset" functions that will undo all these changes. This is used at the beginning of the re-patch flow.
Patches 20-22 move various functions and values out of the .init section. Most of the existing mitigation logic was marked as __init and the mitigation settings as __ro_after_init but now these can be changed at runtime. The __ro_after_init marking functioned as a defense-in-depth measure but is arguably of limited meaningful security value as an attacker who can modify kernel data can do a lot worse than change some speculation settings. As re-patching requires being able to modify these settings, it was simplest to remove them from that section.
Patches 23-27 involve linker and related modifications to keep alternative information around at runtime instead of free'ing it after boot. This does result in slightly higher runtime memory consumption which is one reason why this feature is behind a Kconfig option. On a typical kernel, this was measured at around 2MB of extra kernel memory usage.
Patches 28-30 focus on the new stop_machine_nmi() which behaves like stop_machine() but runs the handler in NMI context, thus ensuring that even NMIs cannot interrupt the handler. As dynamic mitigations involves re-patching functions used by NMI entry code, this is required for safety.
Patches 31-40 focus on support for restoring the kernel text at runtime. This involves saving the original kernel bytes when patched the first time and adding support to then restore those later.
Patches 41-44 start building support for updating code, in particular module code at runtime.
Patches 45-47 focus on support for the Indirect Target Selection mitigation which is particularly challenging because it requires runtime memory allocations and permission changes which are not possible in NMI context. As a result, ITS memory is pre-allocated before entering NMI context.
Patch 50 adds the complete function for resetting and re-patching the kernel.
Patches 51-53 build the sysfs interface for re-patching and support for parsing the new options provided.
Patches 54-56 add debugfs interfaces to values which are important for mitigations. These are useful for userspace test utilities to be able to force a CPU to appear to be vulnerable or immune to certain bugs as well as being able to help verify if the kernel is correctly mitigating various vulnerabilities.
David Kaplan (56):
Documentation/admin-guide: Add documentation x86/Kconfig: Add CONFIG_DYNAMIC_MITIGATIONS
cpu: Reset global mitigations
x86/bugs: Reset spectre_v1 mitigations x86/bugs: Reset spectre_v2 mitigations x86/bugs: Reset retbleed mitigations
x86/bugs: Reset spectre_v2_user mitigations x86/bugs: Reset SSB mitigations
x86/bugs: Reset L1TF mitigations
x86/bugs: Reset MDS mitigations
x86/bugs: Reset MMIO mitigations
x86/bugs: Reset SRBDS mitigations
x86/bugs: Reset SRSO mitigations
x86/bugs: Reset GDS mitigations
x86/bugs: Reset BHI mitigations
x86/bugs: Reset ITS mitigation
x86/bugs: Reset TSA mitigations
x86/bugs: Reset VMSCAPE mitigations
x86/bugs: Define bugs_smt_disable()
x86/bugs: Move bugs.c logic out of .init section x86/callthunks: Move logic out of .init
cpu: Move mitigation logic out of .init x86/vmlinux.lds: Move alternative sections x86/vmlinux.lds: Move altinstr_aux conditionally x86/vmlinux.lds: Define __init_alt_end
module: Save module ELF info
x86/mm: Conditionally free alternative sections
stop_machine: Add stop_machine_nmi()
x86/apic: Add self-NMI support
x86/nmi: Add support for stop_machine_nmi() x86/alternative: Prepend nops with retpolines x86/alternative: Add module param x86/alternative: Avoid re-patching init code x86/alternative: Save old bytes for alternatives x86/alternative: Save old bytes for retpolines x86/alternative: Do not recompute len on re-patch x86/alternative: Reset alternatives x86/callthunks: Reset callthunks x86/sync_core: Add sync_core_nmi_safe() x86/alternative: Use sync_core_nmi_safe()
static_call: Add update_all_static_calls()
module: Make memory writeable for re-patching
module: Update alternatives
x86/module: Update alternatives x86/alternative: Use boot_cpu_has in ITS code x86/alternative: Add ITS re-patching support x86/module: Add ITS re-patch support for modules x86/bugs: Move code for updating speculation MSRs x86/fpu: Qualify warning in os_xsave x86/alternative: Add re-patch support
cpu: Parse string of mitigation options x86/bugs: Support parsing mitigation options drivers/cpu: Re-patch mitigations through sysfs x86/debug: Create debugfs interface to x86_capabilities x86/debug: Show return thunk in debugfs x86/debug: Show static branch config in debugfs
.../ABI/testing/sysfs-devices-system-cpu | 8 + .../hw-vuln/dynamic_mitigations.rst | 75 ++ Documentation/admin-guide/hw-vuln/index.rst | 1 + arch/x86/Kconfig | 12 + arch/x86/entry/vdso/vma.c | 2 +- arch/x86/include/asm/alternative.h | 51 +- arch/x86/include/asm/bugs.h | 4 + arch/x86/include/asm/module.h | 10 + arch/x86/include/asm/sync_core.h | 14 + arch/x86/kernel/alternative.c | 497 ++++++++++++- arch/x86/kernel/apic/ipi.c | 7 + arch/x86/kernel/callthunks.c | 85 ++- arch/x86/kernel/cpu/bugs.c | 686 +++++++++++++----- arch/x86/kernel/cpu/common.c | 65 +- arch/x86/kernel/cpu/cpu.h | 4 - arch/x86/kernel/fpu/xstate.h | 2 +- arch/x86/kernel/module.c | 96 ++- arch/x86/kernel/nmi.c | 4 + arch/x86/kernel/static_call.c | 3 +- arch/x86/kernel/vmlinux.lds.S | 110 +-- arch/x86/mm/init.c | 12 +- arch/x86/mm/mm_internal.h | 2 + arch/x86/tools/relocs.c | 1 + drivers/base/cpu.c | 113 +++ include/linux/cpu.h | 10 + include/linux/module.h | 11 + include/linux/static_call.h | 2 + include/linux/stop_machine.h | 32 + kernel/cpu.c | 62 +- kernel/module/main.c | 78 +- kernel/static_call_inline.c | 22 + kernel/stop_machine.c | 79 +- 32 files changed, 1876 insertions(+), 284 deletions(-) create mode 100644 Documentation/admin-guide/hw-vuln/dynamic_mitigations.rst
base-commit: a5652f0f2a69fadcfb2f687a11a737a57f15b28e
Signature · 1 line
2.34.1