Skip to content

fix: check names a pickle fetches or builds at load time - #173

Merged
lab700xdev merged 2 commits into
mainfrom
judge-getattr-partial-unresolved
Dec 4, 2026
Merged

lab700xdev merged 2 commits into
mainfrom
judge-getattr-partial-unresolved

Conversation

@lab700xdev

@lab700xdev lab700xdev commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Three routes let a pickle reach a sink without importing it by name. Each is now CRITICAL, with or without --strict.

  1. getattr.
    • On something imported, it's judged as the dotted name it amounts to: getattr(posixpath.os, "system") is posixpath.os.system. The result is still treated as an import, so chained getattr calls are followed.
    • On anything else, only a name that reaches a namespace counts, such as __globals__ or __subclasses__.
    • Bound methods, which Python pickles as getattr(obj, name), stay clean whatever they're called.
  2. Names computed at load time. These are reported as UNRESOLVED_IMPORT: an import whose module or name is the result of a call during the load, such as a string decoded with codecs, and such a name given to getattr, methodcaller or attrgetter. Names written as literals or fetched from the memo, as joblib writes them, are unaffected.
  3. functools.partial. Around methodcaller, attrgetter or getattr, it's judged by the arguments it's given. That includes arguments added when it's called and those Python restores through BUILD when it unpickles a partial.

Design notes:

  • getattr is not added to DUAL_USE_CONSTRUCTORS: its name is the second argument, and Python writes it for every bound method. It gets its own table, ATTRIBUTE_GETTERS.
  • The simulated stack now marks values produced by calls, keeps the contents of short tuples, and carries a partial's wrapped constructor forward to its call.

Checks:

  • Full suite (8,635 tests) at 96% coverage, bypass-scorecard --check, and the CI scan steps on demo_data and the repo, with --strict, VEX and SPDX 3.0 output.
  • Harmless versions of all 14 attack shapes in the new tests were loaded with Python's own pickle to confirm they reach the attribute.
  • Real files: 2,738 file-and-mode results from local trees are identical to main.
  • Standard-library pickles:
    • 211,062 pickles of standard-library objects, bound methods and partials, at every protocol.
    • The new rules flag 6 of them in each mode, all names that reach a namespace.
    • A first version that judged getattr by name alone flagged 5,438; the design above replaces it.
  • 400,000 random opcode streams: no finding lost against main.

Costs: the large ordinary pickles we timed take up to about 11% longer to scan.

Not for release yet: CLI releases are on hold, so this lands under Unreleased.

Three routes reached a sink without the file importing it by name:

- getattr on an import is judged as the dotted name it amounts to, and
  on anything else by whether the name reaches a namespace. Bound
  methods, which Python pickles with getattr, stay clean.
- An import, or a name given to getattr or a dual-use constructor, that
  is the result of a call during the load is reported as
  UNRESOLVED_IMPORT.
- functools.partial around methodcaller, attrgetter or getattr is judged
  by the arguments it is given, including those restored by BUILD.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-02T04:33:39.320421Z a6ae4d0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6ae4d03f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread aisbom/safety.py Outdated
Comment on lines +820 to +821
constructor, before = inner
return _Opaque(wraps=(constructor, before + tuple(args[1:])))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Bound the accumulated arguments retained for nested partials

When a pickle repeatedly constructs partial(previous_partial, None) and retains the preceding results on the stack or in the memo, this concatenation copies an increasingly long argument tuple into every wrapper. The new simulation therefore consumes quadratic memory and time on attacker-controlled input, despite _KEPT_TUPLE limiting individual argument tuples. A valid 36 KB pickle containing 4,000 such constructions used about 62 MB during scanning, versus roughly 40 KB in the parent implementation; larger small files can exhaust the scanner's memory. Bound or structurally share the accumulated arguments, reporting incomplete analysis if a tracking limit is reached.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and fixed in f3219fe. A partial that wraps nothing worth judging now keeps no arguments at all. The rest keep at most eight, and a call past that adds PICKLE_SCAN_INCOMPLETE rather than being judged or passed. A new test builds 4,000 nested partials, each kept in the memo: it stays under 8 MB and 5 s of CPU, and reports the call as not fully scanned.

Comment thread aisbom/safety.py Outdated
Comment on lines +849 to +850
if isinstance(given, _Opaque) and given.items is not None:
return _Opaque(wraps=(inner[0], inner[1] + given.items))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Update the tracked partial in place when applying BUILD

If a partial is memoized before BUILD, as Python's pickler normally does, returning a new _Opaque updates only the stack value and leaves the memo pointing to the partial's old function and arguments. A subsequent BINGET therefore bypasses the new wrapped-call checks: memoize partial(getattr), restore its arguments to (posixpath.os, 'system') with BUILD, then retrieve and invoke the memoized partial. I verified that this returns no findings in either mode while Python executes the resulting os.system call (mocked during verification). Mutate the existing tracked wrapper so all memo and stack aliases observe the restored state.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and fixed in f3219fe. BUILD now changes the tracked partial in place, so a copy fetched from the memo sees the restored function and arguments. Your shape (memoize partial(getattr), restore (posixpath.os, 'system') with BUILD, fetch, call) is a regression test in both modes. A harmless twin is loaded with Python's pickle to confirm it reaches the attribute. That also showed an empty argument tuple was being read as unknown, which is fixed too.

- BUILD now changes the tracked partial in place, so a copy fetched from
  the memo sees the restored function and arguments.
- A partial of nothing worth judging keeps no arguments, and the rest
  keep at most eight. A call past that is reported as not fully
  scanned, instead of nested partials copying ever longer tuples.
- An empty tuple counts as a known, empty argument list.
@lab700xdev
lab700xdev merged commit 5e1840b into main Dec 4, 2026
2 checks passed
@lab700xdev
lab700xdev deleted the judge-getattr-partial-unresolved branch December 4, 2026 03:16
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.

1 participant