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