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

Free lesson

Confining a program with AppArmor

After this lesson: I can confine a program with an AppArmor profile and read what it refuses.

60 min lesson14 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

A notes reader that can read anything

AppArmor is a part of the Linux kernel that limits what one program may touch, whatever account runs it. Each program gets a profile: a list of the files it may use.

A college runs a small tool that shows notes from one folder. It runs as root. A bug lets a user ask it for /etc/shadow, and it obeys. A profile would have refused. Work only on your own lab copy.

By the end of this lesson

Note

I can confine a program with an AppArmor profile and read what it refuses.

Key words

WordWhat it meansExample
Mandatory access controlrules the system enforces, which even root cannot ignore
Profilethe list of files and actions one program may use
Enforce modea profile that refuses what it does not allow
Complain modea profile that allows everything but logs what it would refuse
Deniala log line saying AppArmor refused an actionapparmor="DENIED"

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

Permissions ask who, a profile asks which program

File modes ask which account wants a file. Root passes every such check. Mandatory access control adds a second question: which program is asking?

With AppArmor, a program with a profile may only use what the profile lists. That holds even when the program runs as root. Ubuntu ships with AppArmor on and profiles for some programs.

Is AppArmor on in this kernel?

Command
cat /sys/module/apparmor/parameters/enabled

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

Install the AppArmor tools

Command
sudo apt-get install -y -qq apparmor-utils

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

How many profiles are loaded?

Command
sudo aa-status --profiled

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 practice program and its notes

You will confine a copy of cat called notecat, so no real program changes. Its job is to show files in /srv/notes. Anything else it opens is a sign of a bug or an attack.

Make the practice program

Command
sudo cp /usr/bin/cat /usr/local/bin/notecat

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

Make the notes folder

Command
sudo mkdir -p /srv/notes

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

Write one note

Command
echo 'Lab at 10' | sudo tee /srv/notes/today.txt

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

Write the profile

Command
printf 'abi <abi/4.0>,\ninclude <tunables/global>\n\n/usr/local/bin/notecat {\n  include <abstractions/base>\n  /usr/local/bin/notecat mr,\n  /srv/notes/ r,\n  /srv/notes/** r,\n}\n' | sudo tee /etc/apparmor.d/usr.local.bin.notecat

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

Worked example: the profile line by line

LineWhat it means
/usr/local/bin/notecat {This profile is for the program at this path
include <abstractions/base>The libraries every program needs
/usr/local/bin/notecat mr,It may map and read its own file
/srv/notes/ r,It may list the folder
/srv/notes/** r,It may read everything below it

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

Load it in enforce mode

Command
sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.notecat

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

Read the note

Command
sudo notecat /srv/notes/today.txt

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

Try a file outside the folder

Command
sudo notecat /etc/hostname

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

Find the denial in the audit log

Command
sudo grep DENIED /var/log/audit/audit.log | grep notecat | tail -1

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

What did the terminal print?

Have a go

What did the terminal print?

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 complain mode protects a little. It refuses nothing: it only logs what enforce mode would refuse. Use it while you build a profile, read the log, add what is missing, then switch to enforce.

Switch to complain mode

Command
sudo aa-complain /usr/local/bin/notecat

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

The same read now works

Command
sudo notecat /etc/hostname

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

Back to enforce mode

Command
sudo aa-enforce /usr/local/bin/notecat

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

Decide a request like a profile

Code: python
rules = ["/srv/notes/** r", "/var/log/notecat.log w"]
path = "/srv/notes/today.txt"
want = "r"

allowed = False
for rule in rules:
    words = rule.split(" ")
    pattern = words[0]
    if pattern.endswith("/**") and path.startswith(pattern[:-2]) and want in words[1]:
        allowed = True
    if pattern == path and want in words[1]:
        allowed = True
print(path, want, "allowed" if allowed else "DENIED")
# Change path to "/etc/hostname" and press Run again.

Make a free account to save your work and track your progress.

Assignment: decide requests against a profile

A small profile is a list of rules such as "/srv/notes/** r": a path pattern, a space and the letters it allows (r read, w write). A rule that starts "deny " refuses instead. A pattern ending /** matches every path below that folder; one ending /* matches only names directly in it; any other pattern matches that exact path. Write matches(pattern, path). Then write decide(rules, path, want), which returns "allowed" when an allow rule matches with the letter want and no deny rule matches with it, and "denied" otherwise. Last, write denials(rules, requests), where each request is [path, want], returning the paths refused, in order.

Code: python
def matches(pattern, path):
    # /** is everything below, /* is one level, anything else is exact.
    return False


def decide(rules, path, want):
    # A deny rule wins over an allow rule.
    return "allowed"


def denials(rules, requests):
    return []

Make a free account to save your work and track your progress.

Going further: profiles you did not write

aa-genprof builds a profile by watching a program run and asking you about each access. It asks questions, so it is not a Run here. SELinux does the same job on Red Hat systems, with labels instead of paths.

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.