Profiles for security-sensitive programs
precise rules, every operation for application developers for administrators for security researchers
Privileged daemons and setuid helpers and network-facing services get profiles written by someone who knows every operation the program performs. Capabilities are listed one by one with reasons and paths are exact and every program started gets its own or a child profile. Shown on the shipped ping and dnsmasq profiles with the places where shipped profiles fall short.
On the map: security-sensitive programs
▶ Watch this deck — 10 scenes, ~8 min
Worth seeing first
- Profiles for general-purpose apps — the two shipped profiles and the profiling tools
Part of journeys
- Hardening a privileged program, step 1 of 11 · next: Hard links and name-based rules
01 A profile for a program that holds privileges
unix-chkpwd: capability audit_write, and capability dac_read_search, each with a reason, and /etc/shadow r,. A precise profile lists individual capabilities, exact paths and named exec targets.
unix_chkpwd is the little helper that checks your password. To do that it reads the shadow file, so if it's ever taken over, the attacker is reading password hashes. Now look at how its shipped profile is written. Two capabilities, each with a comment that says what it's for. One exact rule: /etc/shadow, read. Not write, and not /etc as a whole. You can only write a profile like this if you know every operation the program performs. This deck shows how such profiles are built, how their authors find out what the program does, and where shipped ones still fall short.
Details and sources
Details
- The profile is
unix-chkpwd, attached to/{,usr/}{,s}bin/unix_chkpwd(unix-chkpwd:14). Its two capabilities each sit under a comment that gives the reason:capability audit_write,"To write records to the kernel auditing log." andcapability dac_read_search,"To read shadow with 000 permissions." (unix-chkpwd:18-21). - The only rule for the password database is
/etc/shadow r,(unix-chkpwd:27). The only network rule of its own isnetwork netlink raw,(unix-chkpwd:23). - The three red chips are rules the profile does not contain. They are not quoted from a log.
- It still includes two abstractions,
abstractions/baseandabstractions/nameservice(unix-chkpwd:15-16), which add their own rules; the next scene shows whatnameservicebrings. owner /dev/pts/[0-9]* rw,andowner /dev/tty[0-9]* rw,stand under the comment "file_inherit" (unix-chkpwd:37-39): terminals the helper inherits already open.- The profile line carries
flags=(attach_disconnected.path=/att/unix-chkpwd/), and five rules name paths below that prefix, for example/att/unix-chkpwd/run/systemd/journal/dev-log w,(unix-chkpwd:41-46). See The other profile flags. - By the numbers: 22 rule lines, 2 abstractions, 2 capabilities, no
**, 2ownerrules. - Nothing on this slide was observed on a running kernel; kernel behaviour is read from the kernel source, profiles from AppArmor 836052d21 (6.0.0~alpha2).
Read more
02 ping next to curl: what precise means
ping: capability net_raw, capability setuid, network inet raw, network inet6 raw, and two exact paths. Through abstractions/nameservice it also gets capability net_bind_service, inet stream and dgram sockets and network netlink raw.
Here are the two shipped profiles from the general-purpose deck, with ping in front this time. On the left is ping's whole profile: two capabilities, two raw socket rules, three file rules, and you could explain every one. Curl, on the right, has a rule for your entire home directory. That's the difference in style. But be honest about the left side too. Ping includes the nameservice abstraction, the same one curl uses, so after expansion it also has ordinary TCP and UDP sockets and a third capability. Precise is a discipline, not something a profile gets by being short. Newer profiles take the nameservice-strict abstraction instead.
Details and sources
Details
- ping's whole body is on the slide: bin.ping:15-27. curl is summarised from curl:14-48: six abstraction includes,
owner file rw @{HOME}/**,(curl:33), sixnetworkrules (curl:36-41) andfile rw @{run}/snapd.socket,(curl:45). - What
abstractions/nameserviceadds to every profile that includes it:network inet stream,network inet6 stream,network inet dgram,network inet6 dgram,andnetwork netlink raw,(nameservice:92-99), and throughabstractions/niscapability net_bind_service,(nis:16). The parser built from the AppArmor tree, run onbin.ping, lists them next to ping's own two capabilities. - Profiles that include
abstractions/nameservice-strictinstead: iotop-c:8, hostname:21, nslookup:20, mbsync:18. - The tally counts lines in the two files. A row "exact file rules" was planned and left out: ping has two (
/etc/modules.conf, the/procpath) and curl has four (.curlrc,.config/curlrc,/etc/rpmdevtools/curlrc,@{run}/snapd.socket), so the number does not separate the styles. - The precise group in numbers (rule lines, abstractions, capability lines, lines with
**,ownerlines):
| File | Lines | Abstractions | Capabilities | ** | owner | Flags |
|---|---|---|---|---|---|---|
| bin.ping | 15 | 3 | 2 | 0 | 0 | none |
| sbin.klogd | 18 | 1 | 2 | 0 | 0 | none |
| unix-chkpwd | 22 | 2 | 2 | 0 | 2 | attach_disconnected.path=/att/unix-chkpwd/ |
| usr.sbin.ntpd | 58 | 4 | 9 | 0 | 0 | attach_disconnected |
| usr.sbin.dnsmasq | 88 | 4 | 7 | 1 | 5 | attach_disconnected |
| usr.sbin.dovecot | 66 | 7 | 9 | 4 | 1 | attach_disconnected |
| usr.sbin.smbd | 65 | 8 | 12 | 10 | 0 | attach_disconnected.path=/att/smbd/ |
| curl, for contrast | 23 | 6 | 0 | 1 | 3 | none |
- A larger example of the same style: ntpd has nine capabilities and 38 file rules, each a single file or a one-level pattern, and no
**(usr.sbin.ntpd:22-69). - The extras
passwdprofile has one rule per file the program rewrites:/etc/shadow rwl,/etc/shadow.old rwl,/etc/shadow.tmp?????? rwl,/etc/shadow.lock rwl,/etc/shadow- rw,/etc/shadow+ rw,(extras/usr.bin.passwd:23-37).
03 Capabilities, one by one, each with a reason
deny capability sys_admin, plus capability sys_nice, (iotop-c) forces the weaker capability and silences the probe. In the kernel a capability without a rule fails with EPERM; with deny it fails without a log record. Read from the kernel code.
Iotop wants to set a real-time I/O priority. In the kernel, that call checks for sys_admin first and falls back to sys_nice, and sys_admin is nearly root. The profile's author read that code and wrote it down: deny sys_admin, allow sys_nice. The deny does two jobs: the kernel's first question gets a no without a log line, and no include can grant it back. So grant the weakest capability that works and say why, like dnsmasq's net_admin for the DHCP server. Decline what the program survives without, as fusermount does. And give a dangerous one only to the helper that needs it.
Details and sources
Details
- Syntax:
capability <name> [<name>...],anddeny capability <name>,. - iotop-c: the comment and the two rules are at iotop-c:10-20. The slide quotes the end of the comment; the start says that
ioprio_setcheckssys_adminorsys_nice, "with sys_admin checked first, with OR shortcut logic", when it sets a real-time class. - That order is in the kernel:
if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) return -EPERM;for the real-time class (ioprio.c:47). deny capability X,and no rule for X both refuse X. The deny form adds two things: no log record, and no later rule or include can grant it. In the kernel a capability without a rule fails withEPERM(capability.c:150-154); one in the quiet set, which is wheredenyputs it, returns the error without a record (capability.c:87-92).- The record at the bottom is built, not captured. Its layout is that of testcase_dmesg_capability.in (
operation="capable" ... capability=23 capname="sys_nice"); profile, pid and comm are illustrative, and...stands for the fixture'sparent=field.sys_adminis capability 21 andsys_niceis 23 (capability.h:281, capability.h:296). - Grant with a reason:
capability net_admin, # for DHCP serverandcapability net_raw, # for DHCP server ping checks(usr.sbin.dnsmasq:22-28). - Decline: fusermount3 keeps
# capability dac_override,and# capability setuid,commented out under "Flatpak seems to work fine without these capabilities, so don't grant them unless actual functionality breakage is caused elsewhere" (fusermount3:11-15). - Isolate:
nvidia_modprobeholdschown,mknod,setuidandsys_admin;capability sys_module,exists only in itskmodchild (nvidia_modprobe:12-15, nvidia_modprobe:44). - More silencing:
deny capability net_admin, # noisy setsockopt() calls(usr.sbin.traceroute:20),deny capability block_suspend,(usr.sbin.nscd:22),deny capability sys_admin,with "libudev seems to work fine without the BPF filter in place, so we can silence the denial" (lsusb:43-45). - The bare form
capability,grants every capability (plasmashell:8). - aa-logprof shows a severity from severity.db with each capability question (
CAP_SYS_ADMIN 10,CAP_SYS_MODULE 10,CAP_DAC_OVERRIDE 9,CAP_SETUID 9,CAP_NET_RAW 8). The number informs the author; the tool refuses nothing by severity. - What the log hides about capabilities in the kernel: the same capability for the same credentials is not logged again within one second (capability.c:70, capability.c:94-106), and optional probes are logged only in complain mode, with
info="optional: no audit"(capability.c:139-144, capability.c:156-163). - Nothing on this slide was observed on a running kernel; kernel behaviour is read from the kernel source, profiles from AppArmor 836052d21 (6.0.0~alpha2).
Read more
- apparmor.d(5), Capabilities, apparmor.d(5), deny.
- wiki: TechnicalDoc_Kernel (DAC, capabilities and LSM).
- Capability rules, allow and deny, Child profiles.
for AppArmor developers for security researchers
Code
profile_capable: capability.c:121-166; the quiet test inaudit_caps: capability.c:87-92; the hookapparmor_capable: lsm.c:199-211.- Which branch of
profile_capablea profile compiled withabi <abi/5.0>takes (policy DFA or the older bit sets) was not determined; both refuse a capability that has no rule. - The severity of the bare
capability,rule is that of__ALL__: capability.py:127-129.
04 Exact paths, and the one place for two stars
dnsmasq: /etc/dnsmasq.d/ r, /etc/dnsmasq.d/* r, /var/lib/misc/dnsmasq.leases rw, @{run}/*dnsmasq*.pid w, and one read-only **: @{TFTP_DIR}/** r,. Site additions go in local/usr.sbin.dnsmasq.
Dnsmasq holds seven capabilities, so its file rules are careful. Configuration: the directory and its direct entries, two rules, read only. The lease file, read and write, with a note that only the DHCP server needs it. Pid files: a star for the part of the name that varies, inside one directory. The binary lives in bin on some systems and sbin on others, so the path is an alternation, not a wildcard. And the only double star is read only, for the TFTP directory. The price is upkeep: every program that integrates with dnsmasq has its own block of rules, and a new one needs a new block.
Details and sources
Details
*matches within one path component,**crosses/. A rule for a directory itself ends in/:/etc/dnsmasq.d/ r,lists the directory,/etc/dnsmasq.d/* r,reads its direct entries (usr.sbin.dnsmasq:42-43).- The other rules on the slide:
/var/lib/misc/dnsmasq.leases rw, # Required only for DHCP server usage(usr.sbin.dnsmasq:66),@{run}/*dnsmasq*.pid w,(usr.sbin.dnsmasq:61), the profile line with/usr/{bin,sbin}/dnsmasq(usr.sbin.dnsmasq:17), and@{TFTP_DIR}/** r,under "for the read-only TFTP server" (usr.sbin.dnsmasq:74-76). @{TFTP_DIR}=/var/tftp /srv/tftp /srv/tftpbootis declared in the profile file itself (usr.sbin.dnsmasq:14).- "Seven capabilities" counts usr.sbin.dnsmasq:22-28.
- The integration blocks: libvirt, lxc-net, lxd-bridge, NetworkManager, podman's dnsname plugin and waydroid (usr.sbin.dnsmasq:78-119). Several of them add further lease and pid files, such as
/var/lib/misc/dnsmasq.*.leases rw,. - Site additions go through
include if exists <local/usr.sbin.dnsmasq>(usr.sbin.dnsmasq:142), so the reviewed file stays unchanged. - More exact paths in shipped profiles:
/dev/nvidia-modeset w,/dev/nvidia-uvm w,/dev/nvidia-uvm-tools w,(nvidia_modprobe:27-29);@{run}/mount/utab.lock rwk,(fusermount3:55);/var/log/wtmp.@{d} r,for rotated logs (who:11);/usr/lib{,64}/libvirt/libvirt_leaseshelper(usr.sbin.dnsmasq:87). - In aa-logprof the
(N)ewkey lets you type the exact pattern; the(G)lobkey works against this style (aa.py:1248-1287). - Chroots: ntpd lists the same files below its chroot,
/var/lib/ntp/@{PROC}/@{pid}/net/if_inet6 r,and/var/lib/ntp@{run}/ntp/ntpd.pid w,(usr.sbin.ntpd:57, usr.sbin.ntpd:72-73), because the name the kernel reports depends on where the task's root is.
05 Every program it starts gets a profile
Cx -> child moves a helper into a child profile: capability sys_module, exists only in nvidia_modprobe's kmod child. ix keeps the parent's profile, capabilities included: /{,usr/}bin/{ba,da,}sh ix, in dnsmasq.
A privileged program that starts another program hands something on. The question is what. nvidia_modprobe needs a kernel module loaded, and that takes sys_module, about the most dangerous capability there is. The profile doesn't hold it. It runs kmod in a child profile, and only the child has sys_module. Dnsmasq does the same for a libvirt helper: a child with no capabilities and nine file rules. Now the next line. The shell runs with ix, inherit, so a DHCP script runs with dnsmasq's whole profile, seven capabilities included. The comment says why. Somebody made that decision, and in a precise profile every exec line is one.
Details and sources
Details
- Forms:
<path> Px,(the target's own profile),<path> Px -> name,(a named profile),<path> Cx -> child,(a child profile in the same file),<path> ix,(stay in the current profile). An upper-casePorCasks for secure execution, which scrubs the environment; see safe and unsafe exec. - nvidia_modprobe: four separate
capabilitylines, summarised on the slide (nvidia_modprobe:12-15);/usr/bin/kmod Cx -> kmod,(nvidia_modprobe:23); the child withcapability sys_module,(nvidia_modprobe:39-62). A child profile is namedparent//childin the kernel, hencenvidia_modprobe//kmod. - dnsmasq:
/usr/libexec/libvirt_leaseshelper Cx -> libvirt_leaseshelper,(usr.sbin.dnsmasq:87-88); the child has one abstraction, no capability and nine file rules (usr.sbin.dnsmasq:121-139). /{,usr/}bin/{ba,da,}sh ix, # Required to execute --dhcp-script argument(usr.sbin.dnsmasq:68). Withixthe shell and the script keep the profilednsmasq, including usr.sbin.dnsmasq:22-28. This follows from the rule; it was not run.- dovecot: 19 helpers with
Px, each with its own profile file, and two withrix(usr.sbin.dovecot:50-70). - Several children with different rights: zgrep sends the compressors and grep to
helper, sed tosed, and runscat,expr,mktemp,rmand the shell withix(zgrep:23-37, zgrep:46-73). - A named
Pxacross files:/usr/lib*/samba/{,samba/}samba-dcerpcd Px -> samba-dcerpcd,(usr.sbin.smbd:43). - Programs that switch profile themselves: php-fpm has
change_profile -> php-fpm//*,and write access to@{PROC}/@{pid}/attr/{apparmor/,}current(php-fpm:29, php-fpm:50-57); apache2 uses hats (usr.sbin.apache2:90-103). - aa-logprof offers inherit, profile, child, a named target, unconfined and deny for each exec event (aa.py:622-661); the child choice is not offered for an exec seen inside a child profile or hat (aa.py:777-779).
- The only
ux-family rule inprofiles/apparmor.d/is/** pux,in plasmashell (plasmashell:24), one of the allow-everything profiles.
Read more
- apparmor.d(5), px, apparmor.d(5), ix, apparmor.d(5), ux, apparmor.d(5), Directed profile transitions.
- aa_change_hat(2), aa_change_profile(2).
- wiki: sanitized_helper: environment scrubbing on exec transitions.
- Exec modes, Child profiles, Named exec transitions, safe and unsafe exec, Hats, Exec and domain transitions.
06 Subtract, name the peer, and log your guesses
audit deny /var/** w, after include <abstractions/wutmp> (who) removes a grant. signal (receive) peer=libvirtd, (dnsmasq) names the sender. audit on an allow rule (wpa_supplicant) logs each use.
Three small tools that precise profiles lean on. First, subtraction. Who includes the login records abstraction for its list of files, but that abstraction also allows writing them. So the profile denies the writes again, with audit, so an attempt shows up in the log. Second, peers. Dnsmasq accepts signals, and being inspected, but only from libvirtd, by name. Third, an honest guess. The author of the wpa_supplicant profile wasn't sure about one D-Bus path, so the rule allows it with audit in front, and every use leaves a record. One thing to remember: a plain deny is silent. If you want to hear about attempts, write audit deny.
Details and sources
Details
- Qualifier order:
[audit] [deny] [owner] <rule>. - who:
include <abstractions/wutmp>(who:9), then "Deny the writes allowed by abstractions/wutmp" withaudit deny /var/** w,andaudit deny @{run}/utmp w,(who:39-41). - dnsmasq:
signal (receive) peer=libvirtd,andptrace (readby) peer=libvirtd,, each also written with the path-named peer/usr/{bin,sbin}/libvirtd(usr.sbin.dnsmasq:32-35). The peer is a profile name. The rule covers any signal; the slide shows one. - wpa_supplicant: the
audit dbus (send)rule spans five lines in the file and is shortened with...on the slide (wpa_supplicant:84-89). It is a D-Bus rule: the bus daemon enforces and logs it, not the kernel. See D-Bus mediation. auditappears in four files ofprofiles/apparmor.d/(tnftp, unprivileged_userns, wpa_supplicant, who); three of them use it only asaudit deny.- A plain
denywrites no record;audit denydoes. See The audit qualifier. peer=<profile>andpeer=(label=<profile>)are accepted on signal, ptrace, unix and dbus rules. More shipped examples:unix (receive, send) type=stream peer=(label=dovecot-anvil),(usr.sbin.dovecot:39-40);signal (send) peer=@{profile_name}//*,(usr.sbin.apache2:76); a unix rule tied to an address,addr="@/tmp/.X11-unix/X[0-9]*"(Xorg:65).- Deny for what must not happen at all:
audit deny capability,andaudit deny change_profile,(unprivileged_userns:8-9);deny network,in tnftp's command child (tnftp:77). - Deny to silence a harmless probe:
deny /usr/bin/apt-cache x,anddeny /tmp/gtalkplugin.log w,(lsb_release:43, lsb_release:52);deny / rw,(php-fpm:47). ownerinside privileged profiles: unix-chkpwd:37-39; for rootless podman, next to the unqualified root-owned form, usr.sbin.dnsmasq:111-116.- A deny cannot be undone by a later allow of the same priority,
local/included. - aa-logprof cannot add a deny through an include: "Denying via an include file isn't supported by the AppArmor tools" (aa.py:1232-1234).
- Known discrepancy: an
audit denyfile rule in a complain-mode profile appears to be allowed, going by the kernel code (verdict unclear, not run).whorelies onaudit denyto cancel the writes fromwutmp. Test deny rules in enforce mode. - Known discrepancy: zgrep has both
/etc/passwd r,anddeny /etc/passwd r,(zgrep:22, zgrep:42). The deny decides and the allow is dead. Read from the file, not run.
Read more
- apparmor.d(5), Rule Qualifiers, apparmor.d(5), deny, apparmor.d(5), audit, apparmor.d(5), owner.
- apparmor.d(5), Signal rules, apparmor.d(5), PTrace rules, apparmor.d(5), DBus rules, apparmor.d(5), abstractions/wutmp.
- wiki: rule-prefixes-and-modes.
- allow and deny, The audit qualifier, Signal rules, Ptrace rules, Unix socket rules, D-Bus mediation.
07 Finding every operation: the log is not enough
Complain-mode records and aa-audit show what reached AppArmor during your runs. Shipped comments (who, iotop-c, lsusb) cite system calls, kernel code and library source: knowledge from tracing and reading code, which the log does not contain. That an O_PATH open is not mediated is read from the kernel code.
How did the author know all that? The AppArmor side is the log. In complain mode each missing permission becomes a record while the program keeps running, and aa-audit logs what the profile allows. But read the comment in the who profile. It quotes an openat call with O_PATH, and concludes those calls aren't mediated. No log line says that. It comes from system calls and man pages. The log also can't show code you didn't run, denials a deny rule silenced, or why the program wants a file. So you need both: the log, and the program itself, through a tracer such as strace and its source.
Details and sources
Details
- The record on the slide is built, not captured: the field layout is that of syslog_datetime_01.in (
apparmor="ALLOWED" operation="open" parent=... profile=... name=... pid=... comm=... requested_mask="r" denied_mask="r" fsuid=... ouid=...), with illustrative values. It is the record that/dev/pts/ r,in the who profile answers (who:27). - File records carry
operation=,profile=,name=,requested_mask=,denied_mask=,fsuid=,ouid=; capability records carrycapability=andcapname=. Reading them is in Reading a denial. - The comment on the right is who:14-24, wrapped for the panel. It ends "Overall result: the above calls are not mediated by LSMs", and the profile has no
/dev/ r,rule. - In the kernel an
O_PATHopen returns before the file-open security hook is called (open.c:951-956, open.c:977). Read from the code, not run. See What is not mediated. aa-auditadds theauditflag to the profile and reloads it ("Setting %s to audit mode.", tools.py:228-243).aa-audit --removetakes it away again ("Removing audit mode from %s."). In the kernel every allowed permission of such a profile is logged as anAUDITrecord, and denials thatdenyrules keep quiet are logged too. aa-logprof dropsAUDITrecords, so they are read by a person. No shipped profile carries the flag.- The rule classes aa-logprof turns into rules: capability, change_profile, dbus, file, ptrace, signal, userns, mqueue, io_uring, mount, unix, network, pivot_root (logparser.py:61-75).
aa-unconfined"Lists unconfined processes having tcp or udp ports" (aa-unconfined:31): a way to find network-facing programs that have no profile yet.- Not in the log, the complete list: operations already allowed (unless the audit flag or an
auditrule is set); denials quieted bydenyrules; a capability check repeated for the same credentials within one second; optional capability probes in enforce mode; records lost to rate limiting (aa-genprof sets/proc/sys/kernel/printk_ratelimitto 0 and warns "Can't set printk_ratelimit, some events might be lost" when it cannot, aa-genprof:133-143; aa-logprof alone does not); code paths the runs did not reach; operations that are not mediated; and the reason for any of it. - Where shipped comments got their reasons: system-call arguments (who:19-20), kernel source (iotop-c:11-18), library source files by name (lsusb:40-41), program options (curl:30-32, usr.sbin.dnsmasq:68), bug reports (fusermount3:11).
- Neither tree contains or invokes a tracer. Tracing and source reading are recommended here as practice that these comments give evidence of. Which tools the authors used was not determined.
Read more
- aa-audit(8), aa-complain(8), aa-unconfined(8), aa-logprof(8).
- apparmor(7), Complain mode, apparmor(7), Force audit mode.
- apparmor.net: Verify AppArmor coverage and audit profiles, apparmor.net: Audit the system, wiki: Complain-Mode.
- Reading a denial, How a denial is logged, What is not mediated, The audit qualifier.
for AppArmor developers for security researchers
Code
- The profile's audit flag is unpacked as
AUDIT_ALL: policy_unpack.c:1241. With it, every permission is audited and nothing is quiet: lib.c:374-380; for files file.c:122-123 and file.c:142-149; for capabilities capability.c:80-92. - The one-second cache for capability records,
AUDIT_CACHE_TIMEOUT_NS: capability.c:70, capability.c:94-106. do_dentry_openreturns forO_PATHat open.c:951-956;security_file_openis called at open.c:977.- aa-logprof returns early for
AUDIT,STATUSandERRORevents: logparser.py:303-304.
08 Privileged and broad: what to look for in a shipped profile for security researchers
Shipped examples of privilege next to breadth: tar (dac_override, file rwl /**,), sbin.syslogd (dac_override, /var/log/** rw,), usr.sbin.apache2 (/** mrwlkix,), usr.sbin.dnsmasq (sh ix with seven capabilities), extras usr.sbin.sshd (Uxr for shells). What each allows is derived from the rules, not demonstrated.
Precise is the goal, and the tree doesn't always reach it. Look for these combinations. A capability that bypasses file permissions, next to a whole-tree rule: tar has dac_override with read, write and link on everything, and syslogd has it with all of /var/log. A profile whose main rule is /**, like apache's. A shell run with inherit by a daemon that holds capabilities, as in dnsmasq. And unconfined exec, which the extras sshd profile uses for login shells on purpose. None of these is a kernel bug. Each is that profile's real boundary. So expand the profile and grep before you trust its name.
Details and sources
Attack surface
Each consequence below is derived from the rule text. None was demonstrated on a running system.
- tar:
capability dac_override,andcapability dac_read_search,under the comment "used to compress user files as root", thenfile rwl /**,(tar:18-25). The file rule covers every path, link permission included, and the capability removes the permission-bit check. The same profile runs any program in the bin directories withix(tar:27-30), which keeps those capabilities. What remains as a control: the profile's lack of other capabilities and of network rules. - syslogd:
capability dac_override,(sbin.syslogd:22) next to/var/log/** rw,(sbin.syslogd:42): any file under/var/log, whoever owns it. - apache2: six capabilities,
dac_overrideamong them, with/ rw,and/** mrwlkix,(usr.sbin.apache2:78-86); the hatsDEFAULT_URIandHANDLING_UNTRUSTED_INPUThave the same file rule (usr.sbin.apache2:95, usr.sbin.apache2:102). The profile gives the process a name and a capability list; file access is left to the permission bits. - dnsmasq:
/{,usr/}bin/{ba,da,}sh ix, # Required to execute --dhcp-script argument(usr.sbin.dnsmasq:68) next to seven capabilities (usr.sbin.dnsmasq:22-28),net_admin,dac_overrideandsetuidamong them. A script run this way has the whole profile. - extras sshd: 14
Uxrrules for shells and fornologinandfalse(extras/usr.sbin.sshd:92-105),/usr/lib/openssh/sftp-server PUx,(extras/usr.sbin.sshd:160), fifteen capabilities (extras/usr.sbin.sshd:29-43) and/** r,withowner /** rwl,(extras/usr.sbin.sshd:153-155). For a login service the session is meant to leave the daemon's profile. The extras directory holds profiles that are not enabled by default. - Side grants inside precise profiles: ping, dnsmasq, ntpd, unix-chkpwd and nscd include
abstractions/nameservice, which adds inet stream and dgram sockets,network netlink raw,andcapability net_bind_service,(nameservice:92-99, nis:16). - The
lpermission in world-writable directories, in a root daemon:/tmp/ntp* rwl,and/var/tmp/ntp* rwl,(usr.sbin.ntpd:43, usr.sbin.ntpd:68). See Hard links and name-based rules. - A loaded profile name is not evidence of confinement: of the 198 profile files directly in
profiles/apparmor.d/, 76 areflags=(unconfined)and 14 areallow all,. aa-logprof -aaccepts every logged event and is a source of weak generated profiles.- Known discrepancy: with
aa-logprof -athe secure-execution question is answered "no", so an exec to a profiled target is written as lower-casepx, although the interactive default isPx(ui.py:159-160, ui.py:518-526, aa.py:884-887). Read from the code, not run.
Details
apparmor_parser -p FILE(--preprocess) prints the profile with its includes expanded, which is what thegrepon the slide needs. The pattern is an illustration of where to look; no output of it was recorded for the map.aa-statuslists the loaded profiles and their modes; see aa-status and aa-enabled.
Read more
- apparmor.d(5), Capabilities, apparmor.d(5), ux, apparmor_parser(8), OPTIONS, aa-status(8).
- wiki: TechnicalDoc_Kernel (DAC, capabilities and LSM).
- apparmor.net: Verify AppArmor coverage and audit profiles.
- AppArmor's threat model, The limits of name-based mediation, What is not mediated, Hard links and name-based rules, Exec modes.
09 From complain to enforce, the careful way
aa-genprof --no-abstraction, (N)ew for exact paths, px/cx with secure execution, hand-written comments, deny and peer= rules, aa-enforce, then aa-audit for a watch period. Site changes go into local/<profile>.
The tools are the ones from the general-purpose deck. Your answers change. Start aa-genprof with the --no-abstraction option, so it offers paths instead of includes. Exercise everything, including reload, shutdown and error paths. For each question, press N and type the exact path instead of globbing. For each exec, choose a profile or a child, and say yes to secure execution. Then edit by hand: reasons, denies, peers. The tools don't write those. Enforce, and run with aa-audit to see what the profile uses and refuses. Keep site changes in the local file. And stay away from aa-logprof -a, which accepts everything.
Details and sources
Details
aa-genprof [-d DIR] [-f LOGFILE] [-j] [--no-abstraction] [-o DIR] program.--no-abstraction: "Do not use any abstractions in profiles" (aa-genprof:68-73). The new profile starts in complain mode; Finish switches it to enforce (aa-genprof:187-190).aa-logprof [-d DIR] [-f LOGFILE] [-m MARK] [-j] [-a] [--no-abstraction] [-o DIR].-a,--allow-all: "Accept silently all rules" (aa-logprof:27-33).aa-enforce,aa-complainandaa-audittake program names, or--profile,--profile-file,--executable-file, plus-d DIRand--no-reload.aa-enforceandaa-complainskip aflags=(unconfined)profile without--force("Profile %s is in unconfined mode. Use --force to switch it to enforce mode.").aa-audit -r(--remove) removes the audit flag again (tools.py:196-243).aa-enforceclears the complain flag and leaves anauditflag in place (aa.py:276-281).(N)ewasks for the path and warns when it does not match the log entry: "The specified path does not match this log entry" (aa.py:1260-1287).- After
pxorcxthe tool asks "Should AppArmor enable secure-execution mode when switching profiles?" with yes as the default; yes givesPxorCx. Afteruxit warns "Launching processes in an unconfined state is a very dangerous operation and can cause serious security holes." (aa.py:863-900). - The tools do not write comments, peer conditions or
audit denyrules. Those are edits by hand. - Long-running daemons in the tree carry
attach_disconnected(ntpd, dnsmasq, dovecot) orattach_disconnected.path=(smbd, unix-chkpwd), and the second form needs rules for the prefixed paths (unix-chkpwd:41-46). See The other profile flags. - Not determined: whether aa-genprof's Finish clears
complainoncxchild profiles created during the session. Check the flags of child profiles in the written file. - Test deny rules in enforce mode. Known discrepancy: an
audit denyfile rule in a complain-mode profile appears to be allowed, going by the kernel code (verdict unclear, not run). - Known discrepancy: with
aa-logprof -aan exec to a profiled target is written as lower-casepxalthough the interactive default isPx(ui.py:159-160, ui.py:518-526). Read from the code, not run.
Read more
- aa-genprof(8), aa-logprof(8), aa-enforce(8), aa-complain(8), aa-audit(8), logprof.conf(5).
- apparmor.net: Generate profiles with aa-genprof, apparmor.net: Confine an application, apparmor.net: Standardized AppArmor policy.
- Generating a profile, aa-logprof, Switching profile modes, Profile mode flags, The other profile flags, Reading a denial.
10 Three reviews before you enforce
Review a precise profile for symlinks (prac-symlinks), hard links (prac-hardlinks) and /proc and /sys paths (prac-sys-proc). Then: prac-abstractions, syn-capability, syn-child-profiles, syn-audit, sec-unmediated, adm-denials.
Before you enforce a precise profile, review three things, each with its own deck. Symlinks: an exact rule has to name the path the kernel ends up at, which is why ping's profile spells out both bin directories. Hard links: the link letter in a directory other users can write, like ntpd's temp file rule, deserves a second look. And /proc and /sys, where the path your program opens is often not the path that gets checked. After those, the decks on capabilities, child profiles and audit go deeper. And if your program holds no privileges at all, the general-purpose deck is the easier road.
Details and sources
Details
- Symlinks: a rule must name the path the kernel resolves. Shipped profiles spell out both sides of the /usr merge, for systems where
/binis a symlink tousr/bin:/{usr/,}bin/{,iputils-}ping(bin.ping:15),/{,usr/}{,s}bin/unix_chkpwd(unix-chkpwd:14). - Hard links: the
lpermission appears on pid, lock and state files,@{run}/klogd.pid krwl,and/var/log/boot.msg rwl,(sbin.klogd:30-31),/etc/shadow rwl,(extras/usr.bin.passwd:31),/var/lib/ntp/drift rwl,(usr.sbin.ntpd:48); and in world-writable directories,/tmp/ntp* rwl,and/var/tmp/ntp* rwl,(usr.sbin.ntpd:43, usr.sbin.ntpd:68),owner /tmp/** rwkl,(user-tmp:21). - /proc and /sys: nvidia_modprobe lists
@{sys}/bus/pci/devices/ r,and then reads through the resolved device path,@{sys}/devices/@{pci_bus}/**/config r,(nvidia_modprobe:30-31). Three spellings of a /proc path exist in the tree:@{PROC}/kmsg(sbin.klogd:25), the literal/proc/kmsg(sbin.syslogd:34) and the literal/proc/*/cmdline(iotop-c:24). - Chroots: ntpd repeats its rules below
/var/lib/ntp(usr.sbin.ntpd:57, usr.sbin.ntpd:72-73). - Recursive write globs next to capabilities that bypass permission bits: sbin.syslogd:22 with sbin.syslogd:42; tar:22-25.
- Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2), the kernel-rc2 (97d73c825).
Read more
- apparmor.d(5), Access Modes Details, apparmor.d(5), Link rules, apparmor.d(5), Special builtin variables.
- kernel docs: protected_symlinks, kernel docs: protected_hardlinks, wiki: TechnicalDoc_Proc_and_ptrace.
- Symlinks: rules see through them, Hard links and name-based rules, Writing rules for /sys and /proc.
- Choosing abstractions, Capability rules, Child profiles, The audit qualifier, What is not mediated, Reading a denial, Profiles for general-purpose apps.
Read more
Basis of the statements on this page: profile text is quoted from the AppArmor tree at 836052d21 (6.0.0~alpha2); "code" means the source of mainline Linux 7.4 (97d73c825) or of the AppArmor utilities was read, not run; "derived" means the result was reasoned from that code or from rule text. Nothing here was observed on a running kernel.
Precise profiles in the tree
Counts per file in profiles/apparmor.d/: rule lines, include <abstractions/...> lines, capability lines, lines containing **, owner lines.
| File | Lines | Abstractions | Capabilities | ** | owner | Flags |
|---|---|---|---|---|---|---|
| bin.ping | 15 | 3 | 2 | 0 | 0 | none |
| sbin.klogd | 18 | 1 | 2 | 0 | 0 | none |
| unix-chkpwd | 22 | 2 | 2 | 0 | 2 | attach_disconnected.path=/att/unix-chkpwd/ |
| usr.sbin.ntpd | 58 | 4 | 9 | 0 | 0 | attach_disconnected |
| usr.sbin.dnsmasq | 88 | 4 | 7 | 1 | 5 | attach_disconnected |
| usr.sbin.dovecot | 66 | 7 | 9 | 4 | 1 | attach_disconnected |
| usr.sbin.smbd | 65 | 8 | 12 | 10 | 0 | attach_disconnected.path=/att/smbd/ |
Privileged and broad: capabilities next to a whole-tree rule.
| Profile | Rules | Where |
|---|---|---|
| tar | capability dac_override, and file rwl /**, | tar:18-25 |
| sbin.syslogd | capability dac_override, and /var/log/** rw, | sbin.syslogd:22, sbin.syslogd:42 |
| usr.sbin.apache2 and its two hats | six capabilities and /** mrwlkix, | usr.sbin.apache2:78-86 |
zgrep, child helper | capability dac_override, capability dac_read_search, and /{,**} r, | zgrep:50-51, zgrep:62 |
| extras usr.sbin.sshd | fifteen capabilities, /** r,, owner /** rwl,, Uxr for shells | extras/usr.sbin.sshd:29-43, extras/usr.sbin.sshd:92-105, extras/usr.sbin.sshd:153-155 |
Of the 198 profile files directly in profiles/apparmor.d/, 76 are flags=(unconfined) and 14 are allow all,.
Capability techniques
| Technique | Shipped example | Where |
|---|---|---|
| Grant with a reason | capability net_admin, # for DHCP server | usr.sbin.dnsmasq:22-28 |
| Decline | # capability dac_override, kept commented out | fusermount3:11-15 |
| Choose the weaker | deny capability sys_admin, with capability sys_nice, | iotop-c:10-20 |
| Silence | deny capability net_admin, # noisy setsockopt() calls | usr.sbin.traceroute:20 |
| Isolate in a child | capability sys_module, only in nvidia_modprobe//kmod | nvidia_modprobe:44 |
Kernel results (code):
- A capability with no rule fails with
EPERM. - A capability under
denyfails withEPERMand no record, unless the profile has the audit flag. - The same capability for the same credentials is not logged again within one second.
- Optional probes are not logged in enforce mode; in complain mode they are logged with
info="optional: no audit". - The bare rule
capability,grants every capability.
Path techniques
| Technique | Example from dnsmasq | Line |
|---|---|---|
| Directory, then its entries | /etc/dnsmasq.d/ r, and /etc/dnsmasq.d/* r, | usr.sbin.dnsmasq:42-43 |
| A single state file | /var/lib/misc/dnsmasq.leases rw, | usr.sbin.dnsmasq:66 |
| One-star pattern in one directory | @{run}/*dnsmasq*.pid w, | usr.sbin.dnsmasq:61 |
| Alternation for two layouts | /usr/{bin,sbin}/dnsmasq | usr.sbin.dnsmasq:17 |
Read-only ** | @{TFTP_DIR}/** r, | usr.sbin.dnsmasq:74-76 |
| Variable declared in the profile | @{TFTP_DIR}=/var/tftp /srv/tftp /srv/tftpboot | usr.sbin.dnsmasq:14 |
| Site additions | include if exists <local/usr.sbin.dnsmasq> | usr.sbin.dnsmasq:142 |
- Chroot spellings:
/var/lib/ntp/@{PROC}/@{pid}/net/if_inet6 r,and/var/lib/ntp@{run}/ntp/ntpd.pid w,(usr.sbin.ntpd:57, usr.sbin.ntpd:72-73).
Exec decisions
| Mode | Meaning | Shipped example | What the started program has |
|---|---|---|---|
Px | the target's own profile, secure execution | dovecot's 19 helpers (usr.sbin.dovecot:50-70) | only its own profile |
Px -> name | a named profile, secure execution | samba-dcerpcd Px -> samba-dcerpcd, (usr.sbin.smbd:43) | the named profile |
Cx -> child | a child profile in the same file | /usr/bin/kmod Cx -> kmod, (nvidia_modprobe:23) | the child's rules only |
ix | stay in the current profile | /{,usr/}bin/{ba,da,}sh ix, (usr.sbin.dnsmasq:68) | everything the parent has, capabilities included |
Ux, PUx | leave confinement, secure execution | shells in the extras sshd profile (extras/usr.sbin.sshd:92-105) | no profile |
Qualifiers in privileged profiles
| Use | Example | Where |
|---|---|---|
audit deny to subtract from an include | audit deny /var/** w, after include <abstractions/wutmp> | who:39-41 |
deny for what must not happen | audit deny capability, and audit deny change_profile, | unprivileged_userns:8-9 |
deny to silence a probe | deny /usr/bin/apt-cache x, | lsb_release:43 |
audit on an allow rule | audit dbus (send) bus=system path=/fi/w1/wpa_supplicant1/** ... | wpa_supplicant:84-89 |
owner | owner /dev/pts/[0-9]* rw, | unix-chkpwd:37-39 |
| Peer by profile name | signal (receive) peer=libvirtd, | usr.sbin.dnsmasq:32-35 |
| Peer by label | unix (receive, send) type=stream peer=(label=dovecot-anvil), | usr.sbin.dovecot:39-40 |
- A plain
denyis silent;audit denylogs the attempt. - A deny cannot be undone by a later allow of the same priority,
local/included.
What the log shows and what it does not
Shows:
- one record per refused permission, or in complain mode per permission that would have been refused;
- with
auditon a rule, or afteraa-auditon the profile, each use of an allowed permission as anAUDITrecord (code).
Does not show:
- operations that were already allowed;
- denials quieted by
denyrules; - a capability check repeated for the same credentials within one second (code);
- optional capability probes in enforce mode (code);
- records lost to rate limiting; aa-genprof sets
printk_ratelimitto 0, aa-logprof alone does not; - code paths the runs did not reach: error handling, options, reload, shutdown;
- operations that are not mediated, such as an
O_PATHopen (code); - why the program does any of it.
The procedure
| Step | Command or key | Tool message or note |
|---|---|---|
| 1 | aa-genprof --no-abstraction /usr/sbin/dnsmasq | "Do not use any abstractions in profiles"; the profile starts in complain mode |
| 2 | exercise the program, then (S)can | include reload, shutdown and error paths |
| 3 | (N)ew for each path; profile or child for each exec | "Should AppArmor enable secure-execution mode when switching profiles?": yes |
| 4 | edit by hand | reasons as comments, deny, audit deny, peer= |
| 5 | aa-enforce dnsmasq, then aa-audit dnsmasq | "Setting %s to enforce mode.", "Setting %s to audit mode."; aa-audit --remove ends the watch: "Removing audit mode from %s." |
| 6 | local/usr.sbin.dnsmasq | read through include if exists <local/usr.sbin.dnsmasq> |
aa-logprof -a ("Accept silently all rules") is the opposite of this procedure.
Review checklist
| Check | Shipped line | Deck |
|---|---|---|
| Symlinks: does the rule name the resolved path? | /{usr/,}bin/{,iputils-}ping (bin.ping:15) | Symlinks: rules see through them |
Hard links: l where others can write | /tmp/ntp* rwl, (usr.sbin.ntpd:43) | Hard links and name-based rules |
| /proc and /sys: self, pids, device paths | @{sys}/devices/@{pci_bus}/**/config r, (nvidia_modprobe:30-31) | Writing rules for /sys and /proc |
| Chroots: paths as seen from the task's root | /var/lib/ntp@{run}/ntp/ntpd.pid w, (usr.sbin.ntpd:57) | Symlinks: rules see through them |
| Write globs next to DAC-bypass capabilities | /var/log/** rw, with capability dac_override, (sbin.syslogd:42) | The limits of name-based mediation |
Known discrepancies
aa-logprof -aandpx: in allow-all mode the secure-execution question is answered "no", so an exec to a profiled target is written as lower-casepxalthough the interactive default isPx(ui.py:159-160, ui.py:518-526, aa.py:884-887). Read from the code, not run.- zgrep allows and denies the same file:
/etc/passwd r,anddeny /etc/passwd r,(zgrep:22, zgrep:42). The deny decides; the allow is dead. Read from the file, not run. - An
audit denyfile rule in a complain-mode profile appears to be allowed by the kernel (verdict unclear).whorelies onaudit denyto cancel writes fromabstractions/wutmp. Test deny rules in enforce mode. Read from the code, not run.
for AppArmor developers for security researchers
Code
- Capability check:
profile_capablecapability.c:121-166; quiet handling and the one-second cache inaudit_capscapability.c:70-106; hookapparmor_capablelsm.c:199-211. ioprio_setasks forCAP_SYS_ADMINbeforeCAP_SYS_NICE: ioprio.c:47.- Audit flag: unpacked as
AUDIT_ALLpolicy_unpack.c:1241; applied to permissions lib.c:374-380; file records file.c:122-123, file.c:142-149. O_PATH:do_dentry_openreturns at open.c:951-956, beforesecurity_file_openat open.c:977.- Utilities: mode changes tools.py:196-243; exec questions aa.py:622-661 and aa.py:863-900;
(G)loband(N)ewaa.py:1248-1287; allow-all answers ui.py:159-160, ui.py:518-526; droppedAUDITevents logparser.py:303-304;--no-abstractionaa-genprof:72.
Roads from here
FILE ACCESS
- Exec modes — Px and Cx and ix
QUALIFIERS
- allow and deny — deny rules and what they silence
- The audit qualifier — audit on rules and aa-audit on profiles
RULE CLASSES
- Capability rules — the capability rule
PROFILE STRUCTURE
- Child profiles — child profiles for helpers
IN PRACTICE · general-purpose apps
- Profiles for general-purpose apps — the broad style and why it is not enough here
- Choosing abstractions — which abstractions are narrow enough
IN PRACTICE · security-sensitive programs
- Symlinks: rules see through them — an exact rule must name the path the kernel resolves
- Hard links and name-based rules — what the l permission in a writable directory gives away
- Writing rules for /sys and /proc — exact rules for /proc and /sys
SECURITY · threat model and limits
- What is not mediated — operations no rule can cover
RUNNING THE PARSER
- Reading a denial — reading the records