File: setuid.c

package info (click to toggle)
charliecloud 0.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,084 kB
  • sloc: python: 6,021; sh: 4,284; ansic: 3,863; makefile: 598
file content (34 lines) | stat: -rw-r--r-- 868 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Try to change effective UID. */

#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#define NOBODY  65534
#define NOBODY2 65533

int main(int argc, char ** argv)
{
   // target UID is nobody, unless we're already nobody
   uid_t start = geteuid();
   uid_t target = start != NOBODY ? NOBODY : NOBODY2;
   int result;

   fprintf(stderr, "current EUID=%u, attempting EUID=%u\n", start, target);

   result = seteuid(target);

   // setuid(2) fails with EINVAL in user namespaces and EPERM if not root.
   if (result == 0) {
      printf("RISK\tsetuid(2) succeeded for EUID=%u\n", target);
      return 1;
   } else if (errno == EINVAL) {
      printf("SAFE\tsetuid(2) failed as expected with EINVAL\n");
      return 0;
   }

   printf("ERROR\tsetuid(2) failed unexpectedly with errno=%d\n", errno);
   return 1;
}