1SETUID(3P) POSIX Programmer's Manual SETUID(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 setuid — set user ID
13
15 #include <unistd.h>
16
17 int setuid(uid_t uid);
18
20 If the process has appropriate privileges, setuid() shall set the real
21 user ID, effective user ID, and the saved set-user-ID of the calling
22 process to uid.
23
24 If the process does not have appropriate privileges, but uid is equal
25 to the real user ID or the saved set-user-ID, setuid() shall set the
26 effective user ID to uid; the real user ID and saved set-user-ID shall
27 remain unchanged.
28
29 The setuid() function shall not affect the supplementary group list in
30 any way.
31
33 Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
34 returned and errno set to indicate the error.
35
37 The setuid() function shall fail, return -1, and set errno to the cor‐
38 responding value if one or more of the following are true:
39
40 EINVAL The value of the uid argument is invalid and not supported by
41 the implementation.
42
43 EPERM The process does not have appropriate privileges and uid does
44 not match the real user ID or the saved set-user-ID.
45
46 The following sections are informative.
47
49 None.
50
52 None.
53
55 The various behaviors of the setuid() and setgid() functions when
56 called by non-privileged processes reflect the behavior of different
57 historical implementations. For portability, it is recommended that new
58 non-privileged applications use the seteuid() and setegid() functions
59 instead.
60
61 The saved set-user-ID capability allows a program to regain the effec‐
62 tive user ID established at the last exec call. Similarly, the saved
63 set-group-ID capability allows a program to regain the effective group
64 ID established at the last exec call. These capabilities are derived
65 from System V. Without them, a program might have to run as superuser
66 in order to perform the same functions, because superuser can write on
67 the user's files. This is a problem because such a program can write on
68 any user's files, and so must be carefully written to emulate the per‐
69 missions of the calling process properly. In System V, these capabili‐
70 ties have traditionally been implemented only via the setuid() and set‐
71 gid() functions for non-privileged processes. The fact that the behav‐
72 ior of those functions was different for privileged processes made them
73 difficult to use. The POSIX.1‐1990 standard defined the setuid() func‐
74 tion to behave differently for privileged and unprivileged users. When
75 the caller had appropriate privileges, the function set the real user
76 ID, effective user ID, and saved set-user ID of the calling process on
77 implementations that supported it. When the caller did not have appro‐
78 priate privileges, the function set only the effective user ID, subject
79 to permission checks. The former use is generally needed for utilities
80 like login and su, which are not conforming applications and thus out‐
81 side the scope of POSIX.1‐2008. These utilities wish to change the user
82 ID irrevocably to a new value, generally that of an unprivileged user.
83 The latter use is needed for conforming applications that are installed
84 with the set-user-ID bit and need to perform operations using the real
85 user ID.
86
87 POSIX.1‐2008 augments the latter functionality with a mandatory feature
88 named _POSIX_SAVED_IDS. This feature permits a set-user-ID application
89 to switch its effective user ID back and forth between the values of
90 its exec-time real user ID and effective user ID. Unfortunately, the
91 POSIX.1‐1990 standard did not permit a conforming application using
92 this feature to work properly when it happened to be executed with
93 (implementation-defined) appropriate privileges. Furthermore, the
94 application did not even have a means to tell whether it had this priv‐
95 ilege. Since the saved set-user-ID feature is quite desirable for
96 applications, as evidenced by the fact that NIST required it in FIPS
97 151‐2, it has been mandated by POSIX.1‐2008. However, there are imple‐
98 mentors who have been reluctant to support it given the limitation
99 described above.
100
101 The 4.3BSD system handles the problem by supporting separate functions:
102 setuid() (which always sets both the real and effective user IDs, like
103 setuid() in POSIX.1‐2008 for privileged users), and seteuid() (which
104 always sets just the effective user ID, like setuid() in POSIX.1‐2008
105 for non-privileged users). This separation of functionality into dis‐
106 tinct functions seems desirable. 4.3BSD does not support the saved set-
107 user-ID feature. It supports similar functionality of switching the
108 effective user ID back and forth via setreuid(), which permits revers‐
109 ing the real and effective user IDs. This model seems less desirable
110 than the saved set-user-ID because the real user ID changes as a side-
111 effect. The current 4.4BSD includes saved effective IDs and uses them
112 for seteuid() and setegid() as described above. The setreuid() and
113 setregid() functions will be deprecated or removed.
114
115 The solution here is:
116
117 * Require that all implementations support the functionality of the
118 saved set-user-ID, which is set by the exec functions and by privi‐
119 leged calls to setuid().
120
121 * Add the seteuid() and setegid() functions as portable alternatives
122 to setuid() and setgid() for non-privileged and privileged pro‐
123 cesses.
124
125 Historical systems have provided two mechanisms for a set-user-ID
126 process to change its effective user ID to be the same as its real user
127 ID in such a way that it could return to the original effective user
128 ID: the use of the setuid() function in the presence of a saved set-
129 user-ID, or the use of the BSD setreuid() function, which was able to
130 swap the real and effective user IDs. The changes included in
131 POSIX.1‐2008 provide a new mechanism using seteuid() in conjunction
132 with a saved set-user-ID. Thus, all implementations with the new
133 seteuid() mechanism will have a saved set-user-ID for each process, and
134 most of the behavior controlled by _POSIX_SAVED_IDS has been changed to
135 agree with the case where the option was defined. The kill() function
136 is an exception. Implementors of the new seteuid() mechanism will gen‐
137 erally be required to maintain compatibility with the older mechanisms
138 previously supported by their systems. However, compatibility with this
139 use of setreuid() and with the _POSIX_SAVED_IDS behavior of kill() is
140 unfortunately complicated. If an implementation with a saved set-user-
141 ID allows a process to use setreuid() to swap its real and effective
142 user IDs, but were to leave the saved set-user-ID unmodified, the
143 process would then have an effective user ID equal to the original real
144 user ID, and both real and saved set-user-ID would be equal to the
145 original effective user ID. In that state, the real user would be
146 unable to kill the process, even though the effective user ID of the
147 process matches that of the real user, if the kill() behavior of
148 _POSIX_SAVED_IDS was used. This is obviously not acceptable. The alter‐
149 native choice, which is used in at least one implementation, is to
150 change the saved set-user-ID to the effective user ID during most calls
151 to setreuid(). The standard developers considered that alternative to
152 be less correct than the retention of the old behavior of kill() in
153 such systems. Current conforming applications shall accommodate either
154 behavior from kill(), and there appears to be no strong reason for
155 kill() to check the saved set-user-ID rather than the effective user
156 ID.
157
159 None.
160
162 exec, getegid(), geteuid(), getgid(), getuid(), setegid(), seteuid(),
163 setgid(), setregid(), setreuid()
164
165 The Base Definitions volume of POSIX.1‐2017, <sys_types.h>, <unistd.h>
166
168 Portions of this text are reprinted and reproduced in electronic form
169 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
170 table Operating System Interface (POSIX), The Open Group Base Specifi‐
171 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
172 Electrical and Electronics Engineers, Inc and The Open Group. In the
173 event of any discrepancy between this version and the original IEEE and
174 The Open Group Standard, the original IEEE and The Open Group Standard
175 is the referee document. The original Standard can be obtained online
176 at http://www.opengroup.org/unix/online.html .
177
178 Any typographical or formatting errors that appear in this page are
179 most likely to have been introduced during the conversion of the source
180 files to man page format. To report such errors, see https://www.ker‐
181 nel.org/doc/man-pages/reporting_bugs.html .
182
183
184
185IEEE/The Open Group 2017 SETUID(3P)