Skip to content

Commit

Permalink
fix(EXC-1811): Disable anyhow backtraces in sandbox (#2991)
Browse files Browse the repository at this point in the history
EXC-1811

As of version 1.0.77, the `anyhow` crate will capture a backtrace any
time an error is generated (if `RUST_BACKTRACE` is set). This triggers
some SELinux denials in the sandbox because libunwind reads and writes
to a pipe when capturing the backtrace. We can disable the capturing in
the sandbox by setting the `RUST_LIB_BACKTRACE` env variable since we
don't use these backtraces anyway.
  • Loading branch information
adambratschikaye authored Dec 9, 2024
1 parent 8622959 commit c7ce230
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions rs/canister_sandbox/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ impl LauncherService for LauncherServer {
socket,
}: LaunchSandboxRequest,
) -> rpc::Call<LaunchSandboxReply> {
match spawn_socketed_process(&sandbox_exec_path, &argv, socket) {
match spawn_socketed_process(
&sandbox_exec_path,
&argv,
&[("RUST_LIB_BACKTRACE", "0")],
socket,
) {
Ok(child_handle) => {
// Ensure the launcher closes its end of the socket.
drop(unsafe { UnixStream::from_raw_fd(socket) });
Expand Down Expand Up @@ -218,7 +223,7 @@ impl LauncherService for LauncherServer {
args.push("--embedder-config".to_string());
args.push(self.embedder_config_arg.clone());

match spawn_socketed_process(&exec_path, &args, socket) {
match spawn_socketed_process(&exec_path, &args, &[], socket) {
Ok(child_handle) => {
// Ensure the launcher closes its end of the socket.
drop(unsafe { UnixStream::from_raw_fd(socket) });
Expand Down
9 changes: 8 additions & 1 deletion rs/canister_sandbox/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ use std::sync::Arc;
pub fn spawn_socketed_process(
exec_path: &str,
argv: &[String],
env: &[(&str, &str)],
socket: RawFd,
) -> std::io::Result<Child> {
let mut cmd = Command::new(exec_path);
cmd.args(argv);
for (k, v) in env {
cmd.env(k, v);
}

// In case of Command we inherit the current process's environment. This should
// particularly include things such as Rust backtrace flags. It might be
Expand All @@ -46,6 +50,7 @@ pub fn spawn_socketed_process(
Ok(child_handle)
}

/// Only used for testing setups.
/// Spawn a canister sandbox process and yield RPC interface object to
/// communicate with it.
///
Expand All @@ -61,6 +66,8 @@ pub fn spawn_canister_sandbox_process(
) -> std::io::Result<(Arc<dyn SandboxService>, Pid, std::thread::JoinHandle<()>)> {
spawn_canister_sandbox_process_with_factory(exec_path, argv, controller_service, safe_shutdown)
}

/// Only used for testing setups.
/// Spawn a canister sandbox process and yield RPC interface object to
/// communicate with it. When the socket is closed by the other side,
/// we check if the safe_shutdown flag was set. If not this function
Expand All @@ -77,7 +84,7 @@ pub fn spawn_canister_sandbox_process_with_factory(
safe_shutdown: Arc<AtomicBool>,
) -> std::io::Result<(Arc<dyn SandboxService>, Pid, std::thread::JoinHandle<()>)> {
let (socket, sock_sandbox) = std::os::unix::net::UnixStream::pair()?;
let pid = spawn_socketed_process(exec_path, argv, sock_sandbox.as_raw_fd())?.id() as i32;
let pid = spawn_socketed_process(exec_path, argv, &[], sock_sandbox.as_raw_fd())?.id() as i32;

let socket = Arc::new(socket);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn spawn_launcher_process(
>,
) -> std::io::Result<(Box<dyn LauncherService>, Child)> {
let (socket, sock_launcher) = std::os::unix::net::UnixStream::pair()?;
let child_handle = spawn_socketed_process(exec_path, argv, sock_launcher.as_raw_fd())?;
let child_handle = spawn_socketed_process(exec_path, argv, &[], sock_launcher.as_raw_fd())?;

let socket = Arc::new(socket);

Expand Down

0 comments on commit c7ce230

Please sign in to comment.