fix: guard against unbounded tensor allocation in Layer.__call__#22094
fix: guard against unbounded tensor allocation in Layer.__call__#22094HyperPS wants to merge 2 commits intokeras-team:masterfrom
Conversation
Summary of ChangesHello @HyperPS, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a critical security enhancement by introducing a framework-level mechanism to limit tensor memory allocation during Keras layer execution. This prevents malicious or untrusted models from consuming excessive memory, thereby mitigating denial-of-service risks and improving the robustness of Keras in deployment scenarios. The safeguard is configurable and intelligently handles dynamic and symbolic tensors. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a crucial security safeguard against unbounded tensor allocation, which could lead to denial-of-service attacks. The implementation is solid, introducing a configurable limit via an environment variable and performing validation at an appropriate stage in the layer's __call__ method. My only suggestion is to improve the error message to be more informative for the user, in line with the Keras API design guidelines.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #22094 +/- ##
==========================================
- Coverage 82.83% 78.42% -4.41%
==========================================
Files 592 592
Lines 62991 63019 +28
Branches 9886 9895 +9
==========================================
- Hits 52179 49425 -2754
- Misses 8262 11142 +2880
+ Partials 2550 2452 -98
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
hertschuh
left a comment
There was a problem hiding this comment.
There is more to memory allocation than what Layers can see (or what Keras can see for that matter).
Keras is the wrong software layer to apply this:
- Most computations allocate memory, and Keras may only knows about some of them. Only a compiler can have the full picture of the memory allocation. So this is at a lower layer than Keras.
- In terms of serving / inference. First, Keras is typically not how you perform inference. But if you do use it, you need to understand the exact characteristics of your model to know what the maximum input size for your model is. This is something that your inference service would enforce even before calling Keras, it doesn't belong in Keras.
Summary
This PR adds a framework-level safeguard to prevent unbounded tensor allocations during layer execution, mitigating a potential denial-of-service vector when loading or executing untrusted
.kerasmodels.Problem
Keras currently allows model-defined layers to request arbitrarily large output tensors during execution (e.g. via shape-controlled ops such as
tf.image.resize). Because no framework-level limits are enforced, a malicious or untrusted model can trigger excessive memory allocation, leading to process crashes or resource exhaustion.This is particularly relevant in deployment scenarios where
.kerasmodels are fetched from external sources (model hubs, CI pipelines, inference services) and executed automatically.Solution
This change introduces:
KERAS_MAX_TENSOR_ELEMENTSenvironment variable (default:1e8)Layer.__call__that rejects outputs exceeding this limitKey details
KerasTensor) are explicitly excludedSecurity impact
Backward compatibility
Related