Mount rules

mounting filesystems for application developers for administrators for security researchers

A mount rule allows mount calls that match a source a mount point a filesystem type and a set of mount flags. options=( ) is an exact set of flags and options in ( ) is a set of optional ones. The kernel sorts each call into remount bind propagation change move or new mount and compares only the fields that kind has. A refusal is logged as operation mount with the field that failed.

On the map: rule classes

▶ Watch this deck — 9 scenes, ~6 min

Worth seeing first

Part of journeys

01 demo mounts a scratch area

MOUNT RULES mount("tmpfs", "/run/demo", "tmpfs", MS_NOSUID, NULL) demo kernel /run/demo/ nothing mounted = -1 EACCES no mount rule in the profile mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, line 14 of demo's profile with the rule: tmpfs type flags source mount point the call also needs:capability sys_admin, remount, umount and pivot_root are rules of their own

A mount rule describes the mount(2) calls a profile allows: type, flags, source and mount point. The program also needs capability sys_admin,.

Demo wants a private scratch area for its worker, so at start-up it mounts a tmpfs on /run/demo. Under a profile, that call fails with EACCES unless a mount rule matches it. This is line fourteen of demo's profile. The rule describes the call in four parts: the filesystem type, the mount flags, the source and, after the arrow, the mount point. The rule doesn't make demo privileged: it still needs the sys_admin capability. And changing a mount, unmounting and pivot_root are separate rules with their own decks.

Details and sources

Details

  • The LSM hook is sb_mount for mount(2) (lsm.c:707-742, registered at lsm.c:1792) and move_mount for move_mount(2) (lsm.c:744-758). An unconfined task is not checked.
  • A confined profile is checked only if it mediates the mount class (mount.c:254-255, mount.c:313-314). It does when it was compiled for a feature set that has mount: the feature file /sys/kernel/security/apparmor/features/mount/mask reads mount umount pivot_root (apparmorfs.c:2650). Then a mount call without a matching rule is refused with EACCES.
  • mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, is line 14 of demo's profile. It and the three lines after it (remount, umount, pivot_root) compile together with no warning (run with the tree's parser). The program demo and its profile are invented for the map.
  • The rule does not replace the capability: mount(2) needs CAP_SYS_ADMIN, which a confined task has only with capability sys_admin,. Without it the call fails with EPERM. mount(2) asks AppArmor first, so a call with no matching rule is refused with EACCES and logged whether or not the task has the capability (namespace.c:4279-4283).
  • Kernel statements are read from the kernel source and were not run.

02 Conditionals, source, arrow, mount point

GRAMMAR QUALIFIERSmountfstype=TYPEoptions=(FLAGS)SOURCE-> MOUNTPOINT, mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, fstype=tmpfs fstype=(ext4, xfs) vfstype is the same word options=(rw, nosuid) options in (ro, nodev) option is the same word mount, any mount mount /dev/sdb1, one path alone is the source mount -> /mnt/**, any source onto these mount points conditionals come before the paths; owner is refused
The syntax diagram of a mount rule: optional qualifiers, the word mount, an optional fstype conditional, an optional options conditional, an optional source, an optional arrow and mount point, a comma. Examples of fstype and options values, and three short rules: mount alone allows any mount, mount with one path names the source, mount with an arrow and a path names the mount point.

mount [fstype=...] [options=... | options in ...] [SOURCE] [-> MOUNTPOINT],. Everything after the keyword is optional.

After the keyword come the conditionals, and a mount rule knows two: fstype, for the filesystem type, and options, for the mount flags. Options takes either an equals sign or the word in, and that difference matters enough to get its own slide. Then comes the source, then an arrow and the mount point. Everything is optional. The bare keyword allows any mount. One path on its own is the source. An arrow with one path names only the mount point. Just remember that the conditionals have to come before the paths.

Details and sources

Details

  • The conditional names are fstype, vfstype, options and option. The operators are = and in. options:=ro is a scanner error: Lexer found unexpected character: ':' (0x3a) in state: MOUNT_MODE.
  • fstype values are patterns (fstype=ext*, fstype={procfs,sysfs}). Several fstype conditionals in one rule are collected into one list of alternatives, and for fstype the operators = and in mean the same (run with the tree's parser).
  • Source, mount point and fstype take variables.
  • A conditional after the arrow is refused. The mount point is not checked to be an absolute path: mount options=(rw) /a -> b, is accepted.
  • Known discrepancy, on the bug list as "A source-only mount rule also allows mounting anything onto that path": mount /dev/sdb1, also allows bind mounts, moves and remounts onto /dev/sdb1. Write the mount point (mount /dev/sdb1 -> /mnt/**,) or options=( ); options in does not avoid it.
  • The source word detached stands for a mount that is not attached anywhere yet, as seen at move_mount(2). The feature file is /sys/kernel/security/apparmor/features/mount/move_mount, which reads detached (apparmorfs.c:2651).
  • Seats: audit, allow, deny, prompt and priority= are taken; owner and other are refused with owner prefix not allowed on mount rules.
  • Examples in the tree: ok_3.sd, ok_opt_combo_1.sd.

for AppArmor developers for security researchers

In the code

03 One system call, five kinds of mount

WHAT THE KERNEL COMPARES demo mount(source, target, type, flags) the kernel which kind? remount MS_REMOUNT mount point · flags bind MS_BIND mount point · source · bind or rbind propagation MS_SHARED MS_PRIVATE ... mount point · propagation flags move MS_MOVE mount point · source new mount none of the above mount point · source · type · flags the first four: no filesystem type is compared mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, a rule with fstype= covers new mounts only
demo's mount call enters the kernel, which sorts it by its flags into one of five kinds: remount, compared on mount point and flags; bind, compared on mount point, source and bind or rbind; propagation change, compared on mount point and propagation flags; move, compared on mount point and source; and new mount, compared on mount point, source, type and flags. No filesystem type is compared for the first four. demo's rule names fstype, so it covers new mounts only.

The kernel tests MS_REMOUNT, MS_BIND, the propagation flags and MS_MOVE in that order; anything else is a new mount. Only new mounts have a filesystem type to compare.

Mount is one system call that does five different jobs, and the kernel decides which by looking at the flags, in a fixed order: remount, bind, a change of propagation such as private or shared, move, and if it's none of those, a new mount. Each kind carries different information. Only a new mount has a filesystem type. A remount or a propagation change has no source. So a rule only covers the kinds it can describe. Demo's rule names a type, which makes it a rule about new mounts and nothing else. A bind mount onto the same directory would need its own rule.

Details and sources

Details

  • Before sorting, the kernel drops the flags MS_KERNMOUNT, MS_NOSEC, MS_ACTIVE and MS_BORN (mount.h:26, lsm.c:718). The order of the tests is MS_REMOUNT, MS_BIND, the propagation flags, MS_MOVE, else a new mount (lsm.c:707-742).
  • What each kind passes on for comparison: - remount (aa_remount): the mount point, no source, no type, all flags (mount.c:328-353); - bind (aa_bind_mount): the mount point, the resolved source path, the flags cut down to MS_REC | MS_BIND (mount.c:355-394); - propagation (aa_mount_change_type): the mount point, the flags cut down to MS_REC | MS_SILENT | MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE (mount.c:396-422); - move (aa_move_mount_old): the mount point, the source path, MS_MOVE (mount.c:424-474); - new mount (aa_new_mount): the mount point, the type, all flags, and as source a resolved path if the filesystem type needs a device, otherwise the source string as the caller gave it (mount.c:476-542).
  • An unknown filesystem type returns ENODEV before any policy check (mount.c:492-497).
  • move_mount(2) goes to aa_move_mount; a mount that is not attached has no source name (mount.c:444-450).
  • Which kinds a rule covers. With no options: every kind the rule's other parts allow (remount only without source and fstype; bind and move only without fstype; propagation only without fstype and not with both paths). With only options in: the same kinds, each only if its own flag (remount, bind or rbind, a propagation flag, move) is in the list. With options=: the kind named by remount, by bind or rbind, by a propagation flag or by move, else a new mount (mount.cc:1089-1123).
  • A bind rule compares bind or rbind only: mount options=(bind ro) /a -> /b, does not require ro (run with the tree's parser; the compiled flags are the bind byte alone).
  • Kernel statements are read from the kernel source and were not run.

for security researchers

Attack surface

  • The hooks AppArmor registers for this class are move_mount, sb_mount, sb_umount and sb_pivotroot (lsm.c:1791-1794). None of the other calls of the new mount API (fsopen, fsconfig, fsmount, open_tree, mount_setattr) has an AppArmor hook. A mount built with them is checked only when move_mount attaches it, as a move with the source word detached.
  • Known discrepancy, on the bug list as "Kernel: a bind mount made with open_tree and move_mount is checked as a move without a source, so deny mount options in (bind) does not stop mount --bind": current mount(8) makes bind mounts this way, so a rule about bind mounts, allow or deny, does not apply to them, and a rule that allows moves onto the target allows them. See Known bugs.
  • Filesystem-specific options (the data argument of mount(2), for example size= or upperdir=) cannot be written in a rule and are not compared: the parser refuses any word outside its flag table (mount.cc:492-496).

04 options= is exact, options in is a choice

FLAGS mount options=(rw, nosuid) ... required: nosuid · every other flag: absent rw asks for nothing a call with the flags nosuidallowed nosuid, nodevrefused (no flags)refused mount options in (ro, nosuid, nodev) ... each flag may be there or not a call with the flags (no flags)allowed nosuid, nodevallowed nosuid, noexecrefused mount options=(nosuid) options in (nodev) tmpfs -> /run/demo/, nosuid required, nodev optional two options=( ) in one rule are two alternatives, not one bigger set
Left: mount options=(rw, nosuid) requires nosuid and no other flag; a call with nosuid is allowed, a call with nosuid and nodev is refused, a call with no flags is refused. Right: mount options in (ro, nosuid, nodev) makes each flag optional; a call with no flags is allowed, a call with nosuid and nodev is allowed, a call with nosuid and noexec is refused. Below: options=(nosuid) with options in (nodev) requires nosuid and makes nodev optional.

options=(a b): exactly these flags. options in (a b): any of these flags or none. Both together: required plus optional.

This is the part of mount rules that surprises people. options= is exact. Demo's rule says rw and nosuid, so the call must carry nosuid and no other flag. And rw isn't a flag at all: it's the absence of read-only, so it adds nothing. Pass nodev as well and the call is refused. Options in is a choice: any of the listed flags may be present, and none of them is fine too. You can use both in one rule, one for what must be there and one for what may be. And if you write options= twice, you get two alternatives.

Details and sources

Details

  • The "allowed" and "refused" results on the slide are derived from the compiled flags and the kernel's comparison (mount.c:132-143); nothing was run on a kernel. The compiled flags were produced with the tree's parser: demo's rule requires nosuid; mount options in (ro, nosuid, nodev) fstype=tmpfs tmpfs -> /run/demo/, makes the three flags optional; mount options=(nosuid) options in (nodev) tmpfs -> /run/demo/, requires nosuid and makes nodev optional.
  • The words that clear a flag (rw, suid, dev, exec, async, atime, ...) add nothing to options= and only take part in the conflict check. In options in they make their flag optional, the same as the setting word: options in (ro, atime) makes ro and noatime optional.
  • No options at all means every flag is optional. options= with only clearing words (options=(rw)) means a mount with no flags.
  • The flags a kind of call does not have are ignored by the rule for that kind; see "One system call, five kinds of mount".
  • verbose, silent and loud share one flag. The full table of flag words and their synonyms (r, read-only, B, R, M, make-private, ...) is in the reference below.
  • The kernel compares the flags of the call after the cuts it makes for each kind of call.
  • Known discrepancy, on the bug list as "options in (...) also matches no flags and the opposite words; apparmor.d(5) says it does not": apparmor.d(5) says mount options in (ro,atime) /dev/foo -> /mnt/, does not match a mount with no flags or with rw,noatime (apparmor.d.pod:1745-1765); the compiled rule accepts both.
  • Known discrepancy, on the bug list as "nouser is accepted and has no effect, and no mount rule can match flag bit 31": nouser is accepted and has no effect, and no rule, mount, included, matches a call with that flag set (mount.h:72-92).
  • Kernel statements are read from the kernel source and were not run.

for AppArmor developers for security researchers

In the code

05 The log names the field that failed

THE DENIAL mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo, no slash at the end demo mount(...) = -1 EACCES apparmor="DENIED" operation="mount" class="mount" info="failed mntpnt match" error=-13profile="demo" name="/run/demo/" pid=19085 comm="demo" fstype="tmpfs" srcname="tmpfs" flags="rw, nosuid" /run/demo/ tmpfs tmpfs nosuid 1 mount point 2 source 3 type 4 flags failed mntpnt match failed srcname match failed type match failed flags match all four match but the rule does not allow:failed perms check mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, with the slash the mount goes through
A mount rule whose mount point /run/demo has no slash at the end. The kernel compares mount point, source, type and flags in that order; the mount point of the call is /run/demo/ and fails. demo gets EACCES. The log record has operation mount, info failed mntpnt match, name /run/demo/, fstype tmpfs, srcname tmpfs and flags rw, nosuid. The info texts name the first field that failed. With the slash the mount goes through.

Refused mounts return EACCES and are logged as operation="mount" with name= (mount point), srcname=, fstype=, flags= and info="failed ... match". Directory mount points end in /.

Suppose the rule was written without the slash at the end of the mount point. Demo's mount fails with EACCES. The log record says operation="mount", and it carries everything the kernel compared: name is the mount point, then the type, the source and the flags. Best of all, info says which field failed first. The kernel checks the mount point, then the source, then the type, then the flags. Here it's the mount point, because the kernel's name for a directory ends in a slash and the rule's doesn't. Add the slash and the mount goes through.

Details and sources

Details

  • The record on the slide is assembled, not captured: the field order comes from the fixture testcase_mount_01.in and class="mount" after operation from testcase_remount_01.in; the values are demo's and are illustrative.
  • The info texts are failed mntpnt match, failed srcname match, failed type match, failed flags match, failed data match and failed perms check (mount.c:145-153). The comparison order is fixed (mount.c:159-211). failed perms check means every field matched a rule that lacks the mount permission, for example a deny rule.
  • The mount fields are fstype=, srcname=, trans=, flags="..." and options=; each is printed only when it has a value (mount.c:92-118). For a tmpfs the source is the string the caller passed, so srcname="tmpfs".
  • The flags text starts with rw or ro and has no word for lazytime (mount.c:31-85).
  • A directory is named with a trailing / (mount.c:214-221), so -> /run/demo, does not match and -> /run/demo/, does.
  • The source of a new mount is a resolved path only for filesystem types that need a device. The type is the string the caller passed, so fstype=fuse does not match a request for fuse.sshfs.
  • In complain mode the record is ALLOWED with the same info (testcase_mount_02.in).
  • error=-13 is EACCES.
  • Kernel statements are read from the kernel source and were not run.

06 Four fields and a run of flag bytes for AppArmor developers for security researchers

IN THE PARSER mount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/, class mnt_rule \x07 /run/demo/ \x00 tmpfs \x00 (tmpfs) \x00 \x02 class 7 mount point source type flags perms 0x2 AA_MAY_MOUNT ro \x01 nosuid \x02 nodev \x03 noexec \x04 ... remount \x06 bind \x0d move \x0e one byte per flag: bit number + 1, lowest bit first options=(ro, nosuid)\x01\x02required: the byte itself options in (ro, nosuid)(\x01|)(\x02|)optional: the byte or nothing do_match_mnt() mntpnt, NUL, srcname, NUL, type, NUL, one byte per set flag, then perms & AA_MAY_MOUNT mount,compiles to five such rules, one per kind of call
demo's mount rule as a match string: the class byte 07, the mount point /run/demo/, a NUL, the source tmpfs, a NUL, the type tmpfs, a NUL and the flag byte 02 for nosuid, with permission 0x2. Each flag is one byte, the bit number plus one. options=(ro, nosuid) gives the bytes 01 02; options in (ro, nosuid) gives each byte or nothing. In the kernel do_match_mnt walks the same sequence and reads the permission.

mnt_rule::gen_policy_re (mount.cc:1129-1192) writes \x07 mount point NUL source NUL type NUL flag bytes; do_match_mnt (mount.c:159-211) walks it.

One class, mnt_rule, serves all four keywords. A mount rule becomes a string in the policydb: the class byte seven, the mount point, a NUL, the source, a NUL, the type, a NUL, and then the flags. Flags aren't a bitmask here. Each flag is one byte, its bit number plus one, written lowest bit first. A required flag is the byte itself; an optional one is the byte or nothing. The kernel builds the same sequence from the call, walking the set bits from bit zero up, and reads the permission at the end. The bare rule expands into five of these, one for each kind of call.

Details and sources

for AppArmor developers for security researchers

Details

  • The match string on the slide was produced with the tree's parser (-D rule-exprs): \x07/run/demo/\x00tmpfs\x00(tmpfs)\x00\x02, permission 0x2. mount, gives five rules.
  • Class AA_CLASS_MOUNT is 7. Permissions: AA_MAY_PIVOTROOT 0x1, AA_MAY_MOUNT 0x2, AA_MAY_UMOUNT 0x4 (mount.h:20-22).
  • build_mnt_flags writes \xNN for a required flag and (\xNN|) for an optional one and, when no flag byte results, (\xfe|) (mount.cc:716-755). The kernel's match_mnt_flags feeds one byte per set bit, from bit 0 up (mount.c:132-143).
  • The generators per kind of call and their flag masks: remount MS_REMOUNT_FLAGS, bind bind|rbind, propagation MS_MAKE_FLAGS, move move, new mount everything except the command flags (mount.h:106-121; mount.cc:817, mount.cc:890, mount.cc:948, mount.cc:996, mount.cc:1043). The choice of generator: mount.cc:1082-1127.
  • More dumps from the tree's parser: mount options=(bind ro) /a -> /b, gives \x07/b\x00/a\x00[^\000]*\x00\x0d; mount options=(rprivate) -> /a, ends in \x00\x0f\x13; mount options=(move) detached -> /b, gives \x07/b\x00\x00[^\000]*\x00\x0e.
  • The class marker \d7 is added at priority 1001 when the feature set has mount (parser_regex.c:1403-1405). Without it the rule is dropped with the warning mount rules not enforce (class rule-not-enforced, off by default; mount.cc:782, mount.cc:1139-1142). The spelling is the parser's; it is on the bug list under "Message typo and a stale message" (Known bugs).
  • Rules of the mount class that differ only in keyword are merged and their permissions ORed (rule.h:540-554, mount.cc:688-714).
  • do_match_mnt: mount.c:159-211.
  • Kernel statements are read from the kernel source and were not run.

for security researchers

Attack surface

  • The kernel has code that matches the data argument of mount(2) as a fifth field (mount.c:195-207, AA_MNT_CONT_MATCH). The parser never writes that field: the member opts is never assigned (mount.cc:503; on the bug list under "Dead code and unused state in the mount rule"). No rule can constrain filesystem-specific options.
  • Merged permissions: a mount and a umount rule with the same paths become one object whose strings carry both bits. The kernel reads the permission at a different point of the string for each operation, and no cross-grant was found by reading.

07 What mount /dev/sdb1, compiles to for AppArmor developers for security researchers

ONE PATH, NO OPTIONS mount /dev/sdb1, apparmor.d(5): "allow mounting of /dev/foo anywhere with any options" bind mount point: any source: /dev/sdb1 move mount point: any source: /dev/sdb1 new mount mount point: any source: /dev/sdb1 type: any propagation mount point: /dev/sdb1 source: any type: any the path is the mount point its flags, each optional: ro sync remount mand dirsync nosymfollow bind move rec silent acl unbindable private slave shared iversion lazytime mount("/dev/sda1", "/dev/sdb1", NULL, MS_BIND, NULL) mount point /dev/sdb1, flag bind: matches that compiled rule (not run on a kernel) mount /dev/sdb1 -> /mnt/**, with a mount point, the propagation rule is not generated
The rule mount /dev/sdb1, which the man page describes as mounting that device anywhere, compiles to four rules. Three, for bind, move and new mount, have /dev/sdb1 as the source and any mount point. The fourth, for propagation changes, has /dev/sdb1 as the mount point, any source, any type and seventeen optional flags including bind and move. A bind mount of another path onto /dev/sdb1 matches that compiled rule; this call was not run on a kernel. With a mount point in the rule the propagation rule is not generated.

mount SOURCE, also produces a rule with SOURCE as the mount point and a wide set of optional flags (mount.cc:936-948, 1097-1099). Read from the compiled rules.

Here is a rule that reads narrower than it compiles. One path and nothing else: the man page describes it as mounting that device anywhere. With no options, the parser generates a rule for every kind of call that fits. Three of them have the path as the source. The fourth is for propagation changes, which have no source, so the path goes in the mount point position. Every flag of that rule is optional, bind and move included. So the policy also matches mounts onto that path. The warning for this is off by default. Naming a mount point, or writing options with the equals sign, avoids that rule.

Details and sources

for AppArmor developers for security researchers

Details

  • Run with the tree's parser (--warn=all -D rule-exprs), mount /dev/sdb1, gives four rules, each with permission 0x2: - bind: \x07[^\000]*\x00/dev/sdb1\x00[^\000]*\x00(\x0d|)(\x0f|) - propagation: \x07/dev/sdb1\x00[^\000]*\x00[^\000]*\x00(\x01|)(\x05|)(\x06|)(\x07|)(\x08|)(\x09|)(\x0d|)(\x0e|)(\x0f|)(\x10|)(\x11|)(\x12|)(\x13|)(\x14|)(\x15|)(\x18|)(\x1a|) - move: \x07[^\000]*\x00/dev/sdb1\x00[^\000]*\x00(\x0e|) - new mount: \x07[^\000]*\x00/dev/sdb1\x00[^\000]*\x00 followed by sixteen optional flag bytes.
  • The same run prints the warning The use of source as mount point for propagation type flags is deprecated. Its class is deprecated, which is off by default (mount.cc:937-938, parser.h:77); --warn=deprecated or --warn=all shows it.
  • The propagation rule is generated when the rule lacks a source or lacks a mount point (mount.cc:1097-1099) and uses the source as mount point when there is no mount point (mount.cc:936-940). Its flags are masked with MS_MAKE_FLAGS, which is every flag except the per-mount ones, not only the propagation flags (mount.cc:948, mount.h:110).
  • mount /dev/sdb1 -> /mnt/**, gives three rules (bind, move, new mount) and no warning (run with the tree's parser).
  • The example call on the slide is derived from the compiled rule and the kernel source; it was not run.

for AppArmor developers for security researchers

Attack surface

  • What the propagation rule matches, with /dev/sdb1 as the mount point: a bind mount of any path (the kernel sends mount point, source and the flag bind, mount.c:371-387), a move, a remount, and a new mount of any type with any of ro sync mand dirsync nosymfollow silent acl iversion lazytime, or no flags.
  • Known discrepancy, on the bug list as "A source-only mount rule also allows mounting anything onto that path": apparmor.d(5) documents the rule as source-only (apparmor.d.pod:1813).
  • Status: confirmed. The bug list has the compiled rules and a kernel run in which a bind mount onto the rule's path succeeded under a profile with only this rule and the capability (Known bugs). The example call on the slide was not run.
  • When auditing profiles, look for mount rules with one path and neither options nor ->. Writing mount options=(...) SOURCE -> MOUNTPOINT, gives one kind of call and no extra rule.

for AppArmor developers for security researchers

Read more

08 Mistakes and their messages

MISTAKES mount options=(rw, nosuid, size=1m) tmpfs -> /run/demo/, unsupported mount option value 'size=1m' only flags; no filesystem options mount options=(rw, ro) tmpfs -> /run/demo/, conflicting flags in the rule two words for one flag mount tmpfs -> /run/demo/ options=(rw), syntax error, unexpected TOK_CONDID, expecting TOK_END_OF_RULE conditionals go before the paths mount options=(remount) /dev/sda1 -> /run/demo/, Encoding of mount rule failed a remount has no source owner mount, owner prefix not allowed on mount rules the first, second and fourth come without file name or line number
Five refused mount rules with the parser's message: a filesystem option size=1m among the options; rw and ro together; a conditional after the paths; a remount with a source; owner on a mount rule. The first, second and fourth messages come without a file name or line number.

Flag words only; no conflicting pairs; conditionals before paths. Encoding of mount rule failed means the rule describes no kind of mount call.

The most common one is trying to write a filesystem option such as a size: rules know mount flags only. Next, two words that fight over the same flag, rw and ro. Then order: conditionals come before the paths. A rule that combines things no call can have, like a remount with a source, fails late, with encoding of mount rule failed. And owner has no meaning here. Be warned that three of these messages don't tell you the line, so check the mount rules you touched last.

Details and sources

Details

  • The parser prints an error as AppArmor parser error for FILE in profile FILE at line N: MESSAGE; the slide shows MESSAGE. All five rows were run with the tree's parser. Row 2 prints conflicting flag values = 1, 1 before the line shown.
  • Known discrepancy, on the bug list as "Mount option errors bypass the parser's error reporting": three messages (unsupported mount option value, conflicting flags in the rule, Encoding of mount rule failed) carry no file, profile or line.
  • Encoding of mount rule failed is followed by ERROR processing policydb rules for profile demo, failed to load. Rules that fail this way: a command flag with fstype (mount options=(rbind) fstype=ext4 /a -> /b,), a propagation flag with both paths (mount options=(rprivate) /a -> /b,), remount alone with a source (mount options=(remount) /run/demo/,).
  • Known discrepancy, on the bug list as "A remount rule with fstype= or with a source compiles to a new-mount rule without the remount flag": with one more flag the same shape is accepted. mount options=(remount, ro) /run/demo/, compiles to a new-mount rule with the flag ro and no remount; see remount rules.
  • An unknown conditional name gives invalid mount conditional with the name, then bad mount rule.
  • mount options=defaults /a -> /b, gives unsupported mount option value 'defaults'. options:= is a scanner error.
  • A mount point without a trailing / is not an error; it does not match a directory.
  • The full table of messages is in the reference below. Failing examples in the tree: the bad_*.sd files in parser/tst/simple_tests/mount.

for AppArmor developers for security researchers

In the code

09 The rest of a mount's life

ROADS mount ... -> /run/demo/, the scratch area is mounted syn-remount make it read-only syn-pivot-root move the root there syn-umount take it down syn-rule-conditionals in and lists syn-capability sys_admin adm-denials the mount record syn-all every mount in one rule sec-unmediated what no rule covers
Mount rules in the centre with roads to syn-remount, syn-pivot-root and syn-umount, to syn-rule-conditionals and syn-capability, and to adm-denials, syn-all and sec-unmediated.

Mount rules allow mounts. Remount, umount and pivot_root rules cover the rest.

Demo's scratch area is mounted. What happens to it next is three more rules: remount to make it read-only, pivot_root to move the worker's root into it, and umount to take it down. The in operator comes from the conditionals deck. The capability deck covers sys_admin, which every one of these calls needs as well. And when a mount is refused, the denials deck shows how to read the record.

Details and sources

Details

  • [QUALIFIERS] mount [CONDITIONALS] [SOURCE] [-> MOUNTPOINT],: each part left out means any.
  • A refused mount returns EACCES and is logged as operation="mount" with the field that failed in info=.
  • Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2) and the kernel tree named on the page. The kernel behaviour was read from the source, not observed on a running kernel.

Read more

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

Grammar

MOUNT RULE:    [QUALIFIERS] mount [CONDITIONALS] [SOURCE] [-> MOUNTPOINT] ,
CONDITIONALS:  fstype=TYPE | fstype=( TYPE ... ) | fstype in ( TYPE ... )
               options=FLAG | options=( FLAG ... ) | options in ( FLAG ... )
QUALIFIERS:    [priority=N] [audit] [allow|deny|prompt]
  • vfstype is the same as fstype; option is the same as options (code).
  • Everything after the keyword is optional. mount, allows any mount; one path alone is the source; -> PATH alone is the mount point (tree).
  • Known discrepancy, on the bug list as "A source-only mount rule also allows mounting anything onto that path": mount /dev/sdb1, also allows bind mounts, moves and remounts onto /dev/sdb1. Write the mount point (mount /dev/sdb1 -> /mnt/**,) or options=( ) (tree; a kernel run is on the bug list, Known bugs).
  • Conditionals come before the paths. A conditional after the arrow is refused (tree).
  • Inside the parentheses blanks or commas separate the words (tree).
  • Source, mount point and fstype are patterns and take variables (code). The mount point is not checked to be an absolute path (tree).
  • The source word detached stands for a mount that is not attached anywhere, as seen at move_mount(2) (code).
SeatOn a mount rule
priority=taken
audittaken
allow, deny, prompttaken: deny is not logged; prompt is in syn-prompt
owner, otherrefused: owner prefix not allowed on mount rules (tree)

Flag words

"Sets" words name a flag that must be (or may be) present. "Clears" words name its absence: they add nothing to options= and make the flag optional in options in. The table is parser/mount.cc:247-311; bit values parser/mount.h:33-81.

SetsClearsmount(2) flagBelongs to
ro, r, read-onlyrw, wMS_RDONLYnew mount, remount
nosuidsuidMS_NOSUIDnew mount, remount
nodevdevMS_NODEVnew mount, remount
noexecexecMS_NOEXECnew mount, remount
syncasyncMS_SYNCHRONOUSnew mount, remount
remountMS_REMOUNTremount
mandnomandMS_MANDLOCKnew mount, remount
dirsyncMS_DIRSYNCnew mount, remount
nosymfollowsymfollowMS_NOSYMFOLLOWnew mount, remount
noatimeatimeMS_NOATIMEnew mount, remount
nodiratimediratimeMS_NODIRATIMEnew mount, remount
bind, BMS_BINDbind
rbind, RMS_BIND and MS_RECbind
move, MMS_MOVEmove
silent, verboseloudMS_SILENTnew mount, remount, propagation
aclnoaclMS_POSIXACLnew mount, remount
unbindable, make-unbindableMS_UNBINDABLEpropagation
runbindable, make-runbindableMS_UNBINDABLE and MS_RECpropagation
private, make-privateMS_PRIVATEpropagation
rprivate, make-rprivateMS_PRIVATE and MS_RECpropagation
slave, make-slaveMS_SLAVEpropagation
rslave, make-rslaveMS_SLAVE and MS_RECpropagation
shared, make-sharedMS_SHAREDpropagation
rshared, make-rsharedMS_SHARED and MS_RECpropagation
relatimenorelatimeMS_RELATIMEnew mount, remount
iversionnoiversionMS_I_VERSIONnew mount, remount
strictatimenostrictatimeMS_STRICTATIMEnew mount, remount
lazytimenolazytimeMS_LAZYTIMEnew mount, remount
nouseruserMS_NOUSERno effect (known discrepancy)
  • A remount rule keeps every flag except move, the propagation flags included (parser/mount.h:114-120).
  • Any other word, a filesystem option such as size=1m or the word defaults included, is refused (tree).

What options mean

The rule hasMeaning
no optionsevery flag is optional; every kind of call the rest of the rule allows
options=(a b)exactly the flags a and b; every other flag absent
options=(rw) (clearing words only)a mount with no flags
options in (a b)any of a and b, or none
options=(a) options in (b)a required, b optional
options=(a) options=(b)two alternatives: exactly a, or exactly b

Kinds of mount call

The kernel tests the flags of mount(2) in this order (linux:security/apparmor/lsm.c:707-742, code).

KindSelected byThe kernel comparesCovered by a rule withExample
remountMS_REMOUNTmount point, flagsoptions= naming remount, no source, no fstype; or a remount rulemount options=(remount, ro) -> /mnt/,
bindMS_BIND in mount(2)mount point, source, bind or rbindoptions= naming bind or rbind, no fstypemount options=(bind) /a/ -> /b/,
propagationMS_SHARED, MS_PRIVATE, MS_SLAVE, MS_UNBINDABLEmount point, propagation flagsoptions= naming a propagation flag, one path, no fstypemount options=(rprivate) -> /mnt/,
moveMS_MOVEmount point, sourceoptions= naming move, no fstypemount options=(move) /a/ -> /b/,
new mountnone of the abovemount point, source, type, flagsany other options=; any rule with fstypemount fstype=tmpfs options=(rw, nosuid) tmpfs -> /run/demo/,

A rule with no options covers every kind its other parts allow; with only options in a kind is covered when its flag is in the list (mount options in (ro, nodev) /a -> /b/, covers new mounts only) (parser/mount.cc:1089-1104, code; tree).

Names the kernel compares

FieldWhat it is
mount pointthe path of the target; a directory ends in /
sourcebind and move: the resolved path; new mount: a resolved path if the filesystem type needs a device, otherwise the string the caller passed; remount and propagation: none; detached for a mount that is not attached
typethe string the caller passed (fuse.sshfs is not fuse); new mounts only

Read from linux:security/apparmor/mount.c:214-221, 328-542 (code).

The log record

FieldMeaning
operation="mount"every kind of mount call, a remount included
class="mount"on current kernels; the fixture below is older
info=failed mntpnt match, failed srcname match, failed type match, failed flags match, failed data match or failed perms check: the first field that did not match
error=-13EACCES
name=the mount point
fstype=the type, when the call has one
srcname=the source, when the call has one
flags=the flags as words, starting with rw or ro

A record from the tree (libraries/libapparmor/testsuite/test_multi/testcase_mount_01.in):

apparmor="DENIED" operation="mount" info="failed mntpnt match" error=-13 ... name="/tmp/.../mountpoint/" ... comm="mount" fstype="ext2" srcname="/dev/loop0/" flags="rw, mand"

Format strings: linux:security/apparmor/mount.c:31-118, 145-153 (code).

Errors and warnings

RuleMessageHas line number
mount options=(rw, nosuid, size=1m) tmpfs -> /run/demo/,unsupported mount option value 'size=1m'no
mount options=defaults /a -> /b,unsupported mount option value 'defaults'no
mount options=(rw, ro) tmpfs -> /run/demo/,conflicting flag values = 1, 1 then conflicting flags in the ruleno
mount tmpfs -> /run/demo/ options=(rw),syntax error, unexpected TOK_CONDID, expecting TOK_END_OF_RULEyes
mount options=(remount) /dev/sda1 -> /run/demo/,Encoding of mount rule failedno
mount options=(rbind) fstype=ext4 /a -> /b,Encoding of mount rule failedno
mount options=(rprivate) /a -> /b,Encoding of mount rule failedno
an unknown conditional nameinvalid mount conditional with the name, then bad mount ruleyes
a conditional after ->mount point conditions not currently supportedyes
mount options:=ro /a -> /b,Lexer found unexpected character: ':' (0x3a) in state: MOUNT_MODEyes
owner mount,owner prefix not allowed on mount rulesyes
mount /dev/sdb1, with --warn=deprecatedwarning The use of source as mount point for propagation type flags is deprecated.yes
any mount rule, no mount in the feature set, --warn=rule-not-enforcedwarning mount rules not enforce

Rows with a rule are from the tree's parser; the others are read from the parser source (code).

Feature files

File under /sys/kernel/security/apparmor/features/ContentMeaning
mount/maskmount umount pivot_rootthe kernel mediates the mount class
mount/move_mountdetachedthe kernel reports a mount that is not attached as source detached

Without mount in the feature set every rule of the mount class is dropped, and the warning for that is off by default (code). See syn-abi and adm-features.

for AppArmor developers for security researchers

In the parser and the kernel

  • One class, mnt_rule, for mount, remount, umount and pivot_root; policydb class AA_CLASS_MOUNT 7; permissions AA_MAY_PIVOTROOT 0x1, AA_MAY_MOUNT 0x2, AA_MAY_UMOUNT 0x4 (parser/mount.h:129-131).
  • Match string of a mount rule: \x07 mount point NUL source NUL type NUL flag bytes (parser/mount.cc:1129-1192). Demo's line is \x07/run/demo/\x00tmpfs\x00(tmpfs)\x00\x02 with 0x2 (tree).
  • A flag byte is the bit number + 1, written from bit 0 up: \xNN required, (\xNN|) optional, (\xfe|) when no flag byte results (parser/mount.cc:716-755).
  • Five generators, chosen in gen_flag_rules (parser/mount.cc:1082-1127): remount, bind, propagation, move, new mount, each with its own flag mask (parser/mount.h:106-121). mount, produces all five (tree).
  • mount options=ro options in (nosuid) /dev/x -> /mnt/, compiles to the flag bytes \x01(\x02|); mount options=ro options=nosuid /dev/x -> /mnt/, to two rules (tree).
  • Kernel: sb_mount sorts the call (linux:security/apparmor/lsm.c:707-742); do_match_mnt walks mount point, NUL, source, NUL, type, NUL, one byte per set flag, and tests AA_MAY_MOUNT (linux:security/apparmor/mount.c:159-211); match_mnt_flags (linux:security/apparmor/mount.c:132-143).
  • Hooks of the class: move_mount, sb_mount, sb_umount, sb_pivotroot (linux:security/apparmor/lsm.c:1791-1794).
  • Grammar and scanner: parser/parser_yacc.y:1277-1300, 1777-1838, parser/parser_lex.l:876-879.

for AppArmor developers for security researchers

Known discrepancies

None of these is taught as intended behaviour. All are on the bug list as confirmed; see Known bugs.

  • mount SOURCE, also compiles to a rule with SOURCE as the mount point and seventeen optional flags, bind and move among them; reproduce with mount /dev/sdb1, and -D rule-exprs (tree). Confirmed by a kernel run on the bug list.
  • options in (ro,atime) accepts a mount with no flags and one with noatime; apparmor.d(5) says it does not (tree, compiled flags (\x01|)(\x0b|)). Not run on a kernel.
  • nouser is accepted and has no effect: mount options=(nouser) /a -> /b, compiles to a mount without flags, and no rule matches a call with that flag set (tree).
  • Dead code and unused state in the mount rule: the filesystem-option ("data") field is never written, and two error messages cannot be reached (code).
  • A bind mount made with open_tree and move_mount is checked as a move without a source; bind rules do not apply to it (kernel; on the bug list).
  • unsupported mount option value, conflicting flags in the rule and Encoding of mount rule failed are printed without file, profile or line (tree).

Not determined: which flags mount(8) passes for a given command line.

Roads from here

START HERE

FILE ACCESS

PREAMBLE AND REUSE

QUALIFIERS

RULE CLASSES

SECURITY · threat model and limits

RUNNING THE PARSER