Summary
LLaMA-Factory through version 0.9.5 allows attackers with WebUI access to execute arbitrary Python code by providing a crafted model path in Chat or Training workflows.
Because user-controlled model paths are passed directly into Hugging Face from_pretrained() calls with trust_remote_code=True, the server may fetch and execute untrusted code from remote or local model repositories.
Affected context
This vulnerability appears in deployments of LLaMA-Factory that expose the WebUI for chat, inference, or training tasks. It becomes relevant when users are allowed to specify custom model paths or repositories, and those values are forwarded to the underlying Hugging Face Transformers APIs. The risk is heightened in environments where the server runs with elevated privileges or has access to sensitive data, because any loaded model can introduce executable code if remote code execution is implicitly trusted.
Exploit preconditions
- Attack surface: LLaMA-Factory WebUI endpoints for Chat and Training that accept a model path or repository identifier.
- Required access: Ability to authenticate or otherwise interact with the WebUI interface where model paths can be supplied.
- Trigger point: Submission of a malicious model path that points to a repository containing custom loading code; the application then calls
AutoTokenizer.from_pretrained()andAutoModel.from_pretrained()withtrust_remote_code=True. - Why exploitation is possible: User input is treated as a trusted reference to executable model code, and the framework automatically imports and runs repository-provided Python without validation, sandboxing, or restriction, aligning with CWE-94 and CWE-829.
Why this happens
The bug stems from directly binding user-controlled model path input to dynamic code loading mechanisms in the Hugging Face Transformers library. When trust_remote_code=True is hardcoded, the framework will execute arbitrary Python modules shipped with the specified model repository during object initialization. Conceptually, this is an instance of CWE-94: Improper Control of Generation of Code, because external input controls which code is loaded and executed. It also reflects CWE-829: Inclusion of Functionality from Untrusted Control Sphere, where remote or locally supplied repositories are treated as trusted components without provenance checks, review, or isolation.
Potential impact
- Attackers may execute arbitrary Python code with the privileges of the LLaMA-Factory server process.
- Malicious models could exfiltrate API keys, secrets, or training data accessible to the application.
- An attacker can modify or corrupt model weights and outputs, leading to data poisoning or unreliable inference.
- Compromised servers may be used as a foothold for lateral movement within the hosting environment.
- Persistent backdoors or cryptominers may be deployed through hostile model loading code.
General mitigation guidance
Effective mitigation focuses on tightening the trust boundary around model loading and reducing the ability for user inputs to control executable code sources. Common defensive patterns include:
- Disabling or avoiding
trust_remote_code=Truefor user-specified models, favoring standard, non-executable config formats. - Restricting model selection to a curated, vetted catalog rather than arbitrary paths or repositories supplied via WebUI.
- Introducing strict validation and allowlisting on model identifiers to ensure only known-safe repositories are referenced.
- Running the application under least-privilege accounts and isolating model loading in constrained environments to limit blast radius.
- Treating all externally sourced models as untrusted artifacts and applying code review or provenance checks before deployment.
How teams detect issues like this
Organizations can detect and manage this class of vulnerability by:
- Defining static analysis rules and configuration checks that flag
trust_remote_code=Trueusage in code paths influenced by user input. - Reviewing WebUI handlers and API endpoints for any parameters that are forwarded into dynamic
from_pretrained()calls. - Enforcing security policies that require curated model registries and prohibit runtime loading of arbitrary repositories.
- Using automated scanning tools to monitor dependencies and application configuration for risky dynamic loading patterns.
- Instrumenting logging around model loading events to identify unusual or unapproved repositories being fetched by the server.

