-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Pickle BINSTRING
incorrect data type for size
#135321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
serhiy-storchaka
added a commit
that referenced
this issue
Jun 11, 2025
…0x7fffffff in pickle (GH-135322) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Jun 11, 2025
…ent > 0x7fffffff in pickle (pythonGH-135322) (cherry picked from commit 2b8b4774d29a707330d463f226630185cbd3ceff) Co-authored-by: Justin Applegate <70449145+Legoclones@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Jun 11, 2025
…ent > 0x7fffffff in pickle (pythonGH-135322) (cherry picked from commit 2b8b477) Co-authored-by: Justin Applegate <70449145+Legoclones@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
The
BINSTRING
opcode in Pickle has two arguments: a "4-byte little-endian signed int" length, and a string of that many bytes (see the code comment forBINSTRING
inpickletools
). Since it is signed, this means that any provided value over0x7fffffff
would be interpreted as a negative number. The Python pickle implementation specifically treats it as a signed 32-bit length and checks to see if the length is < 0:cpython/Lib/pickle.py
Lines 1454 to 1458 in 754e7c9
However, the C pickle implementation runs
calc_binsize(s, 4)
forBINSTRING
and returns aPy_ssize_t
. SincePy_ssize_t
data types are the same size as the compiler'ssize_t
type (PEP0353), this means aPy_ssize_t
is 64-bits long on 64-bit systems. Since thesize
variable here is also aPy_ssize_t
, that means the threshold for negative values is much higher.cpython/Modules/_pickle.c
Lines 5546 to 5558 in a58026a
This is all just the background to illustrate that because
size
is not anint
, a pickle with theBINSTRING
opcode using a length > 0x7fffffff will fail in the Python implementation (since it's negative), but deserialize just fine in the C implementation.The following payload will demonstrate the difference:
The required payload does need to be 2GB in size which is very large, but not impossible on modern systems.
Note that the
LONG4
opcode is in a similar situation, except the output forcalc_binint()
is anint
data type so this issue does not exist there.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
size
variable forBINSTRING
in_pickle
#135322The text was updated successfully, but these errors were encountered: