2020-05-22 06:59:18 -07:00
|
|
|
lib: pkgs:
|
2019-04-27 16:53:26 -07:00
|
|
|
|
|
|
|
with lib;
|
2021-02-03 13:44:41 -08:00
|
|
|
|
|
|
|
# See `man systemd.exec` and `man systemd.resource-control` for an explanation
|
2021-08-15 02:28:44 -07:00
|
|
|
# of the systemd-related options available through this file.
|
2020-09-08 05:25:33 -07:00
|
|
|
let self = {
|
2020-05-06 01:57:48 -07:00
|
|
|
# These settings roughly follow systemd's "strict" security profile
|
2019-04-27 12:21:45 -07:00
|
|
|
defaultHardening = {
|
|
|
|
PrivateTmp = "true";
|
2020-05-05 08:15:16 -07:00
|
|
|
ProtectSystem = "strict";
|
2019-04-27 15:27:25 -07:00
|
|
|
ProtectHome = "true";
|
2019-04-27 12:21:45 -07:00
|
|
|
NoNewPrivileges = "true";
|
|
|
|
PrivateDevices = "true";
|
|
|
|
MemoryDenyWriteExecute = "true";
|
2019-04-27 15:27:25 -07:00
|
|
|
ProtectKernelTunables = "true";
|
|
|
|
ProtectKernelModules = "true";
|
2021-01-30 14:08:38 -08:00
|
|
|
ProtectKernelLogs = "true";
|
|
|
|
ProtectClock = "true";
|
2021-08-04 15:49:01 -07:00
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProcSubset = "pid";
|
2019-04-27 15:27:25 -07:00
|
|
|
ProtectControlGroups = "true";
|
|
|
|
RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6";
|
2019-04-28 06:11:27 -07:00
|
|
|
RestrictNamespaces = "true";
|
2019-04-27 15:27:25 -07:00
|
|
|
LockPersonality = "true";
|
2019-04-27 16:53:26 -07:00
|
|
|
IPAddressDeny = "any";
|
2020-05-06 01:28:00 -07:00
|
|
|
PrivateUsers = "true";
|
2020-05-06 01:19:14 -07:00
|
|
|
RestrictSUIDSGID = "true";
|
|
|
|
RemoveIPC = "true";
|
|
|
|
RestrictRealtime = "true";
|
|
|
|
ProtectHostname = "true";
|
2020-05-05 06:27:07 -07:00
|
|
|
CapabilityBoundingSet = "";
|
2020-05-06 01:57:48 -07:00
|
|
|
# @system-service whitelist and docker seccomp blacklist (except for "clone"
|
|
|
|
# which is a core requirement for systemd services)
|
2021-02-01 13:53:08 -08:00
|
|
|
# @system-service is defined in src/shared/seccomp-util.c (systemd source)
|
2022-04-30 06:35:45 -07:00
|
|
|
SystemCallFilter = [ "@system-service" "~add_key kcmp keyctl mbind move_pages name_to_handle_at personality process_vm_readv process_vm_writev request_key set_mempolicy setns unshare userfaultfd" ];
|
2021-02-01 13:53:10 -08:00
|
|
|
SystemCallArchitectures = "native";
|
2019-04-27 12:21:45 -07:00
|
|
|
};
|
2019-11-27 05:04:22 -08:00
|
|
|
|
2021-03-22 05:19:46 -07:00
|
|
|
allowNetlink = {
|
|
|
|
RestrictAddressFamilies = self.defaultHardening.RestrictAddressFamilies + " AF_NETLINK";
|
|
|
|
};
|
|
|
|
|
2019-05-03 03:44:16 -07:00
|
|
|
# nodejs applications apparently rely on memory write execute
|
|
|
|
nodejs = { MemoryDenyWriteExecute = "false"; };
|
2021-03-22 05:19:45 -07:00
|
|
|
|
|
|
|
# Allow takes precedence over Deny.
|
|
|
|
allowLocalIPAddresses = {
|
2022-05-07 11:34:21 -07:00
|
|
|
IPAddressAllow = [
|
|
|
|
"127.0.0.1/32"
|
|
|
|
"::1/128"
|
|
|
|
"169.254.0.0/16"
|
|
|
|
];
|
2019-04-28 11:54:13 -07:00
|
|
|
};
|
2021-03-22 05:19:45 -07:00
|
|
|
allowAllIPAddresses = { IPAddressAllow = "any"; };
|
|
|
|
allowTor = self.allowLocalIPAddresses;
|
|
|
|
allowedIPAddresses = onlyLocal:
|
|
|
|
if onlyLocal
|
|
|
|
then self.allowLocalIPAddresses
|
|
|
|
else self.allowAllIPAddresses;
|
2019-04-27 16:53:26 -07:00
|
|
|
|
2021-11-28 12:24:49 -08:00
|
|
|
tor = {
|
|
|
|
proxy = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether to proxy outgoing connections with Tor.";
|
|
|
|
};
|
|
|
|
enforce = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enforce Tor on a service by only allowing connections
|
|
|
|
from and to localhost and link-local addresses.
|
|
|
|
'';
|
|
|
|
};
|
2019-08-07 05:52:34 -07:00
|
|
|
};
|
2020-05-22 06:59:18 -07:00
|
|
|
|
2021-02-01 13:53:23 -08:00
|
|
|
script = name: src: pkgs.writers.writeBash name ''
|
2020-05-22 06:59:18 -07:00
|
|
|
set -eo pipefail
|
|
|
|
${src}
|
|
|
|
'';
|
2020-08-21 13:36:00 -07:00
|
|
|
|
2020-09-08 05:25:33 -07:00
|
|
|
# Used for ExecStart*
|
2021-08-15 02:28:34 -07:00
|
|
|
rootScript = name: src: "+${self.script name src}";
|
2020-09-08 05:25:33 -07:00
|
|
|
|
2020-08-21 13:36:00 -07:00
|
|
|
cliExec = mkOption {
|
|
|
|
# Used by netns-isolation to execute the cli in the service's private netns
|
|
|
|
internal = true;
|
|
|
|
type = types.str;
|
|
|
|
default = "exec";
|
|
|
|
};
|
2021-02-03 13:44:42 -08:00
|
|
|
|
2021-08-04 15:49:00 -07:00
|
|
|
mkOnionService = map: {
|
2021-02-03 13:44:42 -08:00
|
|
|
map = [ map ];
|
|
|
|
version = 3;
|
|
|
|
};
|
2021-10-01 02:51:57 -07:00
|
|
|
|
|
|
|
# Convert a bind address, which may be a special INADDR_ANY address,
|
|
|
|
# to an actual IP address
|
|
|
|
address = addr:
|
|
|
|
if addr == "0.0.0.0" then
|
|
|
|
"127.0.0.1"
|
|
|
|
else if addr == "::" then
|
|
|
|
"::1"
|
|
|
|
else
|
|
|
|
addr;
|
|
|
|
|
|
|
|
addressWithPort = addr: port: "${self.address addr}:${toString port}";
|
|
|
|
|
2020-09-08 05:25:33 -07:00
|
|
|
}; in self
|