1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#![deny(clippy::all, clippy::cargo)]

#[macro_use]
mod utils;

mod cgroup_v1;
mod child;
mod cmd;
mod mount;
mod pipe;
mod proc;
mod run;
mod seccomp;
mod signal;

pub use crate::cmd::Command;

use crate::utils::RawFd;

use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use std::process;

use anyhow::Result;
use clap::Clap;
use memchr::memchr;
use serde::{Deserialize, Serialize};

pub fn run(config: &SandboxConfig) -> Result<SandboxOutput> {
    crate::run::run(config)
}

#[derive(Debug, Default, Serialize, Deserialize, Clap)]
#[clap(
    version = clap::crate_version!(),
    author = clap::crate_authors!(),
    setting(clap::AppSettings::DeriveDisplayOrder),
)]
pub struct SandboxConfig {
    pub bin: PathBuf, // relative to chroot

    pub args: Vec<OsString>,

    #[clap(short = 'e', long)]
    pub env: Vec<OsString>,

    #[clap(short = 'c', long, value_name = "path")]
    pub chroot: Option<PathBuf>, // relative to cwd

    #[clap(long)]
    pub uid: Option<u32>,

    #[clap(long)]
    pub gid: Option<u32>,

    #[clap(long, value_name = "path")]
    pub stdin: Option<PathBuf>, // relative to chroot

    #[clap(long, value_name = "path")]
    pub stdout: Option<PathBuf>, // relative to chroot

    #[clap(long, value_name = "path")]
    pub stderr: Option<PathBuf>, // relative to chroot

    #[clap(long, value_name = "fd", conflicts_with = "stdin")]
    pub stdin_fd: Option<RawFd>,

    #[clap(long, value_name = "fd", conflicts_with = "stdout")]
    pub stdout_fd: Option<RawFd>,

    #[clap(long, value_name = "fd", conflicts_with = "stderr")]
    pub stderr_fd: Option<RawFd>,

    #[clap(short = 't', long, value_name = "milliseconds")]
    pub real_time_limit: Option<u64>,

    #[clap(long, value_name = "seconds")]
    pub rlimit_cpu: Option<u32>,

    #[clap(long, value_name = "bytes")]
    pub rlimit_as: Option<u64>,

    #[clap(long, value_name = "bytes")]
    pub rlimit_data: Option<u64>,

    #[clap(long, value_name = "bytes")]
    pub rlimit_fsize: Option<u64>,

    #[clap(long, value_name = "bytes")]
    pub cg_limit_memory: Option<u64>,

    #[clap(long, value_name = "count")]
    pub cg_limit_max_pids: Option<u32>,

    #[clap(
        long,
        value_name = "bindmount",
        parse(try_from_os_str = BindMount::try_from_os_str)
    )]
    pub bindmount_rw: Vec<BindMount>,

    #[clap(
        short = 'b',
        long,
        value_name = "bindmount",
        parse(try_from_os_str = BindMount::try_from_os_str)
    )]
    pub bindmount_ro: Vec<BindMount>,

    #[clap(
        long,
        value_name = "path",
        min_values = 0,
        require_equals = true,
        default_missing_value = "/proc"
    )]
    pub mount_proc: Option<PathBuf>, // absolute (affected by chroot)

    #[clap(
        long,
        value_name = "path",
        min_values = 0,
        require_equals = true,
        default_missing_value = "/tmp"
    )]
    pub mount_tmpfs: Option<PathBuf>, // absolute (affected by chroot)

    #[clap(long, value_name = "prio")]
    pub priority: Option<i8>,

    #[clap(long)]
    pub seccomp_forbid_ipc: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BindMount {
    pub src: PathBuf, // absolute
    pub dst: PathBuf, // absolute (affected by chroot)
}

impl BindMount {
    fn try_from_os_str(s: &OsStr) -> Result<Self, String> {
        let (src, dst) = match memchr(b':', s.as_bytes()) {
            Some(idx) => {
                let src = OsStr::from_bytes(&s.as_bytes()[..idx]);
                let dst = OsStr::from_bytes(&s.as_bytes()[idx + 1..]);
                if src.is_empty() || dst.is_empty() {
                    return Err("invalid bind mount format".into());
                }
                (src, dst)
            }
            None => (s, s),
        };
        Ok(BindMount {
            src: src.into(),
            dst: dst.into(),
        })
    }

    pub fn new_same(src: PathBuf) -> Self {
        Self {
            dst: src.clone(),
            src,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SandboxOutput {
    pub code: i32,
    pub signal: i32,

    pub real_time: u64, // milliseconds
    pub sys_time: u64,  // milliseconds
    pub user_time: u64, // milliseconds

    pub memory: u64, // KiB
}

impl SandboxOutput {
    pub fn is_success(&self) -> bool {
        self.code == 0 && self.signal == 0
    }
}

impl SandboxConfig {
    pub fn to_cli_cmd(&self) -> process::Command {
        let mut cmd = process::Command::new("carapace");

        macro_rules! push {
            (@os_str $opt: literal, $f: ident) => {
                if let Some(ref $f) = self.$f {
                    cmd.arg($opt).arg($f);
                }
            };
            (@num $opt: literal, $f: ident) => {
                if let Some(ref $f) = self.$f {
                    cmd.arg($opt).arg($f.to_string());
                }
            };
            (@bindmount $opt: literal, $f: ident) => {
                for mnt in &self.$f {
                    cmd.arg($opt);
                    let mut s: OsString = mnt.src.as_os_str().into();
                    if mnt.src != mnt.dst {
                        s.push(":");
                        s.push(&mnt.dst);
                    }
                    cmd.arg(s);
                }
            };
            (@os_str @opt_arg $opt: literal, $f: ident) => {
                if let Some(ref $f) = self.$f {
                    let mut s: OsString = $opt.into();
                    s.push("=");
                    s.push($f);
                    cmd.arg(s);
                }
            };
            (@os_str @multi $opt: literal, $f: ident) => {
                for $f in &self.$f {
                    cmd.arg($opt).arg($f);
                }
            };
            (@flag $opt: literal, $f: ident) => {
                if self.$f {
                    cmd.arg($opt);
                }
            };
        }

        push!(@num "--uid", uid);
        push!(@num "--gid", gid);

        push!(@num "--rlimit-cpu", rlimit_cpu);
        push!(@num "--rlimit-as", rlimit_as);
        push!(@num "--rlimit-data", rlimit_data);
        push!(@num "--rlimit-fsize", rlimit_fsize);

        push!(@num "--cg-limit-memory", cg_limit_memory);
        push!(@num "--cg-limit-max-pids", cg_limit_max_pids);

        push!(@bindmount "--bindmount-rw", bindmount_rw);
        push!(@bindmount "-b", bindmount_ro);

        push!(@os_str @opt_arg "--mount-proc", mount_proc);
        push!(@os_str @opt_arg "--mount-tmpfs", mount_tmpfs);

        push!(@num "--priority", priority);

        push!(@flag "--seccomp-forbid-ipc", seccomp_forbid_ipc);

        push!(@num "--stdin-fd", stdin_fd);
        push!(@num "--stdout-fd", stdout_fd);
        push!(@num "--stderr-fd", stderr_fd);

        push!(@num "-t", real_time_limit);

        push!(@os_str "-c", chroot);

        push!(@os_str @multi "-e", env);

        push!(@os_str "--stdin", stdin);
        push!(@os_str "--stdout", stdout);
        push!(@os_str "--stderr", stderr);

        cmd.arg("--");
        cmd.arg(&self.bin);
        cmd.args(&self.args);

        cmd
    }
}