Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ jobs:
name: Rust
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
# This job executes repository code (cargo build/test); don't persist
# the token in git config.
persist-credentials: false
submodules: true

- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
with:
components: rustfmt, clippy

- uses: taiki-e/install-action@cargo-llvm-cov
- uses: taiki-e/install-action@be76f545a6a2f94fb131c5bb748ebed32edb5760 # cargo-llvm-cov

- uses: Swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2

- name: Check formatting
run: cargo fmt --all -- --check
Expand All @@ -56,24 +56,43 @@ jobs:

- name: Upload coverage report
if: ${{ always() }}
uses: actions/upload-artifact@v7
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: coverage-json
path: coverage.json
if-no-files-found: ignore

jail-platforms:
name: Jail (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
submodules: true
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2
- name: Constraint and jail tests
run: cargo test -p tinybox-core -p tinybox-jail --all-features
- name: Jail without Landlock feature
run: cargo test -p tinybox-jail --no-default-features

docs:
name: Docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
submodules: true

- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable

- uses: Swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2

- name: Build documentation
env:
Expand All @@ -84,7 +103,7 @@ jobs:
name: Minimum supported Rust version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
submodules: true
Expand All @@ -106,11 +125,11 @@ jobs:
fi
echo "version=$msrv" >> "$GITHUB_OUTPUT"

- uses: dtolnay/rust-toolchain@master
- uses: dtolnay/rust-toolchain@e2a55d2ffb04f378e9626c28d38b36d230d1e12f # master
with:
toolchain: ${{ steps.msrv.outputs.version }}

- uses: Swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2

- name: Build with the declared MSRV
run: cargo build --all-targets --all-features
Expand All @@ -119,12 +138,12 @@ jobs:
name: Supply chain
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
submodules: true

- name: Check advisories, licenses, bans, and sources
uses: EmbarkStudios/cargo-deny-action@v2
uses: EmbarkStudios/cargo-deny-action@3c6349835b2b7b196a839186cb8b78e02f7b5f25 # v2
with:
command: check all
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ workspace: alpine:3
runner: local / docker
isolation: kernel
untrusted: safe
supports: filesystem snapshots, forking, resource limits
supports: filesystem snapshots, forking
```

Snapshot a box and branch it — the fork inherits the parent's filesystem and
Expand Down Expand Up @@ -398,3 +398,6 @@ Arch Linux; macOS 15 and 26 on Intel and Apple Silicon; Windows Server 2022 and
## License

GPL-3.0-only. See [LICENSE](LICENSE).

Per-constraint hard preflight and current backend limitations are documented in
[constraint preflight](docs/specs/constraint-preflight.md).
2 changes: 1 addition & 1 deletion crates/tinybox-cli/src/command/mod_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ async fn inspecting_a_docker_box_reports_kernel_isolation() -> Result<()> {
assert!(
inspected
.out
.contains("filesystem snapshots, forking, port forwarding, resource limits")
.contains("filesystem snapshots, forking, port forwarding")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

priority high security confident

Keep the resource-limit capability assertion

This removes resource limits from the expected capability list, allowing the test to pass when the Docker backend silently loses its resource-limit capability. Keep the assertion aligned with the declared capability contract, or add a separate assertion that resource limits remain declared.


Additional critique observation

priority medium confident

Keep resource limits in the Docker capability assertion

[RULE] weakened-test

Docker's sandbox code translates resource limits into enforced Docker flags, so the inspection output should continue to advertise resource limits. Removing it from this assertion allows a regression where Docker enforces limits but no longer declares the capability to pass unnoticed. Restore the capability text in the expected output.

Suggested change for this observation (reference only)

.contains("filesystem snapshots, forking, port forwarding, resource limits")

[RULE] capability-regression ·

);
// The workspace column shows the image, not a Debug dump.
assert!(inspected.out.contains("workspace: alpine:3"));
Expand Down
12 changes: 12 additions & 0 deletions crates/tinybox-core/src/constraint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Constraint preflight

The public model separates individual security boundaries from lifecycle
capabilities. `ConstraintSupport::NONE` is the conservative declaration.
Backends add only enforced or best-effort behavior they actually implement.
`plan_check` is diagnostic and performs no execution; `require` rejects every
requested boundary except `Enforced`, returning a typed error. Setup failures
must still propagate from the backend rather than degrading to passthrough.

The model is used by the object-safe `Sandbox` trait and by `tinybox-jail`.
See [the specification](../../../../docs/specs/constraint-preflight.md) for
backend limitations and the meaning of each hard requirement.
138 changes: 138 additions & 0 deletions crates/tinybox-core/src/constraint/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//! Per-constraint preflight, independent of coarse lifecycle capabilities.
//!
//! A declaration describes implemented enforcement, not merely a configurable
//! knob. Callers must still handle setup failures. Best-effort support is never
//! accepted as a hard requirement. Unknown backends default to unsupported.

/// A security boundary requested before starting a workload.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Constraint {
/// Confine filesystem reads and writes to the granted paths.
Filesystem,
/// Deny external network access.
Network,
/// Cap CPU consumption.
Cpu,
/// Cap resident memory.
Memory,
/// Cap total processes and threads.
Pids,
/// Cap writable disk usage.
Disk,
/// Forbid creating child processes.
Subprocess,
}

impl Constraint {
/// All modeled constraints, in diagnostic order.
pub const ALL: [Self; 7] = [
Self::Filesystem,
Self::Network,
Self::Cpu,
Self::Memory,
Self::Pids,
Self::Disk,
Self::Subprocess,
];
const fn index(self) -> usize {
self as usize
}
}

impl std::fmt::Display for Constraint {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Filesystem => "filesystem",
Self::Network => "network",
Self::Cpu => "cpu",
Self::Memory => "memory",
Self::Pids => "pids",
Self::Disk => "disk",
Self::Subprocess => "subprocess",
})
}
}

/// What a backend actually implements for one constraint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Enforcement {
/// No implementation, including unavailable platform mechanisms.
Unsupported,
/// Partial or advisory implementation, unsuitable for a hard requirement.
BestEffort,
/// Full implementation; failure to apply it must prevent execution.
Enforced,
}

impl std::fmt::Display for Enforcement {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Unsupported => "unsupported",
Self::BestEffort => "best effort",
Self::Enforced => "enforced",
})
}
}

/// Independent enforcement declarations for all constraints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConstraintSupport([Enforcement; 7]);
impl Default for ConstraintSupport {
fn default() -> Self {
Self::NONE
}
}
impl ConstraintSupport {
/// Conservative declaration for unknown and passthrough backends.
pub const NONE: Self = Self([Enforcement::Unsupported; 7]);
/// Declare the actual enforcement of one constraint.
#[must_use]
pub const fn with(mut self, constraint: Constraint, enforcement: Enforcement) -> Self {
self.0[constraint.index()] = enforcement;
self
}
/// Inspect an individual constraint.
#[must_use]
pub const fn enforcement(self, constraint: Constraint) -> Enforcement {
self.0[constraint.index()]
}
/// Inspect all requested constraints without executing anything.
#[must_use]
pub fn plan_check(self, requested: &[Constraint]) -> PlanCheck {
PlanCheck {
constraints: requested
.iter()
.map(|c| (*c, self.enforcement(*c)))
.collect(),
}
}
/// Refuse the first hard constraint that is not fully enforced.
///
/// # Errors
/// Returns [`crate::Error::ConstraintNotEnforced`] for unsupported or
/// best-effort constraints. No workload is started.
pub fn require(self, backend: &str, requested: &[Constraint]) -> crate::Result<()> {
for &constraint in requested {
let enforcement = self.enforcement(constraint);
if enforcement != Enforcement::Enforced {
return Err(crate::Error::ConstraintNotEnforced {
sandbox: backend.to_owned(),
constraint,
enforcement,
});
}
}
Ok(())
}
}

/// Preflight diagnostics, including best-effort and unsupported constraints.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlanCheck {
/// Each requested constraint and its actual enforcement.
pub constraints: Vec<(Constraint, Enforcement)>,
}

#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;
55 changes: 55 additions & 0 deletions crates/tinybox-core/src/constraint/mod_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Hard requirements never accept partial enforcement.
use super::*;
#[test]
fn every_constraint_refuses_unsupported_and_best_effort() {
for constraint in Constraint::ALL {
for enforcement in [
Enforcement::Unsupported,
Enforcement::BestEffort,
Enforcement::Enforced,
] {
let support = ConstraintSupport::NONE.with(constraint, enforcement);
assert_eq!(
support.plan_check(&[constraint]).constraints,
[(constraint, enforcement)]
);
let result = support.require("backend", &[constraint]);
if enforcement == Enforcement::Enforced {
assert!(result.is_ok());
} else {
assert_eq!(
result.err(),
Some(crate::Error::ConstraintNotEnforced {
sandbox: "backend".into(),
constraint,
enforcement
})
);
}
}
}
assert_eq!(ConstraintSupport::default(), ConstraintSupport::NONE);
assert!(ConstraintSupport::NONE.require("backend", &[]).is_ok());
}

#[test]
fn every_constraint_and_enforcement_has_a_lowercase_operator_label() {
for (constraint, expected) in Constraint::ALL.into_iter().zip([
"filesystem",
"network",
"cpu",
"memory",
"pids",
"disk",
"subprocess",
]) {
assert_eq!(constraint.to_string(), expected);
}
for (enforcement, expected) in [
(Enforcement::Unsupported, "unsupported"),
(Enforcement::BestEffort, "best effort"),
(Enforcement::Enforced, "enforced"),
] {
assert_eq!(enforcement.to_string(), expected);
}
}
11 changes: 11 additions & 0 deletions crates/tinybox-core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ pub enum Error {
capability: Capability,
},

/// A hard constraint could not be fully enforced before execution.
#[error("sandbox {sandbox} cannot enforce {constraint}: {enforcement}")]
ConstraintNotEnforced {
/// Backend refusing the request.
sandbox: String,
/// Requested security boundary.
constraint: crate::Constraint,
/// Actual level of support.
enforcement: crate::Enforcement,
},

/// A resource limit was zero, and every limit tinybox applies must be
/// positive to be meaningful.
#[error("resource limit {limit} must be greater than zero")]
Expand Down
13 changes: 13 additions & 0 deletions crates/tinybox-core/src/error/mod_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,16 @@ fn errors_implement_the_standard_error_trait() {
assert_eq!(as_std.to_string(), "no box with id build-1");
assert!(as_std.source().is_none());
}

#[test]
fn constraint_refusals_use_lowercase_user_facing_labels() {
let error = Error::ConstraintNotEnforced {
sandbox: "seatbelt".into(),
constraint: crate::Constraint::Filesystem,
enforcement: crate::Enforcement::BestEffort,
};
assert_eq!(
error.to_string(),
"sandbox seatbelt cannot enforce filesystem: best effort"
);
}
3 changes: 3 additions & 0 deletions crates/tinybox-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub mod spec;
pub mod store;
pub mod template;

pub mod constraint;
Comment thread
senamakel marked this conversation as resolved.
Resolved
pub use constraint::{Constraint, ConstraintSupport, Enforcement, PlanCheck};

pub use capability::{Capability, IsolationLevel, SandboxCapabilities, SnapshotSupport};
pub use clock::{Clock, SystemClock};
pub use error::{Error, Result};
Expand Down
Loading
Loading