Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery#863
Open
tomaioo wants to merge 1 commit into
Open
Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery#863tomaioo wants to merge 1 commit into
tomaioo wants to merge 1 commit into
Conversation
The `_auto_import_patch_modules` function in `primus/backends/maxtext/patches/__init__.py` automatically imports all modules ending with `_patches` or `_patch` from the package directory. This is a form of dynamic module loading based on file system contents. If an attacker can write a file matching this pattern to the package directory, they can achieve arbitrary code execution when the package is imported. The function uses `importlib.import_module()` which will execute any code in the imported module's top-level scope. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
tomaioo
requested review from
Xiaoming-AMD,
limou102 and
wenxie-amd
as code owners
July 8, 2026 06:10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery
Problem
Severity:
High| File:primus/backends/maxtext/patches/__init__.py:L28The
_auto_import_patch_modulesfunction inprimus/backends/maxtext/patches/__init__.pyautomatically imports all modules ending with_patchesor_patchfrom the package directory. This is a form of dynamic module loading based on file system contents. If an attacker can write a file matching this pattern to the package directory, they can achieve arbitrary code execution when the package is imported. The function usesimportlib.import_module()which will execute any code in the imported module's top-level scope.Solution
Implement a whitelist of allowed patch modules instead of auto-discovering based on file name patterns. Verify the integrity of patch modules using cryptographic hashes or signatures before importing them. Consider loading patches from a secure, read-only location rather than from the general package directory.
Changes
primus/backends/maxtext/patches/__init__.py(modified)