From 54ec1d09d375878e6b86e740440ba637f49fd9de Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 11 Mar 2024 21:43:55 -0400 Subject: [PATCH] fs: validate fd from cpp on `close` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/52051 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Paolo Insogna Reviewed-By: Mohammed Keyvanzadeh --- lib/fs.js | 4 ++-- src/node_file.cc | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2377ae68c6cfff..6bc0f7c32ee78f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -518,7 +518,7 @@ function close(fd, callback = defaultCloseCallback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.close(getValidatedFd(fd), req); + binding.close(fd, req); } /** @@ -527,7 +527,7 @@ function close(fd, callback = defaultCloseCallback) { * @returns {void} */ function closeSync(fd) { - binding.close(getValidatedFd(fd)); + binding.close(fd); } /** diff --git a/src/node_file.cc b/src/node_file.cc index e0b52a6af6161a..a8ae691d4b0453 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -984,8 +984,10 @@ void Close(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 1); - CHECK(args[0]->IsInt32()); - int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } env->RemoveUnmanagedFd(fd); if (argc > 1) { // close(fd, req)