# CVE-2025-9905: Keras arbitrary code execution via safe_mode bypass in H5 loading

Source: https://junglewise.ai/threats/cve-2025-9905-keras-arbitrary-code-execution-via-safe-mode-bypass-in-h5-loading  
Publisher: Junglewise Threat Intelligence, https://junglewise.ai/threats  
Published: 2025-09-19. Updated: 2026-06-06  
License: CC BY 4.0 with a link back

CVE-2025-9905 · Severity: high · CVSS 4 · Published 2025-09-19

Keras, a popular machine learning library, contains a vulnerability where it fails to enforce security settings when loading older model file formats (.h5 and .hdf5). Even if a user explicitly enables 'safe mode' to prevent malicious code from running, the system ignores this setting for these specific files. An attacker could distribute a specially crafted model file that, when opened by a user, executes arbitrary commands on their computer with the user's full permissions.

Technologies: [keras (PyPI)](https://junglewise.ai/threats/technologies/keras). Vendors: [PyPI](https://junglewise.ai/threats/vendors/pypi).

## Quick answers

**Is CVE-2025-9905 exploited in the wild?** No. Junglewise Threat Intelligence has no report of CVE-2025-9905 being exploited in the wild.

**How severe is CVE-2025-9905?** High severity, with a CVSS score of 4, published 19 September 2025.

**Which products and versions are affected?** keras (PyPI) >= 3.0.0, < 3.11.3, ECOSYSTEM:introduced=3.0.0,fixed=3.11.3.

**Is there a fix for CVE-2025-9905?** Yes. keras (PyPI): upgrade to 3.11.3 or later. Junglewise finds every place you run it, drafts the upgrade and applies it once you approve.

**How is CVE-2025-9905 attacked?** It is attacked with local access, in a software library.

**Am I affected by CVE-2025-9905?** Check whether you run keras (PyPI) at an affected version. Junglewise does that check for you around the clock: connect your code, cloud and domains, and it matches new vulnerabilities like this one against what you actually run and tells you when one reaches you.

## How to fix

- keras (PyPI): upgrade to 3.11.3 or later

  ```sh
  pip install "keras>=3.11.3"
  ```


## Technical details

A vulnerability exists in Keras's `Model.load_model` API due to improper handling of the `safe_mode` parameter for legacy file formats. While `safe_mode=True` is intended to disallow unsafe lambda deserialization, the implementation in `keras/src/saving/saving_api.py` delegates .h5/.hdf5 loading to `legacy_h5_format.load_model_from_hdf5`, which does not implement or check the `safe_mode` flag. An attacker can craft a malicious HDF5 file containing a Lambda layer with an arbitrary Python payload. When a victim loads this file, the payload is executed during deserialization regardless of the `safe_mode` setting. This is fixed in version 3.11.3 by ensuring the security parameter is respected or an error is raised.

## Affected products

- keras-team keras >= 3.0.0, < 3.11.3

## Identifiers

- CVE-2025-9905
- GHSA-36rr-ww3j-vrjv
- PYSEC-2025-123

## Timeline

- 2025-09-19: disclosed
- 2025-09-19: advisory
- 3.11.3: patched

## References

- [https://api.github.com/users/io-no](https://api.github.com/users/io-no)
- [https://github.com/io-no](https://github.com/io-no)
- [https://api.github.com/users/io-no/gists%7B/gist_id%7D](https://api.github.com/users/io-no/gists%7B/gist_id%7D)
- [https://api.github.com/users/io-no/repos](https://api.github.com/users/io-no/repos)
- [https://avatars.githubusercontent.com/u/58036457?v=4](https://avatars.githubusercontent.com/u/58036457?v=4)
- [https://api.github.com/users/io-no/events%7B/privacy%7D](https://api.github.com/users/io-no/events%7B/privacy%7D)

## Related threats

- [Keras model loading denial of service via HDF5 shape bombs](https://junglewise.ai/threats/cve-2026-12570-keras-model-loading-denial-of-service-via-hdf5-shape-bombs)
- [Keras HDF5 arbitrary file disclosure via ExternalLinks](https://junglewise.ai/threats/cve-2026-9335-keras-hdf5-arbitrary-file-disclosure-via-externallinks)
- [Keras unsafe deserialization in TorchModuleWrapper](https://junglewise.ai/threats/cve-2026-12484-keras-unsafe-deserialization-in-torchmodulewrapper)
- [Keras path traversal via symlink in filter_safe_tarinfos](https://junglewise.ai/threats/cve-2026-12482-keras-path-traversal-via-symlink-in-filter-safe-tarinfos)
- [Keras Team Keras arbitrary code execution in Lambda layer](https://junglewise.ai/threats/cve-2026-12481-keras-team-keras-arbitrary-code-execution-in-lambda-layer)
