[Fix] srt: Admit only allowlisted peers on TCP-bound ZMQ IPC sockets (#45250)
Co-authored-by: daniel-salib <daniel-salib@users.noreply.github.com>
This commit is contained in:
co-authored by
daniel-salib
parent
afe3e1df31
commit
194876bea0
@@ -366,6 +366,8 @@ class Envs:
|
||||
SGLANG_USE_PICKLE_IPC = EnvBool(True)
|
||||
# Log top-level PickleWrapper frames unwrapped on msgpack IPC decode.
|
||||
SGLANG_LOG_PICKLE_IPC_OBJECTS = EnvBool(False)
|
||||
# Comma-separated peer IPs admitted by TCP-bound IPC sockets; empty admits all.
|
||||
SGLANG_ZMQ_ALLOWED_PEERS = EnvTuple(tuple())
|
||||
SGLANG_USE_MESSAGE_QUEUE_BROADCASTER = EnvBool(True)
|
||||
SGLANG_TCP_STORE_PORT = EnvInt(29600)
|
||||
# Base port hint for ephemeral sockets (ZMQ, SHM broadcaster, etc.).
|
||||
|
||||
@@ -654,7 +654,7 @@ class DataParallelController:
|
||||
)
|
||||
for slot in range(bind_count):
|
||||
worker_port, worker_socket = get_zmq_socket_on_host(
|
||||
self.context, zmq.PUSH, host=bind_host
|
||||
self.context, zmq.PUSH, host=bind_host, restrict_peers=True
|
||||
)
|
||||
worker_ports.append(worker_port)
|
||||
self.workers[slot] = worker_socket
|
||||
|
||||
@@ -53,7 +53,7 @@ import msgspec.structs
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
from sglang.srt.runtime_context import get_parallel, get_serving
|
||||
from sglang.srt.utils.network import is_zmq_endpoint_ipv6
|
||||
from sglang.srt.utils.network import is_zmq_endpoint_ipv6, restrict_zmq_peers
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -541,6 +541,8 @@ class ZmqShmLoadSnapshotReader:
|
||||
self._socket.setsockopt(_zmq.IPV6, 1)
|
||||
self._socket.setsockopt(_zmq.LINGER, 0)
|
||||
self._socket.setsockopt(_zmq.CONFLATE, 1)
|
||||
if endpoint.startswith("tcp://"):
|
||||
restrict_zmq_peers(self._ctx, self._socket)
|
||||
self._socket.bind(endpoint)
|
||||
|
||||
self._endpoint = endpoint
|
||||
|
||||
@@ -114,7 +114,7 @@ class RayDataParallelController(DataParallelController):
|
||||
worker_ports = []
|
||||
for dp_rank in range(get_parallel().num_dp_ranks):
|
||||
worker_port, worker_socket = get_zmq_socket_on_host(
|
||||
self.context, zmq.PUSH, host=self.rank0_node_ip
|
||||
self.context, zmq.PUSH, host=self.rank0_node_ip, restrict_peers=True
|
||||
)
|
||||
worker_ports.append(worker_port)
|
||||
self.workers[dp_rank] = worker_socket
|
||||
|
||||
@@ -5,15 +5,22 @@ import logging
|
||||
import os
|
||||
import random
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
import psutil
|
||||
import zmq
|
||||
from zmq.auth.thread import ThreadAuthenticator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# libzmq allows one ZAP handler per context, so sockets created from the same
|
||||
# context share an authenticator. Keyed by `zmq.Context.underlying`.
|
||||
_zmq_authenticators: dict[int, ThreadAuthenticator] = {}
|
||||
_zmq_authenticators_lock = threading.Lock()
|
||||
|
||||
|
||||
def get_open_port() -> int:
|
||||
from sglang.srt.environ import envs
|
||||
@@ -222,10 +229,33 @@ def bind_port(port):
|
||||
return try_bind_socket(port=port, listen=True)
|
||||
|
||||
|
||||
def restrict_zmq_peers(context: zmq.Context, socket: zmq.Socket) -> None:
|
||||
"""Reject TCP peers outside `SGLANG_ZMQ_ALLOWED_PEERS` (CVE-2026-93034).
|
||||
|
||||
Must be called before `socket` binds. No-op when the allowlist is unset.
|
||||
"""
|
||||
from sglang.srt.environ import envs
|
||||
|
||||
peers = envs.SGLANG_ZMQ_ALLOWED_PEERS.get()
|
||||
if not peers:
|
||||
return
|
||||
with _zmq_authenticators_lock:
|
||||
if context.underlying not in _zmq_authenticators:
|
||||
auth = ThreadAuthenticator(context)
|
||||
auth.start()
|
||||
# ZAP compares the peer address as a string; libzmq formats it
|
||||
# the same way as `ipaddress` (compressed, lowercase).
|
||||
auth.allow(*(ipaddress.ip_address(p).compressed for p in peers))
|
||||
_zmq_authenticators[context.underlying] = auth
|
||||
# ZAP only vets NULL-mechanism connections when a domain is set.
|
||||
socket.zap_domain = b"sglang"
|
||||
|
||||
|
||||
def get_zmq_socket_on_host(
|
||||
context: zmq.Context,
|
||||
socket_type: zmq.SocketType,
|
||||
host: Optional[str] = None,
|
||||
restrict_peers: bool = False,
|
||||
) -> Tuple[int, zmq.Socket]:
|
||||
"""Create and configure a ZeroMQ socket.
|
||||
|
||||
@@ -236,12 +266,16 @@ def get_zmq_socket_on_host(
|
||||
"127.0.0.1" (localhost-only) to avoid exposing unauthenticated
|
||||
sockets to the network (CVE-2026-3060). Callers that need
|
||||
cross-machine reachability must pass an explicit host.
|
||||
restrict_peers: Apply `restrict_zmq_peers`. Off by default because
|
||||
disaggregation sockets accept peers from other jobs.
|
||||
|
||||
Returns:
|
||||
Tuple of (port, socket) where port is the randomly assigned TCP port.
|
||||
"""
|
||||
socket = context.socket(socket_type)
|
||||
config_socket(socket, socket_type)
|
||||
if restrict_peers:
|
||||
restrict_zmq_peers(context, socket)
|
||||
if host is None:
|
||||
host = "127.0.0.1"
|
||||
if is_valid_ipv6_address(host):
|
||||
@@ -420,6 +454,7 @@ def get_zmq_socket(
|
||||
if endpoint is None:
|
||||
# Bind to random TCP port
|
||||
config_socket(socket, socket_type)
|
||||
restrict_zmq_peers(context, socket)
|
||||
port = socket.bind_to_random_port("tcp://*")
|
||||
return port, socket
|
||||
else:
|
||||
@@ -429,6 +464,8 @@ def get_zmq_socket(
|
||||
config_socket(socket, socket_type)
|
||||
|
||||
if bind:
|
||||
if endpoint.startswith("tcp://"):
|
||||
restrict_zmq_peers(context, socket)
|
||||
socket.bind(endpoint)
|
||||
else:
|
||||
socket.connect(endpoint)
|
||||
|
||||
Reference in New Issue
Block a user