1SETUID(2) Linux Programmer's Manual SETUID(2)
2
3
4
6 setuid - set user identity
7
9 #include <unistd.h>
10
11 int setuid(uid_t uid);
12
14 setuid() sets the effective user ID of the calling process. If the
15 calling process is privileged (more precisely: if the process has the
16 CAP_SETUID capability in its user namespace), the real UID and saved
17 set-user-ID are also set.
18
19 Under Linux, setuid() is implemented like the POSIX version with the
20 _POSIX_SAVED_IDS feature. This allows a set-user-ID (other than root)
21 program to drop all of its user privileges, do some un-privileged work,
22 and then reengage the original effective user ID in a secure manner.
23
24 If the user is root or the program is set-user-ID-root, special care
25 must be taken: setuid() checks the effective user ID of the caller and
26 if it is the superuser, all process-related user ID's are set to uid.
27 After this has occurred, it is impossible for the program to regain
28 root privileges.
29
30 Thus, a set-user-ID-root program wishing to temporarily drop root priv‐
31 ileges, assume the identity of an unprivileged user, and then regain
32 root privileges afterward cannot use setuid(). You can accomplish this
33 with seteuid(2).
34
36 On success, zero is returned. On error, -1 is returned, and errno is
37 set to indicate the error.
38
39 Note: there are cases where setuid() can fail even when the caller is
40 UID 0; it is a grave security error to omit checking for a failure re‐
41 turn from setuid().
42
44 EAGAIN The call would change the caller's real UID (i.e., uid does not
45 match the caller's real UID), but there was a temporary failure
46 allocating the necessary kernel data structures.
47
48 EAGAIN uid does not match the real user ID of the caller and this call
49 would bring the number of processes belonging to the real user
50 ID uid over the caller's RLIMIT_NPROC resource limit. Since
51 Linux 3.1, this error case no longer occurs (but robust applica‐
52 tions should check for this error); see the description of EA‐
53 GAIN in execve(2).
54
55 EINVAL The user ID specified in uid is not valid in this user name‐
56 space.
57
58 EPERM The user is not privileged (Linux: does not have the CAP_SETUID
59 capability in its user namespace) and uid does not match the
60 real UID or saved set-user-ID of the calling process.
61
63 POSIX.1-2001, POSIX.1-2008, SVr4. Not quite compatible with the 4.4BSD
64 call, which sets all of the real, saved, and effective user IDs.
65
67 Linux has the concept of the filesystem user ID, normally equal to the
68 effective user ID. The setuid() call also sets the filesystem user ID
69 of the calling process. See setfsuid(2).
70
71 If uid is different from the old effective UID, the process will be
72 forbidden from leaving core dumps.
73
74 The original Linux setuid() system call supported only 16-bit user IDs.
75 Subsequently, Linux 2.4 added setuid32() supporting 32-bit IDs. The
76 glibc setuid() wrapper function transparently deals with the variation
77 across kernel versions.
78
79 C library/kernel differences
80 At the kernel level, user IDs and group IDs are a per-thread attribute.
81 However, POSIX requires that all threads in a process share the same
82 credentials. The NPTL threading implementation handles the POSIX re‐
83 quirements by providing wrapper functions for the various system calls
84 that change process UIDs and GIDs. These wrapper functions (including
85 the one for setuid()) employ a signal-based technique to ensure that
86 when one thread changes credentials, all of the other threads in the
87 process also change their credentials. For details, see nptl(7).
88
90 getuid(2), seteuid(2), setfsuid(2), setreuid(2), capabilities(7), cre‐
91 dentials(7), user_namespaces(7)
92
94 This page is part of release 5.13 of the Linux man-pages project. A
95 description of the project, information about reporting bugs, and the
96 latest version of this page, can be found at
97 https://www.kernel.org/doc/man-pages/.
98
99
100
101Linux 2021-03-22 SETUID(2)