1SETUID(2)                  Linux Programmer's Manual                 SETUID(2)
2
3
4

NAME

6       setuid - set user identity
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <unistd.h>
11
12       int setuid(uid_t uid);
13

DESCRIPTION

15       setuid()  sets  the  effective  user ID of the calling process.  If the
16       calling process is privileged (more precisely: if the process  has  the
17       CAP_SETUID  capability  in  its user namespace), the real UID and saved
18       set-user-ID are also set.
19
20       Under Linux, setuid() is implemented like the POSIX  version  with  the
21       _POSIX_SAVED_IDS  feature.  This allows a set-user-ID (other than root)
22       program to drop all of its user privileges, do some un-privileged work,
23       and then reengage the original effective user ID in a secure manner.
24
25       If  the  user  is root or the program is set-user-ID-root, special care
26       must be taken: setuid() checks the effective user ID of the caller  and
27       if  it  is the superuser, all process-related user ID's are set to uid.
28       After this has occurred, it is impossible for  the  program  to  regain
29       root privileges.
30
31       Thus, a set-user-ID-root program wishing to temporarily drop root priv‐
32       ileges, assume the identity of an unprivileged user,  and  then  regain
33       root privileges afterward cannot use setuid().  You can accomplish this
34       with seteuid(2).
35

RETURN VALUE

37       On success, zero is returned.  On error, -1 is returned, and  errno  is
38       set appropriately.
39
40       Note:  there  are cases where setuid() can fail even when the caller is
41       UID 0; it is a grave security error to  omit  checking  for  a  failure
42       return from setuid().
43

ERRORS

45       EAGAIN The  call would change the caller's real UID (i.e., uid does not
46              match the caller's real UID), but there was a temporary  failure
47              allocating the necessary kernel data structures.
48
49       EAGAIN uid  does not match the real user ID of the caller and this call
50              would bring the number of processes belonging to the  real  user
51              ID  uid  over  the  caller's RLIMIT_NPROC resource limit.  Since
52              Linux 3.1, this error case no longer occurs (but robust applica‐
53              tions  should  check  for  this  error);  see the description of
54              EAGAIN in execve(2).
55
56       EINVAL The user ID specified in uid is not valid in  this  user  names‐
57              pace.
58
59       EPERM  The  user is not privileged (Linux: does not have the CAP_SETUID
60              capability in its user namespace) and uid  does  not  match  the
61              real UID or saved set-user-ID of the calling process.
62

CONFORMING TO

64       POSIX.1-2001, POSIX.1-2008, SVr4.  Not quite compatible with the 4.4BSD
65       call, which sets all of the real, saved, and effective user IDs.
66

NOTES

68       Linux has the concept of the filesystem user ID, normally equal to  the
69       effective  user ID.  The setuid() call also sets the filesystem user ID
70       of the calling process.  See setfsuid(2).
7