Skip to content

Commit 803fbfb

Browse files
tniessendanielleadams
authored andcommitted
process: fix uid/gid validation to avoid crash
id |= 0 turns unsigned 32-bit integer values exceeding the unsigned 31-bit range into negative integers, causing a crash. Use id >>>= 0 instead, which works properly for all unsigned 32-bit integers. Refs: #36786 PR-URL: #44910 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9f2dd48 commit 803fbfb

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

lib/internal/bootstrap/switches/does_own_process_state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function wrapPosixCredentialSetters(credentials) {
7676
function wrapIdSetter(type, method) {
7777
return function(id) {
7878
validateId(id, 'id');
79-
if (typeof id === 'number') id |= 0;
79+
if (typeof id === 'number') id >>>= 0;
8080
// Result is 0 on success, 1 if credential is unknown.
8181
const result = method(id);
8282
if (result === 1) {

test/parallel/test-process-uid-gid.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,13 @@ assert.throws(() => {
5353

5454
// Passing -0 shouldn't crash the process
5555
// Refs: https://github.com/nodejs/node/issues/32750
56-
try { process.setuid(-0); } catch {
57-
// Continue regardless of error.
58-
}
59-
try { process.seteuid(-0); } catch {
60-
// Continue regardless of error.
61-
}
62-
try { process.setgid(-0); } catch {
63-
// Continue regardless of error.
64-
}
65-
try { process.setegid(-0); } catch {
66-
// Continue regardless of error.
56+
// And neither should values exceeding 2 ** 31 - 1.
57+
for (const id of [-0, 2 ** 31, 2 ** 32 - 1]) {
58+
for (const fn of [process.setuid, process.setuid, process.setgid, process.setegid]) {
59+
try { fn(id); } catch {
60+
// Continue regardless of error.
61+
}
62+
}
6763
}
6864

6965
// If we're not running as super user...

0 commit comments

Comments
 (0)