From 992af05719ebe47c95f9bb8cd3345bfc1f2ca6ac Mon Sep 17 00:00:00 2001 From: Hierax_Umbra <72476163+frandle331-yh@users.noreply.github.com> Date: Fri, 21 Aug 2026 08:56:28 +0900 Subject: [PATCH] fs: fix out-of-bounds write in mkdtemp for long prefixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 PR-URL: https://github.com/nodejs/node/pull/64770 Reviewed-By: René --- src/node_file.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index f7437da0f2b..871d8f16bd3 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3649,10 +3649,11 @@ static void Mkdtemp(const FunctionCallbackInfo& 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);