Skip to content

Commit 7dd8982

Browse files
tniessentargos
authored andcommittedJun 2, 2020
napi: fix memory corruption vulnerability
Fixes: https://hackerone.com/reports/784186 CVE-ID: CVE-2020-8174 PR-URL: nodejs-private/node-private#195 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent c6d0bda commit 7dd8982

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed
 

‎src/js_native_api_v8.cc

+9-3
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ napi_status napi_get_value_string_latin1(napi_env env,
22092209
if (!buf) {
22102210
CHECK_ARG(env, result);
22112211
*result = val.As<v8::String>()->Length();
2212-
} else {
2212+
} else if (bufsize != 0) {
22132213
int copied =
22142214
val.As<v8::String>()->WriteOneByte(env->isolate,
22152215
reinterpret_cast<uint8_t*>(buf),
@@ -2221,6 +2221,8 @@ napi_status napi_get_value_string_latin1(napi_env env,
22212221
if (result != nullptr) {
22222222
*result = copied;
22232223
}
2224+
} else if (result != nullptr) {
2225+
*result = 0;
22242226
}
22252227

22262228
return napi_clear_last_error(env);
@@ -2248,7 +2250,7 @@ napi_status napi_get_value_string_utf8(napi_env env,
22482250
if (!buf) {
22492251
CHECK_ARG(env, result);
22502252
*result = val.As<v8::String>()->Utf8Length(env->isolate);
2251-
} else {
2253+
} else if (bufsize != 0) {
22522254
int copied = val.As<v8::String>()->WriteUtf8(
22532255
env->isolate,
22542256
buf,
@@ -2260,6 +2262,8 @@ napi_status napi_get_value_string_utf8(napi_env env,
22602262
if (result != nullptr) {
22612263
*result = copied;
22622264
}
2265+
} else if (result != nullptr) {
2266+
*result = 0;
22632267
}
22642268

22652269
return napi_clear_last_error(env);
@@ -2288,7 +2292,7 @@ napi_status napi_get_value_string_utf16(napi_env env,
22882292
CHECK_ARG(env, result);
22892293
// V8 assumes UTF-16 length is the same as the number of characters.
22902294
*result = val.As<v8::String>()->Length();
2291-
} else {
2295+
} else if (bufsize != 0) {
22922296
int copied = val.As<v8::String>()->Write(env->isolate,
22932297
reinterpret_cast<uint16_t*>(buf),
22942298
0,
@@ -2299,6 +2303,8 @@ napi_status napi_get_value_string_utf16(napi_env env,
22992303
if (result != nullptr) {
23002304
*result = copied;
23012305
}
2306+
} else if (result != nullptr) {
2307+
*result = 0;
23022308
}
23032309

23042310
return napi_clear_last_error(env);

‎test/js-native-api/test_string/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ assert.throws(() => {
8181
assert.throws(() => {
8282
test_string.TestLargeUtf16();
8383
}, /^Error: Invalid argument$/);
84+
85+
test_string.TestMemoryCorruption(' '.repeat(64 * 1024));

‎test/js-native-api/test_string/test_string.c

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <limits.h> // INT_MAX
2+
#include <string.h>
23
#include <js_native_api.h>
34
#include "../common.h"
45

@@ -244,6 +245,24 @@ static napi_value TestLargeUtf16(napi_env env, napi_callback_info info) {
244245
return output;
245246
}
246247

248+
static napi_value TestMemoryCorruption(napi_env env, napi_callback_info info) {
249+
size_t argc = 1;
250+
napi_value args[1];
251+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
252+
253+
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
254+
255+
char buf[10] = { 0 };
256+
NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], buf, 0, NULL));
257+
258+
char zero[10] = { 0 };
259+
if (memcmp(buf, zero, sizeof(buf)) != 0) {
260+
NAPI_CALL(env, napi_throw_error(env, NULL, "Buffer overwritten"));
261+
}
262+
263+
return NULL;
264+
}
265+
247266
EXTERN_C_START
248267
napi_value Init(napi_env env, napi_value exports) {
249268
napi_property_descriptor properties[] = {
@@ -258,6 +277,7 @@ napi_value Init(napi_env env, napi_value exports) {
258277
DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8),
259278
DECLARE_NAPI_PROPERTY("TestLargeLatin1", TestLargeLatin1),
260279
DECLARE_NAPI_PROPERTY("TestLargeUtf16", TestLargeUtf16),
280+
DECLARE_NAPI_PROPERTY("TestMemoryCorruption", TestMemoryCorruption),
261281
};
262282

263283
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)
Please sign in to comment.