isolate-integration 0.1.1

A Rust interface for the ioi/isolate sandbox program.
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
use anyhow::{Context, Result, bail};
use std::collections::HashMap;
use std::path::PathBuf;
use tokio::fs;
use tokio::process::Command;

/// Directory binding rule
#[derive(Debug, Clone)]
pub struct DirectoryRule {
    pub inside_path: PathBuf,
    pub outside_path: Option<PathBuf>,
    pub options: DirectoryOptions,
}

/// Directory binding options
/// NOTE: Unless --no-default-dirs is specified, the default set of directory rules binds /bin, /dev (with devices allowed), /lib, /lib64 (if it exists), and /usr.
/// It also binds the working directory to /box (read-write), mounts the proc filesystem at /proc, and creates a temporary directory /tmp.
#[derive(Debug, Clone, Default)]
pub struct DirectoryOptions {
    pub read_write: bool,
    pub allow_devices: bool,
    pub no_exec: bool,
    pub maybe: bool,
    pub is_filesystem: bool,
    pub is_tmp: bool,
    pub no_recursive: bool,
}

/// Environment variable rule
#[derive(Debug, Clone)]
pub enum EnvRule {
    Inherit(String),
    Set(String, String),
    FullEnv,
}

/// Resource limits for isolate sandbox
/// All size-related items are in kilobytes (kB), time-related items are in seconds (s).
/// NOTE: use cgroups-related options first to control memory precisely.
#[derive(Debug, Clone)]
pub struct ResourceLimits {
    pub time_limit: Option<f64>,
    pub wall_time_limit: Option<f64>,
    pub extra_time: Option<f64>,
    pub memory_limit: Option<u32>,
    pub cg_memory_limit: Option<u32>,
    pub stack_limit: Option<u32>,
    pub open_files_limit: Option<u32>,
    pub file_size_limit: Option<u32>,
    pub core_limit: Option<u32>,
    pub process_limit: Option<u32>,
    pub quota: Option<(u32, u32)>,
}

/// Special options for isolate
#[derive(Debug, Clone, Default)]
pub struct SpecialOptions {
    pub share_net: bool,
    pub inherit_fds: bool,
    pub tty_hack: bool,
    pub special_files: bool,
    pub use_cgroups: bool,
    pub no_default_dirs: bool,
    pub verbose: bool,
    pub silent: bool,
    pub wait: bool,
    pub as_uid: Option<u32>,
    pub as_gid: Option<u32>,
}

/// Execution result from isolate
#[derive(Debug, Clone)]
pub struct ExecutionResult {
    pub exit_code: Option<i32>,
    pub signal: Option<i32>,
    pub time_used: f64,
    pub wall_time_used: f64,
    pub memory_used: u32,
    pub cg_memory_used: Option<u32>,
    pub killed: bool,
    pub cg_oom_killed: bool,
    pub status: String,
    pub message: String,
    pub stdout: String,
    pub stderr: String,
}

/// Main isolate sandbox implementation
pub struct IsolateSandbox {
    pub box_id: u32,
    pub isolate_bin: String,
    pub directory_rules: Vec<DirectoryRule>,
    pub env_rules: Vec<EnvRule>,
    pub stdin_file: Option<String>,
    pub stdout_file: Option<String>,
    pub stderr_file: Option<String>,
    pub stderr_to_stdout: bool,
    pub chdir: Option<String>,
    pub meta_file: Option<PathBuf>,
    pub special_options: SpecialOptions,
}

impl ResourceLimits {
    pub fn new() -> Self {
        Self {
            time_limit: None,
            wall_time_limit: None,
            extra_time: None,
            memory_limit: None,
            cg_memory_limit: None,
            stack_limit: None,
            open_files_limit: None,
            file_size_limit: None,
            core_limit: None,
            process_limit: None,
            quota: None,
        }
    }

    pub fn with_time_limit(mut self, seconds: f64) -> Self {
        self.time_limit = Some(seconds);
        self
    }

    pub fn with_memory_limit(mut self, kilobytes: u32) -> Self {
        self.memory_limit = Some(kilobytes);
        self
    }

    pub fn with_wall_time_limit(mut self, seconds: f64) -> Self {
        self.wall_time_limit = Some(seconds);
        self
    }

    pub fn with_cg_memory_limit(mut self, kilobytes: u32) -> Self {
        self.cg_memory_limit = Some(kilobytes);
        self
    }

    pub fn with_process_limit(mut self, count: u32) -> Self {
        self.process_limit = Some(count);
        self
    }
}

impl DirectoryRule {
    pub fn bind(inside: impl Into<PathBuf>, outside: impl Into<PathBuf>) -> Self {
        Self {
            inside_path: inside.into(),
            outside_path: Some(outside.into()),
            options: DirectoryOptions::default(),
        }
    }

    pub fn bind_same(path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        Self {
            inside_path: path.clone(),
            outside_path: Some(path),
            options: DirectoryOptions::default(),
        }
    }

    pub fn tmp(inside: impl Into<PathBuf>) -> Self {
        Self {
            inside_path: inside.into(),
            outside_path: None,
            options: DirectoryOptions {
                is_tmp: true,
                read_write: true,
                ..Default::default()
            },
        }
    }

    pub fn filesystem(name: impl Into<PathBuf>) -> Self {
        Self {
            inside_path: name.into(),
            outside_path: None,
            options: DirectoryOptions {
                is_filesystem: true,
                ..Default::default()
            },
        }
    }

    pub fn read_write(mut self) -> Self {
        self.options.read_write = true;
        self
    }

    pub fn allow_devices(mut self) -> Self {
        self.options.allow_devices = true;
        self
    }

    pub fn no_exec(mut self) -> Self {
        self.options.no_exec = true;
        self
    }

    pub fn maybe(mut self) -> Self {
        self.options.maybe = true;
        self
    }

    pub fn no_recursive(mut self) -> Self {
        self.options.no_recursive = true;
        self
    }
}

impl IsolateSandbox {
    pub fn new(box_id: u32) -> Self {
        Self {
            box_id,
            isolate_bin: std::env::var("ISOLATE_BIN").unwrap_or_else(|_| "isolate".to_string()),
            directory_rules: Vec::new(),
            env_rules: vec![EnvRule::Set(
                "LIBC_FATAL_STDERR_".to_string(), // send fatal errors to stderr by default
                "1".to_string(),
            )],
            stdin_file: None,
            stdout_file: None,
            stderr_file: None,
            stderr_to_stdout: false,
            chdir: None,
            meta_file: None,
            special_options: Default::default(),
        }
    }

    /// Initialize the sandbox
    pub async fn init(&self, limits: &ResourceLimits) -> Result<()> {
        let mut cmd = Command::new(&self.isolate_bin);

        cmd.arg(format!("--box-id={}", self.box_id));
        cmd.arg("--init");

        if let Some((blocks, inodes)) = limits.quota {
            cmd.arg(format!("--quota={},{}", blocks, inodes));
        }

        // Add special options
        if self.special_options.use_cgroups {
            cmd.arg("--cg");
        }
        if self.special_options.verbose {
            cmd.arg("--verbose");
        }
        if self.special_options.silent {
            cmd.arg("--silent");
        }
        if self.special_options.wait {
            cmd.arg("--wait");
        }
        if let Some(uid) = self.special_options.as_uid {
            cmd.arg(format!("--as-uid={}", uid));
        }
        if let Some(gid) = self.special_options.as_gid {
            cmd.arg(format!("--as-gid={}", gid));
        }

        let output = cmd
            .output()
            .await
            .context("Failed to execute isolate --init")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("isolate --init failed: {}", stderr);
        }

        Ok(())
    }

    /// Run a command in the sandbox
    pub async fn run<I, S>(
        &self,
        program: &str,
        args: I,
        limits: &ResourceLimits,
    ) -> Result<ExecutionResult>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut cmd = Command::new(&self.isolate_bin);

        cmd.arg(format!("--box-id={}", self.box_id));
        cmd.arg("--run");

        // Add resource limits
        if let Some(time) = limits.time_limit {
            cmd.arg(format!("--time={}", time));
        }
        if let Some(wall_time) = limits.wall_time_limit {
            cmd.arg(format!("--wall-time={}", wall_time));
        }
        if let Some(extra_time) = limits.extra_time {
            cmd.arg(format!("--extra-time={}", extra_time));
        }
        if let Some(memory) = limits.memory_limit {
            cmd.arg(format!("--mem={}", memory));
        }
        if let Some(cg_memory) = limits.cg_memory_limit {
            cmd.arg(format!("--cg-mem={}", cg_memory));
        }
        if let Some(stack) = limits.stack_limit {
            cmd.arg(format!("--stack={}", stack));
        }
        if let Some(open_files) = limits.open_files_limit {
            cmd.arg(format!("--open-files={}", open_files));
        }
        if let Some(file_size) = limits.file_size_limit {
            cmd.arg(format!("--fsize={}", file_size));
        }
        if let Some(core) = limits.core_limit {
            cmd.arg(format!("--core={}", core));
        }
        match limits.process_limit {
            Some(processes) if processes > 0 => {
                cmd.arg(format!("--processes={}", processes));
            }
            _ => {
                cmd.arg("--processes");
            }
        }

        // Add I/O redirection
        if let Some(ref stdin) = self.stdin_file {
            cmd.arg(format!("--stdin={}", stdin));
        }
        if let Some(ref stdout) = self.stdout_file {
            cmd.arg(format!("--stdout={}", stdout));
        }
        if let Some(ref stderr) = self.stderr_file {
            cmd.arg(format!("--stderr={}", stderr));
        }
        if self.stderr_to_stdout {
            cmd.arg("--stderr-to-stdout");
        }

        // Add directory change
        if let Some(ref chdir) = self.chdir {
            cmd.arg(format!("--chdir={}", chdir));
        }

        // Add meta file
        if let Some(ref meta) = self.meta_file {
            cmd.arg(format!("--meta={}", meta.display()));
        }

        // Add directory rules
        if self.special_options.no_default_dirs {
            cmd.arg("--no-default-dirs");
        }
        for rule in &self.directory_rules {
            let mut dir_arg = if rule.options.is_filesystem {
                format!("{}:fs", rule.inside_path.display())
            } else if rule.options.is_tmp {
                format!("{}:tmp", rule.inside_path.display())
            } else if let Some(ref outside) = rule.outside_path {
                format!("{}={}", rule.inside_path.display(), outside.display())
            } else {
                rule.inside_path.display().to_string()
            };

            let mut options = Vec::new();
            if rule.options.read_write {
                options.push("rw");
            }
            if rule.options.allow_devices {
                options.push("dev");
            }
            if rule.options.no_exec {
                options.push("noexec");
            }
            if rule.options.maybe {
                options.push("maybe");
            }
            if rule.options.no_recursive {
                options.push("norec");
            }

            if !options.is_empty() {
                dir_arg.push(':');
                dir_arg.push_str(&options.join(","));
            }

            cmd.arg(format!("--dir={}", dir_arg));
        }

        // Add environment rules
        for rule in &self.env_rules {
            match rule {
                EnvRule::Inherit(var) => {
                    cmd.arg(format!("--env={}", var));
                }
                EnvRule::Set(var, value) => {
                    cmd.arg(format!("--env={}={}", var, value));
                }
                EnvRule::FullEnv => {
                    cmd.arg("--full-env");
                }
            }
        }

        // Add special options
        if self.special_options.use_cgroups {
            cmd.arg("--cg");
        }
        if self.special_options.share_net {
            cmd.arg("--share-net");
        }
        if self.special_options.inherit_fds {
            cmd.arg("--inherit-fds");
        }
        if self.special_options.tty_hack {
            cmd.arg("--tty-hack");
        }
        if self.special_options.special_files {
            cmd.arg("--special-files");
        }
        if self.special_options.verbose {
            cmd.arg("--verbose");
        }
        if self.special_options.silent {
            cmd.arg("--silent");
        }

        // Add the command to execute
        cmd.arg("--").arg(program);
        for arg in args {
            cmd.arg(arg.as_ref());
        }

        // print the command in string for debugging to stderr
        let command_to_string = |cmd: &Command| -> String {
            let program = cmd.as_std().get_program().to_string_lossy();
            let args: Vec<String> = cmd
                .as_std()
                .get_args()
                .map(|arg| arg.to_string_lossy().to_string())
                .collect();
            format!("{} {}", program, args.join(" "))
        };
        eprintln!("Executing command: {}", command_to_string(&cmd));

        let output = cmd
            .output()
            .await
            .context("Failed to execute isolate --run")?;

        // Read output files if specified
        let stdout = if let Some(ref stdout_file) = self.stdout_file {
            fs::read_to_string(stdout_file).await.unwrap_or_default()
        } else {
            String::from_utf8_lossy(&output.stdout).to_string()
        };

        let stderr = if let Some(ref stderr_file) = self.stderr_file {
            fs::read_to_string(stderr_file).await.unwrap_or_default()
        } else {
            String::from_utf8_lossy(&output.stderr).to_string()
        };

        // Parse metadata if available
        let metadata = if let Some(ref meta_file) = self.meta_file {
            self.parse_metadata(meta_file).await.unwrap_or_default()
        } else {
            HashMap::new()
        };

        Ok(ExecutionResult {
            exit_code: metadata.get("exitcode").and_then(|s| s.parse().ok()),
            signal: metadata.get("exitsig").and_then(|s| s.parse().ok()),
            time_used: metadata
                .get("time")
                .and_then(|s| s.parse().ok())
                .unwrap_or(0.0),
            wall_time_used: metadata
                .get("time-wall")
                .and_then(|s| s.parse().ok())
                .unwrap_or(0.0),
            memory_used: metadata
                .get("max-rss")
                .and_then(|s| s.parse().ok())
                .unwrap_or(0),
            cg_memory_used: metadata.get("cg-mem").and_then(|s| s.parse().ok()),
            killed: metadata.get("killed").map(|s| s == "1").unwrap_or(false),
            cg_oom_killed: metadata.get("cg-oom-killed").is_some(),
            status: metadata.get("status").cloned().unwrap_or_default(),
            message: metadata.get("message").cloned().unwrap_or_default(),
            stdout,
            stderr,
        })
    }

    /// Cleanup the sandbox
    pub async fn cleanup(&self) -> Result<()> {
        let mut cmd = Command::new(&self.isolate_bin);

        cmd.arg(format!("--box-id={}", self.box_id));
        cmd.arg("--cleanup");

        if self.special_options.use_cgroups {
            cmd.arg("--cg");
        }

        let output = cmd
            .output()
            .await
            .context("Failed to execute isolate --cleanup")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            eprintln!("isolate --cleanup warning: {}", stderr);
        }

        Ok(())
    }

    async fn parse_metadata(&self, meta_file: &PathBuf) -> Result<HashMap<String, String>> {
        let content = fs::read_to_string(meta_file)
            .await
            .context("Failed to read metadata file")?;

        let mut metadata = HashMap::new();
        for line in content.lines() {
            if let Some((key, value)) = line.split_once(':') {
                metadata.insert(key.to_string(), value.to_string());
            }
        }

        Ok(metadata)
    }

    /// The following are builder options.

    pub fn with_directory_rule(mut self, rule: DirectoryRule) -> Self {
        self.directory_rules.push(rule);
        self
    }

    pub fn with_env_rule(mut self, rule: EnvRule) -> Self {
        self.env_rules.push(rule);
        self
    }

    pub fn with_stdin(mut self, file: impl Into<String>) -> Self {
        self.stdin_file = Some(file.into());
        self
    }

    pub fn with_stdout(mut self, file: impl Into<String>) -> Self {
        self.stdout_file = Some(file.into());
        self
    }

    pub fn with_stderr(mut self, file: impl Into<String>) -> Self {
        self.stderr_file = Some(file.into());
        self
    }

    pub fn with_stderr_to_stdout(mut self) -> Self {
        self.stderr_to_stdout = true;
        self
    }

    pub fn with_chdir(mut self, dir: impl Into<String>) -> Self {
        self.chdir = Some(dir.into());
        self
    }

    pub fn with_meta_file(mut self, file: impl Into<PathBuf>) -> Self {
        self.meta_file = Some(file.into());
        self
    }

    pub fn with_special_options(mut self, options: SpecialOptions) -> Self {
        self.special_options = options;
        self
    }

    pub fn use_cgroups(mut self) -> Self {
        self.special_options.use_cgroups = true;
        self
    }

    pub fn disable_cgroups(mut self) -> Self {
        self.special_options.use_cgroups = false;
        self
    }

    pub fn share_network(mut self) -> Self {
        self.special_options.share_net = true;
        self
    }

    pub fn no_default_dirs(mut self) -> Self {
        self.special_options.no_default_dirs = true;
        self
    }

    pub fn verbose(mut self) -> Self {
        self.special_options.verbose = true;
        self
    }
}