Hey everyone, I'm Suha. I'm really excited to be here today. So, let's get started. I want to talk to you about ML security. Specifically, I want to talk about this new class of exploits I identified called incubated ML exploits that combine backdoors and input handling bugs. Don't worry if you don't know too much about ML or ML security. I'll explain all the important stuff as we go along. So, who am I and why am I even talking to you today? I'm an engineer at Trail of Bits where I focus on AI and ML security. I've been in the field for a few years now. I graduated from Georgia Tech and I'm originally from Queens. Outside of work, I like Brazilian Jiu-Jitsu, trying new restaurants, making things, and an obscure card game called Q-Birds. So, it's becoming pretty clear with ML and AI popping up everywhere, people are figuring out how to trick these systems based on how these models work. Maybe you've seen someone use prompt injection to convince a chatbot to give them a refund. Or maybe you've seen this story right here of protesters tricking self-driving cars with traffic cones. Notice the fact that this trick is rooted in an understanding of the training data for these models. So, how can we actually construct our own useful exploits against ML systems? Let's play a game of pretend real quick. You're a college student and you really, really want the prize money for a robotics competition. So, naturally, you decide to sabotage another team. Side note, I don't recommend this. I don't condone this. I've never done it myself. But anyway, the competition requires teams to build a tiny autonomous vehicle that uses a specific pre-trained model and stops at stop signs. So, you find out that some of these stop signs have these little stickers on them. And you also find some flaws with how they've stored and distributed the model. Which, by the way, isn't out of the question. ML artifacts are often widely shared without any meaningful or substantial trust mechanisms. So, you decide to grab that file and inject a model backdoor in it using a file format RCE of some kind. And then you put it back. Then, on the day of the competition, you sit back and you just watch as your competitor's vehicle just plows through and ignores every stop sign with a sticker on it. What you just did is execute an incubated ML exploit, which is what my talk is all about. So, obviously, the stakes of this story is just a lost robotics competition. But the idea of attacking a real autonomous vehicle is a hallmark of model backdoor research. And you can see that with the image on the left. So, I'll let you use your imagination to raise the stakes. So, in my talk, first I'm going to tell you about this framework that I've been using to bridge the gap between model and system security. Because we can't continue to treat models as standalone objects. Next, I'll tell you about these input handling bugs I found in model serialization and connect them to backdoors. I'll do that by taking a page out of this subfield called langsec. So, effectively, I'll be going through a bunch of examples of incubated ML exploits and use langsec to organize them. But first, I need to explain some stuff. What even is a model vulnerability or an ML backdoor? So, super briefly, you can think of ML models as these squishy, flexible sequences of linear algebra operations that are trained on tons and tons of data. There's a pretty popular saying, all models are wrong, but some are useful. We're just saying that these models aren't perfect. There are many different ways they can mess up or get tripped up by something that might be unexpected to us. And that's the basis of such model vulnerabilities. While popular examples of model vulnerabilities include model inversion and membership inference, we're zooming in on one specific type. Model backdoors. So, to be precise about it, a backdoor attack allows a malicious actor to force an ML model to produce specific outputs given specific inputs. Now, there's a couple of things that I think make backdoors really interesting to study. First up, you can use them as primitives for other model vulnerabilities, like membership inference. You can also identify pre-existing, quote-unquote, natural backdoors in them. And there's also some pretty strong evidence that suggests that this is an inherent threat. Now, while there's a lot of awesome research on ML model attacks, they can actually be pretty hard to exploit in the real world, with some exceptions, of course. While there are multiple reasons for this, one thing that really sticks out to me is the big gap between research and the real world. So, for the most part, many attacks and attack frameworks and tools restrict their analysis to this formulation. An ML model receives an input and produces an output. But this isn't an accurate representation of what an ML system actually looks like. There's so much more going on in practice. Here's a software architecture diagram for an ML system reviewed by Trail of Bits recently. This is a system that uses the Ask Astro tool for RAG. And I've circled where the model actually is in this photo. Do you see what I mean? We need to be looking at all of this holistically. There's a large and evolving landscape of tools being used in and for ML systems. And that brings me to the exploit framework. So, the title of my talk references an incubated ML exploit. But there's this larger category of exploits called hybrid ML exploits that are important to think about first. Specifically, a hybrid ML exploit chains a system security issue with a model vulnerability. This can go in either direction, and you can see that on the diagram. You can have a model vulnerability that exposes a system security issue, or you could use a system security issue to exploit a model vulnerability. So, this part's pretty important. The big issue that I see with ML security is that model security and system security are treated separately. But what I need you to understand is that if you only know model security, you're missing a big piece. And if you're only covering system security, you're still missing a big piece. And if you're treating the two processes completely independently, once again, you're missing a big piece. You're then entirely ignoring the potential for hybrid ML exploits. This is an emergent property. So, your model is embedded in a system, and it's going to interact with all of the different system components in new and exploitable ways. So, one thing you'll notice is that there's a lot of screenshots of paper titles on this slide. That's because there have been specific instances of hybrid ML exploits in the literature and in practice. They're just not called that explicitly. Exploitable software gadgets have been used for backdoors. The summoning demons paper up there at the top chained model evasion and memory corruption. And the learn system security paper next to it has an example of a poisoning attack that caused an exponential memory blowup in an index structure. But the ML security literature framework and tools are largely limited to just that. Specific instances or implications. What I'm trying to do here, what I want to be doing here, is treating this interaction explicitly and systematically. And that's why I made this framework. So, one kind of system security issue is an input handling bug. And one kind of model vulnerability is a model backdoor. Put that together and we get an incubated ML exploit. Which is a type of hybrid ML exploit where an attacker uses input handling bug to inject a backdoor. So, I made this diagram to make the distinction between the two pretty clear. And here's the definition again. I'm going to leave the framework here for now. We did end up going into a bit of a more formal model of exploitation including this exploit schema. But we'll return to that later. So, to backdoor a pre-existing model, the attacker should be able to change the parameters or the architecture. Now, at the level of extraction we're dealing with, we can put input and component manipulation on the side for now. But how this actually plays out can vary a lot. For example, sometimes the attacker has control over some element of the training process. And they use that to sneak in some manipulated data that changes the model's parameters. That's usually called data poisoning. Or they might go a step further and fiddle with the model's source code to change the architecture. Now, before we talk about exploits, I want to explain a few things about input handling bugs. So, an ML model is stored as a file. To process these models, you need parsers. And parsing these files into objects and back is deserialization and serialization. But wait, quoting Ange Albertini here, a file has no intrinsic meaning. The meaning of a file, its type, its validity, its contents can be different for each parser or interpreter. Now, this is the reason we can make cool, potentially malicious file artifacts like polyglots and ambiguous files, which I'll talk more about later. So, I'm focused very specifically on bugs that arise when you parse ML model files. There's also cool bugs in other parts of the pipeline, but I'm picking ML model files for several reasons. The first reason is, and I'm sure you all will agree with me on this, the most important. I think it's fun. But more seriously, the security of ML file formats has become increasingly important. Real malicious model files have been found on the Hugging Face Hub, for example. And there's also just an absolute ton of ML file formats out there. I've tried to list and organize them in the repository in the middle, but what's important for you to take away is that there's a large set of possibilities for these exploits, as well as just fun hacks with these formats. And there's already a lot of great work in this area, as shown on my slide. So, file format tricks are within the realm of LangSec, but this field actually thinks more abstractly about inputs as a general class. LangSec applies formal language theory to system security. It focuses on exploring input handling bugs, or parser problems, as a big root cause for security issues. After all, lots of impactful vulnerabilities like Heartbleed and Android Masterkey have been parser bugs. Now, while I like formal language theory, this talk isn't Theoretical Computer Science 101. So, just know that fundamentally what LangSec is saying is, hey, let's treat all of the inputs as a specific language, and make our code just capable enough to understand that language properly. So, my work is centered around a specific taxonomy of input handling bugs. And these are all the different bug classes. There are eight different types. Quick note, these categories aren't actually completely distinct from each other. The one you choose comes from a root cause analysis. So, with the exception of one, I'm going to show you multiple examples of each in ML Tools, and use them to build an ML Backdoor. So, I'm going to run that one more time. In order to show that input handling bugs are a vector, I identified issues with ML Model Serialization across these different bug classes in order to construct ML Backdoors. So, now we can dive into the most fun part, exploits. Now, for the sake of time, I'm going to focus more on the useful gadgets for these exploits. So, these are some characters that play important roles in the ML ecosystem that can help us understand the impact of these exploits. So, Alice, she distributes models. She takes open source LLMs and fine-tunes them. The models she distributes are what everyone else in our story is going to be using. Bob is a frontline user who directly uses Alice's models in his own life. Maybe he has a nice chat interface pulled up. There's Dave. Dave is a developer who is trying to integrate these models into products. Frank is the end user who is relying on Dave's products in his daily life. He might be unaware of the ML models working behind the scenes. And now we have Chuck. Chuck is the attacker. Our focus here will mainly be on how Chuck can impact Bob and Dave. So, I'll describe some exploits involving the file formats associated with Pickle, PyTorch, TorchScript, ONX, and SafeTensors. So, this first category is called Non-Minimalist Input Handling Code. It sounds a little fancy, but all it means is that the code used to check and parse these inputs is too complex. So, an attacker can potentially grab the necessary gadgets for their exploits. So, Pickling is a serialization method that allows you to save arbitrary objects, and it's very, very common in the ML ecosystem. Recently, my coworker Boyan Milanov led the development of SleepyPickle, which is an incubated ML exploit. And what it does is it chains a Pickle RCE with model backdoors. So, on the right, you can see an LLM that has been backdoored to phish users. The blog post also has some examples of an LLM being used to spread misinformation and even steal user data. But what's cool about this exploit is that it can happen on the fly. So, there's more room and possibilities for an attacker than just uploading a malicious model. So, what do I mean by Pickle RCE? Python Pickles are compiled programs that run in a unique virtual machine called a Pickle Machine, or a PM for short. The PM interprets the Pickle file sequence of opcodes to construct an arbitrarily complex Python object. But it has two opcodes, global and reduce, that can execute arbitrary code outside of the PM, which makes it possible to construct malicious Pickle data. Now, the underlying issue here is that the PM is more complex than something that's only parsing ML models should be. So, way back in the year 2021, we released this tool called Fickling. This project was led by Evan Sultanik. To our knowledge, Fickling was the first Pickle security tool tailored for ML use cases. It's a decompiler, static analyzer, and bytecode rewriter for Python Pickle. So, it can help you detect, analyze, and even create malicious Pickle files. So, the reason it's safe to run on potentially malicious files is because it symbolically executes code using its own PM implementation. Relatively recently, I added a PyTorch module to it to make it easy to statically analyze and inject code into PyTorch files. But Pickles are clearly bad for Bob. If Alice is distributing models as Pickle files, that makes it that much easier for Chuck to inject a backdoor using a Pickle RCE. So, on to the next class. This term just means you shouldn't try to correct invalid input, reject it altogether. I've heard it referred to as the anti-robustness principle. So, to deal with the issues with Pickling, many devs write these things called restricted unpicklers. Now, those are subclasses of unpickler that try to enforce an allow list or a block list. The thing is, they're actually not that hard to bypass. There's this methodology called PainPickle that demonstrates how to automatically bypass restricted unpicklers, which would enable arbitrary code execution and, to that end, backdoors. So, they identified eight different types of unpicklers and three strategies that work against the vast majority of them. So, much like Pickle was bad for Bob, restricted unpickling bypasses pose a problem for Dave if he relies on them in some fashion. Now we can talk about parser differentials. So, this happens when different parsers in a system read the same input but interpret it differently. So, when two parsers interpret the same file in different ways, that file is known as an ambiguous file. This is a very common exploit technique. It's pretty good for bypasses. It means you can create an ML model file that is benign for one system or system component but backdoored for another. There's some more bigger implications for ML system exploitation here that we'll talk a bit more about later. But just a quick note, whether or not this is impactful at all depends on your system. So, that's where you want to do threat modeling, of course. So, we actually were able to create two differential proof of concepts with TorchScript. TorchScript is a popular format to store ML models in for a couple of reasons, mainly performance and portability. But you can make a parser differential with it and chain it to an architectural backdoor. So, that's because you can turn a PyTorch model into a TorchScript one through tracing or scripting. And tracing doesn't incorporate dynamic control flow. So, all you have to do is represent the malicious components for the backdoor with dynamic control flow. Now, the second example was found during the YOLO audit. Last year, my team and I audited a popular open source code base for computer vision called YOLOv7. They released standard versions of their model and TorchScripted versions for deployment. We noticed many cases where tracing didn't capture the model accurately. After serialization and deserialization, key information was lost and the usual PyTorch warnings didn't show up. So, to spot this differential, we used the TorchScript automatic trace checker, TorchFX, and the TorchScript IR. So, with what we found, we created an input that made the two versions of the model act differently. Effectively, a backdoor attack. So, once again, bad for Bob. He's getting a fundamentally different model than the one Alice trained, so this breaks any pre-existing promises. So, we also identified a parser differential with SafeTensors. SafeTensors is another file format for ML models developed in response to the insecurity of pickling. Last year, I was on an audit of the SafeTensors library, where we identified the inclusion of JSON in the file format as a source of parser differentials. Now, JSON's pretty well-known to be underspecified. There's a bunch of exploits in the web security world that leverage this. But the thing is, the reference SafeTensors implementation, the main implementation, uses a cert parser. Which is good. It's strict. It rejects duplicate keys. But there's a bunch of external tools that use the Python built-in JSON parser, which doesn't. So you can use a duplicate key for the offsets to append backdoored weights and create manipulated SafeTensors files. That are rejected by the reference implementation, but accepted by external parsers. It has to be a weights-based backdoor, because the weights and architecture are stored separately here. And there's a bit more details and caveats regarding the exploitability here. But just know, the SafeTensors parser differential is more impactful for Dave. He needs to make sure all the parsers in his product agree. If his tool is using a more permissive SafeTensors parser than the reference implementation, it might just accept manipulated SafeTensors files that have backdoored models. So, one big part of my research is analyzing previous work and noticing trends. Now, I don't want to get too into the weeds here. I'd like to say formalisms for accompanying materials. But one thing that stuck out to me is that from parser differentials, we get these things called model differentials. Instances where the same model is interpreted differently. As expected, this attack is dependent on the supply chain component and lifecycle stage. But in an ML system, you can preprocess inputs or you can apply model transformations before you deploy a model. So, some studies have exploited parser differentials right at the preprocessing stage. These are things like image scaling or Unicode parsing. And those attacks often change the weights. There have also been backdoor attacks that take advantage of model transformations like compilation or quantization. And those usually change the architecture. So, I think it's very possible that most transformations can be encoded in a loss function, that can be encoded in a loss function, can result in an exploitable backdoor. But let's move forward from here. Next up, we have shotgun parsing. This is just what happens when you don't fully and properly check your input before beginning to process it. So, let's talk about polyglot files. These are files that can be validly interpreted as two or more different formats. Polyglot files have been used to distribute malware, bypass code signing checks, and enable other malicious behaviors. So, for ML model serialization, you can take these, put them in model hubs, and confuse some downstream consumers. But, more importantly, you can have two different ML pipelines that interpret the same file as different models, so you can smuggle a backdoor model in with a benign one. So, during our audit of a SafeTensors library, we were able to make multiple polyglots. This includes ZIP, PDF, TF records, Keras Native, and later on, PyTorchMAR. And the SafeTensors audit report itself is a PDF ZIP polyglot, with a ZIP file containing all the polyglots we made during the audit. So, you can just slap on a waste-based backdoored model to one of these formats to a benign SafeTensors model, open it up with SafeTensors, everything's fine, load it up with PyTorchMAR or some other system, and boom, there's your backdoor. Now, this is a real problem for folks like Dave, who's depending on these models, because now you've got malicious models sneaking in with benign ones, right? The reason this is possible is because of a missing check. Specifically, the program didn't check whether the start and end offsets corresponded with the tensor size, so attackers could append arbitrary data to the file. And that can be combined with the ability to change the header size to expand the number of polyglots. Now, this issue has since been fixed with SafeTensors, however. So, our next category is incomplete protocol specification. Just think of it as underspecification for now. There are multiple examples of this in the literature, but for the sake of time, I'm just going to talk about PyTorch polyglots. So, many people are unaware that PyTorch actually supports multiple file formats. Some are deprecated, but still supported by external parsers. Now, one issue is that it does lack consistent versioning here. So, it's not that difficult to create polyglots of files that can be validly interpreted as different types of PyTorch file formats. So, same for ambiguous files. Side note. So, you can just, like, add three files to get a polymock between version 1.3 and TorchScript version 1.4, but a bigger issue is the reliance on zip and pickle. So, pickle is a streaming file format. It ends once it reaches the stop code. So, you can validly just append arbitrary data to it. Now, on the other hand, most zip parsers don't enforce their magic at the start. So, you can prepend to it, and one example of this is PyTorch-mar. So, you can append a zip to a pickle file to create a zip-pickle polyglot, and that gives you some good PyTorch polyglots. So, Fickling now has a polyglot module, so you can differentiate, identify, and create polyglots for the different PyTorch file formats. Now, on to the next class. This one just means your input should be simple and well-defined, so you can check it thoroughly. Take ONNX. It's a protobuf-based way to store ML models. Adlon Travers discovered a neat hack for the ONNX runtime he packaged into a tool called Lobotomy. So, ML runtimes and frameworks often let you add custom ops to a model on the fly. And the language used for the ONNX runtime custom ops at that point in time was complex. So, even though the ONNX protobuf specification officially disallowed side effects, in the ONNX runtime, you can encapsulate arbitrary code into a custom op, and you can use that to launch an architectural backdoor attack. Just like Pickle, this is not good news for Bob. So, to recap, Bob, our direct consumer, was affected by the Pickle, ONNX, and TorchScript issues. Dave, on the other hand, was affected by the PyTorch, unsafe tensors, and restricted unpickling issues. Now, what a lot of people miss is how important and how complex the ML stack is. The model you choose changes the technologies in the stack. So, whenever I'm assessing a system or doing some kind of vulnerability research, I'm always trying to think about what layer of the ML stack I'm dealing with. So, the layers I have listed here are hardware, infrastructure, low-level, compiler, high-level, framework, model, and knowledge. And I just described a bunch of exploits, right? So, of the ones I told you about, the ones that are exposed and impactful at the framework level are the restricted unpickler, ONNX runtime, and Pickle exploits. Now, the compiler level corresponds to the TorchScript differential. And the safe tensors and PyTorch polyglot issues are impactful at the infrastructure level. And this is just a starting point. There's going to be exploits up and down this stack that impact ML systems. So, if you want to get into attacking ML systems, this is a good place to start. Are you really, really good at breaking hardware? Go take a stab at a TPU. Do you happen to know a lot about distributed system security? Go write some hybrid ML exploits at the infrastructure level. So, I made this schema for incubated ML exploits. This is just one piece of a more formal model of exploitation I worked on. I'm just going to talk about this at a very high level to shed some light on the terrain here. If you want to pull off an incubated ML exploit, you want right primitives for the weights or the architecture, right? And with the proof of concepts, we've seen some additional useful capabilities. So, side note, you probably want read primitives as well. But with the safe tensors parser differential, you saw that access to the metadata could enable both kinds of attacks. And you also saw that there's a lot of utility in exploiting model transformations and model differentials. With that, you can construct exploits at different stages of the pipeline that exploit existing procedures. So, with ONNX, it also became pretty obvious that you can use malicious custom ops in serialization formats, and potentially even in places like compiler dialects. I'll release more details on this in accompanying materials. But I do want to make some more explicit recommendations. I apologize for the busy slide here. Models should be checked for integrity, and their metadata should be well parsed. We want good trust mechanisms. We want proper validation. We want to minimize complexity, so we should be avoiding custom ops and separating the weights and architecture storage. I also think we should be doing a better job of following recommended practices for file formats, like they should have versions, and checksums, and magic signatures, and they should enforce the signature at offset zero. And we really need to invest in just more robust specifications and tooling. So, I'm hoping we can see hybrid ML exploits and incubated ML exploits addressed by more frameworks and tools. I'd love to see this framework evolve and be applied to specific ML tools and contexts. I want to see it use more bug classes and more model vulnerabilities. I'd also like to see people investigate exploit persistence, reliability, and defense more. And just generally, I think there's so much interesting work to be done here with ML infrastructure security, with model differentials, and file formats, and specifications, and even reverse engineering. But before we finish, what helps me identify and make progress on ML security problems is understanding these two root causes. First, we're building all of these new systems for ML. New hardware, new programming languages, new compilers, new file formats. There are conferences dedicated just to new ways to design ML infrastructure. And that means all of these new systems are introducing new attack surfaces. At the same time, it's becoming increasingly clear that the ML stack and supply chain have not been subject to sufficient review. That's why we're seeing pickles everywhere, right? Second, simply placing an ML model into a program introduces all of these new vulnerabilities that stem from how the model interacts with different components. ML is not a quick add-on, but something that can and will fundamentally change your system security posture. So, I hope you leave this talk knowing that we need to concurrently and holistically think about system security and model security. I really recommend checking out the full audit reports for SafeTensors and YOLO, as well as the blog posts on Fickling and the file formats repo. I'll post more details and accompanying materials, and we're hoping to release a paper on this topic as well. You can find my contact info on my website, or just send me a message on Twitter. But thank you all for coming, thank you all for listening. If you have any questions, feel free to just come up to me.