Skip to content

Commit c63d986

Browse files
RafaelGSSaduh95
authored andcommitted
fs: do not disable fd-based APIs in permission audit mode
Refs: #65659 Refs: #65419 Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #66726 Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
1 parent 79405fc commit c63d986

4 files changed

Lines changed: 108 additions & 15 deletions

File tree

‎doc/api/permissions.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ both to the top-level `node:fs` functions and to the equivalent
285285
`FileHandle` methods, and currently includes `fsync`/`fdatasync`,
286286
`fchmod`, `fchown`, and `futimes` (and their synchronous variants).
287287

288+
In [audit mode](#audit-mode), these operations are not disabled and run normally. No
289+
diagnostics channel message is published for them.
290+
288291
#### Environment variable permissions
289292

290293
When the Permission Model is enforced, the process only has access to the

‎lib/fs.js‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,9 @@ function fdatasync(fd, callback) {
16121612
const req = new FSReqCallback();
16131613
req.oncomplete = callback;
16141614

1615-
if (permission.isEnabled()) {
1615+
// TODO(rafaelgss): publish a diagnostics channel event for fd-based APIs in
1616+
// audit mode.
1617+
if (permission.isEnabled() && !permission.isAuditMode()) {
16161618
callback(new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.'));
16171619
return;
16181620
}
@@ -1633,7 +1635,7 @@ function fdatasyncSync(fd) {
16331635
if (result !== undefined) return;
16341636
}
16351637

1636-
if (permission.isEnabled()) {
1638+
if (permission.isEnabled() && !permission.isAuditMode()) {
16371639
throw new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.');
16381640
}
16391641
binding.fdatasync(fd);
@@ -1654,7 +1656,7 @@ function fsync(fd, callback) {
16541656

16551657
const req = new FSReqCallback();
16561658
req.oncomplete = callback;
1657-
if (permission.isEnabled()) {
1659+
if (permission.isEnabled() && !permission.isAuditMode()) {
16581660
callback(new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.'));
16591661
return;
16601662
}
@@ -1674,7 +1676,7 @@ function fsyncSync(fd) {
16741676
if (result !== undefined) return;
16751677
}
16761678

1677-
if (permission.isEnabled()) {
1679+
if (permission.isEnabled() && !permission.isAuditMode()) {
16781680
throw new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.');
16791681
}
16801682
binding.fsync(fd);
@@ -2455,7 +2457,7 @@ function fchmod(fd, mode, callback) {
24552457
const h = vfsState.handlers;
24562458
if (h !== null && vfsVoid(h.fchmod(fd, mode), callback)) return;
24572459

2458-
if (permission.isEnabled()) {
2460+
if (permission.isEnabled() && !permission.isAuditMode()) {
24592461
callback(new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.'));
24602462
return;
24612463
}
@@ -2480,7 +2482,7 @@ function fchmodSync(fd, mode) {
24802482
if (result !== undefined) return;
24812483
}
24822484

2483-
if (permission.isEnabled()) {
2485+
if (permission.isEnabled() && !permission.isAuditMode()) {
24842486
throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.');
24852487
}
24862488
binding.fchmod(fd, mode);
@@ -2631,7 +2633,7 @@ function fchown(fd, uid, gid, callback) {
26312633
const h = vfsState.handlers;
26322634
if (h !== null && vfsVoid(h.fchown(fd), callback)) return;
26332635

2634-
if (permission.isEnabled()) {
2636+
if (permission.isEnabled() && !permission.isAuditMode()) {
26352637
callback(new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.'));
26362638
return;
26372639
}
@@ -2658,7 +2660,7 @@ function fchownSync(fd, uid, gid) {
26582660
if (result !== undefined) return;
26592661
}
26602662

2661-
if (permission.isEnabled()) {
2663+
if (permission.isEnabled() && !permission.isAuditMode()) {
26622664
throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.');
26632665
}
26642666

@@ -2772,7 +2774,7 @@ function futimes(fd, atime, mtime, callback) {
27722774
const h = vfsState.handlers;
27732775
if (h !== null && vfsVoid(h.futimes(fd, atime, mtime), callback)) return;
27742776

2775-
if (permission.isEnabled()) {
2777+
if (permission.isEnabled() && !permission.isAuditMode()) {
27762778
callback(new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.'));
27772779
return;
27782780
}
@@ -2801,7 +2803,7 @@ function futimesSync(fd, atime, mtime) {
28012803
if (result !== undefined) return;
28022804
}
28032805

2804-
if (permission.isEnabled()) {
2806+
if (permission.isEnabled() && !permission.isAuditMode()) {
28052807
throw new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.');
28062808
}
28072809

‎lib/internal/fs/promises.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,9 @@ async function rmdir(path, options) {
16861686
}
16871687

16881688
async function fdatasync(handle) {
1689-
if (permission.isEnabled()) {
1689+
// TODO(rafaelgss): publish a diagnostics channel event for fd-based APIs in
1690+
// audit mode.
1691+
if (permission.isEnabled() && !permission.isAuditMode()) {
16901692
throw new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.');
16911693
}
16921694
return await PromisePrototypeThen(
@@ -1697,7 +1699,7 @@ async function fdatasync(handle) {
16971699
}
16981700

16991701
async function fsync(handle) {
1700-
if (permission.isEnabled()) {
1702+
if (permission.isEnabled() && !permission.isAuditMode()) {
17011703
throw new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.');
17021704
}
17031705
return await PromisePrototypeThen(
@@ -1971,7 +1973,7 @@ async function unlink(path) {
19711973
}
19721974

19731975
async function fchmod(handle, mode) {
1974-
if (permission.isEnabled()) {
1976+
if (permission.isEnabled() && !permission.isAuditMode()) {
19751977
throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.');
19761978
}
19771979
mode = parseFileMode(mode, 'mode');
@@ -2033,7 +2035,7 @@ async function lchown(path, uid, gid) {
20332035
async function fchown(handle, uid, gid) {
20342036
validateInteger(uid, 'uid', -1, kMaxUserId);
20352037
validateInteger(gid, 'gid', -1, kMaxUserId);
2036-
if (permission.isEnabled()) {
2038+
if (permission.isEnabled() && !permission.isAuditMode()) {
20372039
throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.');
20382040
}
20392041
return await PromisePrototypeThen(
@@ -2079,7 +2081,7 @@ async function utimes(path, atime, mtime) {
20792081
}
20802082

20812083
async function futimes(handle, atime, mtime) {
2082-
if (permission.isEnabled()) {
2084+
if (permission.isEnabled() && !permission.isAuditMode()) {
20832085
throw new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.');
20842086
}
20852087
atime = toUnixTimestamp(atime, 'atime');
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { isMainThread } = require('worker_threads');
5+
6+
if (!isMainThread) {
7+
common.skip('This test only works on a main thread');
8+
}
9+
10+
const assert = require('assert');
11+
const fs = require('fs');
12+
const { spawnSync } = require('child_process');
13+
const { test } = require('node:test');
14+
const fixtures = require('../common/fixtures');
15+
const tmpdir = require('../common/tmpdir');
16+
17+
tmpdir.refresh();
18+
19+
const target = tmpdir.resolve('audit-fd-apis.txt');
20+
fs.copyFileSync(fixtures.path('permission', 'deny', 'regular-file.md'), target);
21+
22+
function run(flag, op) {
23+
const childScript = `
24+
const fs = require('node:fs');
25+
(async () => {
26+
const fd = fs.openSync(process.env.TARGET, 'r+');
27+
const fh = await fs.promises.open(process.env.TARGET, 'r+');
28+
const cb = (fn, ...args) => new Promise((resolve, reject) => {
29+
fn(fd, ...args, (err) => err ? reject(err) : resolve());
30+
});
31+
try {
32+
await (${op});
33+
console.log('RESULT NO_THROW');
34+
} catch (e) {
35+
console.log('RESULT THREW ' + e.code);
36+
} finally {
37+
await fh.close();
38+
fs.closeSync(fd);
39+
}
40+
})();
41+
`;
42+
const { status, stdout, stderr } = spawnSync(
43+
process.execPath,
44+
[
45+
flag,
46+
'--allow-fs-read=*',
47+
'--allow-fs-write=*',
48+
'--allow-env=TARGET',
49+
'-e',
50+
childScript,
51+
],
52+
{ encoding: 'utf8', env: { ...process.env, TARGET: target } },
53+
);
54+
assert.strictEqual(status, 0, stderr);
55+
const resultLine = stdout.split('\n').find((l) => l.startsWith('RESULT '));
56+
assert.ok(resultLine, stdout);
57+
return resultLine.replace('RESULT ', '');
58+
}
59+
60+
const ops = [
61+
['fs.fsyncSync()', 'fs.fsyncSync(fd)'],
62+
['fs.fsync()', 'cb(fs.fsync)'],
63+
['filehandle.sync()', 'fh.sync()'],
64+
['fs.fdatasyncSync()', 'fs.fdatasyncSync(fd)'],
65+
['fs.fdatasync()', 'cb(fs.fdatasync)'],
66+
['filehandle.datasync()', 'fh.datasync()'],
67+
['fs.fchmodSync()', 'fs.fchmodSync(fd, 0o644)'],
68+
['fs.fchmod()', 'cb(fs.fchmod, 0o644)'],
69+
['filehandle.chmod()', 'fh.chmod(0o644)'],
70+
['fs.fchownSync()', 'fs.fchownSync(fd, -1, -1)'],
71+
['fs.fchown()', 'cb(fs.fchown, -1, -1)'],
72+
['filehandle.chown()', 'fh.chown(-1, -1)'],
73+
['fs.futimesSync()', 'fs.futimesSync(fd, 1, 1)'],
74+
['fs.futimes()', 'cb(fs.futimes, 1, 1)'],
75+
['filehandle.utimes()', 'fh.utimes(1, 1)'],
76+
];
77+
78+
for (const [api, op] of ops) {
79+
test(`audit mode does not disable ${api}`, () => {
80+
assert.strictEqual(run('--permission-audit', op), 'NO_THROW');
81+
});
82+
83+
test(`enforce mode still disables ${api}`, () => {
84+
assert.strictEqual(run('--permission', op), 'THREW ERR_ACCESS_DENIED');
85+
});
86+
}

0 commit comments

Comments
 (0)