Skip to main content
Back

Politecnico di Milano Breaks Keras safe_mode With Six Hidden Code Execution Exploits

Researchers found six zero-day vulnerabilities in Keras and Skops that let attackers run arbitrary code even when 'safe mode' loaders are enabled.

Pro

  • Politecnico di Milano researchers disclosed six 0-day CVEs in Keras and Skops model loaders, all enabling arbitrary code execution.
  • Includes the first CVEs ever filed against Keras safe_mode: CVE-2025-1550, CVE-2025-8747, CVE-2025-9905, CVE-2025-9906.
  • Skops flaws (CVE-2025-54412, 54413, 54886) bypass its trusted-types allowlist via MethodNode traversal and joblib fallback.
  • Hugging Face's integrated scanners labeled two working Skops exploits as "Safe" because no scanner supports the format.
  • Survey of 62 ML practitioners: enabling safe_mode dropped code-execution concern from 83% to 15%, showing misplaced trust in labels.
  • Proof-of-concept exploits, survey data, and fixes are in the necst/security-model-sharing repo; upgrade to Keras 3.11.3 and Skops 0.13.0.

Loading a pretrained model from a hub feels as harmless as importing a package. A team from Politecnico di Milano has just demonstrated that this intuition is dangerously wrong, even when you use the loaders that frameworks and hubs explicitly market as secure. Their systematic study of ML model sharing uncovered six previously unknown vulnerabilities, all leading to arbitrary code execution, and produced the first CVEs ever assigned against Keras's safe_mode.

The work is broader than a bug hunt. It audits how the five most popular frameworks and the five most popular hubs actually handle model persistence, then measures how much users trust the resulting security labels. The gap between the two is the real story.

Why loading a model is like running a script

The threat model is straightforward: an attacker crafts a malicious model artifact to compromise the victim's system upon loading, with the primary goal of arbitrary code execution. This is not theoretical. Recent measurements found 91 malicious models and several poisoned dataset scripts on Hugging Face over a three-month monitoring period, and Protect AI has scanned over 4.47 million model versions across 1.41 million repositories, flagging 352,000 unsafe or suspicious issues in 51,700 models.

Historically the finger has been pointed at Python's pickle, which happily deserializes arbitrary bytecode. The industry response has been to move toward data-based formats like JSON, protobuf, or ONNX, wrapped in loaders with reassuring names: Keras's safe_mode=True, PyTorch's weights_only=True, and Hugging Face's Skops library for scikit-learn. The paper's central claim is that the file format alone does not determine the security of model sharing, and JSON-based loaders end up behaving much like code-based ones because they translate JSON descriptions into live Python objects.

Breaking Keras safe_mode three different ways

The authors reverse-engineered the current Keras and Skops codebases and chained together three separate exploits against Keras alone.

  • KV.1 - Insecure module resolution (CVE-2025-1550, CVSS 7.3). A crafted config.json inside a .keras archive can specify insecure Python modules and functions to be imported and executed during loading; for example, subprocess.run can be interpreted as a model layer. Keras only checked that the referenced symbol was a Python function, so an attacker could invoke subprocess.run("/bin/sh") as if it were a layer instantiation.
  • KV.2 - Code reuse via Lambda layers (CVE-2025-8747 and CVE-2025-9906, CVSS 8.6). After the KV.1 fix introduced an allowlist of trusted Keras modules, the authors bypassed it by calling legitimate internal Keras utilities. Their proof of concept crafts a model that disables safe mode during loading, even if it was initially enabled by the user, by invoking an internal Keras utility, then chains other internals to achieve full code execution.
  • KV.3 - Silent bypass via legacy HDF5 (CVE-2025-9905, CVSS 7.3). When an HDF5 model is loaded using load_model(..., safe_mode=True), the safe_mode flag is silently ignored, without any warning or error, because the argument is never forwarded to the internal legacy loading routine. The Google Open Source Security Team initially marked this Won't Fix, telling the researchers they "won't treat Keras safe_mode as a security boundary" anymore, before the Keras maintainers fixed it in version 3.11.3.

Skops has the same disease

Skops is Hugging Face's JSON-schema alternative to pickle for scikit-learn, designed around a user-facing allowlist workflow. The team found three matching flaws.

  • SV.1 - MethodNode abuse (CVE-2025-54413, CVSS 8.7). An attacker can chain multiple MethodNode entries to traverse the object graph of a legitimate object and reach runtime structures such as __builtins__, which exposes dangerous functions like eval and exec. The trick works because the validation step and the actual load step check different fields.
  • SV.2 - OperatorFuncNode validation bypass (CVE-2025-54412, CVSS 8.7). The __module__ value is validated but then ignored, and only __class__ is used, so setting __class__ to "call" causes operator.call to be invoked regardless of the seemingly benign module path.
  • SV.3 - Silent joblib fallback (CVE-2025-54886, CVSS 8.4). Skops's model card API detects file type by content, not extension, and quietly falls back to joblib.load() when the file is not a valid ZIP, reintroducing pickle-based code execution.

The hub scanners do not save you

The authors then uploaded each proof of concept to Hugging Face, which layers Picklescan, ClamAV, Protect AI, and JFrog on top of every file. The results are unflattering.

ExploitFinal Hugging Face label
KV.1 (.keras)Unsafe
KV.2 (.keras)No label (JFrog stuck in Queued)
KV.3 (HDF5)Unsafe
SV.1 (.skops)Safe
SV.2 (.skops)Safe
SV.3 (pickle)Unsafe

None of the scanners support Skops models, so the final label for both SV.1 and SV.2 was "Safe." This represents a clear false negative and is particularly problematic, as a reassuring label was assigned despite the absence of compatible scanners. Scanners also over-fire on benign Lambda layers, which the authors warn will train users to ignore alerts.

Users trust the label, not the code

To close the loop, the team ran a survey of 62 practitioners, asking them to rate their comfort loading models with different flags set. Average comfort levels grew from 4.5/10 with safe_mode=False to 7.8/10 with safe_mode=True, and the number of participants expressing concerns about arbitrary code execution decreased from 44 (83%) to 8 (15.1%). The Wilcoxon signed-rank test confirmed the effect is real and large. In other words, a boolean flag named "safe" is doing most of the security work in people's heads.

What to actually do about it

The paper is unusually blunt for an academic venue, quoting the Google Security Team directly: "Basically, a truly safe_mode in Python for this isn't really possible. Loading untrusted models is like running untrusted code, models are code." The authors distill their advice into a few practical rules.

  1. Treat any shared model as untrusted code, regardless of whether it lives in a JSON, HDF5, or pickle file.
  2. Prefer constrained formats like ONNX for inference where you can afford to lose flexibility, since a limited operator set genuinely reduces the attack surface.
  3. Do not rely on hub-level scanners as a security boundary. They are signature-based, framework-specific, and have gaps you cannot see from the label.
  4. Pin new framework versions aggressively. Keras 2.x releases from 2023 still outdraw most 3.x releases on PyPI, meaning legacy insecure paths remain live for most of the ecosystem.

All proofs of concept, survey data, and analysis scripts are available in the necst/security-model-sharing repository, and the disclosed issues are fixed in Keras 3.11.3, Skops 0.12.0, and Skops 0.13.0. If your pipeline calls load_model or skops.io.load on artifacts from anywhere but your own storage, those are the minimum versions you want.