Skip to content

Segmentation fault with invalid Unicode command-line arguments in embedded Python #69817

Description

@itaibn
mannequin
BPO 25631
Nosy @ncoghlan, @vstinner, @eryksun

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 = None
closed_at = <Date 2019-09-26.00:53:26.190>
created_at = <Date 2015-11-16.01:06:05.318>
labels = ['interpreter-core', '3.8', '3.9', 'type-crash']
title = 'Segmentation fault with invalid Unicode command-line arguments in embedded Python'
updated_at = <Date 2019-09-26.00:53:26.187>
user = 'https://bugs.python.org/itaibn'

bugs.python.org fields:

activity = <Date 2019-09-26.00:53:26.187>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2019-09-26.00:53:26.190>
closer = 'vstinner'
components = ['Interpreter Core']
creation = <Date 2015-11-16.01:06:05.318>
creator = 'itaibn'
dependencies = []
files = []
hgrepos = []
issue_num = 25631
keywords = []
message_count = 3.0
messages = ['254703', '254739', '353246']
nosy_count = 4.0
nosy_names = ['ncoghlan', 'vstinner', 'eryksun', 'itaibn']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue25631'
versions = ['Python 3.8', 'Python 3.9']

Activity

  1. itaibn commented on Nov 16, 2015

    itaibnmannequin
    MannequinAuthor

    The following embedded application, which calls Py_Main with a "-W X" argument where X is not a valid Unicode string, returns a segmentation fault:

    #include "Python.h"
    
    main() {
        wchar_t *invalid_str;
        invalid_str = malloc(2*sizeof(wchar_t));
        invalid_str[0] = 0x110000;
        invalid_str[1] = 0;
        wchar_t *argv[4] = {L"embedded-python", L"-W", invalid_str, NULL};
    
        Py_Main(3, argv);
    }
    

    This segmentation fault is present in Python 3.4, 3.5, and the latest development branch I downloaded, but is not present in Python 3.2. This program is obviously invalid and it may be reasonable to emit a fatal error in this situation, but it should not give a segmentation fault.

    I believe the issue is that this codes leads to exception being thrown before exceptions are initialized, and more specifically, a call to PyExceptionClass_Check() within PyErr_Object() reads a NULL pointer. I haven't tested this but I expect that this problem would not appear when calling Python directly since Python sanitizes the command line arguments from main(). Nonetheless even here the possibility of other exceptions being raised early in the initialization sequence remains a potential problem.

  2. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Nov 16, 2015
  3. eryksun commented on Nov 16, 2015

    @eryksun
    Contributor

    The interpreter isn't initialized, so calling PyErr_Format in a release build segfaults when it tries to dereference a NULL PyThreadState. OTOH, a debug build should call PyThreadState_Get, which in this case calls Py_FatalError and aborts the process. Unfortunately 3.5.0+ debug builds don't call PyThreadState_Get due to the fix for bpo-25150.

    the possibility of other exceptions being raised early in the
    initialization sequence remains a potential problem.

    PEP-432 proposes a pre-initialization phase that sets a valid Python thread state.

  4. vstinner commented on Sep 26, 2019

    @vstinner
    Member

    This bug has been fixed in the implementation of the PEP-587.

    $ gcc x.c -l python3.8 $(pkg-config python-3.8 --cflags --libs) -o x
    $ ./x
    Fatal Python error: pyinit_main: can't finish initializing sys
    ValueError: character U+110000 is not in range [U+0000; U+10ffff]

    Current thread 0x00007fd23364e6c0 (most recent call first):

    => it doesn't crash anymore, but write a error message and exit.

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)end of life3.9 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions