Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3580,7 +3580,8 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
bool block_flag = true, use_path = true;
static const char *std_name[3] = { "stdin", "stdout", "stderr" };
int std_fds[3];
uint32_t uid = -1, gid = -1;
uint32_t uid = 0, gid = 0;
bool uid_set = false, gid_set = false;
Comment on lines +3583 to +3584
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave uid and gid uninitialized, that way the compiler hopefully complains if we use them without initializing them first. Or does it give off false negatives?

The names uid_set and gid_set imply the action is already done. A better name is do_setuid or must_setuid or something along those lines.

int ngroups = -1;
gid_t groups[64];

Expand Down Expand Up @@ -3675,6 +3676,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
JS_FreeValue(ctx, val);
if (ret)
goto exception;
uid_set = true;
}

val = JS_GetPropertyStr(ctx, options, "gid");
Expand All @@ -3685,6 +3687,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
JS_FreeValue(ctx, val);
if (ret)
goto exception;
gid_set = true;
}

val = JS_GetPropertyStr(ctx, options, "groups");
Expand Down Expand Up @@ -3755,16 +3758,21 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
if (chdir(cwd) < 0)
_exit(127);
}
/* Drop privileges in the correct order: supplementary groups and the
primary gid must change before setuid(), because setuid(non-root)
strips the capability needed for setgroups()/setgid() to succeed.
Track "was set" with explicit bools instead of a (uint32_t)-1
sentinel, which collided with the legitimate value 0xFFFFFFFF. */
Comment on lines +3761 to +3765
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's got that waffling LLM quality to it and I don't like seeing that in code I have to look at.

Suggested change
/* Drop privileges in the correct order: supplementary groups and the
primary gid must change before setuid(), because setuid(non-root)
strips the capability needed for setgroups()/setgid() to succeed.
Track "was set" with explicit bools instead of a (uint32_t)-1
sentinel, which collided with the legitimate value 0xFFFFFFFF. */

if (ngroups != -1) {
if (setgroups(ngroups, groups) < 0)
_exit(127);
}
if (uid != -1) {
if (setuid(uid) < 0)
if (gid_set) {
if (setgid(gid) < 0)
_exit(127);
}
if (gid != -1) {
if (setgid(gid) < 0)
if (uid_set) {
if (setuid(uid) < 0)
_exit(127);
}

Expand Down
Loading