-
Notifications
You must be signed in to change notification settings - Fork 29
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
Create rule S6985 : Usage of "torch.load" can lead to untrusted code execution #3976
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a1abdd
Create rule S6985
ghislainpiot 2415609
add implementation details
ghislainpiot e8c1bc1
Address review
ghislainpiot cbaa479
Update rule to include details about the wheights_only parameter
Seppli11 b9481e5
Remove unnecessary example
Seppli11 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 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,2 @@ | ||
{ | ||
} |
This file contains 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,25 @@ | ||
{ | ||
"title": "Usage of \"torch.load\" can lead to untrusted code execution", | ||
"type": "SECURITY_HOTSPOT", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "15min" | ||
}, | ||
"tags": [ | ||
"pytorch", | ||
"machine-learning" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6985", | ||
"sqKey": "S6985", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "infeasible", | ||
"code": { | ||
"impacts": { | ||
"SECURITY": "HIGH" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains 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,64 @@ | ||
This rule raises an issue when `pytorch.load` is used to load a model. | ||
|
||
== Why is this an issue? | ||
|
||
In PyTorch, it is common to load serialized models using the `torch.load` function. | ||
Under the hood, `torch.load` uses the `pickle` library to load the model and the weights. | ||
If the model comes from an untrusted source, an attacker could inject a malicious payload which would be executed during the deserialization. | ||
|
||
== How to fix it | ||
|
||
Use a safer alternative to load the model, such as `safetensors.torch.load_model`. Alternatively, PyTorch can be instructed to only load | ||
the weights by setting the parameter `weights_only=True`. This avoids the use of the `pickle` library and is therefore safe. Note that the | ||
use of `weights_only` requires saving only the `state_dict` of a model instead of the whole model. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
import torch | ||
|
||
model = torch.load('model.pth') # Noncompliant: torch.load is used to load the model | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
import torch | ||
import safetensors | ||
|
||
model = MyModel() | ||
safetensors.torch.load_model(model, 'model.pth') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I would only provide the code that would fix the issue in the |
||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
|
||
* Pytorch documentation: https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-entire-model[Save/Load Entire Model] | ||
|
||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
(visible only on this page) | ||
|
||
== Implementation specification | ||
|
||
All usages of torch.load | ||
|
||
=== Message | ||
|
||
Primary : Replace this call with a safe alternative | ||
|
||
|
||
=== Issue location | ||
|
||
Primary : name of the function call | ||
|
||
=== Quickfix | ||
|
||
No | ||
|
||
endif::env-github,rspecator-view[] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could add just a small introduction line.
In PyTorch it is common practice to load a serialized model
or something like that.