|
66 | 66 | #include <termios.h>
|
67 | 67 | #include <sys/resource.h>
|
68 | 68 | #include <sys/wait.h>
|
| 69 | +#include <grp.h> |
69 | 70 | #endif
|
70 | 71 |
|
71 | 72 | #if defined(__APPLE__)
|
@@ -3236,6 +3237,8 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
|
3236 | 3237 | static const char *std_name[3] = { "stdin", "stdout", "stderr" };
|
3237 | 3238 | int std_fds[3];
|
3238 | 3239 | uint32_t uid = -1, gid = -1;
|
| 3240 | + int ngroups = -1; |
| 3241 | + gid_t groups[64]; |
3239 | 3242 |
|
3240 | 3243 | val = JS_GetPropertyStr(ctx, args, "length");
|
3241 | 3244 | if (JS_IsException(val))
|
@@ -3339,6 +3342,40 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
|
3339 | 3342 | if (ret)
|
3340 | 3343 | goto exception;
|
3341 | 3344 | }
|
| 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 | + |
3342 | 3379 | }
|
3343 | 3380 |
|
3344 | 3381 | #if !defined(EMSCRIPTEN) && !defined(__wasi__)
|
@@ -3374,6 +3411,10 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
|
3374 | 3411 | if (chdir(cwd) < 0)
|
3375 | 3412 | _exit(127);
|
3376 | 3413 | }
|
| 3414 | + if (ngroups != -1) { |
| 3415 | + if (setgroups(ngroups, groups) < 0) |
| 3416 | + _exit(127); |
| 3417 | + } |
3377 | 3418 | if (uid != -1) {
|
3378 | 3419 | if (setuid(uid) < 0)
|
3379 | 3420 | _exit(127);
|
|
0 commit comments