Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Narrow sudo rules and sandboxed services

After this lesson: I can narrow a sudo rule and reduce what a service may reach.

60 min lesson13 commandsLinux administration lab: users, permissions, cron and systemd in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
sudoersthe rules that say who may run what with sudo
Drop-in filea small rules file in /etc/sudoers.d, read with the main one
Full patha program named from the root folder/usr/bin/systemctl
NOPASSWDa tag that lets a rule run without asking for a password
Exposure scoresystemd's estimate, 0 to 10, of how much a service can reach
Sandboxing settinga unit file line that takes something away from a serviceProtectHome=yes

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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?

Command
sudo -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make the help desk account

Command
sudo useradd -M -s /usr/sbin/nologin helpdesk

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write a narrow rule in your folder

Command
printf 'helpdesk ALL=(root) /usr/bin/systemctl restart cron\n' > helpdesk.sudoers

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check it before it goes live

Command
visudo -cf helpdesk.sudoers

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write a broken rule beside it

Command
printf 'helpdesk ALL=(root) /usr/bin/systemctl restart cron,\n' > broken.sudoers

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check the broken one

Command
visudo -cf broken.sudoers

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Install the good rule, owned by root

Command
sudo install -m 440 -o root -g root helpdesk.sudoers /etc/sudoers.d/60-helpdesk

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What may helpdesk run now?

Command
sudo -l -U helpdesk

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Code: python
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

Command
systemd-analyze security cron.service --no-pager | tail -1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write a small service in your folder

Command
printf '[Unit]\nDescription=Notes page\n\n[Service]\nExecStart=/usr/bin/python3 -m http.server 8090 --directory /srv/notes\n' > notes.service

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Score it before any sandboxing

Command
systemd-analyze security --offline=true --no-pager notes.service | tail -1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: five sandboxing settings

SettingWhat it takes away
NoNewPrivileges=yesGaining rights through a setuid program
ProtectSystem=strictWriting anywhere in the system folders
ProtectHome=yesSeeing anybody's home folder
PrivateTmp=yesSharing /tmp with other programs
DynamicUser=yesRunning as root: it gets a throwaway account

The cells students fill in are left blank in this preview.

Add the five settings

Command
printf 'NoNewPrivileges=yes\nProtectSystem=strict\nProtectHome=yes\nPrivateTmp=yes\nDynamicUser=yes\n' >> notes.service

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Score it again

Command
systemd-analyze security --offline=true --no-pager notes.service | tail -1

Shown 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: 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 *.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.