Skip to content

Commit 01e9eac

Browse files
committed
lib,permission: disable fchmod/fchown when pm enabled
PR-URL: nodejs-private/node-private#584 Refs: https://hackerone.com/reports/2472071 CVE-ID: CVE-2024-36137
1 parent d39e993 commit 01e9eac

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/fs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,11 @@ function fchmod(fd, mode, callback) {
18771877
mode = parseFileMode(mode, 'mode');
18781878
callback = makeCallback(callback);
18791879

1880+
if (permission.isEnabled()) {
1881+
callback(new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.'));
1882+
return;
1883+
}
1884+
18801885
const req = new FSReqCallback();
18811886
req.oncomplete = callback;
18821887
binding.fchmod(fd, mode, req);
@@ -1889,6 +1894,9 @@ function fchmod(fd, mode, callback) {
18891894
* @returns {void}
18901895
*/
18911896
function fchmodSync(fd, mode) {
1897+
if (permission.isEnabled()) {
1898+
throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.');
1899+
}
18921900
binding.fchmod(
18931901
fd,
18941902
parseFileMode(mode, 'mode'),
@@ -2012,6 +2020,10 @@ function fchown(fd, uid, gid, callback) {
20122020
validateInteger(uid, 'uid', -1, kMaxUserId);
20132021
validateInteger(gid, 'gid', -1, kMaxUserId);
20142022
callback = makeCallback(callback);
2023+
if (permission.isEnabled()) {
2024+
callback(new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.'));
2025+
return;
2026+
}
20152027

20162028
const req = new FSReqCallback();
20172029
req.oncomplete = callback;
@@ -2028,6 +2040,9 @@ function fchown(fd, uid, gid, callback) {
20282040
function fchownSync(fd, uid, gid) {
20292041
validateInteger(uid, 'uid', -1, kMaxUserId);
20302042
validateInteger(gid, 'gid', -1, kMaxUserId);
2043+
if (permission.isEnabled()) {
2044+
throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.');
2045+
}
20312046

20322047
binding.fchown(fd, uid, gid);
20332048
}

test/fixtures/permission/fs-write.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,32 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
462462
permission: 'FileSystemWrite',
463463
resource: path.toNamespacedPath(blockedFile),
464464
});
465+
}
466+
467+
// fs.fchown with read-only fd
468+
{
469+
assert.throws(() => {
470+
// blocked file is allowed to read
471+
const fd = fs.openSync(blockedFile, 'r');
472+
fs.fchmod(fd, 777, common.expectsError({
473+
code: 'ERR_ACCESS_DENIED',
474+
}));
475+
fs.fchmodSync(fd, 777);
476+
}, {
477+
code: 'ERR_ACCESS_DENIED',
478+
});
479+
}
480+
481+
// fs.fchmod with read-only fd
482+
{
483+
assert.throws(() => {
484+
// blocked file is allowed to read
485+
const fd = fs.openSync(blockedFile, 'r');
486+
fs.fchown(fd, 999, 999, common.expectsError({
487+
code: 'ERR_ACCESS_DENIED',
488+
}));
489+
fs.fchownSync(fd, 999, 999);
490+
}, {
491+
code: 'ERR_ACCESS_DENIED',
492+
});
465493
}

0 commit comments

Comments
 (0)