Skip to content

Constrain APEX_ASP_CACHE_DIR to a safe base directory#2011

Open
LeSingh1 wants to merge 2 commits into
NVIDIA:masterfrom
LeSingh1:fix-1998-asp-cache-path-traversal
Open

Constrain APEX_ASP_CACHE_DIR to a safe base directory#2011
LeSingh1 wants to merge 2 commits into
NVIDIA:masterfrom
LeSingh1:fix-1998-asp-cache-path-traversal

Conversation

@LeSingh1
Copy link
Copy Markdown
Contributor

Problem

The ASP permutation cache reads APEX_ASP_CACHE_DIR and uses it directly as the destination passed to np.save(), with no validation (apex/contrib/sparsity/permutation_search_kernels/exhaustive_search.py). A caller who controls that environment variable can redirect cache writes outside the intended directory, including via .. traversal or an absolute path (CWE-22 / CWE-73), as reported in #1998.

Fix

Add _resolve_cache_dir(), which canonicalizes the requested directory with os.path.realpath and requires it to stay within the default .cache base (checked via os.path.commonpath). If the requested path escapes the base, it warns and falls back to the safe default. generate_all_unique_combinations now routes through this helper. Stdlib-only, minimal.

Testing

  • Added apex/contrib/sparsity/test/test_asp_cache_path.py (3 tests): a normal in-base path is used, the helper resolves correctly, and a traversal attempt is rejected.
  • Confirmed the test is meaningful: on unfixed code the traversal test fails (the write escapes to a temp /.../evil/... path); with the fix it is blocked.
  • pytest → 3 passed; ruff check clean.

Note: developed with AI assistance and verified locally as described above.

Fixes #1998

LeSingh1 and others added 2 commits May 31, 2026 16:18
The ASP permutation cache read APEX_ASP_CACHE_DIR directly and used it as the
np.save() destination with no validation, so an externally controlled env var
could redirect cache writes to an arbitrary writable location (CWE-22/CWE-73),
enabling cache poisoning or file overwrite.

Resolve the requested cache dir and require it to stay within the default cache
base, otherwise warn and fall back to the safe default. Add regression tests
covering a rejected traversal attempt and a normal in-base path.

Signed-off-by: LeSingh1 <sshaurya914@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CWE-22/CWE-73 in permutation cache path: APEX_ASP_CACHE_DIR controls write destination

1 participant