8 ms·
The V8 Sandbox
- codedokode 3y agoIt seems that the problem is in Javascript itself, where almost every action can cause side-effects. This makes interpreter extremely complex and it is difficult to spot a mistake. Converting something to int shouldn't cause any side effects.
- pjmlp 3y agoThe most relevant part for the TL;DR; folks, > The V8 Sandbox has already been enabled by default on 64-bit (specifically x64 and arm64) versions of Chrome on Android, ChromeOS, Linux, macOS, and Windows for roughly the last two years.
- rzzzt 3y agoElectron is also using V8's sandboxed pointers: https://www.electronjs.org/blog/v8-memory-cage https://www.electronjs.org/blog/v8-memory-cage
- vlovich123 3y agoIt’s interesting that it spends a lot of time talking about how memory safe languages don’t help V8 cause of JIT (true) & then talks about a hardening technique that does get helped by Rust. Why not just be honest and say that the switching cost to a new language is too expensive and error prone than play these games? Similarly the discussion about memory tagging - I know that it would harden the security of things like Cloudflare workers. And if most of the exploits are because it’s in-process, why not work on isolating it behind it’s own process (there must be ways to do this securely while not giving up too much performance)?
- olliej 3y ago…because the overwhelming majority of memory safety bugs in js engines (v8, JSC, and spider monkey) are in operations that would be in unsafe blocks in rust as well? In multiple decades I can think of a handful of engine bugs that would have been prevented by rust - and those were largely preventable (and now are) in c++ as well. It is possible for rust to be a safer language than c++ and to also not meaningfully change the security profile of the language. It’s not just the jit, the interpreter and GCs are also subject largely - necessarily - no more protected by rust than c++.
- vlovich123 3y agoDid you read the article? It has nothing to do with the JIT. It uses the JIT as a smokescreen to talk about sandbox hardening & the issues within the sandbox are definitely not "unsafe" & 100% mitigated by Rust. Take a look at the relevant quotes I extracted in another comment to draw your attention to what the article is actually talking about (sandbox hardening).
- tux3 3y agoI don't think it's fair to call them dishonest here. It's pretty clear they've heard about memory safe languages, they've thought about it, they've considered in details the pros and cons. >why not work on isolating it behind it’s own process (there must be ways to do this securely while not giving up too much performance)? Well, you make it sound like the easy answer. A good exercise would be to try implementing what you're proposing in a comment. Not necessarily going all the way, but enough to know why it might not be as straightforward as you think. The people working on V8 are not completely clueless, the concept of moving things out of process or using a memory safe language is not going to be a novel idea that they'll just start working on now that someone clever thought of it.
- vlovich123 3y agoThe dishonest piece is that the first part talks about why Rust doesn't help with the JIT (true) but then really talks about the V8 sandbox & hardening techniques they're applying to it where Rust would help 100%. > However, assuming these numbers are simply stored as integers somewhere in the JSObject, an attacker could corrupt one of them to break this invariant. Subsequently, the access into the (out-of-sandbox) std::vector would go out of bounds. Adding an explicit bounds check, for example with an SBXCHECK, would fix this. Or use Rust > Encouragingly, nearly all "sandbox violations" discovered so far are like this: trivial (1st order) memory corruption bugs such as use-after-frees or out-of-bounds accesses due to lack of a bounds check Or use Rust > Contrary to the 2nd order vulnerabilities typically found in V8, these sandbox bugs could actually be prevented or mitigated by the approaches discussed earlier. In fact, the particular bug above would already be mitigated today due to Chrome's libc++ hardening Or use Rust I'm not saying rewrite the entire thing in Rust, too expensive & would introduce new bugs in the JIT for questionable benefit. But at least mention that & also discuss the technical challenges why the sandbox mechanism isn't written in Rust & what it would take to address those. Look, I'm not saying the V8 team is making the wrong decisions. My questions are an indication of the shallowness of the blog write-up - why not explain some obvious questions that come up for someone who reads it?
- mandarax8 3y ago
- wavemode 3y ago> talks about a hardening technique that does get helped by Rust What hardening technique discussed in this article would be helped by Rust, and what specific feature of Rust would help?
- vlovich123 3y ago> This code makes the (reasonable) assumption that the number of properties stored directly in a JSObject must be less than the total number of properties of that object. However, assuming these numbers are simply stored as integers somewhere in the JSObject, an attacker could corrupt one of them to break this invariant. Subsequently, the access into the (out-of-sandbox) std::vector would go out of bounds. Adding an explicit bounds check, for example with an SBXCHECK, would fix this. > Encouragingly, nearly all "sandbox violations" discovered so far are like this: trivial (1st order) memory corruption bugs such as use-after-frees or out-of-bounds accesses due to lack of a bounds check. Contrary to the 2nd order vulnerabilities typically found in V8, these sandbox bugs could actually be prevented or mitigated by the approaches discussed earlier. In fact, the particular bug above would already be mitigated today due to Chrome's libc++ hardening. As such, the hope is that in the long run, the sandbox becomes a more defensible security boundary than V8 itself
- wavemode 3y agoIt's still not clear to me what Rust feature would prevent what specific vulnerability here. Rust has bounds-checked and non-bounds-checked array accesses depending on the developer's preference, and so does C++. If there's some point you're making with these quotes you're going to need to simplify it for me since I'm not following.
- rcxdude 3y agoThe defaults are switched though: C++ is unchecked by default, Rust is checked by default.
- tubthumper8 3y ago> Rust has bounds-checked and non-bounds-checked array accesses depending on the developer's preference, and so does C++ You're making it sound like these are the same, the difference is the defaults Unsafely accessing an element in C++ vec[i] Unsafely accessing an element in Rust unsafe { vec.get_unchecked(i) } One of these is screamingly obvious that something potentially unsafe is happening and should be audited more closely, that's the real difference. The cause of potential memory issues is isolated and searchable in `unsafe` blocks rather than being potentially anywhere
- CodeNest 3y ago[dead]
- azakai 3y agoThe technique here keeps a large set of objects from escaping the sandbox. Those objects are accessed both by C++ and JIT code. You are right that using Rust instead of C++ would help on the C++ side, but it would not help at all on the JIT code side, and that is by far the major source of exploits. In other words, even if you write a JS engine in Rust you could benefit greatly from this technique.
- vlovich123 3y agoI have no issue with the sandbox technique. My critique is that the sandbox escapes just have to find an assumption violation in C++ that leads to a memory violation, meaning that sandbox escapes are easier in V8 due to being written in C++. Having the sandbox & security-relevant runtime pieces written in Rust would do a lot to prevent huge swathes of sandbox escapes more robustly than trying to ensure that all C++ today & that will be written remains hardened.
- egnehots 3y agoThey say that Rust is not enough and dismiss it quickly: > V8 vulnerabilities are rarely "classic" memory corruption bugs (use-after-frees, out-of-bounds accesses, etc.) but instead subtle logic issues which can in turn be exploited to corrupt memory. As such, existing memory safety solutions are, for the most part, not applicable to V8. In particular, neither switching to a memory safe language, such as Rust, nor using current or future hardware memory safety features, such as memory tagging, can help with the security challenges faced by V8 today. But looking at the awesome list they provided: https://docs.google.com/spreadsheets/d/1lkNJ0uQwbeC1ZTRrxdtuPLCIl7mlUreoKfSIgajnSyY/edit?usp=sharing https://docs.google.com/spreadsheets/d/1lkNJ0uQwbeC1ZTRrxdtu... There are a lot of use-after-frees and out-of-bounds accesses, buffer overflow in there...
- kevingadd 3y agoType confusion is also a very common attack against JS runtimes and V8 specifically. Of course, it's not trivial to build a high-performance JS runtime without playing around with pointer types pretty liberally, so I can understand saying "Rust won't fix this" in regards to those attacks. But those attacks would basically not be possible against a runtime built on top of Java or C#.
- sroussey 3y agoThe attacks would not be possible against a runtime written in JavaScript as well, by that reasoning.
- 3y ago
- andy_xor_andrew 3y agoI'm confused about the fizzbuzz example they provide. ```js let array = new Array(100); let evil = { [Symbol.toPrimitive]() { array.length = 1; return 15; } }; array.push(evil); // At index 100, the @@toPrimitive callback of |evil| is invoked in // line 3 above, shrinking the array to length 1 and reallocating its // backing buffer. The subsequent write (line 5) goes out-of-bounds. array.fizzbuzz(); ``` I'm probably missing the point, but I thought indexing into an array in Javascript outside its bounds would result in an exception or an error or something?
- rafram 3y agoThe example fizzbuzz() function is implemented in C++. (And out-of-bounds indexing in JS actually doesn't generate an exception/error; it just returns undefined. Great language!)
- olliej 3y agoNot to be confused with undefined in c++! :D :D the best!
- csjh 3y agoOOB indexing from the Javascript side would return undefined, but OOB indexing on the engine side (lines 5/7/9 of JSArray::fizzbuzz()) is the same as OOB indexing a pointer
- eyelidlessness 3y agoDisclaimer: I have very little experience with C++, a bit more with Rust code that bridges with JS in a manner similar to the example, and zero experience with V8 dev. All of that said… I think the technically correct responses you’ve gotten so far may be missing an insight here: wouldn’t the V8 example code be just as safe as the equivalent JS if it used the JS array’s own semantics? More to the point: presumably those JS semantics are themselves implemented in C++ somewhere else, and this example is reimplementing an incorrect subset of them. While it’s likely inefficient to add another native/JS round trip through JSValue to get at the expected JS array functionality, it seems reasonably safe to assume the correct behavior could be achieved with predictable performance by calling into whatever other part of V8 would implement those same JS array semantics. In other words, it doesn’t seem like you’re missing the point. It seems like this kind of vulnerability could be at least isolated by applying exactly the thinking you’ve expressed.
- frankjr 3y ago> Similarly, disabling the JIT compilers would also only be a partial solution: historically, roughly half of the bugs discovered and exploited in V8 affected one of its compilers while the rest were in other components such as runtime functions, the interpreter, the garbage collector, or the parser. Using a memory-safe language for these components and removing JIT compilers could work, but would significantly reduce the engine's performance (ranging, depending on the type of workload, from 1.5–10× or more for computationally intensive tasks). If you're willing to take the performance hit, Chromium actually allows you to disable JIT easily in the Settings and add exceptions for certain sites. Open the Settings and search for V8 Optimiser.
- vlovich123 3y ago> while the rest were in other components such as runtime functions, the interpreter, the garbage collector, or the parser Notably memory safe languages wouldn't really help with the garbage collector since it would have to use unsafe Rust & the confusion about lifetime would still exist or you'd be using something like Java/C# where you're just relying on the robustness of that language's runtime GC. However, the runtime functions, interpreter and parser would be secured by something like Rust & I fail to see how well-written Rust would introduce a 1.5-10x overhead.
- deleted 3y ago[deleted]
- fngjdflmdflg 3y agoI think the key is: >and removing JIT compilers If you read the article it makes more sense.
- kaba0 3y agoThat’s not quite true. It only depends on what level of abstraction are you willing to do — you can write a runtime with GC entirely in safe rust (or a managed language).
- 3y ago
- sanxiyn 3y agoWhy so late? I think WebKit had this like five years ago, aka Gigacage.
- pciexpgpu 3y agoThis is splendid work by Google that will benefit the rest of the ecosystem - especially with the reward program. I wonder how this impacts (positively) Cloudflare Workers/Fly.io-style isolation (both use very different isolation mechanisms I guess). Perhaps, thinking out loud, CF Workers had the right level of isolation to begin with (i.e. pure V8 isolation)?
- throwitaway1123 3y agoFly uses Firecracker micro VMs rather than V8 isolates. Two of the engineers behind both services had a friendly discussion about it a few years ago: https://news.ycombinator.com/item?id=31759170 https://news.ycombinator.com/item?id=31759170
- winrid 3y agoSomeday it would be really cool to execute NodeJS code in a sandbox with a timeout without having to throw the work at a subprocess.
- silverlyra 3y agoI believe that day has already come: https://nodejs.org/docs/latest-v20.x/api/worker_threads.html#worker-threads https://nodejs.org/docs/latest-v20.x/api/worker_threads.html... When you create a Worker with the worker_threads module, Node spawns a new V8 isolate in the same process: https://github.com/nodejs/node/blob/v20.12.1/src/node_worker.h#L90 https://github.com/nodejs/node/blob/v20.12.1/src/node_worker... It’s much more isolation than C threads – the entry point for a thread is a whole module (not a function), and threads must use message passing to communicate. They can share memory, but only via [Shared]ArrayBuffer objects. They're in the same OS process, but each have their own global process object. But I think it'd meet your need for an in-process isolated execution environment, which you can terminate from the main thread after a timeout.
- winrid 3y agoYes, this is what we do today. Was hoping for something a bit more ergonomic when you don't need full module level isolation.
- billywhizz 3y agoi'm pretty sure node.js does not enable v8 sandboxing by default. https://github.com/nodejs/node/blob/main/deps/v8/BUILD.gn#L336 https://github.com/nodejs/node/blob/main/deps/v8/BUILD.gn#L3...
- kevingadd 3y agoI would be concerned about terminating an isolate on timeout, couldn't it be holding mutexes when you terminate it?
- jhgg 3y agoV8 isolate termination is more akin to throwing an uncatchable exception, versus killing a thread abruptly. When you ask v8 to terminate an isolate, it does take some time for it to terminate depending on what code is running.
- amelius 3y ago> neither switching to a memory safe language, such as Rust, nor using current or future hardware memory safety features, such as memory tagging, can help with the security challenges faced by V8 today. And here I thought Rust would fix my security issues ...
- conradludgate 3y agoJIT engines are fundamentally unsafe since they would produce unsafe machine code directly. And for performance sake the runtime should use tactical unsafe. So memory safety is definitely still something to worry about, even if it's less likely to occur in my experience
- elwell 3y agoFizzbuzz with three conditions... criminal.
- deleted 3y ago[deleted]
- alberth 3y agoCloudflare Workers IIRC, Cloudflare Workers are based on V8 sandboxing ... which is in part why the cold starts are so fast. https://developers.cloudflare.com/workers/reference/how-workers-works/ https://developers.cloudflare.com/workers/reference/how-work...