python: ship nsjail so shimmy's --sandbox works - #2
Merged
Conversation
shimmy provides the --sandbox (nsjail) feature and its own runtime image
builds nsjail from source into /usr/sbin/nsjail. This base image copies
only the shimmy binary (`COPY --from=shimmy /shimmy`), not nsjail or its
shared libraries, so any evaluation function that sets SANDBOX_ENABLED=true
has shimmy fail at startup (os.Stat("/usr/sbin/nsjail") -> not found).
Add a nsjail-builder stage that builds nsjail 3.4 from source on the same
python:<ver>-slim-bookworm image used at runtime, so the glibc / libstdc++
/ libprotobuf ABIs match (a binary copied from shimmy's ubuntu:24.04 image
would link against a newer glibc than bookworm provides). Copy the binary
in and install its three runtime libs (libcap2, libnl-route-3-200,
libprotobuf32). A build-time `ldd` check fails the build if a lib is
missing.
nsjail is dormant unless a function opts into --sandbox, so behaviour is
unchanged for every existing image consumer.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RYbUtnGroazAj1fNjXQmV7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
shimmy provides the
--sandbox/SANDBOX_ENABLEDfeature, which wraps worker processes in nsjail. shimmy's own runtime image builds nsjail from source and ships it at/usr/sbin/nsjail(its image isubuntu:24.04specifically "because nsjail requires shared libraries").This base image's
python/DockerfiledoesCOPY --from=shimmy /shimmy /usr/local/bin/shimmy— it takes only the shimmy binary, not/usr/sbin/nsjailor its runtime libs (libprotobuf,libnl-route-3,libcap2).Result: any Python evaluation function that sets
SANDBOX_ENABLED=truehas shimmy fail at startup —NewSandboxedWorkerFactory→os.Stat("/usr/sbin/nsjail")→ not found.Change
nsjail-builderstage, built frompython:${PYTHON_VERSION}-slim-${DEBIAN_VERSION}(the same image used at runtime) so the glibc / libstdc++ / libprotobuf ABIs match. Copying nsjail out of shimmy'subuntu:24.04image would link it against a newer glibc than bookworm ships. Mirrors shimmy's ownnsjail-builderstage; buildsnsjail3.4(pinned viaARG NSJAIL_VERSION,--recurse-submodulesfor the vendoredkafel).COPY --from=nsjail-builder /nsjail-src/nsjail /usr/sbin/nsjailalongside the existing shimmy copy.libcap2 libnl-route-3-200 libprotobuf32(bookworm package names) to the finalapt-get install.RUN ! ldd /usr/sbin/nsjail | grep -q 'not found'— fail the build if a shared lib is missing.nsjail is dormant unless a function opts into
--sandbox, so behaviour is unchanged for every existing consumer. Cost: ~1–2 min build time and ~40–100 MB image size (only when this stage's output is used, i.e. always in the final image — the size increase is unconditional; the runtime behaviour change is not).Testing
build_python.ymlmatrix (3.9–3.12) builds green on this PR (single-arch amd64).main— confirm the runtime lib package set resolves on arm64.docker run --rm --privileged -e FUNCTION_COMMAND=/bin/sh -e FUNCTION_ARGS="-c,cat /etc/shadow" -e SANDBOX_ENABLED=true -e SANDBOX_RO_BINDS="/usr:/bin:/lib:/lib64" <image> serveexits non-zero (/etcnot mounted in the jail).I could not build this locally (no Docker in the authoring environment) — relying on CI.
Follow-ups (not in this PR)
nsjail-builderstanza forlean/(andwolfram/,scratch/) if they should support--sandbox.evaluatePythonalready carries theSANDBOX_*policy env and is waiting on this + a new base tag.🤖 Generated with Claude Code
https://claude.ai/code/session_01RYbUtnGroazAj1fNjXQmV7