Hardening a NixOS desktop with sysctl

The kernel settings my NixOS flake changes, what each one blocks, what it breaks, and the one line in my own config that undoes more than the rest put together.

On this page
  1. Why a desktop needs it
  2. Keep processes out of each other
  3. Hide kernel addresses
  4. Shrink the BPF and perf surface
  5. Close old doors
  6. Protect shared temp directories
  7. Boot parameters
  8. The line that undoes the rest
  9. Left off, and why
  10. Takeaways

Why a desktop needs it

A laptop isn't a server, but mine runs a browser, a chat client, game launchers, npm install and whatever an AppImage decides to do. Most kernel hardening costs nothing noticeable, so my NixOS flake sets a group of sysctls and boot parameters in one module. In NixOS that's an attribute set:

boot.kernel.sysctl = {
  "kernel.yama.ptrace_scope" = 3;
  "kernel.kptr_restrict" = 2;
  # …
};

The settings come from the NixOS hardening discussion and other people's public configs. What's worth writing down is what each one costs.

Keep processes out of each other

"kernel.yama.ptrace_scope" = 3;
"fs.suid_dumpable" = 0;

ptrace is how debuggers attach to processes, and how malware reads another process's memory. Yama scope 3 disables attachment completely, including PTRACE_TRACEME, for every user including root. It can't be lowered until reboot.

Cost: gdb, strace and ltrace stop working, attached or launched. Some anti-cheat and crash reporters break too. Scope 1 (a parent can trace its children) keeps most of the protection and leaves normal debugging working; use that if you debug often.

suid_dumpable = 0 stops setuid programs writing core dumps that could hold privileged memory.

Hide kernel addresses

"kernel.kptr_restrict" = 2;
"kernel.dmesg_restrict" = 1;
"kernel.printk" = "3 3 3 3";

Kernel exploits usually need to know where kernel code and data live. kptr_restrict = 2 prints kernel pointers in /proc as zeros for everyone. dmesg_restrict keeps the kernel log, which leaks the same addresses, away from unprivileged users. The printk levels keep anything below errors off the console at boot.

Cost: sudo dmesg. Nothing else.

Shrink the BPF and perf surface

"kernel.unprivileged_bpf_disabled" = true;
"net.core.bpf_jit_enable" = config.services.scx.enable;
"kernel.perf_event_paranoid" = 3;
"kernel.ftrace_enabled" = false;

eBPF and perf have produced a steady run of local privilege escalation bugs. Requiring CAP_BPF for bpf() removes most of that exposure.

The JIT line is why I like Nix for this. The flake has an option for the scx userspace schedulers, which are BPF programs and need the JIT. Instead of a comment saying "remember to change this", the sysctl is computed from the option: enabling scx turns the JIT on, disabling it turns it off.

Perf caveat: upstream kernels treat any perf_event_paranoid above 2 as 2. Level 3, which blocks perf_event_open for unprivileged users entirely, comes from a patch Debian and Android carry. On a stock NixOS kernel the 3 behaves like 2: harmless, not stronger. perf record needs sudo either way.

Close old doors

"kernel.sysrq" = 0;
"kernel.kexec_load_disabled" = true;
"dev.tty.ldisc_autoload" = 0;
"dev.tty.legacy_tiocsti" = 0;
Setting What it removes Cost
sysrq = 0 Keyboard-triggered kernel actions, such as killing every process No recovery keys on a frozen system
kexec_load_disabled Replacing the running kernel without a reboot, e.g. with one that lacks these protections None for me
ldisc_autoload = 0 Autoloading rarely used TTY line disciplines, a past source of exploitable bugs Loading one needs CAP_SYS_MODULE
legacy_tiocsti = 0 Pushing keystrokes into a terminal's input, e.g. from inside su into the parent shell None for me

Protect shared temp directories

"fs.protected_fifos" = 2;
"fs.protected_regular" = 2;
"fs.protected_symlinks" = 1;

These stop a classic race in world-writable sticky directories like /tmp: an attacker creates a file, FIFO or symlink with the name a privileged program is about to open. With these set, the kernel refuses to open or follow it unless the owners match.

Boot parameters

  • slab_nomerge, page_poison=on, page_alloc.shuffle=1: heap exploits become less reliable.
  • randomize_kstack_offset=on: the kernel stack offset changes on every syscall.
  • vsyscall=none: removes a legacy fixed-address page only very old 64-bit binaries use.
  • debugfs=off: hides an interface that exposes a lot of kernel internals.
  • oops=panic: a recoverable kernel error becomes a panic, so a half-broken kernel doesn't keep running after an exploit attempt.

The line that undoes the rest

Same file:

"mitigations=off"

That disables the kernel's protections against Spectre, Meltdown and related CPU side-channel attacks. It's popular on desktops because the mitigations cost measurable performance on some workloads. But those attacks read memory across exactly the boundaries the settings above protect. Hiding kernel pointers means little when a side channel can leak kernel memory directly.

Each line looks reasonable on its own, which is how a config file hides a contradiction like this. The better default: leave mitigations on, measure the actual cost on your CPU and workloads, and decide from the numbers.

Left off, and why

Some settings are in the file commented out:

  • kernel.modules_disabled = 1 stops module loading after boot. Turning it on broke boot. Doing it properly means finding every module the system needs and loading them before the switch flips.
  • module.sig_enforce=1 and lockdown=confidentiality would block unsigned modules and stop root reading kernel memory, but break out-of-tree drivers and some debugging tools.
  • net.core.bpf_jit_harden = 2 makes JIT spraying harder, at a performance cost I haven't measured yet.

Takeaways

  • Kernel hardening on a desktop is mostly cheap: sudo for dmesg and perf, plus losing debuggers if you go to ptrace scope 3.
  • Derive related settings from each other in code. A sysctl computed from a service option can't drift from it.
  • Read your own config as an attacker would. One performance flag can cancel a page of careful settings.