Profiles for general-purpose apps

broad rules, apps keep working for application developers for administrators for security researchers

Ordinary programs get short profiles built from abstractions with rules wider than the program needs. The program keeps working across versions and the profile still removes capabilities and raw sockets and writes outside the user's own files. Shown on the shipped curl profile.

On the map: general-purpose apps

▶ Watch this deck — 11 scenes, ~8 min

Worth seeing first

Part of journeys

01 A profile that keeps the app working

GENERAL-PURPOSE APPS $ curl -o ~/Downloads/report.pdf https://example.org/r.pdf curl ~/Downloads/report.pdf ~/work/notes/out.json ~/anything/you/name the user picks the file owner file rw @{HOME}/**, the shipped profile /etc/passwd (write) capability sys_admin wider than curl needs, narrower than no profile
curl writes three different files under the home directory through one profile rule, owner file rw @{HOME}/**. A write to /etc/passwd and the capability sys_admin are denied.

The shipped curl profile grants owner file rw @{HOME}/**, because the files are chosen on the command line. Broad rules keep the program working; what the profile leaves out is the protection.

You're writing a profile for curl, and here's the trouble: the user decides where the download goes. Today it's a PDF in Downloads, tomorrow it's a file in some work directory, and the config file can be anywhere too. You can't list those paths. So the profile that ships says: read and write anything this user owns under home. That's more than any single run needs. The bet is that curl keeps working, and that the profile still stops what curl has no business doing. This deck walks through that profile and the tools that produce profiles like it. It's for people who write profiles and people who deploy them.

Details and sources

Details

# allow reading other configuration files/certs from $HOME
# (see --config, --cacert options)
# allow writing output to $HOME, /tmp (see -o option)
owner file rw @{HOME}/**,
  • Size of the profile (curl): 23 non-comment lines, 6 include <abstractions/...> lines, no capability rule of its own.
  • The style in four marks: several include <abstractions/...> lines; owner rules under @{HOME} with **; network rules by family and type, without addresses; no capability rule.
  • owner bounds the breadth: the rule gives nothing on files of other users. See the scene "Your files".
  • /etc/passwd and sys_admin on the slide are examples of what has no rule. The scene "What the broad profile still blocks" goes through the list.
  • Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2), the kernel-rc2 (97d73c825). Nothing in the deck was run on a live kernel; kernel behaviour is read from the source.

02 curl next to ping: two styles in one tree

TWO SHIPPED PROFILES profiles/apparmor.d/curl profile curl /usr/bin/curl { include <abstractions/base> include <abstractions/nameservice> ... 4 more includes owner file rw @{HOME}/**, network inet stream, network inet6 dgram, ... } profiles/apparmor.d/bin.ping profile ping /{usr/,}bin/{,iputils-}ping { include <abstractions/base> ... capability net_raw, capability setuid, network inet raw, network inet6 raw, /etc/modules.conf r, ... } capabilities of its own 0 rules with ** 1 owner rules 3 capabilities of its own 2 rules with ** 0 owner rules 0 runs as you, touches your files starts with privileges, opens a raw socket privileged program: every rule is weighed. Ordinary program: breadth is the price of working.
The shipped curl profile next to the shipped ping profile. curl has six includes, one owner rule with a double star and no capability. ping has two capabilities, two raw socket rules and exact file paths.

curl: 6 abstractions, no capability rules of its own, owner file rw @{HOME}/**,. ping: capability net_raw, capability setuid, two raw socket rules and two exact file paths.

Open the profiles directory in the AppArmor tree and you find two styles side by side. curl has six includes, one big owner rule and sockets by family. ping has two capabilities, two raw sockets and two exact file names, and not one double star. Why the difference? curl runs as you. It can already do what you can do, so the profile's job is to keep a compromised curl inside your files. ping starts with privileges, so each line is a privilege somebody decided to hand over. This deck is the left side. The security-sensitive programs deck is the right side.

Details and sources

Details

  • The two files: curl:14-41 and bin.ping:15-27.
  • The tallies are grep counts over each file: capability lines, lines containing **, owner lines. curl's three owner lines are 27, 28 and 33.
  • ping's other rules, left off the slide: @{exec_path} mixr, and @{PROC}/sys/net/ipv6/conf/all/disable_ipv6 r, (bin.ping:25-27).
  • Both profiles include abstractions/base and abstractions/nameservice, so both get the rules shown in the scene "What the includes brought along".
  • The 198 profile files directly in profiles/apparmor.d/, by style (grep counts):
GroupFiles
flags=(unconfined): a name only, nothing confined76
allow all, (sbuild and 13 sbuild-*) and plasmashell15
all others107, of which 68 have a capability NAME, rule
  • A flags=(unconfined) profile is not a broad profile. Its file says so: "This profile allows everything and only exists to give the application a name instead of having the label "unconfined"" (firefox:1-2, profile line at firefox:7).
  • "Starts with privileges" is deliberate wording: how ping gets its privileges (setuid bit, file capabilities) differs between systems and is not in either tree.

03 Start from abstractions

SIX INCLUDE LINES include <abstractions/base> include <abstractions/consoles> include <abstractions/nameservice> include <abstractions/private-files-strict> include <abstractions/ssl_certs> include <abstractions/user-tmp> libc, locale, loader, logging the terminal users, hosts, DNS denies ~/.ssh, ~/.gnupg, ... CA certificates /tmp, /var/tmp $ apparmor_parser -p /etc/apparmor.d/curl | grep -cvE '^\s*(#|$)' 23 lines you read 397 lines the parser compiles next: what those extra lines contain
The six include lines of the curl profile, each with what it is for. Printed by the parser with includes expanded, the 23 lines of the profile become 397.

Each include <abstractions/NAME> pulls in a shared block of rules. apparmor_parser -p prints the profile with every include expanded: curl's 23 lines become 397.

You don't list the C library's files yourself. You include base, which names libraries by pattern, so an upgrade doesn't break your profile. You don't work out what a name lookup touches. You include nameservice. One line for each thing the program uses: the terminal, certificates, temporary files. That's why the curl profile fits on one screen. But the parser pastes every included file in. Ask it to print the result with -p, count the lines that aren't comments, and twenty-three lines of your own have become about four hundred. That's the trade in numbers: a short text you read, and a much longer one that gets compiled.

Details and sources

Details

  • The six lines: curl:15-22.
  • Why the profile survives upgrades: base names shared libraries by pattern, for example /{usr/,}lib/@{multiarch}/**.so* mr, (base:74-77), so a new library version or file name is still covered. A profile that listed exact library files would need an edit after such an upgrade.
  • apparmor_parser -p (--preprocess) prints the profile with every include pasted in. Each pasted file is announced by a ##included <...> line (parser_lex.l:176); a repeated include is replaced by ##skipped duplicate include <...> (parser_lex.l:223).
  • The numbers 23 and 397 are non-comment lines. They come from running the parser built from the AppArmor tree at 836052d21 with -I profiles/apparmor.d -p on profiles/apparmor.d/curl. The pipeline on the slide is a reading aid for an installed system; that exact command line was not run.
  • The variables used by abstractions (@{HOME}, @{PROC}, @{run}) come from include <tunables/global> in the profile's preamble (curl:12).
  • A profile created by aa-genprof or aa-autodep always starts with abstractions/base (aa.py:391-392).
  • Other shipped profiles of this style: mbsync has four includes, five network rules and 11 owner rules (mbsync:17-40); znc has base, nameservice, ssl_certs, perl, python (znc:6-37).

04 What the includes brought along

EXPANDED WITH -p <abstractions/base> <abstractions/consoles> <abstractions/nameservice> <abstractions/private-files-strict> <abstractions/ssl_certs> <abstractions/user-tmp> rules curl's own text does not show capability net_bind_service, nameservice -> nis network netlink raw, nameservice network inet stream, (and 3 more) nameservice ptrace (readby), base signal (receive) peer=unconfined, base unix (create), base owner @{HOME}/.Private/** mr ix wlk, base documented contents, not defects a takeover gets the expanded list, not the file you opened $ apparmor_parser -p PROFILE | grep -E 'capability|network|ptrace|signal|mount'
Rules that the curl profile gets from its includes and that its own text does not show: capability net_bind_service from nis through nameservice, network netlink raw from nameservice, ptrace, signal and unix rules and an eCryptfs rule with ix from base.

After expansion curl has capability net_bind_service, (from abstractions/nis), network netlink raw, (from nameservice) and ptrace, signal and unix rules (from base). Check with apparmor_parser -p.

The curl profile has no capability line. The profile the parser compiles has one: net_bind_service. It arrives through nameservice, which includes the NIS abstraction. Nameservice also brings a netlink raw socket. Base brings ptrace, signal and unix socket rules, and a rule for an encrypted home directory that includes execute. None of that is a defect. It's what these abstractions are for. But if you're judging what a takeover of curl is worth, this is the list that counts, not the file you opened. So print the expanded profile, and grep for the rule classes you care about before you say what a profile grants.

Details and sources

Details

  • All rules on the slide were found in the output of the parser built from the AppArmor tree at 836052d21, run with -p on profiles/apparmor.d/curl.
  • capability net_bind_service,: nameservice includes nis (nameservice:62), and nis has the rule with the comment "portmapper may ask root processes to do nis/ldap at low ports" (nis:15-16).
  • network inet stream, network inet6 stream, network inet dgram, network inet6 dgram, and network netlink raw, ("Netlink raw needed for nscd"): nameservice:92-99.
  • From base (base:134-163): ptrace (readby), ptrace (tracedby), ptrace (read) peer=@{profile_name}, signal (receive) peer=unconfined, signal peer=@{profile_name}, signal (receive, send) set=("exists"), unix peer=(label=@{profile_name}), unix (receive) peer=(label=unconfined), unix (create), unix (getattr, getopt, setopt, shutdown),.
  • The eCryptfs rules, with ix: owner @{HOME}/.Private/** mrixwlk, and owner @{HOMEDIRS}/.ecryptfs/*/.Private/** mrixwlk, (base:176-180).
  • The precise profile has them too: ping includes nameservice (bin.ping:18) and its expanded text contains the same rules.
  • The narrower choice is nameservice-strict: 30 non-comment lines after expansion against 142 for nameservice.

for security researchers

Attack surface

  • Review the output of apparmor_parser -p, not the profile file. The six include lines of curl stand for about 370 lines the author of the profile did not write.
  • Rules that arrive in every profile with base and matter for lateral movement: ptrace (readby), and ptrace (tracedby), carry no peer condition, so any task whose own policy lets it read or trace may do so to this one; signal (receive) peer=unconfined, and unix (receive) peer=(label=unconfined), open the profile to unconfined peers.
  • owner @{HOME}/.Private/** mrixwlk, gives ix on files the user owns under ~/.Private/: a place where a confined program that can write may also execute, inheriting the same profile.
  • nameservice reaches further than name lookup: through kerberosclient and ldapclient it grants read access to /etc/krb5.keytab and /etc/ldap.secret (kerberosclient:30, ldapclient:15). The files' own permissions still apply.
  • Abstractions are extended on the installed system by files in abstractions/NAME.d/ (for example include if exists <abstractions/base.d>), so the expanded text of a machine can differ from the tree.

05 Your files: owner, @{HOME} and user-tmp

THREE WIDTHS, ONE GUARD owner file rw @{HOME}/**, curl: everything you own under home owner @{HOME}/[^.]**.@{qpdf_ext} rw, qpdf: one file type, no dot directories owner @{HOME}/.znc/moddata/** rw, znc: its own directory curl (fsuid 1000) /home/ann/out.txt owner 1000 /home/ann/shared/log owner 1001 owner matches: rule applies other owner: rule does not apply include <abstractions/user-tmp> = owner /tmp/** rwkl, /tmp/ rw, (same for /var/tmp) narrower alternative: owner /tmp/tr_session_id_* rwk, (transmission) owner test: from the kernel code
Three owner rules of different width from the curl, qpdf and znc profiles. An owner rule applies to a file owned by the user the process runs as and not to a file with another owner. The user-tmp abstraction grants owner rules for /tmp and /var/tmp.

owner limits a rule to files whose owner is the process's fsuid. abstractions/user-tmp grants owner /tmp/** rwkl, and owner /var/tmp/** rwkl,.

The shipped profiles grant user data at three widths. curl takes the whole home directory. qpdf takes one kind of file anywhere in home, and skips dot directories. znc takes its own directory. Pick the narrowest your program can live with. All three start with owner. In the kernel, an owner rule only counts when the process's user is the file's owner, so even curl's rule gives nothing on a file somebody else owns. For temporary files there's user-tmp: the same idea for the temp directories. Notice it includes the link letter. The hard links deck comes back to that.

Details and sources

Details

  • The three widths: owner file rw @{HOME}/**, (curl:33); owner @{HOME}/[^.]**.@{qpdf_ext} rw, with @{qpdf_ext} = [qQpP][dD][fF] [jJ][sS][oO][nN] (qpdf:6, qpdf:17); owner @{HOME}/.znc/moddata/** rw, (znc:32).
  • @{HOME} is defined by tunables/global through tunables/home: @{HOME}=@{HOMEDIRS}/*/ /root/ with @{HOMEDIRS}=/home/ (home:15-21). /home/ann on the slide is an illustration.
  • The owner test in the kernel, read from the code: an owner rule is used when the uid the task accesses files with (its fsuid) equals the file's owner; otherwise the rules without owner decide. The log fields fsuid= and ouid= are the two values compared.
  • abstractions/user-tmp in full (user-tmp:15-22):
owner @{HOME}/tmp/**  rwkl,
owner @{HOME}/tmp/    rw,
owner /var/tmp/**     rwkl,
/var/tmp/             rw,
owner /tmp/**         rwkl,
/tmp/                 rw,
  • The narrower alternative is one named pattern: owner /tmp/tr_session_id_* rwk, (transmission:31).
  • The widest rules in the app profiles are m on files the user can write, which allows mapping them executable: owner file mrw @{HOME}/orcexec.*, (rygel:62-64, generated code) and owner @{HOME}/.znc/modules/* mrw, (znc:30, loadable modules). They are deliberate.

for AppArmor developers

Code

  • The owner test: the state's accept entry carries an owner flag; if uid_eq(subj_uid, cond->uid) the first permission set is used, else the next one (file.c:202-205).

06 What the broad profile still blocks

A COMPROMISED CURL TRIES profile curl curl (taken over) write /etc/cron.d/x no write rule for /etc socket(AF_INET, SOCK_RAW) no network inet raw, exec /bin/sh no x rule: curl's own line is @{exec_path} mr, capability dac_override (even as root) only net_bind_service is listed read ~/.ssh/id_ed25519 audit deny from private-files-strict beats owner rw @{HOME}/** apparmor="DENIED" operation="capable" profile="curl" pid=7292 comm="curl" capability=1 capname="dac_override"
A compromised curl under the shipped profile tries five things and each is denied: writing /etc/cron.d, a raw socket, executing /bin/sh, the capability dac_override and reading an SSH key. A log record for the denied capability is shown.

The expanded curl profile has no write rule for system directories (its write rules outside owner paths name devices, service sockets, the snapd socket and the temp directories themselves), no raw socket rule, no exec rule of its own, one capability, and audit deny @{HOME}/.ssh/{,**} mrwkl, from private-files-strict. A missing rule is a denial. The outcomes are read from the kernel code, not run.

Say an attacker takes over curl through a bad download. What can they do with it? Write a cron job: no, there's no write rule for system directories. Open a raw socket to sniff the network: no raw rule. Start a shell: no execute rule for it. If root ran curl, use root's power to walk past file permissions: no, the profile lists one capability and that isn't it. Read your SSH key, which you do own: no, private-files-strict denies it, and deny wins over the big owner rule. What's left is your own files that aren't on that list. That's real damage, and far less than everything.

Details and sources

Details

  • The rule lists are from the expanded curl profile (parser built from the AppArmor tree at 836052d21, -p). The outcomes are what the kernel code does; none was run on a kernel.
  • Write to /etc/cron.d/x: the write rules are owner file rw @{HOME}/**, (curl:33), abstractions/user-tmp and file rw @{run}/snapd.socket, (curl:45). Nothing covers /etc, /usr, /var/lib or /boot. The open fails with EACCES.
  • Raw socket: the network rules are unix stream, unix dgram, inet stream, inet dgram, inet6 stream, inet6 dgram (curl:36-41) plus netlink raw from nameservice. There is no network inet raw, and no network packet,.
  • Exec: curl's own line is @{exec_path} mr, (curl:24), without x. The expanded profile has exec permission in three places only: @{run}/nscd/db* mix, (from nameservice) and the two eCryptfs rules of base. Any other exec fails with EACCES.
  • Capability: the only capability rule is capability net_bind_service,. Every other capability is refused with EPERM, also for a process started by root.
  • SSH key: audit deny @{HOME}/.ssh/{,**} mrwkl, (private-files-strict:12). A deny rule wins over an allow rule of the same priority, and audit puts the refusal in the log.
  • curl re-allows its own configuration with priority=1 owner file r @{HOME}/.curlrc, and priority=1 owner file r @{HOME}/.config/curlrc, (curl:27-28). private-files has deny @{HOME}/.*rc mrk,, which matches the name .curlrc; which deny each of the two lines was written against was not traced.
  • The log record is assembled on the field layout of testcase_dmesg_capability.in (operation="capable" ... capability=23 capname="sys_nice", two spaces before capname) with illustrative values (testcase_dmesg_capability.in). dac_override is capability 1 (capability.h:121).
  • Also without any rule: mount, umount, pivot_root, userns, change_profile, and ptrace or signals towards other profiles.
  • A missing rule denies only what the kernel mediates. Which classes are mediated depends on the feature set the profile was compiled for (abi <abi/5.0>,) and on the kernel: mqueue has no hook in the kernel, so no profile restricts it there.
  • The profile does not stop curl from reading or changing the user's own files in the granted area.

for AppArmor developers

Code

  • Capabilities: apparmor_capable calls aa_capable for a confined task (lsm.c:199-211); profile_capable returns -EPERM unless the capability is in the allow set (capability.c:128-154).
  • Sockets: aa_profile_af_perm matches family, type and protocol against the profile's rules (net.c:254-270).
  • Exec without a matching rule: error = -EACCES (domain.c:740-770).
  • The parser adds a "mediates" entry per supported class, so that a class without rules is denied instead of unmediated; it skips this for unconfined and default_allow profiles (parser_regex.c:1385-1450).

for security researchers

Attack surface

  • The five refusals are properties of names and rule classes. The protection stands on the path the kernel computes for the object; see Hard links and name-based rules and What is not mediated for where that breaks.
  • "Even as root" covers capabilities only. Root's ownership still counts: a root-run curl satisfies owner on root's files under /root/ (@{HOME} includes /root/).
  • file rw @{run}/snapd.socket, (curl:43-45) lets the profile talk to the snapd socket; the comment says it is kept "until we can revisit it with delegation or profile refactoring". What snapd grants to that peer is outside both trees.
  • The scene "What a hijacked curl keeps" lists what is left inside the granted area.

07 When broad is acceptable

THREE QUESTIONS holds capabilities? writes beyond the user's files? starts programs unconfined? curl mbsync qpdf znc no no no broad profile tnftp runs your commands priority=1 file Cx @{tnftp_rx}/* -> cmds, profile cmds { deny network, ... } kept inside by a child profile tar capability dac_override + file rwl /**, needs a precise profile -> prac-privileged
Three questions as gates: does the program hold capabilities, does it write beyond the files of the user, does it start programs unconfined. curl, mbsync, qpdf and znc pass all three. tnftp keeps its commands in a child profile. tar holds dac_override next to a rule for the whole filesystem and needs a precise profile.

Broad fits when the program has no capabilities, writes only owner paths and keeps its children confined. tar (capability dac_override, with file rwl /**,) shows what happens otherwise.

How do you know broad is good enough? Look at what the shipped broad profiles have in common. They hold no capabilities of their own. They write only files the user owns. And when they start another program, it stays confined. curl, mbsync, qpdf and znc pass all three. tnftp runs commands you type, so it sends them into a child profile that can't use the network. tar is the warning: four capabilities, including dac_override, next to a rule for the whole filesystem. That combination is what the precise style exists to avoid.

Details and sources

Details

  • The three properties are what the shipped broad profiles have in common. They are derived from the profiles; no document in the tree states them as a rule.
  • No capabilities of their own: curl, mbsync, qpdf, znc, john, tnftp, rygel and the four transmission profiles have no capability line in their files.
  • Writes through owner rules only: under @{HOME}, /tmp, /var/tmp, @{run}/user/*, or for the daemon variant under its own state directory: owner /var/lib/transmission{-daemon,}/downloads/** rw, (transmission:39).
  • Exec stays inside: no exec rule (curl, mbsync, qpdf), ix of an interpreter (/{usr/,}bin/python{3,3.[0-9],3.[1-9][0-9]} rix,, znc:41) or a child profile (file Cx /usr/bin/dash,, tnftp:34; file Cx /usr/libexec/rygel/mx-extract -> mx-extract,, rygel:37). None uses ux, Ux or pux.
  • tnftp: priority=1 file Cx @{tnftp_rx}/* -> cmds, (tnftp:66); the cmds child has deny network, (tnftp:77) and audit deny file w @{tnftp_rx}/*, (tnftp:80).
  • tar: capability chown, capability fowner, capability dac_override, capability dac_read_search, and file rwl /**, (tar:18-25). usr.sbin.apache2 has the same combination.
  • A tolerated exception in the extras profiles, which their README calls "not as mature": the firefox profile there uses Uxr for three helpers and says "Ideally these would use a child profile. They are all ELF executables so running with 'Ux', while not ideal, is ok because we will at least benefit from glibc's secure execute" (firefox:480-485).
  • A profile with flags=(unconfined) is not a broad profile: it gives the program a profile name and confines nothing. 76 of the 198 files in profiles/apparmor.d carry the flag (grep).
  • aa-enforce and aa-complain skip a profile with flags=(unconfined) unless --force is given (tools.py:198-206). aa-enforce --force on one of the 76 stubs would leave a profile whose only rules are userns, and @{exec_path} mr, (firefox:7-11).

08 The profiling loop: complain, run, scan, enforce

aa-genprof $ sudo aa-genprof /usr/bin/curl /usr/bin/curl flags=(complain) { include <abstractions/base> /usr/bin/curl mr, } created and loaded curl run it in another window apparmor="ALLOWED" ... allowed and logged (S)can system log for AppArmor events one question per new record rule added (F)inish -> enforce mode, reloaded the tool can only ask about what the log contains: exercise every feature
The aa-genprof loop: it creates a small profile in complain mode and loads it, you run curl in another window, the kernel allows and logs what is missing, Scan turns each new record into a question and a rule, and Finish switches the profile to enforce mode.

aa-genprof PROGRAM creates the profile with flags=(complain) and abstractions/base, then loops: run the program, (S)can, answer. (F)inish sets enforce mode. Missing permissions in complain mode are logged as apparmor="ALLOWED" (read from the kernel code).

You rarely write a profile from a blank page. For a program without a profile, aa-genprof makes a tiny one, base plus the binary, puts it in complain mode and loads it. In complain mode the kernel lets a missing permission through and logs it as allowed. So you run curl in another window and really use it. Press S, and the tool reads the new log records and asks one question for each. Answer, run more, scan again. When the questions stop, press F and the profile goes to enforce. One catch: a feature you didn't exercise left no record, so it gets no rule.

Details and sources

Details

  • aa-genprof [-d DIR] [-f LOGFILE] [-j] [--no-abstraction] [-o OUTDIR] program (aa-genprof:67-74). Related: aa-autodep [--force] [-d DIR] [--no-reload] program..., aa-logprof [-d DIR] [-f LOGFILE] [-m MARK] [-j] [-a] [--no-abstraction] [-o OUTDIR], aa-complain, aa-enforce.
  • What it does, in order (aa-genprof:122-192): create the profile if no profile file for the program exists; set it to complain mode and reload it; set /proc/sys/kernel/printk_ratelimit to 0 so that records are not dropped, restoring it at exit; print "Please start the application to be profiled in another window and exercise its functionality now."; offer (S)can system log for AppArmor events and (F)inish. On Finish every profile it put into complain mode is set to enforce and reloaded: "Reloaded AppArmor profiles in enforce mode."
  • The slide shows a program that has no profile yet. Where a profile file already exists, as it does for curl on a system that installs the shipped profile, aa-genprof uses that file and puts it in complain mode.
  • The new profile: flags=(complain), include <abstractions/base>, and for a binary PATH mr, (aa.py:386-410); a script with a #! line gets r on the script, ix on the interpreter and the interpreter's abstraction. The file also gets abi <abi/5.0>, and include <tunables/global> when those files exist (aa.py:516-519); they are left off the slide.
  • The profile is named after the full path, which is also its attachment, so the head reads /usr/bin/curl flags=(complain) { (read from the tool's code, not from a run).
  • Log file: -f, or the first existing of /var/log/audit/audit.log /var/log/syslog /var/log/messages (logprof.conf:15).
  • Complain mode in the kernel, read from the code: a permission that no rule allows and no rule denies is let through and logged as apparmor="ALLOWED". An exec with no matching rule runs under a learning profile named in target= with //null- in its name.
  • Complain mode is not the same as unconfined: plain deny rules still refuse, and silently. A complain-mode profile that includes private-files-strict still blocks ~/.ssh.
  • A running program keeps the confinement it started with. Start it after the profile is loaded; that is why the tool asks for "another window".
  • Errors: "It seems AppArmor was not started. Please enable AppArmor and try again." (aa-genprof:90-92). Programs listed under [qualifiers] in logprof.conf (shells, core utilities) are refused a profile of their own (aa.py:2501-2510).
  • The same loop by hand, from the extras README: copy the profile, aa-complain, restart the service, use it, aa-logprof, answer, aa-enforce (README:11-34).

for AppArmor developers

Code

09 One log record, one question, and how profiles get broad

aa-logprof ASKS apparmor="ALLOWED" operation="open" profile="/usr/bin/curl" name="/etc/ssl/certs/ca-certificates.crt" ... requested_mask="r" denied_mask="r" fsuid=1000 ouid=0 Profile: /usr/bin/curl Path: /etc/ssl/certs/ca-certificates.crt New Mode: r Severity: ... [1 - include <abstractions/ssl_certs>] 2 - include <abstractions/ssl_keys> 3 - /etc/ssl/certs/ca-certificates.crt r, (A)llow / [(D)eny] / (I)gnore / (G)lob / Glob with (E)xtension / (N)ew / Audi(t) / Abo(r)t / (F)inish /etc/ssl/** r, private keys too nothing ranks the offers by breadth (G)lob, pressed again /usr/share/foo/a.txt /usr/share/foo/* /usr/share/** /usr/** /** exact path = precise. include or Glob = broad. You choose per question.
A log record of curl reading /etc/ssl/certs/ca-certificates.crt and the aa-logprof question for it: two include options, ssl_certs and ssl_keys, then the exact path, and the row of keys. Pressing Glob repeatedly widens a path step by step to /**.

aa-logprof offers include <abstractions/...> lines whose rules cover the event, then the exact path and globs. Keys: (A)llow, (D)eny, (I)gnore, (G)lob, Glob with (E)xtension, (N)ew, Audi(t). Glob widens one step per press, ending at /**. The menu is rendered from the tool's code, not captured from a session.

Here's one record: curl opened the system's certificate bundle. aa-logprof turns it into a question. First it offers every abstraction that has a rule covering this path, then the exact path. Two abstractions qualify here: ssl_certs, which is what a client needs, and ssl_keys, which reads everything in that directory tree, private keys included. The tool doesn't rank them. Then there's Glob: each press widens the path one step, until it covers the whole filesystem. So a broad profile isn't an accident of the tool. It's the sum of your answers. For curl, an include is usually the right answer. Just read what you're including.

Details and sources

Details

  • The record is assembled on the field layout of an operation="open" record in syslog_datetime_01.in (syslog_datetime_01.in), with illustrative values; pid= and comm= are behind the .... The profile name is the one aa-genprof gave the new profile in the previous scene.
  • The menu is rendered from the tool's code and was not captured from a session; the bracket on (D)eny follows the sessions recorded for aa-logprof. Headers Profile, Path, New Mode, Severity (aa.py:1167-1173, file.py:353-399); options are printed as N - text with the selected one in brackets (ui.py:500-507). The severity number comes from utils/severity.db, is left out on the slide and blocks nothing.
  • Order of the options: include <abstractions/NAME> for every abstraction with a rule that covers the event, sorted by name (aa.py:1152-1156); then, for a file, paths of existing rules that match, globs from the [globs] table of logprof.conf, and the exact path last (aa.py:2378-2408). The first option is the preselected one.
  • Why these two: ssl_certs has /etc/{,libre}ssl/certs/{,**} r, (ssl_certs:17) and ssl_keys has /etc/ssl/** r, under the comment "Just include the whole /etc/ssl directory if we should have access to private keys too" (ssl_keys:16-19). Nothing ranks proposals by breadth. That these are the only two for this path is read from the abstraction files, not from a run.
  • An event already covered by the profile or by one of its includes, followed recursively, produces no question (aa.py:2341-2351). base already covers /etc/ssl/openssl.cnf through crypto and openssl.
  • Keys (aa.py:1331-1361): (A)llow, (D)eny, (I)gnore, (G)lob, Glob with (E)xtension, (N)ew (edit the path), Audi(t), Abo(r)t, (F)inish. (O)wner permissions off appears only when the record's fsuid equals its ouid and the proposed rule therefore starts with owner.
  • The bracketed default is (D)eny, as the slide shows, also for a record logged in complain mode (ALLOWED): Return writes a deny rule, so press A to allow. Known discrepancy, "the default answer of every rule question is Deny": the code means Allow to be the default for complain-mode records and Deny for enforce-mode records, but the test that should select Allow never matches (aa.py:1177-1182). Observed in the recorded sessions of aa-logprof: every rule question under Complain-mode changes: showed (A)llow / [(D)eny].
  • Glob replaces the last path component with *; when the path already ends in /* or /** it removes one more directory and ends in /** (aare.py:97-123).
  • --no-abstraction on either tool suppresses the include offers (aa-genprof:72, aa-logprof:32); base is still added to a new profile.
  • aa-logprof -a accepts every rule and answers exec questions with px when a profile for the target exists, otherwise ix (ui.py:518-526).
  • Exec records get their own menu: inherit, profile, child, named, unconfined, deny (aa.py:622-661). Choosing unconfined prints "Launching processes in an unconfined state is a very dangerous operation and can cause serious security holes." (aa.py:890-895).
  • Known discrepancy: the tool's matcher does not expand variables (aare.py:49), so a rule written with @{etc_ro} or @{HOME} may not be recognised as covering a logged path, and the question is asked again. A full session was not run. See Choosing abstractions.

for security researchers

Attack surface

  • The loop learns from whatever the program did while it was in complain mode. Activity triggered by hostile input during that window is logged like any other and is offered as a rule; with aa-logprof -a it is accepted without a question.
  • The preselected option is the first abstraction in name order, and one keypress accepts it. ssl_keys on a client is the example on the slide.

10 What a hijacked curl keeps for security researchers

WHAT THE ATTACKER KEEPS curl (taken over) denied by name: ~/.ssh/ ~/.gnupg/ ~/.config/autostart/ read ~/.git-credentials ~/.kube/config not on the deny list: owner file rw @{HOME}/** applies write ~/.config/systemd/user/x.service no deny matches; where the session reads it, it runs later connect to any host, TCP or UDP network inet stream, has no address condition also granted: file rw @{run}/snapd.socket, (the snapd socket) in a broad profile the deny list is the wall derived from the profile text and the kernel code, not run
What a compromised curl can still do under the shipped profile: read files in the home directory that the deny list does not name, write a systemd user unit, and connect to any host; it also keeps read and write access to the snapd socket. The SSH and GnuPG directories and the autostart directory are denied.

owner file rw @{HOME}/**, minus the deny rules of private-files-strict is what a compromised curl can read and write; network inet stream, has no address condition. This is derived from the profile text and the kernel code; it was not run.

What does a hijacked curl keep? The secrets list in private-files-strict is a list of names. Your SSH and GPG directories are on it. A Git credentials file or a Kubernetes config isn't, so the big owner rule hands them over. Writing works the same way. The autostart directory is denied, but nothing denies a new unit file in the user's systemd directory, and on a system where your session reads that directory, the code runs later, outside this profile. And the network rules name no address, so whatever was read can be sent anywhere. In a broad profile the deny list is the wall. Read it.

Details and sources

Details

  • Everything on this slide is derived from the text of the expanded curl profile and the kernel code. None of it was run.
  • Read and write: owner file rw @{HOME}/**, (curl:33) covers every file the user owns under the home directory, less the deny rules below.
  • The deny list is two files. private-files (private-files:9-49) denies shell history and editor backup files, reading and writing ~/.*rc, ~/.bash*, ~/.profile* and login files, and writing under ~/bin/, ~/.config/autostart/, ~/.config/upstart/, ~/.init/, the KDE autostart and env directories and ~/.local/share/thumbnailers/. private-files-strict (private-files-strict:10-27) adds ~/.aws, ~/.gnupg, ~/.ssh, GNOME keyrings, ~/.mozilla, Chromium, Evolution, Thunderbird, KMail and KWallet data.
  • Not named by either file, so granted: ~/.git-credentials, ~/.kube/config, documents and source trees. These names are examples chosen for the slide; the claim is only that no deny rule in the two files matches them.
  • audit deny @{HOME}/.config/ w, is a rule for the directory itself. It does not match ~/.config/systemd/user/x.service, and no other deny does. Whether a file there is ever run depends on the system: on a system where the user's systemd instance reads that directory, the unit runs as a new process that the curl profile does not confine, unless a profile attaches to it.
  • Network: network inet stream, network inet dgram, and the inet6 pair (curl:36-41) have no address or port condition.
  • file rw @{run}/snapd.socket, (curl:43-45) is the one grant in the profile that is neither the user's own files nor a network family: the profile may open the snapd socket for reading and writing. The comment above the rule says it stays "until we can revisit it with delegation or profile refactoring". What snapd lets that peer do is outside both trees.
  • This scene is the other half of "What the broad profile still blocks".

11 Where to go from here

ROADS general-purpose apps includes + owner rules the loop + check with -p prac-abstractions what each include grants prac-privileged the precise style syn-owner bounding a home rule syn-globbing one star, two stars ut-genprof ut-logprof the two tools adm-denials reading the records
Profiles for general-purpose apps in the centre with roads to prac-abstractions, prac-privileged, syn-owner, syn-globbing, ut-genprof with ut-logprof, and adm-denials.

Next: prac-abstractions, prac-privileged, syn-owner, syn-globbing, ut-genprof, ut-logprof, adm-denials.

So: start from abstractions, bound your own rules with owner, let the profiling loop find the rest, and check the expanded result. If you want to know what each include line really grants, go to the choosing abstractions deck. If your program holds privileges, the security-sensitive programs deck shows the opposite style, on ping and dnsmasq. And owner, globbing, the two tools and reading denials each have a deck of their own.

Details and sources

Details

Read more

Basis of the statements on this page: "tree" means the parser built from the AppArmor commit above was run with -p on a shipped profile, with nothing loaded into a kernel; "code" means the source of the tools or of mainline Linux 7.4 was read, not run; "derived" means the result was reasoned from that code and from the profile text. Counts are grep counts over the tree. Nothing here was observed on a running kernel.

The curl profile, annotated

profiles/apparmor.d/curl, 23 non-comment lines.

LineTextRemark
10abi <abi/5.0>,the feature set the profile is compiled for
12include <tunables/global>defines @{HOME}, @{PROC}, @{run}
14profile curl /usr/bin/curl {name and attachment
15include <abstractions/base>libc, locale, loader, logging; also ptrace, signal and unix rules
16include <abstractions/consoles>/dev/tty, /dev/pts/*, /dev/console
17include <abstractions/nameservice>users, hosts, DNS; also sockets and one capability
18include <abstractions/private-files-strict>deny rules for key stores and start-up files
19include <abstractions/ssl_certs>CA certificates, read-only
22include <abstractions/user-tmp>owner rules for /tmp, /var/tmp, ~/tmp
24@{exec_path} mr,map and read the binary; no x
27-28priority=1 owner file r @{HOME}/.curlrc, and .config/curlrccurl's own configuration, above the deny rules
33owner file rw @{HOME}/**,the broad rule: --config, --cacert, -o
36-41network unix stream, unix dgram, inet stream, inet dgram, inet6 stream, inet6 dgram,by family and type, no addresses
45file rw @{run}/snapd.socket,the snap socket
48/etc/rpmdevtools/curlrc r,rpmdev-spectool's curlrc
51include if exists <local/curl>site additions

Rules of other classes after expansion (tree):

RuleComes fromFile and line
capability net_bind_service,nameservice includes nisabstractions/nis:16 (include at abstractions/nameservice:62)
network netlink raw,nameserviceabstractions/nameservice:99
network inet stream, and three morenameservice (and curl itself)abstractions/nameservice:92-95
ptrace (readby), ptrace (tracedby), ptrace (read) peer=@{profile_name},baseabstractions/base:134-142
signal (receive) peer=unconfined, signal peer=@{profile_name}, signal (receive, send) set=("exists"),baseabstractions/base:145-151
unix peer=(label=@{profile_name}), unix (receive) peer=(label=unconfined), unix (create), unix (getattr, getopt, setopt, shutdown),baseabstractions/base:154-163
owner @{HOME}/.Private/** mrixwlk, owner @{HOMEDIRS}/.ecryptfs/*/.Private/** mrixwlk,baseabstractions/base:177,180
@{run}/nscd/db* mix,nameserviceabstractions/nameservice:40
  • 23 non-comment lines become 397 after expansion (tree).
  • nameservice alone expands to 142 non-comment lines, nameservice-strict to 30 (tree).

Shipped profiles by style

The 198 regular files directly in profiles/apparmor.d/ (not abstractions/, tunables/, abi/, local/, apache2.d/).

GroupFilesEvidence
Name only, nothing confined76 with flags=(unconfined) (1password, brave, chrome, chromium, code, firefox, flatpak, steam, thunderbird, ...)body is userns, and @{exec_path} mr,
Everything allowed by rulesbuild and 13 sbuild-*; plasmashellallow all,; bare capability, network, mount, ...
Broad, abstraction-based, no capability rules of their owncurl, tnftp, mbsync, qpdf, znc, john, gs, rygel, transmission, lsb_release3 to 11 abstractions per profile, owner @{HOME}/... rules, recursive globs
Precise, privilegedbin.ping, usr.sbin.traceroute, sbin.klogd, unix-chkpwd, who, iotop-c, nvidia_modprobe, usr.sbin.ntpd, usr.sbin.dnsmasq, the dovecot files, ...1 to 12 named capabilities, exact paths, Px/Cx, peer conditions
Privileged and broadtar, usr.sbin.apache2, zgrep's helper childcapabilities next to a whole-filesystem glob

Numbers per file (non-comment lines; include <abstractions/ lines; capability lines; lines with **; owner lines):

FileLinesAbstractionsCapabilities**owner
curl236013
mbsync2740911
qpdf25101612
znc2750413
tnftp (4 profiles)4916025
transmission (4 profiles)7023069
bin.ping153200
usr.sbin.dnsmasq884715
  • profiles/apparmor/profiles/extras/ holds 111 more profile files. Its README says they "are not turned on by default because they are not as mature as the profiles in /etc/apparmor.d/".

What a broad profile leaves out

The expanded curl profile (tree) and the result in the kernel (code, derived).

Rule classIn the expanded curl profileRequest without a rule
capabilitynet_bind_service onlyrefused with EPERM
file writeowner rules under @{HOME}, /tmp, /var/tmp; @{run}/snapd.socketrefused with EACCES
networkunix and inet/inet6 stream and dgram; netlink rawsocket creation refused
exec@{run}/nscd/db* mix, and the two eCryptfs ix rulesrefused with EACCES
ptracebe read or traced by others; read itselfrefused
signalreceive from unconfined; itself; the exists checkrefused
mount, umount, pivot_root, userns, change_profilenonerefused
named secretsaudit deny on ~/.ssh, ~/.gnupg, ~/.aws and othersrefused and logged
  • A class is denied by a missing rule only if the kernel mediates it and the profile was compiled for a feature set that has it. mqueue has no hook in the mainline kernel.
  • A broad profile does not protect the user's own files in the granted area from the program.

Widths of home directory rules

WidthShipped exampleFile
The whole home directoryowner file rw @{HOME}/**,curl:33
One file type, outside dot directoriesowner @{HOME}/[^.]**.@{qpdf_ext} rw,qpdf:17
The program's own directoryowner @{HOME}/.znc/moddata/** rw,znc:32
A directory tree kept in a tunableowner file r @{rygel_media_dirs}/{,**},rygel:56

abstractions/user-tmp:

owner @{HOME}/tmp/**  rwkl,
owner @{HOME}/tmp/    rw,
owner /var/tmp/**     rwkl,
/var/tmp/             rw,
owner /tmp/**         rwkl,
/tmp/                 rw,
  • owner in the kernel (code): the rule is used when the task's fsuid equals the file's owner.
  • A narrower temp rule: owner /tmp/tr_session_id_* rwk, (transmission:31).

The tools

aa-autodep  [--force] [-d DIR] [--no-reload] program...
aa-genprof  [-d DIR] [-f LOGFILE] [-j] [--no-abstraction] [-o OUTDIR] program
aa-logprof  [-d DIR] [-f LOGFILE] [-m MARK] [-j] [-a] [--no-abstraction] [-o OUTDIR]
aa-complain [-d DIR] [--no-reload] [--force] program...
aa-enforce  [-d DIR] [--no-reload] [--force] program...

A new profile contains (code):

ProgramContents
anyflags=(complain), include <abstractions/base>; in the file also abi <abi/5.0>, and include <tunables/global> when present
a binaryPATH mr,
a #! scriptSCRIPT r,, INTERPRETER ix, and abstractions/bash, perl, python or ruby when the interpreter is one of those

Question keys for a file record:

KeyEffect
(A)llowadd the selected option to the profile
(D)enyadd a deny rule
(I)gnoreskip this record
(G)lobwiden the path one step and add it to the options
Glob with (E)xtensionwiden the path and keep the file extension
(N)ewedit the path by hand
Audi(t)toggle audit on the proposed rule
(O)wner permissions on / offtoggle owner; offered when the record's fsuid equals its ouid
Abo(r)t, (F)inishabandon the session (after a confirmation), or stop asking
  • Options are listed in this order: matching abstractions by name, paths of existing rules, [globs] entries of logprof.conf, the exact path. The first is preselected.
  • Glob ladder: /usr/share/foo/a.txt, /usr/share/foo/*, /usr/share/**, /usr/**, /**.
  • Default key: Allow for ALLOWED records, Deny for DENIED records.
  • aa-logprof -a: every rule accepted, exec questions answered with px if a profile for the target exists, else ix.
  • Log files searched when -f is not given: /var/log/audit/audit.log, /var/log/syslog, /var/log/messages, first existing.
MessageWhen
It seems AppArmor was not started. Please enable AppArmor and try again.aa-genprof when AppArmor is not available
... is currently marked as a program that should not have its own profile. ...the program is listed under [qualifiers] in logprof.conf (shells, core utilities)
Profile ... is in unconfined mode. Use --force to switch it to enforce mode.aa-enforce on a flags=(unconfined) profile
WARNING: Can't find abstractions/base, therefore not adding it to the new profile.the abstraction file is missing

Complain mode in the kernel

  • Let through and logged as apparmor="ALLOWED": any permission that no rule allows and no rule denies (code).
  • Not let through: what a plain deny rule covers. It is refused without a record (code).
  • An exec with no matching rule runs under a learning profile instead of failing (code). A record from the library's test suite (libraries/libapparmor/testsuite/test_multi/exec01.in, shortened) names it in target=:
apparmor="ALLOWED" operation="exec" profile="/home/cb/bin/hello.sh" name="/usr/bin/rm" ... requested_mask="x" denied_mask="x" fsuid=1000 ouid=0 target="/home/cb/bin/hello.sh//null-/usr/bin/rm"
  • A denied capability in enforce mode, from testcase_dmesg_capability.in (shortened):
apparmor="DENIED" operation="capable" ... pid=7292 comm="syscall_setprio" capability=23  capname="sys_nice"
  • aa-logprof turns that record into capability sys_nice,.

for AppArmor developers

Where the defaults are decided

WhatWhere
new profile: complain flag, base, mr on the binaryutils/apparmor/aa.py:386-428
interpreter to abstraction mappingutils/apparmor/aa.py:347-383
abi and tunables/global added to the fileutils/apparmor/aa.py:516-519
genprof loop, printk ratelimit, Scan and Finishutils/aa-genprof:122-192
one pass over the logutils/apparmor/aa.py:1501-1516
include proposals (match_includes)utils/apparmor/aa.py:1419-1459
known-rule check (is_known_rule)utils/apparmor/aa.py:2341-2351
file options (propose_file_rules), option order (order_globs)utils/apparmor/aa.py:2378-2408, utils/apparmor/aa.py:1022-1034
keys (available_buttons), default keyutils/apparmor/aa.py:1331-1361, utils/apparmor/aa.py:1177-1182
Glob (glob_path)utils/apparmor/aare.py:97-123
-a answersutils/apparmor/ui.py:518-526
exec menu and its defaultsutils/apparmor/aa.py:622-661, utils/apparmor/aa.py:785-789
[qualifiers] refusalutils/apparmor/aa.py:2501-2510
--force guard for unconfined profilesutils/apparmor/tools.py:198-206
owner testlinux:security/apparmor/file.c:202-205
capability checklinux:security/apparmor/lsm.c:199-211, linux:security/apparmor/capability.c:128-154
socket checklinux:security/apparmor/net.c:254-270
exec without a rule; learning profile in complain modelinux:security/apparmor/domain.c:749-770
complain set and cleared errorlinux:security/apparmor/lib.c:369-370, linux:security/apparmor/lib.c:460-461, linux:security/apparmor/include/audit.h:206-211
"mediates" entry per classparser/parser_regex.c:1385-1450

Roads from here

FILE ACCESS

PREAMBLE AND REUSE

  • include — how include lines are read

PROFILE HEAD

QUALIFIERS

IN PRACTICE · general-purpose apps

IN PRACTICE · security-sensitive programs

RUNNING THE PARSER

TOOLS · utils/