{"id":265,"date":"2021-12-06T09:56:35","date_gmt":"2021-12-06T17:56:35","guid":{"rendered":"https:\/\/blog.mozilla.org\/attack-and-defense\/?p=265"},"modified":"2022-02-25T10:00:43","modified_gmt":"2022-02-25T18:00:43","slug":"webassembly-and-back-again-fine-grained-sandboxing-in-firefox-95","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/attack-and-defense\/2021\/12\/06\/webassembly-and-back-again-fine-grained-sandboxing-in-firefox-95\/","title":{"rendered":"WebAssembly and Back Again: Fine-Grained Sandboxing in Firefox 95"},"content":{"rendered":"<p style=\"display:none\">\n<p><span style=\"font-weight: 400;\">In Firefox 95, we&#8217;re shipping a novel sandboxing technology called <\/span><a href=\"https:\/\/plsyssec.github.io\/rlbox_sandboxing_api\/sphinx\/\"><span style=\"font-weight: 400;\">RLBox<\/span><\/a><span style=\"font-weight: 400;\"> \u2014 developed in collaboration with researchers at the University of California San Diego and the University of Texas \u2014 that makes it easy and efficient to isolate subcomponents to make the browser more secure. <\/span><span style=\"font-weight: 400;\">This technology opens up new opportunities beyond what&#8217;s been possible with traditional process-based sandboxing, and we look forward to expanding its usage and (hopefully) seeing it adopted in other browsers and software projects.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This technique, which uses WebAssembly to isolate potentially-buggy code, builds on the <\/span><a href=\"https:\/\/hacks.mozilla.org\/2020\/02\/securing-firefox-with-webassembly\/\"><span style=\"font-weight: 400;\">prototype<\/span><\/a><span style=\"font-weight: 400;\"> we shipped last year to Mac and Linux users. Now, we\u2019re bringing that technology to all supported Firefox platforms (desktop and mobile), and isolating five different modules: <\/span><a href=\"https:\/\/scripts.sil.org\/cms\/scripts\/page.php?site_id=projects&amp;item_id=graphite_home\"><span style=\"font-weight: 400;\">Graphite<\/span><\/a><span style=\"font-weight: 400;\">, <\/span><a href=\"http:\/\/hunspell.github.io\/\"><span style=\"font-weight: 400;\">Hunspell<\/span><\/a><span style=\"font-weight: 400;\">, <\/span><a href=\"https:\/\/xiph.org\/ogg\/\"><span style=\"font-weight: 400;\">Ogg<\/span><\/a><span style=\"font-weight: 400;\">, <\/span><a href=\"https:\/\/libexpat.github.io\/\"><span style=\"font-weight: 400;\">Expat<\/span><\/a><span style=\"font-weight: 400;\"> and <\/span><a href=\"https:\/\/github.com\/google\/woff2\"><span style=\"font-weight: 400;\">Woff2<\/span><\/a> [1]<span style=\"font-weight: 400;\">. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Going forward, we can treat these modules as untrusted code, and \u2014 assuming we did it right \u2014 even a zero-day vulnerability in any of them should pose no threat to Firefox. Accordingly, we\u2019ve updated our <\/span><a href=\"https:\/\/www.mozilla.org\/en-US\/security\/client-bug-bounty\/#exploit-mitigation-bounty\"><span style=\"font-weight: 400;\">bug bounty program<\/span><\/a><span style=\"font-weight: 400;\"> to pay researchers for bypassing the sandbox even without a vulnerability in the isolated library.<\/span><\/p>\n<h2><strong>The Limits of Process Sandboxing<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">All major browsers run Web content in its own sandboxed process, in theory preventing it from exploiting a browser vulnerability to compromise your computer. On desktop operating systems, Firefox also isolates each site in its own process in order to protect sites from each other. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Unfortunately, threat actors routinely attack users by chaining together two vulnerabilities \u2014 one to compromise the sandboxed process containing the malicious site, and another to escape the sandbox [2]<\/span><span style=\"font-weight: 400;\">. To keep our users secure against the most well-funded adversaries, we need multiple layers of protection.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Having already isolated things along trust boundaries, the next logical step is to isolate across functional boundaries. Historically, this has meant hoisting a subcomponent into its own process. For example, Firefox runs audio and video codecs in a dedicated, locked-down process with a limited interface to the rest of the system. However, there are some serious limitations to this approach. <\/span><span style=\"font-weight: 400;\">First, it requires decoupling the code and making it asynchronous, which is usually time-consuming and may impose a performance cost. Second, processes have a fixed memory overhead, and adding more of them increases the memory footprint of the application. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">For all of these reasons, nobody would seriously consider hoisting something like the XML parser into its own process. To isolate at that level of granularity, we need a different approach.<\/span><\/p>\n<h2><strong>Isolating with RLBox<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">This is where RLBox comes in. Rather than hoisting the code into a separate process, we instead compile it into WebAssembly and then compile that WebAssembly into native code. This doesn\u2019t result in us shipping any .wasm files in Firefox, since the WebAssembly step is only an intermediate representation in our build process. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, the transformation places two key restrictions on the target code: it can\u2019t jump to unexpected parts of the rest of the program, and it can\u2019t access memory outside of a specified region. Together, these restrictions <\/span><a href=\"http:\/\/www.cse.psu.edu\/~gxt29\/papers\/sfi-final.pdf\"><span style=\"font-weight: 400;\">make it safe to share an address space<\/span><\/a><span style=\"font-weight: 400;\"> (<\/span><a href=\"https:\/\/arxiv.org\/abs\/2105.00033\"><span style=\"font-weight: 400;\">including the stack<\/span><\/a><span style=\"font-weight: 400;\">) between trusted and untrusted code, allowing us to run them in the same process largely as we were doing before. <\/span><span style=\"font-weight: 400;\">This, in turn, makes it easy to apply without major refactoring: the programmer only needs to sanitize any values that come from the sandbox (since they could be maliciously-crafted), a task which RLBox makes easy with a <a href=\"https:\/\/hacks.mozilla.org\/2020\/02\/securing-firefox-with-webassembly\/\">tainting layer<\/a><\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The first step in this transformation is straightforward: we use <\/span><a href=\"https:\/\/clang.llvm.org\/\"><span style=\"font-weight: 400;\">Clang<\/span><\/a><span style=\"font-weight: 400;\"> to compile Firefox, and Clang knows how to emit WebAssembly, so we simply need to switch the output format for the given module from native code to wasm. For the second step, our prototype implementation used <\/span><a href=\"https:\/\/github.com\/bytecodealliance\/wasmtime\/tree\/main\/cranelift\"><span style=\"font-weight: 400;\">Cranelift<\/span><\/a><span style=\"font-weight: 400;\">. Cranelift is excellent, but a second native code generator added complexity \u2014 and we realized that it would be simpler to just map the WebAssembly back into something that our existing build system could ingest. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We accomplished this with <\/span><a href=\"https:\/\/github.com\/WebAssembly\/wabt\/tree\/main\/wasm2c\"><span style=\"font-weight: 400;\">wasm2c<\/span><\/a><span style=\"font-weight: 400;\">, which performs a straightforward translation of WebAssembly into equivalent C code, which we can then feed back into Clang along with the rest of the Firefox source code. This approach is very simple, and automatically enables a number of important features that we support for regular Firefox code: profile-guided optimization, inlining across sandbox boundaries, crash reporting, debugger support, source-code indexing, and likely other things that we have yet to appreciate.<\/span><\/p>\n<h2><strong>Next Steps<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">RLBox is a big win for us on several fronts: it protects our users from accidental defects as well as supply-chain attacks, and it reduces the need for us to scramble when such issues are disclosed upstream. <\/span><span style=\"font-weight: 400;\">As such, we intend to continue applying to more components going forward. Some components are not a good fit for this approach \u2014 either because they depend too much on sharing memory with the rest of the program, or because they\u2019re too performance-sensitive to accept the modest overhead incurred \u2014 but we\u2019ve identified a number of other good candidates. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Moreover, we hope to see this technology make its way into other browsers and software projects to make the ecosystem safer. <\/span><a href=\"https:\/\/github.com\/PLSysSec\/rlbox_sandboxing_api\"><span style=\"font-weight: 400;\">RLBox<\/span><\/a><span style=\"font-weight: 400;\"> is a standalone project that\u2019s designed to be very modular and easy-to-use, and the team behind it would welcome other use-cases.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Speaking of the team: I\u2019d like to thank <\/span><a href=\"https:\/\/shravanrn.com\/\"><span style=\"font-weight: 400;\">Shravan Narayan<\/span><\/a><span style=\"font-weight: 400;\">, <\/span><a href=\"https:\/\/cseweb.ucsd.edu\/~dstefan\/\"><span style=\"font-weight: 400;\">Deian Stefan<\/span><\/a><span style=\"font-weight: 400;\">, and <\/span><a href=\"https:\/\/www.cs.utexas.edu\/~hovav\/\"><span style=\"font-weight: 400;\">Hovav Shacham<\/span><\/a><span style=\"font-weight: 400;\"> for their tireless work in bringing this work from research concept to production. Shipping to hundreds of millions of users is hard, and <\/span><span style=\"font-weight: 400;\">they did some seriously impressive work.<\/span><\/p>\n<p>Read more about RLBox and this announcement on the <a class=\"c-link\" tabindex=\"-1\" href=\"https:\/\/jacobsschool.ucsd.edu\/news\/release\/3374\" target=\"_blank\" rel=\"noopener noreferrer\" data-stringify-link=\"https:\/\/jacobsschool.ucsd.edu\/news\/release\/3374\" data-sk=\"tooltip_parent\" data-remove-tab-index=\"true\">UC San Diego Jacobs School of Engineering website<\/a>.<\/p>\n<p>&#8212;<\/p>\n<p>[1] Cross-platform sandboxing for Graphite, Hunspell, and Ogg is shipping in Firefox 95, while Expat and Woff2 will ship in Firefox 96.<\/p>\n<p>[2] By using a syscall to to exploit a vulnerability in the OS, or by using an IPC message to exploit a vulnerability in a process hosting more-privileged parts of the browser.<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Firefox 95, we&#8217;re shipping a novel sandboxing technology called RLBox \u2014 developed in collaboration with researchers at the University of California San Diego and the University of Texas \u2014 &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/attack-and-defense\/2021\/12\/06\/webassembly-and-back-again-fine-grained-sandboxing-in-firefox-95\/\">Read more<\/a><\/p>\n","protected":false},"author":1610,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[449530,449215,449525],"tags":[],"coauthors":[465198],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/posts\/265"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/users\/1610"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/tags?post=265"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/attack-and-defense\/wp-json\/wp\/v2\/coauthors?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}