Skip to content

fix(bubblewrap): start bwrap with an empty environment - #1807

Open
Carlos Alexandro Becker (caarlos0) wants to merge 3 commits into
mainfrom
fix-bwrap-environ-leak-1783
Open

Carlos Alexandro Becker (caarlos0) wants to merge 3 commits into
mainfrom
fix-bwrap-environ-leak-1783

Conversation

@caarlos0

@caarlos0 Carlos Alexandro Becker (caarlos0) commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

📖 Description

bwrap was started with the full environment of the calling process. The sandbox's PID 1 is bwrap, so a confined command could read that environment from /proc/1/environ, even with inheritDefaultEnvironment: false and an explicit environment. --clearenv only changes the environment of the final command, not bwrap's own.

This change calls env_clear() on the single bwrap launch in spawn_bwrap, so bwrap starts with an empty environment. The confined command's environment does not change: it is built from the request and passed to bwrap as --clearenv/--setenv arguments, and bwrap is already started by absolute path.

The Bubblewrap environment test now checks that /proc/self/environ, /proc/1/environ and $PPID do not hold the host's MXC_LEAK_PROBE. It also checks that each file is readable (so an unreadable file cannot pass as a zero count) and that FOO=bar from the request still reaches the command. The "Process environment" section of the Bubblewrap guide describes the behavior.

Not covered: the unshare supervisor in proxy_network.rs also inherits the host environment, but it runs in the host PID namespace and cannot be read from inside the sandbox, so it is left unchanged.

🔗 References

Fixes #1783

🔍 Validation

Run on macOS (the Bubblewrap module is Linux-only, so bwrap_runner.rs is not compiled there):

  • cargo fmt --all -- --check (from src/): passed
  • cargo clippy -p mxc-sdk --all-targets -- -D warnings (from src/): passed
  • cargo test -p mxc-sdk --lib bwrap_runner (from src/): passed, 0 tests matched on macOS
  • bash -n tests/scripts/run_bwrap_environment_test.sh: passed
  • JSON parse of tests/configs/bwrap_env_host_environ.json: passed

Not run: bash tests/scripts/run_bwrap_environment_test.sh, because no Linux host or container daemon was available. This PR is a draft until the Linux CI and scheduled Bubblewrap validation confirm it. The new test is part of run_bwrap_all_tests.sh.

✅ Checklist

  • Signed the Contributor License Agreement
  • Linked to an issue
  • For features or changes requiring a spec: linked the issue with maintainer-applied feature-accepted
  • For implementation: linked the required approved and merged design/API spec, or explained why no spec is required
  • Feature issue remains open unless this PR completes all agreed implementation, validation, and documentation
  • Updated documentation (if applicable)
  • Updated Copilot instructions (if build, architecture, or conventions changed)
  • If this PR changes Cargo.lock, the dependency-feed-check check passes (see pull request builds)

📋 Issue Type

  • Bug fix
  • Feature
  • Task
Microsoft Reviewers: Open in CodeFlow

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 19:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Clearing the environment breaks existing bubblewrap test wrappers that rely on inherited executable paths and per-case settings, so those wrappers must be updated alongside the change.

1 open issue
What changed in this PR

Addresses #1783 by clearing the Bubblewrap launcher's environment, preventing sandboxed commands from reading inherited host secrets through /proc while preserving request-supplied variables.

Changes:

  • Starts bwrap with an empty environment.
  • Adds regression checks for host-variable exposure and explicit environment delivery.
  • Documents launcher and workload environment handling.
File Description
tests/​scripts/​run_bwrap_environment_test.sh Adds environment-leak and readability assertions.
tests/​configs/​bwrap_env_host_environ.json Adds the sandbox environment probe request.
src/​mxc-sdk/​src/​backends/​bubblewrap/​common/​bwrap_runner.rs Clears the environment before launching bwrap.
docs/​backends/​bwrap/​bubblewrap-backend.md Explains launcher environment isolation.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 18, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The containment-sensitive Linux launcher change has only macOS validation, leaving Linux compilation and the relevant regression suites unverified on a supported host.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Needs-Author-Feedback Waiting for additional information or action from the issue or pull-request author. label Nov 18, 2026

@MGudgin Gudge (MGudgin) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Please address the two inline findings. Keep the launcher-side env_clear(): the common spawn is the right place to close the reported leak, and this is consistent with Seatbelt's environment clearing in #555 and the separate workload-default rules in #1195.

Reviewed commit 0ffa457ac94111ad607510e903ab2dcbcf1e27d0 against merge-base a1afe6b18859690f1ad0d2705b36dfff9bd12f4f. Both comments are anchored to added lines, and their attribution was independently rechecked before posting.

Verified clean with receipts: the shared spawn clears the launcher environment while retaining request-supplied workload values through --clearenv/--setenv. For this head, GitHub Actions run 44383538499, Ubuntu 24.04 job 135119226929, records 19 streaming tests and 15 Bubblewrap executor-characterization tests passing, including the updated cancellation wrapper. The inspected build log does not establish execution of the new CLI environment regression. Local Linux execution was unavailable; the findings are source-supported rather than locally reproduced.

Findings outside the diff

No separate out-of-diff finding. Finding 1 is anchored to the new launch-side clearing at bwrap_runner.rs:703 and concerns the resulting mismatch with unchanged native and Node probes (bwrap_version.rs:429-430, sdk/node/src/bwrap-probe-helper.ts:132-135).

Verified pre-existing - not attributed to this PR

The native and Node probes already inherited their environment; both files are byte-identical between base and head. Their existence is not charged as an old defect. Finding 1 is newly_exposed_by_change because this PR changes the actual launch environment while leaving those checks unchanged. No unrelated pre-existing issue is a merge condition.

// 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium (reliability/correctness) - Apply the same environment rule to availability probes.

Attribution: newly_exposed_by_change - clearing the real launch here makes the unchanged native and Node probes test different execution conditions.

bwrap_version.rs:429-430 still runs bwrap --version with inherited variables, as does sdk/node/src/bwrap-probe-helper.ts:132-135. A resolved wrapper such as #!/bin/sh followed by exec "$REAL_BWRAP" "$@" therefore passes probing when the host supplies REAL_BWRAP, but fails when this launch clears it. Discovery/dry-run validation can report success and the actual run then fails after startup preparation. The test-wrapper repairs in this PR demonstrate the same dependency mechanism; ordinary distro binaries are not claimed to require these variables.

Fix: keep this env_clear(), clear the native version probe's environment too, and pass env: {} to the Node helper's bwrap spawn. Update environment-dependent probe fixtures and check that such a wrapper is rejected during probing rather than failing only during execution. This scenario was source-verified, not locally reproduced on Linux.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declining for this PR. The scenario needs a bwrap that is a wrapper script reading host variables; no distro bwrap does, and the only wrappers like that were this repo's own test wrappers, which are fixed here. bwrap --version only checks that the binary runs, so passing the host environment there is not a sandbox exposure. Changing the native and Node probes is a separate behavior change; I can file it as its own issue if you want it tracked.

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 <[email protected]>
Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582
Signed-off-by: Carlos Alexandro Becker <[email protected]>
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 <[email protected]>
Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582
Signed-off-by: Carlos Alexandro Becker <[email protected]>
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 <[email protected]>
Copilot-Session: 780d59bc-8382-46d2-85a5-a617cb08e582
Signed-off-by: Carlos Alexandro Becker <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 22:26
@microsoft-github-policy-service microsoft-github-policy-service Bot added Needs-Attention Requires attention or a decision from the MXC maintainers. and removed Needs-Author-Feedback Waiting for additional information or action from the issue or pull-request author. labels Nov 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The security-sensitive Linux containment change lacks completed Linux runtime validation, with CI still running and the /proc environment regression and related isolation fixtures awaiting confirmation.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs-Attention Requires attention or a decision from the MXC maintainers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bubblewrap via the SDK: bwrap inherits the host process environment, readable from inside the sandbox

4 participants