From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sat, 18 Jan 2025 01:06:45 +0000 (+0100) Subject: [3.12] gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets (GH-128933... X-Git-Tag: v3.12.9~67 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8a8f5d636d06c122bc1db0ee6b11a883e550a803;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets (GH-128933) (#128970) gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets (GH-128933) * gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets Do not attempt to set ``SO_REUSEPORT`` on sockets of address familifies other than ``AF_INET`` and ``AF_INET6``, as it is meaningless with these address families, and the call with fail with Linux kernel 6.12.9 and newer. * Apply suggestions from code review --------- (cherry picked from commit 3829104ab412a47bf3f36b8c133c886d2cc9a6d4) Co-authored-by: Michał Górny Co-authored-by: Vinay Sajip --- diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 3146f7f3f655..9cbed3044968 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -1550,7 +1550,9 @@ class BaseEventLoop(events.AbstractEventLoop): if reuse_address: sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, True) - if reuse_port: + # Since Linux 6.12.9, SO_REUSEPORT is not allowed + # on other address families than AF_INET/AF_INET6. + if reuse_port and af in (socket.AF_INET, socket.AF_INET6): _set_reuseport(sock) # Disable IPv4/IPv6 dual stack support (enabled by # default on Linux) which makes a single socket diff --git a/Lib/socket.py b/Lib/socket.py index c1880c4ea512..91782b30ae8d 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -932,7 +932,9 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False, # Fail later on bind(), for platforms which may not # support this option. pass - if reuse_port: + # Since Linux 6.12.9, SO_REUSEPORT is not allowed + # on other address families than AF_INET/AF_INET6. + if reuse_port and family in (AF_INET, AF_INET6): sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) if has_ipv6 and family == AF_INET6: if dualstack_ipv6: diff --git a/Lib/socketserver.py b/Lib/socketserver.py index cd028ef1c63b..35b2723de3ba 100644 --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -468,7 +468,12 @@ class TCPServer(BaseServer): """ if self.allow_reuse_address and hasattr(socket, "SO_REUSEADDR"): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - if self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT"): + # Since Linux 6.12.9, SO_REUSEPORT is not allowed + # on other address families than AF_INET/AF_INET6. + if ( + self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT") + and self.address_family in (socket.AF_INET, socket.AF_INET6) + ): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.socket.bind(self.server_address) self.server_address = self.socket.getsockname() diff --git a/Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst b/Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst new file mode 100644 index 000000000000..f2db341ef816 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst @@ -0,0 +1,3 @@ +Do not attempt to set ``SO_REUSEPORT`` on sockets of address families +other than ``AF_INET`` and ``AF_INET6``, as it is meaningless with these +address families, and the call with fail with Linux kernel 6.12.9 and newer.