1# A profile with most (vanilla) hardening options enabled by default,
2# potentially at the cost of features and performance.
3
4{ config, lib, pkgs, ... }:
5
6with lib;
7
8{
9 boot.kernelPackages = mkDefault pkgs.linuxPackages_hardened;
10
11 security.hideProcessInformation = mkDefault true;
12
13 security.lockKernelModules = mkDefault true;
14
15 security.apparmor.enable = mkDefault true;
16
17 boot.kernelParams = [
18 # Overwrite free'd memory
19 "page_poison=1"
20
21 # Disable legacy virtual syscalls
22 "vsyscall=none"
23
24 # Disable hibernation (allows replacing the running kernel)
25 "nohibernate"
26 ];
27
28 # Restrict ptrace() usage to processes with a pre-defined relationship
29 # (e.g., parent/child)
30 boot.kernel.sysctl."kernel.yama.ptrace_scope" = mkOverride 500 1;
31
32 # Prevent replacing the running kernel image w/o reboot
33 boot.kernel.sysctl."kernel.kexec_load_disabled" = mkDefault true;
34
35 # Restrict access to kernel ring buffer (information leaks)
36 boot.kernel.sysctl."kernel.dmesg_restrict" = mkDefault true;
37
38 # Hide kptrs even for processes with CAP_SYSLOG
39 boot.kernel.sysctl."kernel.kptr_restrict" = mkOverride 500 2;
40
41 # Unprivileged access to bpf() has been used for privilege escalation in
42 # the past
43 boot.kernel.sysctl."kernel.unprivileged_bpf_disabled" = mkDefault true;
44
45 # Disable bpf() JIT (to eliminate spray attacks)
46 boot.kernel.sysctl."net.core.bpf_jit_enable" = mkDefault false;
47
48 # ... or at least apply some hardening to it
49 boot.kernel.sysctl."net.core.bpf_jit_harden" = mkDefault true;
50
51 # A recurring problem with user namespaces is that there are
52 # still code paths where the kernel's permission checking logic
53 # fails to account for namespacing, instead permitting a
54 # namespaced process to act outside the namespace with the
55 # same privileges as it would have inside it. This is particularly
56 # bad in the common case of running as root within the namespace.
57 #
58 # Setting the number of allowed user namespaces to 0 effectively disables
59 # the feature at runtime. Attempting to create a user namespace
60 # with unshare will then fail with "no space left on device".
61 boot.kernel.sysctl."user.max_user_namespaces" = mkDefault 0;
62
63 # Raise ASLR entropy for 64bit & 32bit, respectively.
64 #
65 # Note: mmap_rnd_compat_bits may not exist on 64bit.
66 boot.kernel.sysctl."vm.mmap_rnd_bits" = mkDefault 32;
67 boot.kernel.sysctl."vm.mmap_rnd_compat_bits" = mkDefault 16;
68}