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

Multiple use after frees in obj2ast_* methods #68286

Closed
pkt mannequin opened this issue May 1, 2015 · 9 comments
Closed

Multiple use after frees in obj2ast_* methods #68286

pkt mannequin opened this issue May 1, 2015 · 9 comments
Assignees
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@pkt
Copy link
Mannequin

pkt mannequin commented May 1, 2015

BPO 24098
Nosy @tiran, @benjaminp, @berkerpeksag, @serhiy-storchaka
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • poc_obj2mod.py
  • issue24098.patch: patch
  • issue24098-check-size.patch
  • issue24098-iterate-tuple.patch
  • 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 2016-10-07.20:37:29.733>
    created_at = <Date 2015-05-01.14:10:29.629>
    labels = ['extension-modules', '3.7', 'type-crash']
    title = 'Multiple use after frees in obj2ast_* methods'
    updated_at = <Date 2017-03-31.16:36:27.216>
    user = 'https://bugs.python.org/pkt'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:27.216>
    actor = 'dstufft'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-10-07.20:37:29.733>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules']
    creation = <Date 2015-05-01.14:10:29.629>
    creator = 'pkt'
    dependencies = []
    files = ['39249', '44830', '44838', '44839']
    hgrepos = []
    issue_num = 24098
    keywords = ['patch']
    message_count = 9.0
    messages = ['242315', '242750', '242980', '246066', '246147', '277419', '277469', '277504', '278261']
    nosy_count = 7.0
    nosy_names = ['christian.heimes', 'benjamin.peterson', 'Arfrever', 'python-dev', 'berker.peksag', 'serhiy.storchaka', 'pkt']
    pr_nums = ['552']
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue24098'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7']

    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented May 1, 2015

    \bpo-3617 for (i = 0; i < len; i++) {
    # (gdb) print *(PyListObject*)tmp
    # $1 = {ob_base = {ob_base = {ob_next = 0x4056f8f4, _ob_prev = 0x4057329c, ob_refcnt = 2, ob_type = 0x830e1c0 <PyList_Type>},
    # ob_size = 1337}, ob_item = 0x8491ae0, allocated = 1432}
    # (gdb) n
    \bpo-3619 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
    # (gdb) n
    \bpo-3620 if (res != 0) goto failed;
    # (gdb) print *(PyListObject*)tmp
    # $2 = {ob_base = {ob_base = {ob_next = 0x4056f8f4, _ob_prev = 0x4057329c, ob_refcnt = 2, ob_type = 0x830e1c0 <PyList_Type>},
    # ob_size = 1}, ob_item = 0x8491ae0, allocated = 4}
    # (gdb) c
    # Continuing.
    #
    # Program received signal SIGSEGV, Segmentation fault.
    # 0x080f2c17 in PyObject_GetAttr (v=<unknown at remote 0x405733b4>, name='lineno') at Objects/object.c:872
    # 872 if (tp->tp_getattro != NULL)
    #
    # Objects freed in __getattr
    are used later in the loop above. There are two
    # bugs actually. One is the use-after-free and the second is using a stale size
    # variable "len" to control the for(...) loop. "body" can be mutated inside
    # obj2ast_stmt.

    This construct:

                for (i = 0; i < len; i++) {
                    stmt_ty value;
                    res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
                    if (res != 0) goto failed;
                    asdl_seq_SET(body, i, value);
                }

    is repeated multiple times in multiple obj2ast_ methods. It contains two bugs:

    1. tmp[i] isn't protected from deletion inside python code (refcnt is not increased by GET_ITEM),
    2. tmp's length can drop below "len" resulting in an OOB read, because the loop counter is static.

    @pkt pkt mannequin added the type-crash A hard crash of the interpreter, possibly with a core dump label May 1, 2015
    @tiran tiran added the extension-modules C modules in the Modules dir label May 1, 2015
    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented May 8, 2015

    ping

    3 similar comments
    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented May 12, 2015

    ping

    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented Jul 2, 2015

    ping

    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented Jul 3, 2015

    ping

    @pkt
    Copy link
    Mannequin Author

    pkt mannequin commented Sep 26, 2016

    Fix by replacing static 'len' in loops with a macro, so that mutations of size of the containter do not casue OOB reads.

    @tiran tiran added the 3.7 (EOL) end of life label Sep 26, 2016
    @berkerpeksag
    Copy link
    Member

    Please note that Python/Python-ast.c is automatically generated by Parser/asdl_c.py.

    @serhiy-storchaka
    Copy link
    Member

    Bad things happen not only when a list shrinks, but also when it grows during iteration.

    The one solution is to check if the size is changed on every iteration. The other solution is to convert a list to a tuple for iterating.

    @serhiy-storchaka serhiy-storchaka self-assigned this Oct 5, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 7, 2016

    New changeset 47d5bf5a846f by Serhiy Storchaka in branch '2.7':
    Issue bpo-24098: Fixed possible crash when AST is changed in process of
    https://hg.python.org/cpython/rev/47d5bf5a846f

    New changeset f575710b5f56 by Serhiy Storchaka in branch '3.5':
    Issue bpo-24098: Fixed possible crash when AST is changed in process of
    https://hg.python.org/cpython/rev/f575710b5f56

    New changeset 7528154cadaa by Serhiy Storchaka in branch '3.6':
    Issue bpo-24098: Fixed possible crash when AST is changed in process of
    https://hg.python.org/cpython/rev/7528154cadaa

    New changeset def217aaad2f by Serhiy Storchaka in branch 'default':
    Issue bpo-24098: Fixed possible crash when AST is changed in process of
    https://hg.python.org/cpython/rev/def217aaad2f

    @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 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

    3 participants