Skip to content

Tolerate a missing _cloudpickle_submodules key in _function_setstate - #596

Closed
soodoku wants to merge 2 commits into
cloudpipe:masterfrom
gojiplus:fix/593-function-setstate-missing-submodules
Closed

soodoku wants to merge 2 commits into
cloudpipe:masterfrom
gojiplus:fix/593-function-setstate-missing-submodules

Conversation

@soodoku

@soodoku soodoku commented Jul 27, 2026 •

Copy link
Copy Markdown

Fixes #593.

_function_setstate pops _cloudpickle_submodules from the slotstate without a default, so a payload that does not carry that key raises KeyError: '_cloudpickle_submodules' from inside pickle.loads, rather than a normal unpickling failure.

This restores handling that used to be there. Before c6f8cd4 the code read:

if '_cloudpickle_submodules' in state:
    state.pop('_cloudpickle_submodules')

That commit ("code clean-up after EOLed Python 3.6 and 3.7") consolidated the module and the surviving version pops unconditionally. Valid cloudpickle payloads always set the key, so this changes nothing for them — it only affects payloads that omit it.

I've deliberately left the issue's CVE framing aside: pickle.loads on untrusted input already permits arbitrary code execution, so I don't think a KeyError there is a security boundary. The narrow case for this change is just that a malformed payload should not crash with an opaque KeyError from the middle of a pop.

If you'd rather have this raise an explicit pickle.UnpicklingError with a clear message than tolerate the omission, that's a one-line change and I'm happy to switch.

Scope of the public API

On current upstream, cloudpickle.load is pickle.load and cloudpickle.loads is pickle.loads. Standard-library round trips of a dictionary and an importable function both pass before this change. The proposed change only concerns restoration of dynamic function state that omits the submodule list. A test using ordinary pickle.dump would already pass on the base branch.

Verification

Run against a Python 3.12 venv (dev-requirements.txt pins numpy to python_version <= '3.12', so 3.12 exercises more of the suite than 3.14 would):

  • Baseline on unmodified master: 266 passed, 10 skipped, 2 xfailed
  • With this change: 267 passed, 10 skipped, 2 xfailed

The only delta is the new test. It fails on master with KeyError: '_cloudpickle_submodules' at cloudpickle/cloudpickle.py:1156 and passes with the fix.

black 25.9.0 (the version pinned in .pre-commit-config.yaml) reports both touched files unchanged. ruff check cloudpickle tests reports the same 81 pre-existing findings before and after, none on the touched lines.

The changelog entry links the issue since the PR number isn't known yet — happy to swap it for the PR link.

_function_setstate pops "_cloudpickle_submodules" from the slotstate without
a default, so a payload that does not carry that key raises an opaque
KeyError from inside pickle.loads rather than a normal unpickling failure.

Before c6f8cd4 the code guarded this:

    if '_cloudpickle_submodules' in state:
        state.pop('_cloudpickle_submodules')

That commit consolidated the module and the surviving version pops
unconditionally. Restore the tolerance by passing a default. Valid
cloudpickle payloads always set the key, so this is a no-op for them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ogrisel

ogrisel commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

If I understand the problem correctly, this fix is about making cloudpickle.load able to load any valid pickle payload generated by pickle.dump instead of cloudpickle.dump.

If that's the case, I would rather use a non-regression test that test this explicitly (using only public API) and update the changelog entry accordingly.

@soodoku

soodoku commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks for taking a look, @ogrisel, and sorry for the slow follow-up. Your question made me check the premise more carefully, and I think you're right to be skeptical: a payload written by pickle.dump never reaches _function_setstate (the standard pickler can't serialize a dynamic function, and module-level functions are pickled by reference), so I couldn't write the public-API test you suggested. The only way to hit this line is a hand-built payload. I've updated the changelog entry to say that plainly (701362e). Since that's the case you described in #593 as out of scope, I completely understand if you'd rather not take this. I'm happy to close it, or to keep it as a small robustness tweak if you see value in restoring the old guard.

@ogrisel ogrisel closed this Dec 3, 2026
@ogrisel

ogrisel commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for the followup. I don't see the pount of the original issue.

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.

Bug: unguarded dict.pop() in _function_setstate causes KeyError crash on crafted pickle (regression from c6f8cd4)

2 participants