-
Notifications
You must be signed in to change notification settings - Fork 480
[vulnops][data] fix: Replace unsafe pickle.loads with restricted unpickler in Qwen VL pipeline #3139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[vulnops][data] fix: Replace unsafe pickle.loads with restricted unpickler in Qwen VL pipeline #3139
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9073c70
[data] fix: Replace unsafe pickle.loads with restricted unpickler in …
yaoyu-33 666aa36
Merge branch 'main' into security/fix-qwenvl-pickle
yaoyu-33 858ed24
fix: address review comments for safe pickle module
yaoyu-33 ef47a9d
fix: remove unused warnings import (ruff F401)
yaoyu-33 f795f1a
fix(test): replace hardcoded pickle payloads with pickle.dumps for po…
yaoyu-33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import io | ||
| import pickle | ||
| from types import MappingProxyType | ||
|
|
||
|
|
||
| class _RestrictedUnpickler(pickle.Unpickler): | ||
| """Unpickler that only allows safe built-in types to prevent arbitrary code execution.""" | ||
|
|
||
| _SAFE_MODULES = MappingProxyType( | ||
| { | ||
| "builtins": frozenset( | ||
| { | ||
| "list", | ||
| "dict", | ||
| "tuple", | ||
| "set", | ||
| "frozenset", | ||
| "bytes", | ||
| "bytearray", | ||
| "str", | ||
| "int", | ||
| "float", | ||
| "bool", | ||
| "complex", | ||
| "slice", | ||
| "range", | ||
| "NoneType", | ||
| } | ||
| ), | ||
| "collections": frozenset({"OrderedDict"}), | ||
| } | ||
| ) | ||
|
|
||
| def find_class(self, module: str, name: str) -> type: | ||
| if module in self._SAFE_MODULES and name in self._SAFE_MODULES[module]: | ||
| return super().find_class(module, name) | ||
| raise pickle.UnpicklingError( | ||
| f"Restricted unpickler refused to load '{module}.{name}'. Only safe built-in types are allowed." | ||
| ) | ||
|
|
||
|
|
||
| def safe_pickle_load(fp) -> object: | ||
| """Deserialize from a file using a restricted unpickler that only allows safe types.""" | ||
| return _RestrictedUnpickler(fp).load() | ||
|
|
||
|
|
||
| def safe_pickle_loads(data: bytes) -> object: | ||
| """Deserialize pickle data using a restricted unpickler that only allows safe types.""" | ||
| return _RestrictedUnpickler(io.BytesIO(data)).load() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for safe_pickle module.""" | ||
|
|
||
| import io | ||
| import pickle | ||
| from collections import OrderedDict | ||
|
|
||
| import pytest | ||
|
|
||
| from megatron.bridge.utils.safe_pickle import safe_pickle_load, safe_pickle_loads | ||
|
|
||
|
|
||
| class TestSafePickleRoundTrip: | ||
| """Verify that safe types round-trip correctly.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "obj", | ||
| [ | ||
| [1, 2, 3], | ||
| {"key": "value", "num": 42}, | ||
| (1, "a", 3.14), | ||
| {1, 2, 3}, | ||
| frozenset([4, 5, 6]), | ||
| b"binary data", | ||
| bytearray(b"mutable bytes"), | ||
| "hello", | ||
| 42, | ||
| 3.14, | ||
| True, | ||
| complex(1, 2), | ||
| slice(1, 10, 2), | ||
| range(5), | ||
| None, | ||
| OrderedDict([("a", 1), ("b", 2)]), | ||
| ], | ||
| ids=lambda x: type(x).__name__, | ||
| ) | ||
| def test_allowed_types(self, obj): | ||
| data = pickle.dumps(obj) | ||
| result = safe_pickle_loads(data) | ||
| assert result == obj | ||
|
|
||
| def test_nested_structures(self): | ||
| obj = {"list": [1, 2, None], "nested": {"a": (True, 3.14)}, "bytes": b"\x00\x01"} | ||
| data = pickle.dumps(obj) | ||
| assert safe_pickle_loads(data) == obj | ||
|
|
||
| def test_safe_pickle_load_from_file(self): | ||
| obj = {"key": [1, 2, 3]} | ||
| buf = io.BytesIO() | ||
| pickle.dump(obj, buf) | ||
| buf.seek(0) | ||
| assert safe_pickle_load(buf) == obj | ||
|
|
||
|
|
||
| class TestSafePickleRejectsUnsafe: | ||
| """Verify that disallowed types are rejected.""" | ||
|
|
||
| def test_rejects_eval(self): | ||
| data = pickle.dumps(eval) # noqa: S301 | ||
| with pytest.raises(pickle.UnpicklingError, match="Restricted unpickler refused"): | ||
| safe_pickle_loads(data) | ||
|
|
||
| def test_rejects_os_system(self): | ||
| import os | ||
|
|
||
| data = pickle.dumps(os.system) | ||
| with pytest.raises(pickle.UnpicklingError, match="Restricted unpickler refused"): | ||
| safe_pickle_loads(data) | ||
|
|
||
| def test_rejects_subprocess(self): | ||
| import subprocess | ||
|
|
||
| data = pickle.dumps(subprocess.Popen) | ||
| with pytest.raises(pickle.UnpicklingError, match="Restricted unpickler refused"): | ||
| safe_pickle_loads(data) | ||
|
|
||
| def test_rejects_builtins_type(self): | ||
| # type(None) pickles as builtins.type — should be rejected | ||
| data = pickle.dumps(type(None)) | ||
| with pytest.raises(pickle.UnpicklingError, match="Restricted unpickler refused"): | ||
| safe_pickle_loads(data) | ||
|
|
||
|
|
||
| class TestAllowlistImmutability: | ||
| """Verify the allowlist cannot be mutated at runtime.""" | ||
|
|
||
| def test_cannot_mutate_modules(self): | ||
| from megatron.bridge.utils.safe_pickle import _RestrictedUnpickler | ||
|
|
||
| with pytest.raises(TypeError): | ||
| _RestrictedUnpickler._SAFE_MODULES["os"] = frozenset({"system"}) | ||
|
|
||
| def test_cannot_mutate_allowed_names(self): | ||
| from megatron.bridge.utils.safe_pickle import _RestrictedUnpickler | ||
|
|
||
| with pytest.raises((TypeError, AttributeError)): | ||
| _RestrictedUnpickler._SAFE_MODULES["builtins"].add("eval") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.