From f960b73c0d4193e02dae8fed38a2130d3a71a618 Mon Sep 17 00:00:00 2001 From: Yohannes Desta Date: Sun, 15 Nov 2026 22:53:59 +0300 Subject: [PATCH] fs: reject a mode above INT32_MAX instead of aborting parseFileMode() accepted any 32-bit unsigned integer, but the native bindings read the mode as a signed 32-bit integer and CHECK that it is one. A mode between 2**31 and 2**32 - 1 therefore passed validation and then aborted the process from fs.open(), fs.chmod(), fs.mkdir() and their sync and promise variants. Validate the mode as a non-negative Int32 instead so that the caller gets a RangeError. ReadStream and WriteStream now validate options.mode in the constructor too, so an invalid mode throws where the stream is created instead of failing inside the open callback. Fixes: https://github.com/nodejs/node/issues/62516 Refs: https://github.com/nodejs/node/pull/62538 Co-authored-by: wei_ds Signed-off-by: Yohannes Desta --- lib/internal/fs/streams.js | 5 ++- lib/internal/validators.js | 3 +- test/parallel/test-fs-fchmod.js | 4 +- test/parallel/test-fs-lchmod.js | 4 +- .../test-fs-mode-out-of-int32-range.js | 45 +++++++++++++++++++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-fs-mode-out-of-int32-range.js diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index c8c86b59640a..af0a902026b6 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -27,6 +27,7 @@ const { kEmptyObject, } = require('internal/util'); const { + parseFileMode, validateBoolean, validateFunction, validateInteger, @@ -211,7 +212,7 @@ function ReadStream(path, options) { // Path will be ignored when fd is specified, so it can be falsy this.path = toPathIfFileURL(path); this.flags = options.flags === undefined ? 'r' : options.flags; - this.mode = options.mode === undefined ? 0o666 : options.mode; + this.mode = parseFileMode(options.mode, 'options.mode', 0o666); validatePath(this.path); } else { @@ -367,7 +368,7 @@ function WriteStream(path, options) { // Path will be ignored when fd is specified, so it can be falsy this.path = toPathIfFileURL(path); this.flags = options.flags === undefined ? 'w' : options.flags; - this.mode = options.mode === undefined ? 0o666 : options.mode; + this.mode = parseFileMode(options.mode, 'options.mode', 0o666); validatePath(this.path); } else { diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 1c4acd51613c..977ee1db1ec2 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -80,7 +80,8 @@ function parseFileMode(value, name, def) { value = NumberParseInt(value, 8); } - validateUint32(value, name); + // The C++ bindings read the mode as a signed 32-bit integer. + validateInt32(value, name, 0); // Coerce -0 to +0. return value + 0; } diff --git a/test/parallel/test-fs-fchmod.js b/test/parallel/test-fs-fchmod.js index 16425e7d1fe8..9ada3897b81b 100644 --- a/test/parallel/test-fs-fchmod.js +++ b/test/parallel/test-fs-fchmod.js @@ -42,12 +42,12 @@ assert.throws(() => fs.fchmod(1, '123x'), { assert.throws(() => fs.fchmodSync(input, 0o666), errObj); }); -[-1, 2 ** 32].forEach((input) => { +[-1, 2 ** 31].forEach((input) => { const errObj = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "mode" is out of range. It must be >= 0 && <= ' + - `4294967295. Received ${input}` + `2147483647. Received ${input}` }; assert.throws(() => fs.fchmod(1, input, () => {}), errObj); diff --git a/test/parallel/test-fs-lchmod.js b/test/parallel/test-fs-lchmod.js index d43971029149..0223353fd089 100644 --- a/test/parallel/test-fs-lchmod.js +++ b/test/parallel/test-fs-lchmod.js @@ -53,12 +53,12 @@ assert.throws(() => fs.lchmodSync(f, '123x'), { code: 'ERR_INVALID_ARG_VALUE' }); -[-1, 2 ** 32].forEach((input) => { +[-1, 2 ** 31].forEach((input) => { const errObj = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "mode" is out of range. It must be >= 0 && <= ' + - `4294967295. Received ${input}` + `2147483647. Received ${input}` }; assert.rejects(promises.lchmod(f, input, () => {}), errObj).then(common.mustCall()); diff --git a/test/parallel/test-fs-mode-out-of-int32-range.js b/test/parallel/test-fs-mode-out-of-int32-range.js new file mode 100644 index 000000000000..27599683b038 --- /dev/null +++ b/test/parallel/test-fs-mode-out-of-int32-range.js @@ -0,0 +1,45 @@ +'use strict'; + +// This tests that a mode above INT32_MAX is reported to the caller as a +// RangeError instead of failing a CHECK in the native binding and aborting +// the process. + +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const file = tmpdir.resolve('file.txt'); +fs.writeFileSync(file, 'x'); + +for (const mode of [2 ** 31, 2176057344, 2 ** 32 - 1]) { + const expected = (name) => ({ + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: `The value of "${name}" is out of range. It must be >= 0 && <= ` + + `2147483647. Received ${mode}`, + }); + assert.throws(() => fs.openSync(file, 'w', mode), expected('mode')); + assert.throws(() => fs.open(file, 'w', mode, common.mustNotCall()), + expected('mode')); + assert.rejects(fs.promises.open(file, 'w', mode), expected('mode')) + .then(common.mustCall()); + assert.throws(() => fs.chmodSync(file, mode), expected('mode')); + assert.throws(() => fs.mkdirSync(tmpdir.resolve('dir'), { mode }), + expected('options.mode')); + assert.throws( + () => fs.mkdirSync(tmpdir.resolve('a/b'), { recursive: true, mode }), + expected('options.mode'), + ); + assert.throws(() => fs.createReadStream(file, { mode }), + expected('options.mode')); + assert.throws(() => fs.createWriteStream(file, { mode }), + expected('options.mode')); +} + +// The largest signed 32-bit value is still accepted. +fs.closeSync(fs.openSync(file, 'w', 2 ** 31 - 1)); +fs.createWriteStream(file, { mode: 0o644 }) + .on('open', common.mustCall(function() { this.close(); }));