/

CVE-2026-31217

Severity:

CRITICAL RISK

(9.8)

Arbitrary Code Execution in optimate

CVE-2026-31217

Analyzed

Vulnerability type

Category:

Arbitrary Code Execution

CWE:

CWE-94

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

The neural_magic_training.py script in the optimate project executes a user-supplied module.py file using Python's exec(), without validation or restrictions.

If an attacker can control the directory passed to the --model argument, they can run arbitrary Python code with the privileges of the process executing the script.

Affected context

This vulnerability arises in environments where the optimate neural_magic_training.py script is used to load models from a filesystem path supplied at runtime.

The issue is relevant when:

  • The --model argument can point to directories not fully controlled or vetted by trusted operators, and
  • The script assumes that module.py inside the given directory is safe to execute.

It is particularly risky in shared or automated workflows (e.g., CI/CD, training pipelines, or multi-user servers) where users or external processes can influence the model directory path or its contents.

Exploit preconditions

  • Attack surface: Command-line interface parameter (--model) consumed by neural_magic_training.py.
  • Required access: Ability to provide or influence the directory path passed to --model and to create or modify a module.py file under that directory.
  • Trigger point: Invocation of the _load_model() function, which reads module.py and executes it via Python's exec().
  • Why exploitation is possible: The implementation treats the contents of module.py as trusted Python code, executing it directly without validation, sandboxing, or restriction, effectively giving full interpreter control to any attacker who controls that file.

Why this happens

The core issue is the direct execution of untrusted input as code, which maps to CWE-94: Improper Control of Generation of Code ('Code Injection').

The _load_model() function retrieves a module.py file from a user-specified directory and passes its contents to Python's exec() built-in.

This pattern assumes that filesystem content at the provided path is benign and trustworthy, but in practice:

  • The path can often be influenced by end users, automation, or external systems.
  • There is no structural validation (e.g., checking for specific functions only) or syntactic constraint beyond Python's own parsing.
  • No sandboxing or privilege separation is in place to limit what the executed code can do.

As a result, the function grants arbitrary execution capability to anyone who can place or alter module.py in the chosen directory.

Potential impact

  • Execution of arbitrary Python code with the full privileges of the user or service account running the training script.
  • Reading, modification, or deletion of local files accessible to the process.
  • Exfiltration of sensitive data such as training datasets, model weights, or API keys from the environment.
  • Installation of backdoors or persistence mechanisms in the environment hosting the training pipeline.
  • Lateral movement within the infrastructure if credentials, network access, or cloud metadata are accessible from the compromised process.

General mitigation guidance

Mitigation commonly focuses on treating model loading as data processing, not code execution, and minimizing trust in user-supplied paths.

General defensive patterns include:

  • Avoiding direct use of exec() or similar dynamic code execution primitives on files derived from user-controlled paths.
  • Designing the model loading API around declarative formats (e.g., JSON, YAML, or framework-native checkpoint formats) rather than arbitrary Python modules.
  • If extensibility is required, providing a vetted plugin system with:
  • Explicit registration of allowed entry points, and
  • Whitelisting or signing of plugin code.
  • Enforcing strict controls over where models can be loaded from, such as:
  • Restricted, administrator-managed directories, or
  • Signed artifacts retrieved from a trusted registry or storage.
  • Running training and model-handling code under least-privilege accounts and, where feasible, in isolated environments (e.g., containers, sandboxes) to limit damage if untrusted code executes.

How teams detect issues like this

Detection strategies typically target both code patterns and runtime behavior:

  • Static analysis and code review:
  • Flag uses of exec(), eval(), importlib, or dynamic imports that are influenced by external input (like command-line arguments or environment variables).
  • Look for filesystem reads of Python code (e.g., module.py) followed by direct execution without checks.
  • Establish review checklists requiring justification and safeguards for any dynamic execution construct.
  • Configuration and policy enforcement:
  • Enforce policies in CI pipelines that disallow new uses of exec() or similar primitives unless explicitly approved.
  • Use pre-commit hooks or linters configured to detect insecure code execution patterns.
  • Runtime monitoring:
  • Monitor training and pipeline environments for unexpected file or network activity originating from training scripts.
  • Log and review invocations of sensitive scripts with their --model paths, especially when those paths deviate from expected, controlled locations.

Sources

SourceLink
github.comgithub.com/nebuly-ai/optimate
notion.sonotion.so/CVE-2026-31217-35d1e13931888179ae40dea5258d2db9

Frequently asked

questions

It can be, depending on how the script is integrated. If a remote service, API, web UI, or job scheduler allows users to specify or influence the --model directory, or to upload model artifacts that include module.py, then an attacker can exploit the issue remotely through that interface.

The attacker must be able to control or significantly influence the directory supplied to --model and place or modify a module.py file there. This may correspond to local user access on a shared machine, access to a storage bucket or artifact repository used for models, or logical access through an application that passes user inputs to the script.

The safest approach is to remove the reliance on exec() for loading model logic. Instead, use structured, non-executable formats for model configuration and weights, or a tightly controlled plugin interface with explicit entry points and trusted, pre-approved code. Model directories should not be treated as arbitrary code bundles.

Avoid treating user-supplied files and paths as executable code, especially in ML workflows where models are often shared and downloaded. Establish coding standards that prohibit unbounded use of exec()/eval(), require threat modeling for any dynamic loading mechanism, and integrate automated checks into the development pipeline to flag dangerous patterns early.

Find out what's already
exploitable in your codebase.