worker: file creation races with process.umask()
This code is unsafe when worker threads are active:
| old = umask(0); | |
| umask(static_cast<mode_t>(old)); |
The umask(0) call temporarily changes the process-wide umask and races with fs operations from other threads.
Test case:
'use strict'; const { Worker, isMainThread } = require('worker_threads'); const { statSync, writeFileSync, unlinkSync } = require('fs'); function pummel() { for (let i = 0; i < 1e4; i++) process.umask(); setImmediate(pummel); } if (isMainThread) { process.umask(0o22); new Worker(__filename); pummel(); } else { const file = 'x.txt'; for (;;) { writeFileSync(file, 'ok', { mode: 0o666 }); const s = statSync(file); s.mode &= 0o777; if (0o644 !== s.mode) throw 'unexpected mode: ' + s.mode.toString(8); unlinkSync(file); } }
Fails within a few iterations with unexpected mode: 666
process.umask() (no arg) is allowed in workers so this test case works both ways.
This bug is potentially a security issue.