diff options
author | Seth Michael Larson <seth@python.org> | 2024-07-29 16:44:35 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-29 14:44:35 -0700 |
commit | 78df1043dbdce5c989600616f9f87b4ee72944e5 (patch) | |
tree | 20f92525f43536e120fe1f934dc1a20f31cce366 /Lib/socket.py | |
parent | 76bdfa4cd02532519fb43ae91244e2b4b3650d78 (diff) | |
download | cpython-78df1043dbdce5c989600616f9f87b4ee72944e5.tar.gz cpython-78df1043dbdce5c989600616f9f87b4ee72944e5.zip |
gh-122133: Authenticate socket connection for `socket.socketpair()` fallback (GH-122134)
* Authenticate socket connection for `socket.socketpair()` fallback when the platform does not have a native `socketpair` C API. We authenticate in-process using `getsocketname` and `getpeername` (thanks to Nathaniel J Smith for that suggestion).
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/socket.py')
-rw-r--r-- | Lib/socket.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/socket.py b/Lib/socket.py index 524ce1361b9..2e6043cbdb8 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -650,6 +650,23 @@ else: raise finally: lsock.close() + + # Authenticating avoids using a connection from something else + # able to connect to {host}:{port} instead of us. + # We expect only AF_INET and AF_INET6 families. + try: + if ( + ssock.getsockname() != csock.getpeername() + or csock.getsockname() != ssock.getpeername() + ): + raise ConnectionError("Unexpected peer connection") + except: + # getsockname() and getpeername() can fail + # if either socket isn't connected. + ssock.close() + csock.close() + raise + return (ssock, csock) __all__.append("socketpair") |