Skip to content

Harden program-cache permissions, binary-path resolution, and coredump helper lifetime#2399

Open
rparolin wants to merge 10 commits into
NVIDIA:mainfrom
rparolin:hardening/cache-perms-loader-coredump
Open

Harden program-cache permissions, binary-path resolution, and coredump helper lifetime#2399
rparolin wants to merge 10 commits into
NVIDIA:mainfrom
rparolin:hardening/cache-perms-loader-coredump

Conversation

@rparolin

@rparolin rparolin commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Small safety fixes across three areas, each with a test. Mostly hardening; one intentional behavior change is called out below.

What changed

cuda.core

  • The program cache writes compiled GPU code to disk. It now creates that folder as owner-only (0o700) so other users on the machine can't read or tamper with it.

cuda.pathfinder

  • find_nvidia_binary_utility now always returns a full absolute path, as the docs promise (it could return a relative one before).
  • On Windows, if AddDllDirectory fails, we now warn instead of failing silently.

cuda.bindings

  • Fix two memory bugs in the coredump-settings helper: it kept a pointer to bytes it didn't own (dangling pointer), and never freed a 1 KB buffer (leak).
  • Clarify get_buffer_pointer's docs: don't resize the buffer while its pointer is in use.
  • Behavior change: a Python int that's too big for a c_int/c_byte kernel argument now raises OverflowError instead of being silently truncated (e.g. 2**32+5 used to become 5). This is stricter than ctypes' own silent wrap — flagged for reviewer sign-off.

Tests

Added tests for the folder permissions, the absolute path, the coredump helper, and the out-of-range kernel argument. All pass locally (RTX 5880).

One thing to confirm

The folder-permission fix uses chmod. If the cache folder is owned by someone else (a shared cache), construction will now raise an error instead of silently continuing. That's intentional — shared caches aren't safe — but please confirm it's okay. Easy to soften if you'd rather it tolerate that case.

Reviewers: @leofang (core/bindings), @rwgk (pathfinder).

…er lifetime

- cuda.core: create the on-disk program cache tree owner-only (0o700) and
  re-assert restrictive perms on POSIX, so cached device code cannot be read
  or planted by other local users regardless of the inherited umask.
- cuda.pathfinder: absolutize the resolved binary-utility path to honor the
  documented absolute-path contract; surface AddDllDirectory failures on
  Windows with GetLastError instead of silently swallowing them.
- cuda.bindings: retain the caller's bytes in _HelperCUcoredumpSettings so the
  borrowed pointer cannot outlive its backing buffer, and free the getter's
  1 KiB buffer in __dealloc__; clarify get_buffer_pointer's lifetime contract.

Adds regression tests for the cache permissions, path absolutization, and
coredump helper lifetime.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@rparolin rparolin added this to the cuda.core next milestone Jul 21, 2026
@rparolin rparolin added bug Something isn't working cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module cuda.pathfinder Everything related to the cuda.pathfinder module labels Jul 21, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@rparolin
rparolin requested review from leofang and rwgk July 21, 2026 20:40
@rparolin
rparolin marked this pull request as ready for review July 21, 2026 20:41
rparolin and others added 3 commits July 21, 2026 13:47
Shorten the explanatory comments to a line or two each and drop the internal
issue-number references (which don't resolve in this repo). No code changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
It didn't actually guard the two lifetime fixes: the driver copies the path
during the set call (so the NVIDIA#379 latent UAF can't trigger), and a missing free
leaks silently (so NVIDIA#381 wouldn't fail the test either).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@rparolin rparolin self-assigned this Jul 21, 2026
The test pre-creates a 0o777 cache dir to verify the loader tightens it to
0o700; the permissive mask is the point of the test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

rparolin and others added 4 commits July 21, 2026 15:34
The abspath change makes find_nvidia_binary_utility return a drive-qualified
path on Windows (C:\... ), so the three search-order tests must compare against
os.path.abspath(expected). No-op on Linux; fixes the Windows CI failures.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ting

The c_int/c_byte param feeders silently narrowed an out-of-range Python int
(e.g. 2**32+5 -> 5). Range-check in the feeder and raise OverflowError; declare
feed() as 'except? -1' so Cython propagates the exception instead of swallowing
it. Addresses Glasswing V9.1 (leofang chose the raise option over matching
ctypes' silent wrap).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Per Leo's review: on 3.13+ PyLong_AsInt converts and range-checks in one call
(raising OverflowError itself), so gate the manual INT_MIN/INT_MAX check to
pre-3.13 only. c_byte keeps the manual check (no 8-bit CPython equivalent).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…flow

Per Leo's follow-up: use a single code path on all supported Pythons instead of
version-gating PyLong_AsInt. PyLong_AsLongAndOverflow flags out-of-long values
via its overflow out-param (no exception set), then we bounds-check the 32-bit
(c_int) / 8-bit (c_byte) target range and raise OverflowError. Drops the
PY_VERSION_HEX gate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…#2402)

The out-of-range c_int/c_byte -> OverflowError change is a distinct behavior
change; moved to a standalone PR so it can be reviewed separately from this
hardening batch. No functional change to the remaining hardening work.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
use. The buffer export is released before this returns, so resizing a mutable
buffer (e.g. ``bytearray``) during a ``nogil`` call is a use-after-free.
Prefer immutable ``bytes``.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This code is part of cybind (which @mdboom is improving). Let's not touch it.

Comment on lines +400 to +409
# Cache holds compiled device code loaded via cuLibraryLoadData, so keep
# it owner-only (0o700) so other local users can't read or replace it.
# mkdir's mode is umask-masked and exist_ok keeps an existing dir's
# perms, so re-chmod on POSIX.
self._root.mkdir(parents=True, exist_ok=True, mode=0o700)
self._entries.mkdir(exist_ok=True, mode=0o700)
self._tmp.mkdir(exist_ok=True, mode=0o700)
if os.name != "nt":
for d in (self._root, self._entries, self._tmp):
os.chmod(d, 0o700)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure this is acceptable. One can imagine in a compute cluster a group of users want to share the same kernel cache. If the directory already exists, we should use it as-is without changing the permission.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

self._tmp can certainly be made owner-only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module cuda.pathfinder Everything related to the cuda.pathfinder module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants