Repository navigation
fix: check names a pickle fetches or builds at load time - #173
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| constructor, before = inner | ||
| return _Opaque(wraps=(constructor, before + tuple(args[1:]))) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| if isinstance(given, _Opaque) and given.items is not None: | ||
| return _Opaque(wraps=(inner[0], inner[1] + given.items)) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
Three routes let a pickle reach a sink without importing it by name. Each is now CRITICAL, with or without
--strict.getattr.getattr(posixpath.os, "system")isposixpath.os.system. The result is still treated as an import, so chainedgetattrcalls are followed.__globals__or__subclasses__.getattr(obj, name), stay clean whatever they're called.UNRESOLVED_IMPORT: an import whose module or name is the result of a call during the load, such as a string decoded withcodecs, and such a name given togetattr,methodcallerorattrgetter. Names written as literals or fetched from the memo, as joblib writes them, are unaffected.functools.partial. Aroundmethodcaller,attrgetterorgetattr, it's judged by the arguments it's given. That includes arguments added when it's called and those Python restores throughBUILDwhen it unpickles a partial.Design notes:
getattris not added toDUAL_USE_CONSTRUCTORS: its name is the second argument, and Python writes it for every bound method. It gets its own table,ATTRIBUTE_GETTERS.Checks:
bypass-scorecard --check, and the CI scan steps ondemo_dataand the repo, with--strict, VEX and SPDX 3.0 output.pickleto confirm they reach the attribute.main.getattrby name alone flagged 5,438; the design above replaces it.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.