Repository navigation
fix(sandbox): do not load a workspace __pycache__ in the worker (#767) - #777
sresarehumantoo wants to merge 8 commits into
Conversation
|
The step 5 failure in the live e2e ( |
|
Built The branch currently conflicts with main in CHANGELOG.md only, so it needs a rebase. |
BaaRai
left a comment
There was a problem hiding this comment.
The worker environment change blocks the documented shadowing cases: timestamp and hash-based pyc with a matching source, symlinked __pycache__, a pyc next to its source, PYTHONPATH, sys.path.insert, python -B, subpackages and namespace packages. That part is right.
Four gaps remain:
- a pyc inside a
.zipor.eggis still executed throughzipimport; - a sourceless pyc in the workspace is executed;
- an explicit
importlibSourcelessFileLoaderor a custom import hook bypasses the prefix; python -EignoresPYTHONPYCACHEPREFIX(documented), and a.pthin user site-packages is not covered (HOMEis a writable tmpfs andPYTHONNOUSERSITEis not set).
The prefix itself is mounted writable for uid 10001, so a pyc forged under <prefix>/... is still loaded.
Please either close these gaps or state them explicitly as accepted limits in the PR body and CHANGELOG. The branch also conflicts with main and needs a rebase before it can be reviewed again.
2b35e6f to
d58255d
Compare
|
Rebased on 8f91643. The only conflict was CHANGELOG.md. With #764 on main, I reproduced each gap on the worker image (uid 10001, read-only rootfs, this branch's env and tmpfs):
Changes:
On the writable subtrees: dropping them makes An explicit Tests: PR body updated: the "Not covered" list matches the CHANGELOG, and the step 5 note is gone. |
|
My earlier review on this PR checked only the forged I checked d58255d myself. I built
All of these match the CHANGELOG and the PR body. The CHANGELOG diff against One small wording point: Tests on d58255d: This looks right to us. On the guard you mentioned as a possible follow-up: while checking this I prototyped one as a root-owned module loaded from a system |
|
Thanks for the independent run, especially the The guard on ec93686 looks like the right shape for the follow-up. I'll go through it once #777 settles and build the separate PR on it if it holds up, with credit to you. |
|
I ran ec93686 in Docker with the worker shape. On the system interpreter it does what you said: the e2e passes, and the I found two holes. A venv doesn't load the guard. With
A few minor ones:
If you'd rather hand it off, I can build the follow-up on your branch once #777 settles and keep your commits. |
|
@BaaRai the four gaps from your review are now either closed or listed as accepted limits in the PR body and CHANGELOG. |
Problem
#767, the
.pychalf of #771. A target tree can ship__pycache__/<mod>.cpython-311.pycnext to a.pythat reads clean. If the pyc header carries the source's mtime and size, or the pyc is hash-based (unchecked, or checked and built with the source's own hash),import <mod>runs the shipped bytecode and never compiles the reviewed source. This is filesystem behavior, so it is not visible to an intercept rule on command text.Change
artex-runnercreates the worker withPYTHONPYCACHEPREFIX=/usr/local/lib/artex-pycacheandPYTHONDONTWRITEBYTECODE=1. Python then looks for bytecode only under the prefix, never in an in-tree__pycache__, and does not write any.Dockerfile.workerrunscompileallinto that prefix at build time. The prefix sits on the read-only rootfs, so the worker cannot plant bytecode there. Without the prebuilt tree every run recompiles the stdlib: a 13-module import set went from 212 ms to 994 ms per start with an empty prefix, and is 179 ms with the prebuilt one. The tree adds 13 MB to the image.py_compileof/workspace/x.pywrites<prefix>/workspace/x.cpython-311.pyc. With the whole prefix read-only that failed ([Errno 30] Read-only file system, exit 1;mainexits 0). The runner now mounts small tmpfs subtrees at<prefix>/workspace,<prefix>/tmpand<prefix>/home/exec(noexec, nosuid, 64m, uid 10001, mode 0700).py_compileandcompileallexit 0 again, and the stdlib entries under<prefix>/usrstay on the read-only rootfs:touch <prefix>/usr/...andmkdir <prefix>/workspace/../usr/xboth fail with EROFS.py_compileor an archive extraction that honors absolute or../paths (Pythontarfileon this image,tar -P). This is an accepted limit: keeping the subtrees read-only would makepy_compileon a workspace file fail with EROFS.FilterPanelEnvreservesPYTHONPYCACHEPREFIXandPYTHONDONTWRITEBYTECODEnext toPATHand the proxy variables, so a panel process env cannot override them per exec.ENVin the image, so a customARTEX_WORKER_IMAGEgets it too. On an image without the prebuilt tree it is slower, not open.PYTHONNOUSERSITE=1, so a.pthin the user site under/home/execdoes not run at interpreter start.FilterPanelEnvreserves it too.PYTHONSAFEPATHis not set. It stops a script in/workspacefrom importing the module next to it (measured below), which is the open question on Track B: reverse injection, target artifacts must stay data (pickle, .pyc, git hooks, SDKs) #767.Measured
Python 3.11.2 from
Dockerfile.worker(bookworm), worker shape: read-only rootfs, uid 10001,cap-drop ALL, tmpfs/tmpand/home/exec. Each vector iscd <dir> && python3 main.py, wheremain.pyisimport <mod>and<mod>.pyprintsGOOD; the planted bytecode printsEVIL.DONTWRITEBYTECODEonlyPYTHONSAFEPATH=1__pycache__timestamp pyc, header forged to the.pymtime and sizepython3 -m mod__pycache__unchecked hash-based pyc__pycache__checked hash-based pyc, wrong hash__pycache__checked hash-based pyc built with the correct source hashmod.pyc, nomod.pymod.pycnext tomod.py__init__via__pycache__evil.pthnext to the script/workspace--check-hash-based-pycs alwayscloses only (b), not (c2) (EVIL onmainwith the flag), and has no environment variable.PYTHONPYCACHEPREFIX=/dev/nullcloses the same rows as this PR but costs the stdlib recompile.Bypasses of this PR, all measured:
python3 -E main.py: EVIL.-Eignores everyPYTHON*variable.python3 -Iwithsys.path.insert(0, <script dir>)in the script: EVIL.-Iimplies-E.env -u PYTHONPYCACHEPREFIX python3 main.py: EVIL. The variable is a default, not a boundary against the command itself.A
.pthonly runs from a site directory. In/workspaceit runs only throughsite.addsitedir(...)(#773 asks on that). The user site under/home/exec/.localis off withPYTHONNOUSERSITE=1;python3 -Eturns it back on.Repro, after
docker build -f Dockerfile.worker -t artex-worker:local .:Not covered
mod.pyc(row d). No interpreter variable turns offSourcelessFileLoader.-E,-Iplus a manualsys.pathinsert, and a command that unsets the variables.-EignoresPYTHONNOUSERSITEtoo, so it also brings back a.pthin the user site (-Idoes not, since it implies-s).mod.cpython-311-x86_64-linux-gnu.sobesidemod.pywins over the source and runs native code (EVIL_SOonmainand on this branch). Extensions come first in theFileFinderloader order, and/workspaceis a bind mount that allows exec. Closing it needs/workspacenoexec (the goal of fix(sandbox): add noexec,nosuid to /workspace bind mount #770, whichdockerrefused as a bind option) or a loader that refuses extensions outside system paths.py_compileon one version ofmod.pyand an extraction that keeps mtime then replaces it with another of the same size, the cached bytecode of the first version runs. That is normal timestamp-pyc behavior..zipor.eggonsys.path(viaPYTHONPATH,sys.path.insertor a.pth):zipimportruns a sourceless pyc, or a forged pyc next to its source, from the archive.importlib.machinery.SourcelessFileLoaderor a custom import hook. That is the command choosing to run bytecode, the same asexec./workspace,/tmpand/home/execsubtrees of the prefix are writable by uid 10001, so a pyc written there is loaded (see Change).ARTEX_SANDBOX=off. The env exists only in the worker container, so the panel-local path gets no protection.Recover()after a runner upgrade.Recoverreuses the running container by label, so a worker created before this change keeps its old env and mounts until it is removed and recreated.A follow-up I tested for the sourceless pyc and the
-Ebypass: a.pthin/usr/lib/python3/dist-packageson the read-only rootfs that setssys.pycache_prefixandsys.dont_write_bytecode, and replaces theFileFinderpath hook with one that has only the extension and source loaders. It does not touch the extension row. With it, rows a, b and g are GOOD under-Etoo, and row d isModuleNotFoundErrorwith and without-E. A workspacesitecustomize.pyonPYTHONPATHdoes not displace it.python3 -Sstill skips it (EVIL). The code has to sit inline in the.pthline, not in an imported module, or a module of the same name onPYTHONPATHshadows it. It changes interpreter startup for every Python process in the worker, so I kept it out of this PR.Overlap with #773
None in behavior. #773 matches command text (
PYTHONPATH=,sitecustomize,site.addsitedir() and strips.git/config; this PR changes where Python reads bytecode. Textually both edit the workerEnvlist insandbox/docker.go(this PR also adds to itsTmpfsmap, which #773 does not touch) andDockerfile.worker, so whichever lands second rebases the two env lines intoworkerEnv. If #773 lands,artex-exec-wrapis where a per-command check would go: it already runs before every command, so it could write aruntime_incidentfor a sourceless*.pycor a.pthunder the cwd, the same wayneutralizeCwdGitdoes. That is detection; the.pthguard above is the enforcement.Tests
The second needs
artex-worker:localandartex-egress-proxy:local.The live test plants a forged
__pycache__pyc, checks thatpython3 -Eloads it (the fixture works) andpython3does not, thatsys.pycache_prefixis set, that the stdlib cache exists in the image, thatpy_compileon a workspace file exits 0, and that the stdlib part of the prefix is not writable. With #764 on main, the test passes end to end.Refs #767