Skip to content

Commit f731e4a

Browse files
authored
Make os.exec support supplementary groups (#1056)
Add a .groups property that is an array of group ids for setgroups. Fixes: #1055
1 parent 3c9afc9 commit f731e4a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

docs/docs/stdlib.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ object containing optional parameters:
253253
process.
254254
- `uid` - Integer. If present, the process uid with `setuid`.
255255
- `gid` - Integer. If present, the process gid with `setgid`.
256+
- `groups` - Array of integer. If present, the supplementary
257+
group IDs with `setgroup`.
256258

257259
### `waitpid(pid, options)`
258260

quickjs-libc.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include <termios.h>
6767
#include <sys/resource.h>
6868
#include <sys/wait.h>
69+
#include <grp.h>
6970
#endif
7071

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

32403243
val = JS_GetPropertyStr(ctx, args, "length");
32413244
if (JS_IsException(val))
@@ -3339,6 +3342,40 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
33393342
if (ret)
33403343
goto exception;
33413344
}
3345+
3346+
val = JS_GetPropertyStr(ctx, options, "groups");
3347+
if (JS_IsException(val))
3348+
goto exception;
3349+
if (!JS_IsUndefined(val)) {
3350+
int64_t idx, len;
3351+
JSValue prop;
3352+
uint32_t id;
3353+
ngroups = 0;
3354+
if (JS_GetLength(ctx, val, &len)) {
3355+
JS_FreeValue(ctx, val);
3356+
goto exception;
3357+
}
3358+
for (idx = 0; idx < len; idx++) {
3359+
prop = JS_GetPropertyInt64(ctx, val, idx);
3360+
if (JS_IsException(prop))
3361+
break;
3362+
if (JS_IsUndefined(prop))
3363+
continue;
3364+
ret = JS_ToUint32(ctx, &id, prop);
3365+
JS_FreeValue(ctx, prop);
3366+
if (ret)
3367+
break;
3368+
if (ngroups == countof(groups)) {
3369+
JS_ThrowRangeError(ctx, "too many groups");
3370+
break;
3371+
}
3372+
groups[ngroups++] = id;
3373+
}
3374+
JS_FreeValue(ctx, val);
3375+
if (idx < len)
3376+
goto exception;
3377+
}
3378+
33423379
}
33433380

33443381
#if !defined(EMSCRIPTEN) && !defined(__wasi__)
@@ -3374,6 +3411,10 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
33743411
if (chdir(cwd) < 0)
33753412
_exit(127);
33763413
}
3414+
if (ngroups != -1) {
3415+
if (setgroups(ngroups, groups) < 0)
3416+
_exit(127);
3417+
}
33773418
if (uid != -1) {
33783419
if (setuid(uid) < 0)
33793420
_exit(127);

0 commit comments

Comments
 (0)