How the kernel checks an access

a profile as a state machine for everyone

A real profile compiled into a state machine, and the kernel walking it character by character to answer one request. The widget under the deck lets you walk the same machine with your own paths.

On the map: start here

▶ Watch this deck — 10 scenes, ~8 min

Part of journeys

01 One question, on every open

HOW THE KERNEL CHECKS AN ACCESS open("/etc/demo.conf") demo the kernel: may it? yes or no on every open, so it must be fast profile demo /usr/bin/demo { audit /etc/demo.conf r, /etc/demo/* r, deny /etc/demo/secret r, owner /run/demo/** rw, } the profile: four file rules no profile text in the kernel apparmor_parser one small machine built when the profile is loaded this is what the kernel walks This deck shows the real machine of these four rules. Under the deck you can walk it yourself.
demo opens its configuration file and the kernel must answer yes or no; the profile text with four file rules never reaches the kernel; apparmor_parser turns it into one state machine that the kernel walks

The kernel holds no profile text. apparmor_parser compiles the file rules into one state machine when the profile is loaded, and each check is a walk over it.

Demo opens its configuration file, and the kernel has one question to answer: may it? That question comes up on every open, so the answer has to be quick. Here is the profile we'll use, with four file rules. You might picture the kernel reading those lines and trying one rule after another. It doesn't. There is no profile text in the kernel. Before the program ran, the parser turned the rules into one small machine, and that is what the kernel walks. This deck shows you that machine, the real one, and it's for everyone. One scene near the end is for people who read the code.

Details and sources

Details

  • The profile used in this deck, complete:
abi <abi/5.0>,

profile demo /usr/bin/demo {
  audit /etc/demo.conf r,
  /etc/demo/* r,
  deny /etc/demo/secret r,
  owner /run/demo/** rw,
}
  • Everything drawn in this deck comes from compiling that profile with the parser of the AppArmor tree and reading the tables back from the binary policy it writes with -S. The steps and the full state table are in the reference under the deck.
  • The check for an open starts at the file_open hook, apparmor_file_open (lsm.c:465), and ends in __aa_path_perm (file.c:233-251), which walks the machine and compares the result with the request.
  • An unconfined process is not checked: the hook tests if (!unconfined(label)) first (lsm.c:488).

Read more

for AppArmor developers for security researchers

In the code

  • The file rules are compiled by process_profile_regex; the file machine is created at parser_regex.c:797.
  • The same profile also yields a 15-state machine for the attachment /usr/bin/demo and a 5-state machine for the rule classes. This deck draws only the file machine.

02 Four rules, one machine

FOUR RULES, ONE MACHINE audit /etc/demo.conf r, /etc/demo/* r, deny /etc/demo/secret r, owner /run/demo/** rw, 1 / 2 e 3 t 5 c 7 / 9 d 11 e 13 m 15 o 17 . 19 c 22 o 26 n 28 f 30 read, audit / 20 23 not / read else s 24 e 27 c 29 r 31 e 32 t 33 read denied r 4 u 6 n 8 / 10 d 12 e 14 m 16 o 18 / 21 25 not / any owner: read, write 34 states 33 that a path can reach, and state 0: no match one step per character of the path
The four file rules and the single state machine compiled from them: 34 states, with the three rules under /etc/demo sharing their first states and the /run/demo rule branching off after the first slash

All file rules of a profile become one machine: 34 states here. Rules with a common beginning share states. A check costs one step per character of the path, whatever the number of rules.

The parser reads all four rules and builds a single machine from them. Not four machines: one. Rules that begin the same way share their beginning. Three of ours start with /etc/demo, so they walk the same states and split only where they differ: one goes on to the config file, one into the directory, and the deny rule takes a side track there. The fourth rule branches off right after the first slash. That makes thirty-four states. And however many rules a profile has, a check takes one step per character of the path, whether there are four rules or four thousand.

Details and sources

Details

  • The machine has 34 states: 33 that some path can reach, and state 0, which means no match. The parser reports it with -D dfa-stats: Created dfa: states 34 and Dfa minimization no states removed: partitions 34.
  • States 1, 2, 3, 5, 7, 9, 11, 13, 15 and 17 spell /etc/demo and are shared by three rules. States 4 to 18 (even numbers) spell /run/demo.
  • The numbers in the circles are the state numbers of the compiled table; the widget under the deck uses the same numbers.
  • The cost of a check is the length of the path: the kernel does one lookup per byte and has no loop over rules (match.c:518-548).
  • The number of rules still matters for the size of the machine and for the time the parser needs to build it.

Read more

for AppArmor developers for security researchers

In the code

03 States and edges

STATES AND EDGES 1 a state start: nothing read yet / an edge: one character 2 read so far: / e 3 read so far: /e could become /etc/… r 4 read so far: /r could become /run/… state 0: no match h, or anything else: no edge for it nothing leads back out of state 0
State 1, the start, has one edge labelled slash to state 2; state 2 has edges e to state 3 and r to state 4; any other character leads to state 0, which means no match

A state stands for what has been read so far; an edge says where to go for one character. The walk starts in state 1. A character without an edge leads to state 0, and state 0 has no way out.

A state is a circle. It stands for what has been read so far. An edge is an arrow with a character on it: if the next character of the path is this one, go there. We begin in state one, with nothing read. Its only edge is a slash. State two has two edges: e, on the way to /etc, and r, on the way to /run. And if the next character is something else, an h for /home? There's no edge for it. That drops us into state zero, which means no rule can match any more. Nothing leads back out of it.

Details and sources

Details

  • The start state is 1 and the no-match state is 0: DFA_START and DFA_NOMATCH (match.h:17-18).
  • State 1 has one edge, /. State 2 has two, e to state 3 and r to state 4. Every other byte in states 1 and 2 leads to state 0.
  • A machine like this has exactly one next state for every state and every character. That is what makes it deterministic: there is nothing to choose and nothing to retry.
  • State 0 leads only to itself, so once the walk is there the answer is fixed.

Read more

  • The full table of states and edges is in the reference under the deck.

for AppArmor developers for security researchers

In the code

  • "No edge" is not a special case in the kernel. Each state has a default next state, used for every byte it has no entry for; for most states of this machine the default is 0 (match.c:443-455).

04 Walking a path

ONE CHARACTER, ONE STEP open("/etc/demo.conf") the kernel reads the path it built, left to right 1 / 2 e 3 t 5 c 7 / 9 d 11 e 13 m 15 o 17 . 19 c 22 o 26 n 28 f 30 14 characters, 14 steps: the walk stops in state 30 no going back, no second attempt open("/home/user/.ssh/id_rsa") 1 / 2 h state 0 and there the walk stays, to the end of the path
The path /etc/demo.conf read one character at a time: fourteen characters lead from state 1 through fourteen edges to state 30; a path under /home drops to state 0 at its second character

The kernel reads the path one byte at a time and follows one edge per byte. /etc/demo.conf takes 14 steps and ends in state 30. A path no rule covers ends in state 0.

Now watch the kernel walk. Demo opens /etc/demo.conf. The kernel reads that path from the left, one character at a time. Slash: state one to state two. Then e, t, c: three more steps. For each character it follows one edge, and that's all. It doesn't go back, and it doesn't try alternatives. Fourteen characters, fourteen steps, and the walk stops in state thirty. Now a path that no rule covers, a key under /home. Slash, then h, and we are in state zero. That's where the walk stays, all the way to the end of the path.

Details and sources

Details

  • The walk for /etc/demo.conf: 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 22, 26, 28, 30.
  • The walk for /home/user/.ssh/id_rsa: 1, 2, then 0 for each of the remaining 20 characters. The kernel does not stop early; it reads the whole path (match.c:541-546).
  • The path that is matched is the one the kernel builds for the opened object: absolute, with symbolic links already followed. It is not the string the program passed to open() (path.c:202).
  • A directory's name gets a trailing slash, so the directory /etc/demo is matched as /etc/demo/ (path.c:167-177).

Read more

for AppArmor developers for security researchers

In the code

  • aa_str_perms calls aa_dfa_match(file_rules->dfa, start, name) (file.c:221-231). The loop is while (*str): the terminating NUL ends the walk.
  • The start state is rules->file->start[AA_CLASS_FILE] (file.c:244), which is 1 unless the policy carries a dfa_start field (policy_unpack.c:1029-1031).
  • Matching at run time covers the other entry points, such as aa_dfa_match_len and aa_dfa_leftmatch.

05 A wildcard is a loop

A WILDCARD IS A LOOP /etc/demo/* r, 17 …/etc/demo / 20 not / 23 s: a side track not / m, o, t, d: four times around / state 0 /etc/demo/motd ends in state 23 /etc/demo/sub/x drops out at the second slash owner /run/demo/** rw, 18 …/run/demo / 21 not / 25 any character slashes too /run/demo/cache/db ends in state 25 * is one or more characters, none of them a slash. ** is one that is not a slash, then anything.
The star rule: after /etc/demo/ any character except a slash leads to state 23, which loops on any character except a slash, and a slash leads to state 0. The two-star rule: state 25 loops on every character

* compiles to a state that loops on every character except /; ** to one that loops on every character. Both need at least one character first, so /etc/demo/ itself matches neither.

What about wildcards? A star becomes a loop. After /etc/demo and one more slash, we're in state twenty. Any character except a slash takes us on to state twenty-three, and that state has an edge that leads straight back to itself: any character except a slash, stay here. m, o, t, d: four times around. A slash has no edge here, so a file one directory deeper drops to state zero. That is all it means that a star doesn't cross a slash. Two stars make the same shape with a wider loop: in state twenty-five every character, slashes too, keeps you there.

Details and sources

Details

  • /etc/demo/motd: the walk reaches state 20 after /etc/demo/, state 23 with m, and stays in 23 for o, t, d.
  • /etc/demo/sub/x: s leads to state 24 (the first state of the side track for secret), u to 23, b stays, and the second / has no edge: state 0.
  • /etc/demo/ alone stops in state 20, which carries no permissions: * and ** at the end of a rule both need at least one character after the slash.
  • /run/demo/cache/db: state 21 after /run/demo/, state 25 with c, and every further character, slashes included, stays in 25.
  • The first character after the slash may not be a slash in either rule.

Read more

for AppArmor developers for security researchers

In the code

  • At the end of a pattern and after a slash, * is emitted as [^/\x00] followed by [^/\x00]* (parser_regex.c:204, parser_regex.c:225), and ** as [^/\x00] followed by [^\x00]* (parser_regex.c:220). The NUL byte is kept out of every class because it separates the parts of multi-part matches.

06 Where the path ends

PERMISSIONS LIVE ON STATES the path ends in state what that state carries /etc/demo.conf 30 read, and audit /etc/demo/motd 23 read /run/demo/cache/db 25 read and write (for the owner) /etc/demo/ 20 nothing Only the state where the path ends counts. A double ring marks a state that carries permissions.
Four paths and the states they end in: /etc/demo.conf ends in state 30 which carries read and audit, /etc/demo/motd in state 23 with read, /run/demo/cache/db in state 25 with read and write for the owner, and /etc/demo/ in state 20 which carries nothing

Permissions are attached to states. Only the state the path ends in counts: 30 carries read (with audit), 23 read, 25 read and write for the owner, and most states nothing.

Reading the path gives the kernel a state number, and nothing more. The answer is attached to that state. Some states carry permissions; they're drawn with a double ring. State thirty, where the config file ends, carries read. State twenty-three, the star loop, carries read too. State twenty-five carries read and write. Most states carry nothing. Stop in state twenty, having read the directory name and its slash but no file name, and nothing is allowed. So only the state where the path ends counts. The states you passed on the way there don't.

Details and sources

Details

  • Nine of the 34 states carry permissions: 30 (/etc/demo.conf), 23 and the five states 24, 27, 29, 31, 32 (names under /etc/demo/), 25 (anything under /run/demo/), and 33 (/etc/demo/secret, where the deny rule applies).
  • A rule that says r gives more than the read bit: the compiled entry also allows opening the file and reading its attributes. rw adds write, append, create, delete, and changing attributes, mode and owner.
  • The kernel's decision is one comparison: is every requested permission in the allowed set of the final state (file.c:246-247)?

Read more

for AppArmor developers for security researchers

In the code

  • The final state is an index into the accept table, whose value is an index into the profile's permission table (aa_lookup_condperms, file.c:194-209). The table for this machine has 12 entries; the reference under the deck lists them.
  • The permission bits are in perms.h:16-39. The entry of state 23 has allow 0x244: AA_MAY_GETATTR, AA_MAY_OPEN, MAY_READ.
  • Permissions in the DFA shows how the parser computes these sets.

07 Owner, other and deny

OWNER, OTHER AND DENY owner /run/demo/** rw, 25 owner: read, write other: nothing the file belongs to the user demo runs as fsuid = ouid the file belongs to anyone else fsuid ≠ ouid deny /etc/demo/secret r, a side track inside /etc/demo/* 20 …/etc/demo/ s e c r e 24 27 29 31 32 each of these still allows read t 33 read taken out one more 23 the * loop: read
State 25 carries two permission sets, read and write for the owner and nothing for others. The deny rule adds a chain of states spelling secret inside the star rule: each still allows read except the last, and one more character returns to the star loop

Each state has an owner set and an other set; the kernel picks by comparing the process's fsuid with the file's owner. A deny rule compiles to states of its own in which the permission is absent.

Two refinements. First, owner. Each state really carries two sets of permissions: one for when the file belongs to the user demo runs as, and one for when it doesn't. State twenty-five has read and write in the owner set and nothing in the other. The kernel compares the two user IDs and picks. Second, deny. The deny rule adds a side track inside the star rule: s, e, c, r, e, t. Every state along it still allows read, because a file called sec is just a file. Only the last one, the exact name, has read taken out. One more character, and you're back in the loop.

Details and sources

Details

  • Owner: the kernel compares the filesystem uid of the process (fsuid in the log) with the uid that owns the file (ouid). Equal: the owner set. Different: the other set.
  • State 25: the owner set has read and write; the other set allows neither. A rule without owner puts the same permissions in both sets.
  • Deny: the states reached by /etc/demo/s, se, sec, secr and secre are 24, 27, 29, 31 and 32. All of them carry read, from the * rule. State 33, reached by /etc/demo/secret, does not.
  • /etc/demo/secrets leaves state 33 for state 23 with its last character: read is allowed again.
  • In the compiled entry of state 33 there is no "deny" mark. Read is simply missing from the allowed set, and it is marked quiet, which matters for the log (next scene).

Read more

for AppArmor developers for security researchers

In the code

  • aa_lookup_condperms returns perms[index] when uid_eq(subj_uid, cond->uid) and perms[index + 1] otherwise, for states whose accept2 entry has ACCEPT_FLAG_OWNER (file.c:202-206). The subject uid is current_fsuid() (file.c:227); cond->uid is the inode's owner (lsm.c:492-496).
  • State 33 uses entries 2 and 3: allow 0x200 (AA_MAY_GETATTR only), quiet 0x244, deny 0.

08 The decision and the log line

THE DECISION, AND THE LOG what demo asked for what the state allows the result read /etc/demo.conf read is in the allowed set the open goes ahead apparmor="AUDIT" operation="open" … profile="demo" name="/etc/demo.conf" … requested_mask="r" write /etc/demo.conf write is not in the set the open fails: EACCES apparmor="DENIED" operation="open" … name="/etc/demo.conf" … requested_mask="w" denied_mask="w" read /etc/demo/secret read is not in the set the open fails: EACCES no log line: the deny rule marked read as quiet The lines are composed from the kernel's audit format; … stands for fields left out. Try other paths in the widget under this deck.
Three requests: reading /etc/demo.conf is allowed and logged as AUDIT; writing it fails with EACCES and is logged as DENIED with the name and the refused permission; reading /etc/demo/secret fails and nothing is logged

Requested permissions outside the allowed set fail with EACCES and are logged as DENIED. An allowed access is logged as AUDIT only if the rule said audit. A deny rule refuses without a log line.

Now the kernel holds two things: what demo asked for, and what the state allows. It compares them. Demo asked to read its config file. Read is allowed, so the open goes ahead. That rule also said audit, so the kernel writes a log line marked AUDIT. Had demo asked to write, write isn't in the set. The open fails with permission denied, and the log line says DENIED, with the name and the permission that was refused. And the secret file? Refused too, but silently: a deny rule also marks the permission as quiet. Under this deck there's a widget where you can try all of it yourself.

Details and sources

Details

  • The three lines, in full, as the kernel's format gives them for these requests (composed, not captured; pid and comm are invented, and a real record starts with audit: type=1400 audit(time:serial):):
apparmor="AUDIT" operation="open" class="file" profile="demo" name="/etc/demo.conf" pid=4242 comm="demo" requested_mask="r" fsuid=1000 ouid=0
apparmor="DENIED" operation="open" class="file" profile="demo" name="/etc/demo.conf" pid=4242 comm="demo" requested_mask="w" denied_mask="w" fsuid=1000 ouid=0
  • Reading /etc/demo/secret returns EACCES and writes nothing. Writing audit deny in the rule would bring the line back.
  • A path that no rule covers is refused and logged: the entry used for it has nothing marked quiet.
  • This is enforce mode. In complain mode the same refusal is let through and logged as ALLOWED.
  • In a denial, requested_mask holds only the refused part of what was asked.

for AppArmor developers for security researchers

In the code

  • __aa_path_perm: if (request & ~perms->allow) e = -EACCES; then aa_audit_file (file.c:246-250).
  • aa_audit_file: an allowed request is masked with perms->audit and logged as AUDIT_APPARMOR_AUDIT if anything remains (file.c:119-130); a refused one is reduced to the bits outside allow, loses its quiet bits, and is logged if anything remains (file.c:131-150).
  • The fields are written by audit_pre (audit.c:93-141) and file_audit_cb (file.c:45-67).

09 Four arrays for AppArmor developers for security researchers

FOUR ARRAYS pos = base[state] + c if check[pos] == state: state = next[pos] else: state = default[state] match_char, in short: one step of the walk state 17 reads / (byte 47) base[17] = 4 4 + 47 = 51 check[51] = 17 ours next[51] = 20 the new state state 20 reads m (byte 109) base[20] = 6 6 + 109 = 115 check[115] = 22 not ours default[20] = 23 the new state after the last byte, in state 23 accept[23] = 8 accept2[23]: owner and other pair perms[8] for the owner, perms[9] for others 34 states · next and check hold 275 entries each · the permission table holds 12
The lookup of match_char on the base, check, next and default arrays, worked for state 17 reading a slash and state 20 reading m, then the accept table turning state 23 into an entry of the permission table

match_char: pos = base[state] + c; if check[pos] == state the next state is next[pos], otherwise default[state]. accept[state] then indexes the permission table.

If you read the code: the machine is four arrays, and one step is a few lines in a macro called match_char. Take the state's entry in base, add the byte, and look at that position in check. If check holds our state number, next gives the new state. If it doesn't, default does. In state seventeen, a slash lands on position fifty-one; check says seventeen, so next says twenty. In state twenty, an m misses, and default says twenty-three. After the last byte, accept turns the final state into an index into the permission table, and a flag in accept2 marks an owner and other pair.

Details and sources

Details

  • match_char is at match.c:443-455. base_idx keeps the low 24 bits of a base entry (match.c:23); the high bits are flags (match.h:196-202).
  • If the base entry has MATCH_FLAG_DIFF_ENCODE, a miss repeats the lookup from the default state with the same byte instead of ending the step. The tables of this machine do not use it: their header flags are 0.
  • Rows overlap in next and check: each state's row is placed at an offset (base) where its entries do not collide with others, and check records the owner of each slot. For this machine 34 states with 256 possible bytes each fit in 275 slots.
  • States 20, 23, 24, 27, 29, 31, 32 and 33 have default 23; states 21 and 25 have default 25. They carry explicit entries for / and for the NUL byte that lead to state 0.
  • accept[state] is the index of the state's permission entry; with ACCEPT_FLAG_OWNER in accept2[state] (policy.h:61-63) the next entry is the one for non-owners (file.c:197-206).
  • Each permission entry is an aa_perms record unpacked from 14 u32 fields (policy_unpack.c:901-923).
  • The kernel verifies the tables when the policy is loaded, so the walk needs no bounds checks.

10 Where to go from here

ROADS How the kernel checks an access From profile text to kernel the whole pipeline Globbing every wildcard File rules paths and permissions Building the DFA how the parser makes states Permissions in the DFA deny, audit, priority Matching at run time the kernel code Reading a denial every field of the line
Seven roads from this deck: the pipeline, globbing, file rules, building the DFA, permissions in the DFA, matching at run time and reading a denial

Next: the pipeline for where the machine comes from, globbing and file rules for the language, the DFA engine decks for the compiler, matching at run time for the kernel code, reading a denial for the log.

That's a check, start to finish: one machine, one step per character, one state at the end, and its permissions against what was asked. From here, the pipeline shows where this machine is built and how it reaches the kernel. Globbing and file rules cover what you can write in a profile. If you want to know how the parser makes the states, and how deny, audit and priority become permissions, there are two decks in the DFA engine district. Matching at run time is the kernel code itself. And reading a denial takes the log line apart field by field.

Details and sources

Details

  • The widget under this deck uses the tables of the compiled profile. Its walk, its choice of permission entry and its log lines follow the kernel functions named in the earlier scenes.
  • The same kind of machine answers the other checks as well: capabilities, network, mount, signals and the rest are matched by a second machine in the same profile, and attachment by a third.

Read more

The profile

abi <abi/5.0>,

profile demo /usr/bin/demo {
  audit /etc/demo.conf r,
  /etc/demo/* r,
  deny /etc/demo/secret r,
  owner /run/demo/** rw,
}

The four file rules compile to one machine with 34 states. Everything below was read back from the binary policy that apparmor_parser writes for this profile.

Every state

"Reached by" is the shortest input that leads to the state. A byte that is not listed leads to state 0. "Accept" is the state's index into the permission table; for the other half add 1.

StateReached byEdgesAcceptCarries
0(no match)none: every byte stays in 00
1(start)/ → 20
2/e → 3, r → 40
3/et → 50
4/ru → 60
5/etc → 70
6/run → 80
7/etc/ → 90
8/run/ → 100
9/etc/d → 110
10/run/d → 120
11/etc/de → 130
12/run/de → 140
13/etc/dem → 150
14/run/dem → 160
15/etc/demo → 170
16/run/demo → 180
17/etc/demo. → 19, / → 200
18/run/demo/ → 210
19/etc/demo.c → 220
20/etc/demo/[^\x00/s] → 23, s → 240
21/run/demo/[^\x00/] → 250
22/etc/demo.co → 260
23/etc/demo/a[^\x00/] → 238r
24/etc/demo/s[^\x00/e] → 23, e → 278r
25/run/demo/a[^\x00] → 256owner rw
26/etc/demo.con → 280
27/etc/demo/se[^\x00/c] → 23, c → 298r
28/etc/demo.conf → 300
29/etc/demo/sec[^\x00/r] → 23, r → 318r
30/etc/demo.confnone10r audit
31/etc/demo/secr[^\x00/e] → 23, e → 328r
32/etc/demo/secre[^\x00/t] → 23, t → 338r
33/etc/demo/secret[^\x00/] → 232deny r

The permission table

Entries come in pairs: the even one is used when the file's owner is the user the process runs as, the odd one otherwise. getattr is present in every entry.

EntryallowauditquietUsed by
0getattrstates without permissions (25 of them), owner
1getattrstates without permissions (23 of them), other
2getattrread, open, getattrstates 33, owner
3getattrread, open, getattrstates 33, other
4getattrread, open, getattrno state
5getattrread, open, getattrno state
6write, read, append, create, delete, open, setattr, getattr, chmod, chownstates 25, owner
7getattrstates 25, other
8read, open, getattrstates 23, 24, 27, 29, 31, 32, owner
9read, open, getattrstates 23, 24, 27, 29, 31, 32, other
10read, open, getattrread, open, getattrstates 30, owner
11read, open, getattrread, open, getattrstates 30, other

Requests to try

demo runs as user 1000. "owner" means the file belongs to user 1000, "other" that it belongs to user 0. Each row was computed from the compiled tables with the kernel's rules; the widget shows the same results.

RequestFile isFinal stateEntryOutcomeLog record
read /etc/demo.confother3011allowed and loggedapparmor="AUDIT"
write /etc/demo.confother3011deniedapparmor="DENIED"
read /etc/demo/motdother239allowednone
read /etc/demo/sub/xother00deniedapparmor="DENIED"
read /etc/demo/secretother333denied quietlynone
read /etc/demo/secretsother239allowednone
read /etc/demo/other201deniedapparmor="DENIED"
write /run/demo/cache/dbowner256allowednone
write /run/demo/cache/dbother257deniedapparmor="DENIED"
read /home/user/.ssh/id_rsaowner00deniedapparmor="DENIED"
exec /usr/bin/demo-helperother00deniedapparmor="DENIED"

How the kernel decides

  • The file_open hook maps the open flags to a request: read, write, append, create.
  • The kernel builds the path of the object and walks the machine from state 1, one byte per step.
  • The final state's accept value, plus 1 if the file belongs to someone else, selects a permission entry.
  • If the request has a bit that is not in allow, the open fails with EACCES.
  • Allowed: a log record (AUDIT) only for bits that are in audit.
  • Refused: a log record (DENIED) unless every refused bit is in quiet.

for AppArmor developers for security researchers

Reproducing the machine

tools/head-parser -Q -K --skip-kernel-load -D rule-exprs -D dfa-stats -D dfa-states -D compressed-dfa demo.sd
tools/head-parser -S -K demo.sd > demo.bin
tools/matching-demo.py demo.sd demo.bin > site/matching-demo.json
  • -D dfa-states prints the states and their edges, -D compressed-dfa the packed tables, -S the binary policy. The feature set is abi/5.0.
  • tools/matching-demo.py unpacks the file machine from the binary policy and walks it with a port of match_char.
  • Header of the compiled tables: flags 0 (no diff-encoding), no equivalence classes; accept, accept2 and base are 32 bits wide, default, next and check 16 bits; next and check have 275 entries.

for AppArmor developers for security researchers

The packed tables

TableValues
base7:1, 8:2, 10:2, 11:2, 12:3, 14:2, 15:1, 16:2, 17:4, 18:5, 19:6, 20:6, 21:7, 22:4, 23:8, 24:17, 25:1, 26:9, 27:9, 28:4, 29:10, 31:19, 32:11, 33:12; 0 for the rest
default20:23, 21:25, 23:23, 24:23, 25:25, 27:23, 29:23, 31:23, 32:23, 33:23; 0 for the rest
accept23:8, 24:8, 25:6, 27:8, 29:8, 30:10, 31:8, 32:8, 33:2; 0 for the rest
accept20 for states 0 and 1, 1 for all others

Entries of next and check that belong to a state (position: check → next):

  1: 25 → 0     6: 20 → 0     7: 21 → 0     8: 23 → 0     9: 27 → 0 
 10: 29 → 0    11: 32 → 0    12: 33 → 0    17: 24 → 0    19: 31 → 0 
 47:  1 → 2    48:  7 → 9    49:  8 → 10   50: 17 → 19   51: 17 → 20
 52: 18 → 21   53: 20 → 0    54: 21 → 0    55: 23 → 0    56: 27 → 0 
 57: 29 → 0    58: 32 → 0    59: 33 → 0    64: 24 → 0    66: 31 → 0 
 99:  5 → 7   100:  9 → 11  101:  2 → 3   102: 10 → 12  103: 11 → 13
104: 12 → 14  105: 19 → 22  106: 28 → 30  108: 27 → 29  109: 13 → 15
110:  6 → 8   111: 14 → 16  112: 15 → 17  113: 16 → 18  114:  2 → 4 
115: 22 → 26  116:  3 → 5   117:  4 → 6   118: 24 → 27  119: 26 → 28
120: 31 → 32  121: 20 → 24  124: 29 → 31  127: 32 → 33

Roads from here

START HERE

FILE ACCESS

RUNNING THE PARSER

IN THE KERNEL

THE DFA ENGINE · libapparmor_re