brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: NoNewPrivileges=yes stops a systemd service from gaining root through setuid

Systemd
~/tips/tip-systemd-no-new-privs

NoNewPrivileges=yes

Make sure a service and its children can never gain more rights through sudo or setuid binaries.

If someone gets code running inside a web app, the next step is often a setuid program or sudo to get root. One setting closes that path for the whole service.

sudo systemctl edit myapp.service

[Service]
NoNewPrivileges=yes

With this set, the kernel flag no_new_privs is turned on for the process and everything it starts. Setuid and setgid bits, and file capabilities, are ignored from then on, so sudo, su or ping with capabilities can not lift the process above its own user.

Check that it took effect:

grep NoNewPrivs /proc/$(systemctl show -p MainPID --value myapp)/status

For a service with the setting, that prints NoNewPrivs: 1.

It is one of the cheapest hardening settings, and several other sandbox settings, such as SystemCallFilter=, turn it on implicitly. systemd-analyze security lists it among its checks.

Things break when a service needs those rights on purpose. A backup script that calls sudo, a mail setup that relies on a setgid sendmail, or a monitoring check using a setuid helper will fail with a permission error. I’d test it first and look in the journal for sudo complaining that the effective uid is not 0.

Further reading: systemd.exec man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link