Skip to content
RCE Write-up Advanced

Enter "Sandbreak" - Vulnerability In vm2 Sandbox Module Enables Remote Code Execution (CVE-2022-36067)

A vulnerability in the vm2 sandbox library allowed an attacker to bypass the sandbox and execute arbitrary shell commands on the host machine. The flaw involved overriding the Error object's prepareStackTrace method to access CallSite objects from outside the sandbox.

Oxeye Follow

Published on oxeye.io

The Oxeye research team has found "Sandbreak", a critical remote code execution vulnerability in the popular sandbox library vm2. The vulnerability was disclosed to the project owners and was rapidly patched in version 3.9.11.

Oxeye (@OxeyeSecurity), on oxeye.io

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

vm2 is a JavaScript sandbox library, distributed through NPM, that gives an application an isolated environment in which to run untrusted code. The Oxeye research team describes it as widely used, with more than 16 million monthly downloads according to the NPM package manager. Sandboxes of this kind appear in places such as examining attached files in email servers, adding a security layer in web browsers, or isolating running applications in certain operating systems. The premise of the library is that code inside the sandbox cannot reach the host.

The research team found a critical remote code execution vulnerability in vm2, which they named "Sandbreak". It was disclosed to the project owners and rapidly patched in version 3.9.11. GitHub issued CVE-2022-36067 for it, with the maximum CVSS score of 10.0.

From the original on oxeye.io

Setup

The team says it uses its own commercial analysis product during vulnerability research. The relevant precondition is simply an application that runs attacker-supplied JavaScript inside vm2: if the attacker can get code executed in the sandbox, the question is whether they can get out of it.

Finding it

The team's stated criteria for choosing research topics are pervasiveness (how wide-reaching the vulnerability is) and impact (how severe the consequences can be if it is exploited). Sandboxes attracted them precisely because they are trusted by definition as safe places that isolate dangerous code from the application. The thesis driving the work was: what happens if that trust is compromised?

Their usual approach when evaluating a piece of software is to analyse the previous security lapses found in the same software. That gives a picture of the available attack surface, can surface low-hanging bugs from incomplete fixes, and helps in devising techniques to bypass the fixes that were implemented. While reviewing previous bugs disclosed to the vm2 maintainers, they noticed an interesting technique: the bug reporter had abused the error mechanism in Node.js to escape the sandbox.

Node.js lets the application developer customise the call stack of an error that occurred in the application. This is done by implementing the prepareStackTrace method under the global Error object. When an error occurs and the stack property of the thrown error object is accessed, Node.js calls this method, providing it with a string representation of the error alongside an array of CallSite objects as arguments. The screenshot below shows Node.js attempting to call the prepareStackTrace function:

From the original on oxeye.io

Each CallSite object in the array represents a different stack frame, and together they comprise the call stack state at the moment the error occurred. One of the methods exposed by CallSite objects is getThis, which returns the this object that was available in the related stack frame. This behaviour can lead to sandbox escapes, because some of the CallSite objects may return objects created outside the sandbox when getThis is invoked. Once an attacker holds a CallSite object created outside the sandbox, it may be possible to reach Node's global objects and execute arbitrary system commands from there.

The vm2 maintainers were aware that overriding prepareStackTrace could lead to a sandbox escape, and tried to mitigate this escape path by wrapping the Error object and the prepareStackTrace method with their own implementation, which prevents users from overriding the method. The earlier reporter's proof of concept bypassed that logic because vm2 missed wrapping specific methods related to the WeakMap JavaScript built-in type. That let the attacker supply their own implementation of prepareStackTrace, trigger an error, and escape the sandbox.

Exploitation

Step 1: understand the target mechanism

The escape hinges on the Error object's prepareStackTrace function. Providing your own implementation of it and then triggering an error results in a sandbox escape, because the implementation receives CallSite objects that can belong to frames outside the sandbox.

Step 2: override the global Error object

Rather than finding a way to override prepareStackTrace itself, the team tried a simpler route: override the global Error object with their own object that implements the prepareStackTrace function. The following code would result in their own implementation being called:

From the original on oxeye.io

Step 3: reach outside the sandbox and run commands

With the implementation in place, the only thing left is to access the CallSite object of a frame that resides outside the sandbox. From there, Node's global members are reachable, and through them the currently executing process, which allows commands to be executed:

From the original on oxeye.io

Root cause

The mistake is in how vm2 tried to defend the error mechanism. Wrapping the Error object and the prepareStackTrace method with its own implementation blocks the direct override that the earlier bug used, but the wrapping was incomplete: specific methods related to the WeakMap built-in type were missed. That gap allowed an attacker to supply their own prepareStackTrace implementation anyway. Once attacker-controlled code runs as the stack-trace preparer, Node.js hands it CallSite objects for frames outside the sandbox, and getThis on such a frame yields an object created outside the sandbox. From that object, Node's globals and the running process are reachable. The defence did not stop the attack because vm2 missed wrapping specific methods related to the WeakMap built-in type.

Impact

A threat actor who exploits the vulnerability can bypass the vm2 sandbox environment and run shell commands on the machine hosting the sandbox. Given the use cases for sandboxes, the report argues the consequences for applications that use vm2 can be dire. The maximum CVSS score of 10.0 combined with the library's popularity means the potential impact is widespread and critical. The exposure depends on an application actually running untrusted code through vm2; the report does not enumerate specific affected products.

Fix

The vulnerability was rapidly patched in vm2 version 3.9.11. The report does not describe the code change itself. Beyond the patch, the authors recommend that if a sandbox is unavoidable, you separate the logically sensitive part of your application from the microservice that runs the sandboxed code, so that a successful breakout leaves the attacker with only the isolated microservice as attack surface. They also advise avoiding a sandbox that relies on a dynamic language such as JavaScript where possible, since the dynamic nature of the language widens the attack surface and makes defence harder. Finally, they note that researchers are more likely to look at high-profile dependencies, so dependencies should be monitored frequently and upgraded accordingly.

  1. Override the global Error object with an object implementing prepareStackTrace, then trigger an error and read the stack property. A positive result is your implementation being invoked with CallSite objects, and getThis on a frame outside the sandbox returning an object that leads to Node globals.
  2. Look for built-ins that the sandbox wrapper does not cover, in the way vm2 missed WeakMap-related methods. A positive result is any path that lets you install your own prepareStackTrace despite the wrapper.
  3. Review past advisories for the same product and check whether each fix covers every route to the abused primitive, not just the reported one. A positive result is a fix that blocks the original payload but leaves an equivalent route open.
  4. When assessing severity in a real application, distinguish dependencies that are merely installed from those actually loaded and used. The report's own tooling shows the same vulnerability found three times in a staging environment with a different severity for each instance:
From the original on oxeye.io

Discovery and disclosure timeline

From the original on oxeye.io

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

A security research team at Oxeye found a way to break out of a JavaScript sandbox called vm2. They named the technique "Sandbreak". vm2 is a library that many applications use to run code they do not trust in a safe, isolated space. The team showed that an attacker who exploits the flaw can escape the sandbox and run shell commands on the machine hosting it. The problem was reported to the people who maintain vm2, and they fixed it quickly in version 3.9.11. The vulnerability was given the identifier CVE-2022-36067 and the maximum severity score, CVSS 10.0.

From the original on oxeye.io

This article explains the whole story for someone who has never studied security. It covers what a sandbox is, why vm2 exists, how the researchers found the flaw, how the escape works step by step, what the impact is, how it was fixed, and what lessons apply to anyone running software.

What is vm2 and why does it matter?

vm2 is a JavaScript sandbox library. A library is a collection of code that other programs can reuse instead of writing everything from scratch. JavaScript is a programming language used heavily on the web and on servers (through a runtime called Node.js). A sandbox is an isolated environment where code can run without being able to affect the rest of the system, much like a child's sandbox keeps play contained within its walls. The idea is that even if the code is malicious, it cannot reach the host machine, its files, or its network.

vm2 is distributed through NPM, the package manager for JavaScript. A package manager is a tool that downloads and installs libraries for you. According to NPM, vm2 has more than 16 million monthly downloads, which makes it very widely used. Sandboxes like this appear in email servers that examine attachments, in web browsers as an extra security layer, and in operating systems that isolate applications. The whole point is that code inside the sandbox cannot reach the host.

The Oxeye team found a critical remote code execution (RCE) vulnerability in vm2. RCE means an attacker can make the machine run commands of their choosing, as if they were sitting at the keyboard. The vulnerability was disclosed to the project owners and rapidly patched in version 3.9.11. GitHub issued CVE-2022-36067 for it, with the maximum CVSS score of 10.0. CVE is a public catalogue number for a known vulnerability. CVSS is a scoring system from 0 to 10 that rates severity; 10.0 is the highest possible.

Why would anyone run untrusted code?

Sometimes an application needs to run code that a user supplies. Running such code directly is dangerous because the user could abuse it to take over the application. A sandbox like vm2 is meant to remove that risk by keeping the code isolated.

The term "sandbox" refers to an isolated environment within which untrusted code can run, to reduce the risk of malicious code affecting the host machine. Sandboxes are useful, but they must be used with caution because it is possible to bypass the restrictions, as this research shows.

How the researchers approached the problem

The Oxeye team chooses research topics based on two principles:

  1. Pervasiveness, how widely is the software used?
  2. Impact, how severe are the consequences if the vulnerability is exploited?

Sandboxes caught their attention because they are trusted by definition as safe places that isolate dangerous code. The team asked: what happens if that trust is broken?

Their usual method when evaluating software security is to first analyse previous security lapses in the same software. This helps them understand the attack surface (all the ways an attacker could try to interact with the system), can reveal easy bugs left by incomplete fixes, and helps them devise ways to bypass the fixes that were implemented. While reviewing previous bugs reported to the vm2 maintainers, they noticed an interesting technique: a previous reporter had abused the error mechanism in Node.js to escape the sandbox.

The error mechanism in Node.js

Node.js lets a developer customise the call stack of an error. The call stack is the list of function calls that led to the current point in the program, like a stack of plates where each plate is a function call. When an error occurs, Node.js can produce a stack trace, which is a report of that call stack. By default, Node.js builds this report itself, but a developer can provide their own function to build it. This is done by implementing a method called prepareStackTrace on the global Error object. The global Error object is the built-in object that represents errors in JavaScript; every error is an instance of it.

When an error occurs and the stack property of the thrown error object is accessed, Node.js calls this prepareStackTrace method. It passes two arguments: a string representation of the error, and an array of CallSite objects. A CallSite object represents one frame in the call stack. A frame is one function call in the stack. The screenshot below shows Node.js attempting to call the prepareStackTrace function:

From the original on oxeye.io

Each CallSite object in the array represents a different stack frame. Together they describe the state of the call stack at the moment the error occurred. One of the methods exposed by CallSite objects is getThis, which returns the this object that was available in the related stack frame. In JavaScript, this refers to the object that a function is currently operating on. This behaviour can lead to sandbox escapes because some CallSite objects may return objects created outside the sandbox when getThis is invoked. Once an attacker holds a CallSite object created outside the sandbox, it may be possible to reach Node's global objects and execute arbitrary system commands from there.

The vm2 maintainers knew that overriding prepareStackTrace could lead to a sandbox escape. They tried to mitigate this by wrapping the Error object and the prepareStackTrace method with their own implementation, which prevents users from overriding the method. Wrapping here means replacing the original with a controlled version that checks or restricts what can be done. The earlier reporter's proof of concept bypassed that logic because vm2 missed wrapping specific methods related to the WeakMap JavaScript built-in type. WeakMap is a built-in JavaScript collection that stores key-value pairs and allows the keys to be garbage-collected when no longer used. That gap allowed an attacker to supply their own implementation of prepareStackTrace, trigger an error, and escape the sandbox.

Escaping the sandbox step by step

Step 1: understand the target mechanism

The escape depends on the Error object's prepareStackTrace function. If you can provide your own implementation of it and then trigger an error, you can escape the sandbox, because your implementation receives CallSite objects that can belong to frames outside the sandbox. The researchers understood this from the previous bug report.

Step 2: override the global Error object

Instead of finding a way to override prepareStackTrace itself, the team tried a simpler route: override the global Error object with their own object that implements the prepareStackTrace function. The following code would result in their own implementation being called:

From the original on oxeye.io

Then, when any error occurs and its stack is accessed, Node.js calls their method instead of the original one.

Step 3: reach outside the sandbox and run commands

With their implementation in place, the only thing left is to access the CallSite object of a frame that resides outside the sandbox. From there, Node's global members are reachable, and through them the currently executing process, which allows commands to be executed:

From the original on oxeye.io

From that object, they can reach the process object, which represents the running Node.js process and provides ways to execute system commands.

Why did the defence fail?

The mistake is in how vm2 tried to defend the error mechanism. Wrapping the Error object and the prepareStackTrace method with its own implementation blocks the direct override that the earlier bug used, but the wrapping was incomplete: specific methods related to the WeakMap built-in type were missed. That gap allowed an attacker to supply their own prepareStackTrace implementation anyway. Once attacker-controlled code runs as the stack-trace preparer, Node.js hands it CallSite objects for frames outside the sandbox, and getThis on such a frame yields an object created outside the sandbox. From that object, Node's globals and the running process are reachable. The defence did not stop the attack because vm2 missed wrapping specific methods related to the WeakMap built-in type.

What is the impact?

A threat actor who exploits this vulnerability can bypass the vm2 sandbox environment and run shell commands on the machine hosting the sandbox. Given the use cases for sandboxes, the report argues the consequences for applications that use vm2 can be dire. The maximum CVSS score of 10.0 combined with the library's popularity means the potential impact is widespread and critical. The exposure depends on an application actually running untrusted code through vm2; the report does not enumerate specific affected products.

How was it fixed?

The vulnerability was rapidly patched in vm2 version 3.9.11. The report does not describe the code change itself. Beyond the patch, the authors recommend that if a sandbox is unavoidable, you separate the logically sensitive part of your application from the microservice that runs the sandboxed code, so that a successful breakout leaves the attacker with only the isolated microservice as attack surface. A microservice is a small, independent part of an application that can be isolated from the rest. They also advise avoiding a sandbox that relies on a dynamic language such as JavaScript where possible, since the dynamic nature of the language widens the attack surface and makes defence harder. Finally, they note that researchers are more likely to look at high-profile dependencies, so dependencies should be monitored frequently and upgraded accordingly. A dependency is a library that your application relies on.

  1. Override the global Error object with an object implementing prepareStackTrace, then trigger an error and read the stack property. A positive result is your implementation being invoked with CallSite objects, and getThis on a frame outside the sandbox returning an object that leads to Node globals.
  2. Look for built-ins that the sandbox wrapper does not cover, in the way vm2 missed WeakMap-related methods. A positive result is any path that lets you install your own prepareStackTrace despite the wrapper.
  3. Review past advisories for the same product and check whether each fix covers every route to the abused primitive, not just the reported one. A positive result is a fix that blocks the original payload but leaves an equivalent route open.
  4. When assessing severity in a real application, distinguish dependencies that are merely installed from those actually loaded and used. The report's own tooling shows the same vulnerability found three times in a staging environment with a different severity for each instance:
From the original on oxeye.io

Discovery and disclosure timeline

From the original on oxeye.io

Glossary

  • API: Application Programming Interface; a set of rules that lets one piece of software talk to another.
  • Attack surface: all the ways an attacker could try to interact with a system.
  • Call stack: the list of function calls that led to the current point in a program.
  • CallSite object: an object representing one frame in a call stack, with methods like getThis.
  • CVE: Common Vulnerabilities and Exposures; a public catalogue number for a known vulnerability.
  • CVSS: Common Vulnerability Scoring System; a 0 to 10 score for severity.
  • Dependency: a library that an application relies on.
  • Dynamic language: a programming language that can change its behaviour at runtime, such as JavaScript.
  • Frame: one function call in a call stack.
  • Global Error object: the built-in JavaScript object that represents errors.
  • JavaScript: a programming language used on the web and on servers.
  • Library: a collection of reusable code.
  • Microservice: a small, independent part of an application that can be isolated.
  • NPM: the package manager for JavaScript.
  • Package manager: a tool that downloads and installs libraries.
  • prepareStackTrace: a method on the Error object that Node.js calls to build a stack trace.
  • Proof of concept (POC): a demonstration that a vulnerability can be exploited.
  • Remote code execution (RCE): a vulnerability that lets an attacker run commands on a machine.
  • Sandbox: an isolated environment where code can run without affecting the rest of the system.
  • Stack trace: a report of the call stack at the moment an error occurred.
  • WeakMap: a built-in JavaScript collection that stores key-value pairs and allows keys to be garbage-collected.
  • Wrapping: replacing a function or object with a controlled version that restricts what can be done.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.