Skip to content

Commit 90cd3d5

Browse files
committed
libtcb: use setgroups syscall instead of the libc function
Per POSIX, setgroups function affects all threads, making its users, tcb_drop_priv_r and tcb_gain_priv_r, unsuitable for threaded processes.
1 parent c7a4369 commit 90cd3d5

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2023-01-21 Dmitry V. Levin <ldv at owl.openwall.com>
2+
3+
Use setgroups syscall instead of the libc function.
4+
* libs/libtcb.c (sys_setgroups): New function, a thin wrapper around
5+
setgroups syscall.
6+
(tcb_drop_priv_r, tcb_gain_priv_r): Use it instead of setgroups.
7+
18
2023-01-20 Dmitry V. Levin <ldv at owl.openwall.com>
29

310
Make -DENABLE_SETFSUGID the default and only implementation.

libs/libtcb.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <sys/fsuid.h>
13+
#include <sys/syscall.h>
1314

1415
#include "tcb.h"
1516
#include "attribute.h"
@@ -160,6 +161,15 @@ static int ch_gid(gid_t gid, gid_t *save)
160161
return (gid_t) setfsgid(gid) == gid;
161162
}
162163

164+
static int sys_setgroups(size_t size, const gid_t *list)
165+
{
166+
#ifdef SYS_setgroups32
167+
if (sizeof(*list) == 4)
168+
return syscall(SYS_setgroups32, size, list);
169+
#endif
170+
return syscall(SYS_setgroups, size, list);
171+
}
172+
163173
#define PRIV_MAGIC 0x1004000a
164174
#define PRIV_MAGIC_NONROOT 0xdead000a
165175

@@ -200,7 +210,7 @@ int tcb_drop_priv_r(const char *name, struct tcb_privs *p)
200210

201211
p->number_of_groups = res;
202212

203-
if (setgroups(0, NULL) == -1)
213+
if (sys_setgroups(0, NULL) == -1)
204214
return -1;
205215
if (!ch_gid(shadow_gid, &p->old_gid))
206216
return -1;
@@ -230,7 +240,7 @@ int tcb_gain_priv_r(struct tcb_privs *p)
230240
return -1;
231241
if (!ch_gid(p->old_gid, NULL))
232242
return -1;
233-
if (setgroups(p->number_of_groups, p->grplist) == -1)
243+
if (sys_setgroups(p->number_of_groups, p->grplist) == -1)
234244
return -1;
235245

236246
p->is_dropped = 0;

0 commit comments

Comments
 (0)