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
| Word | What it means | Example |
|---|---|---|
| Mandatory access control | rules the system enforces, which even root cannot ignore | |
| Profile | the list of files and actions one program may use | |
| Enforce mode | a profile that refuses what it does not allow | |
| Complain mode | a profile that allows everything but logs what it would refuse | |
| Denial | a log line saying AppArmor refused an action | apparmor="DENIED" |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsPermissions 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?
cat /sys/module/apparmor/parameters/enabledShown here, not run. Press Start lab to type it into your own lab’s terminal.
Install the AppArmor tools
sudo apt-get install -y -qq apparmor-utilsShown here, not run. Press Start lab to type it into your own lab’s terminal.
How many profiles are loaded?
sudo aa-status --profiledShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
sudo cp /usr/bin/cat /usr/local/bin/notecatShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the notes folder
sudo mkdir -p /srv/notesShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write one note
echo 'Lab at 10' | sudo tee /srv/notes/today.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the profile
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.notecatShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the profile line by line
| Line | What 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
sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.notecatShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the note
sudo notecat /srv/notes/today.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try a file outside the folder
sudo notecat /etc/hostnameShown here, not run. Press Start lab to type it into your own lab’s terminal.
Find the denial in the audit log
sudo grep DENIED /var/log/audit/audit.log | grep notecat | tail -1Shown 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 questionsA 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
sudo aa-complain /usr/local/bin/notecatShown here, not run. Press Start lab to type it into your own lab’s terminal.
The same read now works
sudo notecat /etc/hostnameShown here, not run. Press Start lab to type it into your own lab’s terminal.
Back to enforce mode
sudo aa-enforce /usr/local/bin/notecatShown here, not run. Press Start lab to type it into your own lab’s terminal.
Decide a request like a profile
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.
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