Arbitrary Code Execution in MONAI NumpyReader
The MONAI library contains a hardcoded insecure deserialization vulnerability in NumpyReader, allowing arbitrary code execution when processing malicious .npy or .npz files.
The NumpyReader class in the MONAI medical imaging framework (specifically monai/data/image_reader.py) contains a critical security flaw where numpy.load is invoked with allow_pickle=True. This parameter is hardcoded and cannot be overridden by end users via keyword arguments. Because the LoadImage transform automatically selects NumpyReader for all .npy and .npz files, any automated data pipeline or dataset processing workflow - including PersistentDataset or CacheDataset - becomes a vector for arbitrary code execution. The vulnerability stems from Python's pickle protocol, which can be leveraged to execute arbitrary code during the deserialization of untrusted objects within a data file. This vulnerability affects all MONAI versions prior to 1.6.0.
Attack Chain
- Attacker creates a malicious serialized Python object using the
__reduce__method to define the payload (e.g.,os.systemcommands). - Attacker writes this object to a
.npyor.npzfile usingnp.save. - Attacker poisons a shared research dataset or contributes the malicious file to a public repository, tutorial, or MONAI bundle.
- Victim downloads or maps the malicious dataset to their local environment or server.
- Victim initiates a MONAI data pipeline (e.g., training loop or inference script) that invokes
LoadImage. LoadImageidentifies the file extension and triggersNumpyReader.read().NumpyReadercallsnp.load(filename, allow_pickle=True).- Python deserializes the malicious payload, resulting in execution of attacker-supplied code within the process context.
Impact
Successful exploitation results in arbitrary code execution on the server or workstation processing the medical data. This poses significant risks in clinical and research environments, potentially leading to unauthorized access to protected health information (PHI), lateral movement within institutional networks, and compromise of high-performance computing clusters used for medical image analysis.
Recommendation
Prioritize immediate remediation and containment:
- Upgrade the MONAI library to version 1.6.0 or higher immediately.
- Audit all data pipelines and research environments for untrusted
.npyor.npzfile sources. - Isolate high-performance computing environments where shared datasets are processed from sensitive network segments until updates are verified.
- Restrict filesystem access to dataset directories to prevent unauthorized modification by low-privileged users or external contributors.
Immediate actions
Upgrade MONAI to version 1.6.0 or later across all training and inference environments.
Mitigations
Restrict read/write permissions on shared dataset repositories and storage locations.
Dataset poisoning vector