Skip to content

Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery#863

Open
tomaioo wants to merge 1 commit into
AMD-AGI:mainfrom
tomaioo:fix/security/unsafe-dynamic-module-loading-in-patch-a
Open

Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery#863
tomaioo wants to merge 1 commit into
AMD-AGI:mainfrom
tomaioo:fix/security/unsafe-dynamic-module-loading-in-patch-a

Conversation

@tomaioo

@tomaioo tomaioo commented Jul 8, 2026

Copy link
Copy Markdown

Summary

Security: Unsafe Dynamic Module Loading in Patch Auto-Discovery

Problem

Severity: High | File: primus/backends/maxtext/patches/__init__.py:L28

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.

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)

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

1 participant