Skip to content

Make os.exec support supplementary groups #1056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/docs/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ object containing optional parameters:
process.
- `uid` - Integer. If present, the process uid with `setuid`.
- `gid` - Integer. If present, the process gid with `setgid`.
- `groups` - Array of integer. If present, the supplementary
group IDs with `setgroup`.

### `waitpid(pid, options)`

Expand Down
41 changes: 41 additions & 0 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include <termios.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <grp.h>
#endif

#if defined(__APPLE__)
Expand Down Expand Up @@ -3236,6 +3237,8 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
static const char *std_name[3] = { "stdin", "stdout", "stderr" };
int std_fds[3];
uint32_t uid = -1, gid = -1;
int ngroups = -1;
gid_t groups[64];

val = JS_GetPropertyStr(ctx, args, "length");
if (JS_IsException(val))
Expand Down Expand Up @@ -3339,6 +3342,40 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
if (ret)
goto exception;
}

val = JS_GetPropertyStr(ctx, options, "groups");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
int64_t idx, len;
JSValue prop;
uint32_t id;
ngroups = 0;
if (JS_GetLength(ctx, val, &len)) {
JS_FreeValue(ctx, val);
goto exception;
}
for (idx = 0; idx < len; idx++) {
prop = JS_GetPropertyInt64(ctx, val, idx);
if (JS_IsException(prop))
break;
if (JS_IsUndefined(prop))
continue;
ret = JS_ToUint32(ctx, &id, prop);
JS_FreeValue(ctx, prop);
if (ret)
break;
if (ngroups == countof(groups)) {
JS_ThrowRangeError(ctx, "too many groups");
break;
}
groups[ngroups++] = id;
}
JS_FreeValue(ctx, val);
if (idx < len)
goto exception;
}

}

#if !defined(EMSCRIPTEN) && !defined(__wasi__)
Expand Down Expand Up @@ -3374,6 +3411,10 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
if (chdir(cwd) < 0)
_exit(127);
}
if (ngroups != -1) {
if (setgroups(ngroups, groups) < 0)
_exit(127);
}
if (uid != -1) {
if (setuid(uid) < 0)
_exit(127);
Expand Down