Skip to content
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

Pickle crashes unpickling invalid NEWOBJ_EX opcode #85460

Closed
ImanSharafaldin mannequin opened this issue Jul 12, 2020 · 15 comments
Closed

Pickle crashes unpickling invalid NEWOBJ_EX opcode #85460

ImanSharafaldin mannequin opened this issue Jul 12, 2020 · 15 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@ImanSharafaldin
Copy link
Mannequin

ImanSharafaldin mannequin commented Jul 12, 2020

BPO 41288
Nosy @abalkin, @tiran, @ned-deily, @ambv, @serhiy-storchaka, @pganssle, @miss-islington
PRs
  • bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. #21458
  • [3.9] bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) #21459
  • [3.8] bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) #21460
  • [3.7] bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) #21461
  • [3.6] bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) #21462
  • bpo-41288: Refactor of unpickling NEWOBJ and NEWOBJ_EX opcodes. #21472
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2020-07-26.14:19:49.073>
    created_at = <Date 2020-07-12.21:36:09.137>
    labels = ['3.8', '3.9', '3.10', 'extension-modules', '3.7', 'type-crash']
    title = 'Pickle crashes unpickling invalid NEWOBJ_EX opcode'
    updated_at = <Date 2020-07-26.15:12:33.081>
    user = 'https://bugs.python.org/ImanSharafaldin'

    bugs.python.org fields:

    activity = <Date 2020-07-26.15:12:33.081>
    actor = 'Iman Sharafaldin'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2020-07-26.14:19:49.073>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules']
    creation = <Date 2020-07-12.21:36:09.137>
    creator = 'Iman Sharafaldin'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41288
    keywords = ['patch']
    message_count = 15.0
    messages = ['373573', '373575', '373599', '373600', '373603', '373604', '373605', '373616', '373617', '373886', '374022', '374086', '374311', '374314', '374320']
    nosy_count = 8.0
    nosy_names = ['belopolsky', 'christian.heimes', 'ned.deily', 'lukasz.langa', 'serhiy.storchaka', 'p-ganssle', 'miss-islington', 'Iman Sharafaldin']
    pr_nums = ['21458', '21459', '21460', '21461', '21462', '21472']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue41288'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @ImanSharafaldin
    Copy link
    Mannequin Author

    ImanSharafaldin mannequin commented Jul 12, 2020

    The following code generates a segfault on the Pickle module [it's a crafted datetime object] (Python 3.10.0a0 (heads/master:b40e434, Jul 4 2020), Python 3.6.11 and Python 3.7.2):

    import io
    import pickle
    
    
    hex_string = "8004952A000000000000008C086461746574696D65948C086461746574696D65949388430A07B2010100000000000092059452942E"
    myb = bytes.fromhex(hex_string)
    f = io.BytesIO(myb)
    print(f)
    data = pickle.load(f)
    print(data)
    print('We have segfault but we cannot see!')

    @ImanSharafaldin ImanSharafaldin mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes type-crash A hard crash of the interpreter, possibly with a core dump labels Jul 12, 2020
    @tiran
    Copy link
    Member

    tiran commented Jul 12, 2020

    datetime_new assumes that args is a tuple. load_newobj_ex() doesn't check that args is a tuple and kwargs is a dictionary. The demo exploit passes True as args, which triggers a segfault in PyTuple_GET_SIZE in datetime_new.

    #0 datetime_new (type=0x7fffea5d2740 <PyDateTime_DateTimeType>, args=True, kw=b'\x07\xb2\x01\x01\x00\x00\x00\x00\x00\x00') at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Modules/_datetimemodule.c:4737
    #1 0x00007fffea637b1e in load_newobj_ex (self=0x7fffea7a7820) at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Modules/_pickle.c:6008
    #2 0x00007fffea632e7a in load (self=0x7fffea7a7820) at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Modules/_pickle.c:6943
    #3 0x00007fffea63795e in _pickle_load_impl (module=<optimized out>, buffers=0x0, errors=0x7fffea639149 "strict", encoding=0x7fffea6391fa "ASCII", fix_imports=1, file=<_io.BytesIO at remote 0x7fffea888180>)
    at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Modules/_pickle.c:1688
    #4 _pickle_load (module=<optimized out>, args=<optimized out>, nargs=<optimized out>, kwnames=<optimized out>) at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Modules/clinic/_pickle.c.h:731
    #5 0x00007ffff7bd1ced in cfunction_vectorcall_FASTCALL_KEYWORDS (func=<built-in method load of module object at remote 0x7fffea680270>, args=<optimized out>, nargsf=<optimized out>, kwnames=0x0)
    at /usr/src/debug/python3-3.8.3-2.fc32.x86_64/Objects/methodobject.c:437

    @serhiy-storchaka serhiy-storchaka added extension-modules C modules in the Modules dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jul 12, 2020
    @serhiy-storchaka serhiy-storchaka self-assigned this Jul 12, 2020
    @serhiy-storchaka serhiy-storchaka added extension-modules C modules in the Modules dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jul 12, 2020
    @serhiy-storchaka serhiy-storchaka self-assigned this Jul 12, 2020
    @serhiy-storchaka serhiy-storchaka changed the title Pickle crashes using a crafted datetime object Pickle crashes unpickling invalig NEWOBJ_EX opcode Jul 13, 2020
    @serhiy-storchaka serhiy-storchaka changed the title Pickle crashes using a crafted datetime object Pickle crashes unpickling invalig NEWOBJ_EX opcode Jul 13, 2020
    @tiran tiran changed the title Pickle crashes unpickling invalig NEWOBJ_EX opcode Pickle crashes unpickling invalid NEWOBJ_EX opcode Jul 13, 2020
    @tiran tiran changed the title Pickle crashes unpickling invalig NEWOBJ_EX opcode Pickle crashes unpickling invalid NEWOBJ_EX opcode Jul 13, 2020
    @miss-islington
    Copy link
    Contributor

    New changeset 4f309ab by Serhiy Storchaka in branch 'master':
    bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458)
    4f309ab

    @tiran
    Copy link
    Member

    tiran commented Jul 13, 2020

    Ned, would you like to get this fix backported to 3.6 and 3.7?

    @tiran tiran added 3.8 only security fixes 3.9 only security fixes labels Jul 13, 2020
    @ned-deily
    Copy link
    Member

    Sounds like a good idea.

    @ned-deily ned-deily added 3.7 (EOL) end of life labels Jul 13, 2020
    @miss-islington
    Copy link
    Contributor

    New changeset f56c75e by Miss Islington (bot) in branch '3.8':
    bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458)
    f56c75e

    @miss-islington
    Copy link
    Contributor

    New changeset 57c984f by Miss Islington (bot) in branch '3.9':
    bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458)
    57c984f

    @ned-deily
    Copy link
    Member

    New changeset 620e276 by Miss Islington (bot) in branch '3.7':
    bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) (GH-21461)
    620e276

    @ned-deily
    Copy link
    Member

    New changeset 6463cf0 by Miss Islington (bot) in branch '3.6':
    bpo-41288: Fix a crash in unpickling invalid NEWOBJ_EX. (GH-21458) (GH-21462)
    6463cf0

    @serhiy-storchaka
    Copy link
    Member

    New changeset b4c98ed by Serhiy Storchaka in branch 'master':
    bpo-41288: Refactor of unpickling NEWOBJ and NEWOBJ_EX opcodes. (GH-21472)
    b4c98ed

    @ambv
    Copy link
    Contributor

    ambv commented Jul 20, 2020

    Is this fix causing the refleak build to fail on 3.8? See: https://buildbot.python.org/all/#/builders/484/builds/149

    @serhiy-storchaka
    Copy link
    Member

    This fix touched the unpickling code, and the test crashed during pickling, so it is not directly related. But it may be some compiler glitch, when changing one part of code affects compilation of other parts of code. The next run on the buildbot was passed, and there were failures in the past (but logs were not saved), so it may be just unstable test.

    @ImanSharafaldin
    Copy link
    Mannequin Author

    ImanSharafaldin mannequin commented Jul 26, 2020

    @serhiy.storchaka Hi Serhiy, do you count this issue as a security issue? Also, I didn't see that in the latest changelog. Is it now patched on the latest version?

    https://docs.python.org/release/3.8.5/whatsnew/changelog.html

    @serhiy-storchaka
    Copy link
    Member

    I do not think it is a security issue. The crash cannot be triggered by the user input unless you accept the pickle data from untrusted sources, but in that case you are in large danger, because you allow executing arbitrary code.

    The changes in this issue just help to debug in some cases when you play with pickle format. In any case thank you for your report.

    @ImanSharafaldin
    Copy link
    Mannequin Author

    ImanSharafaldin mannequin commented Jul 26, 2020

    Thank you for patching that.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants