Skip to content

Add opt-in read-only rootfs for Docker workloads - #7128

Merged
aponcedeleonch merged 3 commits into
stacklok:mainfrom
sathvik8317:read-only-rootfs
Nov 13, 2026
Merged

aponcedeleonch merged 3 commits into
stacklok:mainfrom
sathvik8317:read-only-rootfs

Conversation

@sathvik8317

@sathvik8317 sathvik8317 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Standalone Docker/Podman workloads always run with a writable root filesystem, while the operator already sets readOnlyRootFilesystem: true on Kubernetes. A compromised or misbehaving MCP server can therefore modify its own image layer locally. This adds an opt-in way to lock the root filesystem down, as discussed in #6729.

  • Add ReadOnlyRootfs to RunConfig (read_only_rootfs), with a thv run --read-only-rootfs flag and a read_only_rootfs field on the workload create/update API.
  • Thread it through runtime.Setup and DeployWorkloadOptions into PermissionConfig, and set HostConfig.ReadonlyRootfs when creating the MCP container.
  • Include ReadonlyRootfs in compareHostConfig so toggling the setting recreates an existing container instead of reusing it.
  • Give /tmp, /var/tmp and /run a rw,nosuid,nodev,noexec,size=64m tmpfs when the rootfs is read-only, skipping paths a mount already targets. Podman's compat API otherwise adds its own executable, uncapped tmpfs there while Docker adds none, so the flag would behave differently per runtime.
  • Carry the field across thv upgrade.
  • Document it in docs/arch/05-runconfig-and-permissions.md and regenerate CLI/API docs.

Fixes #6729

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

I'm on Windows, so I ran go test and golangci-lint v2.15.0 against the touched packages rather than the full task targets. The only failures (squid/envoy file modes, TestWriteInPlace, a few pkg/api/v1 router tests) also fail on a clean main on Windows. Lint found nothing in the changed files.

Unit tests: compareHostConfig is now table-driven with a read-only-rootfs mismatch case. createMcpContainer asserts that ReadonlyRootfs and the scratch tmpfs are wired, with a table test for readOnlyRootfsTmpfs. New tests check that the option reaches DeployWorkload and runtime.Setup and defaults to off. API threading, the round-trip test and the thv upgrade preservation test all cover the field. The e2e test (test/e2e/read_only_rootfs_test.go) runs fetch with and without --read-only-rootfs and checks docker inspect for ReadonlyRootfs and the tmpfs mounts. I developed on Windows without Docker, so the e2e test has not run locally; I'm relying on CI for it.

Does this introduce a user-facing change?

Yes. Before: standalone workloads always had a writable root filesystem. After: thv run --read-only-rootfs (or "read_only_rootfs": true in the API/RunConfig) starts the MCP server with a read-only root filesystem, plus a 64 MiB noexec tmpfs on /tmp, /var/tmp and /run that behaves the same on Docker and Podman. The default is unchanged (writable), so existing workloads behave the same.

Special notes for reviewers

  • Placement follows the issue discussion: a RunConfig field plus a thv run flag, not a permission profile field, so no toolhive-core change is needed.
  • The Kubernetes runtime ignores the field, since the operator already enforces a read-only root filesystem there.
  • Servers that write outside their mounts and the scratch tmpfs paths will fail with the flag on. The Replace tmpfs with bind mounts for .thvignore directory overlays to fix tmpcopyup #1765 copy-up concern doesn't apply here, because the tmpfs sits over the image's own paths rather than host directories.
  • compareHostConfig doesn't compare Tmpfs, because the set follows from ReadonlyRootfs and the mounts, which are already compared.

Standalone workloads always ran with a writable root filesystem,
while the operator already sets readOnlyRootFilesystem on Kubernetes.
Add a RunConfig field, a thv run --read-only-rootfs flag and an API
field that set HostConfig.ReadonlyRootfs on the MCP container. The
setting is part of compareHostConfig so toggling it recreates the
container. Off by default to avoid breaking servers that write to
their root filesystem.

Fixes stacklok#6729
@sathvik8317
sathvik8317 marked this pull request as ready for review November 13, 2026 06:59
@sathvik8317

Copy link
Copy Markdown
Contributor Author

@aponcedeleonch this is the first PR for #6729. Could you approve the workflow runs? I'm on Windows without Docker, so the e2e test hasn't run locally yet. tmpfs scratch mounts will follow in a separate PR as discussed.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 06:59

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Comment thread pkg/container/docker/client.go Resolved
Comment thread pkg/api/v1/workload_service.go Resolved
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.24%. Comparing base (f49417a) to head (2954c75).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7128      +/-   ##
==========================================
+ Coverage   81.20%   81.24%   +0.04%     
==========================================
  Files         877      877              
  Lines       89885    89907      +22     
==========================================
+ Hits        72988    73049      +61     
+ Misses      16892    16853      -39     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Podman's compat API adds its own executable, uncapped tmpfs on /tmp,
/var/tmp and /run whenever ReadonlyRootfs is set, while Docker adds
none, so the same flag behaved differently per runtime. Set those
tmpfs mounts explicitly with rw,nosuid,nodev,noexec,size=64m and skip
any path a volume already targets, since Docker rejects duplicate
mount points.

Also cover the field in the API threading and round-trip tests, the
upgrade preservation test and runtime.Setup.
aponcedeleonch
aponcedeleonch previously approved these changes Nov 13, 2026

@aponcedeleonch aponcedeleonch 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.

Thanks for taking both on. The tmpfs change makes the flag behave the same on Docker and Podman, and the extra tests cover the wiring end to end. LGTM.

@aponcedeleonch

Copy link
Copy Markdown
Member

Small one: the Swagger docs check is failing because the ReadOnlyRootfs doc comment in pkg/runner/config.go changed in f9eded3 but the generated files weren't refreshed. Running task docs and then task sdk-generate should fix it. The second one matters because the SDK spec is generated from swagger.json, so it'll go stale as soon as the swagger is updated.

@sathvik8317

Copy link
Copy Markdown
Contributor Author

Thanks for approving! 2954c75 only regenerates swagger and the SDK for the updated read_only_rootfs comment, which was the one failing check last time. The two failures on it look like flakes unrelated to this change:

  • Tests / Test Go Code: a data race in pkg/authserver/runner tests, which passed on both earlier commits.
  • E2E Test Lifecycle (kindest/node:v1.34.3): VirtualMCPServer Redis-Backed Session Sharing ... pod B should serve the session before termination. 1.33 and 1.35 passed, and all three passed on the previous commit.

Could you re-run the failed jobs?

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.

Standalone Docker runtime: support read-only root filesystem for MCP workloads

3 participants