start	end	text
0	6640	So we have a pre-recorded video, pre-recording from the speakers, and we're going to get started right now.
7140	7900	Thanks.
30000	36500	I completed this work together with my colleague Lishuang Song, and we were really looking forward to presenting in person.
36500	42480	But unfortunately, due to visa application issue, we are unable to attend the event on site.
42480	45300	And we sincerely apologize for this.
45300	51060	And as a result, we have to share our presentations through a pre-recorded video instead.
51060	54560	And if you have any questions, please feel free to contact us.
55140	57020	So here, this is the agenda.
57020	59040	So first is the introduction section.
59040	67480	And after that, we will share where the story begins, and then I will analyze how it only works, and introduce the TorchScript engine.
67480	73260	And finally, I will share the impact of this vulnerability, and share some defense recommendations and summary.
73660	75380	So first, introduction.
75920	84340	And PyTorch is a machine learning framework based on Torch Machine Learning Library, and it was developed by Facebook, and now it is an open source project.
84340	87760	And it is very easy to use and has some really great features.
88480	97440	And PyTorch is widely used in the field of deep learning research, natural language processing, or Corning LP, and computer vision.
97900	106720	And to be honest, in the field of machine learning, there are many, many popular frameworks, and well-known projects like TensorFlow and Keras.
107040	115560	But however, according to the paper with Kodo website, and you can see that PyTorch has undoubtedly been the most popular machine learning framework.
115560	121640	If someone can find some bugs in such a famous AI framework, and that will be a really cool thing.
122000	125720	And next comes the second part, where it all began.
126240	131550	And in the early days, PyTorch used Pico to load and save models for convenience.
131980	143710	However, Pico is one of the most well-known security issues in Python, and in fact, the official Python document was that using Pico can be unsafe, especially when loading untrusted data.
144350	152510	So in 2021, someone raised an exception on GitHub and called for a solution to address the Pico issue in PyTorch.
152510	159030	And finally, in 2022, PyTorch released a notable update introducing the waste-loading parameter.
159910	162870	And let's briefly analyze the implementation of waste-loading.
162870	169930	As you can see that by default, waste-loading is set to false, and it will use Pico to load the model.
169930	178830	And if we set waste-loading equal true, you can see that it will use something called waste-only and Pico, and you can consider it as a restricted Pico.
179030	181290	So now, let's try it out.
181290	186510	So first, since by default, the waste-loading is false, and we will use Pico to load the model.
186510	192670	So here we can easily create an eval model and use this reduce magic method.
192670	204590	And you can see that after we save the model, and then we load this model, and you can see that we successfully execute the system command and we get the output route.
206030	216170	And however, if we set waste-loading equal true, and you can see that it just threw an exception, and it seems that the vulnerability is effectively mitigated.
216470	223790	And also on the official PyTorch website, and you can see that the consider-toys-to-load with waste-loading equal true is also secure.
223790	229870	And it also warns that loading and trusting checkpoint with waste-loading equals false must never be done.
229870	239250	And additionally, very similar security issues are discovered in other PyTorch-based components, and they often fix it by using waste-loading equal true.
239250	241110	And here is an example.
241110	248110	So initially, VLM used torch.load with all waste-loading equal true, and which introduced a security risk.
248110	252610	And they fix it by simply add the waste-loading equal true parameter.
253890	264490	And so not only VLM, and in general, the number of software is found to be using torch.load without waste-loading equal true, and the fix is also the same way, just adding the parameter.
264490	269810	But here comes the question, can we blend in trust and security or waste-loading?
271310	276110	So next part, we will dive deeper into it, how waste-loading actually works.
276870	280890	And earlier, we point out that waste-loading is something like a restricted Pico.
280890	286810	So before we analyze how waste-loading is implemented, and we need to understand how Pico works.
287450	296290	And Pico is something like an interpreter, and it reads and interprets each opcode in sequence, and it's executing the corresponding operation.
296290	303750	So for example, here, we first encounter the global opcode, and it will use the load global function.
303750	308150	And you can see that it will call found class, and it will import this model.
308150	315610	And so here, after we execute this command, and you can see that it will get the system function and push it onto the stack.
316150	320190	And since now, we get the function, and we need to get the arguments.
320230	325250	And so here, we can use the unicode opcode to read the string.
325250	327810	And so here, we will get the Huamai.
327870	333850	And since arguments must be in turbo format, and we need to wrap this string into a turbo.
333850	339150	So we use this turbo1 opcode, and you can see that it will replace it with the turbo.
339250	344850	And so here, after we execute the load turbo, and you can see that we wrap this string into a turbo.
344850	347870	Now, we get the function and its arguments.
348250	352710	So now, we can use the reduce opcode to execute this function.
352710	356550	So here, we use the reduce, and it will execute the load reduce function.
356650	361350	And you can see that it will get the arguments, get the function, and call it.
361350	363490	So finally, we can get a command execution.
364290	366910	So how does Visconi address this issue?
367670	376210	And through the above example, and we can roughly say that the most dangerous part of the pcode deserialization is load global and load reduce.
376210	380690	So Visconi and pcode just add some restriction to this dangerous opcode.
380690	387330	Looking at the code, you can see that the load global function has some blacklist and whitelist.
388810	393830	And also, load reduce is restricted in a similar way, and it has a whitelist.
395610	400290	And so in general, Visconi is implemented using a whitelist and a blacklist.
400290	401790	So how can we bypass?
402750	410490	And a common approach is to analyze the whitelist and check whether there are any potential dangerous functions that could be exploited.
410570	414670	And I did the same, but I didn't find anything useful in the end.
415090	418530	And we are also limited by fixed ways of thinking.
418530	425990	So for example, here in this case, most of us may only see the obvious whitelist and blacklist mechanism and stop here.
425990	432310	But however, in reality, the loading process of Torch.load with Visconi equal true is quite complex.
432310	437290	So is it possible that there might be some issues as well in the loading process?
437290	443150	So I tried to conduct a complete analysis of the Visconi workflow to ensure nothing is missed.
444070	453210	So I started analyzing the entire Torch.load process and stepping into the code and debugging the code line by line.
453210	458970	And during one of my analysis, I noticed something very strange in the Torch.g.load.
458970	460390	So what is that?
460450	462170	And I had no idea with this.
462170	471110	So I just searched it on Google and the document just said that Torch.g.load is something to use to load models in the TorchScript format.
471110	474530	So now you can understand that why we named this topic.
474830	478550	Yes, we will analyze and exploit the TorchScript engine.
479290	480610	Hello, everyone.
480610	485630	This part will cover the basics of TorchScript and its code implementation.
487070	494730	TorchScript is used for deploying models in environments without a Python interpreter, such as CPP and mobile.
494730	504390	It can be compiled from Python code through scripting or tracing, and saved as a PDF file which can be loaded and run in CPP and mobile.
504390	508670	The save and load process involves serialization.
508670	521830	I will explain how Python code is compiled to TorchScript, the serialization and deserialization process, the structure of PDF files, and finally, how it executes.
522290	530130	First, we can use TorchJIT Creator to script one Python function to TorchScript.
530130	537070	The steps to compile are, first, press Python source code into a Python AST.
537070	541310	Second, convert the Python AST to a JIT AST.
541310	545550	Third, transform the JIT AST into an AR graph.
546150	556950	The AR graph initially contains many unnecessary nodes, which are eliminated through optimization to make it more concise.
558270	561890	Let's take a deep look about the compile process.
561890	566670	Before we start, I need to show what is the function and model in this context.
567070	572470	You can see that one is a function, the other is a class.
572470	579500	The scripting process for these two are somewhat different, so we will introduce them separately.
580650	583830	Let's look at the function first.
583850	589670	The compile process is handled within the script mpl function.
589670	595530	There are two main functions, getJITDev and JITScriptCompile.
595530	600210	The getJITDev function performs two main tasks.
600330	606370	First, it uses the AST library to convert Python code into a Python AST.
606370	615110	And second, it calls buildDev to transform that Python AST into a self-implemented JIT AST.
617050	621290	JITScriptCompile is a function bound from CPP to Python.
621290	631110	With its implementation in the CPP layer, it further converts the JIT AST to AR within the CPP code.
632070	641510	It eventually invokes the toAR function, which finishes the transformation from JIT AST to AR.
641610	649070	Additionally, toAR also calls some optimization functions to simplify the AR graph.
649070	652530	Now the function compile process is done.
652530	657010	A high-level understanding of the process is enough for this sharing.
657010	666590	If you are interested in the compile source code details, you can use the code stack I have provided to navigate through the source code.
667350	669750	Then let's look at the model.
669950	674470	The overall process is similar to that of functions.
674470	678150	Expect that there is an additional step at the beginning.
678150	681330	Retrieving all functions from the model.
681330	686870	It needs to retrieve functions and compile functions one by one.
686870	691870	The stub fn function is responsible for handling this.
692490	698630	Stub's fn is analyzed for infer methods to compile in this connect.
698630	703710	Let's see what the infer method to compile function does.
704270	715310	In the infer methods to compile function, it first uses dir to get all attributes and methods of the model.
715310	727390	After filtering, it saves the methods that need to be exported, thereby collecting all functions in the model that requires compilation.
728750	744550	The make stub fn method converts the collected functions into JLTST using the getJLTDEF function, which is the same one used during function compilation.
745510	770600	Back to create a separate model MPL, the method stubs variable stores the JLTST we just converted, and the create methods and properties from stubs fn takes two method stubs as input and is responsible for converting it to AR.
772220	779780	We can see that this call stack is similar to the call stack for function compilation.
780190	785450	Both eventually finish the conversion into AR function.
785450	791310	Now the model compile process is done.
791310	795330	Overall, some key steps are the same as for function.
795330	803070	Both use getJLTDEF to convert Python to JLTST, then convert it to AR through toAR.
804270	810470	The serialization filter can save models and deploy them to other environments.
810470	814090	Use touchsave to save model as pdfile.
814090	817250	Use touchload to load pdfile as model.
817450	820650	The Python code here is converted from AR.
820650	824750	You will notice it's slightly different than the original code.
826070	832390	Let's see how separate function and separate model is serialized.
832570	838330	Separate function has an additional step compared to separate model.
838330	842470	It needs to be converted to separate model first.
842470	846370	After that, both call serialize for serialization.
846430	855630	During the serialization process, some information is stored in the data.pkl and constants.pkl in big format.
855630	863010	Additionally, it saves the Python code converted from AR and debug info to the code folder.
864830	872990	To summarize the save process, for touch function, first convert it to touch model.
872990	876030	The remaining process is the same.
876030	883950	First, the app value corresponding to the model is serialized in PQL format as data.pkl.
884330	891270	Second, obtain code and debug info via Python print and write to code directory.
891970	896730	Third, save tensor constants to constants.pkl.
896730	899710	Now the serialization is done.
899710	903670	Let's look at the detail of the PTFEW.
905030	911310	The PTFEW is compressed achieve using ZIP compression.
911870	916410	The field tree after decompression is shown above.
916410	921250	And we can use PicoTools to disassemble it.
921270	926750	Above is the disassembled output of data.pkl.
926750	938210	The meaning of this PicoTools is create an object named touch placeholder model and set its training attribute to true.
938210	943130	And the touch placeholder model object is a secret model.
943150	949070	So when interpreting data.pkl, secret model object is created.
951270	958970	The constants.pkl contains Pico code, which can create a value object with tensor.
959230	963550	The touch.py field contains Python code strings.
963550	972150	It's the same as the content of the code attribute in secret model because they are obtained in the same way.
972150	980750	OK, now we understand the three most important parts saved in the serialized field.
980750	987110	Data.pkl, constants.pkl, and the source code in the code directory.
987450	994690	Let's see how these parts are restored into a secret model.
995890	1008610	We can see that in the deserialized function, the three main parts of the serialized field are converted into a secret model.
1008610	1011970	Let's look at the function implementation.
1013530	1020210	Deserialize will call read achieve to read constants.pkl and data.pkl.
1020210	1027250	Then we'll get one model in CPP, which is a secret model in Python.
1027830	1041870	During the deserialization process, first I'm picking constants.pkl to obtain tensor constants, then save them in constants.table.
1041870	1049450	Second, I'm picking data.pkl during the process of I'm picking data.pkl.
1051710	1064750	Relevant source code is read from the code directory and pressed as needed, and the related constants are also read from the constants.table.
1064750	1077910	The code processing process includes converting Python code to custom tokens, and then transforming tokens into JIT-ST, and finally into AR.
1077910	1086950	This process differs significantly from the earlier Python-to-Tetrascript compilation.
1088570	1097710	To summarize the load process, first, reach main logic via deserialization.
1097710	1109430	Second, call read achieve to read constants.pkl, convert constants to n-values by I'm picking and save them to constants.table.
1110230	1116930	Third, call read achieve to read data.pkl, restore corresponding n-values by I'm picking data.
1118410	1135570	Fourth, during data.pkl I'm picking, source importer reads code fields and constants.table to restore AR through pressTab to find namedTab to import namedTab.
1135570	1144510	Now the deserialization is done, let's take a look at how Tetrascript is running.
1144930	1158090	The AR graph contains many nodes, and every node contains three parts, yin, output, and opcode.
1158090	1161190	Opcode is the key to execution.
1161770	1173730	The process of executing Tetrascript is to iterate through the AR and convert it into instructions.
1173730	1177870	Then interpret the instructions.
1177870	1181410	Every instruction has one opcode.
1181410	1192370	There is a switch table here that finds the corresponding process logic through the op and the execute set.
1192590	1200030	Now we have a basic understanding of the Tetrascript workflow.
1200940	1205010	While reading the code, I discovered something interesting.
1205010	1211150	There is one opcode named op, and it can do a call on operator table.
1211150	1215990	An operator table contains many functions corresponding to operators.
1215990	1220070	I'm curious about what operators they are.
1221500	1231470	Some built-in functions register themselves as operators, requiring op instructions to call corresponding functions.
1231670	1241850	The registerOperator class manages these operators, and registers them by registerOperator function.
1241850	1248110	I page the PyTorch code and add one function to output all operators.
1248110	1253130	You can see there are over 2000 operators here.
1254570	1258890	Are all these operators safe?
1259530	1270910	During our code audit of these operators, we discovered two interesting operators, save and fulfill.
1271510	1277990	Here we can see the corresponding implementations of these two functions.
1277990	1284630	The save function will save specified value as a model to specified field.
1285370	1290390	This can lead to arbitrary field writes.
1290390	1296150	But the saved field contains a lot of dirty data, which model information.
1296170	1306290	The fulfill function will read the field which I specified, and convert the field contained to tensor.
1306290	1311310	Then return the tensor, we can get the field contained from the tensor.
1312810	1317210	Now we know these two operators have vulnerabilities.
1318650	1321550	But there is still a problem.
1321550	1324210	How to call them from touch script?
1325470	1332230	To get the answer, we need to first understand the operator registering process.
1332230	1339930	The getBuildingTable function registers some object member functions to building of ops.
1340050	1347790	And during this process, it also adds prefixes to the member functions for rename.
1347790	1357730	Finally, the address of these functions and their modified names will be placed into built-in table.
1357730	1372370	During the touch script compilation process, two shrugged values will be triggered, where it checks whether the Python object exists in the built-in table.
1372370	1376910	If it exists, it returns a built-in function.
1377630	1385990	Finally, this built-in function will be inserted into the AR graph through emitter built-in call.
1386650	1400930	Back to the built-in registering process, we now understand that add and save may come from one of these objects in models containing built-ins.
1400930	1415950	By testing each object individually for the presence of a save function, we will find that the save function exists in the touch object.
1416090	1420710	Adding from field can also be obtained in the same way.
1421350	1430130	So, we just need to call touchSave() or touchFromField() to get arbitrary field write ability in touch script.
1430830	1445290	Now, we can try writing a field to get RCE, but since there are some dirty characters, only specified special fields will work.
1446710	1466790	Though we want to call and type a field because of a permission mismatch, our script uses touchSave(), which writes a field with 644 permissions, but the current demo requires 6000 permissions.
1467690	1470370	Here is one poke video.
1506270	1508820	Okay, you can see we get RCE.
1512180	1520770	The vulnerabilities we have discussed so far might be more similar to logic vulnerabilities.
1520850	1526910	On the other hand, some hackers may prefer memory-related vulnerabilities.
1526910	1531750	We have also discovered a few overflow issues.
1532470	1540950	Here you can see that when we load this modelpt field, it triggers a heap overflow.
1540950	1549290	As a result, we can observe that the heap address and the loaded address of the library are leaked.
1551630	1554230	This is one poke video.
1570270	1575430	Okay, you can see we get RCE by memory bug.
1576510	1583030	But due to time limited, we won't go into them in great detail here.
1583030	1585710	We hope to share them in the future.
1587950	1591890	This is the full details of the CVE.
1593010	1597190	The fix is also relatively simple.
1597190	1603970	It prevents the use of touchscript when voice-only is enabled.
1604510	1607370	So what is the impact of this vulnerability?
1607830	1615210	The entire AI ecosystem, like this house and petology, is one of the fundamental components.
1615210	1623970	If there is an issue with the petology, just like we have removed one of the bottom-level components, the entire AI ecosystem is then compromised.
1624690	1628350	We did a simple search on GitHub for projects using voice-only code.
1628470	1632990	We can see that in fact, many projects use it in this way.
1633230	1639170	Here we select two well-known projects as an example to illustrate the impact of this vulnerability.
1639390	1642490	One is VOM and the other is Transformers.
1642490	1650610	VOM is a high-performance library for efficient large-language model inference and optimized for speed and memory usage.
1650990	1652390	So why did I choose it?
1652390	1662190	Because it has a CVE number and you can see that it uses the torch.load function and the voice-only parameter is the default value of force, so it has an RCE vulnerability.
1662650	1666190	The fix is very simple, just add the voice-only parameter.
1666550	1673650	This seems to solve the problem, but because of the petology issue, mitigation ends up being ineffective.
1674730	1678950	And the sea harbor you once saw was actually also waters.
1680030	1691470	And at the same time, I found a very interesting phenomenon and you can see that when I check this requirements.cpu.txt and you can see that it just hard-coded the torch version.
1691850	1696990	And also in the GPU version, you can see that it also hard-coded the torch version.
1697350	1698750	So what does this mean?
1698750	1708210	It means that even if PyTorch released a new patched version and users of VRM cannot update it timely because VRM has hard-coded the version.
1708210	1713650	So as a result, the users will remain exposed to this vulnerability for a long period.
1714430	1716410	And so now let's set up the environment.
1716410	1725290	Since the latest VRM has already patched its issue, so we need to install the old version, exactly 0.7.3.
1725290	1731070	And you can see that when you install VRM, then it will install the exact version of torch.
1732250	1738310	And so here we will use this function as an example to illustrate the existence of the vulnerability.
1739330	1742230	And at first, I didn't think too much about that.
1742230	1748410	I just want to take the poke from the previous vulnerability and just hoping for one should succeed.
1748430	1754570	And you can see that finally, we will definitely enter the logic we want and it will load our email model.
1754570	1755990	So we are ready to proceed.
1756570	1760950	But it failed and you can see that we don't create this fail, so why?
1761970	1774610	And let's take a look at the previous poke and you can see that after we use torch.load to load this model and we need to call the model to invoke the model in order to trigger this forward logic.
1775050	1780090	But here you can see after we load this evil model and it didn't invoke the model.
1780090	1783190	It just fetched the items and then deleted the model.
1783190	1785370	So this model was not invoked.
1786170	1788810	And is it really an end?
1789750	1796230	And at that point, I noticed the error message and it says that the model don't have the attribute items.
1796230	1799130	And this error message gave me some inspiration.
1800490	1808070	And items is essentially a keyword in Python and it is used to iterate the dictionary type object.
1808170	1811390	So you can see that in fact, items is a function.
1811390	1814390	So here can we spoof the function name?
1815130	1822590	And so here I just try for the first time and you can see that I change the function name from forward to item and just run the code.
1822590	1827690	And you can see that why it is still saying that our model don't have the items attribute.
1828910	1843110	But if we write in this way and you can see that we just call the items function in our forward logic and we run a code and you can see that we successfully create a field and this means we succeed.
1843330	1852790	And this is very strange and the reason is that when retrieving model functions, it determines whether these functions need to be exported.
1852790	1864750	For functions not decorated as exports and a lazy export strategy is adopted, meaning that they are only exported if called within the exported function.
1864790	1871330	So here you can see that in the left picture, we just decorated our items function with exported.
1871330	1876850	So here it will export the item function and we can get this item attribute.
1876930	1886730	And in the right picture and you can see that since we call it in our exported function forward and our object, we also have this item function.
1887670	1894990	And so finally, we report our ability to the official team and who highly acknowledge our work.
1895010	1900970	And you can see that I said it's very interesting since PyTorch document claims it is safe.
1901670	1905630	And the patch is very simple, just change this version.
1906570	1908170	And next, Transformers.
1908210	1919030	Transformers is a popular open source library by Hugging Face and provided pre-trained models and tools for natural language processing tasks using the transformer-based architecture.
1919970	1923790	And Transformers also implemented some security hardening measures.
1923790	1929210	For example, here you can see that it just used which only equals true to load the model.
1930150	1941210	And for now, we set our environment and you can see that since the latest version of Transformers also fixed this issue and we need to install our old version 4.51.3.
1941390	1954290	And here, this is the official document example from Transformers and you can see that we need to load the model from Hugging Face and create a pipeline and then we can create our conversation.
1954290	1956670	So, here, for example, we just ask a question.
1956670	1961670	And you can see that just give me some answer.
1962390	1972530	And here, this is my demo project and you can see that we only have three fields, readme, config.json, and a test PyTorch field.
1972530	1974390	And it doesn't contain any evil code.
1975150	1982610	And if we run the code and you can see that it will use Torch.load to load our remote PyTorch model.
1983030	1990510	And let me introduce this code and when we load a remote Hugging Face repo, then it will load this model.
1990510	1995770	And by default, fields in C tensors or PyTorch is allowed.
1996950	2002950	And just like the previous VRM vulnerability and it will call the keys function.
2003570	2011250	So, we can just change the function from items to keys and we save the project in our local repo.
2011890	2018430	And, of course, if we run this code and you can see that we can exploit and we can create this field.
2019290	2026490	And after that, we call this vulnerability to Hugging Face and you can see that it just patched it.
2026490	2028330	And the patch is also very simple.
2028330	2031690	You can see that it can check Torch.load is safe.
2031690	2036530	It checks the Torch version greater or equal than 2.6.
2037250	2040370	And finally, it defends a summary.
2040690	2045110	So, now, if you are still using your own version of PyTorch, please update.
2045850	2047910	And here is also some recommendations.
2047910	2055910	So, for example, from the model format perspective, we should use more secure format like safe tensors.
2056150	2066090	And also from the model community perspective, we should scan and flag malicious models and Hugging Face has already done the relevant work.
2066270	2073250	And also from the user perspective, we should not load untrusted models and it is better to load them in a sandbox.
2074650	2076610	So, any questions?
2078530	2080410	And thanks again for listening.
2080410	2084290	And if you have any questions, please feel free to contact us at any time.
