change_profile rules
switching profiles for application developers for administrators for security researchers
A change_profile rule lets a confined task ask the kernel to move it to another profile either immediately or at its next exec. The rule names the target after an arrow and may name the executable the change is tied to. With an exec condition the change is safe by default which means the new program starts in secure execution mode. The rule only gives permission. The task asks through libapparmor.
On the map: rule classes
▶ Watch this deck — 9 scenes, ~5 min
Worth seeing first
- Named exec transitions — profile names as targets after an arrow
- safe and unsafe exec — the safe and unsafe words of the exec condition
- Child profiles — the child profile demo changes into
Part of journeys
- Exec, transitions and labels, step 7 of 13 · previous: Hats · next: Profile stacking
01 demo becomes the worker
change_profile -> TARGET, lets a task under this profile ask to continue under TARGET.
Demo has prepared everything with its broad profile, and the child process that will do the risky work should give those rights up. It asks the kernel: move me to the worker profile. The kernel checks demo's own profile first, because a profile that could be left at will wouldn't confine anything. Line twenty-three is the permission: change_profile, arrow, the target. Note what the rule doesn't do: it doesn't switch anything by itself. The program has to make the call.
Details and sources
Details
- The calls are
int aa_change_profile(const char *profile)andint aa_change_onexec(const char *profile)(apparmor.h:80-81). They writechangeprofile NAMEto/proc/self/attr/apparmor/current(or/proc/self/attr/current) andexec NAMEto theexecfile next to it. - The kernel reads the commands at lsm.c:882-904 and calls
aa_change_profile(domain.c:1484-1660).aa-exec -p PROFILE -- CMDuses the same interface. - Feature files:
/sys/kernel/security/apparmor/features/domain/change_profile,change_onexecandstack(apparmorfs.c:2604-2606). - An unconfined task needs no rule (domain.c:295-299).
change_profile -> demo//worker,is line 23 of demo's profile; it compiles with no warning (run with the tree's parser). The programdemoand its profile are invented for the map.- Kernel statements are read from the kernel source and were not run.
02 An exec condition, an arrow, a target
[QUALIFIERS] change_profile [[safe|unsafe] EXEC] [-> TARGET],. No target means any profile.
Three optional parts follow the keyword. First an exec condition: the path of a program, with safe or unsafe in front if you like. Then the arrow and the target, which is a profile name and may be a pattern. Leave the target out and it means any profile. Leave the exec condition out and the change isn't tied to a program. The target can also start with an ampersand, which asks for a stack, or name a profile in another namespace. Those two have their own decks.
Details and sources
Details
- EXEC begins with
/or@{.safeandunsafeare allowed only with EXEC. - TARGET may be a variable, a glob or an alternation (
-> {new_profile1,new_profile2},). A rule without a target means any profile. A target literally namedsafeorunsafeis read as a name after the arrow:change_profile -> safe,is accepted. change_profile -> b,andchange_profile -> &b,are different permissions: the&stays in the compiled target. What a stack target means is in stacking; a target in another namespace is in namespaces.- Seats:
allow,deny,promptandpriority=are accepted, andauditis accepted by the grammar (run with the tree's parser).ownerandotherare refused:owner change_profile -> demo//worker,givesowner conditional not allowed on change_profile rules. - Known discrepancy, on the bug list as "
audit change_profileis never audited, and the warning blames the kernel":audit change_profile ...is accepted and the audit flag is not compiled; see the details of "What demo and the log see". - Known discrepancy, on the bug list as "
owner { change_profile -> x, }is accepted althoughowner change_profileis an error". - Known discrepancy, on the bug list as "
change_profile -> :ns://name,compiles to a pattern the kernel never walks". - Known discrepancy, on the bug list as "Without an
abirule,xattrs=is dropped silently andchange_profile -> &xis refused "by the kernel"": in a profile with noabirule,change_profile -> &x,is refused withThe current kernel does not support stacking of named transitions: &x, whatever the kernel supports (run with the tree's parser). - Known discrepancy, on the bug list as "Stack targets (
&name) are validated and feature-gated only forchange_profile, and only without stacking support". See Known bugs. - Examples in the tree: a_ok_3.sd, stacking_ok_3.sd; apparmor.d(5): apparmor.d.pod:2216, apparmor.d.pod:2235.
Read more
for AppArmor developers for security researchers
In the code
- The production: parser_yacc.y:1648-1691; EXEC must begin with
/or@{(parser_yacc.y:1674-1675);safeandunsafeneed EXEC (parser_yacc.y:1672-1673); an absent TARGET is**(parser_yacc.y:1681). - The scanner state
CHANGE_PROFILE_MODE;->pushesSUB_ID_WS(parser_lex.l:593-608, parser_lex.l:873-874). - The stacking message: parser_regex.c:751-753.
03 Two ways to ask: now, or at the next exec
aa_change_profile() changes now; aa_change_onexec() changes at the next exec. Every change_profile rule grants both.
There are two ways for a program to use this permission. Change now: the calling task continues under the new profile from the next instruction on. Or change at exec: the task says which profile it wants, keeps running as it is, and the switch happens when it executes its next program. The second is the cleaner one, because the new program starts fresh under the new profile, and nothing of the old one's memory comes along. One rule in the profile permits both forms.
Details and sources
Details
- Commands on the
currentattribute file:changeprofile NAME(change now),permprofile NAME(test only) andstack NAME. On theexecattribute file:exec NAMEandstack NAME(lsm.c:882-904). - The kernel's requests are
AA_MAY_CHANGE_PROFILEandAA_MAY_ONEXEC. A change now replaces the label (domain.c:1622-1628); a change at exec stores the target for the next exec (domain.c:1629-1636). At exec the stored target is checked again against the executable (domain.c:837-862). - Every
change_profilerule grants both requests. - Known discrepancy, on the bug list as "Kernel: the unprivileged-unconfined restriction is decided at exec time for
change_onexec, so a task that drops CAP_MAC_OVERRIDE after asking is stacked withunconfined". It applies wherekernel.apparmor_restrict_unprivileged_unconfinedis on; see Known bugs. - "Nothing of the old one's memory comes along" is the general meaning of exec, said as motivation; it is not a finding from the source trees.
- Kernel statements are read from the kernel source and were not run.
Read more
for AppArmor developers for security researchers
In the code
- The parser's note that a change_profile rule also implies the on-exec permission: block_rules.cc:598.
04 Tying the change to one program
change_profile EXEC -> TARGET, permits the change at exec only for executables matching EXEC.
A rule with only a target says: you may become the worker, whatever you execute next. The worker profile then confines a shell just as well as the worker binary, which may not be what you meant. Put the program's path before the arrow and the change at exec is tied to it: executing the worker binary is fine, executing anything else is refused. And watch the short form: one word after the keyword is the exec condition, not the target.
Details and sources
Details
- "Allowed" and "refused" on the slide are derived from the compiled rules and the kernel's check at exec (domain.c:837-862); they were not run. At exec the kernel matches the name of the executable, requires the on-exec permission, and then matches the target. When the executable does not match, the refusal's
infoisno change_onexec valid for executable(domain.c:840). - The exec condition does not limit a change now: a rule with a condition still permits
aa_change_profile()to the target. change_profile /usr/lib/demo/worker,compiles with any profile as the target (run with the tree's parser).- Kernel statements are read from the kernel source and were not run.
for security researchers
Attack surface
change_profile -> T,lets the task enter T with any executable.change_profile,lets it enter any profile visible to it, less restrictive ones included, subject to the kernel's other checks: the target must exist, a tracer must be allowed to trace the new label (domain.c:1581-1585), and under no_new_privs the change must not reduce confinement (domain.c:1612-1619).- A glob target (
-> demo//*) grows with every profile loaded later that matches.
for AppArmor developers for security researchers
In the code
- The three compiled entries (parser_regex.c:764-784): TARGET with change-now and on-exec; EXEC, or any absolute path when there is none, with on-exec; EXEC NUL TARGET with on-exec plus the exec bits. With the condition of the slide they are
demo//worker,/usr/lib/demo/workerand/usr/lib/demo/worker\x00demo//worker(run with the tree's parser).
05 safe is the default
With an exec condition the exec sets AT_SECURE unless the rule says unsafe. safe and unsafe need an exec condition.
When a change happens at exec, the new program inherits its environment from a process that was under another profile. Safe, which is what you get when you write nothing, tells the kernel to start the program in secure execution mode: the dynamic loader then ignores variables like LD_PRELOAD, exactly as it does for a setuid program. Unsafe turns that off, for programs that need their environment. Both words only make sense with an exec condition. And you can't have a safe and an unsafe rule for the same program and target.
Details and sources
Details
- At exec the kernel sets secure execution when the permissions it matched lack the unsafe bit (domain.c:855-862). Secure execution is what sets
AT_SECUREfor the new program; what the loader then ignores is in safe and unsafe exec. - Run with the tree's parser, a rule with an exec condition compiles as safe unless it says
unsafe; a rule without an exec condition carries no exec bits at all. - The two rules at the bottom of the slide, in one profile, give
profile demo: has merged rule demo//worker with conflicting x modifiers, followed byERROR merging rules for profile demo, failed to load(run with the tree's parser). Examples in the tree: theonx_conflict_unsafe_*.sdfiles in parser/tst/simple_tests/change_profile. - Known discrepancy, on the bug list as ""downgrading change_profile safe rule to unsafe" does not describe what is compiled": when the feature set lacks
domain/stack(a profile with noabirule), asaferule prints the warningdowngrading change_profile safe rule to unsafe due to lack of necessary kernel support(classrule-downgraded, off by default) and is compiled exactly like a rule with no mode, without the unsafe bits. - Kernel statements are read from the kernel source and were not run.
for AppArmor developers for security researchers
In the code
- The compiled permissions of the exec pair: 0x40004001 for safe, 0x40404101 for unsafe (run with the tree's parser). The conflict check: block_rules.cc:325. The warning, with no change to the rule: parser_yacc.y:1663-1665.
06 What demo and the log see
Missing rule: EACCES, operation="change_profile" / "change_onexec" with target=. Missing profile: ENOENT, info="label not found".
If the rule is missing, the call fails with EACCES, and the record says operation="change_profile", or change_onexec, with the target. Put that target after the arrow. A different failure looks similar: the target profile isn't loaded, or is misspelt. Then the error is no such entry, and info says label not found. A rule won't help: the profile has to exist. And one silent case: a task that has set no_new_privs can only move to tighter confinement. That refusal isn't logged at all.
Details and sources
Details
- Both records on the slide are assembled from fixtures, with demo's values, which are illustrative. The first follows testcase_changeprofile_02.in (
apparmor="DENIED" operation="change_profile" profile="php-fpm" name="php-fpm//webapp" pid=825 comm="php-fpm" target="php-fpm//webapp") withclass="file"afteroperationas in the newer testcase_changeprofile_03.in. The second follows change_onexec_gl636.in (operation="change_onexec" class="file" info="label not found" error=-2 profile=... name="snap.firefox.firefox" pid=6209 comm="snap-confine"). - The operations are
change_profile,change_onexec,stackandstack_onexec.error=-2isENOENT. - In complain mode a missing target is replaced by a learning profile (domain.c:1536-1558).
- The no_new_privs refusal of the request returns
EPERM, next to the commentnot an apparmor denial per se, so don't log it(domain.c:1612-1619). A change that is refused for the same reason at exec time is logged withinfo="no new privs"(domain.c:1050-1056); an older record of that kind is change_onexec_lp1648143.in. - A tracer must be allowed to trace the new label (domain.c:1581-1585).
- Known discrepancy, on the bug list as "
audit change_profileis never audited, and the warning blames the kernel":audit change_profile ...is accepted and the audit flag is not compiled; the only notice isWarning kernel does not support audit modifier for change_profile rule., shown with--warn=rule-downgradedor--warn=all(run with the tree's parser; adeny change_profilerule prints the same warning). - Known discrepancy, on the bug list as "Kernel:
changeprofile &nameis audited asoperation="change_profile"": achangeprofile &namerequest is logged asoperation="change_profile". See Known bugs. - Kernel statements are read from the kernel source and were not run.
Read more
07 Three strings in the file rules for AppArmor developers for security researchers
parser_regex.c:764-784 adds target, exec and exec NUL target to the file DFA; block_rules.cc:602-608 adds the /proc/*/attr/ rules; domain.c:837-862, 1448-1469 match them.
A change_profile rule isn't a rule_t. The grammar builds a file style entry whose name is the target and whose extra field is the exec condition. Then process_dfa_entry turns that into three entries of the file rules: the target with both permissions; the exec pattern with on exec; and exec pattern, NUL, target, with on exec and the exec bits, where safe and unsafe live. The profile also gets write access to its own attribute files in proc, which is how the request is made. The kernel matches the target for a change now, and the exec name, a NUL, and the target at exec.
Details and sources
for AppArmor developers for security researchers
Details
- The grammar action: parser_yacc.y:1650-1691. The three entries: parser_regex.c:717-785. The implied file rules: block_rules.cc:527-570, block_rules.cc:602-608.
- Dumps from the tree's parser (
-D rule-exprs): demo's line 23 givesdemo//worker0xc0000000,/[^/\x00][^\x00]*0x40000000 and/[^/\x00][^\x00]*\x00demo//worker0x40000000; with the exec condition of the slide,demo//worker0xc0000000,/usr/lib/demo/worker0x40000000 and/usr/lib/demo/worker\x00demo//worker0x40004001; withunsafethe last entry is 0x40404101;change_profile,gives[^\x00]*,/[^/\x00][^\x00]*and/[^/\x00][^\x00]*\x00[^\x00]*. A target&demo//workerkeeps the&in the string. - The implied rules are
/proc/*/attr/apparmor/and/sys/module/apparmor/parameters/enabledwith read, and/proc/*/attr/{apparmor/,}{current,exec}with write (run with the tree's parser). - Only
domain/stackis consulted for feature gating (parser_main.c:1093-1095); without it the target is split into namespace and name (parser_regex.c:742-759). - Kernel: the check at exec (domain.c:837-862); the check of a request (domain.c:1448-1469).
- Kernel statements are read from the kernel source and were not run.
for security researchers
Attack surface
- The implied file rules give every profile with a change_profile rule write access to
/proc/*/attr/{apparmor/,}{current,exec}for any pid in the path pattern. What a write there can do is decided by the kernel's own checks on the attribute interface; see who can change policy. - The audit flag is dropped (known discrepancy, on the bug list as "
audit change_profileis never audited, and the warning blames the kernel"; see "What demo and the log see"), so allowed profile changes cannot be audited per rule.
for AppArmor developers for security researchers
Read more
08 Mistakes and their messages
The target goes after ->. safe/unsafe need an exec condition. Target names are not checked at compile time.
Forget the arrow and the parser reads your target as an exec condition, and complains that it doesn't start with a slash. Safe or unsafe without a program is an error. Owner isn't allowed. Safe and unsafe for the same pair conflict. And a misspelt target is accepted, because profiles can be loaded later. You'll find out at run time, with label not found.
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. The conflict of row 4 is printed without a line number and is followed byERROR merging rules for profile demo, failed to load. - Row 5,
change_profile -> demo//wrker,, is accepted (run with the tree's parser). That it fails at run time withlabel not foundis derived from the kernel source. - In a profile with no
abirule a&target givesThe current kernel does not support stacking of named transitions: &x. Known discrepancy, on the bug list as "Without anabirule,xattrs=is dropped silently andchange_profile -> &xis refused "by the kernel""; the check itself is on the bug list as "Stack targets (&name) are validated and feature-gated only forchange_profile, and only without stacking support". - Two warnings are in the class
rule-downgraded, which is off by default: the warning that says asaferule was downgraded (see "safe is the default"), andWarning kernel does not support audit modifier for change_profile rule. - The tree has 512 example files under parser/tst/simple_tests/change_profile.
09 Asking, stacking, namespaces
change_profile permits; libapparmor asks. & targets: Stacking. :ns: targets: Namespaces.
The worker is now running under its own profile. How a program makes the call is in the libapparmor deck. A target with an ampersand adds a profile on top of the current one, which is stacking. A target with colons crosses into another namespace. And if you'd rather not change the program at all, exec rules can switch profiles without it asking.
Details and sources
Details
[QUALIFIERS] change_profile [[safe|unsafe] EXEC] [-> TARGET],: no target means any profile; no exec condition means any program.- The rule permits; the task asks with
aa_change_profile()oraa_change_onexec(). - 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
CHANGE_PROFILE RULE: [QUALIFIERS] change_profile [ [safe|unsafe] EXEC ] [ -> TARGET ] , QUALIFIERS: [priority=N] [audit] [allow|deny|prompt]
- EXEC is a path pattern; it begins with
/or@{(tree). safeandunsafeneed EXEC; with EXEC and neither word the rule is safe (tree).- TARGET is a profile name; it may be a glob, an alternation or a variable. No TARGET means any profile (tree).
- One word after the keyword is EXEC, not TARGET:
change_profile /usr/lib/demo/worker,(tree).
| Seat | On a change_profile rule |
|---|---|
priority= | taken |
audit | taken by the grammar: the audit flag is not compiled (known discrepancy) |
allow, deny, prompt | taken; prompt is in syn-prompt |
owner, other | refused: owner conditional not allowed on change_profile rules (tree) |
Rule and what it permits
| Rule | Change now | Change at exec | Secure execution at that exec |
|---|---|---|---|
change_profile, | to any profile | to any profile, with any executable | the rule carries no exec bits |
change_profile -> T, | to T | to T, with any executable | the rule carries no exec bits |
change_profile EXEC -> T, | to T | to T, only when executing EXEC | yes (safe is the default) |
change_profile EXEC, | to any profile | to any profile, only when executing EXEC | yes |
change_profile unsafe EXEC -> T, | to T | to T, only when executing EXEC | no |
The first three columns follow from the compiled entries (tree) and the kernel's checks (linux:security/apparmor/domain.c:837-862, 1448-1469, code); nothing was run on a kernel. The kernel sets secure execution when the matched permissions lack the unsafe bit (linux:security/apparmor/domain.c:855-862). By that test a change at exec under the first two rows would also get secure execution, because an entry without EXEC cannot carry the unsafe bit; this is derived from the code and was not run.
Target forms
| Form | Example | Meaning |
|---|---|---|
| name | -> demo//worker | that profile |
| glob or alternation | -> demo//*, -> {a,b} | every profile whose name matches, also ones loaded later |
| variable | -> @{profile_name}//worker | expanded by the parser |
&name | -> &demo//worker | a stack: see stacking |
:ns:name | -> :ns:demo | a profile in another namespace: see namespaces |
Requests and interfaces
| libapparmor call | Attribute file and command | Kernel request | Operation in the log |
|---|---|---|---|
aa_change_profile(name) | /proc/self/attr/apparmor/current: changeprofile NAME | AA_MAY_CHANGE_PROFILE | change_profile |
aa_change_onexec(name) | /proc/self/attr/apparmor/exec: exec NAME | AA_MAY_ONEXEC | change_onexec |
aa_stack_profile(name) | current: stack NAME | stack | |
aa_stack_onexec(name) | exec: stack NAME | stack_onexec |
The commands are read at linux:security/apparmor/lsm.c:882-904 (code). The calls are in lib-change-profile.
The log records and the caller's errors
| The caller gets | When | The record |
|---|---|---|
EACCES | no rule permits the change | operation="change_profile" or "change_onexec", name= and target= the target |
ENOENT | the target profile is not loaded | info="label not found" error=-2, name= the target |
EPERM | under no_new_privs, a request that would reduce confinement | none |
A tracer that may not trace the new label also refuses the change, with info="ptrace prevents transition" (linux:security/apparmor/domain.c:50-82, 1581-1585, code); see stacking.
Records from the tree (libraries/libapparmor/testsuite/test_multi/testcase_changeprofile_02.in and change_onexec_gl636.in):
apparmor="DENIED" operation="change_profile" profile="php-fpm" name="php-fpm//webapp" pid=825 comm="php-fpm" target="php-fpm//webapp" apparmor="DENIED" operation="change_onexec" class="file" info="label not found" error=-2 profile="/usr/lib/snapd/snap-confine" name="snap.firefox.firefox" pid=6209 comm="snap-confine"
Errors and warnings
| Rule | Message |
|---|---|
change_profile demo//worker, | Exec condition must begin with '/'. |
change_profile safe -> demo//worker, | Exec condition is required when unsafe or safe keywords are present |
owner change_profile -> demo//worker, | owner conditional not allowed on change_profile rules |
| a safe and an unsafe rule for the same EXEC and TARGET | profile demo: has merged rule demo//worker with conflicting x modifiers, then ERROR merging rules for profile demo, failed to load |
change_profile -> demo//wrker, | none; the target is not checked |
audit change_profile -> demo//worker, with --warn=rule-downgraded | Warning kernel does not support audit modifier for change_profile rule. |
a safe rule in a profile with no abi rule, --warn=rule-downgraded | warning downgrading change_profile safe rule to unsafe due to lack of necessary kernel support |
change_profile -> &x, in a profile with no abi rule | The current kernel does not support stacking of named transitions: &x |
All rows are from the tree's parser.
for AppArmor developers for security researchers
In the parser and the kernel
- No rule class of its own: the grammar builds a file-style entry with the target as name and EXEC as the extra field (
parser/parser_yacc.y:1650-1691). - Three entries in the file rules (
parser/parser_regex.c:764-784): TARGET with change-now and on-exec (0xc0000000); EXEC, or/[^/\x00][^\x00]*when absent, with on-exec (0x40000000); EXEC NUL TARGET with on-exec and the exec bits (0x40004001 safe, 0x40404101 unsafe, 0x40000000 without EXEC) (tree). - Implied file rules for
/proc/*/attr/apparmor/,/proc/*/attr/{apparmor/,}{current,exec}and/sys/module/apparmor/parameters/enabled(parser/block_rules.cc:602-608). - Kernel:
aa_change_profile(linux:security/apparmor/domain.c:1484-1660); a change now replaces the label, a change at exec stores the target; at exec the kernel matches the exec name, requiresAA_MAY_ONEXEC, steps over a NUL and matches the target (linux:security/apparmor/domain.c:837-862).
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.
audit change_profile ...is accepted and the audit flag is not compiled; the only notice is a warning that is off by default (tree).- Without an
abirule,change_profile -> &xis refused with a message that names the kernel (code). change_profile -> :ns://name,compiles to a pattern the kernel never walks (code).- Stack targets (
&name) are validated and feature-gated only forchange_profile, and only without stacking support (code). owner { change_profile -> x, }is accepted althoughowner change_profileis an error (tree).- A
changeprofile &namerequest is audited asoperation="change_profile"(code). - "downgrading change_profile safe rule to unsafe" does not describe what is compiled: the warning is printed and the rule is compiled like one with no mode (tree).
- Kernel: the unprivileged-unconfined restriction is decided at exec time for
change_onexec, so a task that drops CAP_MAC_OVERRIDE after asking is stacked withunconfined(kernel; on the bug list).
Roads from here
START HERE
- The profile language at a glance — every kind of line on one page
ATTACHMENT, NAMESPACES, STACKING
- Profile stacking — targets that begin with an ampersand
- Policy namespaces — targets in another policy namespace
FILE ACCESS
- safe and unsafe exec — what secure execution removes
- Exec modes — profile changes that happen on exec without asking
- Named exec transitions — the arrow on exec rules
RULE CLASSES
- The all rule — the rule that does not include change_profile
PROFILE STRUCTURE
- Hats — change_hat which is the lighter relative
- Child profiles — the child profile demo changes to
SECURITY · threat model and limits
- Who can change policy (no slides yet) — who may change confinement
LIBAPPARMOR
- aa_change_profile and onexec (no slides yet) — the calls a program makes to use the permission
IN THE KERNEL
- Exec and domain transitions — what the kernel does at exec