From 1cb6d223867d1da42267bd6e748714a0c984ebc5 Mon Sep 17 00:00:00 2001 From: HackerNCoder Date: Fri, 18 Oct 2024 16:38:30 +0200 Subject: [PATCH] nixos/bind: harden systemd service --- nixos/modules/services/networking/bind.nix | 55 ++++++++++++++++++---- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/nixos/modules/services/networking/bind.nix b/nixos/modules/services/networking/bind.nix index 6079062db6c3..1192645c2472 100644 --- a/nixos/modules/services/networking/bind.nix +++ b/nixos/modules/services/networking/bind.nix @@ -249,29 +249,68 @@ in }; users.groups.${bindUser} = {}; + systemd.tmpfiles.settings."bind" = lib.mkIf (cfg.directory != "/run/named") { + ${cfg.directory} = { + d = { + user = bindUser; + group = bindUser; + age = "-"; + }; + }; + }; systemd.services.bind = { description = "BIND Domain Name Server"; after = [ "network.target" ]; wantedBy = [ "multi-user.target" ]; preStart = '' - mkdir -m 0755 -p /etc/bind if ! [ -f "/etc/bind/rndc.key" ]; then ${bindPkg.out}/sbin/rndc-confgen -c /etc/bind/rndc.key -u ${bindUser} -a -A hmac-sha256 2>/dev/null fi - - ${pkgs.coreutils}/bin/mkdir -p /run/named - chown ${bindUser} /run/named - - ${pkgs.coreutils}/bin/mkdir -p ${cfg.directory} - chown ${bindUser} ${cfg.directory} ''; serviceConfig = { Type = "forking"; # Set type to forking, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=900788 - ExecStart = "${bindPkg.out}/sbin/named -u ${bindUser} ${lib.optionalString cfg.ipv4Only "-4"} -c ${cfg.configFile}"; + ExecStart = "${bindPkg.out}/sbin/named ${lib.optionalString cfg.ipv4Only "-4"} -c ${cfg.configFile}"; ExecReload = "${bindPkg.out}/sbin/rndc -k '/etc/bind/rndc.key' reload"; ExecStop = "${bindPkg.out}/sbin/rndc -k '/etc/bind/rndc.key' stop"; + User = bindUser; + RuntimeDirectory = "named"; + RuntimeDirectoryPreserve = "yes"; + ConfigurationDirectory = "bind"; + ReadWritePaths = [ + (lib.mapAttrsToList (name: config: "-${config.file}") cfg.zones) + cfg.directory + ]; + CapabilityBoundingSet = "CAP_NET_BIND_SERVICE"; + AmbientCapabilities = "CAP_NET_BIND_SERVICE"; + # Security + NoNewPrivileges = true; + # Sandboxing + ProtectSystem = "full"; + ReadOnlyPaths = "/sys"; + ProtectHome = true; + PrivateTmp = true; + PrivateDevices = true; + PrivateMounts = true; + ProtectHostname = true; + ProtectClock = true; + ProtectKernelTunables = true; + ProtectKernelModules = true; + ProtectKernelLogs = true; + ProtectControlGroups = true; + ProtectProc = "invisible"; + ProcSubset = "pid"; + RemoveIPC = true; + RestrictAddressFamilies = [ "AF_UNIX AF_INET AF_INET6 AF_NETLINK" ]; + LockPersonality = true; + MemoryDenyWriteExecute = true; + RestrictRealtime = true; + RestrictSUIDSGID = true; + RestrictNamespaces = true; + # System Call Filtering + SystemCallArchitectures = "native"; + SystemCallFilter = "~@mount @debug @clock @reboot acct modify_ldt add_key adjtimex clock_adjtime delete_module fanotify_init finit_module get_mempolicy init_module io_destroy io_getevents iopl ioperm io_setup io_submit io_cancel kcmp kexec_load keyctl lookup_dcookie migrate_pages move_pages open_by_handle_at perf_event_open process_vm_readv process_vm_writev ptrace remap_file_pages request_key set_mempolicy swapoff swapon uselib vmsplice"; }; unitConfig.Documentation = "man:named(8)";