The modern scheduled job
Most Linux machines no longer lean on cron alone. systemd runs services, and timers start them on a schedule.
The old bug follows. If a service runs a script an ordinary account can edit, or a unit file it can rewrite, root will run whatever that account wants.
The one rule
Warning
This is your own lab machine. Editing a service on a machine you do not own is a crime without written permission. In the UK that is the Computer Misuse Act 1990. The service here is a practice you build on your own copy.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can find a systemd service an ordinary account can turn into a root shell.
Key words
| Word | What it means | Example |
|---|---|---|
| Service | a program systemd runs and manages | |
| Unit file | the file describing a service | labmon.service |
| ExecStart | the program line a service runs | |
| Timer | a unit that starts a service on a schedule | |
| systemctl | the command that reads and controls services |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsBuild a practice service
The Run sets up a service, labmon, whose program is a world-writable script, and a timer that starts it every half minute. An admin might leave the script writable by mistake.
This is on your own machine, built so you can see the weakness safely.
Set up the practice timer
sudo mkdir -p /opt/labmon && printf '#!/bin/bash\ndate >> /opt/labmon/log\n' | sudo tee /opt/labmon/run.sh && sudo chmod 777 /opt/labmon/run.sh && printf '[Unit]\nDescription=Lab monitor\n[Service]\nType=oneshot\nExecStart=/opt/labmon/run.sh\n' | sudo tee /etc/systemd/system/labmon.serviceShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add the timer and start it
printf '[Unit]\nDescription=Run lab monitor\n[Timer]\nOnBootSec=10s\nOnUnitActiveSec=30s\n[Install]\nWantedBy=timers.target\n' | sudo tee /etc/systemd/system/labmon.timer && sudo systemctl daemon-reload && sudo systemctl enable --now labmon.timerShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the service like an attacker
systemctl cat prints a unit and its ExecStart line. systemctl list-timers shows what is scheduled. The question, as ever: can an ordinary account edit what root runs?
Read the ExecStart line, then check the mode of the file it names.
Read the service and its script's mode
systemctl cat labmon.service --no-pager | grep ExecStart && stat -c "%a %U %n" /opt/labmon/run.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the timers
systemctl list-timers labmon.timer --no-pagerShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: which file lets others win?
# A unit file and its ExecStart script. Which file lets an ordinary account win?
files = {
"/etc/systemd/system/labmon.service": "644 root",
"/opt/labmon/run.sh": "777 root",
}
for path, info in files.items():
mode, owner = info.split()
others_write = mode[2] in "2367"
if others_write:
print(path, "-> anybody can edit it: root runs this")
else:
print(path, "-> only root can edit it")
# Change the unit file to "646 root". Does that open a second door?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsEdit the script, let the timer run it
intern can write the script, so it adds a line. The timer starts the service as root within half a minute, running intern's line as root.
The Run adds the line and waits for the next tick.
intern edits the service's script
sudo -u intern bash -c "echo 'id > /tmp/svc-proof.txt' >> /opt/labmon/run.sh"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Wait for the timer, then read the proof
for i in $(seq 10); do [ -s /tmp/svc-proof.txt ] && break; sleep 5; done; cat /tmp/svc-proof.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people guard the unit file and forget the ExecStart script, or the other way round. Both matter. If an account can edit the unit, it changes what runs; if it can edit the script, it changes what the service does. Lock both.
Lock the unit and the script
The fix is ownership and mode. The unit file is root-owned at 644. The script is root-owned and not writable by others, 755, so the service still runs.
The Run tightens the script; intern can no longer edit it, and the timer still runs.
Lock the script, keep the service working
sudo chmod 644 /etc/systemd/system/labmon.service && sudo chmod 755 /opt/labmon/run.sh && sudo -u intern bash -c "echo x >> /opt/labmon/run.sh" 2>&1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
The timer still works
systemctl is-active labmon.timerShown here, not run. Press Start lab to type it into your own lab’s terminal.
What did the fix do?
Have a go
What did the fix do?
3 questionsHow a defender sees it
systemd-analyze security <service> scores how exposed a service is, and endpoint hardening uses it. Watch /etc/systemd for changes, and keep a baseline of the ExecStart scripts.
The next lesson rolls the setuid, capability, cron and service checks into one baseline.
Assignment: audit the units
Audit a set of systemd units. Each is a dictionary with unit (the unit file path), unit_mode (ten characters), exec (the ExecStart path) and exec_mode (ten characters). Write others_write(mode), a helper returning True when the ninth character of a ten-character mode is w. Write weak_units(units): the sorted unit names (the last part of the unit path) whose unit file or ExecStart script is writable by others. Use others_write.
def others_write(mode):
return False
def weak_units(units):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go