Summary
The TFSMLayer class in Keras 3.13.0 unsafely loads attacker-controlled TensorFlow SavedModels during deserialization of .keras models, even when safe_mode=True is enabled.
By embedding malicious SavedModel paths inside a serialized model, an attacker can achieve arbitrary code execution during model loading or inference under the victim's privileges.
Affected context
This vulnerability affects environments that use Keras 3.13.0 and the TFSMLayer class to load or run .keras models, especially when:
- Models are loaded from external or semi-trusted sources (e.g., shared artifacts, public repositories, model hubs, or user uploads).
- The application relies on
safe_mode=Trueas a security boundary when calling Keras model deserialization APIs. - TensorFlow SavedModels referenced by
TFSMLayerare fetched from file paths or locations that are not tightly controlled or validated.
In such contexts, the mere act of loading a .keras file that includes a TFSMLayer configuration can trigger loading of external SavedModels without proper safety checks.
Exploit preconditions
- Attack surface: Keras model loading and inference pipeline that accepts
.kerasfiles. - Required access: Ability to supply or influence a serialized
.kerasmodel consumed by the victim environment (e.g., upload, file drop, CI artifact injection, or supply-chain compromise). - Trigger point: Deserialization of the
.kerasmodel that contains aTFSMLayerwith attacker-controlled configuration, particularly file paths to external SavedModels. - Why exploitation is possible:
TFSMLayer.from_config()unconditionally loads referenced TensorFlow SavedModels.- Attacker-controlled file paths are serialized into the model configuration and later deserialized without robust validation.
- The
safe_mode=Trueflag does not prevent loading of these external SavedModels, effectively bypassing its intended security guarantees.
Why this happens
The core issue is an unsafe deserialization pattern where serialized configuration data is implicitly trusted and used to load executable model components.
Specifically, the TFSMLayer:
- Persists file system paths to external TensorFlow SavedModels within the
.kerasmodel configuration. - During
from_config(), reads and uses these paths without strong validation or restriction. - Invokes TensorFlow loading mechanisms that can execute attacker-controlled logic when a crafted SavedModel is loaded.
This behavior maps to CWE-502: Deserialization of Untrusted Data, where untrusted serialized content is used to reconstruct complex objects that can run code or execute side effects during or immediately after deserialization. The design incorrectly treats safe_mode=True as sufficient isolation while still allowing arbitrary external model loading governed solely by attacker-controlled configuration.
Potential impact
- Arbitrary code execution during model loading or inference under the victim application's privileges.
- Compromise of the ML serving process, including data access, API keys, or downstream systems.
- Tampering with inference results, leading to data integrity issues or model misuse (e.g., adversarial predictions, fraud, or safety policy bypasses).
- Use of the compromised environment as an entry point for lateral movement within the surrounding infrastructure.
- Execution of additional payloads (e.g., cryptominers, data exfiltration tools) embedded within malicious SavedModels.
General mitigation guidance
Effective mitigation focuses on treating all serialized models and their configurations as untrusted input and minimizing what deserialization is allowed to do by default.
Recommended defensive patterns include:
- Tightening trust boundaries for models:
- Only load models from authenticated, integrity-checked channels (e.g., signed artifacts, controlled registries).
- Avoid loading user-supplied
.kerasmodels directly into production environments. - Restricting external model loading:
- Avoid or restrict use of features that dynamically load external SavedModels based on configuration values.
- Introduce allowlist-based validation for any file paths or URIs used by
TFSMLayer-like components before load-time. - Hardening deserialization behavior:
- Treat
safe_modeas a defense-in-depth feature, not a primary security control; apply additional checks around any deserialization entry points. - Prefer minimal, schema-driven configuration formats that do not implicitly trigger complex object construction.
- Least privilege and sandboxing:
- Run ML inference services with the least filesystem and network privileges necessary.
- Use containerization or sandboxing to limit the blast radius of potential code execution arising from malicious models.
- Operational controls:
- Separate environments for model evaluation and production serving, so untrusted or experimental models are never directly loaded into high-value systems.
How teams detect issues like this
Detection should aim to identify unsafe deserialization paths and unexpected external model loading behaviors.
Possible strategies include:
- Static analysis and code review:
- Search for
TFSMLayerusage and inspect any code paths that deserialize.kerasmodels, especially wherefrom_config()is indirectly invoked. - Flag patterns where file paths or URIs from model configuration are used without validation to load external resources.
- Review reliance on
safe_mode=Trueas a primary security control and document assumptions around model trust. - Configuration and policy enforcement:
- Implement policies that disallow loading models from non-approved locations or from user-controllable directories.
- Enforce checks that block or alert on models whose configuration contains unexpected file paths or references to external SavedModels.
- Runtime monitoring:
- Monitor ML serving processes for unusual filesystem access patterns when loading models (e.g., reading from unexpected directories).
- Use behavioral logging and anomaly detection to identify abnormal model loading flows or sudden changes in inference service behavior.
- Automated tooling:
- Integrate SAST/IAST tools that recognize deserialization entry points and flag untrusted data flows into model loading APIs.

