Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Harden torch.export .pt2 loading against CVE-2026-4538 (#12657)
Daniel Han35b90da9b7e3
diff --git a/tests/test_torch_export_pt2_load_guard.py b/tests/test_torch_export_pt2_load_guard.pynew file mode 100644index 00000000000..d3ac2591210--- /dev/null+++ b/tests/test_torch_export_pt2_load_guard.py@@ -0,0 +1,214 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""CVE-2026-4538: torch.export .pt2 loading unpickles archive payloads with weights_only=False+(always on older torch, as a fallback after a failed weights_only=True load on newer torch, and+for `use_pickle` weights / constants), so a crafted .pt2 runs code on load. No torch release+fixes it (pytorch/pytorch#176791 was closed unmerged).++patch_torch_export_pt2_unsafe_load forces weights_only=True only for loads issued from the two+export loader modules. The tests call torch.load from a function whose globals are those+modules, which is exactly what the guard keys on, with a harmless class that torch's+weights_only allowlist rejects.+"""++import importlib+import importlib.util+import io+import pathlib+import pickle+import sys+import types++import pytest++torch = pytest.importorskip("torch")++_REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]+_SERIALIZE = "torch._export.serde.serialize"+_PACKAGE = "torch.export.pt2_archive._package"+++def _load_import_fixes():+ # By path: importing the unsloth package needs an accelerator.+ spec = importlib.util.spec_from_file_location(+ "unsloth_import_fixes_under_test", _REPO_ROOT / "unsloth" / "import_fixes.py"+ )+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++import_fixes = _load_import_fixes()+++class NotATensor:+ pass+++def _payload(obj):+ buffer = io.BytesIO()+ torch.save(obj, buffer)+ return buffer.getvalue()+++def _load_from(module_name):+ """torch.load(weights_only=False) issued from inside `module_name`."""+ namespace = dict(vars(importlib.import_module(module_name)))+ namespace.update(torch = torch, io = io)+ code = (lambda raw: torch.load(io.BytesIO(raw), weights_only = False)).__code__+ return types.FunctionType(code, namespace)+++def guarded(monkeypatch):+ import torch._dynamo # noqa: F401 # `import unsloth` always loads dynamo++ monkeypatch.delenv("UNSLOTH_ALLOW_UNSAFE_PT2_LOAD", raising = False)+ original_load = torch.load+ package = sys.modules.get(_PACKAGE)+ original_pickle = getattr(package, "pickle", None)+ original_meta_path = list(sys.meta_path)+ import_fixes.patch_torch_export_pt2_unsafe_load()+ yield+ torch.load = original_load+ sys.meta_path[:] = original_meta_path+ package = sys.modules.get(_PACKAGE)+ if package is not None and original_pickle is not None:+ package.pickle = original_pickle+ elif package is not None and hasattr(+ getattr(package, "pickle", None), "_unsloth_pt2_real_pickle"+ ):+ package.pickle = package.pickle._unsloth_pt2_real_pickle+++def _unguarded_load():+ load = torch.load+ while getattr(load, "_unsloth_pt2_guard", False):+ load = load.__wrapped__+ return load+++def test_unpatched_torch_unpickles_from_export_loader(monkeypatch):+ # The defect: without the guard the export loader path unpickles arbitrary objects.+ monkeypatch.setattr(torch, "load", _unguarded_load())+ loaded = _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+ assert type(loaded["x"]).__name__ == "NotATensor"+++def test_export_loader_non_tensor_payload_blocked(guarded):+ with pytest.raises(pickle.UnpicklingError, match = "UNSLOTH_ALLOW_UNSAFE_PT2_LOAD=1"):+ _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+++def test_export_loader_tensor_payload_still_loads(guarded):+ loaded = _load_from(_SERIALIZE)(_payload({"w": torch.ones(2)}))+ assert torch.equal(loaded["w"], torch.ones(2))+++def test_opt_out_restores_torch_behaviour(guarded, monkeypatch):+ monkeypatch.setenv("UNSLOTH_ALLOW_UNSAFE_PT2_LOAD", "1")+ loaded = _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+ assert type(loaded["x"]).__name__ == "NotATensor"+++def test_unrelated_callers_unaffected(guarded):+ loaded = torch.load(io.BytesIO(_payload({"x": NotATensor()})), weights_only = False)+ assert type(loaded["x"]).__name__ == "NotATensor"+++def test_patch_is_idempotent(guarded):+ first = torch.load+ package = sys.modules.get(_PACKAGE)+ first_pickle = getattr(package, "pickle", None)+ import_fixes.patch_torch_export_pt2_unsafe_load()+ assert torch.load is first+ assert getattr(package, "pickle", None) is first_pickle+++def test_opaque_pickle_loads_blocked(guarded):+ package = sys.modules.get(_PACKAGE)+ if package is None or not hasattr(package, "pickle"):+ pytest.skip("this torch has no pt2_archive._package pickle use")+ assert package.pickle.dumps({"a": 1})+ with pytest.raises(pickle.UnpicklingError, match = "CVE-2026-4538"):+ package.pickle.loads(pickle.dumps({"a": 1}))+++def _rng_style_wrapper(inner, flag):+ # Stands in for patch_unsafe_trainer_rng_load's torch.load shim, which lives in unsloth.*.+ namespace = {"__name__": "unsloth.import_fixes", "inner": inner}+ exec("def wrapper(*args, **kwargs):\n return inner(*args, **kwargs)\n", namespace)+ wrapper = namespace["wrapper"]+ wrapper._unsloth_rng_guard = True+ wrapper._unsloth_rng_flag = flag+ return wrapper+++def test_rng_guard_composes_in_either_order(guarded):+ # The rng guard (CVE-2026-1839) keys its idempotency on markers on torch.load.+ flag = object()+ guarded_load = torch.load+ # rng shim outside the pt2 guard: its unsloth frame is skipped, the payload still blocked.+ torch.load = _rng_style_wrapper(guarded_load, flag)+ with pytest.raises(pickle.UnpicklingError):+ _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+ # pt2 guard applied over the rng shim keeps the rng markers and still blocks.+ torch.load = _rng_style_wrapper(_unguarded_load(), flag)+ import_fixes.patch_torch_export_pt2_unsafe_load()+ assert torch.load._unsloth_pt2_guard and torch.load._unsloth_rng_flag is flag+ with pytest.raises(pickle.UnpicklingError):+ _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+++def test_export_roundtrip_and_compile_unchanged(guarded, tmp_path):+ class Model(torch.nn.Module):+ def __init__(self):+ super().__init__()+ self.linear = torch.nn.Linear(4, 3)+ self.register_buffer("scale", torch.full((3,), 2.0))++ def forward(self, x):+ return self.linear(x) * self.scale + 1++ model, x = Model(), torch.randn(2, 4)+ path = tmp_path / "model.pt2"+ torch.export.save(torch.export.export(model, (x,)), str(path))+ reloaded = torch.export.load(str(path))+ assert torch.allclose(reloaded.module()(x), model(x))+ compiled = torch.compile(lambda a: torch.sin(a) * 2 + 1, backend = "eager")+ assert torch.allclose(compiled(x), torch.sin(x) * 2 + 1)+++def test_outer_torch_load_wrapper_does_not_hide_export_caller(guarded):+ # Another library wrapping torch.load after Unsloth puts its own frame between the export+ # loader and the guard.+ inner = torch.load++ def outer(*args, **kwargs):+ return inner(*args, **kwargs)++ torch.load = outer+ with pytest.raises(pickle.UnpicklingError, match = "UNSLOTH_ALLOW_UNSAFE_PT2_LOAD=1"):+ _load_from(_SERIALIZE)(_payload({"x": NotATensor()}))+++def test_package_imported_after_patch_is_hardened(guarded):+ if importlib.util.find_spec("torch.export.pt2_archive") is None:+ pytest.skip("this torch has no pt2_archive package")+ parent = importlib.import_module("torch.export.pt2_archive")+ saved_module = sys.modules.pop(_PACKAGE, None)+ saved_attr = parent.__dict__.pop("_package", None)+ try:+ import_fixes.patch_torch_export_pt2_unsafe_load()+ module = importlib.import_module(_PACKAGE)+ if not isinstance(getattr(module, "pickle", None), types.ModuleType):+ pytest.skip("this torch's _package does not use pickle")+ assert isinstance(module.pickle, import_fixes._Pt2PickleModule)+ with pytest.raises(pickle.UnpicklingError):+ module.pickle.loads(pickle.dumps(1))+ finally:+ if saved_module is not None:+ sys.modules[_PACKAGE] = saved_module+ if saved_attr is not None:+ parent._package = saved_attrdiff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.pyindex 127f35bc4cf..45fbe0e7c3f 100644--- a/unsloth/_gpu_init.py+++ b/unsloth/_gpu_init.py@@ -293,6 +293,7 @@ from .import_fixes import (patch_psutil_cpu_freq,patch_enable_input_require_grads,patch_unsafe_trainer_rng_load,+ patch_torch_export_pt2_unsafe_load,fix_openenv_no_vllm,patch_openspiel_env_async,fix_executorch,@@ -382,6 +383,7 @@ patch_datasets()patch_psutil_cpu_freq()patch_enable_input_require_grads()patch_unsafe_trainer_rng_load()+patch_torch_export_pt2_unsafe_load()fix_openenv_no_vllm()patch_openspiel_env_async()fix_executorch()diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.pyindex 307cb80dac4..80e42f24e5d 100644--- a/unsloth/import_fixes.py+++ b/unsloth/import_fixes.py@@ -33,6 +33,7 @@ import sysconfigimport threadingimport functoolsimport inspect+import types# We cannot do from unsloth_zoo.log import logger since FBGEMM might cause seg faults.UNSLOTH_ENABLE_LOGGING = os.environ.get("UNSLOTH_ENABLE_LOGGING", "0") in (@@ -4730,6 +4731,162 @@ def patch_unsafe_trainer_rng_load():logger.info("Unsloth: Hardened Trainer._load_rng_state rng loading (CVE-2026-1839).")+_PT2_UNSAFE_LOAD_ENV = "UNSLOTH_ALLOW_UNSAFE_PT2_LOAD"+# torch.export's .pt2 readers: their weights_only=False loads unpickle archive bytes.+_PT2_LOADER_MODULES = frozenset(+ (+ "torch._export.serde.serialize",+ "torch.export.pt2_archive._package",+ )+)+++def _pt2_unsafe_load_allowed():+ return os.environ.get(_PT2_UNSAFE_LOAD_ENV, "0").strip().lower() in ("1", "true", "yes", "on")+++def _pt2_unsafe_load_error(what):+ import pickle+ return pickle.UnpicklingError(+ f"Unsloth: refused to {what} inside a torch.export .pt2 archive. Unpickling it can run "+ f"arbitrary code (CVE-2026-4538). If you trust this file, set {_PT2_UNSAFE_LOAD_ENV}=1 "+ "and load it again."+ )+++def _pt2_loader_caller(frame):+ # A few frames up: other torch.load wrappers (the rng guard, later patches) sit in between.+ hops = 0+ while frame is not None and hops < 8:+ name = frame.f_globals.get("__name__", "")+ if name in _PT2_LOADER_MODULES:+ return name+ frame = frame.f_back+ hops += 1+ return ""+++class _Pt2PickleModule(types.ModuleType):+ """Stands in for `pickle` inside torch.export.pt2_archive._package: loads is refused."""++ def __init__(self, real):+ super().__init__(real.__name__)+ self._unsloth_pt2_real_pickle = real++ def __getattr__(self, name):+ return getattr(self._unsloth_pt2_real_pickle, name)++ def loads(self, *args, **kwargs):+ if _pt2_unsafe_load_allowed():+ return self._unsloth_pt2_real_pickle.loads(*args, **kwargs)+ raise _pt2_unsafe_load_error("unpickle an opaque object")+++_PT2_PACKAGE_MODULE = "torch.export.pt2_archive._package"+_PT2_FINDER_SENTINEL = "_unsloth_pt2_package_finder"+++def _install_pt2_pickle_proxy(package):+ real_pickle = getattr(package, "pickle", None)+ # Marker, not isinstance: a reloaded import_fixes defines a new _Pt2PickleModule class.+ already = hasattr(real_pickle, "_unsloth_pt2_real_pickle")+ if isinstance(real_pickle, types.ModuleType) and not already:+ package.pickle = _Pt2PickleModule(real_pickle)+ return True+ return False+++class _Pt2PackageLoader(importlib.abc.Loader):+ __slots__ = ("_loader",)++ def __init__(self, loader):+ self._loader = loader++ def create_module(self, spec):+ create_module = getattr(self._loader, "create_module", None)+ return None if create_module is None else create_module(spec)++ def exec_module(self, module):+ self._loader.exec_module(module)+ _install_pt2_pickle_proxy(module)++ def __getattr__(self, name):+ return getattr(self._loader, name)+++class _Pt2PackageFinder(importlib.abc.MetaPathFinder):+ """Installs the pickle stand-in right after torch.export.pt2_archive._package first runs."""++ def __init__(self):+ setattr(self, _PT2_FINDER_SENTINEL, True)++ def find_spec(+ self,+ fullname,+ path = None,+ target = None,+ ):+ if fullname != _PT2_PACKAGE_MODULE:+ return None+ for finder in sys.meta_path:+ if finder is self or getattr(finder, _PT2_FINDER_SENTINEL, False):+ continue+ find_spec = getattr(finder, "find_spec", None)+ try:+ spec = find_spec(fullname, path, target) if find_spec is not None else None+ except Exception:+ spec = None+ if spec is not None:+ if spec.loader is not None and hasattr(spec.loader, "exec_module"):+ spec.loader = _Pt2PackageLoader(spec.loader)+ return spec+ return None+++def patch_torch_export_pt2_unsafe_load():+ """CVE-2026-4538 (pytorch/pytorch#176791 never merged): torch.export.load unpickles .pt2+ payloads with weights_only=False. Those loads become weights_only=True and the bare+ pickle.loads for opaque constants is refused; tensors and ordinary exported programs still+ load. UNSLOTH_ALLOW_UNSAFE_PT2_LOAD=1 restores torch's behaviour."""+ try:+ import torch+ except Exception:+ return+ patched = False+ load = torch.load+ if not getattr(load, "_unsloth_pt2_guard", False):++ @functools.wraps(load)+ def _pt2_guarded_torch_load(*args, **kwargs):+ if kwargs.get("weights_only") is not False or _pt2_unsafe_load_allowed():+ return load(*args, **kwargs)+ if _pt2_loader_caller(sys._getframe(1)) not in _PT2_LOADER_MODULES:+ return load(*args, **kwargs)+ kwargs["weights_only"] = True+ try:+ return load(*args, **kwargs)+ except Exception as error:+ raise _pt2_unsafe_load_error("unpickle a non-tensor payload") from error++ _pt2_guarded_torch_load._unsloth_pt2_guard = True+ # Carry the rng guard's markers so patch_unsafe_trainer_rng_load stays idempotent.+ for attribute in ("_unsloth_rng_guard", "_unsloth_rng_flag"):+ if hasattr(load, attribute):+ setattr(_pt2_guarded_torch_load, attribute, getattr(load, attribute))+ torch.load = _pt2_guarded_torch_load+ patched = True++ # Opaque constants bypass torch.load: patch _package now, or lazily so import stays cheap.+ package = sys.modules.get(_PT2_PACKAGE_MODULE)+ if package is not None:+ patched = _install_pt2_pickle_proxy(package) or patched+ elif not any(getattr(finder, _PT2_FINDER_SENTINEL, False) for finder in sys.meta_path):+ sys.meta_path.insert(0, _Pt2PackageFinder())+ patched = True+ if patched:+ logger.info("Unsloth: Hardened torch.export .pt2 loading (CVE-2026-4538).")++def _is_custom_torch_build(raw_version_str):"""Check if a raw version string indicates a custom or source build.