Hugging Face Diffusers Remote Code Execution via None.py
A remote code execution vulnerability exists in Hugging Face diffusers versions prior to 0.38.0 allowing arbitrary code execution through the `custom_pipeline` flow via a `None.py` file in a Hugging Face Hub repository, bypassing trust checks.
A remote code execution (RCE) vulnerability has been identified in Hugging Face diffusers library versions prior to 0.38.0. This flaw stems from insufficient validation in the DiffusionPipeline.from_pretrained function when loading custom pipelines from the Hugging Face Hub. By including a file named None.py in a model repository, an attacker can bypass the trust_remote_code check, leading to arbitrary code execution when a user loads the model. This vulnerability allows attackers to execute malicious code on a user’s machine simply by having them load a seemingly benign model, without requiring any explicit trust or custom pipeline specifications. The vulnerability was introduced due to a flaw in how the library resolves custom pipeline paths, leading to the unintentional inclusion of None.py as a valid custom pipeline file.
Attack Chain
- Attacker creates a Hugging Face Hub repository containing a malicious
None.pyfile, alongside other model files and amodel_index.jsonconfiguration file. - The
None.pyfile contains malicious code disguised within a class that inherits fromDiffusionPipeline, such as shadowingFluxPipelineand executing arbitrary commands like writing a file to/tmp/pwned. - A victim user attempts to load the model using
DiffusionPipeline.from_pretrained('attacker/malicious-repo'). - The
from_pretrainedfunction callsDiffusionPipeline.download(), which ordinarily checks fortrust_remote_codewhen a custom pipeline is specified. - Due to a flaw,
_resolve_custom_pipeline_and_clsresolvescustom_pipelinetoNone.pyif the file exists in the repo, bypassing thetrust_remote_codecheck because the check evaluatedcustom_pipeline is None -> False. - The
_get_pipeline_classfunction is then called with the resolvedNone.pypath, loading and executing the malicious code within the file. - The malicious code executes, performing actions such as creating a file, establishing a reverse shell, or exfiltrating data.
- The pipeline is instantiated and appears functional to the user, masking the underlying malicious activity.
Impact
Successful exploitation of this vulnerability allows an attacker to achieve remote code execution on the victim’s machine. This can lead to complete system compromise, data theft, or deployment of further malicious payloads. The vulnerability affects any user who loads a malicious model from the Hugging Face Hub using the vulnerable versions of the diffusers library. The impact is significant because it requires no user interaction beyond loading a model, making it easy to exploit at scale.
Recommendation
- Upgrade the
diffuserspackage to version 0.38.0 or later usingpip install --upgrade "diffusers>=0.38.0"to patch the vulnerability as recommended by the vendor. - Implement the provided Sigma rule
Detect Diffusers None.py RCEto detect the execution ofNone.pywithin the diffusers library. - Prioritize scanning Hugging Face Hub repositories before use, looking for unexpected
*.pyfiles, especiallyNone.py, using manual code review or automated tools. - As a workaround, only load models from trusted sources, and inspect local snapshots for unexpected
*.pyfiles as described in the advisory.
Detection coverage 2
Detect Diffusers None.py RCE
criticalDetects the execution of None.py as part of a diffusers pipeline, indicating a potential RCE vulnerability exploitation.
Detect Diffusers Dynamic Module Load from Unusual Location
highDetects loading of python modules from temporary directories, which can be indicative of malicious code execution in Diffusers
Detection queries are kept inside the platform. Get full rules →