Skip to content

Commit

Permalink
fs: fix valid id range on chown, lchown, fchown
Browse files Browse the repository at this point in the history
PR-URL: #31694
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
himself65 authored and codebytere committed Mar 30, 2020
1 parent 3f1b916 commit de5d162
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
29 changes: 15 additions & 14 deletions lib/fs.js
Expand Up @@ -102,9 +102,10 @@ const {
parseMode,
validateBuffer,
validateInteger,
validateInt32,
validateUint32
validateInt32
} = require('internal/validators');
// 2 ** 32 - 1
const kMaxUserId = 4294967295;

let truncateWarn = true;
let fs;
Expand Down Expand Up @@ -1124,26 +1125,26 @@ function chmodSync(path, mode) {
function lchown(path, uid, gid, callback) {
callback = makeCallback(callback);
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const req = new FSReqCallback();
req.oncomplete = callback;
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req);
}

function lchownSync(path, uid, gid) {
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
}

function fchown(fd, uid, gid, callback) {
validateInt32(fd, 'fd', 0);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const req = new FSReqCallback();
req.oncomplete = makeCallback(callback);
Expand All @@ -1152,8 +1153,8 @@ function fchown(fd, uid, gid, callback) {

function fchownSync(fd, uid, gid) {
validateInt32(fd, 'fd', 0);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const ctx = {};
binding.fchown(fd, uid, gid, undefined, ctx);
Expand All @@ -1163,8 +1164,8 @@ function fchownSync(fd, uid, gid) {
function chown(path, uid, gid, callback) {
callback = makeCallback(callback);
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const req = new FSReqCallback();
req.oncomplete = callback;
Expand All @@ -1173,8 +1174,8 @@ function chown(path, uid, gid, callback) {

function chownSync(path, uid, gid) {
path = getValidatedPath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-fchown.js
Expand Up @@ -44,16 +44,16 @@ function testGid(input, errObj) {
testGid(input, errObj);
});

[-1, 2 ** 32].forEach((input) => {
[-2, 2 ** 32].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "fd" is out of range. It must be ' +
`>= 0 && <= 2147483647. Received ${input}`
};
testFd(input, errObj);
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
`< 4294967296. Received ${input}`;
errObj.message = 'The value of "uid" is out of range. It must be >= -1 && ' +
`<= 4294967295. Received ${input}`;
testUid(input, errObj);
errObj.message = errObj.message.replace('uid', 'gid');
testGid(input, errObj);
Expand Down

0 comments on commit de5d162

Please sign in to comment.