setgroups.c 933 B

123456789101112131415161718192021222324252627282930313233343536
  1. #define _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include "syscall.h"
  5. #include "libc.h"
  6. struct ctx {
  7. size_t count;
  8. const gid_t *list;
  9. int ret;
  10. };
  11. static void do_setgroups(void *p)
  12. {
  13. struct ctx *c = p;
  14. if (c->ret<0) return;
  15. int ret = __syscall(SYS_setgroups, c->count, c->list);
  16. if (ret && !c->ret) {
  17. /* If one thread fails to set groups after another has already
  18. * succeeded, forcibly killing the process is the only safe
  19. * thing to do. State is inconsistent and dangerous. Use
  20. * SIGKILL because it is uncatchable. */
  21. __block_all_sigs(0);
  22. __syscall(SYS_kill, __syscall(SYS_getpid), SIGKILL);
  23. }
  24. c->ret = ret;
  25. }
  26. int setgroups(size_t count, const gid_t list[])
  27. {
  28. /* ret is initially nonzero so that failure of the first thread does not
  29. * trigger the safety kill above. */
  30. struct ctx c = { .count = count, .list = list, .ret = 1 };
  31. __synccall(do_setgroups, &c);
  32. return __syscall_ret(c.ret);
  33. }