fix(picklescanner): handle GET/BINGET to missing memo without aborting scan#348
Open
palios-taey wants to merge 1 commit into
Open
fix(picklescanner): handle GET/BINGET to missing memo without aborting scan#348palios-taey wants to merge 1 commit into
palios-taey wants to merge 1 commit into
Conversation
…g scan
PickleUnsafeOpScan currently raises KeyError when STACK_GLOBAL analysis
encounters a GET/BINGET/LONG_BINGET opcode referencing a memo index that
was never PUT. The unhandled exception propagates up and the scanner
returns zero issues for the whole file, even when unsafe globals appear
earlier in the byte stream.
This is one instance of the EDP (Exception-Directed Programming) class
discussed in arXiv:2508.19774 ("Hide and Seek"; GHSA-9gvj-pp9x-gcfr) and
the "Invalid Memo Reference" category in protectai#338: a malformed-but-parseable
pickle whose only function is to crash the scanner before the malicious
payload is reported.
Fix: when GET/BINGET references a missing memo index, treat the resolved
value as "unknown" (matching the existing convention at the next branch
of the same conditional for unrecognized opcodes) and continue scanning
the remainder of the stream.
Test: tests/test_modelscan.py::test_scan_picklescanner_resilient_to_invalid_memo_binget
generates a minimal pickle that calls os.system via GLOBAL+REDUCE and
follows it with STACK_GLOBAL referencing memo indices 7 and 8 (never
PUT). Before this fix, modelscan returns 0 issues. After, it reports
the os.system CRITICAL.
Refs protectai#338.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
PickleUnsafeOpScanwalks a pickle stream and encounters aSTACK_GLOBALwhose operands resolve viaGET/BINGET/LONG_BINGETinto a memo index that was neverPUT, the unhandledKeyErrorpropagates and the scanner returns no issues for the whole file — even when unsafe globals were already seen earlier in the byte stream.This PR adds the missing handling: when a memo reference is missing, treat the resolved value as
"unknown"(the same convention already used at the next branch of the same conditional for unrecognized opcodes) and continue scanning. A regression test reproduces the case.Refs #338 (Invalid Memo Reference category).
Why
This pattern is one instance of the EDP (Exception-Directed Programming) class described in arXiv:2508.19774 and credited via GHSA-9gvj-pp9x-gcfr: a malformed-but-parseable pickle whose sole purpose is to crash the scanner before the malicious payload is reported. Without this fix, a CI gate using
modelscanlets affected files through silently — the JSON output shows"total_issues": 0, and the only signal is a one-charactererrors[].descriptionfield (the missing memo key as a bare string) which most consumers ignore.The fix is the minimal, locally-correct change: a single conditional inside the existing
STACK_GLOBALoperand-resolution loop. It does not alter behavior for well-formed pickles. It does not bias toward any particular architectural direction (denylist expansion vs allowlist) — the test asserts only that unsafe globals appearing before the malformed reference are still reported.Testing
Added
tests/test_modelscan.py::test_scan_picklescanner_resilient_to_invalid_memo_binget. The test generator (edp_invalid_memo_binget_gen) constructs a minimal pickle:GLOBAL+REDUCEinvokingos.system("echo pwned")(benign demo command)BINGETopcodes referencing memo indices 7 and 8 (neverPUT)STACK_GLOBAL(would consume those references)STOPVerified locally:
len(ms.issues.all_issues) == 0— theos.systemglobal is silently dropped.len(ms.issues.all_issues) == 2— theos.systemCRITICAL is reported, plus anunknown/unknownCRITICAL from the malformedSTACK_GLOBALitself.The fixture is self-contained, runs offline (no network fetch), and contains no exploitation primitives beyond what is already public via the Hide-and-Seek paper, GHSA-9gvj-pp9x-gcfr, and the corresponding artifact repo.