Remount rules

changing mount options for application developers for administrators for security researchers

A remount rule allows changing the flags of an existing mount. It is a mount rule with the remount flag added and the single path read as the mount point. Without options any other flags may accompany the remount. With options=( ) the flags must be exactly those. A refusal is logged as operation mount with remount among the flags.

On the map: rule classes

▶ Watch this deck — 8 scenes, ~5 min

Worth seeing first

  • Mount rules — the mount rule, its options and the flag table

01 demo makes its scratch area read-only

REMOUNT RULES mounted and filled; the worker should only read it demo /run/demo/ tmpfs: rw, nosuid kernel mount(NULL, "/run/demo", NULL, MS_REMOUNT | MS_RDONLY, NULL) = -1 EACCES the mount rule on line 14 does not cover it remount options=(ro) /run/demo/, line 15 of demo's profile with the rule: tmpfs: ro the flags of the call, besides remount itself the mount point no source and no filesystem type: the mount already exists
A tmpfs is mounted read-write and nosuid on /run/demo/. demo calls mount with MS_REMOUNT and MS_RDONLY to make it read-only and gets EACCES. With the rule remount options=(ro) /run/demo/ the call goes through and the tmpfs is read-only. options=(ro) are the flags of the call besides remount itself; /run/demo/ is the mount point. There is no source and no filesystem type.

remount [options...] MOUNTPOINT, allows changing the flags of an existing mount.

Demo has filled its scratch area and now wants it read-only before the worker starts. That's not a new mount. It's a change to an existing one, which the mount system call does when you pass the remount flag. The mount rule on line fourteen doesn't cover it. Line fifteen does: remount, the flags, and the mount point. There's no source and no type, because nothing new is being mounted.

Details and sources

Details

  • The kernel treats a mount(2) call with MS_REMOUNT as a remount before testing anything else (lsm.c:722-724) and compares the mount point and the full flags, with no source and no type (mount.c:328-353). The hook is sb_mount (lsm.c:707-742).
  • The permission is the mount permission; there is no separate remount permission.
  • A remount is a mount(2) call: EPERM without capability sys_admin,, after the rule has been checked (namespace.c:4279-4283).
  • A remount sets the flags to those of the call: this call leaves nosuid out, and the mount loses it (namespace.c:3506-3541). A program that wants to keep it passes it again, and the rule must then name it; see "Which other flags may come along".
  • remount options=(ro) /run/demo/, is line 15 of demo's profile; it compiles with no warning (run with the tree's parser). The program demo and its profile are invented for the map.
  • The flags of the call on the slide illustrate a remount to read-only. Which flags mount(8) passes for a command line was not determined, so the slide shows the system call and not a shell command.
  • Kernel statements are read from the kernel source and were not run.

Read more

for AppArmor developers for security researchers

In the code

  • The mount permission is AA_MAY_MOUNT (0x2) (mount.h:21).

02 remount, options, a mount point

GRAMMAR QUALIFIERSremountoptions=(FLAGS)MOUNTPOINT, remount options=(ro) /run/demo/, line 15 of demo's profile remount options in (ro, nosuid, nodev) /run/demo/, options in works as in mount rules remount /run/demo/ -> /mnt/, syntax error, unexpected TOK_ARROW, expecting TOK_END_OF_RULE remount, any remount of any mount point remount /run/demo/, any remount of this mount point
The syntax diagram of a remount rule: optional qualifiers, the word remount, an optional options conditional, an optional mount point, a comma. Examples: demo's line remount options=(ro) /run/demo/; remount with options in; remount with an arrow, which is a syntax error; remount alone for any remount of any mount point; remount /run/demo/ for any remount of that mount point.

[QUALIFIERS] remount [options=... | options in ...] [MOUNTPOINT],. The one path is the mount point.

The shape is the mount rule's, with the paths simplified. The keyword, the options conditional with equals or with in, and one path, which here is the mount point. No arrow. Leave out the path and it's every mount point. Leave out the options and any flags may come with the remount. The qualifiers work as for mount rules.

Details and sources

Details

  • The conditional names are those of mount rules (options, option, fstype, vfstype), but a remount has no filesystem type to compare.
  • 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": remount fstype=ext4 /a, is accepted and allows new ext4 mounts onto /a, and no remount. Do not write fstype= in a remount rule.
  • The mount point is a pattern and, for a directory, ends in /.
  • 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: remount /foo, (ok_22.sd); remount, (plasmashell:14).
  • The arrow gives syntax error, unexpected TOK_ARROW, expecting TOK_END_OF_RULE (run with the tree's parser).

for AppArmor developers for security researchers

In the code

  • TOK_REMOUNT opt_conds opt_id TOK_END_OF_RULE calls do_mnt_rule($2, NULL, NULL, $3, AA_DUMMY_REMOUNT) (parser_yacc.y:1287-1290).

03 A mount rule, written shorter

THE SAME RULE TWICE remount options=(ro) /run/demo/, the keyword becomes a flag the path becomes the mount point mount options=(remount, ro) -> /run/demo/, the long form compiled: identical mount options=(remount) /run/demo/, Encoding of mount rule failed in a mount rule one path alone is the source; a remount has none

remount options=(F) PATH, = mount options=(remount, F) -> PATH,.

Remount isn't really a new kind of rule. It's a mount rule that the parser completes for you. The keyword puts the remount flag into your options, and the path you wrote goes into the mount point position. You could write the long form yourself, with mount, remount in the options, and an arrow. They compile to the same thing. The short form saves you from one trap: in a mount rule a single path is the source, and a remount with a source can't be expressed at all.

Details and sources

Details

  • Run with the tree's parser, remount options=(ro) /run/demo/, and mount options=(remount, ro) -> /run/demo/, compile to the same rule: the mount point /run/demo/, any source, any type, and the two flags ro and remount, both required.
  • The difference without options: remount /a, requires the remount flag and lets every other remount flag be present or absent, while mount options=(remount) -> /a, requires the remount flag alone (run with the tree's parser).
  • mount options=(remount) /run/demo/, prints Encoding of mount rule failed, without a line number, followed by ERROR processing policydb rules for profile demo, failed to load (run with the tree's parser). A failing example in the tree: bad_opt_8.sd.

for AppArmor developers for security researchers

In the code

  • The keyword adds MS_REMOUNT to every options= set (mount.cc:541-542) and the placeholder permission is replaced by the mount permission (mount.cc:578-580).
  • The compiled string of both forms: \x07/run/demo/\x00[^\000]*\x00[^\000]*\x00\x01\x06, permission 0x2.

04 Which other flags may come along

FLAGS the call's flags besides remount ro ro + nosuid (nothing else) remount options=(ro) /run/demo/, allowed refused refused demo's tmpfs was mounted nosuid; a remount that repeats nosuid needs it in the rule remount options in (ro, nosuid) /run/demo/, allowed allowed allowed remount /run/demo/, allowed allowed allowed also rw: back to writable use options=( ) when the direction matters
Three remount rules against three calls whose flags besides remount are ro, ro and nosuid, or nothing else. remount options=(ro) allows only the first. remount options in (ro, nosuid) allows all three. remount without options allows all three and also rw, back to writable. demo's tmpfs was mounted nosuid, so a remount that repeats nosuid needs it in the rule.

remount options=(ro) P,: exactly remount + ro. remount options in (ro, nosuid) P,: remount plus any of those. remount P,: remount plus anything.

Options means the same here as in a mount rule, with one addition: the remount flag is always part of the set. So demo's rule, options= ro, allows a remount whose flags are exactly remount and read-only. A program that repeats the other flags of the mount, say nosuid, sends a different set and is refused. Options in is the forgiving form: any of these, or none. And a remount rule with no options at all allows any flags, which includes remounting read-write again. Pick the exact form when the direction matters.

Details and sources

Details

  • The "allowed" and "refused" cells are derived from the compiled flags and the kernel's flag comparison (mount.c:132-143, mount.c:328-353); nothing was run on a kernel. The compiled flags were produced with the tree's parser: row 1 requires ro and remount; row 2 requires remount and makes ro and nosuid optional; row 3 requires remount and makes every other remount flag optional.
  • The kernel passes the full flags of a remount call, propagation flags included; programs do pass them together with remount.
  • The flags a remount rule can name are all flags except move: remount options=(move) /a, is accepted and the move is dropped (run with the tree's parser).
  • remount options=(bind) /a, requires bind and remount: that is the call that changes the per-mount flags of a bind mount.
  • Clearing words (rw, suid, ...) add nothing to options=: remount options=(rw) /a, allows a remount with no other flag.
  • remount options=(rw, ro) /a, gives conflicting flag values = 33, 1, then conflicting flags in the rule.
  • Kernel statements are read from the kernel source and were not run.

for AppArmor developers for security researchers

In the code

05 What demo and the log see

THE DENIAL demo mount(..., MS_REMOUNT | MS_RDONLY | MS_NOSUID, ...) = -1 EACCES apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13profile="demo" name="/run/demo/" pid=14155 comm="demo" flags="ro, nosuid, remount" there is no operation called remount; it is one of the flags remount options=(ro) /run/demo/, exact set: ro. The call had ro and nosuid. remount options=(ro, nosuid) /run/demo/, the flags as logged remount options in (ro, nosuid) /run/demo/, or any of them
demo calls mount with MS_REMOUNT, MS_RDONLY and MS_NOSUID and gets EACCES. The record has operation mount, info failed flags match, name /run/demo/ and flags ro, nosuid, remount. There is no operation called remount; it is one of the flags. The rule remount options=(ro) is an exact set and the call had ro and nosuid. Two fixes: options=(ro, nosuid), or options in (ro, nosuid).

A remount is logged as operation="mount" with remount in flags=. info="failed flags match" means the path matched and the flags did not.

A refused remount fails with EACCES like any mount. In the log, don't look for the word remount as the operation: the operation is mount, and remount is one of the flags. There's no source and no type in the record, because a remount has neither. Info tells you which field failed. Here it's the flags: the rule wanted exactly read-only, and the call also carried nosuid. Copy the flags from the record into the rule, or use options in.

Details and sources

Details

  • The record on the slide is assembled from the fixture testcase_remount_01.in (apparmor="DENIED" operation="mount" class="mount" info="failed mntpnt match" error=-13 profile="/home/user/test/testmount" name="/tmp/foo/" pid=14155 comm="testmount" flags="ro, remount") with demo's values, which are illustrative. info="failed flags match" is from the kernel's table of texts (mount.c:145-153) and the order of the words in flags= from the kernel's format code (mount.c:31-85).
  • A remount is audited as operation="mount"; remount appears among the flags (lsm.c:722-724, mount.c:328-353).
  • A remount has no source and no type, so the record has no srcname= and no fstype=, and both are compared as empty. info is usually failed mntpnt match or failed flags match. It is failed srcname match or failed type match when the mount point matches only rules that name a source or a type, as demo's line 14 does before line 15 is added, and failed perms check when every field matched a rule without the permission (mount.c:179-189).
  • The mount point is named with a trailing / for a directory.
  • A complain-mode example with a long flag list: testcase_mount_02.in (flags="rw, nosuid, nodev, remount, bind, silent, relatime").
  • The profile must mediate the mount class for the check to happen; see mount rules.
  • Both fixes on the slide compile (run with the tree's parser). error=-13 is EACCES.
  • Kernel statements are read from the kernel source and were not run.

06 A placeholder permission that turns into a flag for AppArmor developers for security researchers

IN THE PARSER remount options=(ro) /run/demo/, grammar do_mnt_rule(conds, NULL, NULL, "/run/demo/", AA_DUMMY_REMOUNT) 0x40000000 mnt_rule constructor flags |= MS_REMOUNT perms = AA_MAY_MOUNT no options: remount required, the rest optional gen_policy_remount \x07/run/demo/\x00[^\000]* \x00[^\000]*\x00\x01\x06 any source, any type aa_remount() match_mnt(profile, path, buffer, NULL, NULL, NULL, flags, data, ...) no source, no type the remount generator runs only without a source and without fstype
The rule remount options=(ro) /run/demo/ passes three stations. The grammar calls do_mnt_rule with the placeholder permission AA_DUMMY_REMOUNT. The mnt_rule constructor adds MS_REMOUNT to the flags and sets the permission to AA_MAY_MOUNT. gen_policy_remount writes the class byte, the mount point, any source, any type and the flag bytes 01 and 06. In the kernel aa_remount calls match_mnt with the mount point and the flags and no source or type. The remount generator runs only without a source and without fstype.

AA_DUMMY_REMOUNT (mount.h:134) -> MS_REMOUNT added (mount.cc:541-542, 561-571) -> gen_policy_remount (mount.cc:786-859); kernel aa_remount (mount.c:328-353).

There is no remount permission in the kernel, so the parser needs a way to carry the keyword from the grammar to the rule. It uses a placeholder permission bit. The constructor sees it, adds the remount flag to every exact set, or makes it the exact set if there are no options, with every other flag optional, and swaps the placeholder for the ordinary mount permission. From there the remount generator writes the mount point, any source, any type, and the flag bytes. The kernel's remount path passes only the mount point and the flags. And that generator only runs when the rule has no source and no fstype.

Details and sources

for AppArmor developers for security researchers

Details

  • AA_DUMMY_REMOUNT is 0x40000000 (mount.h:134). The constructor adds MS_REMOUNT to every options= set (mount.cc:541-542), uses it as the required set when there is no options= (mount.cc:559-571) and replaces the placeholder by AA_MAY_MOUNT (mount.cc:578-580).
  • gen_policy_remount: mount.cc:786-859. It is chosen when the flags contain remount and the rule has neither source nor fstype (mount.cc:1105-1107). MS_REMOUNT_FLAGS is every flag except move (mount.h:114-120).
  • Flag bytes: ro is \x01, remount is \x06 (bit number + 1).
  • Dumps from the tree's parser: demo's line gives \x07/run/demo/\x00[^\000]*\x00[^\000]*\x00\x01\x06 with 0x2; remount /run/demo/, gives ...\x00(\x01|)(\x02|)(\x03|)(\x04|)(\x05|)\x06(\x07|)...; remount options in (ro) /a, gives (\x01|)\x06; remount options=(bind) /a, gives \x06\x0d.
  • Kernel: aa_remount (mount.c:328-353).
  • Kernel statements are read from the kernel source and were not run.

for security researchers

Attack surface

  • 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": mount options=(remount, ro) /run/demo/, (the long form with the path in the source position and one more flag) is accepted and compiles to \x07[^\000]*\x00/run/demo/\x00[^\000]*\x00\x01: a new mount with source /run/demo/ onto any mount point with the flag ro, and no remount (run with the tree's parser). With remount alone the same shape fails to encode.
  • 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": remount fstype=ext4 /a, is accepted and compiles to a new-mount rule without the remount flag, \x07/a\x00[^\000]*\x00(ext4)\x00(\x01|)(\x02|)(\x03|)(\x04|)(\x05|)(\x07|)...: new mounts of that type from any source onto /a with any combination of the ordinary flags, and no remount. The cause is the order of the tests: with an fstype the remount branch is skipped and the last branch still sees optional flags (mount.cc:559-571, mount.cc:1105-1106, mount.cc:1118-1123, mount.cc:1043). The long form mount options=(remount) fstype=ext4 -> /a, fails with Encoding of mount rule failed.
  • When auditing, look for remount rules with fstype= or vfstype=. Both are on the bug list as confirmed on the parser side; see Known bugs. They were not run on a kernel.
  • A remount rule covers mount(2) with the remount flag. mount_setattr(2), which changes the read-only, nosuid, nodev and noexec flags of a mount, has no AppArmor hook, so a remount rule, deny remount included, is not asked about it (read from the kernel source, lsm.c:1791-1794; not run on a kernel). See what is not mediated.
  • A remount rule without options also allows remounting read-write or dropping nosuid, nodev or noexec. That is how the rule reads, but it is worth a second look in a profile meant to keep a mount restricted.

for AppArmor developers for security researchers

Read more

07 Mistakes and their messages

MISTAKES remount /run/demo/ -> /mnt/, syntax error, unexpected TOK_ARROW, expecting TOK_END_OF_RULE remount options=(rw, ro) /run/demo/, conflicting flags in the rule remount options=(ro, size=1m) /run/demo/, unsupported mount option value 'size=1m' flag words only owner remount /run/demo/, owner prefix not allowed on mount rules remount /run/demo, accepted; does not match the directory write /run/demo/
Five remount rules. An arrow is a syntax error. rw and ro together conflict. A filesystem option size=1m is unsupported. owner is refused. remount /run/demo without the slash is accepted and does not match the directory.

One path with a trailing /, flag words only, no arrow.

An arrow is a syntax error, because there's only one path. The words rw and ro together conflict. Filesystem options such as a size can't be written, only flags. Owner isn't allowed. And, as with the other mount rules, the mount point of a directory needs its slash, which the parser won't remind you of.

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. Rows 1 to 4 were run with the tree's parser.
  • Row 2 prints two lines, conflicting flag values = 33, 1 and conflicting flags in the rule. The numbers are the two flag masks in decimal: 33 is remount plus ro.
  • Known discrepancy, on the bug list as "Mount option errors bypass the parser's error reporting": the option errors of rows 2 and 3 carry no file, profile or line.
  • An unknown conditional gives invalid mount conditional foo, then bad mount rule. The long form with a source, mount options=(remount) /run/demo/,, gives Encoding of mount rule failed.
  • Row 5 is accepted by the parser; that it does not match a directory is derived from the kernel's naming of mount points and was not run.

Read more

08 Before and after the remount

ROADS remount options=(ro) /run/demo/, the scratch area is read-only now syn-mount flags and options syn-pivot-root next: the worker's root syn-umount last: take it down syn-rule-conditionals in and lists adm-denials the mount record
Remount rules in the centre with roads to syn-mount, to syn-pivot-root and syn-umount, and to syn-rule-conditionals and adm-denials.

Remount rules are mount rules with the remount flag. The flag table is in the mount deck.

The scratch area is read-only now. The complete table of flag words, and the difference between options= and options in, are in the mount deck. Next in demo's story is pivot_root, and at the end, umount. The conditionals deck has the list syntax, and the denials deck explains the record.

Details and sources

Details

  • [QUALIFIERS] remount [options=... | options in ...] [MOUNTPOINT], is the same rule as mount options=(remount, ...) -> MOUNTPOINT,.
  • A refused remount returns EACCES and is logged as operation="mount" with remount in flags=.
  • 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

REMOUNT RULE:  [QUALIFIERS] remount [CONDITIONALS] [MOUNTPOINT] ,
CONDITIONALS:  options=FLAG | options=( FLAG ... ) | options in ( FLAG ... )
QUALIFIERS:    [priority=N] [audit] [allow|deny|prompt]
  • The one path is the mount point. There is no source and no arrow (tree).
  • remount options=(F) P, is the same rule as mount options=(remount, F) -> P, (tree).
  • option is the same word as options (code).
SeatOn a remount 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)

remount and the long form

RuleRequired flagsOptional flags
remount P,remountevery other flag except move
remount options=(ro) P,remount, ronone
remount options in (ro, nosuid) P,remountro, nosuid
mount options=(remount) -> P,remountnone
mount options=(remount, ro) -> P,remount, ronone
mount options=(remount) S -> P,error: Encoding of mount rule failed

All rows are from the tree's parser.

Flags in a remount rule

  • Every flag word of a mount rule except move; the table is in syn-mount. remount options=(move) P, is accepted and the move is dropped (tree).
  • remount options=(bind) P, requires bind and remount: the call that changes the per-mount flags of a bind mount (tree).
  • Clearing words (rw, suid, ...) add nothing to options=; remount options=(rw) P, allows a remount with no other flag (tree).
  • Propagation flags may accompany a remount, because programs pass them together (parser/mount.h:114-120, code).

The log record

FieldMeaning
operation="mount"a remount is logged as a mount
class="mount"always
info=failed mntpnt match, failed flags match or failed perms check; failed srcname match, failed type match (the mount point matched only a rule with a source or a type)
error=-13EACCES
name=the mount point; a directory ends in /
flags=the flags as words; remount is among them

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

apparmor="DENIED" operation="mount" class="mount" info="failed mntpnt match" error=-13 profile="/home/user/test/testmount" name="/tmp/foo/" pid=14155 comm="testmount" flags="ro, remount"

Errors and warnings

RuleMessageHas line number
remount /run/demo/ -> /mnt/,syntax error, unexpected TOK_ARROW, expecting TOK_END_OF_RULEyes
remount options=(rw, ro) /run/demo/,conflicting flag values = 33, 1 then conflicting flags in the ruleno
remount options=(ro, size=1m) /run/demo/,unsupported mount option value 'size=1m'no
owner remount /run/demo/,owner prefix not allowed on mount rulesyes
mount options=(remount) /run/demo/,Encoding of mount rule failedno
remount /run/demo,none; the rule does not match a directory
any remount rule, no mount in the feature set, --warn=rule-not-enforcedwarning mount rules not enforce

Rows with an error message are from the tree's parser.

for AppArmor developers for security researchers

In the parser and the kernel

  • Grammar action: do_mnt_rule($2, NULL, NULL, $3, AA_DUMMY_REMOUNT) (parser/parser_yacc.y:1287-1290); the placeholder is 0x40000000 (parser/mount.h:134).
  • Constructor: MS_REMOUNT is added to every options= set (parser/mount.cc:541-542); without options= the required set is {remount} and the optional set every other remount flag (parser/mount.cc:559-571); the permission becomes AA_MAY_MOUNT (parser/mount.cc:578-580).
  • gen_policy_remount writes mount point, any source, any type, flags masked with MS_REMOUNT_FLAGS (parser/mount.cc:786-859, parser/mount.h:114-120). Demo's line is \x07/run/demo/\x00[^\000]*\x00[^\000]*\x00\x01\x06 with 0x2 (tree).
  • Kernel: sb_mount tests MS_REMOUNT first (linux:security/apparmor/lsm.c:722-724); aa_remount passes the mount point and the flags (linux:security/apparmor/mount.c:328-353).

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.

  • remount fstype=ext4 /a, is accepted and compiles to a new-mount rule for type ext4 onto /a with every ordinary flag optional and no remount flag; reproduce with -D rule-exprs (tree). Not run on a kernel.
  • mount options=(remount, ro) /run/demo/, is accepted and compiles to a new mount with source /run/demo/ and the flag ro, without remount (tree). Not run on a kernel.
  • The option errors are printed without file, profile or line (tree).

Roads from here

START HERE

QUALIFIERS

RULE CLASSES

RUNNING THE PARSER