Skip to content
RCE Write-up Advanced

My LLM Bug Bounty Journey on Hugging Face Hub via Protect AI

Unsafe pickle deserialisation in Hugging Face transformers models (transfo_xl and RagRetrieve) could be triggered remotely via the from_pretrained API, allowing an attacker to execute arbitrary code on a victim's machine when they load a malicious model from the Hub. The Pickle Scanning defence could be bypassed by chaining two repositories so that a benign-looking pickle loads a second malicious pickle.

Peng Zhou Follow

Published on medium.com

Bounty$3,250total stated by author View original

I am writing this article to share my bug-bounty experiences for LLM/AI security, specifically for the LLM supply chain vulnerabilities I have disclosed across the third-party LLM libraries integrated into the Hugging Face hub (HF in short), which include the huggingface/transformers, PaddlePaddle/PaddleNLP…

Peng Zhou, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

Context

Hugging Face Hub (HF) is a platform where users publish and fetch pre-trained large models. The client-side library huggingface/transformers lets end users download and load those models remotely through the from_pretrained API, and the HF website publishes demo code showing how to call it. The same pattern is repeated by other third-party libraries integrated into the hub: PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3.

The class of bug described here is abuse of pickle.loads. Pickle deserialisation is a critical vulnerability that necessarily leads to remote code execution, but in the AI community it is excluded as a security bug if it cannot be called remotely. The reason is controversial: PyTorch implements its key API torch.load using pickle.loads as the core, so the community reached a consensus to overlook the risk in local pickle.loads. That is a bad security practice, but it has to be followed because maintainers are unlikely to acknowledge reports about local pickle.loads.

The question that drives the work is therefore: which pickle.loads calls in huggingface/transformers can load pickle files remotely, through the demo code published on the hub?

Setup

The journey started from an AI Bug Bounty championship held by Protect AI (huntr.com) in August and September 2023, with huggingface/transformers as the target. The researcher later reported through Hugging Face's private HackerOne program and through huntr.com. No specific tooling, accounts or versions are described beyond that.

The precondition for exploitation is a victim who fetches a pre-trained model from the hub using the documented demo code, for example AutoTokenizer.from_pretrained().

Finding it

The first step was to search the target source code for risky Python function calls. pickle.loads turned out to be widely used across the codebase. Since a local pickle.loads would not be accepted, the next step was to investigate the hub in depth to find which of those calls can load pickle files remotely.

Two observations shaped the approach:

  1. huggingface/transformers is a client-side library that end users use to fetch and load models from HF remotely through from_pretrained.
  2. HF publishes demo code on its website that guides end users on how to call from_pretrained.

If a pickle.loads can be reached through a chain of function calls up to from_pretrained, the issue becomes a security vulnerability that the AI community should acknowledge.

Checking the pickle.loads calls one by one quickly confirmed a vulnerable point in the transfo_xl model. The pickle.load() is located in the TransfoXLTokenizer class and can be reached from the demo code through AutoTokenizer.from_pretrained() via a chain of function calls.

The vulnerable code for the bug in transfo-xl model in huggingface/transformers From the original on medium.com

The vulnerable code for the bug in the transfo_xl model in huggingface/transformers.

The demo code is shown on a transfo_xl model’s webpage in the Hugging Face hub From the original on medium.com

The demo code shown on a transfo_xl model's webpage in the Hugging Face hub.

Exploitation

Step 1: understand the existing defence

HF had already understood the risk of pickle deserialisation on its hub and launched a Pickle Scanning method. It scans all pickle files deposited on HF and raises an "unsafe file" message to alert users when a pickle file is detected to contain black-listed Python modules.

Further analysis showed the scanning is flawed. It uses a whitelist to group safe modules and functions and a blacklist for unsafe ones, but it also keeps an orange list for all the other modules and functions that cannot be determined safe or unsafe. Modules on the orange list cannot trigger the unsafe alerts displayed in HF.

Step 2: find a call that sits on the orange list

AutoTokenizer.from_pretrained() was tested against Pickle Scanning and found to be listed as orange. That makes it usable as a bypass primitive through code reuse.

Step 3: split the payload across two repositories

Two repositories, A and B, are deployed on HF:

  1. In repo A, vocab.pkl is crafted to call AutoTokenizer.from_pretrained("B").
  2. In repo B, the real malicious vocab.pkl is deployed with the OS commands for RCE.

Repo A looks benign because its vocab.pkl does not contain any black-listed imports. A victim who fetches the pre-trained TransfoXLTokenizer model from repo A is forced to execute the malicious command deployed in repo B. This is what makes the transfo_xl pickle deserialisation exploitable from the Hugging Face hub.

The video shows how to exploit the transfo_xl vulnerability to RCE and worm infection. It lasts around 5 minutes, and the download of vocab.pkl alone costs about 1 minute.

Informative rejection

The transfo_xl vulnerability was first reported to HF via Protect AI (huntr.com). HF misunderstood the attacking logic and marked it "informative", considering it as just loading pickle files locally by the attackers themselves:

"Hi! The reporter uses one of our methods that loads a pickle file, passes it as a malicious pickle file, and then reports that it loads the file. We recommend using the safetensors format instead. Thanks!".

An explanation email was sent to [redacted] for clarification. HF then invited the researcher to submit the report to its private bug bounty program on HackerOne:

"Thanks for reaching out and for reporting these details. We've taken a closer look and would like you to please submit a report to our private bug bounty program with HackerOne at https://hackerone.com/hugging_face."

The transfo_xl vulnerability was then submitted to the H1 program. It was triaged as low priority with a bounty of $250. HF made that choice mainly because of the few users of this model, not because the security impact was considered low:

"We'd like to proceed with a fix though this is a low priority for us as there are not many users of this model. In TransfoXL we also have support for loading pickle files to deserialise a dict."

The second report

To make HF recognise the critical severity of this class of vulnerabilities, a second report was submitted to the HackerOne program with the unsafe pickle.loads in the RagRetrieve model, which involves a large number of users as potential victims. It was triaged as high severity and answered quickly with a first fix in PR https://github.com/huggingface/transformers/pull/27748. The final patch, PR https://github.com/huggingface/transformers/pull/27776, introduced an environment variable TRUST_REMOTE_CODE to control access to these unsafe pickle.loads calls in both the transfo_xl and RagRetrieve vulnerabilities.

Because the fix is in the transformers source code and the HackerOne program targets the huggingface.co website, the H1 report for RagRetrieve was closed without a bounty, with the suggestion to report back to Protect AI (huntr.com):

"Hi @zpbrent, Thanks for submitting this report, and sorry for the wait. We've taken a deeper look, and our open source team implemented a solution that will require users to set an env. variable (TRUST_REMOTE_CODE) in order to access pickle.load in transformers code. Full details on the PR can be found at https://github.com/huggingface/transformers/pull/27776. As this is related specifically to open source code, this is out of scope of our H1 program though it appears you could try to report to https://huntr.com/bounties for a potential bounty related to /transformers. Many thanks again for taking the time to submit to our private program."

Following that suggestion, RagRetrieve was reported at https://huntr.com/bounties/423611ee-7a2a-442a-babb-3ed2f8385c16/. HF immediately validated it as Critical (9.0) severity and Protect AI rewarded $1500. The report has been published with CVE-2023-6730.

Back to transfo_xl

The transfo_xl report had initially been closed as "informative" on huntr.com, possibly because of a misunderstanding of the threat model. The issue was discussed with the HF team at https://huntr.com/bounties/e1a3e548-e53a-48df-b708-9ee62140963c/, where the rebuttals and HF's feedback are recorded. HF finally accepted the rebuttals and admitted the severity as Critical for transfo_xl. The team sent an email to Protect AI requesting that the report be reopened for validation. Protect AI reopened it and Hugging Face validated it as Critical (9.6) severity, which brought another $1500 bounty. The report has been published with CVE-2023-7018.

The disclosure was complicated. Possibly because of the novelty of these exploits and the stubborn impression that ML libraries commonly exclude pickle.loads as security issues when they cannot be used to load files remotely, HF could not clearly get the key points at the beginning. Reports were lost between the H1 program and huntr.com, and in some cases the critical severity was not recognised. A lot of effort went into clarifying the detailed attacking scenarios until HF understood the critical severity and patched the reported vulnerabilities.

In total: $3250 in bounties and two CVE numbers for the exploitable vulnerabilities in huggingface/transformers.

Item Amount CVE CVSS
RagRetrieve, Protect AI $1500 CVE-2023-6730 9.0 critical
transfo_xl, Protect AI $1500 CVE-2023-7018 9.6 critical
transfo_xl, Hugging Face via HackerOne $250 same transfo_xl issue

Root cause

The mistake is the reachability of pickle.loads from a remote, documented entry point. In huggingface/transformers, pickle.load() in the TransfoXLTokenizer class can be reached from AutoTokenizer.from_pretrained() through a chain of function calls, and from_pretrained is exactly what the demo code on the hub tells users to call. The same pattern exists in RagRetrieve.

Existing defences did not stop it because Pickle Scanning only raises alerts for black-listed modules. Modules and functions that cannot be classified, such as AutoTokenizer.from_pretrained(), sit on the orange list and do not trigger the unsafe alert. Splitting the payload across two repositories means the file a victim actually fetches contains no black-listed imports, so the scan has nothing to flag, while the second fetch performs the malicious work. The scanning design has a whitelist, a blacklist and an orange list.

Impact

An attacker who can publish repositories on HF can make a victim who fetches a pre-trained TransfoXLTokenizer model from the attacker's repository execute arbitrary OS commands, because the fetched vocab.pkl triggers a second fetch of a malicious pickle from another repository. The same class of vulnerability was found in PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3, and all of them could be exploited over the Hugging Face hub using different tricks to bypass Pickle Scanning. The demonstration for transfo_xl covers RCE and worm infection.

Severity depends on the model's user base: HF triaged transfo_xl as low priority because there are not many users of that model, while RagRetrieve was rated Critical (9.0) and transfo_xl was later rated Critical (9.6).

Fix

HF's open source team implemented a solution that requires users to set an environment variable, TRUST_REMOTE_CODE, in order to access pickle.load in transformers code. The first fix was PR https://github.com/huggingface/transformers/pull/27748 and the final patch covering both transfo_xl and RagRetrieve was PR https://github.com/huggingface/transformers/pull/27776.

Variants to test

  1. Search other model-hub client libraries for pickle.loads calls reachable from a documented remote-loading API. A positive result is a call chain from the public API to pickle.loads that a user triggers by following the hub's own demo code.
  2. Test the loading API against the hub's pickle scanner and check whether it is classified as safe, unsafe or undetermined. A positive result is an entry point that does not raise the "unsafe file" alert.
  3. Split a payload across two repositories: a benign-looking pickle in repo A that fetches a malicious pickle from repo B. A positive result is that repo A passes scanning while the victim's fetch still executes the payload from repo B.
  4. Repeat the same approach on the other integrated libraries named here, PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3, using different bypass tricks. A positive result is code execution over the hub from a fetched model.

References

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 19 min read.

What happened, in plain words

Imagine a public library where anyone can drop off a box of books and anyone else can pick one up. The library has a guard at the door who opens each box and checks it against a list of known-bad items. If the guard finds something on the list, he puts a warning sticker on the box. If he finds something on a list of known-good items, he waves it through. But if he finds something he has never seen before, he does not know what to do, so he puts it on a third list, an "orange list", and lets it through without a warning.

That is roughly the situation on the Hugging Face Hub, a website where people publish and download pre-trained AI models. The models are often stored in a file format called pickle, and a pickle file can contain instructions that the computer will run when it opens the file. If an attacker can put a malicious pickle file on the hub and get someone to download it, the attacker can run commands on the victim's computer. This is called remote code execution, or RCE.

The researcher in this story found a way to make that happen through the normal, documented way of using the hub. The hub's own demo code tells users to call a function called from_pretrained, and that function eventually reaches a dangerous pickle.loads call. The hub's guard, called Pickle Scanning, did not flag it because the function sat on the orange list. The researcher then split the attack across two repositories so that the file the victim actually downloads looks harmless, while a second download does the damage.

The first report was rejected as "informative", meaning the maintainers thought it was not a real security issue. After a long back and forth, the issue was accepted as Critical, a second similar bug was found in another model, and the researcher ended up with $3250 in bounties and two CVE numbers, which are public identifiers for security vulnerabilities. The work was also presented as a briefing at Black Hat Asia 2024, a well-known security conference.

From the original on medium.com

The setting: what Hugging Face and transformers are

Hugging Face Hub (HF for short) is a website where people publish and fetch pre-trained large models. A "model" here is a big bundle of numbers that an AI has learned, plus some supporting files. Think of it as a recipe that someone else has already cooked and frozen, so you can heat it up instead of cooking from scratch.

The library huggingface/transformers is a piece of software that runs on your own computer (the "client side") and lets you download and load those models from HF. The function you call to do that is from_pretrained. An API, by the way, is just a named doorway that one program offers to another: you call the name, hand over some inputs, and get a result back. from_pretrained is such a doorway.

The HF website publishes demo code showing how to call from_pretrained. Demo code is a short snippet on a web page that shows a user the recommended way to use something, like the example recipe printed on the back of a cake mix box.

The same pattern is repeated by other third-party libraries that are integrated into the hub: PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3. "Third-party" means they are made by other teams, not by Hugging Face itself, but they plug into the same hub.

The bug class: pickle and why it is dangerous

The class of bug described here is abuse of pickle.loads. Pickle is a way for Python, a programming language, to save an object to a file and load it back later. Think of it as a very powerful packing tape: it does not just store the shape of the object, it stores instructions for rebuilding it. When you call pickle.loads on a file, Python follows those instructions. If an attacker wrote the file, the instructions can include "run this command on the computer".

That is why pickle deserialisation is a critical vulnerability that necessarily leads to remote code execution. "Deserialisation" is just the act of turning stored bytes back into a live object. "Remote code execution" means the attacker gets your computer to run their commands.

But in the AI community, this is excluded as a security bug if it cannot be called remotely. The reason is controversial: PyTorch, a very popular AI library, implements its key API torch.load using pickle.loads as the core. So the community reached a consensus to overlook the risk in local pickle.loads, meaning a pickle file that the user themselves chose to open on their own machine. That is a bad security practice, but it has to be followed because maintainers are unlikely to acknowledge reports about local pickle.loads.

The question that drives the work is therefore: which pickle.loads calls in huggingface/transformers can load pickle files remotely, through the demo code published on the hub? "Remotely" here means the file comes from the internet, from someone else's repository, not from the user's own disk.

Setup: where the journey started

The journey started from an AI Bug Bounty championship held by Protect AI (huntr.com) in August and September 2023, with huggingface/transformers as the target. A bug bounty is a programme where a company pays researchers for finding and reporting security bugs in its software. huntr.com is a platform that runs such programmes for AI projects. The researcher later reported through Hugging Face's private HackerOne program and through huntr.com. HackerOne is another bug bounty platform; a "private" programme means you can only submit if you are invited.

The precondition for exploitation is a victim who fetches a pre-trained model from the hub using the documented demo code, for example AutoTokenizer.from_pretrained(). A repository (repo) is a folder of files hosted on the hub, like a shared drive folder that anyone can browse.

Finding it: how the vulnerable spot was located

The first step was to search the target source code for risky Python function calls. Source code is the human-readable text of a program. pickle.loads turned out to be widely used across the codebase. Since a local pickle.loads would not be accepted as a bug, the next step was to investigate the hub in depth to find which of those calls can load pickle files remotely.

Two observations shaped the approach:

  1. huggingface/transformers is a client-side library that end users use to fetch and load models from HF remotely through from_pretrained.
  2. HF publishes demo code on its website that guides end users on how to call from_pretrained.

If a pickle.loads can be reached through a chain of function calls up to from_pretrained, the issue becomes a security vulnerability that the AI community should acknowledge. A "chain of function calls" means one function calls another, which calls another, and so on, like a series of doors that each open onto the next. The attacker only needs the first door to be reachable from outside.

Checking the pickle.loads calls one by one quickly confirmed a vulnerable point in the transfo_xl model. The pickle.load() is located in the TransfoXLTokenizer class and can be reached from the demo code through AutoTokenizer.from_pretrained() via a chain of function calls. A "class" in programming is a named bundle of code and data; TransfoXLTokenizer is the piece of code that handles the tokenizer for that model. A tokenizer is the part that chops text into pieces the model can understand.

The vulnerable code for the bug in transfo-xl model in huggingface/transformers From the original on medium.com

The vulnerable code for the bug in the transfo_xl model in huggingface/transformers.

The demo code is shown on a transfo_xl model’s webpage in the Hugging Face hub From the original on medium.com

The demo code shown on a transfo_xl model's webpage in the Hugging Face hub.

Exploitation

Step 1: understand the existing defence

HF had already understood the risk of pickle deserialisation on its hub and launched a Pickle Scanning method. It scans all pickle files deposited on HF and raises an "unsafe file" message to alert users when a pickle file is detected to contain black-listed Python modules. A "module" is a named chunk of Python code that can be imported and used. A blacklist is a list of things that are known to be bad; a whitelist is a list of things known to be good.

Further analysis showed the scanning is flawed. It uses a whitelist to group safe modules and functions and a blacklist for unsafe ones, but it also keeps an orange list for all the other modules and functions that cannot be determined safe or unsafe. Modules on the orange list cannot trigger the unsafe alerts displayed in HF. Think of a nightclub bouncer with a list of banned guests and a list of VIPs; anyone not on either list is let in without a second look.

Step 2: find a call that sits on the orange list

AutoTokenizer.from_pretrained() was tested against Pickle Scanning and found to be listed as orange. That makes it usable as a bypass primitive through code reuse. "Code reuse" here means using a legitimate, already-present function as a stepping stone, rather than writing new malicious code that the scanner would recognise.

Step 3: split the payload across two repositories

Two repositories, A and B, are deployed on HF:

  1. In repo A, vocab.pkl is crafted to call AutoTokenizer.from_pretrained("B").
  2. In repo B, the real malicious vocab.pkl is deployed with the OS commands for RCE.

A "payload" is the part of the attack that actually does the damage. vocab.pkl is a pickle file that holds the vocabulary, the list of words the tokenizer knows. "OS commands" are commands for the operating system, the underlying software that runs the computer, such as commands to list files or start programs.

Repo A looks benign because its vocab.pkl does not contain any black-listed imports. A victim who fetches the pre-trained TransfoXLTokenizer model from repo A is forced to execute the malicious command deployed in repo B. This is what makes the transfo_xl pickle deserialisation exploitable from the Hugging Face hub.

In plain words: the guard checks box A, sees nothing on the banned list, and waves it through. But box A contains a note saying "now go and fetch box B", and box B is the one with the bomb. The victim never chose box B; the note in box A made the program fetch it automatically.

The video shows how to exploit the transfo_xl vulnerability to RCE and worm infection. It lasts around 5 minutes, and the download of vocab.pkl alone costs about 1 minute. A "worm" is malicious code that copies itself from one place to another automatically, like a cold spreading through a classroom.

Informative rejection

The transfo_xl vulnerability was first reported to HF via Protect AI (huntr.com). HF misunderstood the attacking logic and marked it "informative", considering it as just loading pickle files locally by the attackers themselves. "Informative" is a status that means the report was read but not treated as a security bug worth fixing or paying for.

"Hi! The reporter uses one of our methods that loads a pickle file, passes it as a malicious pickle file, and then reports that it loads the file. We recommend using the safetensors format instead. Thanks!".

Safetensors is a different file format for storing model data.

An explanation email was sent to [redacted] for clarification. HF then invited the researcher to submit the report to its private bug bounty program on HackerOne:

"Thanks for reaching out and for reporting these details. We've taken a closer look and would like you to please submit a report to our private bug bounty program with HackerOne at https://hackerone.com/hugging_face."

The transfo_xl vulnerability was then submitted to the H1 program. "H1" is shorthand for HackerOne. It was triaged as low priority with a bounty of $250. "Triaged" means the report was sorted and given a severity and a reward. HF made that choice mainly because of the few users of this model, not because the security impact was considered low:

"We'd like to proceed with a fix though this is a low priority for us as there are not many users of this model. In TransfoXL we also have support for loading pickle files to deserialise a dict."

A "dict" is a Python data structure that maps names to values, like a phone book.

The second report

To make HF recognise the critical severity of this class of vulnerabilities, a second report was submitted to the HackerOne program with the unsafe pickle.loads in the RagRetrieve model, which involves a large number of users as potential victims. It was triaged as high severity and answered quickly with a first fix in PR https://github.com/huggingface/transformers/pull/27748. A "PR" is a pull request, a proposed change to the source code that the maintainers review and merge. The final patch, PR https://github.com/huggingface/transformers/pull/27776, introduced an environment variable TRUST_REMOTE_CODE to control access to these unsafe pickle.loads calls in both the transfo_xl and RagRetrieve vulnerabilities. An "environment variable" is a named setting that lives outside the program, like a switch on the wall that the program reads when it starts. A "patch" is a fix applied to the code.

Because the fix is in the transformers source code and the HackerOne program targets the huggingface.co website, the H1 report for RagRetrieve was closed without a bounty, with the suggestion to report back to Protect AI (huntr.com):

"Hi @zpbrent, Thanks for submitting this report, and sorry for the wait. We've taken a deeper look, and our open source team implemented a solution that will require users to set an env. variable (TRUST_REMOTE_CODE) in order to access pickle.load in transformers code. Full details on the PR can be found at https://github.com/huggingface/transformers/pull/27776. As this is related specifically to open source code, this is out of scope of our H1 program though it appears you could try to report to https://huntr.com/bounties for a potential bounty related to /transformers. Many thanks again for taking the time to submit to our private program."

"Out of scope" means the report falls outside the rules of that particular programme, so it cannot be paid there even if it is a real bug.

Following that suggestion, RagRetrieve was reported at https://huntr.com/bounties/423611ee-7a2a-442a-babb-3ed2f8385c16/. HF immediately validated it as Critical (9.0) severity and Protect AI rewarded $1500. The report has been published with CVE-2023-6730. A CVE is a public catalogue number for a known vulnerability, so anyone can look it up.

Back to transfo_xl

The transfo_xl report had initially been closed as "informative" on huntr.com, possibly because of a misunderstanding of the threat model. A "threat model" is a description of who the attacker is and what they can do; if the maintainers picture the wrong attacker, they judge the bug wrongly. The issue was discussed with the HF team at https://huntr.com/bounties/e1a3e548-e53a-48df-b708-9ee62140963c/, where the rebuttals and HF's feedback are recorded. A "rebuttal" is a written argument against a decision. HF finally accepted the rebuttals and admitted the severity as Critical for transfo_xl. The team sent an email to Protect AI requesting that the report be reopened for validation. Protect AI reopened it and Hugging Face validated it as Critical (9.6) severity, which brought another $1500 bounty. The report has been published with CVE-2023-7018.

The disclosure was complicated. "Disclosure" is the process of telling the vendor about a bug and then making it public. Possibly because of the novelty of these exploits and the stubborn impression that ML libraries commonly exclude pickle.loads as security issues when they cannot be used to load files remotely, HF could not clearly get the key points at the beginning. Reports were lost between the H1 program and huntr.com, and in some cases the critical severity was not recognised. A lot of effort went into clarifying the detailed attacking scenarios until HF understood the critical severity and patched the reported vulnerabilities.

In total: $3250 in bounties and two CVE numbers for the exploitable vulnerabilities in huggingface/transformers.

Item Amount CVE CVSS
RagRetrieve, Protect AI $1500 CVE-2023-6730 9.0 critical
transfo_xl, Protect AI $1500 CVE-2023-7018 9.6 critical
transfo_xl, Hugging Face via HackerOne $250 same transfo_xl issue

CVSS is a standard score for how severe a vulnerability is, from 0 to 10, with 9.0 and above usually called critical.

Root cause

The mistake is the reachability of pickle.loads from a remote, documented entry point. "Reachability" means whether one piece of code can be reached from another by following the calls. In huggingface/transformers, pickle.load() in the TransfoXLTokenizer class can be reached from AutoTokenizer.from_pretrained() through a chain of function calls, and from_pretrained is exactly what the demo code on the hub tells users to call. The same pattern exists in RagRetrieve.

Existing defences did not stop it because Pickle Scanning only raises alerts for black-listed modules. Modules and functions that cannot be classified, such as AutoTokenizer.from_pretrained(), sit on the orange list and do not trigger the unsafe alert. Splitting the payload across two repositories means the file a victim actually fetches contains no black-listed imports, so the scan has nothing to flag, while the second fetch performs the malicious work. The scanning design has a whitelist, a blacklist and an orange list.

Impact

An attacker who can publish repositories on HF can make a victim who fetches a pre-trained TransfoXLTokenizer model from the attacker's repository execute arbitrary OS commands, because the fetched vocab.pkl triggers a second fetch of a malicious pickle from another repository. "Arbitrary" means the attacker can choose any command, not just one. The same class of vulnerability was found in PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3, and all of them could be exploited over the Hugging Face hub using different tricks to bypass Pickle Scanning. The demonstration for transfo_xl covers RCE and worm infection.

Severity depends on the model's user base: HF triaged transfo_xl as low priority because there are not many users of that model, while RagRetrieve was rated Critical (9.0) and transfo_xl was later rated Critical (9.6).

Fix

HF's open source team implemented a solution that requires users to set an environment variable, TRUST_REMOTE_CODE, in order to access pickle.load in transformers code. The first fix was PR https://github.com/huggingface/transformers/pull/27748 and the final patch covering both transfo_xl and RagRetrieve was PR https://github.com/huggingface/transformers/pull/27776.

Variants to test

If you want to look for similar bugs yourself, here is the same checklist in plain terms:

  1. Search other model-hub client libraries for pickle.loads calls reachable from a documented remote-loading API. A positive result is a call chain from the public API to pickle.loads that a user triggers by following the hub's own demo code. In other words: can a user who follows the official instructions end up running a pickle file from a stranger?
  2. Test the loading API against the hub's pickle scanner and check whether it is classified as safe, unsafe or undetermined. A positive result is an entry point that does not raise the "unsafe file" alert. In other words: does the guard wave it through?
  3. Split a payload across two repositories: a benign-looking pickle in repo A that fetches a malicious pickle from repo B. A positive result is that repo A passes scanning while the victim's fetch still executes the payload from repo B. In other words: can the harmless-looking box smuggle in the dangerous one?
  4. Repeat the same approach on the other integrated libraries named here, PaddlePaddle/PaddleNLP, facebookresearch/mbrl-lib and DLR-RM/stable-baselines3, using different bypass tricks. A positive result is code execution over the hub from a fetched model.

References

Glossary

  • API: a named doorway that one program offers to another; you call the name, hand over inputs, and get a result back.
  • Blacklist: a list of things known to be bad, which are blocked or flagged.
  • Bug bounty: a programme where a company pays researchers for finding and reporting security bugs in its software.
  • Chain of function calls: one function calling another, which calls another, and so on, like a series of doors that each open onto the next.
  • Class: a named bundle of code and data in a program.
  • Client-side library: software that runs on the user's own computer rather than on the website's servers.
  • Code reuse: using a legitimate, already-present function as a stepping stone instead of writing new malicious code.
  • Critical: the highest severity band for a vulnerability, usually CVSS 9.0 and above.
  • CVE: a public catalogue number for a known vulnerability, so anyone can look it up.
  • CVSS: a standard score for how severe a vulnerability is, from 0 to 10.
  • Demo code: a short snippet on a web page showing the recommended way to use something.
  • Deserialisation: turning stored bytes back into a live object in a program.
  • Dict: a Python data structure that maps names to values, like a phone book.
  • Disclosure: the process of telling the vendor about a bug and then making it public.
  • Environment variable: a named setting that lives outside the program, like a switch on the wall that the program reads when it starts.
  • HackerOne (H1): a bug bounty platform; a private programme means you can only submit if you are invited.
  • huntr.com: a bug bounty platform run by Protect AI for AI projects.
  • Informative: a report status meaning the report was read but not treated as a security bug worth fixing or paying for.
  • Module: a named chunk of Python code that can be imported and used.
  • Orange list: in this story, a third list for modules and functions that cannot be classified as safe or unsafe, which do not trigger alerts.
  • Out of scope: outside the rules of a particular bug bounty programme, so it cannot be paid there even if it is a real bug.
  • Patch: a fix applied to source code.
  • Payload: the part of an attack that actually does the damage.
  • Pickle: a Python format for saving an object to a file and loading it back, which can carry executable instructions.
  • Pickle Scanning: Hugging Face's method of scanning pickle files on the hub and raising an "unsafe file" alert for black-listed modules.
  • PR (pull request): a proposed change to source code that maintainers review and merge.
  • RCE (remote code execution): getting a victim's computer to run commands chosen by the attacker.
  • Reachability: whether one piece of code can be reached from another by following the calls.
  • Rebuttal: a written argument against a decision.
  • Repository (repo): a folder of files hosted on the hub, like a shared drive folder that anyone can browse.
  • Safetensors: a file format for storing model data that does not carry executable instructions.
  • Source code: the human-readable text of a program.
  • Third-party: made by another team, not by the platform itself, but plugged into the same platform.
  • Threat model: a description of who the attacker is and what they can do.
  • Tokenizer: the part of a model that chops text into pieces the model can understand.
  • Triaged: a report that has been sorted and given a severity and a reward.
  • Whitelist: a list of things known to be good, which are allowed through.
  • Worm: malicious code that copies itself from one place to another automatically.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.