summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-07-17 15:39:22 (GMT)
committerGitHub <noreply@github.com>2022-07-17 15:39:22 (GMT)
commit65d87a2cb843d5f6af6489cb9784f55938196a28 (patch)
tree981613e7ac4088f5223d9000f8b8727cae42a2a1 /Modules
parent7bca87d384b9c8b5243d930536c13c66b559010d (diff)
downloadcpython-65d87a2cb843d5f6af6489cb9784f55938196a28.zip
cpython-65d87a2cb843d5f6af6489cb9784f55938196a28.tar.gz
cpython-65d87a2cb843d5f6af6489cb9784f55938196a28.tar.bz2
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 Fixes f6b3a07b7df6 (bpo-44493: Add missing terminated NUL in sockaddr_un's length (GH-26866) (cherry picked from commit c22f134211743cd5ad14cec1dd4f527bee542b4c) Co-authored-by: Nir Soffer <nsoffer@redhat.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/socketmodule.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 461096c..4e936e2 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1692,8 +1692,10 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
struct sockaddr_un* addr = &addrbuf->un;
#ifdef __linux__
- if (path.len > 0 && *(const char *)path.buf == 0) {
- /* Linux abstract namespace extension */
+ if (path.len == 0 || *(const char *)path.buf == 0) {
+ /* Linux abstract namespace extension:
+ - Empty address auto-binding to an abstract address
+ - Address that starts with null byte */
if ((size_t)path.len > sizeof addr->sun_path) {
PyErr_SetString(PyExc_OSError,
"AF_UNIX path too long");