Skip to content

[ID-Mapped Mount] implement idmapped mount support - #3719

Open
YamasouA wants to merge 10 commits into
youki-dev:mainfrom
YamasouA:ft/id-mapped-mount-mount-fd-service
Open

YamasouA wants to merge 10 commits into
youki-dev:mainfrom
YamasouA:ft/id-mapped-mount-mount-fd-service

Conversation

@YamasouA

@YamasouA YamasouA commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

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 idmap and
ridmap mount options, mount-level uidMappings / gidMappings, and the
container's own user namespace mappings when a mount asks for idmap without
specifying its own. Spec validation no longer rejects these mounts, and youki features now reports mountExtensions.idmap.enabled: true. The kernel must
support open_tree(2) and mount_setattr(2) with MOUNT_ATTR_IDMAP, so Linux
5.12 or later.

mount_setattr(MOUNT_ATTR_IDMAP) needs privileges the init process no longer has
once 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's
goCreateMountSources. The worker only starts for init containers whose spec
contains 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:

  1. libcontainer: extract mount_requests_idmap helper — share the idmap
    recognition rule between spec validation and the service.
  2. libcontainer: add parent-side idmapped mount fd service — the worker thread
    and the AskMountFd branch of the main process event loop.
  3. libcontainer: wire idmapped mounts into rootfs setup — the init side,
    move_mount, spec validation, and youki features.
  4. test(contest): add idmapped mount integration tests — 24 cases ported from
    runc'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

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test updates
  • CI/CD related changes
  • Other (please describe):

Testing

  • Added new unit tests
  • Added new integration tests
  • Ran existing test suite
  • Tested manually (please provide steps)

Related Issues

Related to #2307

Follows #3409, #3572 and #3577. Final part of #3384, which was split for review.

Additional Context

@YamasouA
YamasouA marked this pull request as draft September 6, 2026 09:51
@YamasouA
YamasouA force-pushed the ft/id-mapped-mount-mount-fd-service branch from 69dfafa to ff3c9f5 Compare September 6, 2026 12:54
@YamasouA
YamasouA marked this pull request as ready for review September 6, 2026 15:12
@saku3

saku3 commented Sep 26, 2026

Copy link
Copy Markdown
Member

@YamasouA
Thank you for PR.

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?

@YamasouA
YamasouA force-pushed the ft/id-mapped-mount-mount-fd-service branch from b5434c0 to 9d74d00 Compare September 27, 2026 02:47
@YamasouA

Copy link
Copy Markdown
Contributor Author

@saku3
Thank you for your comment!
I agree that we may need to revise the approach taken in this PR when working on the follow-up PRs.
I originally split the work into several PRs because including everything in a single PR would make the diff quite large. However, if splitting the changes into separate commits is enough to keep it reviewable, I’ll include the remaining changes in this PR as well.
For now, I’ll mark this PR as a draft while I add the remaining changes.

@YamasouA
YamasouA marked this pull request as draft September 27, 2026 02:50
@YamasouA
YamasouA force-pushed the ft/id-mapped-mount-mount-fd-service branch from 7ccba26 to b5f6d12 Compare October 12, 2026 15:50
@YamasouA YamasouA changed the title [ID-Mapped Mount] add parent-side idmapped mount fd service [ID-Mapped Mount] implement idmapped mount support Oct 16, 2026
@YamasouA
YamasouA marked this pull request as ready for review October 17, 2026 00:21
@YamasouA

Copy link
Copy Markdown
Contributor Author

@saku3
Thanks for the advice. I've implemented it as suggested: the init-side integration and
the end-to-end tests are in this PR, split into commits. I've rewritten the PR
description for the wider scope, so please take a look there.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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);
Comment thread crates/libcontainer/src/rootfs/mount.rs Resolved
Comment on lines +345 to +346
unsafe { libc::close(fd as libc::c_int) };
None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 saku3 self-assigned this Oct 24, 2026

@saku3 saku3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is_idmap_requested or requires_idmapped_mount might also be good.

Comment thread crates/libcontainer/src/rootfs/mount.rs Outdated
mount: &SpecMount,
options: &MountOptions,
mount_option_config: &MountOptionConfig,
comm: &mut MountChannels<'_>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would mount_channels be a clearer name than comm here?

Comment thread crates/libcontainer/src/rootfs/mount.rs Outdated
&self,
mount: &SpecMount,
options: &MountOptions,
mut comm: Option<&mut MountChannels<'_>>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would mount_channels be a clearer name than comm here?

Comment thread crates/libcontainer/src/rootfs/mount.rs Resolved
Comment on lines +345 to +346
unsafe { libc::close(fd as libc::c_int) };
None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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())?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let _ = reply_tx.send(reply);
// Worker -> main process: the main process forwards it to init.
let _ = reply_tx.send(reply);

Comment on lines +76 to +78
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<()>>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[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.

Comment on lines +345 to +346
unsafe { libc::close(fd as libc::c_int) };
None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

saku3 commented Oct 31, 2026

Copy link
Copy Markdown
Member

@YamasouA
Thanks for your work on this PR!

Could you please take a look at my review comments when you have a chance?

That's all from my side for now.

@saku3 saku3 removed their assignment Oct 31, 2026
@saku3

saku3 commented Nov 22, 2026

Copy link
Copy Markdown
Member

Once you've finished making the changes, please request my review.

@YamasouA

Copy link
Copy Markdown
Contributor Author

@saku3
Sorry it took me so long to follow up on your review. I’ve now addressed each of your comments and pushed the changes.
I grouped the related comments by topic and split the changes into separate commits for each group to make them easier to review.
When you have a chance, could you please take another look? Thank you again for your review and patience.

#[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" を含まないケース

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be addressed in this PR.

コメントニニホンゴハイッチャッテルヨ

@saku3

saku3 commented Nov 25, 2026

Copy link
Copy Markdown
Member

@YamasouA
Sorry for the extra work. We've changed how test groups are registered in contest in #3918, which has now been merged.

Could you please rebase your branch onto main and update the test registration accordingly?

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>
@YamasouA
YamasouA force-pushed the ft/id-mapped-mount-mount-fd-service branch from cdc076c to 5c853c4 Compare November 26, 2026 14:47
@YamasouA

YamasouA commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased this branch onto the current main and updated the idmap contest group to use test_framework::register_test_group!. I also removed the redundant Japanese test comment.

@saku3 Could you please take another look when you have time?

@saku3 saku3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

https://github.com/opencontainers/runc/blob/d0d6f097adccfb5ae23c894c811fd4bacc3e213a/libcontainer/internal/userns/usernsfd_linux.go#L118

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.

@saku3
saku3 requested a review from a team November 29, 2026 02:28

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants