1SETUID(2) Linux Programmer's Manual SETUID(2)
2
3
4
6 setuid - set user identity
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 int setuid(uid_t uid);
13
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
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 re‐
42 turn from setuid().
43
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 EA‐
54 GAIN in execve(2).
55
56 EINVAL The user ID specified in uid is not valid in this user name‐
57 space.
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
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
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).
71
72 If uid is different from the old effective UID, the process will be
73 forbidden from leaving core dumps.
74
75 The original Linux setuid() system call supported only 16-bit user IDs.
76 Subsequently, Linux 2.4 added setuid32() supporting 32-bit IDs. The
77 glibc setuid() wrapper function transparently deals with the variation
78 across kernel versions.
79
80 C library/kernel differences
81 At the kernel level, user IDs and group IDs are a per-thread attribute.
82 However, POSIX requires that all threads in a process share the same
83 credentials. The NPTL threading implementation handles the POSIX re‐
84 quirements by providing wrapper functions for the various system calls
85 that change process UIDs and GIDs. These wrapper functions (including
86 the one for setuid()) employ a signal-based technique to ensure that
87 when one thread changes credentials, all of the other threads in the
88 process also change their credentials. For details, see nptl(7).
89
91 getuid(2), seteuid(2), setfsuid(2), setreuid(2), capabilities(7), cre‐
92 dentials(7), user_namespaces(7)
93
95 This page is part of release 5.10 of the Linux man-pages project. A
96 description of the project, information about reporting bugs, and the
97 latest version of this page, can be found at
98 https://www.kernel.org/doc/man-pages/.
99
100
101
102Linux 2019-03-06 SETUID(2)