Keras: TorchModuleWrapper can deserialize unsafe PyTorch pickle data
The TorchModuleWrapper.from_config method loads PyTorch pickle data with weights_only set to false unless an ambient safe mode is active, so a crafted layer configuration can trigger unsafe deserialisation. An attacker who can supply an untrusted Keras layer configuration could therefore achieve arbitrary code execution.
keras-team/keras
Originally disclosed on huntr
A vulnerability in keras-team/keras version 3.15.0 allows unsafe deserialization of attacker-controlled PyTorch pickle data through the public keras.layers.TorchModuleWrapper.from_config method. This method invokes torch.load(..., weights_only=False) without requiring an explicit unsafe opt-in, such as a safe_mode=False parameter. When called outside a SafeModeScope(True) context, the absence of an ambient safe mode state permits unsafe deserialization by default. This issue can lead to arbitrary code execution if untrusted Keras layer configurations are processed using this method. The vulnerability arises because the method does not enforce safe deserialization practices unless explicitly guarded by Keras safe mode.
From the GitHub Advisory Database (GHSA-v2w2-w228-c444, CC BY 4.0).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the method calls torch.load with weights_only disabled and only relies on an ambient safe-mode flag rather than requiring an explicit unsafe opt-in.
Impact
An attacker who can supply an untrusted Keras layer configuration could achieve arbitrary code execution, unless the call happens inside a SafeModeScope(True) context.
What the attacker needed
Attacker can supply a layer configuration; no SafeModeScope(True) is active
The fix
The method should require an explicit unsafe opt-in before loading pickle data, rather than defaulting to unsafe deserialisation when no safe mode is set.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Pickle is Python's way of saving objects to disk, but loading a pickle can run code hidden inside it. Here, a Keras function that rebuilds a layer from a saved configuration loads PyTorch pickle data in an unsafe mode by default. If someone can give the application a malicious configuration, that hidden code may run on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.