safe and unsafe exec

environment scrubbing for application developers for security researchers

Whether the new program is started in secure-execution mode when an exec changes the profile. Uppercase P C and U request it and lowercase letters do not. The safe and unsafe keywords override the letter case and also apply to change_profile rules.

On the map: file access

▶ Watch this deck — 8 scenes, ~6 min

Worth seeing first

  • Exec modes — the exec modes whose letter case this deck explains

Part of journeys

01 The environment crosses the exec

SAFE AND UNSAFE EXEC profile demo demo tightly confined profile demo-helper demo-helper may do more execve() LD_PRELOAD=/tmp/evil.so the environment comes along evil.so loaded demo's code, helper's permissions /usr/bin/demo-helper P x, one flag at exec: do not trust the environment capital P uppercase: ask for a clean start
The program demo in profile demo executes demo-helper in profile demo-helper; the variable LD_PRELOAD=/tmp/evil.so travels along the exec and the library is loaded into the helper; below, the rule /usr/bin/demo-helper Px with the capital P highlighted asks for a clean start.

An exec that changes profile can ask for secure-execution mode, in which the C library ignores variables such as LD_PRELOAD. Rules that ask are "safe"; rules that do not are "unsafe". The path /tmp/evil.so is only an illustration.

Demo is tightly confined. Its helper runs under a different profile, and it may do things demo may not. When demo executes the helper, the profile changes, but the environment comes along: every variable demo chose arrives in the new program. Now suppose demo has been taken over. It sets LD_PRELOAD, and the dynamic loader pulls a library of demo's choosing into the helper. That's demo's code, running with the helper's permissions. AppArmor's answer is one flag, raised at exec, that tells the new program not to trust its environment. Whether a rule raises it is what safe and unsafe mean, and the capital P in this rule is one way to ask.

Details and sources

Details

  • execve passes the caller's environment to the new program unchanged. AppArmor does not filter it; it can only mark the exec as secure.
  • A safe rule (Px, Cx, Ux, or the keyword safe) asks for that mark when the exec changes profile. An unsafe rule (px, cx, ux, or unsafe) does not.
  • In the kernel the mark is requested in the exec hook when the rule's unsafe bit is clear (domain.c:776-784). Read from the source, not observed on a running kernel.
  • The manual page's wording for Ux: AppArmor sets "ld.so(8) secure-execution mode" to "clear environment variables such as LD_PRELOAD, similar to setuid programs" (apparmor.d.pod:974-977).
  • Which variables are ignored is the C library's list (see ld.so(8)); it is in neither source tree. LD_PRELOAD is the usual example.
  • /tmp/evil.so on the slide is an illustration, not a file from either tree.

02 Two ways to say it

GRAMMAR safe Px Cx Ux Pix Cix PUx CUx unsafe px cx ux pix cix pux cux the case of the first letter QUALIFIERS file safe | unsafe PATH MODE , a file rule with x; grey is optional unsafe /usr/bin/demo-helper Px, the keyword wins over the case change_profile safe | unsafe EXEC PATH -> TARGET , change_profile unsafe /usr/bin/demo-helper -> demo-helper, ix only one form both keywords are optional; without one, a file rule goes by the letter
Two rows of exec modes: the safe row Px, Cx, Ux, Pix, Cix, PUx, CUx and the unsafe row px, cx, ux, pix, cix, pux, cux, with ix alone in one form. Below, two syntax diagrams: a file rule with the optional keyword safe or unsafe after the qualifiers and the word file, and a change_profile rule with the same keyword before the exec path.

Uppercase P, C, U = safe; lowercase = unsafe. safe and unsafe before a rule override the case. change_profile [safe|unsafe] EXEC -> TARGET, takes the same keywords.

There are two spellings for the same bit. The short one is the case of the first letter: capital P, C or U is safe, lowercase is unsafe. The long one is a word, safe or unsafe, in front of the rule: after any qualifiers, and after the word file if you write it. Use the keyword and it decides, whatever the case of the letter. The same two keywords appear in change_profile rules, between the rule's name and the program path. And ix has just one form, with no safe version at all, for a reason we'll get to.

Details and sources

Details

  • File rule: [QUALIFIERS] [file] [safe|unsafe] PATH MODE [-> TARGET], or with the mode first: [QUALIFIERS] [file] [safe|unsafe] MODE PATH [-> TARGET],.
  • change_profile [safe|unsafe] [EXEC PATH] [-> TARGET],.
  • Letter case of the first exec letter: safe Px Cx Ux Pix Cix PUx CUx; unsafe px cx ux pix cix pux cux. ix has one form.
  • The letters as the parser names them: UNCONFINED = 'U', UNSAFE_UNCONFINED = 'u', PROFILE = 'P', UNSAFE_PROFILE = 'p', LOCAL = 'C', UNSAFE_LOCAL = 'c' (parser.h:198-203).
  • safe and unsafe are keywords of the profile language (parser_misc.c:92-93).
  • Position, from the parser built from the AppArmor tree at 836052d21: file unsafe /usr/bin/demo-helper Px, and audit unsafe /usr/bin/demo-helper Px, are accepted; safe file /x Px, gives syntax error, unexpected TOK_FILE, expecting TOK_ID or TOK_MODE or TOK_SET_VAR; unsafe audit /x Px, gives syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VAR.

for AppArmor developers for security researchers

In the code

03 What "scrubbing" really is

ONE FLAG /usr/bin/demo-helper Px, in profile demo demo execve() AppArmor new profile, safe rule secure exec = yes ELF loader AT_SECURE = 1 in the new program's auxiliary vector demo-helper ld.so secure-execution mode LD_PRELOAD=/tmp/evil.so ignored by the C library the lowercase form /usr/bin/demo-helper px, AT_SECURE = 0 LD_PRELOAD honoured
A safe exec from left to right: demo executes the helper under the rule Px; AppArmor chooses the new profile and marks the exec as secure; the ELF loader writes AT_SECURE = 1 into the new program's auxiliary vector; in demo-helper ld.so runs in secure-execution mode and LD_PRELOAD is ignored. Below, the same exec with px: AT_SECURE is 0 and LD_PRELOAD is honoured.

A safe exec sets AT_SECURE for the new program; ld.so then runs in secure-execution mode. Which variables are ignored is decided by the C library, not by AppArmor. Read from the kernel source; not observed on a running kernel.

People say the environment gets scrubbed, but the kernel doesn't touch it. Here's what the code does. Demo executes its helper, and AppArmor's exec hook picks the new profile. The rule was the safe form, so AppArmor marks this exec as secure. The program loader copies that mark into a value called AT_SECURE, which every new program can read. The dynamic linker reads it, switches to secure execution mode, and ignores LD_PRELOAD and its relatives. Which variables those are is the C library's decision, not AppArmor's. With the lowercase form the mark stays off, and the variables are honoured.

Details and sources

Details

All of this is read from the kernel source; none of it was observed on a running kernel.

  • The LSM hook is bprm_creds_for_exec, implemented by apparmor_bprm_creds_for_exec. When the chosen rule is safe, profile_transition sets *secure_exec = true (domain.c:776-784).
  • The hook then sets bprm->secureexec = 1 (domain.c:1070-1078).
  • The ELF loader copies the flag into the auxiliary vector of the new program: NEW_AUX_ENT(AT_SECURE, bprm->secureexec); (binfmt_elf.c:262).
  • The dynamic linker of the new program reads the entry and switches to secure-execution mode; see ld.so(8) for the variables it then ignores. The list belongs to the C library and is in neither source tree.
  • The kernel does not modify the environment.
  • Separately, whenever the exec changes the label, safe or not, AppArmor asks for the PER_CLEAR_ON_SETID personality bits to be cleared (domain.c:1080-1089).
  • apparmor.d(5) says the kernel's "unsafe_exec routines" are invoked (apparmor.d.pod:974-977). The kernel has no function of that name; the mechanism is the secureexec flag.

for security researchers

Attack surface

  • The kernel does not remove or change any environment variable. A safe exec sets one flag, bprm->secureexec, which reaches the new program as AT_SECURE (domain.c:1070-1078, binfmt_elf.c:262; read from the kernel source, not observed on a running kernel). What is ignored afterwards is the C library's decision, and the variables are still in the environment: code that reads the environment itself sees every one.
  • AT_SECURE covers only what the C library ties to it. Open file descriptors are a separate mechanism: when the exec changes the label to a confined one, aa_inherit_files revalidates the inherited descriptors against the new label (file.c:721, called at lsm.c:967), whether the rule was safe or unsafe.
  • Whenever the exec changes the label, safe or not, AppArmor asks for the PER_CLEAR_ON_SETID personality bits to be cleared (domain.c:1080-1089).
  • ix, and an ix fallback, do not set the flag: the test is skipped when the new label is the profile's own (domain.c:730-739). A learning profile created in complain mode is always unsafe (domain.c:767).
  • Lowercase px, cx, pix, cix, pux and cux are the unsafe forms and print no warning; only lowercase ux does, and only on request.
  • change_profile safe EXEC -> TARGET, compiled without stacking support prints "downgrading change_profile safe rule to unsafe" but the compiled rule is byte-identical to the rule with no keyword (run with the parser built from the AppArmor tree at 836052d21, nothing loaded into a kernel; parser_yacc.y:1663-1670). The warning describes what such a kernel does, not what the parser wrote.

for AppArmor developers for security researchers

In the code

  • The parser's unsafe bit, 0x100 per half, becomes AA_X_UNSAFE in dfa_map_xindex (policy_compat.c:29-30). The test in profile_transition is !(perms.xindex & AA_X_UNSAFE).

04 The keyword overrides the case

SAME BIT unsafe /usr/bin/demo-helper Px, = /usr/bin/demo-helper px, safe /usr/lib/demo/worker cx, = /usr/lib/demo/worker Cx, unsafe cx /bin/bar, letters first works too (from the tree's tests) Px Cx Ux the profile changes AT_SECURE set px cx ux the profile changes not set ix the profile stays not set: nothing to protect Pix: set if the search finds a profile; on the ix fallback, not set. PUx: set, also on the ux fallback. from the kernel source, not from a test run
Equivalences: unsafe before Px equals px; safe before cx equals Cx; unsafe cx /bin/bar with the letters first also works. A table: Px, Cx, Ux change the profile and set AT_SECURE; px, cx, ux change the profile and do not set it; ix keeps the profile and does not set it. Pix sets it only when a profile is found; PUx sets it on the ux fallback too.

unsafe PATH Px, equals PATH px,; safe PATH px, equals PATH Px,. An exec that stays in the same profile (ix, or an ix fallback) does not set AT_SECURE. The table is worked out from the kernel source; it was not measured on a kernel.

The keyword and the letter case drive one bit, and the keyword is applied last. So unsafe in front of capital Px gives you lowercase px. And safe in front of lowercase cx gives you capital Cx. The tree's own tests write the letters first, and that works too. Now, why is there no safe ix? Going by the code of the kernel, the flag is raised only when the exec lands in a different profile. With ix the profile stays, old and new program have the same rights, and there's nothing to protect. The same holds when a fallback mode ends up inheriting.

Details and sources

Details

  • The keyword is applied after the mode word is read, so it overrides the letter case: unsafe /a Px, equals /a px,; safe /a px, equals /a Px,. Each pair merges without a conflict (the parser was run on the fragments).
  • Mode-first spellings from the parser's tests: unsafe px /foo6,, unsafe Px /foo7, (front_perms_ok_2.sd); unsafe cx /bin/bar, (simple_ok_cx_1.sd).
  • When AT_SECURE is set, worked out from the kernel source and not measured:
  • Px Cx Ux, a different profile or unconfined: set. px cx ux: not set.
  • ix: not set, with or without a keyword.
  • Pix Cix: set when a profile was found, not set after the ix fallback. PUx CUx: set in both cases.
  • A learning profile created in complain mode: not set.
  • A stacked label: set if the rule of any one profile asks.
  • The reason for ix: when the result is the profile's own label, the kernel jumps past the test (domain.c:730-739); otherwise the test is at domain.c:776-784.
  • unsafe /a ix, is accepted, but together with a plain /a ix, it fails with has merged rule /a with conflicting x modifiers, although the bit has no effect on an ix exec.

Read more

for AppArmor developers for security researchers

In the code

05 safe and unsafe on change_profile

CHANGE_PROFILE change_profile /usr/bin/demo-helper -> demo-helper, change_profile unsafe /usr/bin/demo-helper -> demo-helper, exec condition target demo aa_change_onexec("demo-helper") execve("/usr/bin/demo-helper") demo-helper label: demo-helper first ask, then exec AT_SECURE set (default) not set change_profile -> demo-helper, no exec condition: no place for safe or unsafe an immediate aa_change_profile() does not exec: the keywords do not apply
Two change_profile rules with the exec condition /usr/bin/demo-helper and the target demo-helper, the second with the keyword unsafe. Demo calls aa_change_onexec and then execve; the helper starts in profile demo-helper. Under the first rule AT_SECURE is set by default; under the second it is not set. An immediate aa_change_profile does not exec, so the keywords do not apply.

change_profile [safe|unsafe] EXEC -> TARGET,. Without a keyword the change at exec is safe in the kernel. The keywords require the exec path. The full rule is in syn-change-profile.

There's a second way to change profile at exec: the program asks for it itself. The rule that allows this is change_profile. The program path is the exec condition, and the target comes after the arrow. At run time demo calls aa_change_onexec, names the helper's profile, and then executes the helper. Here the default is the other way round from lowercase px: with no keyword, the exec is safe. Write unsafe to let the environment through. And the keywords need that exec condition, because an immediate change of profile involves no exec, so there's no new program to protect.

Details and sources

Details

  • Rule: change_profile [safe|unsafe] EXEC -> TARGET,. Tests: change_profile safe /usr/bin/bar, (safe_ok_2.sd); change_profile unsafe /usr/bin/bar -> unsafe,, where the word after -> is a target called unsafe (unsafe_ok_3.sd).
  • "safe is the default": change_profile safe /onexec -> /bin/foo, and change_profile /onexec -> /bin/foo, do not conflict (onx_no_conflict_safe1.sd).
  • The default holds when the feature domain/stack is in the feature set the profile is compiled for (parser_main.c:1093-1095). Without it the parser only warns about an explicit safe; the compiled rule is the same as with no keyword (run with the parser built from the tree at 836052d21), and by the comment at parser_yacc.y:1663-1670 kernels without stacking support only the unsafe behaviour.
  • libapparmor: int aa_change_onexec(const char *profile); (apparmor.h:81). It sends exec PROFILE as the task's exec attribute (kernel.c:1088-1107): through the lsm_set_self_attr syscall when available, else by writing /proc/<tid>/attr/apparmor/exec or the older /proc/<tid>/attr/exec (kernel.c:997-1012, kernel.c:424-426).
  • Kernel, read and not run: the write reaches aa_change_profile(args, AA_CHANGE_ONEXEC) (lsm.c:897-899). At the next exec profile_onexec sets the secure flag unless the rule's unsafe bit is set (domain.c:855-863).
  • An immediate change (aa_change_profile()) involves no exec, so the keywords do not apply to it.

for AppArmor developers for security researchers

In the code

  • The change_profile action: exec bits with an exec condition, the unsafe bits for unsafe, the downgrade warning (parser_yacc.y:1648-1675).
  • In the lexer safe and unsafe are keywords in the CHANGE_PROFILE_MODE state until -> (parser_lex.l:593-603).

06 AA_EXEC_UNSAFE to bprm->secureexec for AppArmor developers for security researchers

PARSER AND KERNEL: THE UNSAFE BIT parse_sub_perms: case 'p', 'c', 'u': tperms = AA_EXEC_UNSAFE parser_misc.c:628,649 file_rule_tail unsafe: perms |= ((perms & AA_EXEC_BITS) << 8) & ALL_AA_EXEC_UNSAFE safe: perms &= ~ALL_AA_EXEC_UNSAFE parser_yacc.y:1122-1127 dfa_map_xindex: mask & 0x100 -> AA_X_UNSAFE policy_compat.c:29 profile_transition: new = x_to_label(...) new && !(xindex & AA_X_UNSAFE) -> *secure_exec = true domain.c:776-784 skipped when new->proxy == profile->label.proxy (ix, ix fallback) domain.c:730-739 apparmor_bprm_creds_for_exec: if (unsafe) bprm->secureexec = 1 create_elf_tables: NEW_AUX_ENT(AT_SECURE, bprm->secureexec) domain.c:1070-1078 binfmt_elf.c:262 change_profile: profile_onexec, the same test (domain.c:837-863)

AA_EXEC_UNSAFE (parser/immunix.h) → AA_X_UNSAFE → *secure_exec in profile_transition (linux:security/apparmor/domain.c:776-784) → bprm->secureexec → AT_SECURE.

In parse_sub_perms the lowercase cases set the unsafe bit and then fall through to the uppercase case. The keywords come afterwards, in file_rule_tail: one sets the bit in every half that has exec, the other clears it. The kernel maps the bit to AA_X_UNSAFE. profile_transition tests it once x_to_label has returned a label, and skips the test when that label is the profile's own, which is the ix case. With a stacked label, any one profile can raise the flag. A learning profile created in complain mode forces unsafe. And mind the caller: its local variable named unsafe actually holds "secure exec requested".

Details and sources

for AppArmor developers for security researchers

Details

07 Messages about safe and unsafe

COMMON MISTAKES safe /etc/demo/demo.conf r, unsafe rule missing exec permissions the same text for either keyword change_profile unsafe -> demo-helper, Exec condition is required when unsafe or safe keywords are present /usr/bin/demo-helper Px, /usr/bin/demo-helper px, profile demo: has merged rule /usr/bin/demo-helper with conflicting x modifiers two exec modes for one path change_profile safe /usr/bin/demo-helper -> demo-helper, warning only: the compiled rule is unchanged non-stacking kernels treat it as unsafe downgrading change_profile safe rule to unsafe due to lack of necessary kernel support seen with no abi line in the file safe file /x Px, syntax error: the keyword goes after "file"
Four fragments and the parser's message: safe on a read rule gives unsafe rule missing exec permissions; change_profile unsafe without an exec path gives Exec condition is required when unsafe or safe keywords are present; Px and px on one path give conflicting x modifiers; change_profile safe without the stacking feature gives a downgrade warning and is compiled unchanged. The keyword before the word file is a syntax error.

"unsafe rule missing exec permissions"; "Exec condition is required when unsafe or safe keywords are present"; px with Px on one path is "conflicting x modifiers". A downgrade of change_profile safe is a warning. All four were produced with the parser built from the tree.

The keywords only make sense on an exec. Put safe on a read rule and the parser says unsafe rule missing exec permissions, in those words whichever keyword you used. On change_profile the keyword needs the program path. A safe rule and an unsafe rule for the same path are two different exec modes, so the parser reports a conflict. And one warning to take seriously: when the feature set lacks stacking support, the parser warns that a change_profile rule you marked safe will be treated as unsafe by such a kernel. The compiled rule is the same as with no keyword. The tree's parser printed it for a file with no abi line.

Details and sources

Details

Messages of the parser built from the AppArmor tree at 836052d21, nothing loaded into a kernel. Errors are printed as AppArmor parser error for FILE in profile FILE at line N: MESSAGE.

  • safe /etc/demo/demo.conf r,: unsafe rule missing exec permissions. The text is the same for both keywords.
  • change_profile unsafe -> demo-helper,: Exec condition is required when unsafe or safe keywords are present (test unsafe_bad_2.sd).
  • change_profile unsafe a -> b,: Exec condition must begin with '/'.
  • Px and px for one path: profile demo: has merged rule PATH with conflicting x modifiers, with no line number, followed by ERROR merging rules for profile demo, failed to load.
  • Warning downgrading change_profile safe rule to unsafe due to lack of necessary kernel support: an explicit safe on change_profile when the feature domain/stack is missing (parser_main.c:1093-1095). Class rule-downgraded (parser_main.c:282). Printed for a file with no abi line, not printed with abi <abi/5.0>,.
  • A change_profile rule with no keyword is compiled the same way on such a feature set and prints nothing.
  • The ux warning is tied to the lowercase letter: unsafe /usr/bin/demo-helper Ux, has the effect of ux and prints nothing.

for AppArmor developers for security researchers

In the code

08 Where to go from here

ROADS safe / unsafe uppercase or "safe": AT_SECURE lowercase or "unsafe": the environment is trusted which exec changes profile? syn-exec-modes the modes behind the letters syn-named-transitions exec rules with an arrow the program asks syn-change-profile the rule lib-change-profile the library call where the flag is set k-exec the exec hook in the kernel where the keywords sit in a rule syn-file-rules syntax-map
Safe and unsafe in the centre with a recap: uppercase or safe sets AT_SECURE, lowercase or unsafe trusts the environment. Roads lead to syn-exec-modes and syn-named-transitions, to syn-change-profile and lib-change-profile, to k-exec, and to syn-file-rules and syntax-map.

Next: syn-exec-modes, syn-named-transitions, syn-change-profile, lib-change-profile, k-exec. Rule layout: syn-file-rules, syntax-map.

Here's the rule of thumb. When an exec crosses into a profile with different rights, use the uppercase form, unless you know the new program needs something from its environment. From here, the exec modes deck tells you which exec crosses into which profile, and named transitions adds the arrow. The change_profile decks cover the rule and the library call that asks for a change at the next exec. The kernel's exec deck shows the hook where the flag is set. And if you want to see where the keywords sit among the other parts of a rule, file rules and the syntax map are one step away.

Details and sources

Details

  • Safe: Px Cx Ux Pix Cix PUx CUx, or the keyword safe. Unsafe: the lowercase forms, or unsafe. The keyword overrides the case.
  • A safe exec that changes profile sets AT_SECURE for the new program; ix and an ix fallback never do (source of the kernel, not observed on a running kernel).
  • change_profile EXEC -> TARGET, is safe without a keyword when the feature domain/stack is available.
  • Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2), the kernel-rc2 (97d73c825).

Read more

Basis of the statements on this page: "tree" means the parser built from the AppArmor commit above was run on the fragment, with the tree's abi/5.0 file as the kernel features and nothing loaded into a kernel; "ran" means the researcher ran a parser on the fragment, as recorded in the findings; "code" means the kernel source of mainline Linux 7.4 (7.4-rc2, 97d73c825) or the parser source was read, not run. Nothing on this page was observed on a running kernel: every statement about AT_SECURE is derived from the kernel source.

Grammar

FILE RULE:            [QUALIFIERS] [file] [safe|unsafe] PATH MODE [-> TARGET] ,
                      [QUALIFIERS] [file] [safe|unsafe] MODE PATH [-> TARGET] ,
CHANGE PROFILE RULE:  change_profile [safe|unsafe] [EXEC PATH] [-> TARGET] ,
  • On a file rule the keyword needs an exec permission in MODE.
  • On change_profile the keyword needs EXEC PATH.
  • Letter case of the first exec letter:
SafeUnsafeMode
Pxpxprofile transition
Cxcxchild profile transition
Uxuxunconfined
Pix Cixpix cixtransition, falling back to ix
PUx CUxpux cuxtransition, falling back to ux
(none)ixinherit: one form only

The modes themselves: syn-exec-modes. Targets: syn-named-transitions.

Keywords

KeywordPositionEffect
safeafter the qualifiers and after file; before the path or the modeclears the unsafe bit of the rule: same as the uppercase letter
unsafethe samesets the unsafe bit on the owner half and the other half where each has x: same as the lowercase letter
  • The keyword is applied after the mode string is read, so it overrides the letter case. unsafe /a Px, and /a px, merge without a conflict, and so do safe /a px, and /a Px, (ran).
  • Both orders of path and mode take the keyword: unsafe px /foo6,, unsafe Px /foo7, (parser/tst/simple_tests/file/front_perms_ok_2.sd), unsafe cx /bin/bar, (parser/tst/simple_tests/xtrans/simple_ok_cx_1.sd).
  • file unsafe /usr/bin/demo-helper Px, and audit unsafe /usr/bin/demo-helper Px, are accepted (tree).
  • After -> in a change_profile rule the words are ordinary names: change_profile unsafe /usr/bin/bar -> unsafe, names a target called unsafe (parser/tst/simple_tests/change_profile/unsafe_ok_3.sd).

When AT_SECURE is set

Derived from linux:security/apparmor/domain.c (code); not measured on a kernel.

RuleResult of the execAT_SECURE
Px Cx Uxa different profile, or unconfinedset
px cx uxa different profile, or unconfinednot set
ixthe same profilenot set, with or without a keyword
Pix Cixa profile was foundset
Pix Cixix fallbacknot set
pix cixeithernot set
PUx CUxa profile was found, or ux fallbackset
pux cuxeithernot set
change_profile EXEC -> TARGET, and a change requested for the next execthe targetset
change_profile unsafe EXEC -> TARGET, and the same requestthe targetnot set
no exec rule, profile in complain modea learning profile is creatednot set
a stacked labeleach profile decides for itselfset if the rule of any one profile asks
  • An immediate change of profile (aa_change_profile()) does not exec, and the keywords do not apply to it. See lib-change-profile.
  • The change_profile default: without a keyword the rule is safe when the feature domain/stack is in the feature set the profile is compiled for. Without that feature the parser only warns (parser/parser_yacc.y:1663-1670): the compiled rule is unchanged (tree: the -S output of change_profile safe X -> Y, equals that of change_profile X -> Y, and differs from unsafe). The comment there says kernels without stacking support only the unsafe behaviour.

What the flag does

  • AppArmor sets bprm->secureexec; the ELF loader copies it into the AT_SECURE entry of the new program's auxiliary vector (code).
  • The kernel does not modify the environment. The C library of the new program reads AT_SECURE; the manual describes the result as "ld.so(8) secure-execution mode" (parser/apparmor.d.pod:974-977). Which variables are ignored is the C library's list and is in neither tree; LD_PRELOAD is the usual example.
  • Separately, whenever the exec changes the label, safe or not, AppArmor asks for the PER_CLEAR_ON_SETID personality bits to be cleared (code: linux:security/apparmor/domain.c:1080-1089).

Errors and warnings

Errors are printed as AppArmor parser error for FILE in profile FILE at line N: MESSAGE. The conflict comes from the later compile stage: it has no line number and is followed by ERROR merging rules for profile demo, failed to load.

MessageCauseBasis
unsafe rule missing exec permissionssafe or unsafe on a file rule without x, such as safe /etc/demo/demo.conf r,; the text is the same for both keywordstree
Exec condition is required when unsafe or safe keywords are presentchange_profile unsafe -> demo-helper,tree
Exec condition must begin with '/'.change_profile unsafe a -> b,tree
profile demo: has merged rule PATH with conflicting x modifiersPx and px (or any safe and unsafe pair) for one pathtree
syntax error, unexpected TOK_FILE, expecting TOK_ID or TOK_MODE or TOK_SET_VARsafe file /x Px,: the keyword goes after filetree
syntax error, unexpected TOK_AUDIT, expecting TOK_ID or TOK_MODE or TOK_SET_VARunsafe audit /x Px,: the keyword goes after the qualifierstree
warning downgrading change_profile safe rule to unsafe due to lack of necessary kernel supportan explicit safe on change_profile when the feature domain/stack is missing; class rule-downgradedtree: printed for a file with no abi line, not printed with abi <abi/5.0>,
warning Unconfined exec qualifier (ux) allows some dangerous environment variables to be passed to the unconfined process; 'man 5 apparmor.d' for details.a lowercase ux; class dangerous, shown with --warn=dangerous or --warn=alltree
  • The downgrade warning is printed only for an explicit safe. A change_profile rule with no keyword compiles to the same bytes and prints nothing (tree).
  • The ux warning is tied to the lowercase letter. unsafe /usr/bin/demo-helper Ux, has the same effect as ux and prints nothing (tree); pux and cux print nothing either (code).

Known discrepancies

  • unsafe /a ix, is accepted. By the kernel code the bit has no effect on an ix exec, yet the parser treats the rule as a different exec mode: together with a plain /a ix, it fails with has merged rule /a with conflicting x modifiers (tree).
  • apparmor.d(5) says the kernel's "unsafe_exec routines" are invoked (parser/apparmor.d.pod:975, 1001, 1022). Mainline Linux 7.4 has no function of that name; the mechanism is the secureexec flag described above.
  • The descriptions of parser/tst/simple_tests/xtrans/simple_bad_x_mods_first_35.sd and simple_bad_x_mods_first_36.sd say that in leading permissions "P and p are the same". The parser treats them as different in both spellings.

for AppArmor developers for security researchers

Code map

StepWhere
mode letters: cod_t::PROFILE = 'P', UNSAFE_PROFILE = 'p', LOCAL = 'C', UNSAFE_LOCAL = 'c', UNCONFINED = 'U', UNSAFE_UNCONFINED = 'u'parser/parser.h:198-203
AA_EXEC_UNSAFE (0x100 per half), ALL_AA_EXEC_UNSAFE, AA_EXEC_BITSparser/immunix.h:43, 89-92
parse_sub_perms: the lowercase cases set tperms = AA_EXEC_UNSAFE and fall through; the ux warningparser/parser_misc.c:628-632, 649-651
keywords safe, unsafe; opt_exec_modeparser/parser_misc.c:92-93; parser/parser_yacc.y:1065-1067
file_rule_tail: the keyword sets or clears the bit; "missing exec permissions"parser/parser_yacc.y:1117-1130
change_profile action: AA_EXEC_BITS with an exec condition, ALL_AA_EXEC_UNSAFE for unsafe, the downgrade warning, the two errorsparser/parser_yacc.y:1648-1675
lexer: safe and unsafe are keywords in CHANGE_PROFILE_MODE until ->parser/parser_lex.l:593-603
features_supports_stacking from domain/stackparser/parser_main.c:1093-1095
process_dfa_entry: the bits go on the pair rule exec condition, NUL, targetparser/parser_regex.c:767-775
kernel: dfa_map_xindex maps 0x100 to AA_X_UNSAFElinux:security/apparmor/policy_compat.c:29-30
profile_transition: the own-label case jumps past the testlinux:security/apparmor/domain.c:730-739
profile_transition: a learning profile forces AA_X_UNSAFElinux:security/apparmor/domain.c:767
profile_transition: *secure_exec = true when the bit is clearlinux:security/apparmor/domain.c:776-784
profile_onexec: the same test for a change requested for execlinux:security/apparmor/domain.c:837-863
apparmor_bprm_creds_for_exec: the local unsafe holds "secure exec requested"; one flag for all profiles of the label; bprm->secureexec = 1; PER_CLEAR_ON_SETIDlinux:security/apparmor/domain.c:994, 1032-1035, 1070-1089
create_elf_tables: NEW_AUX_ENT(AT_SECURE, bprm->secureexec)linux:fs/binfmt_elf.c:262
  • Parser fixtures: parser/tst/simple_tests/file/front_perms_ok_2.sd, parser/tst/simple_tests/xtrans/, parser/tst/simple_tests/change_profile/ (safe_ok_*, unsafe_ok_*, safe_bad_*, unsafe_bad_*, onx_conflict_unsafe*, onx_no_conflict_safe*).

Roads from here

START HERE

FILE ACCESS

RULE CLASSES

LIBAPPARMOR

IN THE KERNEL