Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/sglang/srt/entrypoints/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,7 @@ async def load_lora_adapter(


@app.api_route("/load_lora_adapter_from_tensors", methods=["POST"])
@auth_level(AuthLevel.ADMIN_OPTIONAL)
async def load_lora_adapter_from_tensors(
obj: Annotated[LoadLoRAAdapterFromTensorsReqInput, Body()], request: Request
):
Expand Down
8 changes: 6 additions & 2 deletions python/sglang/srt/entrypoints/http_server_engine.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import base64
import multiprocessing
import time
from typing import List, Optional, Tuple

import requests
import safetensors.torch
import torch

from sglang.srt.entrypoints.EngineBase import EngineBase
from sglang.srt.entrypoints.http_server import launch_server
from sglang.srt.server_args import ServerArgs
from sglang.srt.utils import MultiprocessingSerializer, kill_process_tree
from sglang.srt.utils import kill_process_tree


def launch_server_process(server_args: ServerArgs) -> multiprocessing.Process:
Expand Down Expand Up @@ -91,7 +93,9 @@ def update_weights_from_tensor(
"update_weights_from_tensor",
{
"serialized_named_tensors": [
MultiprocessingSerializer.serialize(named_tensors, output_str=True)
base64.b64encode(
safetensors.torch.save(dict(named_tensors))
).decode()
for _ in range(self.server_args.tp_size)
],
"load_format": load_format,
Expand Down
11 changes: 9 additions & 2 deletions python/sglang/srt/managers/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4892,8 +4892,15 @@ def load_lora_adapter_from_tensors(
self, recv_req: LoadLoRAAdapterFromTensorsReqInput
) -> LoadLoRAAdapterFromTensorsReqOutput:
"""In-place loading a new lora adapter from serialized tensors."""

result = self.tp_worker.load_lora_adapter_from_tensors(recv_req)
try:
result = self.tp_worker.load_lora_adapter_from_tensors(recv_req)
except Exception as e:
# A malformed/malicious serialized payload (e.g. one rejected by
# SafeUnpickler) must not take down the scheduler event loop.
logger.error("Failed to load LoRA adapter from tensors: %s", e)
return LoadLoRAAdapterFromTensorsReqOutput(
success=False, error_message=str(e)
)
return result

def unload_lora_adapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ def update_weights_from_tensor(self, recv_req: UpdateWeightsFromTensorReqInput):
worker = self.tp_worker
else:
worker = self.draft_worker or self.tp_worker
success, message = worker.update_weights_from_tensor(recv_req)
try:
success, message = worker.update_weights_from_tensor(recv_req)
except Exception as e:
# A malformed/malicious serialized payload (e.g. one rejected by
# SafeUnpickler) must not take down the scheduler event loop.
logger.error("Failed to update weights from tensor: %s", e)
success, message = False, str(e)
if success:
self.flush_cache_after_weight_update(recv_req)
else:
Expand Down
17 changes: 12 additions & 5 deletions python/sglang/srt/managers/tp_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
from sglang.srt.model_executor.pool_configurator import MemoryPoolConfig
from sglang.srt.runtime_context import get_exec, get_model, get_schedule, get_spec
from sglang.srt.server_args import ServerArgs
from sglang.srt.utils import MultiprocessingSerializer, broadcast_pyobj, set_random_seed
from sglang.srt.utils import (
broadcast_pyobj,
deserialize_tensor_payload,
set_random_seed,
)
from sglang.srt.utils.hf_transformers_utils import (
get_processor,
get_tokenizer,
Expand Down Expand Up @@ -201,13 +205,16 @@ def _deserialize_own_rank(self, serialized_named_tensors):
deserializing another rank's copy would break producer-side CUDA-IPC
refcounting."""
monkey_patch_torch_reductions()
return MultiprocessingSerializer.deserialize(
serialized_named_tensors[self.ps.tp_rank]
)
return deserialize_tensor_payload(serialized_named_tensors[self.ps.tp_rank])

def update_weights_from_tensor(self, recv_req: UpdateWeightsFromTensorReqInput):
named_tensors = self._deserialize_own_rank(recv_req.serialized_named_tensors)
# safetensors payloads deserialize to a dict; the weight updater expects
# an iterable of (name, tensor) pairs.
if isinstance(named_tensors, dict):
named_tensors = list(named_tensors.items())
success, message = self.model_runner.weight_updater.update_weights_from_tensor(
named_tensors=self._deserialize_own_rank(recv_req.serialized_named_tensors),
named_tensors=named_tensors,
load_format=recv_req.load_format,
)
return success, message
Expand Down
167 changes: 148 additions & 19 deletions python/sglang/srt/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,35 @@ def _looks_like_pickle_payload(data: bytes) -> bool:
return len(data) >= 2 and data[0] == 0x80 and data[1] <= pickle.HIGHEST_PROTOCOL


def _looks_like_safetensors_payload(data: bytes) -> bool:
"""safetensors blobs start with an 8-byte little-endian JSON header length
followed by the JSON header itself (which begins with '{')."""
if len(data) < 8:
return False
header_len = int.from_bytes(data[:8], "little")
if header_len <= 0 or 8 + header_len > len(data):
return False
return data[8] == 0x7B # '{'


def deserialize_tensor_payload(data) -> Dict[str, torch.Tensor]:
"""Deserialize a serialized tensor payload sent over the wire.

Prefers the safetensors format (no code-execution semantics); falls back to
the legacy MultiprocessingSerializer pickle for backward compatibility with
old clients (the pickle path remains constrained by SafeUnpickler).
"""
if isinstance(data, str):
data = pybase64.b64decode(data, validate=True)

if _looks_like_safetensors_payload(data):
import safetensors.torch

return safetensors.torch.load(data)

return MultiprocessingSerializer.deserialize(data)


def normalize_serialized_named_tensor_payload(data: SerializedTensorPayload) -> bytes:
"""Normalize a serialized tensor payload to raw MultiprocessingSerializer bytes."""
if isinstance(data, str):
Expand Down Expand Up @@ -2961,16 +2990,17 @@ def normalize_serialized_named_tensor_payloads(


class SafeUnpickler(pickle.Unpickler):
# Prefix allowlisting remains ONLY for trusted third-party libraries whose
# payloads SGLang itself serializes (torch tensors/state, multiprocessing
# reductions, NPU storage, peft/transformers objects). SGLang's own
# `sglang.srt.*` modules are NOT prefix-trusted anymore: prefix allowlisting
# there let an attacker reach reflective primitives
# (sglang.srt.utils.common.dynamic_import) and even nested unrestricted
# `pickle.loads` helpers (sglang.srt.managers.io_struct._maybe_unwrap_pickle
# + PickleWrapper) that defeat the whole sandbox. Internal classes that
# legitimate payloads actually reference are instead listed exactly in
# ALLOWED_SGLANG_CLASSES.
ALLOWED_MODULE_PREFIXES = {
# --- Python types ---
"builtins.",
"collections.",
"copyreg.",
"functools.",
"itertools.",
"operator.",
"types.",
"weakref.",
# --- PyTorch types ---
"torch.",
"torch._tensor.",
Expand All @@ -2987,22 +3017,97 @@ class SafeUnpickler(pickle.Unpickler):
# --- multiprocessing ---
"multiprocessing.resource_sharer.",
"multiprocessing.reduction.",
"pickletools.",
# --- PEFT / LoRA ---
# --- PEFT / LoRA / tokenizer payloads ---
"peft.",
"transformers.",
"huggingface_hub.",
# --- SGLang & Unitest ---
"sglang.srt.weight_sync.tensor_bucket.",
"sglang.srt.model_executor.model_runner.",
"sglang.srt.model_executor.model_runner_components.weight_updater.",
"sglang.srt.layers.",
"sglang.srt.utils.",
"sglang.srt.disaggregation.",
"sglang.srt.managers.",
"torch_npu.",
}

# Exact (module, symbol) allowlist for SGLang-internal classes that real
# payloads reference. Nothing else under sglang.srt.* may be loaded.
ALLOWED_SGLANG_CLASSES = {
("sglang.srt.weight_sync.tensor_bucket", "FlattenedTensorBucket"),
("sglang.srt.weight_sync.tensor_bucket", "FlattenedTensorMetadata"),
(
"sglang.srt.model_executor.model_runner_components.weight_updater",
"LocalSerializedTensor",
),
}

# Modules whose members must match an exact name allowlist instead of being
# allowlisted by module prefix. Only the side-effect-free primitives that
# SGLang's own serialized payloads actually need are permitted. Anything
# reflective or reachable-to-syscalls (getattr, __import__, eval, open,
# operator.attrgetter/itemgetter, pickletools.*, types.CodeType/...,
# types.FunctionType/...) is rejected.
ALLOWED_CLASSES = {
"builtins": {
"bool",
"bytearray",
"bytes",
"complex",
"dict",
"enumerate",
"filter",
"float",
"frozenset",
"int",
"iter",
"list",
"map",
"object",
"range",
"reversed",
"set",
"slice",
"str",
"tuple",
"type",
"zip",
},
"collections": {
"Counter",
"OrderedDict",
"defaultdict",
"deque",
"namedtuple",
},
"copyreg": {
"_reconstructor",
"__newobj__",
"__newobj_ex__",
"_slotnames",
},
"functools": {
"partial",
"reduce",
},
"itertools": {
"chain",
"count",
"cycle",
"islice",
"repeat",
"starmap",
"takewhile",
"zip_longest",
},
# operator.attrgetter / itemgetter / getitem / methodcaller are RCE
# primitives; nothing in SGLang's own payloads needs them.
"operator": {},
# pickletools is only a debugging utility and exposes module refs
# (e.g. sys) that enable bypass chains.
"pickletools": {},
"types": {
"ModuleType",
"SimpleNamespace",
},
"weakref": {
"ref",
},
}

DENY_CLASSES = {
("builtins", "eval"),
("builtins", "exec"),
Expand All @@ -3013,6 +3118,15 @@ class SafeUnpickler(pickle.Unpickler):
("codecs", "decode"),
("types", "CodeType"),
("types", "FunctionType"),
# High-risk code-loading entry points inside the otherwise-trusted
# torch.* prefix (defense in depth): torch.load / torch.hub.load /
# torch.utils.cpp_extension.load* can load files or execute code, and
# nothing SGLang serializes references them.
("torch", "load"),
("torch.hub", "load"),
("torch.utils.cpp_extension", "load"),
("torch.utils.cpp_extension", "load_inline"),
("torch.jit", "load"),
}

def find_class(self, module, name):
Expand All @@ -3022,6 +3136,21 @@ def find_class(self, module, name):
f"Blocked unsafe class loading ({module}.{name}), "
f"to prevent exploitation of CVE-2025-10164"
)
# Exact-name allowlist for generic building-block modules.
allowed_names = self.ALLOWED_CLASSES.get(module)
if allowed_names is not None:
if name in allowed_names:
return super().find_class(module, name)
raise RuntimeError(
f"Blocked unsafe class loading ({module}.{name}), "
f"to prevent exploitation of CVE-2025-10164"
)
# Exact (module, symbol) allowlist for SGLang-internal classes that
# legitimate payloads reference. No sglang.srt.* module is
# prefix-trusted, so reflective helpers (dynamic_import) and nested
# pickle.loads helpers (io_struct._maybe_unwrap_pickle) are unreachable.
if (module, name) in self.ALLOWED_SGLANG_CLASSES:
return super().find_class(module, name)
# Allowlist of safe-to-load modules.
if any(
(module + ".").startswith(prefix) for prefix in self.ALLOWED_MODULE_PREFIXES
Expand Down
Loading
Loading