You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bubblewrap currently uses the traditional mount(2) API for everything. This is not a great interface for container'y use cases.
In particular, the mount(2) API has the problem that it is path-based rather than fd-based, requiring some strange and non-robust code to implement --[ro-]bind-fd (which is necessary for Flatpak and other sandboxing frameworks that operate on attacker-controlled directory hierarchies, which need to use --[ro-]bind-fd to avoid vulnerabilities like CVE-2024-42472 and CVE-2026-34078).
If we use the "new mount APIs", it should be possible to implement --[ro-]bind-fd with open_tree(..., AT_EMPTY_PATH | OPEN_TREE_CLONE | AT_RECURSIVE) followed by mount_setattr() and move_mount(..., MOVE_MOUNT_F_EMPTY_PATH), similar to some of the examples in open_tree(2).
Similarly, --{,dev-,ro-}bind-fd could be implemented in a similar way, but starting from AT_FDCWD and an absolute path, rather than a fd and AT_EMPTY_PATH.
On older kernels, none of this would work (ENOSYS or EINVAL), and we'd have to fall back to the way things are currently done.
#754 should probably be done first - that's a much simpler use of the "new mount APIs".
A possible follow-up would be to increase the minimum kernel version to one that implements all of the necessary syscalls, but (like #754) that would mean abandoning the ability to run bubblewrap (and therefore Flatpak, the Steam Runtime, etc.) on older distros like RHEL 8 and Ubuntu 20.04, which is something we have historically tried hard not to do. If that is done, then I think it should be a separate PR and a separate issue.
Great idea. --[ro-]bind-fd sounds like a good start for using "new mount API" in bubblewrap.
Some possible follow up ideas for "new mount API" in bubblewrap:
With "new mount API" you can easily mount symlinks over files or files/directories over symlinks. Maybe a --perms like modifier like --nofollow-dest --bind /foo /bar could be used to have a backward compatible CLI.
This is a repeating flatpak limitation in it's issue tracker AFAIK.
With "new mount API" it is possible to drop newroot/oldroot path rewrites, simplifying codebath and string manipulations (defense-in-depth, performance). This is likely only sensible when dropping "old mount API" codepath someday.
With "new mount API" you can easily perform a --[dev,ro]-bind into a running sandbox/container. This would allow device hot-plug in flatpak (PoC) by watching sd-device/udev/inotify/... in a higher component (e.g. flatpak) and telling bwrap (cli invokation, UNIX-socket, ... API to be defined) to perform this bind.
I spent some time trying to implement --[ro-]bind-fd with the new mount API (open_tree(OPEN_TREE_CLONE) + mount_setattr + move_mount) as suggested here (while investigating the issue I reported in #804), and I ended up convinced it can't work for bubblewrap's unprivileged case. Writing it down so the next person doesn't repeat the attempt.
The problem
--bind-fd/--ro-bind-fd receive a file descriptor that the caller opened, i.e. one that lives in the parent mount + user namespaces. bubblewrap then does unshare(CLONE_NEWUSER | CLONE_NEWNS | …) before setting up the new root.
open_tree(fd, "", OPEN_TREE_CLONE | AT_EMPTY_PATH) can only clone a mount that is in the caller's current mount namespace — __do_loopback() bails out with -EINVAL via check_mnt() (mnt->mnt_ns == current->nsproxy->mnt_ns) otherwise. And open_tree(OPEN_TREE_CLONE) also needs CAP_SYS_ADMIN over the user namespace that owns the mount namespace (may_mount()).
Those two requirements can't be satisfied at the same time for an inherited fd:
Beforeunshare(CLONE_NEWNS): the fd's mount is in our mount namespace, but as an unprivileged process we don't hold CAP_SYS_ADMIN over its owning user namespace → EPERM.
Afterunshare(CLONE_NEWNS): we now have the capability in the new user namespace, but the inherited fd still refers to a mount in the parent mount namespace → EINVAL (check_mnt()).
The path-based implementation works precisely because it re-resolves /proc/self/fd/Nfresh in the new namespace, landing on the new namespace's copy of the mount — which is the very path lookup #755 wanted to eliminate. So the race and the cross-namespace clone are fundamentally at odds here.
Reproducer
A ~300-line standalone program crossns-repro.c (no bubblewrap involved) that walks the same timeline and calls open_tree on an inherited fd at each stage, run as an ordinary unprivileged user on Linux 6.12:
Inherited fd across bwrap's setup timeline:
T0 open_tree(inherited fd 3) = -1 errno=1 (Operation not permitted) # initial ns
T1 open_tree(inherited fd 3) = -1 errno=1 (Operation not permitted) # after CLONE_NEWUSER
T2 open_tree(inherited fd 3) = -1 errno=22 (Invalid argument) # after +CLONE_NEWNS
Control (fd opened inside the new namespaces):
C1 open_tree(fresh in-ns fd 3) = 4 # succeeds
C2 open_tree=4 mount_setattr=0 # clone + attrs OK
T2 = EINVAL is the failure seen when wiring this into setup_newroot(). C1/C2 confirm the flags, syscall numbers and API sequence are all correct — the only thing that fails is cloning from an fd inherited across the unshare, which is what --bind-fd fundamentally requires.
This is specific to cloning a mount, not to the new mount API
To be clear about the scope: the blocker above is OPEN_TREE_CLONE, not the new mount API in general. open_tree(OPEN_TREE_CLONE) clones an existing mount object, which drags check_mnt() into it.
The fsopen/fsconfig/fsmount half of the API creates a new mount, so none of this applies. I used it to mount overlayfs and it works fine unprivileged inside unshare(CLONE_NEWUSER | CLONE_NEWNS): no foreign mount is involved, and bubblewrap opens the layer directories itself, after the unshare, in the new namespace. So "new mount API in bubblewrap" is worth pursuing; it is only the cross-namespace clone that is unreachable.
Conclusion
I don't think the new mount API can close the source-side TOCTOU for --[ro-]bind-fd in the unprivileged model. The post-mount fstat() comparison added in the mount_setattr rework (#754 / #756) looks like the right mitigation: detection, because prevention via open_tree isn't reachable for a cross-namespace fd.
You're right, thanks, I was wrong about where the fd goes.
The LLM had assumed open_tree() would be called on the inherited fd. It isn't. resolve_symlinks_in_ops() runs realpath() over every bind-mount source, including the /proc/self/fd/N string that --bind-fd synthesises, so by the time setup_newroot() runs, op->source is already a plain path and the /proc special case never fires for --bind-fd. The fd bwrap clones is one it opened itself in the new namespace, so check_mnt() is happy.
I had the LLM build #793 and it does work here (Linux 6.12, unprivileged, --bind-fd 9 /mnt with fd 9 on /tmp):
What still holds from the earlier comment is narrower than what was stated. If you skip the realpath() step and clone from the fd directly (which is what this issue originally proposed) it still fails:
4</tmp> versus 5</oldroot/tmp> is the issue: the magic link lands on the caller's mount object, which lives in the parent mount namespace. Re-opening through /proc/self/fd/N after the unshare doesn't help either. Same stx_mnt_id, same EINVAL.
#793 looks like a good change on its own terms, I don't think it closes this issue. The source is still located by re-resolving a path, so the source-side TOCTOU and the post-mount fstat() comparison both remain. Worth keeping this issue open for the path round-trip, specifically.
from experimentation, it seemed to me that you can't just construct a detached mount with an arbitrary source file descriptor due to those mentioned mount namespace restrictions. i managed to work around this in my code by using the recent OPEN_TREE_NAMESPACE to create a mount namespace for every source file descriptor and then create detached mount objects with OPEN_TREE_CLONE. this appears to sidestep the restriction, but it gets very complicated and has the overhead of requiring many mount namespaces. there are ways to lessen the burden from what i can tell, but without privileges, some amount of namespace pressure seems unavoidable.
i've managed to use this to implement bind mounts and overlayfs using almost exclusively file descriptors as the input (some paths were necessary to mount individual files because of other restrictions, directories didn't need this), so i think a similar approach would be workable in something like bubblewrap, but getting it to co-exist with the path-based api might be a bit of a maintenance burden.
Activity
rusty-snake commentedon May 29, 2026
Great idea.
--[ro-]bind-fdsounds like a good start for using "new mount API" in bubblewrap.Some possible follow up ideas for "new mount API" in bubblewrap:
--permslike modifier like--nofollow-dest --bind /foo /barcould be used to have a backward compatible CLI.--[dev,ro]-bindinto a running sandbox/container. This would allow device hot-plug in flatpak (PoC) by watching sd-device/udev/inotify/... in a higher component (e.g. flatpak) and telling bwrap (cli invokation, UNIX-socket, ... API to be defined) to perform this bind.smcv commentedon Aug 28, 2026
smcv commentedon Aug 28, 2026
Use new mount API for bind mount
Use new mount API for bind mount
Use new mount API for bind mount
Use new mount API for bind mount
filbranden commentedon Sep 23, 2026
I spent some time trying to implement
--[ro-]bind-fdwith the new mount API (open_tree(OPEN_TREE_CLONE)+mount_setattr+move_mount) as suggested here (while investigating the issue I reported in #804), and I ended up convinced it can't work for bubblewrap's unprivileged case. Writing it down so the next person doesn't repeat the attempt.The problem
--bind-fd/--ro-bind-fdreceive a file descriptor that the caller opened, i.e. one that lives in the parent mount + user namespaces. bubblewrap then doesunshare(CLONE_NEWUSER | CLONE_NEWNS | …)before setting up the new root.open_tree(fd, "", OPEN_TREE_CLONE | AT_EMPTY_PATH)can only clone a mount that is in the caller's current mount namespace —__do_loopback()bails out with-EINVALviacheck_mnt()(mnt->mnt_ns == current->nsproxy->mnt_ns) otherwise. Andopen_tree(OPEN_TREE_CLONE)also needsCAP_SYS_ADMINover the user namespace that owns the mount namespace (may_mount()).Those two requirements can't be satisfied at the same time for an inherited fd:
unshare(CLONE_NEWNS): the fd's mount is in our mount namespace, but as an unprivileged process we don't holdCAP_SYS_ADMINover its owning user namespace → EPERM.unshare(CLONE_NEWNS): we now have the capability in the new user namespace, but the inherited fd still refers to a mount in the parent mount namespace → EINVAL (check_mnt()).The path-based implementation works precisely because it re-resolves
/proc/self/fd/Nfresh in the new namespace, landing on the new namespace's copy of the mount — which is the very path lookup #755 wanted to eliminate. So the race and the cross-namespace clone are fundamentally at odds here.Reproducer
A ~300-line standalone program
crossns-repro.c(no bubblewrap involved) that walks the same timeline and callsopen_treeon an inherited fd at each stage, run as an ordinary unprivileged user on Linux 6.12:T2=EINVALis the failure seen when wiring this intosetup_newroot().C1/C2confirm the flags, syscall numbers and API sequence are all correct — the only thing that fails is cloning from an fd inherited across theunshare, which is what--bind-fdfundamentally requires.This is specific to cloning a mount, not to the new mount API
To be clear about the scope: the blocker above is
OPEN_TREE_CLONE, not the new mount API in general.open_tree(OPEN_TREE_CLONE)clones an existing mount object, which dragscheck_mnt()into it.The
fsopen/fsconfig/fsmounthalf of the API creates a new mount, so none of this applies. I used it to mount overlayfs and it works fine unprivileged insideunshare(CLONE_NEWUSER | CLONE_NEWNS): no foreign mount is involved, and bubblewrap opens the layer directories itself, after the unshare, in the new namespace. So "new mount API in bubblewrap" is worth pursuing; it is only the cross-namespace clone that is unreachable.Conclusion
I don't think the new mount API can close the source-side TOCTOU for
--[ro-]bind-fdin the unprivileged model. The post-mountfstat()comparison added in themount_setattrrework (#754 / #756) looks like the right mitigation: detection, because prevention viaopen_treeisn't reachable for a cross-namespace fd.xxyzz commentedon Sep 24, 2026
I think LLM is wrong, bubblewrap code doesn't use the exact passed fd number, please see code at the start of
setup_newroot(). Have you checked #793?filbranden commentedon Sep 24, 2026
You're right, thanks, I was wrong about where the fd goes.
The LLM had assumed
open_tree()would be called on the inherited fd. It isn't.resolve_symlinks_in_ops()runsrealpath()over every bind-mount source, including the/proc/self/fd/Nstring that--bind-fdsynthesises, so by the timesetup_newroot()runs,op->sourceis already a plain path and the/procspecial case never fires for--bind-fd. The fd bwrap clones is one it opened itself in the new namespace, socheck_mnt()is happy.I had the LLM build #793 and it does work here (Linux 6.12, unprivileged,
--bind-fd 9 /mntwith fd 9 on/tmp):What still holds from the earlier comment is narrower than what was stated. If you skip the
realpath()step and clone from the fd directly (which is what this issue originally proposed) it still fails:4</tmp>versus5</oldroot/tmp>is the issue: the magic link lands on the caller's mount object, which lives in the parent mount namespace. Re-opening through/proc/self/fd/Nafter the unshare doesn't help either. Samestx_mnt_id, same EINVAL.#793 looks like a good change on its own terms, I don't think it closes this issue. The source is still located by re-resolving a path, so the source-side TOCTOU and the post-mount
fstat()comparison both remain. Worth keeping this issue open for the path round-trip, specifically.xxyzz commentedon Sep 24, 2026
I now feel LLM's conclusion is correct and adding
open_tree()isn't that much improvement as I expected before...xxyzz commentedon Sep 27, 2026
I should be more explicit, the comment at here says the same thing, and it's added when
--bind-fdwas introduced:bubblewrap/bubblewrap.c
Lines 1284 to 1288 in 0f8073d
and current code already use fd for bind mount since 0.12 release: bind_mount_fd().
superwhiskers commentedon Oct 14, 2026
from experimentation, it seemed to me that you can't just construct a detached mount with an arbitrary source file descriptor due to those mentioned mount namespace restrictions. i managed to work around this in my code by using the recent
OPEN_TREE_NAMESPACEto create a mount namespace for every source file descriptor and then create detached mount objects withOPEN_TREE_CLONE. this appears to sidestep the restriction, but it gets very complicated and has the overhead of requiring many mount namespaces. there are ways to lessen the burden from what i can tell, but without privileges, some amount of namespace pressure seems unavoidable.see this code for the namespace capture, and this code for the detached bind mount setup.
i've managed to use this to implement bind mounts and overlayfs using almost exclusively file descriptors as the input (some paths were necessary to mount individual files because of other restrictions, directories didn't need this), so i think a similar approach would be workable in something like bubblewrap, but getting it to co-exist with the path-based api might be a bit of a maintenance burden.