summaryrefslogtreecommitdiffstats
path: root/Lib/socket.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-07-30 12:43:45 (GMT)
committerGitHub <noreply@github.com>2024-07-30 12:43:45 (GMT)
commit5f90abaa786f994db3907fc31e2ee00ea2cf0929 (patch)
tree2b9fe8f0d3dd5bb027fd05eee9ffed43685b3809 /Lib/socket.py
parentd542a9be51776e8d589363ee15164dec8dbd3a76 (diff)
downloadcpython-5f90abaa786f994db3907fc31e2ee00ea2cf0929.zip
cpython-5f90abaa786f994db3907fc31e2ee00ea2cf0929.tar.gz
cpython-5f90abaa786f994db3907fc31e2ee00ea2cf0929.tar.bz2
[3.11] gh-122133: Authenticate socket connection for `socket.socketpair()` fallback (GH-122134) (#122426)
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). (cherry picked from commit 78df1043dbdce5c989600616f9f87b4ee72944e5) Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/socket.py')
-rw-r--r--Lib/socket.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index a0567b7..591d473 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -648,6 +648,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")