From 0c37e70fea03e326d26d5057588e4043a1685110 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 18 Nov 2026 16:30:43 -0300 Subject: [PATCH 1/3] fix(bubblewrap): start bwrap with an empty environment bwrap was spawned with the host process environment. The sandboxed command could read it from /proc/1/environ, even with a cleared command environment, because --clearenv only affects the final command. Call env_clear() on the bwrap command. The command's environment is unchanged: it is built from the request and passed to bwrap as --clearenv/--setenv arguments. Add a functional check to the Bubblewrap environment test and update the backend guide. Fixes #1783 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582 Signed-off-by: Carlos Alexandro Becker --- docs/backends/bwrap/bubblewrap-backend.md | 6 +++++- .../bubblewrap/common/bwrap_runner.rs | 5 ++++- tests/configs/bwrap_env_host_environ.json | 12 ++++++++++++ tests/scripts/run_bwrap_environment_test.sh | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/configs/bwrap_env_host_environ.json diff --git a/docs/backends/bwrap/bubblewrap-backend.md b/docs/backends/bwrap/bubblewrap-backend.md index 7f82722c83..f21e5f3882 100644 --- a/docs/backends/bwrap/bubblewrap-backend.md +++ b/docs/backends/bwrap/bubblewrap-backend.md @@ -338,7 +338,11 @@ backend-specific config block is needed. ### Process environment The host environment is never inherited — the sandbox is built with -`--clearenv`, so host secrets can't leak into untrusted code. +`--clearenv`, so host secrets can't leak into untrusted code. `bwrap` itself is +also started with an empty environment, so the host environment can't be read +from `/proc//environ` (for example `/proc/1/environ`) inside the sandbox. +The command's environment is passed to `bwrap` as `--clearenv` and `--setenv` +arguments. **From schema 0.9** the child gets a default block of `PATH` (`/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`), `TERM` diff --git a/src/mxc-sdk/src/backends/bubblewrap/common/bwrap_runner.rs b/src/mxc-sdk/src/backends/bubblewrap/common/bwrap_runner.rs index 41f7a3c348..6d7e82213b 100644 --- a/src/mxc-sdk/src/backends/bubblewrap/common/bwrap_runner.rs +++ b/src/mxc-sdk/src/backends/bubblewrap/common/bwrap_runner.rs @@ -697,7 +697,10 @@ impl BubblewrapScriptRunner { // 3. Spawn `bwrap`. let mut command = Command::new(&bwrap); - command.args(&args); + // The confined command can read bwrap's exec-time environment through + // /proc, so bwrap must not inherit the host's. The command's own + // environment is passed as --clearenv/--setenv arguments. + command.env_clear().args(&args); let pty = match stdio { StdioMode::Pipes => { command diff --git a/tests/configs/bwrap_env_host_environ.json b/tests/configs/bwrap_env_host_environ.json new file mode 100644 index 0000000000..0805f6e473 --- /dev/null +++ b/tests/configs/bwrap_env_host_environ.json @@ -0,0 +1,12 @@ +{ + "$schema": "../../schemas/stable/mxc-config.schema.1.0.0.json", + "version": "1.0.0", + "containerId": "CLI-bubblewrap-Env-host_environ", + "containment": "bubblewrap", + "process": { + "commandLine": "for p in self 1 $PPID; do if [ -r /proc/$p/environ ]; then echo \"READABLE_$p=[yes]\"; echo \"LEAK_$p=[$(tr '\\0' '\\n' < /proc/$p/environ | grep -c '^MXC_LEAK_PROBE=')]\"; else echo \"READABLE_$p=[no]\"; fi; done; echo \"CONTROL_SELF=[$(tr '\\0' '\\n' < /proc/self/environ | grep -c '^FOO=bar$')]\"; echo \"FOO=[$FOO]\"; echo ENV_PROBE_DONE", + "cwd": "/tmp", + "env": ["FOO=bar"], + "timeout": 20000 + } +} diff --git a/tests/scripts/run_bwrap_environment_test.sh b/tests/scripts/run_bwrap_environment_test.sh index 0f09e6dc44..3c6e8a9302 100644 --- a/tests/scripts/run_bwrap_environment_test.sh +++ b/tests/scripts/run_bwrap_environment_test.sh @@ -112,4 +112,23 @@ expect_ok "inheritDefaultEnv keeps the default HOME" "HOME=[/tmp]" expect_ok "inheritDefaultEnv adds the caller's variable" "FOO=[bar]" expect_ok "a caller entry overrides the same-named default" "TERM=[vt100]" +# `--clearenv` only changes the workload's environment. bwrap itself is the +# sandbox's PID 1 and keeps the environment it was started with, which the +# workload can read from /proc. Each pid must be readable, otherwise a zero +# count would pass without checking anything. +run_config bwrap_env_host_environ.json +expect_ok "the probe ran" "ENV_PROBE_DONE" +expect_ok "a supplied env reaches the command" "FOO=[bar]" +expect_ok "the command's own environ holds the supplied env" "CONTROL_SELF=[1]" +expect_ok "the command's own environ is readable" "READABLE_self=[yes]" +expect_ok "the sandbox's pid 1 environ is readable" "READABLE_1=[yes]" +expect_ok "no host variable in the command's own environ" "LEAK_self=[0]" +expect_ok "no host variable in the sandbox's pid 1 environ" "LEAK_1=[0]" +expect_absent "the host value itself does not appear" "BWRAP_HOST_ENV_LEAKED" +# Also covers $PPID, whose pid differs by launch mode. +if grep -q 'LEAK_[0-9a-z]*=\[[1-9]' <<<"$OUT"; then + fail "a host variable is readable from /proc" "$OUT" +fi +pass "no checked pid exposes a host variable" + echo "All $PASS_COUNT Bubblewrap environment assertions passed." From caca266711048a8b2aadb57516bb0c64fdeb4edf Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 18 Nov 2026 16:40:05 -0300 Subject: [PATCH 2/3] test(bubblewrap): stop test wrappers relying on inherited env bwrap now starts with an empty environment, so the test wrappers that stand in for bwrap can no longer read variables from it. Write the real bwrap path, the per-case failure mode and the scratch directory into the generated wrappers instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582 Signed-off-by: Carlos Alexandro Becker --- src/mxc-sdk/tests/streaming_bubblewrap.rs | 12 ++++--- .../run_bwrap_namespace_startup_test.sh | 31 +++++++++++++------ .../run_bwrap_resolver_isolation_test.sh | 2 +- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/mxc-sdk/tests/streaming_bubblewrap.rs b/src/mxc-sdk/tests/streaming_bubblewrap.rs index 4aea191f03..6a521cd6a0 100644 --- a/src/mxc-sdk/tests/streaming_bubblewrap.rs +++ b/src/mxc-sdk/tests/streaming_bubblewrap.rs @@ -593,17 +593,22 @@ fn streaming_bubblewrap_cancels_without_parent_death_signal() { let wrapper = directory.path().join("bwrap"); // Omit parent-death cleanup to hold the startup condition without a timing race. + // MXC starts bwrap with an empty environment, so the real path is written into + // the wrapper instead of being passed through a variable. + let real_bwrap = format!("'{}'", real_bwrap.to_string_lossy().replace('\'', r"'\''")); std::fs::write( &wrapper, - r#"#!/bin/sh + format!( + r#"#!/bin/sh for arg in "$@"; do shift if [ "$arg" != "--die-with-parent" ]; then set -- "$@" "$arg" fi done -exec "$MXC_TEST_REAL_BWRAP" "$@" -"#, +exec {real_bwrap} "$@" +"# + ), ) .expect("write bwrap wrapper"); std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o700)) @@ -622,7 +627,6 @@ exec "$MXC_TEST_REAL_BWRAP" "$@" "--test-threads=1", ]) .env(FIXTURE_ENV, "1") - .env("MXC_TEST_REAL_BWRAP", real_bwrap) .env("PATH", path); run_terminal_fixture(command); } diff --git a/tests/scripts/run_bwrap_namespace_startup_test.sh b/tests/scripts/run_bwrap_namespace_startup_test.sh index 60cc306070..a9d5f9a8b7 100644 --- a/tests/scripts/run_bwrap_namespace_startup_test.sh +++ b/tests/scripts/run_bwrap_namespace_startup_test.sh @@ -47,14 +47,6 @@ if [ "${1:-}" = "--user" ]; then fi exec "$REAL_UNSHARE" "$@" SHIM -cat >"$WORK_DIR/shim/bwrap" <<'SHIM' -#!/bin/sh -if [ "${TEST_FAILURE:-}" = "bwrap" ] && [ "${1:-}" != "--version" ]; then - echo "test: bwrap network namespace refused" >&2 - exit 1 -fi -exec "$REAL_BWRAP" "$@" -SHIM cat >"$WORK_DIR/shim/iptables-restore" <<'SHIM' #!/bin/sh if [ "${TEST_FAILURE:-}" = "rules" ] && [ "${1:-}" != "--version" ]; then @@ -63,7 +55,24 @@ if [ "${TEST_FAILURE:-}" = "rules" ] && [ "${1:-}" != "--version" ]; then fi exec "$REAL_RESTORE" "$@" SHIM -chmod +x "$WORK_DIR/shim/unshare" "$WORK_DIR/shim/bwrap" "$WORK_DIR/shim/iptables-restore" +chmod +x "$WORK_DIR/shim/unshare" "$WORK_DIR/shim/iptables-restore" + +# MXC starts bwrap with an empty environment, so this shim cannot read the real +# path or the failure mode from variables. Each case gets its own copy with both +# written in. +write_bwrap_shim() { + local dir="$1" failure="$2" + mkdir -p "$dir" + cat >"$dir/bwrap" <&2 + exit 1 +fi +exec "$REAL_BWRAP" "\$@" +SHIM + chmod +x "$dir/bwrap" +} for mode in proxy firewall; do proxy="" @@ -94,8 +103,10 @@ run_case() { shift 3 local dir="$WORK_DIR/$label" mkdir "$dir" + write_bwrap_shim "$dir/shim" "$failure" local rc=0 - TEST_LOG="$dir/unshare.log" TEST_PID="$dir/supervisor.pid" TEST_FAILURE="$failure" \ + PATH="$dir/shim:$PATH" \ + TEST_LOG="$dir/unshare.log" TEST_PID="$dir/supervisor.pid" TEST_FAILURE="$failure" \ timeout 30 "$LXC_EXEC" --experimental "$@" "$WORK_DIR/$mode.json" \ >"$dir/output" 2>&1 || rc=$? echo "$rc" >"$dir/exit" diff --git a/tests/scripts/run_bwrap_resolver_isolation_test.sh b/tests/scripts/run_bwrap_resolver_isolation_test.sh index b183e00b8e..0630eaef2f 100644 --- a/tests/scripts/run_bwrap_resolver_isolation_test.sh +++ b/tests/scripts/run_bwrap_resolver_isolation_test.sh @@ -306,7 +306,7 @@ from pathlib import Path import sys if '--version' not in sys.argv: for value in sys.argv[1:]: - if value.startswith(os.environ['TMPDIR'] + '/mxc-bwrap-resolver-'): + if value.startswith({str(work / 'scratch')!r} + '/mxc-bwrap-resolver-'): os.unlink(value) os.symlink({str(work / 'ungranted-sentinel')!r}, value) if '--file' in sys.argv: From 78337355d9117be1ec64c97ad0770e714ca88a19 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 18 Nov 2026 19:25:30 -0300 Subject: [PATCH 3/3] test(bubblewrap): make the host environ leak checks able to fail The environ probe read /proc/self/environ through a redirect that opens before tr runs, so CONTROL_SELF read empty and LEAK_self could never fail. $PPID is always 1, and an unreadable pid printed no LEAK line and still passed. Sweep every /proc/[0-9]*/environ, count only after a successful read, and fail on any unreadable pid. Also assert in the PR-gated bubblewrap_clears_host_env_by_default test that /proc/1/environ holds no host variable, so dropping env_clear() fails on pull requests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582 Signed-off-by: Carlos Alexandro Becker --- ...e2e_tests_e2e_bubblewrap_characterization.rs | 11 ++++++++++- tests/configs/bwrap_env_host_environ.json | 2 +- tests/scripts/run_bwrap_environment_test.sh | 17 +++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/mxc-sdk/tests/wxc_e2e_tests_e2e_bubblewrap_characterization.rs b/src/mxc-sdk/tests/wxc_e2e_tests_e2e_bubblewrap_characterization.rs index 486eaa22b9..1b45053907 100644 --- a/src/mxc-sdk/tests/wxc_e2e_tests_e2e_bubblewrap_characterization.rs +++ b/src/mxc-sdk/tests/wxc_e2e_tests_e2e_bubblewrap_characterization.rs @@ -118,15 +118,20 @@ fn bubblewrap_streams_stdout() { /// Bubblewrap runs with `--clearenv`, so the sandboxed child does *not* inherit /// the launcher's environment even when `process.env` is empty. The refactor /// should preserve this; if it ever turns RED the env model has drifted. +/// +/// `bwrap` itself is the sandbox's PID 1 and the child can read its environment +/// from `/proc/1/environ`, so the launcher must start it with an empty +/// environment too. #[test] fn bubblewrap_clears_host_env_by_default() { if !ready() { return; } let marker = "CHAR_BWRAP_SHOULD_NOT_APPEAR_8a02f"; + let command = r#"printf 'MARKER=[%s]\n' "$MXC_CHAR_MARKER"; if d=$(tr '\0' '\n' < /proc/1/environ); then printf 'PID1_LEAK=[%s]\n' "$(printf '%s\n' "$d" | grep -c '^MXC_CHAR_MARKER=')"; else echo 'PID1_LEAK=[unreadable]'; fi"#; let result = run_platform_config_value( "bwrap env clear", - &config("env-clear", "printf 'MARKER=[%s]\\n' \"$MXC_CHAR_MARKER\""), + &config("env-clear", command), &[("MXC_CHAR_MARKER", marker)], None, ); @@ -136,6 +141,10 @@ fn bubblewrap_clears_host_env_by_default() { out.contains("MARKER=[]"), "expected cleared env (MARKER=[]); current Bubblewrap --clearenv behavior. Output:\n{out}" ); + assert!( + out.contains("PID1_LEAK=[0]"), + "expected the sandbox's /proc/1/environ to be readable without the host marker. Output:\n{out}" + ); assert!( !out.contains(marker), "host env marker leaked into the sandbox. Output:\n{out}" diff --git a/tests/configs/bwrap_env_host_environ.json b/tests/configs/bwrap_env_host_environ.json index 0805f6e473..80d78bcbaf 100644 --- a/tests/configs/bwrap_env_host_environ.json +++ b/tests/configs/bwrap_env_host_environ.json @@ -4,7 +4,7 @@ "containerId": "CLI-bubblewrap-Env-host_environ", "containment": "bubblewrap", "process": { - "commandLine": "for p in self 1 $PPID; do if [ -r /proc/$p/environ ]; then echo \"READABLE_$p=[yes]\"; echo \"LEAK_$p=[$(tr '\\0' '\\n' < /proc/$p/environ | grep -c '^MXC_LEAK_PROBE=')]\"; else echo \"READABLE_$p=[no]\"; fi; done; echo \"CONTROL_SELF=[$(tr '\\0' '\\n' < /proc/self/environ | grep -c '^FOO=bar$')]\"; echo \"FOO=[$FOO]\"; echo ENV_PROBE_DONE", + "commandLine": "for f in /proc/[0-9]*/environ; do p=${f#/proc/}; p=${p%/environ}; if d=$(tr '\\0' '\\n' < \"$f\"); then echo \"READABLE_$p=[yes]\"; echo \"LEAK_$p=[$(printf '%s\\n' \"$d\" | grep -c '^MXC_LEAK_PROBE=')]\"; else echo \"READABLE_$p=[no]\"; fi; done; echo \"CONTROL_SELF=[$(cat /proc/self/environ | tr '\\0' '\\n' | grep -c '^FOO=bar$')]\"; echo \"FOO=[$FOO]\"; echo ENV_PROBE_DONE", "cwd": "/tmp", "env": ["FOO=bar"], "timeout": 20000 diff --git a/tests/scripts/run_bwrap_environment_test.sh b/tests/scripts/run_bwrap_environment_test.sh index 3c6e8a9302..a62f003215 100644 --- a/tests/scripts/run_bwrap_environment_test.sh +++ b/tests/scripts/run_bwrap_environment_test.sh @@ -114,21 +114,22 @@ expect_ok "a caller entry overrides the same-named default" "TERM=[vt100]" # `--clearenv` only changes the workload's environment. bwrap itself is the # sandbox's PID 1 and keeps the environment it was started with, which the -# workload can read from /proc. Each pid must be readable, otherwise a zero -# count would pass without checking anything. +# workload can read from /proc. The config sweeps every pid in the sandbox and +# counts only after a successful read, so a pid that cannot be read is reported +# as unreadable instead of passing with a zero count. run_config bwrap_env_host_environ.json expect_ok "the probe ran" "ENV_PROBE_DONE" expect_ok "a supplied env reaches the command" "FOO=[bar]" -expect_ok "the command's own environ holds the supplied env" "CONTROL_SELF=[1]" -expect_ok "the command's own environ is readable" "READABLE_self=[yes]" +expect_ok "a successful read of the command's own environ holds the supplied env" "CONTROL_SELF=[1]" expect_ok "the sandbox's pid 1 environ is readable" "READABLE_1=[yes]" -expect_ok "no host variable in the command's own environ" "LEAK_self=[0]" expect_ok "no host variable in the sandbox's pid 1 environ" "LEAK_1=[0]" expect_absent "the host value itself does not appear" "BWRAP_HOST_ENV_LEAKED" -# Also covers $PPID, whose pid differs by launch mode. -if grep -q 'LEAK_[0-9a-z]*=\[[1-9]' <<<"$OUT"; then +if grep -q 'READABLE_[0-9]*=\[no\]' <<<"$OUT"; then + fail "a sandbox pid's environ could not be read" "$OUT" +fi +if grep -q 'LEAK_[0-9]*=\[[1-9]' <<<"$OUT"; then fail "a host variable is readable from /proc" "$OUT" fi -pass "no checked pid exposes a host variable" +pass "no sandbox pid exposes a host variable" echo "All $PASS_COUNT Bubblewrap environment assertions passed."