-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca6130b
commit a454852
Showing
3 changed files
with
62 additions
and
0 deletions.
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,35 @@ | ||
This rule raises an issue when `pytorch.load` is used to load a model. | ||
|
||
== Why is this an issue? | ||
|
||
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`. | ||
=== 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') | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
|
||
* Pytorch documentation: https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-entire-model[Save/Load Entire Model] |