buffer: avoid overrun on UCS-2 string write

CVE-2018-12115
Discovered by ChALkeR - Сковорода Никита Андреевич
Fix by Anna Henningsen

Writing to the second-to-last byte with UCS-2 encoding will cause a -1
length to be send to String::Write(), writing all of the provided Buffer
from that point and beyond.

Fixes: https://github.com/nodejs-private/security/issues/203
PR-URL: https://github.com/nodejs-private/node-private/pull/138
This commit is contained in:
Rod Vagg committed 2018-08-15 17:49:13 +10:00
1 parent 3139897ff5
commit fc14d812b7
2 files changed
+26 -1

No files matched your search

+5 -1
View File
@@ -309,7 +309,11 @@ size_t StringBytes::WriteUCS2(char* buf,
size_t* chars_written) {
uint16_t* const dst = reinterpret_cast<uint16_t*>(buf);
size_t max_chars = (buflen / sizeof(*dst));
size_t max_chars = buflen / sizeof(*dst);
if (max_chars == 0) {
return 0;
}
size_t nchars;
size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
if (alignment == 0) {