infra/nixos/modules/hardening.nix

63 lines
1.8 KiB
Nix
Raw Normal View History

2024-10-13 15:16:39 -05:00
{
config,
lib,
...
}:
with lib; let
2024-12-13 16:47:09 -06:00
cfg = config.gen.hardening;
2024-10-13 15:16:39 -05:00
in {
2024-12-13 16:47:09 -06:00
options.gen.hardening = {
hardenBpf = mkEnableOption "place heavier restrictions on BPF";
fullRpFilter = mkEnableOption "full reverse path filtering. (breaks dynamic routing, probably)";
ignoreIcmpEcho = mkEnableOption "ignore icmp echos. (obviously, this makes pings unresponsive)";
disableSack = mkEnableOption "disable tcp sack";
disableConsole = mkEnableOption "disable console. (not recommended for test machines)";
2024-10-13 15:16:39 -05:00
};
config = {
### Sysctls ###
boot.kernel.sysctl = mkMerge [
2024-10-13 15:16:39 -05:00
{
"kernel.kptr_restrict" = 1;
"kernel.dmesg_restrict" = 1;
"kernel.printk" = "3 3 3 3";
"dev.tty.ldisc_autoload" = 0;
"vm.unprivileged_userfaultfd" = 0;
"kernel.kexec_load_disabled" = 1;
"kernel.sysrq" = 0; # ignore sysrq key
"kernel.perf_event_paranoid" = 3;
"net.ipv4.tcp_rfc1337" = 1; # drop RSTs during time-wait state
}
(mkIf cfg.ignoreIcmpEcho {
2024-10-13 15:16:39 -05:00
"net.ipv4.icmp_echo_ignore_all" = 1;
})
(mkIf cfg.hardenBpf {
2024-10-13 15:16:39 -05:00
"kernel.unprivileged_bpf_disabled" = 1;
"net.core.bpf_jit_harden" = 2;
})
(mkIf cfg.fullRpFilter {
2024-10-13 15:16:39 -05:00
"net.ipv4.conf.all.rp_filter" = 1;
"net.ipv4.conf.default.rp_filter" = 1;
})
(mkIf cfg.disableSack {
2024-10-13 15:16:39 -05:00
"net.ipv4.tcp_sack" = 0;
"net.ipv4.tcp_dsack" = 0;
"net.ipv4.tcp_fack" = 0;
})
];
2024-10-13 15:16:39 -05:00
### Security options ###
security.protectKernelImage = true;
### Disable emergency access ###
systemd.enableEmergencyMode = false;
boot.initrd.systemd.emergencyAccess = false;
### Disable tty login ###
console = {
earlySetup = true;
enable = !cfg.disableConsole;
};
};
}