Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up userns in a straightforward way #2548

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
oci-validation-rust:
runs-on: ubuntu-22.04
needs: [youki-build]
timeout-minutes: 15
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -141,7 +141,7 @@ jobs:
rootless-podman-test:
runs-on: ubuntu-22.04
needs: [youki-build]
timeout-minutes: 15
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
needs: [changes]
if: needs.changes.outputs.any_modified == 'true'
runs-on: ubuntu-22.04
timeout-minutes: 15
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
- name: Setup Rust toolchain and cache
Expand Down Expand Up @@ -102,7 +102,7 @@ jobs:
needs: [changes]
if: needs.changes.outputs.any_modified == 'true'
runs-on: ubuntu-22.04
timeout-minutes: 15
timeout-minutes: 20
name: Run test coverage
steps:
- uses: actions/checkout@v3
Expand Down
49 changes: 31 additions & 18 deletions crates/libcontainer/src/process/container_intermediate_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use crate::{namespaces::Namespaces, process::channel, process::fork};
use libcgroups::common::CgroupManager;
use nix::unistd::{close, write};
use nix::unistd::{Gid, Pid, Uid};
use oci_spec::runtime::{LinuxNamespaceType, LinuxResources};
use oci_spec::runtime::{LinuxNamespace, LinuxNamespaceType, LinuxResources};
use procfs::process::Process;

use super::args::{ContainerArgs, ContainerType};
use super::channel::{IntermediateReceiver, MainSender};
use super::container_init_process::container_init_process;
use super::fork::CloneCb;

Expand Down Expand Up @@ -68,22 +69,7 @@ pub fn container_intermediate_process(
// https://man7.org/linux/man-pages/man7/user_namespaces.7.html for more
// information
if let Some(user_namespace) = namespaces.get(LinuxNamespaceType::User)? {
namespaces.unshare_or_setns(user_namespace)?;
if user_namespace.path().is_none() {
tracing::debug!("creating new user namespace");
// child needs to be dumpable, otherwise the non root parent is not
// allowed to write the uid/gid maps
prctl::set_dumpable(true).unwrap();
main_sender.identifier_mapping_request().map_err(|err| {
tracing::error!("failed to send id mapping request: {}", err);
err
})?;
inter_receiver.wait_for_mapping_ack().map_err(|err| {
tracing::error!("failed to receive id mapping ack: {}", err);
err
})?;
prctl::set_dumpable(false).unwrap();
}
setup_userns(&namespaces, user_namespace, main_sender, inter_receiver)?;

// After UID and GID mapping is configured correctly in the Youki main
// process, We want to make sure continue as the root user inside the
Expand Down Expand Up @@ -201,6 +187,33 @@ pub fn container_intermediate_process(
Ok(())
}

fn setup_userns(
namespaces: &Namespaces,
user_namespace: &LinuxNamespace,
sender: &mut MainSender,
receiver: &mut IntermediateReceiver,
) -> Result<()> {
namespaces.unshare_or_setns(user_namespace)?;
if user_namespace.path().is_some() {
return Ok(());
}

tracing::debug!("creating new user namespace");
// child needs to be dumpable, otherwise the non root parent is not
// allowed to write the uid/gid maps
prctl::set_dumpable(true).unwrap();
sender.identifier_mapping_request().map_err(|err| {
tracing::error!("failed to send id mapping request: {}", err);
err
})?;
receiver.wait_for_mapping_ack().map_err(|err| {
tracing::error!("failed to receive id mapping ack: {}", err);
err
})?;
prctl::set_dumpable(false).unwrap();
Ok(())
}

fn apply_cgroups<
C: CgroupManager<Error = E> + ?Sized,
E: std::error::Error + Send + Sync + 'static,
Expand Down Expand Up @@ -236,7 +249,7 @@ fn apply_cgroups<

#[cfg(test)]
mod tests {
use super::apply_cgroups;
use super::*;
use anyhow::Result;
use libcgroups::test_manager::TestManager;
use nix::unistd::Pid;
Expand Down