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

Calling many methods on _ssl._SSLSocket() segfaults #126455

Closed
devdanzin opened this issue Nov 5, 2024 · 8 comments
Closed

Calling many methods on _ssl._SSLSocket() segfaults #126455

devdanzin opened this issue Nov 5, 2024 · 8 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-SSL type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@devdanzin
Copy link
Contributor

devdanzin commented Nov 5, 2024

Crash report

What happened?

Calling many different methods on a _ssl._SSLSocket() instance causes a segfault, for example:

import _ssl
s = _ssl._SSLSocket()
s.shutdown()
# Segmentation fault

Other methods known to segfault are do_handshake, get_channel_binding, get_verified_chain (but not get_unverified_chain), getpeercert, read, verify_client_post_handshake, write. Backtrace looks like:

#0  0x00007ffff7b89129 in SSL_shutdown () from /lib/x86_64-linux-gnu/libssl.so.3
#1  0x00007ffff7c287bc in _ssl__SSLSocket_shutdown_impl (self=self@entry=0x20000572510) at ./Modules/_ssl.c:2710
#2  0x00007ffff7c28977 in _ssl__SSLSocket_shutdown (self=0x20000572510, _unused_ignored=<optimized out>)
    at ./Modules/clinic/_ssl.c.h:557
#3  0x0000555555694039 in method_vectorcall_NOARGS (func=<method_descriptor at remote 0x20000779380>, args=0x7fffffffd298,
    nargsf=<optimized out>, kwnames=<optimized out>) at Objects/descrobject.c:447
#4  0x000055555567ccec in _PyObject_VectorcallTstate (tstate=0x555555d2c2a0 <_PyRuntime+359904>,
    callable=<method_descriptor at remote 0x20000779380>, args=0x7fffffffd298, nargsf=9223372036854775809, kwnames=0x0)
    at ./Include/internal/pycore_call.h:167
#5  0x000055555567ce0b in PyObject_Vectorcall (callable=callable@entry=<method_descriptor at remote 0x20000779380>,
    args=args@entry=0x7fffffffd298, nargsf=<optimized out>, kwnames=kwnames@entry=0x0) at Objects/call.c:327
#6  0x0000555555841c09 in _PyEval_EvalFrameDefault (tstate=tstate@entry=0x555555d2c2a0 <_PyRuntime+359904>, frame=<optimized out>,
    throwflag=throwflag@entry=0) at Python/generated_cases.c.h:955
#7  0x000055555586fb1f in _PyEval_EvalFrame (throwflag=0, frame=<optimized out>, tstate=0x555555d2c2a0 <_PyRuntime+359904>)
    at ./Include/internal/pycore_ceval.h:116
#8  _PyEval_Vector (tstate=tstate@entry=0x555555d2c2a0 <_PyRuntime+359904>, func=func@entry=0x20000ad32d0,
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <type at remote 0x20000276e10>, '__spec__': None, '__builtins__': <module at remote 0x2000025c640>, '_ssl': <module at remote 0x20000778d60>, 's': <_ssl._SSLSocket at remote 0x20000572510>}, args=args@entry=0x0, argcount=argcount@entry=0, kwnames=kwnames@entry=0x0) at Python/ceval.c:1886
#9  0x000055555586fd6c in PyEval_EvalCode (co=co@entry=<code at remote 0x2000033f190>,
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <type at remote 0x20000276e10>, '__spec__': None, '__builtins__': <module at remote 0x2000025c640>, '_ssl': <module at remote 0x20000778d60>, 's': <_ssl._SSLSocket at remote 0x20000572510>},
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <type at remote 0x20000276e10>, '__spec__': None, '__builtins__': <module at remote 0x2000025c640>, '_ssl': <module at remote 0x20000778d60>, 's': <_ssl._SSLSocket at remote 0x20000572510>}) at Python/ceval.c:662

Found using fusil by @vstinner.

CPython versions tested on:

3.10, 3.11, 3.12, 3.13, 3.14, CPython main branch

Operating systems tested on:

Linux, Windows

Output from running 'python -VV' on the command line:

Python 3.14.0a1+ experimental free-threading build (heads/main-dirty:bfc1d2504c, Nov 4 2024, 07:55:58) [GCC 11.4.0]

Linked PRs

@devdanzin devdanzin added the type-crash A hard crash of the interpreter, possibly with a core dump label Nov 5, 2024
@ZeroIntensity ZeroIntensity added extension-modules C modules in the Modules dir topic-SSL 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 5, 2024
@ZeroIntensity
Copy link
Member

I can probably deal with this considering my previous work with _ssl, unless someone else really wants to.

@devdanzin
Copy link
Contributor Author

Seems not to be related to free-threading, given being present in many previous versions.

@rruuaanng
Copy link
Contributor

rruuaanng commented Nov 6, 2024

I can probably deal with this considering my previous work with _ssl, unless someone else really wants to.

If you don't mind, can you give it to me :)

@vstinner
Copy link
Member

vstinner commented Nov 6, 2024

The problem is that it's possible to create an invalid SSLSocket:

$ ./python -c 'import _ssl; _ssl._SSLSocket().context'
Erreur de segmentation (core dumped)

In the Python implementation, there is:

class SSLSocket(socket):
    def __init__(self, *args, **kwargs):
        raise TypeError(
            f"{self.__class__.__name__} does not have a public "
            f"constructor. Instances are returned by "
            f"SSLContext.wrap_socket()."
        )

Maybe the _ssl extension module needs a similar constructor.

@erlend-aasland
Copy link
Contributor

If you don't mind, can you give it to me :)

@rruuaanng: No, please do not. I suggest you instead focus on the many PRs you already have opened. Only when you're down to a handful of open PR, consider again to open a new PR.

vstinner added a commit to vstinner/cpython that referenced this issue Nov 6, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Nov 6, 2024
Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
@vstinner
Copy link
Member

vstinner commented Nov 6, 2024

I wrote #126481 to fix the issue.

vstinner added a commit that referenced this issue Nov 6, 2024
Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 6, 2024
Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
(cherry picked from commit b1c4ffc)

Co-authored-by: Victor Stinner <vstinner@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 6, 2024
Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
(cherry picked from commit b1c4ffc)

Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner
Copy link
Member

vstinner commented Nov 6, 2024

Fixed. Thanks for the bug report.

@vstinner vstinner closed this as completed Nov 6, 2024
vstinner added a commit that referenced this issue Nov 6, 2024
…126487)

gh-126455: Disallow _ssl.SSLSocket instantiation (GH-126481)

Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
(cherry picked from commit b1c4ffc)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this issue Nov 6, 2024
…126486)

gh-126455: Disallow _ssl.SSLSocket instantiation (GH-126481)

Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
(cherry picked from commit b1c4ffc)

Co-authored-by: Victor Stinner <vstinner@python.org>
@ZeroIntensity
Copy link
Member

Thanks for working on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-SSL type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

6 participants