Symlinks: rules see through them
rules see the target for application developers for administrators for security researchers
AppArmor matches the path of the object the kernel ends up at after following every symlink and never the name the program passed. A rule for a symlink's own path grants nothing on its target. Covers the paths shipped profiles spell out and what creating and reading a symlink need and what a planted symlink can do to other programs.
On the map: security-sensitive programs
▶ Watch this deck — 10 scenes, ~8 min
Worth seeing first
- Profiles for security-sensitive programs — the precise style whose reviews include this one
Part of journeys
- Hardening a privileged program, step 3 of 11 · previous: Hard links and name-based rules · next: Writing rules for /sys and /proc
01 You allowed the file, and it is still denied
AppArmor matches the resolved path. Where /etc/resolv.conf is a symlink, opening it is checked as its target, and name= in the denial shows the target.
You wrote a rule that lets demo read /etc/resolv.conf. Demo opens exactly that path, and it's denied. Why? Look at the name in the denial. It isn't the path in your rule. It's a file under /run. On a system where a resolver manages that file, resolv.conf is a symlink, and AppArmor checks the place the kernel arrives at, not the name the program used. This deck is about that one fact and what follows from it: how to write rules that match, what a symlink can't do, what you need to create one, and what an attacker can do with one.
Details and sources
Details
- The string to put in a rule is the value of
name=in the denial. It is the name AppArmor rebuilt for the object that was opened, and it can differ in every component from what the program passed. requested_mask="r"is what the program asked for anddenied_mask="r"the part that was refused.profile=is the profile that refused it.- The record on the slide is built for the map: the field layout is that of an
operation="open"record in the libapparmor test suite (syslog_datetime_01.in), with the profile, name and masks of this example. The values are illustrative. - Whether
/etc/resolv.confis a symlink depends on the system; neither source tree proves it. The tree's evidence is the comment in the shipped abstraction: "On systems where /etc/resolv.conf is managed programmatically, it is a symlink to @{run}/(whatever program is managing it)/resolv.conf." (nameservice-strict:34-35), followed by rules for the targets, next to@{etc_ro}/resolv.conf r,for the case of a regular file (nameservice-strict:21). - On such a system the name starts with
/run/and not/var/run/, because/var/runis resolved as well where it is a symlink. - The kernel behaviour is read from the kernel source; nothing on this slide was observed on a running kernel.
Read more
- apparmor.d(5): Access Modes Details
- apparmor.net: Troubleshooting and the Denials quick guide
- Reading denials: every field of the record. File rules.
for AppArmor developers for security researchers
Code
- The name is built from the mount and dentry of the opened object by
d_namespace_path(path.c:88-128); the open hook passes&file->f_path(lsm.c:498-500).
02 The kernel walks first and asks AppArmor last
In the kernel symlinks are followed in the VFS before AppArmor's file hook. Only the final object's path is matched; directories and links on the way are not checked by AppArmor.
Here's what happens between the open call and the check, on a system where /bin is a symlink to /usr/bin, and sh in there is a symlink to dash. The kernel walks the path one component at a time. At /bin it finds a symlink and carries on in /usr/bin. At sh it finds another, and lands on dash. AppArmor isn't asked anything along the way: in the kernel it has no hook for following a link, and none for the directories passed through. Only at the end does the kernel ask: may demo read /usr/bin/dash? So a rule for /bin/sh matches nothing.
Details and sources
Details
All of this is read from the kernel source. That /bin is a symlink to usr/bin and /usr/bin/sh a symlink to dash describes a common layout, not a fact from either tree.
- The last component of a path is followed only when the call follows links.
O_NOFOLLOW,lstatandAT_SYMLINK_NOFOLLOWstop at the link itself. - A link text that starts with
/restarts the walk at the task's root. - Mounts with
nosymfollow, andopenat2withRESOLVE_NO_SYMLINKS, fail withELOOPin the VFS, before any AppArmor check. - AppArmor has no hook for following a link, none for
readlink, and none for the permission check on each directory passed through. Search permission on directories (xin the file mode) is left to the ordinary Unix permission checks. - No rule is needed for a symlink's own path in order to pass through it.
Read more
- linux: Documentation/filesystems/path-lookup.rst: how the kernel walks a path, pinned to the tree read for this deck.
- How the kernel matches a name, the limits of deciding by name.
for AppArmor developers for security researchers
Code
pick_linkreads the link text and continues the walk (namei.c:2013-2091); it callssecurity_inode_follow_linkat namei.c:2059-2062.step_into_slowpathdecides whether an entry is a symlink that must be followed (namei.c:2104-2116).- AppArmor's hook list has no
inode_follow_link,inode_readlinkorinode_permissionentry (lsm.c:1796-1816). apparmor_file_openchecks&file->f_path, the object the walk ended at (lsm.c:498-500).- The
ELOOPfornosymfollowandRESOLVE_NO_SYMLINKS: namei.c:2046-2048.
03 The name comes from the file, not from you
The matched name is rebuilt from the object: absolute, without ., .. or symlinks, from the mount namespace root (so a chroot's files keep their outside path), with a trailing / for directories. No path to the root means info="Failed name lookup - disconnected path".
It helps to turn the picture around. AppArmor doesn't take your path and tidy it up. In the kernel it starts from the file the kernel found and rebuilds a name from that file up to the root. So dots and symlinks simply aren't there. If the program runs in a chroot, the name still starts at the system's root, which is why the shipped profile for ntpd lists files under its chroot directory. A directory's name ends with a slash. And if the file can't be reached from your root at all, say it came from another mount namespace, there's no full name. The access is denied as a disconnected path, unless the profile carries the attach_disconnected flag.
Details and sources
Details
Read from the kernel source; not observed on a running kernel.
- The path with dots in the first case is an illustration: the name is the chain of real directory entries above the object, so it contains no
., no..and no symlink. - A file reached through a bind mount is named through that mount.
- Chroot: the name starts at the root of the mount namespace. The shipped ntpd profile lists the files ntpd opens after it has changed root to
/var/lib/ntp, for example/var/lib/ntp/@{PROC}/@{pid}/net/if_inet6 r,(usr.sbin.ntpd:57, usr.sbin.ntpd:72-73). The profile flagchroot_relativemakes the name relative to the task's root instead. - A directory's name gets a trailing
/, so a rule for the directory itself is written/etc/demo/ r,. - When the lookup fails the request is denied and the record carries one of four
info=texts:Failed name lookup - deleted entry,Failed name lookup - disconnected path,Failed name lookup - name too long,Failed name lookup. The record shows whatever partial name was built. flags=(attach_disconnected)puts the leading/back on a disconnected name.flags=(attach_disconnected.path=/att/unix-chkpwd/)puts a prefix in front, and the shipped profile then has rules with that prefix, such as/att/unix-chkpwd/run/systemd/journal/dev-log w,(unix-chkpwd:14, unix-chkpwd:41-46).- File systems that are internal to the kernel and cannot be mounted by users are skipped by the path checks.
- The one place the program's own spelling can appear: an exec whose name lookup failed is audited with the string as given.
Read more
- apparmor.d(5): Flags
- wiki: profileflags
- Other profile flags, how the kernel matches a name, attachment (the scene on disconnected paths), namespaces.
for AppArmor developers for security researchers
Code
aa_path_name(path.c:202-222) callsd_namespace_path(path.c:88-128):d_absolute_pathat path.c:124-127,__d_pathforchroot_relativeat path.c:119-123, the trailing slash at path.c:170-177, the disconnected case at path.c:48-72 and path.c:151-152.- The
infostrings: path.c:209-216. aa_path_permruns the check for every confined profile of the label (file.c:287-309); a failed lookup is audited and denied at file.c:169-177.- Which
struct patheach hook passes: truncate, chmod, chown, getattr (lsm.c:246-259, lsm.c:350-353, lsm.c:450-463); create, mkdir, mknod, symlink, unlink, rmdir (lsm.c:271-278); rename (lsm.c:403-406); open (lsm.c:498-500); exec (domain.c:684-699). SB_NOUSERfile systems are skipped: lib.h:146-149.- The parser maps the flag to the kernel's path flags at parser_interface.c:536-537.
04 Write the rule for where the file really is
Name the target, and both spellings where layouts differ: /{usr/,}bin/, @{run}=/run/ /var/run/, @{run}/systemd/resolve/stub-resolv.conf r,. Find it with readlink -f PATH or the denial's name=.
So how do you write rules that hold? Look at what ships. The profile for ping attaches to /bin and to /usr/bin, so it works whether bin is a directory or a link. The run variable has two values, /run and /var/run. The name service abstraction grants resolv.conf where it might be a real file, and also the places where resolvers keep the real one. Base does the same for local time and the zone info tree. The method is always the same: name the target, and if systems differ, name both. To find the target, run readlink -f on the path, or take the name from the denial, which is already resolved.
Details and sources
Details
- The shipped lines on the slide, with whitespace collapsed: -
profile ping /{usr/,}bin/{,iputils-}ping {(bin.ping:15). A second example of both spellings is/{usr/,}bin/ls mix,(bash:45). -@{run}=/run/ /var/run/(run:1). -@{etc_ro}/resolv.conf r,(nameservice-strict:21) and@{run}/systemd/resolve/stub-resolv.conf r,with its neighbours (nameservice-strict:36-38). -@{etc_ro}/localtime r,(base:34-35) and/usr/share/zoneinfo{,-icu}/** r,(base:41-42). - Which of these paths are symlinks depends on the system. The trees show only that shipped policy is written to cope with both cases.
readlink -f PATHandrealpath PATHresolve every component and print the final path.namei -l PATHshows each step of the walk. The output on the slide is an example for a system where systemd-resolved manages the file; it was not taken from a machine.- Do not add a rule for a symlink's own path in order to follow it: following is not checked. A rule on the link's path is needed only to create, remove or rename the link itself (see the scene "Creating, removing and reading the link itself").
aliasrules rewrite rule paths when the policy is compiled. They are a separate mechanism, shown commented out in alias:13-19.
Read more
- apparmor.d(5): Variables, Alias rules, Globbing
- apparmor.d(5): abstractions/base, abstractions/nameservice
- apparmor.net: Standardized AppArmor policy: where tunables and abstractions live.
- Variables, alias, globbing, /proc and /sys.
05 Exec through a symlink: which profile attaches?
x rules and profile attachment use the resolved path of the executable. Where /usr/bin/editor is a chain of symlinks to /usr/bin/vim.basic, both the rule and the profile must name /usr/bin/vim.basic.
The same thing happens when a program is executed, and here it surprises people twice. On a system that uses alternatives, /usr/bin/editor is a link to a link to the real binary, vim.basic. Demo executes editor. The execute rule that counts is one for the real binary's path; a rule for the link's name doesn't match. And attachment works on the same name. A profile whose attachment says /usr/bin/editor isn't chosen for this program, because the kernel looks for a profile that matches vim.basic. If you profile something that's reached through a link, write the profile for where the binary really lives.
Details and sources
Details
Read from the kernel source; not observed on a running kernel. The chain /usr/bin/editor to /etc/alternatives/editor to /usr/bin/vim.basic describes a system that uses the alternatives mechanism; the profile names editor and vim are invented for the slide.
- At exec the kernel builds the name from the binary it opened. That name is matched against the
xrules of the profile confining the caller, and it is the name the attachment search uses when the kernel has to pick a profile. - A script's interpreter is a separate file with its own resolved path.
- To see which profile a running process has:
aa-status,ps -Z, orcat /proc/PID/attr/current. To see where a path really leads:readlink -f /usr/bin/editor. - A shipped attachment that lists a link location:
profile notepadqq /{{usr/bin,etc/alternatives}/notepadqq,usr/lib/notepadqq/notepadqq.sh} flags=(unconfined) {(notepadqq:7). Theetc/alternativesalternative can match only on a system where that entry is a real file and not a symlink. - If the kernel fails to build a name at exec, the audit record falls back to the string the program passed. That is the one place the program's own spelling can appear in a record.
Read more
for AppArmor developers for security researchers
Code
aa_path_name(&bprm->file->f_path, ...), the fallbackname = bprm->filenameon error, andfind_attachwith the same name for an unconfined task: domain.c:684-699.
06 A symlink cannot widen a profile
With /srv/www/** r,, opening a symlink /srv/www/x that points to /etc/shadow is matched as /etc/shadow and denied. Symlinks cannot extend what a profile allows. This is read from the kernel code; it was not run on a kernel.
All of this cuts in your favour too. Demo may read everything under /srv slash www. Somebody drops a symlink in there that points at the shadow file, hoping demo will serve it. Demo opens the link. The kernel follows it, arrives at /etc/shadow, and that's the name AppArmor checks. Denied. The rule for the web directory doesn't come into it. And since the check is made on the file that was actually opened, there's no gap between checking and opening for someone to swap the link in. A symlink can make your rule miss. It can't make your rule cover more.
Details and sources
Details
This scene is derived from the kernel code. It was not run on a kernel, and the record is built for the map on the layout of an operation="open" record in the libapparmor test suite (syslog_datetime_01.in); its values are illustrative.
- The open check is made on the object that was opened, after the walk. The name is rebuilt from that object, so
/srv/www/xdoes not appear in it. - Because the check and the open concern the same object, replacing the symlink between a check and the open does not change which name is matched. Compare a program that checks a path string itself and then opens it: there the link can be swapped between the two steps, which is the classic symlink attack.
- A hard link is the opposite case. It is a real second name for the file, and the file is judged under that name: see hard links.
- AppArmor's check comes on top of the ordinary Unix permission checks on the target, which still apply.
for AppArmor developers for security researchers
Code
apparmor_file_openpasses&file->f_path(lsm.c:498-500);d_absolute_pathbuilds the name from it (path.c:124-127).
07 Creating, removing and reading the link itself
symlink() needs w on the new name (operation="symlink", requested_mask="c"); the link text is not examined. unlink needs w on the link's path; rename needs rw on the old name and w on the new. readlink is not mediated by AppArmor in the kernel.
Sometimes your program doesn't follow a link, it makes one. Demo publishes a release by pointing a symlink called latest at it. To AppArmor that's creating a file: in the kernel the check is create permission on the new name, and a w rule gives that. Where the link points isn't looked at. Here's a real denial: operation symlink, and the mask is c, for create. Removing the link needs w on the link's own path, and renaming it needs read and write there. Reading where it points, with readlink, isn't checked by AppArmor at all. And opening it, as you now know, is checked on the target. The l permission has nothing to do with any of this.
Details and sources
Details
Read from the kernel source, except the record and the regression test named below.
| Operation | What AppArmor is asked | On which path |
|---|---|---|
symlink(text, new) | create; w grants it | the new name; the text is not examined |
readlink(path) | nothing | not mediated |
lstat(path) | getattr | the link's own path |
unlink(path) | delete; w grants it | the link's own path, with the link's own owner |
rename(old, new) | read, write and delete on the old name (rw grants them); write and create on the new name (w) | the link's own names |
lchown, fchownat and fchmodat with AT_SYMLINK_NOFOLLOW | chown or chmod | the link's own path |
link(old, new) where old is a symlink, without AT_SYMLINK_FOLLOW | a hard link check | the symlink itself is linked |
open with O_PATH and O_NOFOLLOW | nothing | not checked |
open with O_NOFOLLOW, without O_PATH | nothing: the VFS fails with ELOOP |
- The record on the slide is real: symlink.in, without its syslog prefix. The rule
aa-logprofderives from it isowner /home/b.c w,(symlink.profile). c(create) andd(delete) are letters of the log only. No profile letter exists for them;wis what a rule grants.- In a symlink creation record
ouid=is the caller's own fsuid, so anownerrule always matches a creation. - The regression test in the AppArmor tree passes with
won the new name and fails withrlixm(symlink.sh:22-39). lstat: by reading, the getattr permission is granted on every path for policy in the older format, solstatis not denied. For policy that carries a permission table this was not fully determined.
Read more
for AppArmor developers for security researchers
Code
apparmor_path_symlinkrequestsAA_MAY_CREATEand does not useold_name(lsm.c:360-365);common_perm_createtakes the caller's fsuid as owner (lsm.c:319). The VFS calls the hook at namei.c:5835.- rename asks for read, getattr, write, setattr and delete on the old name and for write, setattr and create on the new one (lsm.c:433-442).
- unlink: lsm.c:289-304, lsm.c:327-330. rename: lsm.c:403-442. getattr: lsm.c:460-463.
wmaps toAA_MAY_CREATEamong others (policy_compat.c:84-86); the getattr mapping is at policy_compat.c:103.readlink: the lookup does not follow the last component andsecurity_inode_readlinkis called, for which AppArmor has no hook (stat.c:568-584).AT_SYMLINK_NOFOLLOWin chmod and chown: open.c:726, open.c:846.O_PATHreturns before the open hook: open.c:951-956.ELOOPfor opening a symlink itself: namei.c:4248-4249.
08 Planted links: what AppArmor leaves to others for security researchers
symlink() is checked only as creation of the new name. A confined program can plant links to paths it cannot access; programs that follow them are judged by their own confinement. fs.protected_symlinks covers sticky world-writable directories and the final component only. All of this is what the kernel code permits; none of it was run.
For researchers, here's the other side. In the kernel, creating a symlink asks for create permission on the new name and ignores the text. So a compromised demo with write access to its state directory can plant a link to the shadow file. Demo can't use it: its own open is checked on the target. But a root backup job that copies demo's directory will follow it, and AppArmor has nothing to say about an unconfined process. A confined one is judged by its own profile. The kernel has a separate guard, protected symlinks, but it covers only sticky world-writable directories like /tmp, only the last component of a path, and it's off unless something turns it on. Readlink and O_PATH opens aren't mediated either.
Details and sources
Details
Everything here is derived from the kernel code: it states what the code permits. No exploit was run. /var/lib/demo/report.txt and the backup job are illustrations.
symlink()is checked as creation of the new name; the link text is not passed to the policy check.fs.protected_symlinks: with the value 1, following a symlink is permitted when the follower's fsuid owns the link, or the directory is not both sticky and world-writable, or the directory's owner owns the link. Otherwise the walk fails withEACCESand the kernel logs anAUDIT_ANOM_LINKrecord withfollow_link. That is not an AppArmor record, and no profile rule changes it.- The check is made for the last component of a path only, and
CAP_DAC_OVERRIDEdoes not bypass it. - The variable has no initializer in the kernel source, so the kernel's own default is 0. Which value an installed system sets is outside both trees.
Read more
- kernel docs: protected_symlinks; the same text pinned to the tree read here: linux: Documentation/admin-guide/sysctl/fs.rst:229
- apparmor.d(5): KNOWN BUGS, abstractions/user-tmp
- What is not mediated, the limits of deciding by name, the threat model, hard links (the scene on planted names).
for security researchers
Attack surface
- Link planting. Any directory a profile can write is a place where the confined program can create symlinks to arbitrary paths. That includes
owner /tmp/** rwkl,fromabstractions/user-tmp(user-tmp:21). readlinkis not mediated. Link texts under a path are readable subject to the ordinary Unix permission checks only.O_PATHopens return before the LSM open hook, and so doesfstataton such a descriptor. The shippedwhoprofile records this in a comment: "O_PATH fds bypass mediation" (who:19-24).- Directory traversal is not mediated. Only the final object's name is matched.
- Name-based matching depends on the view of the mount namespace: chroots and bind mounts give the same file other names (see the scene "The name comes from the file, not from you").
- The sysctl covers sticky world-writable directories and the final component, and is 0 unless something sets it. Whether earlier kernels applied it to non-final components was not determined; only the kernel was read.
for AppArmor developers for security researchers
Code
apparmor_path_symlink: lsm.c:360-365.may_follow_link: namei.c:1280-1307, with the comment onCAP_DAC_OVERRIDEat namei.c:1269-1271; called only withWALK_TRAILINGat namei.c:2040-2044.- The variable and the sysctl table: namei.c:1210, namei.c:1217-1225.
do_dentry_openreturns forO_PATHat open.c:951-956, beforesecurity_file_openat open.c:977.security_inode_readlink: stat.c:584.
09 /proc/self and the links that jump
/proc/self/status is matched as /proc/1234/status: write @{PROC}/@{pid}/status r,. /proc/self/exe and /proc/self/fd/N jump to the object and are matched under that object's own path.
One family of symlinks deserves its own slide. /proc/self is a link whose text is your process ID. So when demo opens /proc/self/status, the check is on /proc/1234/status. The word self doesn't reach AppArmor, and a rule containing it matches nothing. Write the rule with the @{PROC} and @{pid} variables instead. Then there are links under each process directory that aren't text at all: exe, cwd, root, and the entries in fd. Open /proc/self/fd/3, and the kernel jumps straight to the file behind descriptor three. AppArmor checks that file's own name, as a fresh open. The proc and sys deck picks up from here.
Details and sources
Details
Read from the kernel source; not observed on a running kernel. The pid 1234 is an example.
/proc/selfis an ordinary symlink whose text is the caller's process (thread group) id./proc/thread-selfresolves to<tgid>/task/<tid>.- The shipped base abstraction already has
@{PROC}/@{pid}/{maps,auxv,status} r,(base:116).@{PROC}is/proc/(proc:12) and@{pid}a pattern for a number (kernelvars:16). - A chain works the same way: on a system where
/etc/mtabis a symlink to../proc/self/mounts, opening it is matched as/proc/1234/mounts. exe,cwd,rootandfd/Nare not text links. The walk switches to the object itself, and the open is matched under that object's own path with the permissions the new open asks for.lstatof/proc/self/fd/3is a getattr on/proc/1234/fd/3.readlinkof it is not mediated by AppArmor; procfs applies its own ptrace-mode check before such a link is used.- If the object behind
fd/Nhas been deleted or cannot be reached from the root, the rules for deleted and disconnected names apply (see the scene "The name comes from the file, not from you").
Read more
- apparmor.d(5): Variables, Special builtin variables
- wiki: TechnicalDoc_Proc_and_ptrace
- linux: Documentation/filesystems/proc.rst, pinned to the tree read for this deck.
- /proc and /sys, builtin variables, variables.
for AppArmor developers for security researchers
Code
proc_self_get_linkreturns the thread group id as text (self.c:11-28).proc_pid_get_linkobtains astruct pathand callsnd_jump_link(base.c:1800-1813), which replaces the walk's position (namei.c:1174-1195).- Deleted and disconnected names: path.c:151-164.
10 Where to go from here
Next: prac-hardlinks, prac-sys-proc, attachment, syn-flags-other, adm-denials, sec-path-limits, k-match.
Three things to keep. Rules name the target. Making a link needs w on its own name. And a link can't widen your profile, though it can mislead somebody else's program. Hard links are the opposite case, a second real name, and they have their own deck. Proc and sys are whole trees of symlinks, with a deck of their own too. The attachment deck covers which profile a binary gets. And for the mechanics underneath, there's the kernel matching deck.
Details and sources
Details
- Rules name the target: the name matched is the resolved path of the object, and
name=in a denial shows it. - Creating, removing or renaming a symlink needs
won the link's own path.land link rules concern hard links. - A symlink cannot give a profile access its rules do not grant on the target. A planted link can still mislead another program, which is judged by its own confinement.
- What each deck answers: - Hard links: what a second real name for a file changes. - /proc and /sys: writing rules for trees that are full of symlinks. - Privileged programs: where exact paths matter most. - Attachment: which profile a binary gets. - Other profile flags:
attach_disconnected. - Reading denials: thename=field and the rest of the record. - The limits of deciding by name, how the kernel matches a name. - File rules, access modes. - Commits read for this deck: AppArmor 836052d21 (6.0.0~alpha2), the kernel-rc2 (97d73c825). Nothing was run on a live kernel.
Read more
Basis of the statements on this page: the kernel behaviour is read from the source of mainline Linux 7.4 (7.4-rc2, 97d73c825) and the shipped policy from the AppArmor tree at 836052d21 (6.0.0~alpha2). Nothing here was observed on a running kernel. "Derived" means reasoned from that code.
Which name is matched
AppArmor does not see the string a program passes. Each file check receives the object the kernel resolved, and the name is rebuilt from it.
| Operation | Object whose path is used |
|---|---|
| open | the opened file, after every symlink was followed |
| exec | the opened binary; the same name is used to search for a profile to attach |
| create, mkdir, mknod, symlink, unlink, rmdir | the entry itself in its parent directory |
| truncate, chmod, chown, getattr | the looked-up object (the link itself only with a no-follow call) |
| rename | both names |
| link | both names: see prac-hardlinks |
Properties of the name:
- Absolute, with no
.,..or symlink: the chain of real directory entries above the object. - It starts at the root of the mount namespace. A task in a chroot at
/srv/chrootthat opens/etc/passwdis matched as/srv/chroot/etc/passwd. The profile flagchroot_relativemakes it relative to the task's root. - A file reached through a bind mount is named through that mount.
- A directory's name ends in
/. - An object that cannot be reached from the root is disconnected: the request fails with
EACCESunless the profile hasattach_disconnected(see syn-flags-other). - A deleted object fails the lookup too, unless the profile's path flags say otherwise.
name=in a denial is this rebuilt name. It is the string to put in a rule.
Operations on a symlink itself
| Operation | What AppArmor is asked | On which path |
|---|---|---|
symlink(text, new) | create; w grants it | the new name; the text is not examined |
readlink(path) | nothing | not mediated |
lstat(path) | getattr | the link's own path |
unlink(path) | delete; w grants it | the link's own path, with the link's own owner |
rename(old, new) | read, write and delete on the old name (rw grants them); write and create on the new name (w) | the link's own names |
lchown, fchownat and fchmodat with AT_SYMLINK_NOFOLLOW | chown or chmod | the link's own path |
link(old, new) where old is a symlink, without AT_SYMLINK_FOLLOW | a hard link check | the symlink itself is linked |
open with O_PATH and O_NOFOLLOW | nothing | not checked |
open with O_NOFOLLOW, without O_PATH | nothing: the VFS fails with ELOOP |
- Following a symlink is not an AppArmor event: no rule is needed, or useful, on the link's own path for that.
- In a creation record
ouid=is the caller's fsuid, so anownerrule always matches. landlinkrules are about hard links only.
Paths that are symlinks on many systems and how shipped policy spells them
Which paths are symlinks on an installed system is not a fact from either source tree. The left column describes common layouts; the trees show only that shipped policy is written to cope with them. The two /proc rows at the end are the exception: those links are created by the kernel.
| Path, where it is a symlink | Shipped rule or variable | Source |
|---|---|---|
/bin to usr/bin | profile ping /{usr/,}bin/{,iputils-}ping {, /{usr/,}bin/ls mix, | bin.ping:15, bash:45 |
/sbin to usr/sbin | profile unix-chkpwd /{,usr/}{,s}bin/unix_chkpwd ... | unix-chkpwd:14 |
/lib, /lib64 to usr/lib... | /{usr/,}lib{,32,64}/** r, | base:74 |
/var/run to /run | @{run}=/run/ /var/run/ | run:1 |
/etc/resolv.conf to a file under /run | @{etc_ro}/resolv.conf r, and @{run}/systemd/resolve/stub-resolv.conf r, with its neighbours | nameservice-strict:21, nameservice-strict:34-38 |
/etc/localtime to a file under /usr/share/zoneinfo | @{etc_ro}/localtime r, and /usr/share/zoneinfo{,-icu}/** r, | base:34-35, base:41-42 |
/etc/alternatives/NAME to the real binary | name the real binary; notepadqq lists etc/alternatives/notepadqq, which can match only where that entry is a real file | notepadqq:7 |
/etc/mtab to ../proc/self/mounts | /etc/mtab r, and @{PROC}/@{pid}/mounts r, | bash:36-37 |
/proc/self to the pid | @{PROC}/@{pid}/{maps,auxv,status} r, | base:116 |
/proc/mounts to self/mounts (kernel) | @{PROC}/@{pid}/mounts r, | bash:37, root.c:383 |
/proc/net to self/net (kernel) | @{PROC}/@{pid}/net/if_inet6 r, | usr.sbin.ntpd:57, proc_net.c:423 |
More on the /proc rows: prac-sys-proc.
Messages
- A failed name lookup denies the request with one of four
info=texts:Failed name lookup - deleted entry,Failed name lookup - disconnected path,Failed name lookup - name too long,Failed name lookup. - Creating a symlink without permission:
operation="symlink"withrequested_mask="c"anddenied_mask="c". The fix is awrule on the new name. A record from the libapparmor test suite:
apparmor="DENIED" operation="symlink" profile="/home/test.sh" name="/home/b.c" pid=8016 comm="ln" requested_mask="c" denied_mask="c" fsuid=0 ouid=0
ELOOPcomes from the VFS and not from AppArmor: opening a symlink itself withO_NOFOLLOW(withoutO_PATH), any symlink on a mount withnosymfollow, andopenat2withRESOLVE_NO_SYMLINKS.EACCESwith anAUDIT_ANOM_LINKrecord (follow_link) comes fromfs.protected_symlinks, below.
fs.protected_symlinks
- A sysctl of the VFS, 0 or 1, independent of AppArmor. Documentation: kernel docs: protected_symlinks.
- With the value 1, following a symlink is permitted when one of these holds: the follower's fsuid owns the link; the directory holding it is not both sticky and world-writable; the directory's owner owns the link.
- Otherwise the walk fails with
EACCESand anAUDIT_ANOM_LINKrecord. No AppArmor record is written and no profile rule changes the result. - It is applied to the last component of a path only.
CAP_DAC_OVERRIDEdoes not bypass it.- The kernel source gives the variable no initial value, so the kernel's own default is 0. Which value an installed system sets is outside both trees. Keeping it at 1 also covers programs that are not confined.
Finding the resolved path
readlink -f PATHorrealpath PATH: every component resolved, the final path printed.namei -l PATH: each step of the walk, with owners and modes.name=in the denial: already the resolved name.- For an executable also check which profile the process carries:
aa-status,ps -Z(see attachment).
for security researchers
Attack surface
Derived from the code; nothing here was run as an exploit.
- Link planting: a profile with
won a directory can create symlinks there to any path, including paths it is denied. That does not help the confined program, whose own opens are matched on the target. Another program that follows the link is judged by its own profile, or by none.owner /tmp/** rwkl,inabstractions/user-tmpis such a rule (user-tmp:21). readlinkis not mediated: link texts are readable subject to the ordinary Unix permission checks only.O_PATHopens return before the LSM open hook, andfstataton such a descriptor is not mediated either; the shippedwhoprofile says so in a comment (who:19-24).- Directory traversal is not mediated: only the final object's name is matched.
- The name depends on the mount namespace's view: chroots and bind mounts give the same file other names.
fs.protected_symlinkscovers sticky world-writable directories and the final component only, and is 0 unless something sets it.- More: sec-unmediated, sec-path-limits.
for AppArmor developers for security researchers
Code
| What | Where |
|---|---|
aa_path_name, the info strings | path.c:202-222 |
d_namespace_path: d_absolute_path, chroot_relative, trailing slash, disconnected and deleted | path.c:88-128, path.c:48-72, path.c:151-177 |
aa_path_perm; a failed lookup audited and denied | file.c:287-309, file.c:169-177 |
AppArmor's hook list (no inode_follow_link, inode_readlink, inode_permission) | lsm.c:1796-1816 |
| create and symlink hooks | lsm.c:271-278, lsm.c:319, lsm.c:360-365 |
| unlink, rename, getattr, open hooks | lsm.c:327-330, lsm.c:403-442, lsm.c:460-463, lsm.c:498-500 |
| exec: name and attachment | domain.c:684-699 |
w to AA_MAY_CREATE; getattr on every state | policy_compat.c:84-86, policy_compat.c:103 |
pick_link, security_inode_follow_link, ELOOP | namei.c:2013-2091 |
may_follow_link, the sysctl | namei.c:1280-1307, namei.c:1210, namei.c:1217-1225 |
nd_jump_link | namei.c:1174-1195 |
security_path_symlink call | namei.c:5835 |
readlink | stat.c:568-584 |
O_PATH in do_dentry_open | open.c:951-956 |
/proc/self, magic links | self.c:11-28, base.c:1800-1813 |
| parser: path flags written to the kernel | parser_interface.c:536-537 |
Not determined
- Whether
lstatof a path matched by no rule can be denied under policy that carries a permission table. For policy in the older format the getattr permission is added to every state. - Which symlinks exist on an installed system and which value distributions set for
fs.protected_symlinks: neither tree contains this. - Whether
fs.protected_symlinkswas applied to non-final components in earlier kernels; only mainline Linux 7.4 was read.
Roads from here
ATTACHMENT, NAMESPACES, STACKING
- Profile attachment — which profile an executable reached through a symlink gets
FILE ACCESS
- File rules — the rule that must name the target
- File access modes — why creating a symlink needs w
PROFILE HEAD
- The other profile flags — attach_disconnected for paths with no root
IN PRACTICE · security-sensitive programs
- Hard links and name-based rules — the other kind of link where the new name does count
- Writing rules for /sys and /proc — /proc/self and /sys/class are symlinks too
- Profiles for security-sensitive programs — where exact paths matter most
SECURITY · threat model and limits
- The limits of name-based mediation — the limits of deciding by name
RUNNING THE PARSER
- Reading a denial — the name field of a denial
IN THE KERNEL
- Matching at run time — how the kernel builds and matches the name