-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Danger of arbitrary code execution by torch.load()
#193
Comments
torch.load()
The way of fixing/mitigating this is to use a custom unpickler with torch.load that only allows certain safe classes to be used. |
EDIT: For some reason, in my case, @BlueAmulet code worked or not, depending on the environnment (possibly due to different torch versions). DISCLAIMER: I'm not expert in security. So, take what I say with caution. NOTE: There is the picklescan python library ( Indeed, ML models can contain executable code. https://www.youtube.com/watch?v=2ethDz9KnLk He also provides a link to a (perfectly safe) toy model to illustrate the point. When you load the model, it opens your default browser, just to show that it launched a shell process. Now, since these models will be loaded in a python environnment, you could look for instructions like "import", "exec", "os.popen", "subprocess", "os.system" and things like that. For example, in linux (you'll have to figure out how to do that on Windows, or Mac by yourself), the following command when issued on the toy model provided by Yannick looks for all strings in the model and searches for given keywords.
In this case, it spits out:
Which, in a real case, should immediately raise your suspicion. Detection with picklescan:
results in:
So, picklescan correctly detects it as malicious. Is it enough to be perfectly safe? Probably not. Hugginface is aware of the threat so they scan the files. But, if you download models from dubious places you might have a bad surprise. Now, to go back to the code mentionned by @BlueAmulet :
The result (I tested it): It opens the browser. So, it doesn't work (torch version: Here is the python script to reproduce it. Of course, you need the toy model by Yannick Kilcher:
EDIT:
|
Cannot reproduce. It correctly returns an error. Did you really try it??? |
I think the BlueAmulet example says almost the same thing as the official Python documentation. |
Absolutely, I executed the code and the browser opens to the page: https://www.ykilcher.com/pickle Strange... Could you paste returned error in your case? But I can prove it. I recorded my Desktop. Link to video: |
Am I doing something wrong? Strange...... |
Weird indeed...
Is it the same for you? |
Yes. |
Thanks for your verification. The post does not need to be deleted. (Rather why did you think that?) Additional investigations are needed. |
So, to get pickle version, the correct way is: But: I have torch version 1.13.1+cu117 in my base env and version 2.0.0+cu117 in my so-vits-svc-fork |
Note: Both torch==1.13.1 and torch==2.0.0 allow for code execution when loading a model with torch.load() method. The "malicious" test model is a toy model by Yannick Kilcher.
CONCLUSION: The problem is that a lot of ML related repositories (that are not necessarily very old) have a torch version lower that 2.0.0 explicitely specified in their requirements... (NOTE: both torch versions use pickle version 4.0)
|
Thank you for a perfect survey. I had no idea that PyTorch had let such a serious problem go so far...... |
As of pytorch-1.13 there is |
Good to know that's now a thing, the unpickler idea I referenced at the beginning of this discussion was tested back when pytorch 1.6.0 was latest. It seems like when weights_only was added they accidentally broke pickle_module pytorch/pytorch#88438 weights_only is simpler to add in, and works starting with 1.13.0 |
Not resolved only in cluster model loading |
Would it be possible to use safetensors instead? https://github.com/huggingface/safetensors |
Do not use untrusted models, as there is currently no way to address this.
The text was updated successfully, but these errors were encountered: