Repository navigation
Conversation
69dfafa to
ff3c9f5
Compare
|
@YamasouA The earlier pieces of this series (#3409, #3572, #3577) were data structures and entry points, so unit tests were enough. This one is different: it introduces runtime behavior that only becomes meaningful once the init side is wired up, and I don't think unit tests alone can validate it sufficiently. Would it make sense to include the init-side integration in this PR as well, and add a contest integration test that verifies the behavior end to end? Splitting the commits would keep it reviewable. With the current split, I’m a little concerned that we may end up changing the parent-side implementation again once we start working on the init-side integration in a follow-up PR. What do you think? |
b5434c0 to
9d74d00
Compare
|
@saku3 |
7ccba26 to
b5f6d12
Compare
|
@saku3 |
There was a problem hiding this comment.
🟡 Changes recommended
Concurrent mapping helpers can deadlock, read-only idmapped /dev mounts can fail setup, and the integration probe does not establish idmap support.
3 open issues
What changed in this PR
Completes ID-mapped bind mount support in youki, building on the earlier validation and channel work for #2307.
Changes:
- Adds a parent-side worker to create idmapped mount file descriptors and integrates it with rootfs setup.
- Enables validated idmapped mounts and advertises support through
youki features. - Adds unit tests and 24 integration cases covering explicit mappings, inherited mappings, and recursive behavior.
| File | Description |
|---|---|
| tests/contest/runtimetest/src/tests.rs | Validates ownership and propagation inside containers. |
| tests/contest/runtimetest/src/main.rs | Dispatches idmap validation cases. |
| tests/contest/contest/src/tests/mod.rs | Exposes the idmap test module. |
| tests/contest/contest/src/tests/idmap/mod.rs | Exports the idmap test group. |
| tests/contest/contest/src/tests/idmap/idmap_test.rs | Adds integration fixtures and 24 cases. |
| tests/contest/contest/src/main.rs | Registers the idmap test group. |
| crates/youki/src/commands/features.rs | Advertises idmapped mount support. |
| crates/libcontainer/src/syscall/linux.rs | Adds required mount syscall constants. |
| crates/libcontainer/src/rootfs/utils.rs | Tracks recursive idmapping and bind options. |
| crates/libcontainer/src/rootfs/rootfs.rs | Passes mount channels through rootfs setup. |
| crates/libcontainer/src/rootfs/mount.rs | Requests and attaches idmapped mounts. |
| crates/libcontainer/src/rootfs/mod.rs | Exports mount channels. |
| crates/libcontainer/src/process/mod.rs | Registers the worker module and exposes messages internally. |
| crates/libcontainer/src/process/init/process.rs | Supplies channels during init rootfs preparation. |
| crates/libcontainer/src/process/idmapped_mount.rs | Implements privileged mount creation and mapping helpers. |
| crates/libcontainer/src/process/container_main_process.rs | Handles repeated mount requests and worker replies. |
| crates/libcontainer/src/error.rs | Removes the blanket unsupported-mount error. |
| crates/libcontainer/src/container/mount_validation.rs | Shares idmap detection and accepts supported configurations. |
Review effort: Balanced
| }; | ||
| // The child must outlive the id-map writes above: its /proc entries | ||
| // vanish when it exits. | ||
| drop(release_write); |
| unsafe { libc::close(fd as libc::c_int) }; | ||
| None |
There was a problem hiding this comment.
The CI runs on ubuntu-24.04 (kernel 6.8+), where both mount_setattr and tmpfs idmapping are supported, and youki itself reports a clear error on unsupported kernels/filesystems. I think the current probe is good enough for this PR.
There was a problem hiding this comment.
As Copilot pointed out, we may need to use mount_setattr to check for support.
Alternatively, I think we could simply remove this support check rather than adding more complexity here.
Currently, youki uses mount_setattr and other modern mount APIs for mount operations without any fallback, so it likely wouldn't be able to start on an unsupported kernel anyway.
saku3
left a comment
There was a problem hiding this comment.
Thank you for creating this PR.
I really appreciate your persistence and all the effort you've put into implementing this feature.
Sorry for sending my feedback in multiple rounds, but I'll submit my review for now.
I haven't had a chance to go through everything in detail yet, so there's no need to start addressing my comments right away.
| } | ||
| } | ||
|
|
||
| pub(crate) fn mount_requests_idmap(mount: &SpecMount) -> bool { |
There was a problem hiding this comment.
Nit: is_idmap_requested or requires_idmapped_mount might also be good.
| mount: &SpecMount, | ||
| options: &MountOptions, | ||
| mount_option_config: &MountOptionConfig, | ||
| comm: &mut MountChannels<'_>, |
There was a problem hiding this comment.
Would mount_channels be a clearer name than comm here?
| &self, | ||
| mount: &SpecMount, | ||
| options: &MountOptions, | ||
| mut comm: Option<&mut MountChannels<'_>>, |
There was a problem hiding this comment.
Would mount_channels be a clearer name than comm here?
| unsafe { libc::close(fd as libc::c_int) }; | ||
| None |
There was a problem hiding this comment.
The CI runs on ubuntu-24.04 (kernel 6.8+), where both mount_setattr and tmpfs idmapping are supported, and youki itself reports a clear error on unsupported kernels/filesystems. I think the current probe is good enough for this PR.
| let _ = setup_tx.send(Ok(())); | ||
|
|
||
| let mut mntns_joined: Option<std::result::Result<(), String>> = None; | ||
| for msg in request_rx { |
There was a problem hiding this comment.
| for msg in request_rx { | |
| // Main process -> worker: wait for the next request, sent by `request_mount_fd`. | |
| for msg in request_rx { |
| )); | ||
| }; | ||
|
|
||
| match service.request_mount_fd(request) { |
There was a problem hiding this comment.
| match service.request_mount_fd(request) { | |
| // Main process -> worker thread: blocks until the worker replies. | |
| match service.request_mount_fd(request) { |
|
|
||
| match service.request_mount_fd(request) { | ||
| Ok(mount_fd) => { | ||
| init_sender.send_mount_fd_reply(mount_fd.as_raw_fd())?; |
There was a problem hiding this comment.
| init_sender.send_mount_fd_reply(mount_fd.as_raw_fd())?; | |
| // Main process -> init (init channel): send the fd to init. | |
| init_sender.send_mount_fd_reply(mount_fd.as_raw_fd())?; |
| Ok(()) => create_idmapped_mount_fd(syscall.as_ref(), &msg, init_pid), | ||
| Err(err) => Err(IdmappedMountError::JoinMountNamespace(err.clone())), | ||
| }; | ||
| let _ = reply_tx.send(reply); |
There was a problem hiding this comment.
| let _ = reply_tx.send(reply); | |
| // Worker -> main process: the main process forwards it to init. | |
| let _ = reply_tx.send(reply); |
| let (request_tx, request_rx) = mpsc::channel::<MountMsg>(); | ||
| let (reply_tx, reply_rx) = mpsc::channel::<Result<OwnedFd>>(); | ||
| let (setup_tx, setup_rx) = mpsc::channel::<Result<()>>(); |
There was a problem hiding this comment.
I added some comments to container_main_process.rs and idmapped_mount.rs to clarify the direction of message flow between the main process, the worker thread, and the init process. These comments are based on my understanding, so feel free to use them if you find them helpful.
That said, I found it a bit difficult to tell which component is sending or receiving messages when reading the code line by line, since the variable names alone don't always make the communication direction clear.
| let (request_tx, request_rx) = mpsc::channel::<MountMsg>(); | |
| let (reply_tx, reply_rx) = mpsc::channel::<Result<OwnedFd>>(); | |
| let (setup_tx, setup_rx) = mpsc::channel::<Result<()>>(); | |
| // Main process -> worker: one mount request per idmapped mount. | |
| let (request_tx, request_rx) = mpsc::channel::<MountMsg>(); | |
| // Worker -> main process: the mount fd (or error) for each request. | |
| let (reply_tx, reply_rx) = mpsc::channel::<Result<OwnedFd>>(); | |
| // Worker -> main process: whether the worker is ready, sent once. | |
| let (setup_tx, setup_rx) = mpsc::channel::<Result<()>>(); |
| use anyhow::{Context, Result}; | ||
| use nix::libc; | ||
| use nix::mount::{MntFlags, umount2}; | ||
| #[cfg(target_os = "linux")] |
There was a problem hiding this comment.
#[cfg(target_os = "linux")] is only used in two places, but this crate cannot be built on non-Linux platforms anyway. Other Linux-specific functions, such as umount2, aren't gated either.
I don't think these attributes are necessary, and removing them would improve consistency.
| unsafe { libc::close(fd as libc::c_int) }; | ||
| None |
There was a problem hiding this comment.
As Copilot pointed out, we may need to use mount_setattr to check for support.
Alternatively, I think we could simply remove this support check rather than adding more complexity here.
Currently, youki uses mount_setattr and other modern mount APIs for mount operations without any fallback, so it likely wouldn't be able to start on an unsupported kernel anyway.
|
@YamasouA Could you please take a look at my review comments when you have a chance? That's all from my side for now. |
|
Once you've finished making the changes, please request my review. |
|
@saku3 |
| #[test] | ||
| fn validate_idmapped_mounts_rejects_bind_type_without_bind_option_as_unsupported() { | ||
| fn validate_idmapped_mounts_allows_bind_type_without_bind_option() { | ||
| // typ="bind" のみで is_bind を満たし、options に "bind"/"rbind" を含まないケース |
There was a problem hiding this comment.
This does not need to be addressed in this PR.
コメントニニホンゴハイッチャッテルヨ
Expose the idmap/ridmap recognition rule from mount validation as a shared predicate, so the mount fd service can gate on the same definition instead of duplicating it. Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Serve AskMountFd requests from the main process: a worker thread detaches its filesystem state eagerly (failing container start if that setup fails), joins the init process's mount namespace lazily on the first request, and answers each request by creating a user namespace fd for the requested id-mappings (or opening the container userns) and building a detached mount fd with open_tree + mount_setattr(MOUNT_ATTR_IDMAP). The mount namespace join must be lazy: the main process learns the init pid before init has entered its mount namespace, so joining at service startup would race with namespace setup. By the time rootfs setup sends AskMountFd, init is guaranteed to be inside. This differs from runc's goCreateMountSources, which can join eagerly because nsexec finishes namespace creation before the parent proceeds. Unlike every other init request, AskMountFd arrives once per idmapped mount, so InitRequestSequence gains a repeatable request in a new Mounts phase that precedes the existing ones: init prepares the rootfs before it asks for hooks. Accepting a request from a later phase closes it, so a mount fd request after hook, network or seccomp setup is rejected as a protocol violation. The service only starts for init containers whose spec contains idmapped mounts, so other containers are unaffected. Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Rootfs setup now asks the main process for an idmapped mount fd instead of rejecting the mount. For each mount matching mount_requests_idmap it builds a MountMsg from the parsed options, sends it over the channel the init process already uses for hooks and seccomp, and installs the returned fd with move_mount. The mount fd channels are threaded from container_init_process through prepare_rootfs as an Option, created from the same mount_fd_service_required predicate the main process uses to start its worker. Specs without idmapped mounts pass None, so an unexpected request fails instead of blocking on a reply nobody will send. Attributes are applied to the detached mount before move_mount, matching the plain bind path, so the mount is never visible without them. Propagation is applied afterwards through the existing apply_mount_propagation, because move_mount does not accept propagation flags. Destination creation is shared with the plain bind path; the two differ only in how they learn whether the source is a directory, so the helper takes that as an argument. Spec validation no longer rejects idmapped mounts, and youki features reports idmap support. Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Port runc's idmapped mount cases. runtimetest checks the resulting file ownership from inside the container, covering idmap and ridmap, mount-level mappings and the implied container userns, and interaction with plain bind mounts. The group skips when open_tree is unavailable. The relative destination case does not run against runc, which warns about the deprecated form on stderr. Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
Signed-off-by: YamasouA <akiakiskyhand@gmail.com>
cdc076c to
5c853c4
Compare
|
Rebased this branch onto the current main and updated the idmap contest group to use @saku3 Could you please take another look when you have time? |
saku3
left a comment
There was a problem hiding this comment.
Thanks for all your work on this feature. LGTM.
@youki-dev/youki
I’m planning to merge this PR, but I’ll wait another two or three days to give other maintainers a chance to provide feedback.
I see the following main follow-up tasks:
- #3942
- #3941
- Caching user namespace FDs to avoid creating a new user namespace for each mount with identical ID mappings.
I won’t go into the details of the caching mechanism here, but please refer to runc’s implementation:
If this PR is merged as is, I’ll either open a follow-up issue or submit a PR myself.
I don’t consider these follow-up tasks to be blockers for merging this PR.


Description
ID-Mapped Mount support, so a bind mount can present uid/gid values inside the
container that differ from the ones on the host. This covers the
idmapandridmapmount options, mount-leveluidMappings/gidMappings, and thecontainer's own user namespace mappings when a mount asks for idmap without
specifying its own. Spec validation no longer rejects these mounts, and
youki featuresnow reportsmountExtensions.idmap.enabled: true. The kernel mustsupport
open_tree(2)andmount_setattr(2)withMOUNT_ATTR_IDMAP, so Linux5.12 or later.
mount_setattr(MOUNT_ATTR_IDMAP)needs privileges the init process no longer hasonce it has joined the container namespaces, so rootfs setup asks the main process
for the mount fd over the channel it already uses for hooks and seccomp, and a
worker thread there builds it with
open_tree+mount_setattr, mirroring runc'sgoCreateMountSources. The worker only starts for init containers whose speccontains idmapped mounts, and the plain bind path is unchanged, so containers
without idmapped mounts are unaffected.
As suggested in review, the init-side integration and the end-to-end tests are in
this PR rather than follow-ups, split into commits:
libcontainer: extract mount_requests_idmap helper— share the idmaprecognition rule between spec validation and the service.
libcontainer: add parent-side idmapped mount fd service— the worker threadand the
AskMountFdbranch of the main process event loop.libcontainer: wire idmapped mounts into rootfs setup— the init side,move_mount, spec validation, andyouki features.test(contest): add idmapped mount integration tests— 24 cases ported fromrunc's
tests/integration/idmap.bats.The commit messages cover the design decisions in the worker, in particular why it
joins the init mount namespace lazily where runc can join eagerly.
Type of Change
Testing
Related Issues
Related to #2307
Follows #3409, #3572 and #3577. Final part of #3384, which was split for review.
Additional Context