Skip to content

Commit 6fd6b6c

Browse files
rparolinclaude
andcommitted
Address review: scope cache perms to tmp, revert cybind docstring
Per @leofang's review on PR #2399: - _file_stream.py: only the tmp/ staging dir is created owner-only (0o700). root/ and entries/ now inherit the umask / any pre-existing permissions so a deliberately shared kernel cache (e.g. group-writable on a compute cluster) keeps working. Dropped the post-mkdir chmod that would have re-tightened an existing shared directory. 0o700 in mkdir mode needs no chmod: umask only clears bits. - _internal/utils.pyx: revert the get_buffer_pointer docstring change; that is cybind code and is out of scope for this PR. - Tests updated to match: assert only tmp is 0o700, and that a pre-existing 0o777 shared root is used as-is. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 1d74206 commit 6fd6b6c

3 files changed

Lines changed: 24 additions & 30 deletions

File tree

‎cuda_bindings/cuda/bindings/_internal/utils.pyx‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,7 @@ cdef bint is_nested_sequence(data):
4242

4343

4444
cdef void* get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except*:
45-
"""Return a raw pointer into ``buf``.
46-
47-
The caller must keep ``buf`` alive AND not resize it while the pointer is in
48-
use. The buffer export is released before this returns, so resizing a mutable
49-
buffer (e.g. ``bytearray``) during a ``nogil`` call is a use-after-free.
50-
Prefer immutable ``bytes``.
51-
"""
45+
"""The caller must ensure ``buf`` is alive when the returned pointer is in use."""
5246
cdef void* bufPtr
5347
cdef int flags = cpython.PyBUF_ANY_CONTIGUOUS
5448
if not readonly:

‎cuda_core/cuda/core/utils/_program_cache/_file_stream.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,16 @@ def __init__(
397397
self._entries = self._root / _ENTRIES_SUBDIR
398398
self._tmp = self._root / _TMP_SUBDIR
399399
self._max_size_bytes = max_size_bytes
400-
# Cache holds compiled device code loaded via cuLibraryLoadData, so keep
401-
# it owner-only (0o700) so other local users can't read or replace it.
402-
# mkdir's mode is umask-masked and exist_ok keeps an existing dir's
403-
# perms, so re-chmod on POSIX.
404-
self._root.mkdir(parents=True, exist_ok=True, mode=0o700)
405-
self._entries.mkdir(exist_ok=True, mode=0o700)
400+
self._root.mkdir(parents=True, exist_ok=True)
401+
self._entries.mkdir(exist_ok=True)
402+
# ``tmp`` stages in-flight writes before they are atomically renamed into
403+
# ``entries``. Create it owner-only (0o700) to keep other local users from
404+
# reading or replacing compiled device code mid-write. ``root``/``entries``
405+
# intentionally inherit the umask / any pre-existing permissions so a
406+
# deliberately shared cache directory keeps working (see PR #2399 review).
407+
# mkdir's mode is not chmod'd afterward, so an existing ``tmp`` is left
408+
# as-is rather than re-tightened.
406409
self._tmp.mkdir(exist_ok=True, mode=0o700)
407-
if os.name != "nt":
408-
for d in (self._root, self._entries, self._tmp):
409-
os.chmod(d, 0o700)
410410
# Opportunistic startup sweep of orphaned temp files left by any
411411
# crashed writers. Age-based so concurrent in-flight writes from
412412
# other processes are preserved.

‎cuda_core/tests/test_program_cache.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,37 +2539,37 @@ def reader(tid: int) -> None:
25392539

25402540

25412541
@pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits only")
2542-
def test_program_cache_dir_created_owner_only(tmp_path):
2543-
"""#359/#375: the on-disk program cache stores raw executable device code,
2544-
so its directory tree must be created owner-only (0o700), independent of the
2545-
inherited umask, so other local principals can neither read cached kernels
2546-
nor plant a binary this process would later load."""
2542+
def test_program_cache_tmp_dir_created_owner_only(tmp_path):
2543+
"""#359/#375: ``tmp`` stages in-flight compiled device code before the atomic
2544+
rename into ``entries``, so it must be created owner-only (0o700) regardless of
2545+
the inherited umask. ``root``/``entries`` intentionally inherit the umask to keep
2546+
deliberately shared caches working (PR #2399 review), so only ``tmp`` is asserted."""
25472547
import stat
25482548

25492549
from cuda.core.utils._program_cache._file_stream import FileStreamProgramCache
25502550

25512551
root = tmp_path / "pc"
25522552
FileStreamProgramCache(path=root)
25532553

2554-
for d in (root, root / "entries", root / "tmp"):
2555-
mode = stat.S_IMODE(os.stat(d).st_mode)
2556-
assert mode == 0o700, f"{d} has mode {oct(mode)}"
2554+
mode = stat.S_IMODE(os.stat(root / "tmp").st_mode)
2555+
assert mode == 0o700, f"tmp has mode {oct(mode)}"
25572556

25582557

25592558
@pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits only")
2560-
def test_program_cache_dir_tightens_preexisting_world_writable(tmp_path):
2561-
"""#359: a pre-created world-writable cache root (the poisoning precondition)
2562-
must be re-tightened to 0o700; mkdir(exist_ok=True) alone would keep the
2563-
attacker-supplied permissions."""
2559+
def test_program_cache_preexisting_shared_root_used_as_is(tmp_path):
2560+
"""PR #2399 review: a deliberately shared cache root (e.g. group-writable on a
2561+
compute cluster) must be used as-is, not re-tightened. Only ``tmp`` is forced
2562+
owner-only; ``root`` keeps whatever permissions it was created with."""
25642563
import stat
25652564

25662565
from cuda.core.utils._program_cache._file_stream import FileStreamProgramCache
25672566

25682567
root = tmp_path / "pc"
25692568
root.mkdir()
2570-
# Simulate the attack precondition: a pre-existing world-writable cache dir.
2569+
# Simulate an intentionally shared cache directory.
25712570
os.chmod(root, 0o777) # noqa: S103
25722571

25732572
FileStreamProgramCache(path=root)
25742573

2575-
assert stat.S_IMODE(os.stat(root).st_mode) == 0o700
2574+
assert stat.S_IMODE(os.stat(root).st_mode) == 0o777
2575+
assert stat.S_IMODE(os.stat(root / "tmp").st_mode) == 0o700

0 commit comments

Comments
 (0)