GHSA-77wq-646f-jrm2: Duplicate Advisory: The Keras `Model.load_model` method **silently** ignores `safe_mode=True` and allows arbitrary code execution when a `.h5`/`.hdf5` file is loaded.
N/A
Basic Information
Technical Details
| Package Name | Ecosystem | Vulnerable Versions | First Patched Version |
|---|---|---|---|
| keras | pip | >= 3.0.0, < 3.11.3 | 3.11.3 |
Vulnerability Intelligence
Miggo AI
Root Cause Analysis
The vulnerability exists in the model loading process for the legacy .h5/.hdf5 format in Keras 3. The root cause is that the safe_mode=True parameter in the top-level keras.saving.load_model function was not being propagated down to the function responsible for handling H5 files, keras.legacy.saving.legacy_h5_format.load_model_from_hdf5.
The exploit takes advantage of the Lambda layer in Keras, which allows for the serialization of arbitrary Python code. An attacker can create a malicious H5 model file containing a pickled Lambda layer. When this model is loaded using load_model, the absence of the safe_mode check in the H5 loading path allows the Lambda.from_config method to deserialize and execute the embedded arbitrary code.
The patch addresses this by ensuring the safe_mode parameter is passed through the entire call chain, from saving_api.load_model to legacy_h5_format.load_model_from_hdf5. The latter function was modified to accept the safe_mode parameter and use a SafeModeScope to conditionally block the deserialization of Lambda layers, thus preventing the arbitrary code execution.
Vulnerable functions
saving_api.load_modelkeras/src/saving/saving_api.py
legacy_h5_format.load_model_from_hdf5keras/src/legacy/saving/legacy_h5_format.py
lambda_layer.Lambda.from_configkeras/src/layers/core/lambda_layer.py