Skip to content

Commit

Permalink
Add getgroups and setgroups (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi authored Sep 18, 2022
1 parent 3665232 commit 4c40c50
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 29 deletions.
10 changes: 6 additions & 4 deletions libc/calls/calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ int fork(void);
int fsync(int);
int ftruncate(int, int64_t);
int getdomainname(char *, size_t);
int getegid(void) nosideeffect;
int geteuid(void) nosideeffect;
int getgid(void) nosideeffect;
uint32_t getegid(void) nosideeffect;
uint32_t geteuid(void) nosideeffect;
uint32_t getgid(void) nosideeffect;
int getgroups(int size, uint32_t list[]);
int gethostname(char *, size_t);
int getloadavg(double *, int);
int getpgid(int) libcesque;
Expand All @@ -112,7 +113,7 @@ int getresgid(uint32_t *, uint32_t *, uint32_t *);
int getresuid(uint32_t *, uint32_t *, uint32_t *);
int getsid(int) nosideeffect libcesque;
int gettid(void) libcesque;
int getuid(void) libcesque;
uint32_t getuid(void) libcesque;
int sys_iopl(int);
int ioprio_get(int, int);
int ioprio_set(int, int, int);
Expand Down Expand Up @@ -163,6 +164,7 @@ int seteuid(uint32_t);
int setfsgid(int);
int setfsuid(int);
int setgid(int);
int setgroups(size_t size, const uint32_t list[]);
int setpgid(int, int);
int setpgrp(void);
int setpriority(int, unsigned, int);
Expand Down
8 changes: 4 additions & 4 deletions libc/calls/getegid.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"

/**
* Returns effective group ID of calling process.
* @return group id
*/
int getegid(void) {
int rc;
uint32_t getegid(void) {
uint32_t rc;
if (!IsWindows()) {
rc = sys_getegid();
} else {
rc = getgid();
}
STRACE("%s() → %d% m", "getegid", rc);
STRACE("%s() → %u% m", "getegid", rc);
return rc;
}
8 changes: 4 additions & 4 deletions libc/calls/geteuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"

/**
* Returns effective user ID of calling process.
* @return user id
*/
int geteuid(void) {
int rc;
uint32_t geteuid(void) {
uint32_t rc;
if (!IsWindows()) {
rc = sys_geteuid();
} else {
rc = getuid();
}
STRACE("%s() → %d% m", "geteuid", rc);
STRACE("%s() → %u% m", "geteuid", rc);
return rc;
}
45 changes: 45 additions & 0 deletions libc/calls/getgroups.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/calls/groups.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/errfuns.h"

/**
* Get list of supplementary group IDs
*
* @param size - maximum number of items that can be stored in list
* @param list - buffer to store output gid_t
* @return -1 w/ EFAULT
*/
int getgroups(int size, uint32_t list[]) {
int rc;
if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) {
rc = efault();
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
rc = sys_getgroups(size, list);
} else {
rc = enosys();
}
STRACE("getgroups(%d, %s) → %d% m", size, DescribeGidList(rc, rc, list), rc);
return rc;
}
14 changes: 7 additions & 7 deletions libc/calls/getuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/accounting.h"
#include "libc/runtime/runtime.h"
Expand Down Expand Up @@ -50,15 +50,15 @@ static textwindows dontinline uint32_t GetUserNameHash(void) {
* @asyncsignalsafe
* @vforksafe
*/
int getuid(void) {
int rc;
uint32_t getuid(void) {
uint32_t rc;
if (!IsWindows()) {
rc = sys_getuid();
} else {
rc = GetUserNameHash();
}

STRACE("%s() → %d% m", "getuid", rc);
STRACE("%s() → %u% m", "getuid", rc);
return rc;
}

Expand All @@ -71,13 +71,13 @@ int getuid(void) {
* @asyncsignalsafe
* @vforksafe
*/
int getgid(void) {
int rc;
uint32_t getgid(void) {
uint32_t rc;
if (!IsWindows()) {
rc = sys_getgid();
} else {
rc = GetUserNameHash();
}
STRACE("%s() → %d% m", "getgid", rc);
STRACE("%s() → %u% m", "getgid", rc);
return rc;
}
16 changes: 16 additions & 0 deletions libc/calls/groups.internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_
#define COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_

#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

int sys_getgroups(int size, uint32_t list[]);
int sys_setgroups(size_t size, uint32_t list[]);

const char *DescribeGidList(char[128], int, int, const uint32_t list[]);
#define DescribeGidList(rc, length, gidlist) \
DescribeGidList(alloca(128), rc, length, gidlist)

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_ */
46 changes: 46 additions & 0 deletions libc/calls/setgroups.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/calls/groups.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/errfuns.h"

/**
* Set list of supplementary group IDs
*
* @param size - number of items in list
* @param list - input set of gid_t to set
* @return -1 w/ EFAULT
*/
int setgroups(size_t size, const uint32_t list[]) {
int rc;
if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) {
rc = efault();
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
rc = sys_setgroups(size, list);
} else {
rc = enosys();
}
STRACE("setgroups(%u, %s) → %d% m", size, DescribeGidList(rc, size, list),
rc);
return rc;
}
51 changes: 51 additions & 0 deletions libc/intrin/describegidlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2021 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/groups.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/popcnt.h"
#include "libc/macros.internal.h"
#include "libc/str/str.h"

#define N 128

const char *(DescribeGidList)(char buf[N], int rc, int size,
const uint32_t list[]) {
if ((rc == -1) || (size < 0)) return "n/a";
if (!size) return "{}";
if (!list) return "NULL";
if ((!IsAsan() && kisdangerous(list)) ||
(IsAsan() && !__asan_is_valid(list, size * sizeof(list[0])))) {
ksnprintf(buf, N, "%p", list);
return buf;
}
int i = 0, n = N;
i += ksnprintf(buf + i, MAX(0, n - i), "{");
unsigned c;
for (c = 0; c < size && MAX(0, n - i) > 0; c++) {
i += ksnprintf(buf + i, MAX(0, n - i), "%u, ", list[c]);
}
if (c == size) {
if (buf[i - 1] == ' ') i--;
if (buf[i - 1] == ',') i--;
i += ksnprintf(buf + i, MAX(0, n - i), "}");
}
return buf;
}
1 change: 1 addition & 0 deletions libc/isystem/grp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#ifndef LIBC_ISYSTEM_GRP_H_
#define LIBC_ISYSTEM_GRP_H_
#include "libc/calls/calls.h"
#include "third_party/musl/passwd.h"
#endif
1 change: 0 additions & 1 deletion libc/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ int mprotect(void *, uint64_t, int) privileged;
int msync(void *, size_t, int);
void *sbrk(intptr_t);
int brk(void *);
int getgroups(int, uint32_t[]);
long gethostid(void);
int sethostid(long);
char *getlogin(void);
Expand Down
2 changes: 1 addition & 1 deletion libc/sysv/calls/sys_getgroups.s
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.include "o/libc/sysv/macros.internal.inc"
.scall sys_getgroups,0x04f04f04f204f073,globl
.scall sys_getgroups,0x04f04f04f204f073,globl,hidden
2 changes: 1 addition & 1 deletion libc/sysv/calls/sys_setgroups.s
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.include "o/libc/sysv/macros.internal.inc"
.scall sys_setgroups,0x0500500502050074,globl
.scall sys_setgroups,0x0500500502050074,globl,hidden
4 changes: 2 additions & 2 deletions libc/sysv/syscalls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ scall sys_getpgid 0x0cf0cf0cf2097079 globl hidden
scall sys_setpgid 0x052052052205206d globl hidden
scall sys_geteuid 0x019019019201906b globl hidden
scall sys_getegid 0x02b02b02b202b06c globl hidden
scall sys_getgroups 0x04f04f04f204f073 globl # no wrapper
scall sys_setgroups 0x0500500502050074 globl # no wrapper
scall sys_getgroups 0x04f04f04f204f073 globl hidden
scall sys_setgroups 0x0500500502050074 globl hidden
scall sys_setreuid 0x07e07e07e207e071 globl hidden
scall sys_setregid 0x07f07f07f207f072 globl hidden
scall sys_setuid 0x0170170172017069 globl hidden
Expand Down
47 changes: 47 additions & 0 deletions test/libc/calls/getgroups_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2022 Gavin Arthur Hayes │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/testlib/testlib.h"

TEST(getgroups, test) {
int rc;
uint32_t res[1000];
rc = getgroups(0, NULL);
if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
EXPECT_NE(-1, rc);
EXPECT_NE(-1, getgroups(sizeof(res) / sizeof(res[0]), res));
} else {
EXPECT_EQ(-1, rc);
EXPECT_EQ(ENOSYS, errno);
}
}

TEST(setgroups, test) {
int rc;
uint32_t src[5];
EXPECT_EQ(-1, setgroups(0, NULL));
if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
EXPECT_EQ(EPERM, errno);
EXPECT_EQ(-1, setgroups(sizeof(src) / sizeof(src[0]), src));
} else {
EXPECT_EQ(ENOSYS, errno);
}
}
Loading

0 comments on commit 4c40c50

Please sign in to comment.