Restart one service, own the server
A sudo rule says which commands an account may run as root. A service runs a program for the whole machine. Both give power away, and both can give away too much.
A help desk team needs to restart one service. Somebody writes them a rule for every systemctl command. One of those commands opens an editor as root. Work only on your own lab copy.
By the end of this lesson
Note
I can narrow a sudo rule and reduce what a service may reach.
Key words
| Word | What it means | Example |
|---|---|---|
| sudoers | the rules that say who may run what with sudo | |
| Drop-in file | a small rules file in /etc/sudoers.d, read with the main one | |
| Full path | a program named from the root folder | /usr/bin/systemctl |
| NOPASSWD | a tag that lets a rule run without asking for a password | |
| Exposure score | systemd's estimate, 0 to 10, of how much a service can reach | |
| Sandboxing setting | a unit file line that takes something away from a service | ProtectHome=yes |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsA narrow rule names the whole command
A sudo rule has the account, the hosts, who it may act as and the commands. A narrow rule gives a full path and the exact arguments.
Rules belong in a drop-in file in /etc/sudoers.d, never pasted into /etc/sudoers. Check a file with visudo -cf before it goes live. A broken sudoers file can lock every admin out.
What may you run with sudo?
sudo -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the help desk account
sudo useradd -M -s /usr/sbin/nologin helpdeskShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a narrow rule in your folder
printf 'helpdesk ALL=(root) /usr/bin/systemctl restart cron\n' > helpdesk.sudoersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check it before it goes live
visudo -cf helpdesk.sudoersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a broken rule beside it
printf 'helpdesk ALL=(root) /usr/bin/systemctl restart cron,\n' > broken.sudoersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the broken one
visudo -cf broken.sudoersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Install the good rule, owned by root
sudo install -m 440 -o root -g root helpdesk.sudoers /etc/sudoers.d/60-helpdeskShown here, not run. Press Start lab to type it into your own lab’s terminal.
What may helpdesk run now?
sudo -l -U helpdeskShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a rule for one program is narrow. A rule for systemctl with any arguments includes systemctl edit, which opens an editor as root, and an editor can start a shell. Name the exact arguments, as the help desk rule does.
Read a rule like an auditor
rule = "helpdesk ALL=(root) NOPASSWD: /usr/bin/systemctl restart cron"
commands = rule.split(") ")[1]
if commands.startswith("NOPASSWD: "):
print("no password asked")
commands = commands[len("NOPASSWD: "):]
for command in commands.split(", "):
if command == "ALL":
print("RISK: every command")
elif not command.startswith("/"):
print("RISK: not a full path:", command)
else:
print("narrow:", command)
# Change the end of the rule to "ALL" and press Run again.
Make a free account to save your work and track your progress.
A service can be given less
A service often runs as root and can read the whole disk, though it needs a folder or two. systemd can take things away with sandboxing settings in the unit file.
systemd-analyze security gives each service an exposure score from 0 to 10, where higher means it can reach more. It is a guide, not a test, but it shows what is left open.
Score the cron service
systemd-analyze security cron.service --no-pager | tail -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a small service in your folder
printf '[Unit]\nDescription=Notes page\n\n[Service]\nExecStart=/usr/bin/python3 -m http.server 8090 --directory /srv/notes\n' > notes.serviceShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score it before any sandboxing
systemd-analyze security --offline=true --no-pager notes.service | tail -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: five sandboxing settings
| Setting | What it takes away |
|---|---|
| NoNewPrivileges=yes | Gaining rights through a setuid program |
| ProtectSystem=strict | Writing anywhere in the system folders |
| ProtectHome=yes | Seeing anybody's home folder |
| PrivateTmp=yes | Sharing /tmp with other programs |
| DynamicUser=yes | Running as root: it gets a throwaway account |
The cells students fill in are left blank in this preview.
Add the five settings
printf 'NoNewPrivileges=yes\nProtectSystem=strict\nProtectHome=yes\nPrivateTmp=yes\nDynamicUser=yes\n' >> notes.serviceShown here, not run. Press Start lab to type it into your own lab’s terminal.
Score it again
systemd-analyze security --offline=true --no-pager notes.service | tail -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
What did the scores show?
Have a go
What did the scores show?
3 questionsAssignment: audit a sudoers file
Each rule in a simple sudoers file is one line: who host=(runas) commands, where the commands may start with NOPASSWD: and are split by , . Blank lines and lines starting with # are ignored. Write commands_of(line), which returns the list of commands after the ) with any NOPASSWD: taken off. Then write problems(text). Number the lines from 1, counting every line, and return a list of [number, reason] for each rule, one entry per reason, in this order: "all commands" if a command is ALL, "no password for all" if that rule also has NOPASSWD, "not a full path" for each command not starting with / (other than ALL), and "wildcard" for each command containing *.
def commands_of(line):
# The commands come after ") ".
return []
def problems(text):
# Count every line, even the ones you skip.
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go