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

Fix : fix gid duplication, additional_gids etc not getting used in exec #3018

Closed
wants to merge 2 commits into from
Closed
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
82 changes: 78 additions & 4 deletions crates/libcontainer/src/container/tenant_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use nix::unistd::{pipe2, read, Pid};
use oci_spec::runtime::{
Capabilities as SpecCapabilities, Capability as SpecCapability, LinuxBuilder,
LinuxCapabilities, LinuxCapabilitiesBuilder, LinuxNamespace, LinuxNamespaceBuilder,
LinuxNamespaceType, LinuxSchedulerPolicy, Process, ProcessBuilder, Spec,
LinuxNamespaceType, LinuxSchedulerPolicy, Process, ProcessBuilder, Spec, UserBuilder,
};
use procfs::process::Namespace;

Expand All @@ -32,6 +32,22 @@ const NAMESPACE_TYPES: &[&str] = &["ipc", "uts", "net", "pid", "mnt", "cgroup"];
const TENANT_NOTIFY: &str = "tenant-notify-";
const TENANT_TTY: &str = "tenant-tty-";

fn get_path_from_spec(spec: &Spec) -> Option<String> {
let process = match spec.process() {
Some(p) => p,
None => return None,
};
let env = match process.env() {
Some(e) => e,
None => return None,
};
env.iter()
.find(|e| e.starts_with("PATH"))
.iter()
.next()
.map(|s| s.to_string())
}

/// Builder that can be used to configure the properties of a process
/// that will join an existing container sandbox
pub struct TenantContainerBuilder {
Expand All @@ -44,6 +60,9 @@ pub struct TenantContainerBuilder {
process: Option<PathBuf>,
detached: bool,
as_sibling: bool,
additional_gids: Vec<u32>,
user: Option<u32>,
group: Option<u32>,
}

impl TenantContainerBuilder {
Expand All @@ -61,6 +80,9 @@ impl TenantContainerBuilder {
process: None,
detached: false,
as_sibling: false,
additional_gids: vec![],
user: None,
group: None,
}
}

Expand Down Expand Up @@ -109,6 +131,21 @@ impl TenantContainerBuilder {
self
}

pub fn with_additional_gids(mut self, gids: Vec<u32>) -> Self {
self.additional_gids = gids;
self
}

pub fn with_user(mut self, user: Option<u32>) -> Self {
self.user = user;
self
}

pub fn with_group(mut self, group: Option<u32>) -> Self {
self.group = group;
self
}

/// Joins an existing container
pub fn build(self) -> Result<Pid, LibcontainerError> {
let container_dir = self.lookup_container_dir()?;
Expand Down Expand Up @@ -323,9 +360,10 @@ impl TenantContainerBuilder {
let process = if let Some(process) = &self.process {
self.get_process(process)?
} else {
let original_path_env = get_path_from_spec(spec);
let mut process_builder = ProcessBuilder::default()
.args(self.get_args()?)
.env(self.get_environment());
.env(self.get_environment(original_path_env));
if let Some(cwd) = self.get_working_dir()? {
process_builder = process_builder.cwd(cwd);
}
Expand All @@ -338,6 +376,22 @@ impl TenantContainerBuilder {
process_builder = process_builder.capabilities(caps);
}

let mut user_builder = UserBuilder::default();

if !self.additional_gids.is_empty() {
user_builder = user_builder.additional_gids(self.additional_gids.clone());
}

if let Some(uid) = self.user {
user_builder = user_builder.uid(uid);
}

if let Some(gid) = self.group {
user_builder = user_builder.gid(gid);
}

process_builder = process_builder.user(user_builder.build()?);

process_builder.build()?
};

Expand Down Expand Up @@ -397,8 +451,28 @@ impl TenantContainerBuilder {
Ok(self.args.clone())
}

fn get_environment(&self) -> Vec<String> {
self.env.iter().map(|(k, v)| format!("{k}={v}")).collect()
fn get_environment(&self, path: Option<String>) -> Vec<String> {
let mut env_exists = false;
let mut env: Vec<String> = self
.env
.iter()
.map(|(k, v)| {
if k == "PATH" {
env_exists = true;
}
format!("{k}={v}")
})
.collect();
// It is not possible in normal flow that path is None. The original container
// creation would have failed if path was absent. However we use Option
// just as a caution, and if neither exec cmd not original spec has PATH,
// the container creation will fail later which is ok
if let Some(p) = path {
if !env_exists {
env.push(p);
}
}
env
Comment on lines +455 to +475
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. If there is a more semantic way, let me know, these multiple checks are not particularly idiomatic.
  2. If there are no envs at all (self.env is empty) should we simply clone from the original container? I feel that is what the user would expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@YJDoc2 rename env_exists to path_exists

}

fn get_no_new_privileges(&self) -> Option<bool> {
Expand Down
5 changes: 4 additions & 1 deletion crates/libcontainer/src/process/container_init_process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
use std::{env, fs, mem};
Expand Down Expand Up @@ -761,6 +761,9 @@ fn set_supplementary_gids(

let gids: Vec<Gid> = additional_gids
.iter()
// this is to remove duplicate ids, so we behave similar to runc
.collect::<HashSet<_>>()
.into_iter()
.map(|gid| Gid::from_raw(*gid))
.collect();

Expand Down
8 changes: 8 additions & 0 deletions crates/youki/src/commands/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ use nix::sys::wait::{waitpid, WaitStatus};
use crate::workload::executor::default_executor;

pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
// TODO: not all values from exec are used here. We need to support
// the remaining ones.
let user = args.user.map(|(u, _)| u);
let group = args.user.and_then(|(_, g)| g);

let pid = ContainerBuilder::new(args.container_id.clone(), SyscallType::default())
.with_executor(default_executor())
.with_root_path(root_path)?
Expand All @@ -22,6 +27,9 @@ pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
.with_process(args.process.as_ref())
.with_no_new_privs(args.no_new_privs)
.with_container_args(args.command.clone())
.with_additional_gids(args.additional_gids)
.with_user(user)
.with_group(group)
.build()?;

// See https://github.com/containers/youki/pull/1252 for a detailed explanation
Expand Down
36 changes: 29 additions & 7 deletions tests/contest/contest/src/tests/process_user/process_user_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Ok, Result};
use anyhow::{anyhow, Context, Ok, Result};
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder, UserBuilder};
use rand::Rng;
use test_framework::{test_result, Test, TestGroup, TestResult};
Expand All @@ -21,12 +21,12 @@ fn generate_unique_random_vec() -> Vec<u32> {
ret
}

fn create_spec() -> Result<Spec> {
fn create_spec(gids: Vec<u32>) -> Result<Spec> {
let umask = 0o002;
let user = UserBuilder::default()
.uid(10u32)
.gid(10u32)
.additional_gids(generate_unique_random_vec())
.additional_gids(gids)
.umask(umask as u32)
.build()?;

Expand All @@ -42,16 +42,38 @@ fn create_spec() -> Result<Spec> {
.context("failed to build spec")?;
Ok(spec)
}
fn process_user_test() -> TestResult {
let spec = test_result!(create_spec());

fn process_user_test_unique_gids() -> TestResult {
let gids = generate_unique_random_vec();
let spec = test_result!(create_spec(gids));
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn process_user_test_duplicate_gids() -> TestResult {
let mut gids = generate_unique_random_vec();
let duplicate = gids[0];
gids.push(duplicate);
let spec = test_result!(create_spec(gids));
match test_inside_container(spec, &CreateOptions::default(), &|_| Ok(())) {
TestResult::Passed => TestResult::Failed(anyhow!(
"expected test with duplicate gids to fail, but it passed instead"
)),
_ => TestResult::Passed,
}
}

pub fn get_process_user_test() -> TestGroup {
let mut process_user_test_group = TestGroup::new("process_user");

let test = Test::new("process_user_test", Box::new(process_user_test));
process_user_test_group.add(vec![Box::new(test)]);
let test1 = Test::new(
"process_user_unique_gids_test",
Box::new(process_user_test_unique_gids),
);
let test2 = Test::new(
"process_user_duplicate_gids_test",
Box::new(process_user_test_duplicate_gids),
);
process_user_test_group.add(vec![Box::new(test1), Box::new(test2)]);

process_user_test_group
}
Loading