4 maintainers = [ lib.maintainers.joachifm ];
8 (lib.mkRenamedOptionModule [ "security" "virtualization" "flushL1DataCache" ] [ "security" "virtualisation" "flushL1DataCache" ])
12 security.allowUserNamespaces = lib.mkOption {
13 type = lib.types.bool;
16 Whether to allow creation of user namespaces.
18 The motivation for disabling user namespaces is the potential
19 presence of code paths where the kernel's permission checking
20 logic fails to account for namespacing, instead permitting a
21 namespaced process to act outside the namespace with the same
22 privileges as it would have inside it. This is particularly
23 damaging in the common case of running as root within the namespace.
25 When user namespace creation is disallowed, attempting to create a
26 user namespace fails with "no space left on device" (ENOSPC).
27 root may re-enable user namespace creation at runtime.
31 security.unprivilegedUsernsClone = lib.mkOption {
32 type = lib.types.bool;
35 When disabled, unprivileged users will not be able to create new namespaces.
36 By default unprivileged user namespaces are disabled.
37 This option only works in a hardened profile.
41 security.protectKernelImage = lib.mkOption {
42 type = lib.types.bool;
45 Whether to prevent replacing the running kernel image.
49 security.allowSimultaneousMultithreading = lib.mkOption {
50 type = lib.types.bool;
53 Whether to allow SMT/hyperthreading. Disabling SMT means that only
54 physical CPU cores will be usable at runtime, potentially at
55 significant performance cost.
57 The primary motivation for disabling SMT is to mitigate the risk of
58 leaking data between threads running on the same CPU core (due to
59 e.g., shared caches). This attack vector is unproven.
61 Disabling SMT is a supplement to the L1 data cache flushing mitigation
62 (see [](#opt-security.virtualisation.flushL1DataCache))
63 versus malicious VM guests (SMT could "bring back" previously flushed
68 security.forcePageTableIsolation = lib.mkOption {
69 type = lib.types.bool;
72 Whether to force-enable the Page Table Isolation (PTI) Linux kernel
73 feature even on CPU models that claim to be safe from Meltdown.
75 This hardening feature is most beneficial to systems that run untrusted
76 workloads that rely on address space isolation for security.
80 security.virtualisation.flushL1DataCache = lib.mkOption {
81 type = lib.types.nullOr (lib.types.enum [ "never" "cond" "always" ]);
84 Whether the hypervisor should flush the L1 data cache before
86 See also [](#opt-security.allowSimultaneousMultithreading).
88 - `null`: uses the kernel default
89 - `"never"`: disables L1 data cache flushing entirely.
90 May be appropriate if all guests are trusted.
91 - `"cond"`: flushes L1 data cache only for pre-determined
92 code paths. May leak information about the host address space
94 - `"always"`: flushes L1 data cache every time the hypervisor
95 enters the guest. May incur significant performance cost.
100 config = lib.mkMerge [
101 (lib.mkIf (!config.security.allowUserNamespaces) {
102 # Setting the number of allowed user namespaces to 0 effectively disables
103 # the feature at runtime. Note that root may raise the limit again
105 boot.kernel.sysctl."user.max_user_namespaces" = 0;
108 { assertion = config.nix.settings.sandbox -> config.security.allowUserNamespaces;
109 message = "`nix.settings.sandbox = true` conflicts with `!security.allowUserNamespaces`.";
114 (lib.mkIf config.security.unprivilegedUsernsClone {
115 boot.kernel.sysctl."kernel.unprivileged_userns_clone" = lib.mkDefault true;
118 (lib.mkIf config.security.protectKernelImage {
119 # Disable hibernation (allows replacing the running kernel)
120 boot.kernelParams = [ "nohibernate" ];
121 # Prevent replacing the running kernel image w/o reboot
122 boot.kernel.sysctl."kernel.kexec_load_disabled" = lib.mkDefault true;
125 (lib.mkIf (!config.security.allowSimultaneousMultithreading) {
126 boot.kernelParams = [ "nosmt" ];
129 (lib.mkIf config.security.forcePageTableIsolation {
130 boot.kernelParams = [ "pti=on" ];
133 (lib.mkIf (config.security.virtualisation.flushL1DataCache != null) {
134 boot.kernelParams = [ "kvm-intel.vmentry_l1d_flush=${config.security.virtualisation.flushL1DataCache}" ];