From 9602bc00a8331790ace8054b8187972acb2f47a4 Mon Sep 17 00:00:00 2001 From: codeasier Date: Wed, 19 Aug 2026 22:32:26 +0800 Subject: [PATCH 1/3] feat: restrict pickle loading to builtins with SafeUnpickler (#99) Co-authored-by: Cursor --- README.md | 3 +- README_zh.md | 2 +- docs/en/database.md | 3 +- docs/en/quickstart.md | 3 +- docs/en/splitting.md | 4 +- docs/zh/database.md | 2 +- docs/zh/quickstart.md | 2 +- docs/zh/splitting.md | 2 +- src/pt_snap_cli/snapshot/PROVENANCE.md | 8 ++++ src/pt_snap_cli/snapshot/util/file_util.py | 31 +++++++++++++- tests/core/test_import_service.py | 13 ++++++ tests/core/test_split_service.py | 11 +++++ tests/snapshot/test_file_util.py | 49 ++++++++++++++++++++++ tests/test_governance.py | 11 ++++- 14 files changed, 131 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2f8103b..f6edf1d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ pip install -e . To start from a raw PyTorch memory snapshot, import the pickle into a SnapshotDB with pt-snap's built-in snapshot support, then list the available query templates: > **Security warning:** Import only pickle snapshots from a trusted source. Pickle -> deserialization can execute arbitrary code. `pt-snap import` is not a sandbox. +> deserialization can execute arbitrary code. The loader rejects non-`builtins` +> global objects, but `pt-snap import` is not a sandbox. ```bash pt-snap import snapshot.pkl diff --git a/README_zh.md b/README_zh.md index 0c6dd0d..ec177c9 100644 --- a/README_zh.md +++ b/README_zh.md @@ -15,7 +15,7 @@ pip install -e . ## 快速开始 > **安全警告:** 只导入来自可信来源的 pickle 快照。反序列化 pickle 可能执行任意代码; -> `pt-snap import` 不是沙箱。 +> 实现会拒绝非 builtins 全局对象,但 `pt-snap import` 不是沙箱。 ```bash # 从原始 pickle 导入;相同内容和配置会自动复用已有 DB diff --git a/docs/en/database.md b/docs/en/database.md index 9aea644..e19f28d 100644 --- a/docs/en/database.md +++ b/docs/en/database.md @@ -17,7 +17,8 @@ SnapshotDB is the SQLite database format for persisting PyTorch memory profiling `pt-snap` analyzes SQLite SnapshotDB files. If you start with a raw PyTorch memory snapshot pickle, import the snapshot with the built-in backend: > **Security warning:** Import trusted pickle files only. Pickle deserialization -> can execute arbitrary code. `pt-snap import` is not a sandbox. +> can execute arbitrary code. The loader rejects non-`builtins` global objects, +> but `pt-snap import` is not a sandbox. ```bash pt-snap import snapshot.pkl diff --git a/docs/en/quickstart.md b/docs/en/quickstart.md index 1ede39e..f7344de 100644 --- a/docs/en/quickstart.md +++ b/docs/en/quickstart.md @@ -19,7 +19,8 @@ pip install -e . If you have a raw `.pkl` memory snapshot, import it first with the built-in backend: > **Security warning:** Use trusted pickle input only. Deserializing a pickle can -> execute arbitrary code. `pt-snap import` is not a sandbox. +> execute arbitrary code. The loader rejects non-`builtins` global objects, but +> `pt-snap import` is not a sandbox. ```bash pt-snap import snapshot.pkl diff --git a/docs/en/splitting.md b/docs/en/splitting.md index b6b3520..fe60677 100644 --- a/docs/en/splitting.md +++ b/docs/en/splitting.md @@ -8,8 +8,8 @@ event range. It does not truncate trace arrays, read focus, update focus, or cre a SnapshotDB. Import a produced pickle separately when you need SQLite queries. > **Security warning:** Use trusted pickle input only. Deserializing the source -> snapshot and any pickle slice can execute arbitrary code. `pt-snap split` is -> not a sandbox. +> snapshot and any pickle slice can execute arbitrary code. The loader rejects +> non-`builtins` global objects, but `pt-snap split` is not a sandbox. ## Command Contract diff --git a/docs/zh/database.md b/docs/zh/database.md index c5382e9..5c084f9 100644 --- a/docs/zh/database.md +++ b/docs/zh/database.md @@ -16,7 +16,7 @@ SnapshotDB 是内存快照数据的 SQLite 数据库存储格式,用于持久 如果输入是 PyTorch 原始 `.pkl` 内存快照,可以直接导入: > **安全警告:** 只能导入可信的 pickle 文件。反序列化 pickle 可能执行任意代码; -> `pt-snap import` 不是沙箱。 +> 实现会拒绝非 builtins 全局对象,但 `pt-snap import` 不是沙箱。 ```bash pt-snap import snapshot.pkl diff --git a/docs/zh/quickstart.md b/docs/zh/quickstart.md index acba468..7298731 100644 --- a/docs/zh/quickstart.md +++ b/docs/zh/quickstart.md @@ -19,7 +19,7 @@ pip install -e . 如果你手上是原始 `.pkl` 内存快照,请先使用内建后端导入: > **安全警告:** 只能使用可信的 pickle 输入。反序列化 pickle 可能执行任意代码; -> `pt-snap import` 不是沙箱。 +> 实现会拒绝非 builtins 全局对象,但 `pt-snap import` 不是沙箱。 ```bash pt-snap import snapshot.pkl diff --git a/docs/zh/splitting.md b/docs/zh/splitting.md index 13c906d..8087627 100644 --- a/docs/zh/splitting.md +++ b/docs/zh/splitting.md @@ -7,7 +7,7 @@ focus,也不会生成 SnapshotDB。如需使用 SQLite 查询,请单独导入生成的 pickle 切片。 > **安全警告:** 只能使用可信的 pickle 输入。反序列化源快照或任意 pickle 切片都可能 -> 执行任意代码;`pt-snap split` 不是沙箱。 +> 执行任意代码;实现会拒绝非 builtins 全局对象,但 `pt-snap split` 不是沙箱。 ## 命令契约 diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index 4f63406..4c8684e 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -138,6 +138,14 @@ The upstream standalone split frontend is deliberately excluded, not deferred: read `device_free`, and mapped `"oom"` to action value `8` so import persists the event as an integer. Replay still skips OOM. This is a local first-party bugfix; audited upstream mappings and licensing evidence are unchanged. +- On 2026-08-19, issue #99 replaced unrestricted `pickle.load()` in + `snapshot/util/file_util.py` with `SafeUnpickler`. Unpickling now allows only + `builtins` container and scalar types (`dict`/`list`/`tuple`/`set`/`str`/ + `int`/`float`/`bool`/`bytes`/`NoneType`); any other `module.name` raises + `UnpicklingError`. Import and split share this loader via + `representation.load_pickle_representation`. The change tightens the + deserialization allowlist; pickle loading remains not a sandbox. Corrupt + streams still map to the established generic `UnpicklingError` wrapper. ## Migration toolchain diff --git a/src/pt_snap_cli/snapshot/util/file_util.py b/src/pt_snap_cli/snapshot/util/file_util.py index 7980a8a..e0594a5 100644 --- a/src/pt_snap_cli/snapshot/util/file_util.py +++ b/src/pt_snap_cli/snapshot/util/file_util.py @@ -10,6 +10,33 @@ from typing import Any +class SafeUnpickler(pickle.Unpickler): + """Allow only builtins container and scalar types during unpickling.""" + + ALLOWED_CLASSES = { + "builtins": { + "dict", + "list", + "tuple", + "set", + "str", + "int", + "float", + "bool", + "bytes", + "NoneType", + }, + } + + def find_class(self, module: str, name: str): + if module in self.ALLOWED_CLASSES and name in self.ALLOWED_CLASSES[module]: + return super().find_class(module, name) + raise pickle.UnpicklingError( + f"Unsafe pickle: global '{module}.{name}' is not allowed. " + "Only basic Python types are permitted for security." + ) + + def load_pickle_to_dict(pickle_file: Path) -> dict: """ 从指定路径加载 pickle 文件,并确保其内容为 dict 类型。 @@ -23,14 +50,14 @@ def load_pickle_to_dict(pickle_file: Path) -> dict: Raises: FileNotFoundError: 文件不存在 ValueError: 文件内容不是 dict 类型 - pickle.UnpicklingError: 反序列化失败(如文件损坏或非 pickle 格式) + pickle.UnpicklingError: 反序列化失败(文件损坏、非 pickle 格式,或不在 allowlist 的全局对象) """ if not pickle_file.is_file(): raise FileNotFoundError(f"Cannot found pickle file: {pickle_file}") try: with open(pickle_file, "rb") as f: - data = pickle.load(f) + data = SafeUnpickler(f).load() except Exception: # Preserve the established exception context and corrupt-pickle behavior. raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}") # noqa: B904 diff --git a/tests/core/test_import_service.py b/tests/core/test_import_service.py index dac351b..77901ec 100644 --- a/tests/core/test_import_service.py +++ b/tests/core/test_import_service.py @@ -16,6 +16,7 @@ SnapshotFileInvalidError, ) from pt_snap_cli.core.models import ImportOptions +from tests.snapshot.test_file_util import UNSAFE_PICKLE_PAYLOAD, write_unsafe_reduce_pickle FIXTURE_DIR = Path(__file__).parents[1] / "fixtures" / "snapshots" EMPTY_CACHE_SNAPSHOT = FIXTURE_DIR / "snapshot_with_empty_cache.pkl" @@ -122,6 +123,18 @@ def test_import_snapshot_file_invalid_corrupt_pickle(tmp_path: Path) -> None: assert not (tmp_path / "corrupt.pkl.db").exists() +def test_import_rejects_pickle_with_non_allowlisted_global(tmp_path: Path) -> None: + import_service_cls = _import_service_type() + service = import_service_cls() + snapshot_file = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") + + with pytest.raises(ImportExecutionError, match=r"backend (failed|reported failure)"): + service.import_snapshot(ImportOptions(snapshot_file=snapshot_file)) + + assert UNSAFE_PICKLE_PAYLOAD["executed"] is False + assert not (tmp_path / "evil.pkl.db").exists() + + def test_failed_reimport_preserves_existing_database(tmp_path: Path) -> None: import_service_cls = _import_service_type() service = import_service_cls() diff --git a/tests/core/test_split_service.py b/tests/core/test_split_service.py index 4f2c483..243aee6 100644 --- a/tests/core/test_split_service.py +++ b/tests/core/test_split_service.py @@ -31,6 +31,7 @@ from pt_snap_cli.snapshot.tools.slice_dump.hooker import SliceDumpHooker from tests.snapshot.golden_observations import ACTION_VALUE_MAP from tests.snapshot.helpers import FIXTURE_DIR, assert_valid_snapshot +from tests.snapshot.test_file_util import UNSAFE_PICKLE_PAYLOAD, write_unsafe_reduce_pickle MULTI_DEVICE = FIXTURE_DIR / "snapshot_with_multi_devices.pkl" @@ -383,6 +384,16 @@ def test_malformed_pickle_is_load_engine_phase(tmp_path: Path) -> None: assert not (tmp_path / "split").exists() +def test_unsafe_pickle_global_is_load_engine_phase(tmp_path: Path) -> None: + source = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") + + with pytest.raises(SplitError, match="load/engine") as caught: + SplitService().split(_options(tmp_path, snapshot_file=source)) + assert str(source) in str(caught.value) + assert UNSAFE_PICKLE_PAYLOAD["executed"] is False + assert not (tmp_path / "split").exists() + + def test_no_nonempty_device_is_device_phase(tmp_path: Path) -> None: source = tmp_path / "empty.pkl" source.write_bytes(pickle.dumps({"segments": [], "device_traces": [[], []]})) diff --git a/tests/snapshot/test_file_util.py b/tests/snapshot/test_file_util.py index ccbdcdb..42e1d22 100644 --- a/tests/snapshot/test_file_util.py +++ b/tests/snapshot/test_file_util.py @@ -9,6 +9,25 @@ load_pickle_to_dict, save_dict_to_pickle, ) +from tests.snapshot.helpers import FIXTURE_DIR + +UNSAFE_PICKLE_PAYLOAD = {"executed": False} + + +def unsafe_pickle_payload() -> dict: + UNSAFE_PICKLE_PAYLOAD["executed"] = True + return {"pwned": True} + + +class UnsafeReducePayload: + def __reduce__(self) -> tuple[object, tuple[()]]: + return (unsafe_pickle_payload, ()) + + +def write_unsafe_reduce_pickle(path: Path) -> Path: + UNSAFE_PICKLE_PAYLOAD["executed"] = False + path.write_bytes(pickle.dumps({"payload": UnsafeReducePayload()})) + return path def test_save_and_load_pickle_round_trip(tmp_path: Path): @@ -35,6 +54,36 @@ def test_load_pickle_to_dict_rejects_missing_file(tmp_path: Path): load_pickle_to_dict(tmp_path / "missing.pkl") +def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_path: Path): + target = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") + + with pytest.raises(pickle.UnpicklingError): + load_pickle_to_dict(target) + + assert UNSAFE_PICKLE_PAYLOAD["executed"] is False + + +def test_load_pickle_to_dict_rejects_corrupt_stream(tmp_path: Path): + target = tmp_path / "corrupt.pkl" + target.write_bytes(b"\x80\x04not a valid pickle stream") + + with pytest.raises(pickle.UnpicklingError, match="Cannot load pickle file"): + load_pickle_to_dict(target) + + +@pytest.mark.parametrize( + "fixture_name", + ( + "snapshot_with_empty_cache.pkl", + "snapshot_expandable.pkl", + "snapshot_with_multi_devices.pkl", + ), +) +def test_load_pickle_to_dict_accepts_fixture_snapshots(fixture_name: str): + data = load_pickle_to_dict(FIXTURE_DIR / fixture_name) + assert isinstance(data, dict) + + def test_save_dict_to_pickle_rejects_non_dict(tmp_path: Path): with pytest.raises(TypeError): save_dict_to_pickle([1, 2, 3], tmp_path / "bad.pkl") diff --git a/tests/test_governance.py b/tests/test_governance.py index c246248..f3e3410 100644 --- a/tests/test_governance.py +++ b/tests/test_governance.py @@ -225,11 +225,18 @@ def test_bilingual_docs_record_split_and_pickle_security_contract() -> None: assert "__device-__slice-." in text assert "arbitrary code" in text.lower() or "任意代码" in text assert "not a sandbox" in text.lower() or "不是沙箱" in text - - for path in (Path("docs/en/quickstart.md"), Path("docs/zh/quickstart.md")): + assert "builtins" in text.lower() + + for path in ( + Path("docs/en/quickstart.md"), + Path("docs/zh/quickstart.md"), + Path("docs/en/database.md"), + Path("docs/zh/database.md"), + ): text = path.read_text() assert "arbitrary code" in text.lower() or "任意代码" in text assert "not a sandbox" in text.lower() or "不是沙箱" in text + assert "builtins" in text.lower() def test_github_issue_forms_and_default_pr_template_use_supported_paths() -> None: From 508d5dfb9b9b73ced3d8c3f33eb8ab86a29de3b0 Mon Sep 17 00:00:00 2001 From: codeasier Date: Wed, 19 Aug 2026 23:59:37 +0800 Subject: [PATCH 2/3] fix: preserve pickle allowlist rejection in UnpicklingError Co-authored-by: Cursor --- src/pt_snap_cli/snapshot/PROVENANCE.md | 3 +++ src/pt_snap_cli/snapshot/util/file_util.py | 2 ++ tests/snapshot/test_file_util.py | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index 4c8684e..c531b51 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -146,6 +146,9 @@ The upstream standalone split frontend is deliberately excluded, not deferred: `representation.load_pickle_representation`. The change tightens the deserialization allowlist; pickle loading remains not a sandbox. Corrupt streams still map to the established generic `UnpicklingError` wrapper. +- On 2026-08-19, PR #102 follow-up kept that wrapper prefix and path, and chained + the original `UnpicklingError` so allowlist rejections keep the rejected + `module.name` in the message and `__cause__`. ## Migration toolchain diff --git a/src/pt_snap_cli/snapshot/util/file_util.py b/src/pt_snap_cli/snapshot/util/file_util.py index e0594a5..0d6d494 100644 --- a/src/pt_snap_cli/snapshot/util/file_util.py +++ b/src/pt_snap_cli/snapshot/util/file_util.py @@ -58,6 +58,8 @@ def load_pickle_to_dict(pickle_file: Path) -> dict: try: with open(pickle_file, "rb") as f: data = SafeUnpickler(f).load() + except pickle.UnpicklingError as exc: + raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}: {exc}") from exc except Exception: # Preserve the established exception context and corrupt-pickle behavior. raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}") # noqa: B904 diff --git a/tests/snapshot/test_file_util.py b/tests/snapshot/test_file_util.py index 42e1d22..f88f7d4 100644 --- a/tests/snapshot/test_file_util.py +++ b/tests/snapshot/test_file_util.py @@ -1,9 +1,11 @@ +import io import pickle from pathlib import Path import pytest from pt_snap_cli.snapshot.util.file_util import ( + SafeUnpickler, check_dir_valid, check_file_valid, load_pickle_to_dict, @@ -57,19 +59,35 @@ def test_load_pickle_to_dict_rejects_missing_file(tmp_path: Path): def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_path: Path): target = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") - with pytest.raises(pickle.UnpicklingError): + with pytest.raises( + pickle.UnpicklingError, + match=r"Unsafe pickle: global 'tests\.snapshot\.test_file_util\.unsafe_pickle_payload'", + ): load_pickle_to_dict(target) assert UNSAFE_PICKLE_PAYLOAD["executed"] is False +@pytest.mark.parametrize("name", ("eval", "exec", "open")) +def test_safe_unpickler_rejects_disallowed_builtin_names(name: str): + unpickler = SafeUnpickler(io.BytesIO(b"")) + + with pytest.raises(pickle.UnpicklingError, match=rf"Unsafe pickle: global 'builtins\.{name}'"): + unpickler.find_class("builtins", name) + + def test_load_pickle_to_dict_rejects_corrupt_stream(tmp_path: Path): target = tmp_path / "corrupt.pkl" target.write_bytes(b"\x80\x04not a valid pickle stream") - with pytest.raises(pickle.UnpicklingError, match="Cannot load pickle file"): + with pytest.raises(pickle.UnpicklingError, match="Cannot load pickle file") as caught: load_pickle_to_dict(target) + message = str(caught.value) + assert str(target) in message + assert "Unsafe pickle" not in message + assert isinstance(caught.value.__cause__, pickle.UnpicklingError) + @pytest.mark.parametrize( "fixture_name", From a9f209af6e7dcbb6ac545384ea7868298410ccd3 Mon Sep 17 00:00:00 2001 From: codeasier Date: Thu, 20 Aug 2026 01:31:23 +0800 Subject: [PATCH 3/3] fix: distinguish unsafe pickle from corrupt streams Co-authored-by: Cursor --- src/pt_snap_cli/core/snapshot_import_backend.py | 3 +++ src/pt_snap_cli/core/split_service.py | 3 +++ src/pt_snap_cli/snapshot/PROVENANCE.md | 9 +++++++++ src/pt_snap_cli/snapshot/representation.py | 1 + .../snapshot/tools/adaptors/snapshot2db.py | 4 +++- src/pt_snap_cli/snapshot/util/file_util.py | 12 +++++++++--- tests/core/test_import_errors.py | 8 ++++++++ tests/core/test_import_service.py | 2 +- tests/core/test_split_service.py | 4 +++- tests/snapshot/test_file_util.py | 8 +++++--- 10 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/pt_snap_cli/core/snapshot_import_backend.py b/src/pt_snap_cli/core/snapshot_import_backend.py index ae60b68..0f671ec 100644 --- a/src/pt_snap_cli/core/snapshot_import_backend.py +++ b/src/pt_snap_cli/core/snapshot_import_backend.py @@ -8,6 +8,7 @@ from pathlib import Path from pt_snap_cli.core.errors import ImportExecutionError, ImportToolMissingError +from pt_snap_cli.snapshot.representation import UnsafePickleError class SnapshotImportBackend: @@ -34,6 +35,8 @@ def dump_to_db( dump_dir=str(tmp_dir), device=device, ) + except UnsafePickleError as exc: + raise ImportExecutionError(f"Snapshot pickle rejected: {exc}") from exc except (OSError, sqlite3.DatabaseError, pickle.UnpicklingError, ValueError) as exc: raise ImportExecutionError(f"Snapshot import backend failed: {exc}") from exc diff --git a/src/pt_snap_cli/core/split_service.py b/src/pt_snap_cli/core/split_service.py index 864cdba..0689239 100644 --- a/src/pt_snap_cli/core/split_service.py +++ b/src/pt_snap_cli/core/split_service.py @@ -14,6 +14,7 @@ from pt_snap_cli.core.errors import SplitError, SplitPhase from pt_snap_cli.core.models import SplitFormat, SplitOptions, SplitResult from pt_snap_cli.snapshot.representation import ( + UnsafePickleError, load_snapshot_representation, replay_snapshot, save_snapshot_representation, @@ -40,6 +41,8 @@ def split(self, options: SplitOptions) -> SplitResult: try: representation = load_snapshot_representation(source, "pickle") + except UnsafePickleError as exc: + self._fail("load/engine", source, f"unsafe pickle: {exc}", exc) except Exception as exc: self._fail("load/engine", source, str(exc), exc) diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index c531b51..1b18c57 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -149,6 +149,15 @@ The upstream standalone split frontend is deliberately excluded, not deferred: - On 2026-08-19, PR #102 follow-up kept that wrapper prefix and path, and chained the original `UnpicklingError` so allowlist rejections keep the rejected `module.name` in the message and `__cause__`. +- On 2026-08-20, PR #102 follow-up removed `NoneType` from `ALLOWED_CLASSES`. + `builtins.NoneType` is not an importable builtin (`hasattr(builtins, "NoneType")` + is false); `None` is encoded by the `NONE` opcode. A GLOBAL of + `builtins.NoneType` is now `UnsafePickleError` rather than an `AttributeError` + swallowed as a corrupt-stream wrapper. `UnsafePickleError` subclasses + `pickle.UnpicklingError`. Import maps it to `Snapshot pickle rejected:` and + split maps it to `unsafe pickle:` so allowlist rejection is distinct from + corrupt input. `dump()` re-raises `UnsafePickleError` instead of returning + false. ## Migration toolchain diff --git a/src/pt_snap_cli/snapshot/representation.py b/src/pt_snap_cli/snapshot/representation.py index 2b4dcef..1edd986 100644 --- a/src/pt_snap_cli/snapshot/representation.py +++ b/src/pt_snap_cli/snapshot/representation.py @@ -6,6 +6,7 @@ from typing import Any, Literal, cast from .simulate import AllocatorHooker, SimulateDeviceSnapshot, SimulateHooker +from .util.file_util import UnsafePickleError as UnsafePickleError from .util.file_util import load_pickle_to_dict, save_dict_to_pickle SnapshotFormat = Literal["pickle", "json"] diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py index 6d4cd69..3127cfd 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py @@ -3,7 +3,7 @@ from pathlib import Path from ...base import Block, BlockState, DeviceSnapshot, TraceEntry -from ...representation import load_snapshot_representation, replay_snapshot +from ...representation import UnsafePickleError, load_snapshot_representation, replay_snapshot from ...simulate import AllocatorHooker, SimulateHooker from ...util.logger import get_logger, set_global_log_file from .database import SnapshotDb, block2record, event2record @@ -138,6 +138,8 @@ def close(self, *, commit: bool = True): def dump(pickle_file: str, dump_file: str, device=None) -> bool: try: data = load_snapshot_representation(Path(pickle_file)) + except UnsafePickleError: + raise except Exception as e: dump_logger.error(f"Failed to load pickle file: {e}") return False diff --git a/src/pt_snap_cli/snapshot/util/file_util.py b/src/pt_snap_cli/snapshot/util/file_util.py index 0d6d494..0f24165 100644 --- a/src/pt_snap_cli/snapshot/util/file_util.py +++ b/src/pt_snap_cli/snapshot/util/file_util.py @@ -10,6 +10,10 @@ from typing import Any +class UnsafePickleError(pickle.UnpicklingError): + """Raised when unpickling requests a global outside the builtins allowlist.""" + + class SafeUnpickler(pickle.Unpickler): """Allow only builtins container and scalar types during unpickling.""" @@ -24,14 +28,13 @@ class SafeUnpickler(pickle.Unpickler): "float", "bool", "bytes", - "NoneType", }, } def find_class(self, module: str, name: str): if module in self.ALLOWED_CLASSES and name in self.ALLOWED_CLASSES[module]: return super().find_class(module, name) - raise pickle.UnpicklingError( + raise UnsafePickleError( f"Unsafe pickle: global '{module}.{name}' is not allowed. " "Only basic Python types are permitted for security." ) @@ -50,7 +53,8 @@ def load_pickle_to_dict(pickle_file: Path) -> dict: Raises: FileNotFoundError: 文件不存在 ValueError: 文件内容不是 dict 类型 - pickle.UnpicklingError: 反序列化失败(文件损坏、非 pickle 格式,或不在 allowlist 的全局对象) + pickle.UnpicklingError: 反序列化失败(文件损坏或非 pickle 格式) + UnsafePickleError: 请求了 allowlist 之外的全局对象 """ if not pickle_file.is_file(): raise FileNotFoundError(f"Cannot found pickle file: {pickle_file}") @@ -58,6 +62,8 @@ def load_pickle_to_dict(pickle_file: Path) -> dict: try: with open(pickle_file, "rb") as f: data = SafeUnpickler(f).load() + except UnsafePickleError: + raise except pickle.UnpicklingError as exc: raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}: {exc}") from exc except Exception: diff --git a/tests/core/test_import_errors.py b/tests/core/test_import_errors.py index 5f31034..c3a16a9 100644 --- a/tests/core/test_import_errors.py +++ b/tests/core/test_import_errors.py @@ -20,3 +20,11 @@ def test_snapshot_file_invalid_error_is_pt_snap_core() -> None: def test_import_execution_error_is_pt_snap_core() -> None: assert issubclass(ImportExecutionError, PtSnapCoreError) + + +def test_unsafe_pickle_error_is_unpickling_error() -> None: + from pickle import UnpicklingError + + from pt_snap_cli.snapshot.representation import UnsafePickleError + + assert issubclass(UnsafePickleError, UnpicklingError) diff --git a/tests/core/test_import_service.py b/tests/core/test_import_service.py index 77901ec..093ccf5 100644 --- a/tests/core/test_import_service.py +++ b/tests/core/test_import_service.py @@ -128,7 +128,7 @@ def test_import_rejects_pickle_with_non_allowlisted_global(tmp_path: Path) -> No service = import_service_cls() snapshot_file = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") - with pytest.raises(ImportExecutionError, match=r"backend (failed|reported failure)"): + with pytest.raises(ImportExecutionError, match=r"Snapshot pickle rejected:.*Unsafe pickle"): service.import_snapshot(ImportOptions(snapshot_file=snapshot_file)) assert UNSAFE_PICKLE_PAYLOAD["executed"] is False diff --git a/tests/core/test_split_service.py b/tests/core/test_split_service.py index 243aee6..356c765 100644 --- a/tests/core/test_split_service.py +++ b/tests/core/test_split_service.py @@ -387,8 +387,10 @@ def test_malformed_pickle_is_load_engine_phase(tmp_path: Path) -> None: def test_unsafe_pickle_global_is_load_engine_phase(tmp_path: Path) -> None: source = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") - with pytest.raises(SplitError, match="load/engine") as caught: + with pytest.raises(SplitError, match="unsafe pickle") as caught: SplitService().split(_options(tmp_path, snapshot_file=source)) + assert caught.value.phase == "load/engine" + assert "Unsafe pickle" in caught.value.detail assert str(source) in str(caught.value) assert UNSAFE_PICKLE_PAYLOAD["executed"] is False assert not (tmp_path / "split").exists() diff --git a/tests/snapshot/test_file_util.py b/tests/snapshot/test_file_util.py index f88f7d4..09cc06e 100644 --- a/tests/snapshot/test_file_util.py +++ b/tests/snapshot/test_file_util.py @@ -6,6 +6,7 @@ from pt_snap_cli.snapshot.util.file_util import ( SafeUnpickler, + UnsafePickleError, check_dir_valid, check_file_valid, load_pickle_to_dict, @@ -60,7 +61,7 @@ def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_p target = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") with pytest.raises( - pickle.UnpicklingError, + UnsafePickleError, match=r"Unsafe pickle: global 'tests\.snapshot\.test_file_util\.unsafe_pickle_payload'", ): load_pickle_to_dict(target) @@ -68,11 +69,11 @@ def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_p assert UNSAFE_PICKLE_PAYLOAD["executed"] is False -@pytest.mark.parametrize("name", ("eval", "exec", "open")) +@pytest.mark.parametrize("name", ("eval", "exec", "open", "NoneType")) def test_safe_unpickler_rejects_disallowed_builtin_names(name: str): unpickler = SafeUnpickler(io.BytesIO(b"")) - with pytest.raises(pickle.UnpicklingError, match=rf"Unsafe pickle: global 'builtins\.{name}'"): + with pytest.raises(UnsafePickleError, match=rf"Unsafe pickle: global 'builtins\.{name}'"): unpickler.find_class("builtins", name) @@ -86,6 +87,7 @@ def test_load_pickle_to_dict_rejects_corrupt_stream(tmp_path: Path): message = str(caught.value) assert str(target) in message assert "Unsafe pickle" not in message + assert not isinstance(caught.value, UnsafePickleError) assert isinstance(caught.value.__cause__, pickle.UnpicklingError)