From cd9827f1054b0b24d1d015daf8ed2b4e78eb8e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 21 Apr 2020 10:21:29 -0700 Subject: [PATCH] napi: fix memory corruption vulnerability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://hackerone.com/reports/784186 CVE-ID: CVE-2020-8174 PR-URL: https://github.com/nodejs-private/node-private/pull/203 Reviewed-By: Beth Griggs Reviewed-By: Michael Dawson Reviewed-By: Tobias Nießen --- src/node_api.cc | 12 +++++++++--- test/addons-napi/test_string/test.js | 2 ++ test/addons-napi/test_string/test_string.c | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index a42ba963e60..c444ad91a5e 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2889,7 +2889,7 @@ napi_status napi_get_value_string_latin1(napi_env env, if (!buf) { CHECK_ARG(env, result); *result = val.As()->Length(); - } else { + } else if (bufsize != 0) { int copied = val.As()->WriteOneByte(env->isolate, reinterpret_cast(buf), @@ -2901,6 +2901,8 @@ napi_status napi_get_value_string_latin1(napi_env env, if (result != nullptr) { *result = copied; } + } else if (result != nullptr) { + *result = 0; } return napi_clear_last_error(env); @@ -2928,7 +2930,7 @@ napi_status napi_get_value_string_utf8(napi_env env, if (!buf) { CHECK_ARG(env, result); *result = val.As()->Utf8Length(env->isolate); - } else { + } else if (bufsize != 0) { int copied = val.As()->WriteUtf8( env->isolate, buf, @@ -2940,6 +2942,8 @@ napi_status napi_get_value_string_utf8(napi_env env, if (result != nullptr) { *result = copied; } + } else if (result != nullptr) { + *result = 0; } return napi_clear_last_error(env); @@ -2968,7 +2972,7 @@ napi_status napi_get_value_string_utf16(napi_env env, CHECK_ARG(env, result); // V8 assumes UTF-16 length is the same as the number of characters. *result = val.As()->Length(); - } else { + } else if (bufsize != 0) { int copied = val.As()->Write(env->isolate, reinterpret_cast(buf), 0, @@ -2979,6 +2983,8 @@ napi_status napi_get_value_string_utf16(napi_env env, if (result != nullptr) { *result = copied; } + } else if (result != nullptr) { + *result = 0; } return napi_clear_last_error(env); diff --git a/test/addons-napi/test_string/test.js b/test/addons-napi/test_string/test.js index 86fdc164060..89398c66e06 100644 --- a/test/addons-napi/test_string/test.js +++ b/test/addons-napi/test_string/test.js @@ -81,3 +81,5 @@ assert.throws(() => { assert.throws(() => { test_string.TestLargeUtf16(); }, /^Error: Invalid argument$/); + +test_string.TestMemoryCorruption(' '.repeat(64 * 1024)); diff --git a/test/addons-napi/test_string/test_string.c b/test/addons-napi/test_string/test_string.c index e682f53e096..e5f9a601c33 100644 --- a/test/addons-napi/test_string/test_string.c +++ b/test/addons-napi/test_string/test_string.c @@ -1,4 +1,5 @@ #include // INT_MAX +#include #include #include "../common.h" @@ -244,6 +245,24 @@ static napi_value TestLargeUtf16(napi_env env, napi_callback_info info) { return output; } +static napi_value TestMemoryCorruption(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc == 1, "Wrong number of arguments"); + + char buf[10] = { 0 }; + NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], buf, 0, NULL)); + + char zero[10] = { 0 }; + if (memcmp(buf, zero, sizeof(buf)) != 0) { + NAPI_CALL(env, napi_throw_error(env, NULL, "Buffer overwritten")); + } + + return NULL; +} + static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1), @@ -257,6 +276,7 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8), DECLARE_NAPI_PROPERTY("TestLargeLatin1", TestLargeLatin1), DECLARE_NAPI_PROPERTY("TestLargeUtf16", TestLargeUtf16), + DECLARE_NAPI_PROPERTY("TestMemoryCorruption", TestMemoryCorruption), }; NAPI_CALL(env, napi_define_properties(