Skip to content

[Bugfix] Fix --clearenv environment variable leak (#725, #1)#2

Open
pratikpawar009 wants to merge 1 commit into
mainfrom
bugfix/001-clearenv-env-leak
Open

[Bugfix] Fix --clearenv environment variable leak (#725, #1)#2
pratikpawar009 wants to merge 1 commit into
mainfrom
bugfix/001-clearenv-env-leak

Conversation

@pratikpawar009
Copy link
Copy Markdown
Owner

Overview

Fix critical security vulnerability where --clearenv flag fails to isolate environment variables.

Problem

Previously, when using --clearenv to isolate containers, parent process environment variables were still visible to child processes, defeating the security intent.

export SECRET=hidden
bwrap --clearenv /bin/sh -c 'echo $SECRET'
# Output: hidden  ✗ Should be empty!

Solution

Implemented proper environment isolation using execve() with custom environment vector:

  • Child process receives ONLY explicitly set variables via --setenv
  • Parent's PATH is saved before clearing environment
  • Program path resolution happens before isolation
  • 100% backward compatible

Changes

Implementation

  • bubblewrap.c: ~150 lines added for proper environment isolation

    • SetEnv struct for accumulating --setenv options
    • Validation functions for KEY=VALUE format
    • PATH resolution and environment vector construction
    • Conditional execve() vs execvp() switching
  • tests/test-clearenv.sh: New comprehensive test suite with 11 tests

    • Tests isolation with parent variables
    • Tests explicit variable propagation
    • Tests special variable clearing
    • Tests backward compatibility

Documentation

Complete specification and design documents in docs/001-clearenv-env-leak/:

  • spec.md: Feature specification
  • plan.md: Implementation plan
  • data-model.md: Technical design
  • contracts/cli-contract.md: CLI interface contract
  • quickstart.md: User guide with examples
  • research.md: Technical decisions

Testing

  • 11 comprehensive tests covering all success criteria
  • Backward compatibility verified
  • Invalid format rejection tested

Backward Compatibility

✓ 100% compatible - default behavior when --clearenv not used is unchanged
✓ All existing tests pass without modification
✓ All existing flags and combinations work as before

Closes containers#725
Closes #1

This commit implements proper environment variable isolation for the --clearenv flag,
fixing a critical security vulnerability where parent process environment variables
were leaking to child processes despite the isolation intent.

PROBLEM:
--------
Previously, the --clearenv flag failed to completely isolate environment variables.
Parent process variables were visible to child processes, defeating the security
guarantee expected by users relying on --clearenv for secret isolation.

SOLUTION:
---------
Implemented environment isolation using execve() with custom environment vector:
- When --clearenv is specified, parent's PATH is saved before clearing environment
- Accumulated --setenv options are stored in linked list during option parsing
- At execution time, an environment vector is constructed from accumulated entries
- execve() is used instead of execvp() to pass custom environment to child
- Child process receives ONLY explicitly set variables via --setenv

TECHNICAL DETAILS:
------------------
- New SetEnv struct for accumulating --setenv KEY=VALUE options
- Validation functions for KEY=VALUE format checking
- PATH resolution in parent process before environment isolation
- Conditional execution path: execve() if --clearenv, execvp() otherwise
- 100% backward compatible: default behavior completely unchanged

TESTING:
--------
- 11 comprehensive tests covering isolation, variable propagation, edge cases
- Tests verify: isolation works, explicit vars visible, special vars cleared,
  backward compatibility maintained, invalid formats rejected
- All tests in tests/test-clearenv.sh

DOCUMENTATION:
---------------
- Complete specification in docs/001-clearenv-env-leak/spec.md
- Implementation plan in docs/001-clearenv-env-leak/plan.md
- Data model and design decisions in docs/001-clearenv-env-leak/data-model.md
- CLI contract in docs/001-clearenv-env-leak/contracts/cli-contract.md
- User guide with examples in docs/001-clearenv-env-leak/quickstart.md
- Research findings and technical decisions in docs/001-clearenv-env-leak/research.md

BACKWARD COMPATIBILITY:
-----------------------
✓ 100% compatible with existing usage
✓ Default behavior when --clearenv not used is unchanged
✓ All existing flags and combinations work as before
✓ No modifications needed to existing code or tests

Fixes containers#725
Fixes #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--clearenv leaks environment #725 --clearenv leaks environment

2 participants