Repository navigation
Description
============================================================================
ANT-2026-FQI492GW [medium] CWE-807 authz
coreutils: symlink to a hard-linked utility bypasses env -a protection
Location: src/common/validation.rs:143 in binary_path()
Commit: 7bc1eb6d1d6fa2e1dc2c9d2277cf576c05f61e47
============================================================================
On installs where the multicall binary is hard-linked under each utility name (make install MULTICALL=y), the env -a guard from #10773 can be bypassed. Code that is only allowed to execute one hard-linked utility, such as /usr/bin/ls, can make that exec run any other utility, including env and therefore any program, while the kernel and a pathname-based policy like AppArmor still see /usr/bin/ls. Where an AppArmor profile gives confined code a domain-changing exec rule (Ux/Px/Cx) for a single hard-linked utility, that code can run every other utility in the target domain; with Ux this is a full escape from the profile. The simplest route needs only a symlink named after the wanted utility that points at the hard link. No control over argv[0] is needed.
The cause is binary_path() in src/common/validation.rs (lines 125-144). It picks the utility from AT_EXECFN and argv[0], and the caller of execve supplies both. AT_EXECFN is the unresolved path string passed to execve, not the executed file, and /proc/self/exe is never consulted. Two other clauses of the same condition also lead to argv[0] being trusted:
- An exec path that merely starts with
/proc/or/dev/fd/(e.g.exec -a env /proc/self/root/usr/bin/ls). This needs no write access. - A
#!probe that re-opens the exec path afterexecve, which a rename can race.
The attached reproducer shows route 1. It hard-links the binary as ls and confirms that env -a readlink ls correctly runs ls. It then runs a symlink readlink -> ls, which runs readlink and prints the ls hard link as /proc/self/exe. An end-to-end AppArmor script is included in repro.d/ but is not run by the reproducer, and the ls Ux profile it uses is constructed. No shipped profile is known to contain such a rule.
The guard was added in b6c5dd1 (first released in 0.8.0). Earlier releases, and musl, 32-bit Android and non-Linux builds at this commit, trust argv[0] outright and are more directly exposed. The default one-binary-per-utility install, symlink-to-coreutils installs and SELinux are not affected. A patch that dispatches on /proc/self/exe when it names a utility closes all three routes.
Details
binary_path() is the guard that makes a hard link of the multicall binary (/usr/bin/ls) run the utility it is named
after, so that a per-utility MAC rule for that path yields that utility and nothing else (b6c5dd1, #10773). It decides
from two strings, AT_EXECFN and argv[0], and the caller of execve supplies both. The file the kernel actually
executed, which is what a pathname-based LSM matched its exec rule against, is never consulted. The guard can therefore
be bypassed in three ways, each through a different clause of the same condition (src/common/validation.rs:133-144),
and one change closes all of them (see Suggested fix):
- A symlink named after another utility that points at the hard link (lines 125-127 and 133; returned at 141 or 143). Deterministic, no
control overargv[0]needed. This is the routereprodemonstrates and the one described first below. - An exec path that starts with
/proc/or/dev/fd/, plus a spoofedargv[0](lines 134-135). Deterministic, no
write access needed. - Replacing the exec path with a
#!file afterexecve, plus a spoofedargv[0](the shebang probe, lines
136-139). A race, won at the first attempt when the rename is issued right after the exec.
Route 1: the name comes from the unresolved AT_EXECFN
binary_path() uses AT_EXECFN as "the actual binary path from the kernel" so that a spoofed argv[0] cannot select
another utility of a hard-linked multicall binary. AT_EXECFN is not resolved by the kernel: it is the path string that
was passed to execve. A symlink named after another utility and pointing at a hard-linked utility therefore selects the
utility, and no argv[0] spoofing is involved:
$ mkdir /tmp/bin /tmp/mine && cp target/debug/coreutils /tmp/bin/ && cd /tmp/bin
$ ln coreutils ls # what `make install MULTICALL=y` does (GNUmakefile:74, 312-314)
$ env -a readlink $PWD/ls /proc/self/exe
/proc/self/exe # ls ran: the spoofed argv[0] is ignored, as intended
$ ln -s $PWD/ls /tmp/mine/readlink
$ /tmp/mine/readlink /proc/self/exe
/tmp/bin/ls # readlink ran, in a process whose executable is /tmp/bin/ls
$ ln -s $PWD/ls /tmp/mine/env
$ /tmp/mine/env echo "any program"
any program
The kernel follows the symlink, executes the directory entry ls and records /tmp/bin/ls as the executable. A
pathname-based LSM matches its exec rule against that resolved path. Only the multicall dispatcher believes it is
readlink or env.
src/common/validation.rs:112-145:
/// Get actual binary path from kernel, not argv0, to prevent `env -a` from bypassing
/// AppArmor, SELinux policies on hard-linked binaries
…
let execfn = rustix::param::linux_execfn(); // 125
let execfn_bytes = execfn.to_bytes();
let exec_path = Path::new(OsStr::from_bytes(execfn_bytes)); // 127
let argv0 = args.next().unwrap();
…
if execfn_bytes.rsplit(|&b| b == b'/').next() == argv0.as_bytes().rsplit(|&b| b == b'/').next() // 133
|| …
{
argv0.into() // 141
} else {
exec_path.into() // 143
}In the benign case, env -a readlink /tmp/bin/ls, AT_EXECFN is /tmp/bin/ls and argv[0] is readlink. The base
names differ, line 143 returns /tmp/bin/ls, and main() (src/bin/coreutils.rs:56-70) dispatches on the file name
ls.
For /tmp/mine/readlink, exec_path is /tmp/mine/readlink. When the program is started by name, argv[0] has the
same base name and line 141 returns it. When argv[0] is something else (ls, cat), line 143 returns exec_path.
Both paths have the file name readlink, and main() runs readlink; each case was run and prints /tmp/bin/ls.
Nothing compares the name with the link the kernel executed. AT_EXECFN is caller input just like argv[0]; the only
constraint on it is that it resolves to the file.
Dispatching on the symlink's name is required when the target is the multicall binary itself (ls -> coreutils). In
that layout all utilities share the path coreutils, so no per-utility policy exists to bypass. The defect is that the
same rule applies when the target is already a hard link named after one utility, which is the layout the doc comment
sets out to protect.
The effect on a policy was observed on Linux 6.1 with AppArmor enforcing (repro.d/apparmor_escape.py), using a
profile constructed for the demonstration. repro does not run it, because it loads a policy into the host kernel;
what repro shows is that another utility runs while /proc/self/exe is the ls hard link, and AppArmor matching its
exec rule against that resolved path is standard behaviour. The attacker process is confined by
profile uu_sandbox {
/usr/** mr, /etc/** r, /proc/** r, /dev/** rw, /tmp/repro.d/** r,
/tmp/uu-aa-XXXX/scratch/** rw,
/tmp/uu-aa-XXXX/ls Ux, # the only exec rule
}
Inside it, /proc/self/attr/current reads uu_sandbox (enforce), and open(secret) and execve("/usr/bin/cat") fail
with EACCES. The attacker then creates a symlink in scratch/ to the ls hard link and executes it with the
arguments cat /proc/self/attr/current secret. With the symlink called ls, ls lists those names. With the symlink
called env, the exec is allowed by the same Ux rule (the profile grants no x below scratch/), env runs cat,
and the output is unconfined followed by the content of the secret.
Route 2: a /proc/ or /dev/fd/ exec path makes argv[0] trusted
The full condition is
if execfn_bytes.rsplit(|&b| b == b'/').next() == argv0.as_bytes().rsplit(|&b| b == b'/').next() // 133
|| execfn_bytes.starts_with(b"/proc/") // 134
|| execfn_bytes.starts_with(b"/dev/fd/") // 135
|| (File::open(Path::new(exec_path)) // 136
.and_then(|mut f| f.read_exact(&mut shebang_buf))
.is_ok()
&& &shebang_buf == b"#!") // 139
{
argv0.into()Lines 134-135 are there for memfd_create/fexecve, where AT_EXECFN is /proc/self/fd/N or /dev/fd/N and carries
no name (binary_name_execfn_fallback, tests/test_util_name.rs:49). They are a byte-prefix test on a string the caller
chose, and their result is "believe argv[0]", which the same caller chose. Any on-disk hard link has spellings with
those prefixes. With the layout above:
$ env -a readlink /proc/self/root/tmp/bin/ls /proc/self/exe
/tmp/bin/ls # readlink ran, in a process whose executable is /tmp/bin/ls
$ env -a readlink /dev/fd/3 /proc/self/exe 3</tmp/bin/ls
/tmp/bin/ls
Each of these was run against the hard link ls with argv = ["readlink", "/proc/self/exe"] and printed the path of
the ls link: /proc/self/root/<absolute path>, /proc/self/cwd/ls, /proc/../<absolute path> (the prefix is only
lexical), /proc/self/fd/N and /dev/fd/N (N may be an O_PATH descriptor, so read access to the file is not
needed), and fexecve(N, …) / execveat(N, "", …, AT_EMPTY_PATH), for which the kernel itself writes /dev/fd/N. The
result does not depend on the pair of utilities: a hard link rm executed as /proc/self/fd/N with
argv = ["chmod", "0666", file] ran chmod (the file ended with mode 0666), while the same argv executed by the
link's plain path ran rm and removed the file. The existing test binary_name_protection
(tests/test_util_name.rs:34) covers only the plain path. Line 134 is part of the original guard commit b6c5dd1; line
135 was added by 49a1261 ("coreutils: accept /dev/fd/ execfn prefixes", first released in 0.12.0).
This route was also checked under an enforcing AppArmor profile on Linux 6.1 whose rules were read access to the
attacker's interpreter and script plus the single exec rule <dir>/ls Ux, with no write access and no rule for /proc
or /dev: execve("<dir>/ls", ["env", "cat", "/proc/self/attr/current", secret]) ran ls, and the same argv with
the path /proc/self/root<dir>/ls was admitted by the same rule, ran env, and printed unconfined followed by the
secret.
Route 3: the shebang probe re-opens a path the caller can replace
The last clause is meant for a script that starts with #!/usr/bin/ls: the kernel puts the script's path in AT_EXECFN
and takes argv[0] from the #! line, so argv[0] is kernel data and trusting it is correct. The probe does not
establish that this is how the process was started. It establishes what the AT_EXECFN string resolves to when
File::open runs, which is 73 system calls after the execve (dynamic loader and runtime start-up, counted with
strace), and nothing ties that second resolution to the first:
execve("./alias", ["rm", "-v", "/tmp/w/home/canary"], …) = 0 # alias -> /tmp/w/bin/ls, a hard link
rename("two_bytes", "alias") # the caller; two_bytes contains "#!"
openat(AT_FDCWD, "./alias", O_RDONLY|O_CLOEXEC) = 3
read(3, "#!", 2) = 2
unlinkat(3, "canary", 0) = 0 # rm ran in a process whose executable is bin/ls
The symlink's own name does not have to be a utility (alias is not; without the rename the same exec ends with
coreutils: unknown program 'alias'), and the path has neither prefix, so this works where only the probe is left. The
caller does not need to hammer the path: it knows when execve has succeeded (a close-on-exec pipe, which is what
subprocess.Popen waits for) and then does one rename; in practice that wins at the first attempt. A second
process that blindly swaps a symlink and a #! file over the path wins about one exec in five (73 to 116 of 500 over
repeated runs). Failed attempts have no side effect.
Impact
The attacker is code confined by a pathname-based MAC profile (AppArmor is the one named in the doc comment) on a system
where the multicall binary is installed with hard links, as make install MULTICALL=y does. The profile lets that code
execute one utility with a profile transition (/usr/bin/ls Ux, Px, Cx) and create a symlink somewhere, for
example in a private temp or cache directory. The attacker can then run every other utility of the binary under the
transition's target. With a symlink called env (or nice, timeout, nohup) that is an arbitrary program, so a Ux
rule becomes a full escape from the profile. With Px/Cx, cp, dd, rm or chmod run under the profile that was
written for one harmless utility: in the same setup with the rule changed to Px and a profile for the ls path that
allows /** r, a symlink called cat printed the secret from a process confined by the ls profile. An ix rule
yields nothing, because the utility stays in the caller's profile. No shipped profile is known to contain such a rule
(the ls Ux rule in the demonstration is constructed), so the issue matters only where a profile gives one hard-linked
utility a domain-changing exec rule.
This is the outcome the AT_EXECFN check was added to prevent for env -a, and it needs less than that attack did:
control over argv[0] is not required, so it works wherever the attacker can only name a program (a shell command
line, system(), find -exec, a configured helper path). It does not depend on the /proc/, /dev/fd/ and shebang
exceptions in the same condition.
Those exceptions give the same result under other preconditions. Route 2 needs control over the exec path and argv[0]
(env -a, exec -a, or a direct execve) but no writable directory at all, so it also works against a profile that
grants the confined code no write access; launchers that canonicalise the command before execve cannot be used for it.
Route 3 needs a writable directory, like route 1, and argv[0] control, like route 2; it is what remains when the
symlink's name and the path prefix carry no utility name.
Installs with one binary per utility (MULTICALL=n, the default) are not affected, and neither are installs that use
symlinks to coreutils: there every name resolves to the same path, so a path-based policy has nothing to distinguish.
SELinux, which the doc comment on binary_path() names next to AppArmor, is not affected either: it labels the file,
not the name, and hard links share one label, so no per-name SELinux policy can exist on a hard-linked install.
The AT_EXECFN check was added in b6c5dd1 ("coreutils: Protect against env -a for security (#10773)", 2026-04-05,
first released in 0.8.0). Earlier releases are exposed as well, and more directly: they dispatch on argv[0] alone,
so a plain exec -a env /usr/bin/ls already selects env. The same holds at this commit for musl, 32-bit Android and non-Linux
builds, which compile the argv[0]-only binary_path() at src/common/validation.rs:101-111.
Reproducing
repro is a single Python script (standard library only). It runs the prebuilt /src/target/debug/coreutils and cleans up after itself.
- Set up (the administrator's part). It copies the binary into a temporary
bin/directory and hard-links it asbin/ls, which is whatmake install MULTICALL=ydoes. - Attack (the attacker's part). In a separate
attacker/directory it creates one symlink,readlink -> bin/ls. The attacker needs nothing else: no control overargv[0], and no write access tobin/. - Runs. Both commands execute the directory entry
bin/lswith the operand/proc/self/exe.lsprints the operand back.readlinkprints the executable the kernel recorded for the process, which is also the path a pathname-based LSM checks its exec rule against. So the output shows which utility ran and which executable the kernel saw.CONTROL:bin/lsrun withargv[0]set toreadlink. This is theenv -acase thatbinary_path()guards against.lsruns and prints'/proc/self/exe\n'.OBSERVED: the attacker's symlink run by its path, as a shell would run it.readlinkruns and prints the path of thelshard link.
Expected output on this tree (the temporary directory name varies):
CONTROL /tmp/tmpXXXX/bin/ls run with argv[0]='readlink' printed '/proc/self/exe\n'
OBSERVED /tmp/tmpXXXX/attacker/readlink -> /tmp/tmpXXXX/bin/ls printed '/tmp/tmpXXXX/bin/ls\n'
BUG
BUG is printed only if both of these hold: the control printed the operand (the argv[0] guard works), and the symlink run printed the path of the ls hard link. The second condition means that readlink ran in a process whose executable, as the kernel sees it, is bin/ls. Once the dispatcher follows the executable the kernel resolved, both runs print /proc/self/exe and the script prints OK.
repro exercises route 1 only. Route 2 can be checked by hand with the two commands shown for it under Details. Route 3 needs a small script that executes a symlink to the hard link with a spoofed argv[0] and renames a file containing #! over the symlink once the exec has returned.
repro.d/ contains the end-to-end AppArmor demonstration referred to in the details, carried over unchanged. repro does not run it, because it needs a host kernel with AppArmor enabled and it changes kernel state: it loads one profile, uu_sandbox, which attaches to no executable, and removes the profile at the end. To run it as root: python3 /tmp/repro.d/apparmor_escape.py. For a symlink called ls and then one called env, it prints the attacker's confinement, the two accesses that were denied (open(secret) and execve("/usr/bin/cat")), and what the executed symlink printed. With env, the output is unconfined followed by the secret.
apparmor_escape.py: setup. It installs the hard link in a temporary directory, writes the secret, mounts securityfs there and loads the profile. It then starts the attacker inside the profile once for each symlink name, and finally removes the profile and the directory.confined_attacker.py: the attacker. It shows its confinement and the two denied accesses, creates the symlink in the scratch directory it is allowed to write to, and executes the symlink.compile_apparmor_profile.py: setup. The image has noapparmor_parser, so this script writes the binary form of the seven profile rules. Loading the textual profile withapparmor_parseris equivalent.
Suggested fix
The attached patch asks the kernel for the resolved executable. It adds validation::resolved_exe()
(src/common/validation.rs), which reads /proc/self/exe, the path after symlink resolution, on Linux and Android and
returns None on other targets. main() (src/bin/coreutils.rs) prefers that path whenever its file name matches a
utility: the binary is then a hard link (or a copy) named after that utility, and that name wins over AT_EXECFN and
argv[0]. Otherwise (coreutils, busybox, a memfd, /proc not mounted) the result of binary_path() is used as
today, so installs that use symlinks to coreutils keep working. The util-name matching is factored into a closure so
both places apply the same rule (longest suffix, *utils excluded). The patch also adds
binary_name_protection_symlink_to_hardlink to tests/test_util_name.rs: a symlink false pointing at a true hard
link must run true.
With the patch applied to a clean checkout and rebuilt, repro prints OK; a symlink readlink -> coreutils,
coreutils readlink, shebang scripts and the memfd dispatch behave as before, and test_util_name passes. The cfg of
resolved_exe() depends neither on the C library nor on the pointer width, so the check also applies to the musl and
32-bit Android builds that have no guard today. Two behaviour changes to decide on: where /proc is not mounted the
check falls back to the current behaviour, so the symlink still selects the utility there; and a symlink alias to a
hard-linked utility now runs the hard link's utility (dir -> ls runs ls), just as a multicall binary whose own file
name ends in a utility name (mytools matching ls) no longer follows the names of symlinks pointing at it.
fs::canonicalize(exec_path) is not a substitute. It resolves the path a second time after the exec, and the owner of
the symlink can repoint it in between.
The same change closes routes 2 and 3, because for a hard link named after a utility neither AT_EXECFN nor argv[0]
nor the probe is consulted any more. With the attached patch applied and the binary rebuilt, the /proc/self/root<path>
and /dev/fd/N (O_PATH) execs with argv[0] = readlink print /proc/self/exe (ls ran), the rename-after-exec race
ran ls in 20 of 20 attempts, and the blind swap loop gave 0 of 500. Fixing the clauses one by one does not work:
- deleting lines 134-135 stops route 2 only (routes 1 and 3 still reproduce), and it breaks the memfd/
fexecvedispatch
thatbinary_name_execfn_fallbacktests; - deleting the probe at 136-139 stops route 3 only (routes 1 and 2 still reproduce), and it breaks scripts that use a
utility as their interpreter.
The regression test in the patch covers the symlink. It is worth extending it to exec the true hard link with
arg0("false") through /proc/self/root<path>, /proc/self/fd/N and /dev/fd/N as well. The doc comment on
binary_path() is also worth correcting while there: it names SELinux, which cannot express a per-name policy on hard
links, and it calls AT_EXECFN the "actual binary path from kernel", which it is not.
b6c5dd1 deliberately avoided /proc/self/exe so that the guard also works under a masked /proc. The change does
not take that away: when the link cannot be read, the present logic decides, so the plain-path env -a case is still
refused there. The three routes stay open on such systems, though, and for fexecve (AT_EXECFN = /dev/fd/N) nothing
names the executed file. The remaining choice there is between keeping the current behaviour and, when /proc/self/exe
is unavailable, refusing to dispatch from argv[0] and dropping the #! clause.
Running the reproducer
ANT-2026-FQI492GW/repro, next to this file, with its companion files in ANT-2026-FQI492GW/repro.d/ (apparmor_escape.py, compile_apparmor_profile.py, confined_attacker.py) — the script reads them from /tmp/repro.d/. It was written by an automated agent and our check ran it as root in a privileged container: run it INSIDE A THROWAWAY VM, with the image oss-scan-7bc1eb6d1d6f built by bash build/build-image.sh, from the archive's top directory:
docker run --rm --network=none --privileged -u 0 -w /src -v "$PWD/ANT-2026-FQI492GW/repro:/tmp/repro:ro" -v "$PWD/ANT-2026-FQI492GW/repro.d:/tmp/repro.d:ro" oss-scan-7bc1eb6d1d6f /tmp/repro
It prints its evidence; the last line is BUG if the issue triggered, OK otherwise.
Activity
oech3 commentedon Oct 25, 2026
I cannot work on this soom. cc @Ecordonnier @julian-klode
oech3 commentedon Oct 25, 2026
Hmm, I might missing something. But supporting symlinks is expected. No?
oech3 commentedon Nov 26, 2026
Do we just need to repeat readlink(2) until it fails (with hardlink)?