From a30a15d69d2fe45a86d6ecd85ff3fbb4370b6ab2 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 11 Mar 2024 21:43:55 -0400 Subject: [PATCH 1/2] fs: validate fd from cpp on `close` --- 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 0377e0341e3efd..caeba5b55335f2 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -522,7 +522,7 @@ function close(fd, callback = defaultCloseCallback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.close(getValidatedFd(fd), req); + binding.close(fd, req); } /** @@ -531,7 +531,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 a3e80898cde6b6..fadbf1ff87a767 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -981,8 +981,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) From 71508f7125b70bcdb471e19240e7840bb72b71ad Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 11 Mar 2024 21:44:04 -0400 Subject: [PATCH 2/2] fs: validate fd from cpp on `fchown` --- 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 caeba5b55335f2..36e3deacbc8f75 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2034,7 +2034,7 @@ function fchown(fd, uid, gid, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.fchown(getValidatedFd(fd), uid, gid, req); + binding.fchown(fd, uid, gid, req); } /** @@ -2048,7 +2048,7 @@ function fchownSync(fd, uid, gid) { validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); - binding.fchown(getValidatedFd(fd), uid, gid); + binding.fchown(fd, uid, gid); } /** diff --git a/src/node_file.cc b/src/node_file.cc index fadbf1ff87a767..23e85448486423 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2593,8 +2593,10 @@ static void FChown(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 3); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } CHECK(IsSafeJsInt(args[1])); const uv_uid_t uid = static_cast(args[1].As()->Value());