allow and deny

which rule wins for application developers

allow is the default access type of a rule. deny removes permissions that other rules of the same or lower priority grant and by default leaves no log record. The decision is made per permission.

On the map: qualifiers

▶ Watch this deck — 8 scenes, ~6 min

Worth seeing first

Part of journeys

01 Everything in the directory, except one file

ALLOW AND DENY demo /usr/bin/demo /etc/demo/demo.conf /etc/demo/tls/cert.pem /etc/demo/** r, read /etc/demo/secret.key matched by the same pattern deny /etc/demo/secret.key r, grants takes away: nothing is granted by a deny the order of the two lines does not matter
The program demo reads three files under /etc/demo. One rule allows the whole directory; a second rule, deny /etc/demo/secret.key r, cuts off the secret key while the other two files stay readable.

deny removes permissions that other rules grant. Rule order in the file has no effect.

Demo reads its configuration from one directory, and a single rule covers all of it. Then someone drops a secret key into that directory. Now what? You could rewrite the pattern so it no longer matches the key, and that gets ugly fast. It's easier to say it directly: allow the directory, deny the key. A deny rule doesn't grant anything. It takes away what other rules grant, so the key is cut off while the other files stay readable. And it doesn't matter which of the two lines comes first in the file.

Details and sources

Details

  • Syntax: the word deny in front of a rule, as in deny /etc/demo/secret.key r,.
  • A deny rule grants nothing. It removes the permissions it names from what allow rules of the same or lower priority grant for the names it matches.
  • The order of the rules in the file has no effect: all rules of a profile are compiled together and the decision is made per name and per permission.
  • The manual page: deny "specifies that permissions requests that match the rule should be denied without logging" (apparmor.d.pod:2561-2565).
  • The same shape in the parser's tests: /usr/bin/** r, with deny /usr/bin/foo r, (ok_deny_3.sd).
  • What the program sees: the open(2) of the key fails with EACCES. In the kernel a file request is refused when it asks for a permission that is not in the allow set of the matched name (file.c:246-247).
  • The program demo, its files and its profile are invented for the map. Nothing on this slide was observed on a running kernel; the kernel behaviour is stated from the kernel source.

for AppArmor developers for security researchers

In the code

  • Compiled with the tree's parser and -D dfa-states-initial, the state for the key is (0x 0/10004//0/0/10004) in the format (allow/deny//prompt/audit/quiet): nothing allowed, read denied and quiet for both the owner and the other half.
  • In the grammar deny is one of the alternatives of opt_rule_mode (parser_yacc.y:700-703).

02 Four seats in front of a rule

GRAMMAR priority=N audit allow deny prompt owner | other RULE , 1 2 3: the access type 4 deny /etc/demo/secret.key r, allow /etc/demo/** r, audit deny owner /var/lib/demo/** w, accepted deny audit /etc/demo/** w, wrong order: syntax error deny allow /etc/demo/** r, one access type per rule: syntax error nothing written = allow the same seats work on every rule class, with a few exceptions noted in each deck
Syntax diagram of the four qualifier seats in fixed order: priority=N, audit, the access type allow, deny or prompt, and owner or other, followed by the rule. Three accepted examples and two rejected ones: deny audit and deny allow.

[priority=N] [audit] [allow|deny|prompt] [owner|other] RULE,. No access type means allow.

Every rule can start with up to four qualifiers, and they have fixed seats. Priority comes first, then audit, then the access type, then owner or other. This deck is about the third seat. It holds one word: allow, deny or prompt. Here are three rules that use the seats in order. Because the seats are fixed, audit deny is right and deny audit is a syntax error. And one seat holds one word, so you can't write deny allow. Leave the seat empty and the rule is an allow rule. The same four seats stand in front of every kind of rule, with a few exceptions.

Details and sources

Details

  • Order of the qualifiers: [priority=N] [audit] [allow|deny|prompt] [owner|other] RULE,. The order is fixed.
  • The third seat holds at most one word. With nothing written the rule is an allow rule.
  • The qualifiers come before the optional word file and before the keyword of any other rule class: deny file /a w,, deny capability chown,.
  • Run with the parser built from the AppArmor tree at 836052d21 (-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel): deny audit /etc/demo/** w, is rejected: syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR; deny allow /foo r, is rejected: syntax error, unexpected TOK_ALLOW, expecting TOK_ID or TOK_MODE or TOK_SET_VAR.
  • The full form of the message is AppArmor parser error for FILE in profile FILE at line N: MESSAGE.
  • "With a few exceptions": set rlimit rules take no qualifier at all, also not through a block; the message is prefix not allowed on rlimit rules (parser.h:171-178, bad_rlimit_block_deny.sd). This is from the parser source and its test file.
  • The grammar line of the manual page, ACCESS TYPE = ( 'allow' | 'deny' ), omits prompt (apparmor.d.pod:198); the parser accepts three words.

Read more

for AppArmor developers for security researchers

In the code

03 allow is what you already write

allow allow /etc/demo/** r, = /etc/demo/** r, /var/lib/demo/** r, /var/lib/demo/** w, allow /var/lib/demo/state k, for /var/lib/demo/state: r w k allow rules accumulate nothing matching = nothing allowed: the default is to refuse deny { allow /etc/demo/** r, } conflicting mode prefix the word matters in one place: inside a deny { } block (see qualifier blocks)
allow /etc/demo/** r, equals the same rule without the word allow. Three allow rules for the same file add up to the permissions r, w and k. A name that no rule matches is refused.

allow RULE, equals RULE,. Permissions of allow rules matching the same name are combined.

You've been writing allow rules all along. Putting the word allow in front changes nothing about what the rule grants. Allow rules that match the same file simply add up: read from one, write from another, lock from a third, and demo's state file ends up with all three. Anything no rule mentions is refused, so there's no need to deny it. Why does the keyword exist, then? Partly for symmetry. And inside a qualifier block the explicit word counts as a statement: it conflicts with a deny around it, where a rule with nothing in front just takes the block's deny.

Details and sources

Details

  • allow RULE, grants exactly what RULE, grants. The manual page: "This is the default value for rules and does not need to be specified" (apparmor.d.pod:2550-2554).
  • Allow rules of the same priority that match the same name add up their permissions. Compiled with the parser built from the AppArmor tree at 836052d21 (-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel), /var/lib/demo/state rw, together with allow /var/lib/demo/state k, gives one name with read, write, append and lock.
  • Anything no rule mentions is refused. In the kernel a file request is denied when it asks for a bit outside the allow set (file.c:246-247); for the other rule classes the test is request & (~perms->allow | perms->deny) (lib.c:430). Read from the source, not run.
  • The explicit word matters in one place, a qualifier block. deny { allow /etc/demo/** r, } is rejected with conflicting mode prefix; deny { /etc/demo/** r, } is accepted and the rule becomes a deny rule.
  • A block that replaces the default access type, from the parser's tests: deny { capability chown, audit capability kill, } (ok_block1.sd).

Read more

for AppArmor developers for security researchers

In the code

  • A rule with allow and a rule with no access type both become a MatchFlag node, or an ExactMatchFlag for a pattern without globs (aare_rules.h:85-92).
  • Accumulation per permission bit: perms.allow |= match->perms & amask; perms.audit |= match->audit & amask; (hfa.cc:1638-1639).
  • The conflict check: an explicit rule mode that differs from the block's is conflicting mode prefix; an unspecified one takes the block's (rule.h:409-416 for rule classes, parser_misc.c:1111-1118 for file rules).
  • The state for the example, -D dfa-states-initial: (0x b802e/0//0/0/0).

04 deny wins, one permission at a time

deny /var/lib/demo/** rw, deny /var/lib/demo/archive/** w, /var/lib/demo/archive/2025.db allow r allow w the deny names only w r stays open(".../archive/2025.db", O_RDONLY) allowed open(".../archive/2025.db", O_WRONLY) EACCES a read-only archive same priority: deny beats allow a higher priority= on the allow turns that around (see syn-priority)
Two rules, /var/lib/demo/** rw and deny /var/lib/demo/archive/** w, both match a file in the archive. The allow lights the r and w permissions, the deny strikes only w. Opening for reading is allowed, opening for writing fails with EACCES.

At equal priority a deny overrides an allow for the permissions the deny names; other permissions of the allow remain. The refusal is what the file check of the kernel does with a missing allow bit.

How exactly does a deny win? Not rule against rule, but permission by permission. Demo may read and write its state, and a second rule denies write under archive. For a file in the archive both rules match. The first grants read and write. The deny names write, so write is gone. It doesn't name read, so read stays. Open that file for reading and it works; open it for writing and you get EACCES. The result is a read-only archive. That's the normal case, where both rules have the same priority. Give the allow rule a higher priority and it wins instead; that's the priority deck.

Details and sources

Details

  • The decision is made for one permission of one name at a time, not rule against rule.
  • At equal priority a deny overrides an allow for the permissions the deny names. With /a rw, and deny /a w,, read stays allowed; write and append are refused. w includes append, so a denied w also removes a.
  • A higher priority on the allow rule reverses it: with priority=1 /a rw, and deny /a w,, read and write are allowed. With priority=1 on both, the deny wins again.
  • A lower priority on the deny makes it a default that more specific rules override: with /a* rw, and priority=-1 deny /** w,, names matching /a* keep read and write.
  • The manual page on priority: rules with a higher priority "completely override permissions of lower priority rules where they overlap" (apparmor.d.pod:2539-2548).
  • The refusal: in the kernel the file check computes the permissions of the name and returns EACCES when the request has a bit that is not allowed (file.c:233-251). Read from the source, not run.
  • The check is made when the file is opened, for the access mode of that open. Opening an archive file read-only succeeds; opening it for writing fails.

for AppArmor developers for security researchers

In the code

  • pri_update_perm scores each matching node per bit: priority << 2, plus 3 for a deny node, 2 for an exact match on an exec bit, 1 for an allow node, 0 for prompt (hfa.cc:1529-1554). The highest score keeps the bit.
  • A winning deny node: perms.deny |= match->perms & amask; perms.quiet |= match->audit & amask; and the bit is cleared from allow, audit and prompt (hfa.cc:1585-1594).
  • State dumps (-D dfa-states-initial, r for both halves is 0x10004, w with append 0x2800a): /a rw, with deny /a w, gives (0x 10004/2800a//0/0/2800a); priority=1 /a rw, with deny /a w, gives (0x 3800e/0//0/0/0).

05 A deny leaves no trace

DENIED, QUIETLY no rule for /home/user/.ssh/id_rsa demo EACCES the log apparmor="DENIED" operation="open" ... name="/home/user/.ssh/id_rsa" ... requested_mask="r" denied_mask="r" ... deny /etc/demo/secret.key r, demo EACCES the log (nothing) a deny rule = "not allowed" + "do not log" flags=(complain) still refused, still silent (code of the kernel) want the record? write audit deny (the audit deck)
Two refusals side by side. With no rule for a path, the open is refused and a DENIED record is logged. With a deny rule, the open is refused and nothing is logged, and by the kernel code that is also so when the profile is in complain mode.

deny refuses without a log record. Reading the kernel code of the kernel, this holds in complain mode as well; it was not tested on a running kernel. audit deny logs the refusal.

There are two ways for an access to be refused: no rule allows it, or a rule denies it. To the program they feel the same: permission denied. To you they don't. A refusal for lack of a rule is logged. A refusal by a deny rule is not. Deny means: I know about this, don't tell me. That's handy for silencing noise, and confusing when you forget the rule is there. One more consequence, read from the code of the kernel: in complain mode, where missing rules are only logged, a plain deny still refuses, silently. To get a log record, write audit in front.

Details and sources

Details

  • Kernel side, read from the kernel source and not observed on a running kernel.
  • No rule allows the request: in enforce mode EACCES and a record with apparmor="DENIED"; in complain mode the request is allowed and the record says apparmor="ALLOWED" (audit.c:200-236).
  • A deny rule: EACCES and no record, in enforce and in complain mode. The denied permissions are marked quiet; the kernel removes quiet bits from the request before logging and returns the error when nothing is left (file.c:142-149 for files, lib.c:440-447 for the other rule classes, capability.c:87-92 for capabilities).
  • A request that mixes a quieted permission with one that is merely not granted is judged on what remains after the quiet bits are removed; in complain mode the whole request is then permitted. Derived by reading.
  • audit deny does not mark the permissions quiet, so the refusal is logged.
  • The kernel's audit mode overrides the quieting for every profile: echo -n noquiet > /sys/module/apparmor/parameters/audit, or apparmor.audit=noquiet on the kernel command line (apparmor.pod:222-235). The modes are normal, quiet_denied, quiet.allowed, quiet, noquiet and all (audit.c:20-27); noquiet and all ignore the quiet marks (file.c:143-146).
  • The manual page describes explicit denials in complain mode in other terms (apparmor.d.pod:609-616): it speaks of denial information carried by the kernel. By the code read here, what keeps a plain deny in force in complain mode is the quiet mark.

for security researchers

Attack surface

  • A plain deny hides probing. The refused request returns EACCES and leaves no record, so a confined program that probes a denied path is not visible in the log (file.c:143-149; read from the kernel source, not observed on a running kernel). Shipped profiles use plain deny to silence noise; the absence of DENIED records is not evidence that nothing was attempted. audit deny, or the kernel audit mode noquiet, brings the records back.
  • In complain mode a plain deny still refuses, silently, because the quiet bits are removed from the request before the complain decision; an audit deny in complain mode is permitted and logged as ALLOWED (derived from the same code, not run; see audit).
  • The compiled policy carries no deny set: with and without -O filter-deny the deny field written for the kernel is 0, and a denial is a bit missing from allow plus a bit in quiet (run with the parser built from the AppArmor tree at 836052d21, nothing loaded into a kernel, -D compressed-dfa; hfa.h:132-138). The kernel's test of perms->deny for the other rule classes (lib.c:430) therefore has nothing to match in policy built by this parser.
  • A deny wins only at equal or higher priority. An included file with priority= on an allow rule overrides a deny written without one: priority.

Read more

for AppArmor developers for security researchers

In the code

  • A plain deny gets audit_t::IMPLIED, audit deny gets UNSPECIFIED (rule.h:426-440); IMPLIED compares equal to FORCE (rule.h:207-211), so the emitters pass the permissions as the fourth argument, which on a deny node is the quiet mask.
  • Complain mode in the file path: complain |= ~(perms->allow | perms->deny) (file.c:117-118); aa_audit turns an AUDIT_APPARMOR_ALLOWED record into success with complain_error, but a fully quieted request has already returned.
  • The module parameter audit (lsm.c:1974-1976).

06 Deny never reaches the kernel as deny for AppArmor developers for security researchers

COMPILER: DENY MatchFlag /var/lib/demo/** rw prio 0 DenyMatchFlag .../archive/** w quiet=w prio 0 allow: 0*4 + 1 = 1 deny: 0*4 + 3 = 3 deny wins the w bit hfa.cc:1536-1554 (allow/deny//prompt/audit/quiet) (r / w // 0 / 0 / w) apply_and_clear_deny hfa.cc:685 -O filter-deny, on by default (r / 0 // 0 / 0 / w) map_perms_to_accept hfa.h:132 allow, audit|quiet, prompt aa_perms, the kernel allow = r deny = 0 quiet = w plain deny: audit_t::IMPLIED == FORCE, so the perms are passed as the quiet mask audit deny: UNSPECIFIED, so quiet stays empty
The write bit of one DFA state: a MatchFlag node scores 1, a DenyMatchFlag node scores 3 and wins. The state holds allow r, deny w, quiet w. apply_and_clear_deny zeroes deny, map_perms_to_accept emits allow, audit or quiet and prompt, and the kernel's aa_perms gets allow r, deny 0, quiet w.

Scores: priority*4 + 3 deny, +1 allow (parser/libapparmor_re/hfa.cc:1536-1554). apply_and_clear_deny and map_perms_to_accept leave allow and quiet; the packed deny field is 0.

A deny rule becomes a DenyMatchFlag node carrying its permissions and a quiet mask. When a state is built, each bit keeps the highest-scoring node: priority times four, plus three for deny, one for allow. Deny takes the write bit: it clears allow, audit and prompt there and sets deny and quiet. Then apply_and_clear_deny, the filter-deny optimization, zeroes the deny word so states differing only there can merge. map_perms_to_accept emits allow, audit or quiet, and prompt. The kernel has a deny field and would honour it, but this parser always writes zero. What survives is a missing allow bit and a quiet bit.

Details and sources

for AppArmor developers for security researchers

Details

  • A deny rule becomes a DenyMatchFlag(priority, perms, quiet) node (expr-tree.h:1191-1197), created by the node cache next to PromptMatchFlag, ExactMatchFlag and MatchFlag (aare_rules.h:85-92).
  • pri_update_perm (hfa.cc:1529-1554): score priority << 2, +3 deny, +2 exact match on an exec bit, +1 allow, +0 prompt. The comment notes that this is the last use of priority; it is not passed to the back end.
  • A winning deny bit sets perms.deny and perms.quiet and clears allow, audit and prompt (hfa.cc:1585-1594).
  • DFA::apply_and_clear_deny (hfa.cc:685-693) calls the per-state function: allow &= ~deny; prompt &= ~deny; deny = 0;, with audit and quiet left as computed (hfa.h:106-130).
  • It runs when the target has no extended permissions or -O filter-deny is set (aare_rules.cc:273-278). The option is on by default: { 1, "filter-deny", "filter out deny information from final dfa", CONTROL_DFA_FILTER_DENY } (common_optarg.c:104-105), default flags at parser_common.c:123-129.
  • map_perms_to_accept emits accept1 = allow, accept2 = PACK_AUDIT_CTL(audit, quiet), accept3 = prompt (hfa.h:132-138); compute_perms_table_ent builds the permission table entries from those three words (hfa.cc:1406-1421, builders at policy_compat.cc:160-244). None of them sets deny.
  • The packed entry has a deny field, written by sd_serialize_perm (parser_interface.c:346-350); with this parser its value is 0.
  • The kernel has the field and would honour it: u32 deny; /* explicit deny, or conflict if allow also set */ (perms.h:69-71), denied = request & (~perms->allow | perms->deny) (lib.c:430), and complain mode excludes it (lib.c:369-370).
  • Dumps, parser built from the AppArmor tree at 836052d21: -D dfa-states-initial for /a rw, with deny /a w, prints (0x 10004/2800a//0/0/2800a); -D dfa-states-post-filter prints (0x 10004/0//0/0/2800a). The header line of the dump reads (allow/deny/prompt/audit/quiet).
  • Capabilities are bit sets, not pattern rules: allowed is caps.allow & ~caps.deny, quiet is caps.deny & caps.quiet (parser_interface.c:549-558).

Read more

07 Deny rules the parser refuses, and one surprise

COMMON MISTAKES deny audit /etc/demo/** w, write: audit deny /etc/demo/** w, syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR deny /usr/bin/demo-helper Px, write: deny /usr/bin/demo-helper x, Invalid perms, in deny rules 'x' must not be preceded by exec qualifier 'i', 'p', or 'u' deny { allow /etc/demo/** r, } leave out allow, or move the rule out of the block conflicting mode prefix priority=10 allow /etc/demo/override.conf r, deny /etc/demo/** r, accepted override.conf is readable: the allow outranks the deny no record in the log and the program still fails? look for a deny rule, also in included abstractions
Three rejected rules with the parser's message: deny audit in the wrong order, a deny rule with Px instead of a bare x, and an explicit allow inside a deny block. A fourth, accepted pair shows an allow with priority=10 overriding a deny.

audit deny, not deny audit. In deny rules write a bare x. A higher-priority allow overrides a deny.

The mistakes here are mostly about seats. Audit goes before deny, not after. A deny rule for exec takes a plain x, because a denied exec has no profile to choose. An explicit allow inside a deny block contradicts the block, and the parser refuses it. Then the surprise: deny doesn't always win. A rule with a higher priority beats it, so this allow with priority ten makes the override file readable in spite of the deny. And when a program fails with no log record at all, suspect a deny rule somewhere in the profile, or in a file it includes.

Details and sources

Details

  • Messages are from the parser built from the AppArmor tree at 836052d21 (-Q -K --skip-kernel-load --warn=all, nothing loaded into a kernel). The full form is AppArmor parser error for FILE in profile FILE at line N: MESSAGE.
  • deny audit /etc/demo/** w,: syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR. Write audit deny.
  • deny /usr/bin/demo-helper Px,: Invalid perms, in deny rules 'x' must not be preceded by exec qualifier 'i', 'p', or 'u'. Write deny /usr/bin/demo-helper x,.
  • deny { allow /etc/demo/** r, }: conflicting mode prefix.
  • priority=10 allow /etc/demo/override.conf r, with deny /etc/demo/** r,: override.conf is readable, the other names are refused and quiet.
  • Finding a silent deny: echo -n noquiet > /sys/module/apparmor/parameters/audit makes the kernel log refusals that deny rules keep quiet (apparmor.pod:222-235). Deny rules also arrive through include lines, for example from abstractions.
  • A deny rule on files does not carry the link and change_profile permissions; priority= has no effect on capability rules, so a deny capability rule cannot be overridden that way.

for AppArmor developers for security researchers

In the code

08 Where to go from here

ROADS priority=N audit allow|deny|prompt owner | other allow adds, deny removes quietly syn-priority let an allow win syn-audit log the denial syn-prompt the third access type syn-owner syn-other limit a rule to some files one deny for many rules, and what happens after syn-qualifier-blocks deny a group of rules adm-denials reading the log re-perms merging per state syntax-map every kind of line
The four qualifier seats with a road under each: syn-priority under priority=N, syn-audit under audit, syn-prompt under the access type, syn-owner and syn-other under owner or other. Further roads: syn-qualifier-blocks, adm-denials, re-perms and syntax-map.

Next: syn-audit (log a denial), syn-priority (override a deny), syn-qualifier-blocks (deny a group of rules).

Allow adds. Deny removes, and keeps quiet about it. The other seats refine that. Priority decides between an allow and a deny when you don't want the deny to win. Audit in front of deny brings the log record back. Prompt is the third word that fits in this seat. Owner and other limit a rule to some files. And a qualifier block puts one deny in front of a whole group of rules. From there, the denials deck shows how to read what does get logged.

Details and sources

Details

  • allow is the default access type; allow rules that match the same name add up.
  • deny removes the permissions it names from allow rules of the same or lower priority, and the refusal is not logged.
  • audit deny logs the refusal; a higher priority= on an allow rule overrides a deny.
  • In the kernel, read from the source, a plain deny also refuses silently in complain mode.
  • Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2), the kernel-rc2 (97d73c825).

Read more

Basis of the statements on this page: "tree" means the parser built from the AppArmor commit above was run on the fragment (-Q -K --skip-kernel-load --warn=all), with nothing loaded into a kernel; "code" means the parser or kernel source was read, not run. Nothing here was observed on a running kernel. Kernel statements are for mainline Linux 7.4.

Grammar

QUALIFIERS:  [priority=N] [audit] [allow|deny|prompt] [owner|other]
RULE LINE:   [QUALIFIERS] RULE ,
  • The order is fixed. Any other order is a syntax error (tree).
  • The access type seat holds at most one of allow, deny, prompt. With nothing written the rule is an allow rule (code).
  • The qualifiers come before the optional word file and before the keyword of any other rule class: deny file /a w,, deny capability chown,.
  • audit deny is the only way to combine the two words; deny audit is rejected (tree).

Keywords

KeywordMeaning
allowThe default access type, spelled out. allow RULE, grants what RULE, grants. The explicit word conflicts with a surrounding deny { } or prompt { } block; a rule with no access type takes the block's.
denyRemoves the named permissions from what allow rules of the same or lower priority grant. Grants nothing. Without audit it also marks the permissions quiet, so the refusal is not logged. audit deny logs it: syn-audit.
promptThe third access type: syn-prompt.

Allow rules of the same priority that match the same name add up their permissions (code).

Which rule wins

The decision is made for one permission of one name at a time, not rule against rule. The order of the rules in the file has no effect. Scoring: syn-priority.

RulesResultBasis
/a rw, and deny /a w,read allowed; write and append refused and quiettree
/etc/demo/** r, and deny /etc/demo/secret.key r,the key is refused and quiet; other names readabletree
priority=1 /a rw, and deny /a w,read and write allowed; nothing quiettree
priority=10 allow /etc/demo/override.conf r, and deny /etc/demo/** r,override.conf readable; other names refused and quiettree
priority=1 /a rw, and priority=1 deny /a w,as the first row: at equal priority the deny winstree
/a* rw, and priority=-1 deny /** w,names matching /a* keep read and write; elsewhere write is refused and quiettree

What a refusal looks like

Kernel side, from the source of mainline Linux 7.4 (code); none of it was run.

CauseEnforce modeComplain mode
no rule allows the requestEACCES and a DENIED recordallowed, with an ALLOWED record
deny ruleEACCES, no recordEACCES, no record
audit deny rulesee syn-auditsee syn-audit
  • The complain-mode row for deny follows from the quiet bit: the kernel removes quieted bits from the request before logging, and returns the error when nothing is left (linux:security/apparmor/file.c:143-149 for files, lib.c:440-447 for other classes, capability.c:87-92 for capabilities).
  • A request that mixes a quieted bit with a bit that is merely not granted is judged on the remaining bits after the quiet bits are removed; in complain mode the whole request is then permitted. Derived by reading; not run.
  • The audit modes noquiet and all ignore the quiet masks, so quieted denials are logged (/sys/module/apparmor/parameters/audit; linux:security/apparmor/file.c:144-145). Reading the records: adm-denials.

Other rule classes

  • deny is accepted on every rule class except rlimit rules, which take no qualifier (code).
  • Capabilities and the legacy network table are bitmasks, not pattern rules: allowed is allow & ~deny, quiet is deny & quiet (code). priority= has no effect on capability rules.
  • A deny rule on files does not carry the link and change_profile bits (code).
  • In a deny rule an exec permission is a bare x; there is no transition to choose (tree).

Compiler option

OptionDefaultEffect
-O filter-denyonremoves the denied bits from allow and prompt and clears the deny word of each state before minimization, so states that differ only in deny can merge
-O no-filter-denykeeps the deny word during minimization

With either setting the deny field written to the kernel is 0 (tree: -D compressed-dfa prints every table entry with deny 0x0; code: parser/libapparmor_re/hfa.h:132-138). A deny rule reaches the kernel as a bit missing from allow and a bit set in quiet.

Errors and warnings

A parse error is printed as AppArmor parser error for FILE in profile FILE at line N: MESSAGE (tree).

MessageCause
syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VARdeny audit /etc/demo/** w,: write audit deny (tree)
syntax error, unexpected TOK_ALLOW, expecting TOK_ID or TOK_MODE or TOK_SET_VARdeny allow /foo r,: one access type per rule (tree)
Invalid perms, in deny rules 'x' must not be preceded by exec qualifier 'i', 'p', or 'u'deny /usr/bin/demo-helper Px,: write a bare x (tree)
conflicting mode prefixa rule's explicit access type against its block's, as in deny { allow /etc/demo/** r, } (tree)
prefix not allowed on rlimit rulesan rlimit rule inside a deny { } block (tree; parser/tst/simple_tests/rlimits/bad_rlimit_block_deny.sd)

Differences from the man page

Known discrepancies between apparmor.d(5) at the commit above and the code:

  • The grammar line ACCESS TYPE = ( 'allow' | 'deny' ) (parser/apparmor.d.pod:198) omits prompt; the parser accepts three words.
  • The complain-mode paragraph on explicit denials (parser/apparmor.d.pod:608-616) speaks of explicit denial information carried by the kernel and stripped by -O filter-deny. By the code read here the parser does not emit that information with or without the option; what keeps a plain deny in force in complain mode is the quiet mask.

for AppArmor developers for security researchers

Code map

StepWhere
grammar: opt_rule_mode is empty, TOK_ALLOW, TOK_DENY or TOK_PROMPTparser/parser_yacc.y:700-703; seat order 705-711
keywordsparser/parser_misc.c:100-101
field prefixes.rule_modeparser/rule.h:244
prefix_rule_t::add_prefix (rule classes): conflict check, IMPLIED for deny, UNSPECIFIED for audit denyparser/rule.h:409-440
generic_add_prefix (file and change_profile entries)parser/parser_misc.c:1111-1142
audit_t::IMPLIED compares equal to FORCEparser/rule.h:207-211
MatchFlag, ExactMatchFlag, DenyMatchFlagparser/libapparmor_re/aare_rules.h:85-92, expr-tree.h:1191-1197
pri_update_perm: score priority*4 + 3 deny, 2 exact exec, 1 allow, 0 promptparser/libapparmor_re/hfa.cc:1529-1653
apply_and_clear_denyparser/libapparmor_re/hfa.cc:685-693, hfa.h:106-127; called from aare_rules.cc:273-278
map_perms_to_acceptparser/libapparmor_re/hfa.h:132-138
permission table builders, which do not set denyparser/libapparmor_re/policy_compat.cc:160-246
packed deny fieldparser/parser_interface.c:350
aa_check_perms (Linux 7.4)linux:security/apparmor/lib.c:430-447
aa_audit_file (Linux 7.4)linux:security/apparmor/file.c:143-153

Dump format (-D dfa-states-initial): (allow/deny//prompt/audit/quiet) in hex. The doubled / is a known defect of the dump. Bits per half: x 0x1, w 0x2, r 0x4, a 0x8; the other half is shifted left by 14, so r for both halves is 0x10004, w (which implies a) is 0x2800a and rw is 0x3800e.

RulesStateBasis
/a rw, deny /a w,(0x 10004/2800a//0/0/2800a), after filter-deny (0x 10004/0//0/0/2800a)tree
/etc/demo/** r, deny /etc/demo/secret.key r,, state for the key(0x 0/10004//0/0/10004)tree
priority=1 /a rw, deny /a w,(0x 3800e/0//0/0/0)tree
priority=10 allow /etc/demo/override.conf r, deny /etc/demo/** r,, state for override.conf(0x 10004/0//0/0/0)tree

Roads from here

START HERE

FILE ACCESS

PROFILE HEAD

QUALIFIERS

RUNNING THE PARSER

THE DFA ENGINE · libapparmor_re