Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to limit non-heap allocations (i.e. V8 "Large Object Storage" / Deno.memoryUsage().external) in Web Workers for untrusted code #26202

Open
josephrocca opened this issue Oct 13, 2024 · 1 comment
Labels
runtime Relates to code in the runtime crate suggestion suggestions for new features (yet to be agreed)

Comments

@josephrocca
Copy link
Contributor

josephrocca commented Oct 13, 2024

Context: I have a use case where I need to run some untrusted1 code in a Worker without it being able to crash the whole Deno process.

To briefly summarize:

  • Limiting heap memory: This is possible via a custom build using create_params.heap_limits and js_runtime.add_near_heap_limit_callback.
  • Limiting non-heap memory: If I understand correctly, V8 allocates large ArrayBuffers and typed arrays to a non-heap memory area - the "Large Object Space". I'm not sure if/how this can be limited in a robust way.
    • My current workaround: Add some code in the Worker that runs during init, which replaces ArrayBuffer and all typed arrays with copies that intercept and check that the bytes being allocated + Deno.memoryUsage().external will not exceed a threshold. Luckily Deno.memoryUsage().external does seem to be specific to the Web Worker that it's called from. But in general I'm not sure how full-proof this strategy is.

So I'm wondering:

  1. Is there currently any deno_core or rusty_v8 feature which could allow limiting the Large Object Space size for a Web Worker?
  2. Is there any chance of something like workerOptions.deno.memoryLimit.heap and workerOptions.deno.memoryLimit.external, so that a custom build wouldn't be required?

Related:


[1] By "untrusted" I don't mean that I need to worry about Spectre-type security issues. I just need to prevent it from crashing the process due to memory usage issues.

@littledivy littledivy added feat new feature (which has been agreed to/accepted) runtime Relates to code in the runtime crate labels Oct 13, 2024
@lucacasonato lucacasonato added suggestion suggestions for new features (yet to be agreed) and removed feat new feature (which has been agreed to/accepted) labels Oct 21, 2024
@lucacasonato
Copy link
Member

@josephrocca I think this falls outside of our security model. A user could allocate memory outside of V8's external heap, inside of the Rust heap. For example they could open a bunch of files.

It is too expensive for us to do heap size checks on every allocation in the entire system, so I suggest a better thing to do would be to use a subprocess and linux cgroups to enforce memory limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
runtime Relates to code in the runtime crate suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

No branches or pull requests

3 participants