4 ms·
All of these isolation techniques (and much more) can be used inside systemd units. Write your .service as usual, then run this command on it: $ systemd-anal
by scaladev 6y ago
All of these isolation techniques (and much more) can be used inside systemd units. Write your .service as usual, then run this command on it:
$ systemd-analyze security service-name
It prints out a long list of hardening flags that can be applied inside of your service, like so:
NAME DESCRIPTION EXPOSURE
PrivateNetwork= Service has access to the host's network 0.5
User=/DynamicUser= Service runs as root user 0.4
CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP) Service may change UID/GID identities/capabilities 0.3
CapabilityBoundingSet=~CAP_SYS_ADMIN Service has administrator privileges 0.3
...
Here's what I typically use for a .NET 5 application:
WorkingDirectory = /opt/appname/app
ReadWritePaths = /opt/appname/data
UMask = 0077
LockPersonality = yes
NoNewPrivileges = yes
PrivateDevices = yes
PrivateMounts = yes
PrivateTmp = yes
PrivateUsers = yes
ProtectClock = yes
ProtectControlGroups = yes
ProtectHome = yes
ProtectHostname = yes
ProtectKernelLogs = yes
ProtectKernelModules = yes
ProtectKernelTunables = yes
ProtectSystem = strict
RemoveIPC = yes
RestrictAddressFamilies = AF_UNIX AF_INET AF_INET6
RestrictNamespaces = yes
RestrictRealtime = yes
RestrictSUIDSGID = yes
SystemCallArchitectures = native
ProtectProc = invisible
CapabilityBoundingSet =
SystemCallFilter = ~@clock @module @mount @raw-io @reboot @swap @privileged @cpu-emulation @obsolete
ReadWritePaths should be replaced with a combination of DynamicUser + writing local persistent data to $STATE_DIRECTORY, but I'm too lazy to do that yet.
See systemd.exec(5) for more.
- CameronNemo 6y agoWell sure, but I'm not using systemd. If I wanted a container runtime written in C, I would use crun. But I'd rather not use that either.
- goatinaboat 6y agoWell sure, but I'm not using systemd. I hear you but fighting systemd in 2021 is like pushing water uphill. With a fork.
- CameronNemo 6y agoI'm not fighting anything. Just don't use it. Easy as that. I don't fudge with configs, carefully read manpages like systemd.exec, or put any effort into how I keep services running on my personal devices.
- nikisweeting 6y agoWhat's the systemd equivalent to docker-compose? The real value-add of Docker imo is not the security, it's the easy packaging, distribution, and running of otherwise complicated apps.
- dundarious 6y agoWriting N systemd service files with After=, etc., to match your docker-compose.yml that has N services in it. I have ported a few docker-compose.yml-s this way, because I don’t understand docker enough to troubleshoot issues with getting my firewall rules to apply to docker traffic. Dependency hell is not an issue for these projects, so I feel happier with systemd.
- eeZah7Ux 6y agoThis is excellent advice. Together with systemd-nspawn it's all one needs to run a container in a strong sandbox.
- t0astbread 6y agoMaybe a bit off-topic on this article but what does systemd-nspawn add compared to the aforementioned isolation options and how can you combine the two?
- eeZah7Ux 6y agosystemd-nspawn does the same things as docker, but with much smaller attack surface and can be sandboxed very effectively by the unit files. The isolation provided by unit files is orthogonal with running containers. nspawn is not even running a dedicated daemon. Plus, it's no secret that docker was not designed with security in mind and its isolation is bolted on. [1] Furthermore, systemd is already installed and running on most systems (like it or not) [1] https://www.cvedetails.com/vendor/13534/Docker.html https://www.cvedetails.com/vendor/13534/Docker.html
- totony 6y agoAt this point isn't it better to just run it inside a container like lxc?