fs: fix out-of-bounds write in mkdtemp for long prefixes

Mkdtemp() allocated the template buffer as `length + strlen("XXXXXX")`,
leaving no room for the terminating NUL byte. For a single-byte prefix
long enough to force the heap allocation path (length + 6 > the
stack-buffer threshold), the terminating NUL was written one byte past
the end of the buffer -- a 1-byte heap-buffer-overflow flagged by
AddressSanitizer.

Allocate room for the terminating NUL, copy the suffix, and use
SetLengthAndZeroTerminate to set the correct length and write the
terminator, following the MaybeStackBuffer paradigm used elsewhere in
this file.

Signed-off-by: frandle331-yh <s1240100@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64770
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
This commit is contained in:
Hierax_Umbra authored and Antoine du Hamel committed 2026-08-26 00:56:14 +02:00
1 parent 109566a2f7
commit 992af05719
1 file changed
+5 -4
+5 -4
View File
@@ -3649,10 +3649,11 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(argc, 2);
BufferValue tmpl(isolate, args[0]);
static constexpr const char* const suffix = "XXXXXX";
const auto length = tmpl.length();
tmpl.AllocateSufficientStorage(length + strlen(suffix));
snprintf(tmpl.out() + length, tmpl.length(), "%s", suffix);
const auto prefix_length = tmpl.length();
static constexpr std::string_view suffix = "XXXXXX";
tmpl.AllocateSufficientStorage(prefix_length + suffix.size() + 1);
memcpy(tmpl.out() + prefix_length, suffix.data(), suffix.size());
tmpl.SetLengthAndZeroTerminate(prefix_length + suffix.size());
CHECK_NOT_NULL(*tmpl);