Skip to content

Preserve PERSID and BINPERSID persistent_load calls via new_variable() - #333

Open
professor-moody wants to merge 2 commits into
trailofbits:masterfrom
professor-moody:fix/persid-and-newobj-ex-ast
Open

professor-moody wants to merge 2 commits into
trailofbits:masterfrom
professor-moody:fix/persid-and-newobj-ex-ast

Conversation

@professor-moody

@professor-moody professor-moody commented Oct 18, 2026 •

Copy link
Copy Markdown

Review follow-up for #333, applying the two suggestions on PersId.run and the same change to BinPersId.run, which the review notes has the same pre-existing flaw.

PersId.run and BinPersId.run used to push the UNPICKLER.persistent_load(<pid>) call straight onto the interpreter stack. A later POP then dropped the call from the AST, so PERSID, POP, NONE, STOP (and the BINPERSID equivalent) unparsed to result = None, although a custom unpickler still runs the callback. Memo reuse could instead repeat the call.

Both calls are now bound to a variable with new_variable() and the variable name is pushed, as NEWOBJ_EX already does.

Tests added to test/test_persid_newobjex.py:

  • test_persid_call_survives_pop and test_binpersid_call_survives_pop: the opcode, POP, NONE, STOP sequence keeps exactly one UNPICKLER.persistent_load('abc') in the unparsed AST;
  • test_persid_call_is_not_repeated_by_memo_reuse and test_binpersid_call_is_not_repeated_by_memo_reuse: a memoised result referenced twice produces one persistent_load( call;
  • test_persid_valid_single_use_keeps_one_call and test_binpersid_valid_single_use_keeps_one_call: the plain opcode, STOP shape still yields one call and check_safety returns a severity.

Verification (Python 3.12.15, the whole test/ directory, no filter): with only the new tests applied to the current PR head, the four POP and memo-reuse tests fail (151 passed); with the source change, 155 passed with no skips. On current master merged with this branch, 161 passed. ruff format and ruff check are clean on both changed files.

AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.

@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ESultanik
ESultanik self-requested a review November 6, 2026 00:05

@ESultanik ESultanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a couple issues, I added suggestions that I think resolve them.

Comment thread fickling/fickle.py Outdated
name = "PERSID"

def run(self, interpreter: Interpreter):
interpreter.stack.append(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
interpreter.stack.append(
var_name = interpreter.new_variable(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A PERSID → POP → NONE → STOP sequence receives LIKELY_SAFE with only result = None in its AST, although a custom unpickler executes the callback. Memo reuse can instead execute it repeatedly in decompiled code. We should preserve the call through new_variable(), as NEWOBJ_EX already does. BINPERSID has the same pre-existing flaw; this PR duplicates it for newly supported PERSID.

Comment thread fickling/fickle.py
[],
)
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
interpreter.stack.append(ast.Name(var_name, ast.Load()))

PersId.run and BinPersId.run pushed the persistent_load call straight
onto the interpreter stack, so a later POP dropped it from the AST.
PERSID, POP, NONE, STOP (and the BINPERSID equivalent) unparsed to
"result = None", although a custom unpickler still runs the callback.
Memo reuse could instead repeat the call.

Bind each call to a variable with new_variable(), as NEWOBJ_EX does, and
push the variable name. Add a POP, a memo-reuse and a benign single-use
test for each of PERSID and BINPERSID.

AI assistance was used in drafting and validating this change; the patch
and tests were reviewed and verified by the author.
@professor-moody professor-moody changed the title Analyse PERSID like BINPERSID and build a valid AST for NEWOBJ_EX Preserve PERSID and BINPERSID persistent_load calls via new_variable() Nov 7, 2026
@professor-moody

Copy link
Copy Markdown
Author

Thanks @ESultanik! Updated PERSID and BINPERSID in b65d99467d19 to preserve persistent_load calls via interpreter.new_variable(...).

This branch has not been deployed

No deployments
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.

3 participants