Repository navigation
Conversation
make_tempdir() builds a predictable name inside the OS temporary directory and used whatever was already there. On a shared host another user could create the directory (or a symlink) first and replace the shared objects that the JIT cache loads. Create the directory with mode 0700 and only accept a real directory owned by the current user and not writable by group or others. Fixes #3130
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3131 +/- ##
==========================================
+ Coverage 83.99% 84.00% +0.01%
==========================================
Files 261 261
Lines 60001 60051 +50
Branches 5064 5069 +5
==========================================
+ Hits 50396 50447 +51
Misses 8760 8760
+ Partials 845 844 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
JDBetteridge
left a comment
There was a problem hiding this comment.
See discussion on slack
| tmpdir.mkdir(parents=True, exist_ok=True) | ||
| with suppress(FileExistsError): | ||
| os.mkdir(tmpdir, 0o700) |
There was a problem hiding this comment.
Why would you remove exist_ok if you're going to suppress the error it raises anyway
There was a problem hiding this comment.
Restored in 1d11efe: it is tmpdir.mkdir(mode=0o700, parents=True, exist_ok=True) again, with no suppress.
| # The name is predictable and the OS temporary directory is typically | ||
| # world-writable, so another user may have created `tmpdir` (or a symlink | ||
| # with that name) first. Shared objects are loaded from here, so only | ||
| # accept a real directory that we own and that nobody else can write to |
There was a problem hiding this comment.
Comment way too long and does little to explain the complex check it preceeds
There was a problem hiding this comment.
Cut to one line in 1d11efe, and the check is split into three named conditions (is_dir, is_ours, is_private).
| assert not list(tmp_path.iterdir()) | ||
|
|
||
|
|
||
| def test_make_tempdir(tmp_path, monkeypatch): |
There was a problem hiding this comment.
AI adds monkeypatch to every test it writes it would seem, I thought we agreed not to do this?
There was a problem hiding this comment.
Removed in 1d11efe. The tests now use the OS temporary directory with a unique prefix. The other-owner case needed a faked lstat, so it is dropped and the description says it is untested.
|
|
||
|
|
||
| @pytest.mark.parametrize('plant', ['writable', 'symlink', 'file', 'foreign']) | ||
| def test_make_tempdir_untrusted(tmp_path, monkeypatch, plant): |
A directory that is not private to the current user was refused with a PermissionError. That breaks setups where the check cannot pass, such as filesystems that report every directory as group or world writable, and caches created under umask 002. Warn and use a fresh private directory instead, reused for the lifetime of the process. The JIT cache is then not reused across runs. The tests no longer use monkeypatch. They use the OS temporary directory with a unique prefix. The case of a directory owned by another user is dropped, as creating one needs root.
|
Given that this is essentially a user specific cache, maybe we should think about moving these files out of |
Fixes #3130.
Problem
make_tempdir()builds a predictable name (devito-<prefix>-uid<UID>) inside the OS temporary directory and used whatever already existed under that name. On a shared host, another local user could create that directory (or a symlink with that name) before the victim's first Devito run, then replace the shared objects thatjit_compileloads from the JIT cache. Loading a replaced library runs its constructor as the victim.Fix
make_tempdir()now creates the directory with mode 0700 and only uses it if it is a real directory (checked withlstat, so not a symlink), owned by the current user, and not writable by group or others. The same function backs both the JIT cache and the codepy cache.Otherwise it warns and returns a fresh private directory from
mkdtemp(), which is reused for the lifetime of the process. Nothing raises.Behaviour change
Where the check cannot pass, Devito keeps working but the JIT cache is not reused across runs, so each run recompiles. This covers:
TMPDIRat a filesystem with Unix permissions restores caching.The fallback directory is not removed at exit.
Tests
tests/test_tools.pygains a test that a fresh directory is created private and reused, and a parametrised test that a world-writable directory, a symlink and a plain file are not used and that the fallback is private and reused within the process. The tests use the OS temporary directory with a unique prefix and do not usemonkeypatch.A directory owned by another user is not tested, as creating one needs root.
Run locally: the new tests pass with the fix and fail against
main'smake_tempdir;tests/test_arch.pyandtests/test_tools.pypass in full and flake8 is clean. ruff and isort were not run locally, and neither was a compiled Operator end to end, so CI is the first full run.🤖 Generated with Claude Code