articles/article.md

5.8 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 server 100% bullet proof - it's a huge endless topic, this article will cover some feasible essential things 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). However, systemd hardening defaults are quite loose (perhaps, not to disturb the operation of new 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.

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

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

While there are many areas of server protection, like keeping the 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), we will focus on systemd means (and a bit more, where systemd is not sophisticated enough).

final notes

#Protection against outside threats: potential vulnerabilities and unauthorized access.

Systemd hardening is just a part of measures to be taken to narrow the potential threat landscape and risks.

Ideally, vulnerability scanning, penetration testing 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.

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

overview of systemd integration within NixOS

- 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

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.