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

[easy C issue] ctypes: segfault with large number of callback arguments #57306

Closed
meadori opened this issue Oct 4, 2011 · 13 comments
Closed
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes easy topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@meadori
Copy link
Member

meadori commented Oct 4, 2011

BPO 13097
Nosy @amauryfa, @abalkin, @vstinner, @meadori, @miss-islington, @furkanonder
PRs
  • bpo-13097: fix segfault in ctypes callback invocation #19914
  • [3.9] bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914) #20453
  • [3.8] bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914) #20454
  • [3.7] bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914) #20455
  • 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/meadori'
    closed_at = <Date 2020-05-27.15:54:46.297>
    created_at = <Date 2011-10-04.03:57:17.101>
    labels = ['easy', '3.8', '3.9', '3.10', 'ctypes', '3.7', 'type-crash']
    title = '[easy C issue] ctypes: segfault with large number of callback arguments'
    updated_at = <Date 2020-05-27.15:54:46.291>
    user = 'https://github.com/meadori'

    bugs.python.org fields:

    activity = <Date 2020-05-27.15:54:46.291>
    actor = 'vstinner'
    assignee = 'meador.inge'
    closed = True
    closed_date = <Date 2020-05-27.15:54:46.297>
    closer = 'vstinner'
    components = ['ctypes']
    creation = <Date 2011-10-04.03:57:17.101>
    creator = 'meador.inge'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 13097
    keywords = ['patch', 'easy (C)', 'newcomer friendly']
    message_count = 13.0
    messages = ['144852', '145259', '148648', '148701', '148712', '149508', '368001', '368064', '370093', '370099', '370102', '370103', '370105']
    nosy_count = 7.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'vstinner', 'meador.inge', 'python-dev', 'miss-islington', 'furkanonder']
    pr_nums = ['19914', '20453', '20454', '20455']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue13097'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @meadori
    Copy link
    Member Author

    meadori commented Oct 4, 2011

    Reproducible in 2.7 and tip:

    [meadori@motherbrain cpython]$ ./python 
    Python 3.3.0a0 (default:61de28fa5537+d05350c14e77+, Oct  3 2011, 21:47:04) 
    [GCC 4.6.0 20110603 (Red Hat 4.6.0-10)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ctypes import *
    >>> NARGS = 2 ** 20
    >>> proto = CFUNCTYPE(None, *(c_int,) * NARGS)
    >>> def func(*args):
    ...    return (1, "abc", None)
    ... 
    >>> cb = proto(func)
    >>> cb(*(1,) * NARGS)
    Segmentation fault (core dumped)

    @meadori meadori added topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 4, 2011
    @meadori
    Copy link
    Member Author

    meadori commented Oct 9, 2011

    As mentioned in bpo-12881, this issue is a result of an unbounded 'alloca' call that trashes the stack.

    @meadori meadori self-assigned this Nov 29, 2011
    @amauryfa
    Copy link
    Member

    Right, alloca() could be replaced by some malloc(), but is it really useful? After all, when a C function calls back to Python, all arguments needs to be pushed to the stack anyway.

    @meadori
    Copy link
    Member Author

    meadori commented Dec 1, 2011

    On Wed, Nov 30, 2011 at 6:20 AM, Amaury Forgeot d'Arc
    <report@bugs.python.org> wrote:

    Right, alloca() could be replaced by some malloc(), but is it really useful?  After all, when a C function calls back to Python, all arguments needs to be > pushed to the stack anyway.

    The case is somewhat pathological. However, there are *four* 'alloca'
    calls in '_ctypes_callproc', three of which are proportional to
    'argcount'. And as you point out there is an additional stack
    allocation inside of 'libffi' when the callback is actually called
    (also using 'alloca').

    I see two reasons switching to 'malloc' might be beneficial: (1) by
    shifting some of the allocation to dynamic allocation we may reduce
    the chance that we blow the stack at all. Keep in mind that this gets
    more likely if the C ffi function is called from a deep in a call tree
    and *not* necessarily with just a huge number of parameters. (2) if
    resources are exhausted, then we can exit more gracefully. Out of
    dynamic memory errors can actually be handled as opposed to an
    'alloca' call that ends up allocating more than is left.

    That being said, if this does get changed it is low priority.

    @vstinner
    Copy link
    Member

    vstinner commented Dec 1, 2011

    Is there really an use case where you need 2 ** 20 (1,048,576) arguments? If yes, I'm not against the torture in this case :-) If no, why not raising an error if there are too many arguments? E.g. limit to 1,024 arguments or maybe just 10?

    @meadori
    Copy link
    Member Author

    meadori commented Dec 15, 2011

    On Thu, Dec 1, 2011 at 2:11 AM, STINNER Victor <report@bugs.python.org> wrote:

    Is there really an use case where you need 2 ** 20 (1,048,576) arguments? If yes, I'm not against the torture in this case :-)

    Not very likely :-) However, the segfault can occur with less
    arguments in low stack space situations (e.g. deep callstack).

    If no, why not raising an error if there are too many arguments? E.g. limit to 1,024 arguments or maybe just 10?

    That is certainly an option.

    @furkanonder
    Copy link
    Mannequin

    furkanonder mannequin commented May 3, 2020

    The issue continues in python 3.8.2.

    @vstinner
    Copy link
    Member

    vstinner commented May 4, 2020

    I suggest to raise an exception if it's called with more than 1024 arguments.

    @vstinner vstinner changed the title ctypes: segfault with large number of callback arguments [easy C issue] ctypes: segfault with large number of callback arguments May 4, 2020
    @vstinner
    Copy link
    Member

    New changeset 29a1384 by Sean Gillespie in branch 'master':
    bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914)
    29a1384

    @miss-islington
    Copy link
    Contributor

    New changeset 788d7bf by Miss Islington (bot) in branch '3.9':
    bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914)
    788d7bf

    @miss-islington
    Copy link
    Contributor

    New changeset 1c4dcaf by Miss Islington (bot) in branch '3.7':
    bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914)
    1c4dcaf

    @miss-islington
    Copy link
    Contributor

    New changeset a285af7 by Miss Islington (bot) in branch '3.8':
    bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914)
    a285af7

    @vstinner
    Copy link
    Member

    Thanks Meador Inge for the bug report and thanks Sean Gillespie for the fix! It just took 9 years to fix this corner case ;-)

    Copy of the comment on the PR:
    #19914 (review)

    I tried to rewrite _ctypes_callproc() to use PyMem_Malloc() instead of alloca(), but it's quite complicated. There are 3 arrays with a length of argcount items: args, avalues, atypes. Moreover, resbuf is also allocated with alloca(). When using PyMem_Malloc(), error handling is much more complicated.

    I also tried to restrict the overall usage of stack memory to 4096 bytes (size of one page on x86), but users would be surprised by CTYPES_MAX_ARGCOUNT value.

    I would say that raising an exception is better than crashing for a lot of arguments. If someone is blocked by this new limitation, in that case we can revisit the PyMem_Malloc() idea.

    @vstinner vstinner added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels May 27, 2020
    @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 easy topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants