Skip to content

Use "new mount API" if available #755

Open

Description

@smcv

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.

[Tracked as steamrt/tasks#1005 elsewhere]

cc @ao2

Activity

  1. rusty-snake commented on May 29, 2026

    @rusty-snake
    Contributor

    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.
  2. smcv commented on Aug 28, 2026

    @smcv
    Author
  3. smcv commented on Aug 28, 2026

    @smcv
    Author
  4. reopened this on Aug 28, 2026
  5. filbranden commented on Sep 23, 2026

    @filbranden

    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:

    • Before unshare(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.
    • After 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/N fresh 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.

  6. xxyzz commented on Sep 24, 2026

    @xxyzz
    Contributor

    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?

  7. filbranden commented on Sep 24, 2026

    @filbranden

    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):

    openat2(4</oldroot>, "/tmp", {resolve=RESOLVE_NO_MAGICLINKS|RESOLVE_IN_ROOT}) = 5</oldroot/tmp>
    open_tree(5</oldroot/tmp>, "", OPEN_TREE_CLONE|AT_EMPTY_PATH|AT_RECURSIVE)    = 7</>
    move_mount(7</>, "", 4</newroot/mnt>, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH) = 0
    

    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:

    openat("/oldroot/proc/self/fd/9", O_PATH)                          = 4</tmp>
    open_tree(4</tmp>, "", OPEN_TREE_CLONE|AT_EMPTY_PATH|AT_RECURSIVE) = -1 EINVAL
    

    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.

  8. xxyzz commented on Sep 24, 2026

    @xxyzz
    Contributor

    I now feel LLM's conclusion is correct and adding open_tree() isn't that much improvement as I expected before...

  9. xxyzz commented on Sep 27, 2026

    @xxyzz
    Contributor

    I should be more explicit, the comment at here says the same thing, and it's added when --bind-fd was introduced:

    bubblewrap/bubblewrap.c

    Lines 1284 to 1288 in 0f8073d

    /* When using bind-fd, there is a race condition between resolving the fd as a magic symlink
    * and mounting it, where someone could replace what is at the symlink target. Ideally
    * we would not even resolve the symlink and directly bind-mount from the fd, but unfortunately
    * we can't do that, because its not permitted to bind mount a fd from another user namespace.
    * So, we verify after the mount that the target is what we expect. */

    and current code already use fd for bind mount since 0.12 release: bind_mount_fd().

  10. superwhiskers commented on Oct 14, 2026

    @superwhiskers

    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.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions