articles/article.md

7.0 KiB

hardening of systemd services in NixOS

introduction

When it comes to security, we care about limiting access of each entity of a system to as few other entities as possible. Network input, executables and users must be able to reach only those resources, which are necessary to perform the defined server tasks.

Generally, it's better to implement as many layers of security as possible. Although, there is no way to make a server 100% bullet proof - it's a huge endless topic, this article covers some feasible essential systemd tunables that give us a layer of protection.

Systemd is the standard software suite for organizing and running services/daemons in a modern GNU/Linux distribution, including NixOS. Systemd provides means to secure services. And in many ways, the isolation level of a systemd service can be similar to that of containers (by the means of namespaces, cgroups, etc; interestingly, systemd even allows running multiple instances of the same service). However, systemd hardening defaults are quite loose (perhaps, not to disturb the operation of newly written services and their administrators in any way).

What NixOS does - it generates systemd configuration files in accordance to NixOS configuration given, written in Nix language. To some extent, Nix acts as a macro language and NixOS configuration module system acts as a unified control center, so that you don't bother about location of systemd files, their syntax and common stuff, which NixOS generates for you. Also, NixOS manages runtime switching between systemd configurations, conducting services restarts when required and whole system rollbacks from GRUB/systemd-boot/extlinux.

overview of systemd integration within NixOS

NixOS features lots of systemd services, which are ready to use (without even knowing what systemd is) just by setting appropriate options in configuration.nix. For example, services.netdata.enable = true. Documentation for all related options can be found on the website or in man configuration.nix (also in man home-configuration.nix for managing desktop user services).

- configuring systemd service units in NixOS step by step (edit, rebuild (maybe in VM), `systemd status`, `systemd restart`, `systemd cat`, `htop` tree)

resources limits strategy

existing practices and solutions within NixOS

list of systemd options and their implications

cgroups

cgroup - control group. Docker's isolation implementation is also based on cgroups.

Enabling netdata service in NixOS enables systemd.enableCgroupAccounting, which in turn enables these options in systemd.conf:

DefaultCPUAccounting=yes
DefaultIOAccounting=yes
DefaultBlockIOAccounting=yes
DefaultIPAccounting=yes

hardening in available services provided by NixOS upstream

NixOS already provides more or less isolation for many services, which are available as services.NAME_OF_SERVICE options.

hardening in your own systemd services

You can manually test various systemd options without writing service files using systemd-run, for example:

$ ls -l /home
total 0
drwx------ 1 alex users 1126 2023-06-21 19:26 alex

sudo systemd-run -p ProtectHome=yes --shell
Running as unit: run-u2544.service
Press ^] three times within 1s to disconnect TTY.

# ls -l /home
total 0

# exit
Finished with result: success
Main processes terminated with: code=exited/status=0
Service runtime: 2.749s
CPU time consumed: 50ms
IP traffic received: 0B
IP traffic sent: 0B
IO bytes read: 0B
IO bytes written: 0B

blocking outgoing internet connections

The idea is to keep responding to incoming requests to some service, but forbid any outgoing connections, initiated by itself.

When it comes to a more sophisticated firewall, unfortunatelly systemd is not capable of such granular control. So, iptables configuration will be:

networking.firewall = {
  extraCommands = ''
    iptables -t filter -I OUTPUT 1 -m owner --uid-owner ${user} -m state --state NEW -j REJECT
  '';
  extraStopCommands = ''
    iptables -t filter -D OUTPUT 1 -m owner --uid-owner ${user} -m state --state NEW
  '';
};

testing

example-systemd-service.nix features a way to run a shell inside a systemd service in order to test our isolation in practice. You can just add path to the file to the imports list in configuration.nix and execute nixos-rebuild switch or nixos-rebuild test (if you don't want new configuration to be permanent; however, it creates ./result symbolic link in current directory).

unsolved problems

confinement.enable is not compatible with systemd's ProtectSystem.

final notes

###security principles and strategy #1. define desired security requirements #2. apply systemd hardening options, suggested by systemd-analyze (until they harm service functionality) #3. vulnerability scanning, penetration testing, and security audits #4. monitor and respond

Systemd hardening is just a part of measures to be taken to narrow the potential threat landscape and risks for a server. Ideally, vulnerabilities scanning, penetration testing, unauthorized access prevention and security audits should be involved. Take advantage of monitoring tools and respond quickly, according to a rescue plan to mitigate the impact of incidents. Keep running software up to date and respond to CVEs (deploying software with patches is easy in NixOS in case it hasn't been already patched).

Have a rescue plan to mitigate the impact of incidents. This might include restoring system from backups, keys and passwords reset. Business continuity plan.

In order for the actions (measures?) taken not to be ad-hoc, but rather systematic.