1{ config, lib, ... }:
2{
3 meta = {
4 maintainers = [ lib.maintainers.joachifm ];
5 };
6
7 imports = [
8 (lib.mkRenamedOptionModule
9 [ "security" "virtualization" "flushL1DataCache" ]
10 [ "security" "virtualisation" "flushL1DataCache" ]
11 )
12 ];
13
14 options = {
15 security.allowUserNamespaces = lib.mkOption {
16 type = lib.types.bool;
17 default = true;
18 description = ''
19 Whether to allow creation of user namespaces.
20
21 The motivation for disabling user namespaces is the potential
22 presence of code paths where the kernel's permission checking
23 logic fails to account for namespacing, instead permitting a
24 namespaced process to act outside the namespace with the same
25 privileges as it would have inside it. This is particularly
26 damaging in the common case of running as root within the namespace.
27
28 When user namespace creation is disallowed, attempting to create a
29 user namespace fails with "no space left on device" (ENOSPC).
30 root may re-enable user namespace creation at runtime.
31 '';
32 };
33
34 security.unprivilegedUsernsClone = lib.mkOption {
35 type = lib.types.bool;
36 default = false;
37 description = ''
38 When disabled, unprivileged users will not be able to create new namespaces.
39 By default unprivileged user namespaces are disabled.
40 This option only works in a hardened profile.
41 '';
42 };
43
44 security.protectKernelImage = lib.mkOption {
45 type = lib.types.bool;
46 default = false;
47 description = ''
48 Whether to prevent replacing the running kernel image.
49 '';
50 };
51
52 security.allowSimultaneousMultithreading = lib.mkOption {
53 type = lib.types.bool;
54 default = true;
55 description = ''
56 Whether to allow SMT/hyperthreading. Disabling SMT means that only
57 physical CPU cores will be usable at runtime, potentially at
58 significant performance cost.
59
60 The primary motivation for disabling SMT is to mitigate the risk of
61 leaking data between threads running on the same CPU core (due to
62 e.g., shared caches). This attack vector is unproven.
63
64 Disabling SMT is a supplement to the L1 data cache flushing mitigation
65 (see [](#opt-security.virtualisation.flushL1DataCache))
66 versus malicious VM guests (SMT could "bring back" previously flushed
67 data).
68 '';
69 };
70
71 security.forcePageTableIsolation = lib.mkOption {
72 type = lib.types.bool;
73 default = false;
74 description = ''
75 Whether to force-enable the Page Table Isolation (PTI) Linux kernel
76 feature even on CPU models that claim to be safe from Meltdown.
77
78 This hardening feature is most beneficial to systems that run untrusted
79 workloads that rely on address space isolation for security.
80 '';
81 };
82
83 security.virtualisation.flushL1DataCache = lib.mkOption {
84 type = lib.types.nullOr (
85 lib.types.enum [
86 "never"
87 "cond"
88 "always"
89 ]
90 );
91 default = null;
92 description = ''
93 Whether the hypervisor should flush the L1 data cache before
94 entering guests.
95 See also [](#opt-security.allowSimultaneousMultithreading).
96
97 - `null`: uses the kernel default
98 - `"never"`: disables L1 data cache flushing entirely.
99 May be appropriate if all guests are trusted.
100 - `"cond"`: flushes L1 data cache only for pre-determined
101 code paths. May leak information about the host address space
102 layout.
103 - `"always"`: flushes L1 data cache every time the hypervisor
104 enters the guest. May incur significant performance cost.
105 '';
106 };
107 };
108
109 config = lib.mkMerge [
110 (lib.mkIf (!config.security.allowUserNamespaces) {
111 # Setting the number of allowed user namespaces to 0 effectively disables
112 # the feature at runtime. Note that root may raise the limit again
113 # at any time.
114 boot.kernel.sysctl."user.max_user_namespaces" = 0;
115
116 assertions = [
117 {
118 assertion = config.nix.settings.sandbox -> config.security.allowUserNamespaces;
119 message = "`nix.settings.sandbox = true` conflicts with `!security.allowUserNamespaces`.";
120 }
121 ];
122 })
123
124 (lib.mkIf config.security.unprivilegedUsernsClone {
125 boot.kernel.sysctl."kernel.unprivileged_userns_clone" = lib.mkDefault true;
126 })
127
128 (lib.mkIf config.security.protectKernelImage {
129 # Disable hibernation (allows replacing the running kernel)
130 boot.kernelParams = [ "nohibernate" ];
131 # Prevent replacing the running kernel image w/o reboot
132 boot.kernel.sysctl."kernel.kexec_load_disabled" = lib.mkDefault true;
133 })
134
135 (lib.mkIf (!config.security.allowSimultaneousMultithreading) {
136 boot.kernelParams = [ "nosmt" ];
137 })
138
139 (lib.mkIf config.security.forcePageTableIsolation {
140 boot.kernelParams = [ "pti=on" ];
141 })
142
143 (lib.mkIf (config.security.virtualisation.flushL1DataCache != null) {
144 boot.kernelParams = [
145 "kvm-intel.vmentry_l1d_flush=${config.security.virtualisation.flushL1DataCache}"
146 ];
147 })
148 ];
149}