Skip to content

Fix stack exhaustion DoS in json parsing via iterative shape traversal#4138

Open
deezsecc wants to merge 1 commit into
tensorflow:masterfrom
deezsecc:fix-json-stack-exhaustion
Open

Fix stack exhaustion DoS in json parsing via iterative shape traversal#4138
deezsecc wants to merge 1 commit into
tensorflow:masterfrom
deezsecc:fix-json-stack-exhaustion

Conversation

@deezsecc
Copy link
Copy Markdown

Description

This PR fixes a stack exhaustion / Denial of Service (DoS) vulnerability in the TensorFlow Serving REST API JSON parsing logic.


Root Cause

When parsing incoming model prediction requests, rapidjson processes the request document iteratively. However, the post-parsing step in GetDenseTensorShape() (inside tensorflow_serving/util/json_tensor.cc) recursively traversed nested arrays (val[0]) to compute the tensor's shape without enforcing a recursion depth guard or limit.

An unauthenticated attacker could send a payload with a deeply nested array structure (e.g., 50,000 nested layers like [[[[... [1] ...]]]]), causing a stack overflow (SIGSEGV) and instantly crashing the tensorflow_model_server process.

A secondary recursive execution vector also existed in FillTensorProto(), which recursively parses nested elements up to the tensor's rank without validating that the rank is within safe limits.


Fix

This patch mitigates the vulnerability on both vectors:

Vector Fix
Iterative Traversal Refactored GetDenseTensorShape() to be fully flat and iterative, using a loop and pointer to resolve shape — eliminating recursion at the shape-calculation level
Rank Capping Added a hard limit (kMaxTensorRank = 254, aligned with TensorFlow's TensorShape::MaxDimensions()) inside GetDenseTensorShape() to stop adding dimensions beyond safe nesting depth
Early Rejection Added an early check in FillTensorProto() to reject payloads with rank > 254 via errors::InvalidArgument, preventing deep recursion on the value-filling path

Verification

  • Iterative while loop behaves identically to the original recursion on standard payloads
  • Deeply nested structures are capped early and rejected via FillTensorProto propagating InvalidArgument instead of exhausting the thread stack

@deezsecc deezsecc force-pushed the fix-json-stack-exhaustion branch from 8ff63b3 to 3eee01a Compare May 28, 2026 09:15
@deezsecc
Copy link
Copy Markdown
Author

deezsecc commented May 28, 2026

@ch1nlu @rtg0795 Please review the PR. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants