Skip to content

Add support for mount() flags customization #54

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions minion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ async fn main() {
shared_items: options.exposed_paths,
cpu_time_limit: Duration::from_millis(u64::from(options.time_limit)),
real_time_limit: Duration::from_millis(u64::from(options.time_limit * 3)),
extensions: None,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions minion-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ pub unsafe extern "C" fn minion_sandbox_create(
),
isolation_root,
shared_items,
extensions: None,
};
let d = backend.0.new_sandbox(opts);
let d = d.unwrap();
Expand Down
1 change: 1 addition & 0 deletions minion-tests/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async fn inner_main(test_cases: &[&'static dyn TestCase]) {
dest: "/me".into(),
kind: minion::SharedItemKind::Readonly,
}],
extensions: None,
};
let sandbox = backend.new_sandbox(opts).expect("can not create sandbox");
let opts = minion::ChildProcessOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub fn check(res: &mut CheckResult) {
#[cfg(target_os = "linux")]
{
crate::linux::check::check(&crate::linux::Settings::default(), res);
crate::linux::check::check(&crate::linux::BackendSettings::default(), res);
}
}

Expand Down
31 changes: 27 additions & 4 deletions src/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! Please note that this API is not type-safe. For example, if you pass
//! `Sandbox` instance to another backend, it will panic.

use anyhow::Context as _;
use futures_util::{FutureExt, TryFutureExt};

/// Type-erased `Sandbox`
Expand Down Expand Up @@ -89,13 +90,35 @@ impl<C: crate::ChildProcess> ChildProcess for C {

/// Type-erased `Backend`
pub trait Backend: Send + Sync + 'static {
fn new_sandbox(&self, options: crate::SandboxOptions) -> anyhow::Result<Box<dyn Sandbox>>;
fn new_sandbox(
&self,
options: crate::SandboxOptions<serde_json::Value>,
) -> anyhow::Result<Box<dyn Sandbox>>;
fn spawn(&self, options: ChildProcessOptions) -> anyhow::Result<Box<dyn ChildProcess>>;
}

impl<B: crate::Backend> Backend for B {
fn new_sandbox(&self, options: crate::SandboxOptions) -> anyhow::Result<Box<dyn Sandbox>> {
let sb = <Self as crate::Backend>::new_sandbox(&self, options)?;
fn new_sandbox(
&self,
options: crate::SandboxOptions<serde_json::Value>,
) -> anyhow::Result<Box<dyn Sandbox>> {
let exts = match options.extensions {
Some(e) => serde_json::from_value(e)
.map(Some)
.context("failed to parse sandbox settings extensions")?,
None => None,
};

let down_options = crate::SandboxOptions {
max_alive_process_count: options.max_alive_process_count,
memory_limit: options.memory_limit,
cpu_time_limit: options.cpu_time_limit,
real_time_limit: options.real_time_limit,
isolation_root: options.isolation_root,
shared_items: options.shared_items,
extensions: exts,
};
let sb = <Self as crate::Backend>::new_sandbox(&self, down_options)?;
Ok(Box::new(sb))
}

Expand Down Expand Up @@ -123,6 +146,6 @@ pub type ChildProcessOptions = crate::ChildProcessOptions<Box<dyn Sandbox>>;
/// Returns backend instance
pub fn setup() -> anyhow::Result<Box<dyn Backend>> {
Ok(Box::new(crate::linux::LinuxBackend::new(
crate::linux::Settings::new(),
crate::linux::BackendSettings::new(),
)?))
}
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ pub trait Backend: Debug + Send + Sync + 'static {
type Error: StdError + Send + Sync + 'static;
type Sandbox: Sandbox<Error = Self::Error>;
type ChildProcess: ChildProcess<Error = Self::Error>;
fn new_sandbox(&self, options: SandboxOptions) -> Result<Self::Sandbox, Self::Error>;
/// Backend-specific sandbox settings
type SandboxOptionsExtensions: serde::de::DeserializeOwned;
fn new_sandbox(
&self,
options: SandboxOptions<Self::SandboxOptionsExtensions>,
) -> Result<Self::Sandbox, Self::Error>;
fn spawn(
&self,
options: ChildProcessOptions<Self::Sandbox>,
Expand Down Expand Up @@ -80,7 +85,7 @@ pub struct ResourceUsageData {
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SandboxOptions {
pub struct SandboxOptions<Extensions> {
pub max_alive_process_count: u32,
/// Memory limit for all processes in cgroup, in bytes
pub memory_limit: u64,
Expand All @@ -90,9 +95,11 @@ pub struct SandboxOptions {
pub real_time_limit: Duration,
pub isolation_root: PathBuf,
pub shared_items: Vec<SharedItem>,
/// Backend-specific extensions
pub extensions: Option<Extensions>,
}

impl SandboxOptions {
impl<E> SandboxOptions<E> {
fn make_relative<'a>(&self, p: &'a Path) -> &'a Path {
if p.starts_with("/") {
p.strip_prefix("/").unwrap()
Expand Down
25 changes: 17 additions & 8 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn spawn(mut options: ChildProcessOptions<LinuxSandbox>) -> Result<LinuxChildPro
/// Allows some customization
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct Settings {
pub struct BackendSettings {
/// All created cgroups will be children of specified group
/// Default value is "/minion"
pub cgroup_prefix: PathBuf,
Expand All @@ -224,9 +224,9 @@ pub struct Settings {
pub allow_unsupported_mount_namespace: bool,
}

impl Default for Settings {
impl Default for BackendSettings {
fn default() -> Self {
Settings {
BackendSettings {
cgroup_prefix: "/minion".into(),
allow_unsupported_mount_namespace: false,
cgroupfs: std::env::var_os("MINION_CGROUPFS")
Expand All @@ -236,22 +236,31 @@ impl Default for Settings {
}
}

impl Settings {
pub fn new() -> Settings {
impl BackendSettings {
pub fn new() -> BackendSettings {
Default::default()
}
}

/// Extended sandbox settings
#[derive(serde::Deserialize, Default, Debug)]
pub struct LinuxSandboxOptionsExtensions {}

#[derive(Debug)]
pub struct LinuxBackend {
settings: Settings,
settings: BackendSettings,
cgroup_driver: Arc<cgroup::Driver>,
}

impl Backend for LinuxBackend {
type Error = Error;
type Sandbox = LinuxSandbox;
type ChildProcess = LinuxChildProcess;
fn new_sandbox(&self, mut options: SandboxOptions) -> Result<LinuxSandbox, Error> {
type SandboxOptionsExtensions = LinuxSandboxOptionsExtensions;
fn new_sandbox(
&self,
mut options: SandboxOptions<LinuxSandboxOptionsExtensions>,
) -> Result<LinuxSandbox, Error> {
options.postprocess();
let sb =
unsafe { LinuxSandbox::create(options, &self.settings, self.cgroup_driver.clone())? };
Expand All @@ -267,7 +276,7 @@ impl Backend for LinuxBackend {
}

impl LinuxBackend {
pub fn new(settings: Settings) -> Result<LinuxBackend, Error> {
pub fn new(settings: BackendSettings) -> Result<LinuxBackend, Error> {
self::check::run_all_feature_checks();
let cgroup_driver = Arc::new(cgroup::Driver::new(&settings)?);
Ok(LinuxBackend {
Expand Down
2 changes: 1 addition & 1 deletion src/linux/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Driver {

#[tracing::instrument]
pub(in crate::linux) fn new(
settings: &crate::linux::Settings,
settings: &crate::linux::BackendSettings,
) -> Result<Driver, crate::linux::Error> {
let mut configs = Vec::new();
for &cgroup_version in &[CgroupVersion::V1, CgroupVersion::V2] {
Expand Down
2 changes: 1 addition & 1 deletion src/linux/check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::linux::cgroup::Driver;

/// `crate::check()` on linux
pub fn check(settings: &crate::linux::Settings, res: &mut crate::check::CheckResult) {
pub fn check(settings: &crate::linux::BackendSettings, res: &mut crate::check::CheckResult) {
if !pidfd_supported() {
res.warning("PID file descriptors not supported")
}
Expand Down
10 changes: 5 additions & 5 deletions src/linux/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
jail_common,
pipe::setup_pipe,
util::{IpcSocketExt, Pid},
zygote, Error,
zygote, Error, LinuxSandboxOptionsExtensions,
},
ExitCode, Sandbox, SandboxOptions,
};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct LinuxSandbox(Arc<LinuxSandboxInner>);
#[repr(C)]
struct LinuxSandboxInner {
id: String,
options: SandboxOptions,
options: SandboxOptions<LinuxSandboxOptionsExtensions>,
zygote_sock: Mutex<Socket>,
zygote_pid: Pid,
state: SandboxState,
Expand All @@ -66,7 +66,7 @@ struct LinuxSandboxInner {
#[derive(Debug)]
struct LinuxSandboxDebugHelper<'a> {
id: &'a str,
options: &'a SandboxOptions,
options: &'a SandboxOptions<LinuxSandboxOptionsExtensions>,
zygote_sock: RawFd,
zygote_pid: Pid,
state: SandboxState,
Expand Down Expand Up @@ -152,8 +152,8 @@ impl LinuxSandbox {
}

pub(in crate::linux) unsafe fn create(
options: SandboxOptions,
settings: &crate::linux::Settings,
options: SandboxOptions<LinuxSandboxOptionsExtensions>,
settings: &crate::linux::BackendSettings,
cgroup_driver: Arc<crate::linux::cgroup::Driver>,
) -> Result<LinuxSandbox, Error> {
let jail_id = jail_common::gen_jail_id();
Expand Down