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