sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

fs_deny: opening a FIFO deadlocks the sandbox

Open congwang-mk opened this issue 2 months ago • 0 comments

With any --fs-deny active, opening a FIFO stalls the whole sandbox until it is killed.

$ D=$(mktemp -d)
$ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /dev -r $D -w $D -- \
    sh -c "mkfifo $D/f; (sleep 1; echo through-fifo > $D/f) & cat $D/f; wait"
through-fifo                                    (1 s)

$ sandlock run ... --fs-deny /tmp/nonexistent -- sh -c "<same>"
                                                (hangs; killed by timeout after 10 s)

The denied path does not have to exist or be related to the FIFO. Seen on main at 0f1d389. It is not only the reader that waits: in

sh -c "mkfifo $D/f; cat $D/f & sleep 1; echo shell-still-runs"

nothing is printed, so every process stops at its next trapped syscall.

Cause: once a deny is active, handle_notification performs every open that the handlers answered with Continue in the supervisor (on_behalf_open_for_deny). reopen_existing_on_behalf reopens the pinned file with the child's access mode (seccomp/notif.rs:605), and for a FIFO that open blocks until the other end is opened. It runs on the supervisor loop. The other end can only be opened by a sandbox process, whose openat is a notification the blocked supervisor never receives, so the two wait on each other.

This is one case of a wider defect, that an open done for a deny runs in the supervisor's context. #246 (/proc/self names the supervisor) and /dev/stdin failing with ELOOP under a deny come from the same place, and /dev/tty names the opener's terminal the same way.

Proposed fix: move the reopen of a FIFO off the supervisor loop on the deferred-handler path (the pinned object's type is known from fstat on the probe), so the two ends meet in the kernel as they do natively. A reopen still waiting when the deferral times out, or when the child dies, can be released by opening the other end with O_NONBLOCK.

An earlier version of this issue proposed avoiding these opens altogether, by withholding READ_FILE and the other open rights from the Landlock rule of every grant that contains a deny and serving only the opens inside such a grant. That does not work: Landlock asks READ_FILE as well as EXECUTE for an exec open, so nothing in such a tree could be executed (measured on a prototype). While a deny exists, opens have to be served by the supervisor, and the fix is to make each kind of open faithful.

congwang-mk avatar Sep 21 '26 17:09 congwang-mk