mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
gh-94821: Fix autobind of empty unix domain address (GH-94826)
When binding a unix socket to an empty address on Linux, the socket is automatically bound to an available address in the abstract namespace. >>> s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) >>> s.bind("") >>> s.getsockname() b'\x0075499' Since python 3.9, the socket is bound to the one address: >>> s.getsockname() b'\x00' And trying to bind multiple sockets will fail with: Traceback (most recent call last): File "/home/nsoffer/src/cpython/Lib/test/test_socket.py", line 5553, in testAutobind s2.bind("") OSError: [Errno 98] Address already in use Added 2 tests: - Auto binding empty address on Linux - Failing to bind an empty address on other platforms Fixesf6b3a07b7d
(bpo-44493: Add missing terminated NUL in sockaddr_un's length (GH-26866) (cherry picked from commitc22f134211
) Co-authored-by: Nir Soffer <nsoffer@redhat.com>
This commit is contained in:
parent
7bca87d384
commit
65d87a2cb8
3 changed files with 25 additions and 2 deletions
|
@ -5480,6 +5480,20 @@ class TestLinuxAbstractNamespace(unittest.TestCase):
|
|||
s.bind(bytearray(b"\x00python\x00test\x00"))
|
||||
self.assertEqual(s.getsockname(), b"\x00python\x00test\x00")
|
||||
|
||||
def testAutobind(self):
|
||||
# Check that binding to an empty string binds to an available address
|
||||
# in the abstract namespace as specified in unix(7) "Autobind feature".
|
||||
abstract_address = b"^\0[0-9a-f]{5}"
|
||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1:
|
||||
s1.bind("")
|
||||
self.assertRegex(s1.getsockname(), abstract_address)
|
||||
# Each socket is bound to a different abstract address.
|
||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2:
|
||||
s2.bind("")
|
||||
self.assertRegex(s2.getsockname(), abstract_address)
|
||||
self.assertNotEqual(s1.getsockname(), s2.getsockname())
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'test needs socket.AF_UNIX')
|
||||
class TestUnixDomain(unittest.TestCase):
|
||||
|
||||
|
@ -5549,6 +5563,11 @@ class TestUnixDomain(unittest.TestCase):
|
|||
self.addCleanup(os_helper.unlink, path)
|
||||
self.assertEqual(self.sock.getsockname(), path)
|
||||
|
||||
@unittest.skipIf(sys.platform == 'linux', 'Linux specific test')
|
||||
def testEmptyAddress(self):
|
||||
# Test that binding empty address fails.
|
||||
self.assertRaises(OSError, self.sock.bind, "")
|
||||
|
||||
|
||||
class BufferIOTest(SocketConnectedTest):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue