293 points · by capableweb · 4y ago · 78 comments
https://news.ycombinator.com/item?id=29976096
Or simply follow whatever `systemd-analyze security` recommends, just make sure you run it on a system with recent systemd.
systemd-analyze security
Is there a tool like `audit2allow` for systemd units? selinux/python/audit2allow/audit2allow: https://github.com/SELinuxProject/selinux/blob/master/python...I haven't seen any difference between distributions with the same systemd version. Anything with a recent one should do fine. More recent than RHEL8, mind you (which is on systemd 239): for example, a syscall allow/deny analysis is buggy there and asks you to enable some protections, and then disable them. The same unit is analyzed correctly on my desktop with v250 (I use the popular rolling release distribution).
I haven't seen anything like audit2allow. It's probably not especially necessary because of the difference in philosophies: SELinux is deny by default, while in systemd you're playing whack-a-mole anyway, and are expected to add directives one by one until the application stops working. Unit logs usually make it obvious if something was denied.
firejail does this a bit better but it also started out with a blacklist approach and it's more geared towards desktop application use, not system services.
If you trust those programmers, it’s indeed way more convenient than other tools, if only because it removes the need for configuring things twice. For example, instead of configuring your web server to serve files from /foo/bar/ _and_ telling SELinux that your web server is allowed to read from /foo/bar, you only configure the web server, and it will tell the OS “I shouldn’t read from anything but /foo/bar, starting … now”.
You’ll have to trust the web server to do that, though.
Systemd also supports firewalling: it supports IP address allow/deny policies, ports, etc. For more advanced firewall policies you're probably better off using an actual firewall daemon like firewalld or ufw.
[0] https://risky.biz/netcasts/risky-business/ [1] https://www.airlockdigital.com/
$ systemd-run --user --scope --property=MemoryHigh=1G qbittorrent
It works just as you'd expect — if qbittorrent's working set goes above 1024 MiB, it pushes the least recently used page out of the page cache. Doesn't really have any effects on upload or download speeds, while helping to keep more useful data in memory.Many isolation flags are not available in `systemd-run --user`, though, so if you'd like to have some protection you either have to combine `sudo systemd-run` with `su -c`, or wrap the command in firejail.
https://github.com/netblue30/firejail/
It uses the same kernel knobs as systemd does, but is more user-friendly and has more features.
I use it for every application that handles data received from other machines: books, images, documents, whatever.
In my case I'm using Bubblewrap because Firejail was only used for Zoom, and this felt a bit of a waste considering Bubblewrap was already installed.
1. add this to .service
AmbientCapabilities=CAP_NET_BIND_SERVICE
2. or listen on :8080 and use NAT: iptables -t nat -I OUTPUT -p tcp -o lo --dport 80 -j REDIRECT --to-ports 8080
3. or make the port unprivileged sysctl -w net.ipv4.ip_unprivileged_port_start=80
It may work for httpd too, I haven't tested it.https://www.freedesktop.org/software/systemd/man/systemd.exe...
Not everything that wants to open up a port seems to support socket activation. I tried with 6tunnel and couldn't get it to work.
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
SocketBindDeny=any
SocketBindAllow=tcp:80
SocketBindAllow=tcp:443
These ports should be denied by the kernel because they're already taken by httpd, and all other will be denied by bpf filters installed by systemd.It feels like plugging holes in a dam, but that's what you do with popular operating systems.
i added support for httpd to support systemd socket activation in 2013: https://svn.apache.org/viewvc?view=revision&revision=1511033
httpd can start as non-root, assuming other configurations like the access / error logs are writable by the non-root user.
This allows to let systemd to manage ports using socket units which will also stay up and buffer requests when restarting a service, allow service activation on demand/incoming requests or per connection service instances, e.g. for better isolation of sshd's per connection/user.
One approach that may work with systemd is to have two processes. One would be a broker, running as root. It would grab a port, for example. The other process would be spawned by the broker as a limited service and inherit that port from the parent, with no permissions of its own to open it, only to inherit.
IDK how to express that in systemd-land though. At that point you might be better off just writing the code to sandbox things yourself.
SELinux is a policy system where policy is enforced via labels.
Labels are applied to processes which classify what the process is.
Labels are applied to files which define the what classification of process can access the file.
The application of labels happens automatically based on policy. Such policy would include the location of the file or the label of the parent process.
As an example, the default policy for httpd would prevent httpd from accessing /etc/passwd even though the process is running as (or can be) the root user. I believe you could also do interesting things like prevent httpd from opening a socket on a non-standard port if you wanted to.
SELinux is very powerful but complicated. Ideally you use this with distro packages which should have policies already configured for you.
Critically it is not one vs the other. Use both if you have it.
I would pretty much never ask a human being to write SELinux policies unless that was explicitly part of their job whereas I can pretty much point any developer to what systemd is providing and they'll be able to work with it.
So the overlap choice seems to be more around SELinux versus Capabilities. Where SELinux is more fine-grained and tunable, but more complicated also.
The overlap/comparison between capabilities, systemds features, and selinux features isn't really well defined in any meaningful way IMO. It's really like 5 different features being used in various ways.
The normal filesystem permissions of read/write/execute for user/group/other are among those known as "discretionary access controls," meaning that they can be relaxed.
The systemd unit security options are discretionary, at the control of the administrator.
The rules can also be adjusted, and there are a number of tunable parameters.
The intent is that it is never disabled.
That is, the same command that gets denied by SELinux through systemd will run fine (and unprotected) when started from a shell.
Do you write your own policies for individual end-user programs?
[Service]
ProtectSystem=strict
ReadWritePaths=/some/path
ReadOnlyPaths=/some/otherpath
InaccessiblePaths=/etc
(and I say this based on both observation and personal experience, I have some stuff to harden later this year and I'm really hoping I'll be able to involve somebody who -has- that level of SElinux knowledge but plan B is almost certainly going to be 'mst does his best with the unit configs')
Caveat: it has to dig into ALL the linked libraries as well.
Then anything with systemd and security can stuff it.