sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

COW cannot serve O_PATH opens of branch entries

Open ghazariann opened this issue 1 month ago • 3 comments

What the workload does

Claude Code's Bash tool creates a tasks directory for its command output, then opens it with O_PATH to check it is still the directory it created, before writing into it. Under a COW workdir (sandlock learn) that directory is created during the run, so it exists only in the upper layer. It lives at <storage>/upper/tmp/..., and only the supervisor knows that. If the open is left to the kernel, the kernel looks for the path as written, does not find it, and fails. So for an entry in the branch there is no correct answer the kernel can give on its own: the supervisor has to open it and hand the descriptor back.

Reproduction

// gcc -o probe probe.c
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>

int main(void) {
    mkdir("/tmp/d", 0755);                 // created during the run, upper layer only
    printf("plain  %d\n", open("/tmp/d", O_RDONLY | O_DIRECTORY));
    printf("O_PATH %d\n", open("/tmp/d", O_PATH | O_DIRECTORY));
    fflush(stdout);
    perror("O_PATH");
    return 0;
}
$ sandlock learn -- ./probe
plain  3
O_PATH -1
O_PATH: Permission denied

Analysis

The supervisor opening the directory itself and handing that descriptor to the child runs into two blockers.

First, the supervisor's own open is refused. The serving open is openat2, which is stricter than the openat the child called. It rejects two combinations real workloads produce: a stray flag beside O_PATH (glibc passes the kernel's __O_LARGEFILE, 0x8000, even though the userspace constant is 0), and a mode set without O_CREAT.

Second, even with the flags accepted, the descriptor cannot be handed over. The only channel is SECCOMP_IOCTL_NOTIF_ADDFD, which resolves the supervisor's source fd with fget(). fget() refuses FMODE_PATH descriptors. The supervisor opens with the child's own flags, so an O_PATH request produces an O_PATH fd on the supervisor side, and ADDFD will not take it.

The failure is not reported

At the first blocker, handle_cow_open in cow/dispatch.rs swallows the failure of the serving open and hands the request to the kernel:

Ok(fd) => fd,
Err(_) => return NotifAction::Continue,

The kernel cannot see the upper layer, so the child gets EACCES for a directory that does exist, and the Bash tool reads that as its directory having been swapped.

Refusing with an errno would at least be honest, and the codebase already does that elsewhere. serve_entry in procfs/net_dispatch.rs returns EOPNOTSUPP rather than working around the same ADDFD restriction.

Open questions

First blocker. The serving open uses the child's flags verbatim, but the supervisor is not forwarding the syscall, it is doing an equivalent open under RESOLVE_IN_ROOT. Should the flags be translated per caller instead, for an open/openat caller only? A direct openat2 caller then keeps its EINVAL for free, since the ABIs already match.

Second blocker. Opening O_RDONLY on the supervisor side would satisfy ADDFD, but does that widen permissions? The supervisor opens with its own credentials, outside the child's Landlock policy, so a path the policy allows traversing but not reading would become readable.

Is there another way to hand the child a descriptor to an upper-layer entry it opened with O_PATH that I missed?

ghazariann avatar Oct 25 '26 08:10 ghazariann

@ghazariann Thanks for your report. There is a known kernel bug which prevents O_PATH working: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=54971c8061943d1cd10290ec754b3597452eb15a . The PR #334 is just to make the failure more honest.

congwang-mk avatar Oct 29 '26 20:10 congwang-mk

Thanks for the PR @congwang-mk. A directory created during the run exists only in the upper layer, so only the supervisor can serve the open, and ADDFD cannot carry an O_PATH descriptor. Until the kernel fix ships, this workload always fails under COW (learn, or run -w).

I tested serving O_PATH as O_RDONLY in handle_cow_open, which makes Claude Code's Bash tool work under learn on top of #334:

if open_flags & libc::O_PATH != 0 {
    open_flags = libc::O_RDONLY
        | (open_flags & (libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC));
}

I am not sure whether this widens permissions. The child asked for a handle it cannot read through and gets one it can, which looks like a widening. On the other hand the same directory already opens read-only. With --workdir /tmp and no grant at all on /tmp/nogrant:

mkdir /tmp/nogrant/made-by-child    CREATED
open(O_RDONLY|O_DIRECTORY)          ALLOWED
open(O_PATH|O_DIRECTORY)            Permission denied

So for an entry in upper a plain O_RDONLY already succeeds with no read grant checked, and only the O_PATH form fails. Which of those is the right way to see it?

Is there any other workaround meanwhile?

ghazariann avatar Nov 01 '26 04:11 ghazariann

Thanks for testing this. Your second reading is the right one: under COW, the workdir governs access to upper-layer entries, not Landlock's read grants. The COW handlers serve those entries without checking fs_readable, so serving O_PATH as O_RDONLY gives nothing the child can't already get with a plain O_RDONLY open.

Please limit it to O_PATH|O_DIRECTORY, though. On a symlink, O_RDONLY|O_NOFOLLOW fails with ELOOP instead of returning an fd to the link, so symlinks should keep the EOPNOTSUPP. A PR would be welcome.

congwang-mk avatar Nov 02 '26 23:11 congwang-mk