-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch changes the API to require spawning a new process to enable the sandbox. This is necessary because PID namespaces on Linux only take effect for new processes. As a result, the new process will be created as PID 1 without access to any other process through system calls like `kill` or the `procfs` filesystem interface. This fixes a gap in Birdcage's environment variable isolation where it was still possible to read the unsandboxed environment from `/proc/self/environ`. Since the new process takes on the responsibility of PID 1 in the new namespace, it will automatically be made parent for any orphaned process. Currently these processes will remain zombies until the sandboxed process is shut down.
- Loading branch information
Showing
45 changed files
with
997 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::fs; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
|
||
use crate::TestSetup; | ||
|
||
pub fn setup() -> TestSetup { | ||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::Read("./".into())).unwrap(); | ||
|
||
TestSetup { sandbox, data: String::new() } | ||
} | ||
|
||
pub fn validate(_data: String) { | ||
// Check for success on reading the `Cargo.toml` file. | ||
let file = fs::read_to_string("./Cargo.toml").unwrap(); | ||
assert!(file.contains("birdcage")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use birdcage::{Birdcage, Sandbox}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::TestSetup; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TestData { | ||
uid: u32, | ||
gid: u32, | ||
euid: u32, | ||
egid: u32, | ||
} | ||
|
||
pub fn setup() -> TestSetup { | ||
let uid = unsafe { libc::getuid() }; | ||
let gid = unsafe { libc::getgid() }; | ||
let euid = unsafe { libc::geteuid() }; | ||
let egid = unsafe { libc::getegid() }; | ||
|
||
let sandbox = Birdcage::new(); | ||
|
||
// Serialize test data. | ||
let data = TestData { uid, gid, euid, egid }; | ||
let data = serde_json::to_string(&data).unwrap(); | ||
|
||
TestSetup { sandbox, data } | ||
} | ||
|
||
pub fn validate(data: String) { | ||
// Deserialize test data. | ||
let data: TestData = serde_json::from_str(&data).unwrap(); | ||
|
||
assert_eq!(data.uid, unsafe { libc::getuid() }); | ||
assert_eq!(data.gid, unsafe { libc::getgid() }); | ||
assert_eq!(data.euid, unsafe { libc::geteuid() }); | ||
assert_eq!(data.egid, unsafe { libc::getegid() }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::env; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
|
||
use crate::TestSetup; | ||
|
||
pub fn setup() -> TestSetup { | ||
// Setup our environment variables | ||
env::set_var("PUBLIC", "GOOD"); | ||
env::set_var("PRIVATE", "BAD"); | ||
|
||
// Activate our sandbox. | ||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::Environment("PUBLIC".into())).unwrap(); | ||
|
||
TestSetup { sandbox, data: String::new() } | ||
} | ||
|
||
pub fn validate(_data: String) { | ||
// Only the `PUBLIC` environment variable remains. | ||
let env: Vec<_> = env::vars().collect(); | ||
assert_eq!(env, vec![("PUBLIC".into(), "GOOD".into())]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::fs; | ||
use std::process::Command; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
|
||
use crate::TestSetup; | ||
|
||
pub fn setup() -> TestSetup { | ||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::ExecuteAndRead("/usr/bin/true".into())).unwrap(); | ||
|
||
TestSetup { sandbox, data: String::new() } | ||
} | ||
|
||
pub fn validate(_data: String) { | ||
// Check for success when executing `true`. | ||
let cmd = Command::new("/usr/bin/true").status().unwrap(); | ||
assert!(cmd.success()); | ||
|
||
// Check for success on reading the `true` file. | ||
let cmd_file = fs::read("/usr/bin/true"); | ||
assert!(cmd_file.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::os::unix::fs as unixfs; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::TestSetup; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TestData { | ||
symlink_dir: PathBuf, | ||
} | ||
|
||
pub fn setup() -> TestSetup { | ||
// Create symlinked executable dir. | ||
let tempdir = tempfile::tempdir().unwrap().into_path(); | ||
let symlink_dir = tempdir.join("bin"); | ||
unixfs::symlink("/usr/bin", &symlink_dir).unwrap(); | ||
|
||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::ExecuteAndRead(symlink_dir.clone())).unwrap(); | ||
|
||
// Serialize test data. | ||
let data = TestData { symlink_dir }; | ||
let data = serde_json::to_string(&data).unwrap(); | ||
|
||
TestSetup { sandbox, data } | ||
} | ||
|
||
pub fn validate(data: String) { | ||
// Deserialize test data. | ||
let data: TestData = serde_json::from_str(&data).unwrap(); | ||
|
||
// Ensure symlinked dir's executable works. | ||
let symlink_dir_exec = data.symlink_dir.join("true"); | ||
let cmd = Command::new(symlink_dir_exec).status().unwrap(); | ||
assert!(cmd.success()); | ||
|
||
// Ensure original dir's executable works. | ||
let cmd = Command::new("/usr/bin/true").status().unwrap(); | ||
assert!(cmd.success()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::os::unix::fs as unixfs; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::TestSetup; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TestData { | ||
symlink_dir_exec: PathBuf, | ||
} | ||
|
||
pub fn setup() -> TestSetup { | ||
// Create symlinked executable dir. | ||
let tempdir = tempfile::tempdir().unwrap().into_path(); | ||
let symlink_dir = tempdir.join("bin"); | ||
let symlink_dir_exec = symlink_dir.join("true"); | ||
unixfs::symlink("/usr/bin", &symlink_dir).unwrap(); | ||
|
||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::ExecuteAndRead(symlink_dir_exec.clone())).unwrap(); | ||
|
||
// Serialize test data. | ||
let data = TestData { symlink_dir_exec }; | ||
let data = serde_json::to_string(&data).unwrap(); | ||
|
||
TestSetup { sandbox, data } | ||
} | ||
|
||
pub fn validate(data: String) { | ||
// Deserialize test data. | ||
let data: TestData = serde_json::from_str(&data).unwrap(); | ||
|
||
// Ensure symlinked dir's executable works. | ||
let cmd = Command::new(data.symlink_dir_exec).status().unwrap(); | ||
assert!(cmd.success()); | ||
|
||
// Ensure original dir's executable works. | ||
let cmd = Command::new("/usr/bin/true").status().unwrap(); | ||
assert!(cmd.success()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::fs; | ||
use std::os::unix::fs as unixfs; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
use birdcage::{Birdcage, Exception, Sandbox}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::TestSetup; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TestData { | ||
symlink_exec: PathBuf, | ||
} | ||
|
||
pub fn setup() -> TestSetup { | ||
// Create symlinked executable. | ||
let tempdir = tempfile::tempdir().unwrap().into_path(); | ||
let exec_dir = tempdir.join("bin"); | ||
fs::create_dir(&exec_dir).unwrap(); | ||
let symlink_exec = exec_dir.join("true"); | ||
unixfs::symlink("/usr/bin/true", &symlink_exec).unwrap(); | ||
|
||
let mut sandbox = Birdcage::new(); | ||
sandbox.add_exception(Exception::ExecuteAndRead(symlink_exec.clone())).unwrap(); | ||
|
||
// Serialize test data. | ||
let data = TestData { symlink_exec }; | ||
let data = serde_json::to_string(&data).unwrap(); | ||
|
||
TestSetup { sandbox, data } | ||
} | ||
|
||
pub fn validate(data: String) { | ||
// Deserialize test data. | ||
let data: TestData = serde_json::from_str(&data).unwrap(); | ||
|
||
// Ensure symlinked executable works. | ||
let cmd = Command::new(data.symlink_exec).status().unwrap(); | ||
assert!(cmd.success()); | ||
|
||
// Ensure original executable works. | ||
let cmd = Command::new("/usr/bin/true").status().unwrap(); | ||
assert!(cmd.success()); | ||
} |
Oops, something went wrong.