GrepBoard/Manuals
605 manuals ·

systemd-analyze security from 9.4 to 1.9: what breaks

·Tested on Ubuntu 24.04 · systemd 255 · 2026-10·Test scope ↓·6 steps

Harden a systemd unit in 12 steps while scoring it with systemd-analyze security --offline. From 9.4 down to 1.9, see where writes and chown started to fail.

output
→ Overall exposure level for step0.service: 9.4 UNSAFE :-{

A service unit with no sandboxing scores 9.4 UNSAFE in systemd-analyze security. Each hardening directive lowers the score, but some of them immediately block writes or privileges the service needs. Below, --offline=true scored unit files directly, and the same directives were applied to a transient service at each step to see what broke.

[1] Score a unit file before installing it. --offline=true reads the file instead of asking PID 1, so nothing has to be registered or started (available since systemd 250).

console
$ systemd-analyze security --offline=true step0.service | grep -E "PrivateTmp=|ProtectSystem=|ProtectHome=|NoNewPrivileges=|CapabilityBoundingSet=~CAP_SYS_ADMIN"✗ NoNewPrivileges=                                            Service processes may acquire new privileges                                 0.2✗ ProtectSystem=                                              Service has full access to the OS file hierarchy                             0.2✗ ProtectHome=                                                Service has full access to home directories                                  0.2✗ CapabilityBoundingSet=~CAP_SYS_ADMIN                        Service has administrator privileges                                         0.3✗ PrivateTmp=                                                 Service has access to other software's temporary files                       0.2

The number at the end of each ✗ line is what that item adds to the score.

[2] Add directives step by step and watch both the score and the behaviour. The unit runs python3 -m http.server as root. Behaviour was checked by applying the same directives to a transient service with systemd-run --wait --pipe -p … that ran a shell probe. The data directory was /home/app/data.

StepDirective addedScoreWhat changed
0(none)9.4Everything allowed
1NoNewPrivileges=yes9.2No change
2PrivateTmp=yes9.0Private /tmp
3ProtectSystem=strict8.9/usr read-only, **the two tested paths under /home still writable**
4ProtectHome=read-only8.8Writes to /home/app/data fail
5ReadWritePaths=/home/app/data8.8Data writes work again (other /home paths still fail)
6PrivateDevices=yes8.2No change
7Four ProtectKernel*/ProtectControlGroups7.2No change
8CapabilityBoundingSet= (empty)5.2chown fails even as root
9RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX4.8No change (bind to 127.0.0.1:18080 works)
10Six more incl. RestrictNamespaces3.7No change
11SystemCallArchitectures=native, SystemCallFilter=@system-service2.1No change
12MemoryDenyWriteExecute, UMask=0077, PrivateUsers=yes1.9No change
console
$ systemd-analyze security --offline=true step12.service | tail -1→ Overall exposure level for step12.service: 1.9 OK :-)

[3] After setting ProtectSystem=strict, do not assume writes are blocked; try a write from inside the service. The documentation says strict mounts the entire file system hierarchy read-only except /dev, /proc and /sys, but in this test environment (systemd 255) step 3 with strict alone could still write to the two tested paths under /home; writes were blocked only after ProtectHome= was added in step 4. The mount layout and propagation of those paths were not checked, so the cause could not be confirmed (the documentation itself lists exceptions such as submounts established later). If the service keeps data under /home, set both and open only the paths it writes with ReadWritePaths=. The error the service produced at step 4:

output
/home/app/probe.sh: 5: cannot create /home/app/data/probe: Read-only file system

When Read-only file system shows up in the log, first check whether that path belongs in ReadWritePaths=.

[4] An empty CapabilityBoundingSet= takes privileges away from a root service too. At step 8, chown on a data file failed. If the service genuinely needs a privilege (changing ownership, binding below port 1024), keep just that one, for example CapabilityBoundingSet=CAP_CHOWN, instead of an empty value.

[5] Apply it to the real unit. Do not edit the packaged unit; add a drop-in with its own name that holds only this change. A plain systemctl edit may open an existing override.conf (environment variables, arguments and other settings), which then gets mixed into your rollback. --drop-in= exists since systemd 253.

console
$ ls /etc/systemd/system/myapp.service.d/ 2>/dev/null$ sudo systemctl edit --drop-in=hardening myapp.service
/etc/systemd/system/myapp.service.d/hardening.conf
[Service]NoNewPrivileges=yesPrivateTmp=yesProtectSystem=strictProtectHome=read-onlyReadWritePaths=/home/app/dataPrivateDevices=yes
console
$ sudo systemctl restart myapp.service$ systemctl status myapp.service$ systemd-analyze security myapp.service | tail -1

Add a few directives at a time, as in the table, and test the features after each restart. A service can start fine while a single feature (uploads, writing its own logs) breaks. If it fails, read the journal first as in debug a failed systemd service.

To undo, delete only the drop-in you created (leave override.conf and any other existing file alone) and reload:

console
$ sudo rm /etc/systemd/system/myapp.service.d/hardening.conf$ sudo systemctl daemon-reload && sudo systemctl restart myapp.service

[6] Gate deployments on the score. --threshold= used ten times the displayed scale: the 1.9 unit failed with --threshold=5 and passed with --threshold=50.

console
$ systemd-analyze security --offline=true --threshold=50 step12.service >/dev/null(exit=0)$ systemd-analyze security --offline=true --threshold=50 step0.service >/dev/null(exit=1)

The (exit=…) lines were printed by the test script. The documentation only says the default is 100 without explaining the unit, so this rests on the results above (9.4 fails at 50 and passes at 100).

#systemd#systemd-analyze#hardening#protectsystem#security