Skip to content

[9.4] Harden model validation: reject __setstate__ hooks and forbid inductor::_reinterpret_tensor (#3078) - #3123

Merged
github-actions[bot] merged 1 commit into
9.4from
backport/9.4/pr-3078
Jul 27, 2026
Merged

[9.4] Harden model validation: reject __setstate__ hooks and forbid inductor::_reinterpret_tensor (#3078)#3123
github-actions[bot] merged 1 commit into
9.4from
backport/9.4/pr-3078

Conversation

@elastic-vault-github-plugin-prod

Copy link
Copy Markdown
Contributor

Backport

This will backport the following commits from main to 9.4:

Questions ?

Please refer to the Backport tool documentation

…r::_reinterpret_tensor (#3078)

TorchScript runs a module's __setstate__ during torch::jit::load(), before the
loaded module reaches CModelGraphValidator (which only walks the inlined forward
graph). A forbidden op hidden in __setstate__ is therefore invisible to the
validator and has already executed by the time validation would run.

Add fixtures and a self-contained repro that demonstrate the gap:
- generate_malicious_models.py: add SetStateFileReaderModel and a submodule
  variant (aten::from_file in __setstate__, benign forward); surface
  __setstate__ ops in the generator output.
- test/test_setstate_load_timing.py: torch-only, build-free repro proving
  load-time execution (benign print probe) and op-hiding (static inspection).
- test_pytorch_inference_evil_models.py: register the fixtures as regression
  guards for the forthcoming pre-load scan.

Close the load-time execution gap: torch::jit::load runs a module's
__setstate__ during deserialization, before CModelGraphValidator (which walks
an already-loaded module's graph) can run. A forbidden op hidden in
__setstate__ therefore executes at load time and never appears in the forward
graph the validator inspects.

Add CModelGraphValidator::scanSerialisedCodeForForbiddenOps, which uses a
PyTorchStreamReader over the buffered archive bytes to textually scan the
serialised TorchScript code (code/**/*.py, including every submodule's
__setstate__) for the forbidden aten ops, WITHOUT loading the model. Main.cc
runs this scan before torch::jit::load and rejects via HANDLE_FATAL, so no
model code executes. Expose the buffered bytes via CBufferedIStreamAdapter::buffer().

This is a defense-in-depth textual check targeted at the curated forbidden set
(from_file, as_strided, save); the post-load allowlist validation is unchanged,
and seccomp remains the syscall backstop. Unit tests cover both setstate
fixtures, forward-graph attacks, a benign model (no false positive), and
malformed input; the binary integration test now expects the setstate models to
be rejected with the forbidden-operations message.

(cherry picked from commit 09ea7b5)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated approval: clean backport of an already-reviewed change. Auto-merge is armed and will merge once the required CI checks are green.

@github-actions
github-actions Bot enabled auto-merge (squash) July 27, 2026 04:10
@github-actions
github-actions Bot merged commit f95b794 into 9.4 Jul 27, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically merge backport PRs when CI passes backport >bug ci:skip-es-tests :ml v9.4.5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants