Skip to content

tools: Do not use an untrusted deterministic temporary directory - #3131

Closed
ggorman wants to merge 2 commits into
mainfrom
fix-tempdir-ownership
Closed

ggorman wants to merge 2 commits into
mainfrom
fix-tempdir-ownership

Conversation

@ggorman

@ggorman ggorman commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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 that jit_compile loads 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 with lstat, 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:

The fallback directory is not removed at exit.

Tests

tests/test_tools.py gains 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 use monkeypatch.

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's make_tempdir; tests/test_arch.py and tests/test_tools.py pass 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

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
@ggorman
ggorman requested a review from JDBetteridge November 30, 2026 18:30
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.00%. Comparing base (afa5529) to head (1d11efe).

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     
Flag Coverage Δ
pytest-gpu-aomp-amdgpuX 68.26% <68.42%> (+0.01%) ⬆️
pytest-gpu-gcc- 78.81% <100.00%> (+0.01%) ⬆️
pytest-gpu-icx- 78.73% <100.00%> (-0.01%) ⬇️
pytest-gpu-nvc-nvidiaX 69.08% <68.42%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

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

See discussion on slack

Comment thread devito/tools/os_helper.py Outdated
Comment on lines +36 to +38
tmpdir.mkdir(parents=True, exist_ok=True)
with suppress(FileExistsError):
os.mkdir(tmpdir, 0o700)

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.

Why would you remove exist_ok if you're going to suppress the error it raises anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Restored in 1d11efe: it is tmpdir.mkdir(mode=0o700, parents=True, exist_ok=True) again, with no suppress.

Comment thread devito/tools/os_helper.py Outdated
Comment on lines +40 to +43
# 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

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.

Comment way too long and does little to explain the complex check it preceeds

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cut to one line in 1d11efe, and the check is split into three named conditions (is_dir, is_ours, is_private).

Comment thread tests/test_tools.py Outdated
assert not list(tmp_path.iterdir())


def test_make_tempdir(tmp_path, monkeypatch):

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.

AI adds monkeypatch to every test it writes it would seem, I thought we agreed not to do this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread tests/test_tools.py Outdated


@pytest.mark.parametrize('plant', ['writable', 'symlink', 'file', 'foreign'])
def test_make_tempdir_untrusted(tmp_path, monkeypatch, plant):

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.

As above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same, removed in 1d11efe.

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.
@ggorman ggorman changed the title tools: Refuse an untrusted deterministic temporary directory tools: Do not use an untrusted deterministic temporary directory Nov 30, 2026
@JDBetteridge

Copy link
Copy Markdown
Contributor

Given that this is essentially a user specific cache, maybe we should think about moving these files out of TMPDIR entirely and placing them in XDG_CACHE_HOME or XDG_RUNTIME_DIR

@ggorman ggorman closed this Dec 1, 2026
@ggorman
ggorman deleted the fix-tempdir-ownership branch December 1, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants