Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / setuid.2
blob3fcc9d105ad745e10442fe44954709fe952a812a
1 .\" Copyright (C), 1994, Graeme W. Wilford (Wilf).
2 .\" and Copyright (C) 2010, 2014, 2015, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Fri Jul 29th 12:56:44 BST 1994  Wilf. <G.Wilford@ee.surrey.ac.uk>
27 .\" Changes inspired by patch from Richard Kettlewell
28 .\"   <richard@greenend.org.uk>, aeb 970616.
29 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\"     Added notes on capability requirements
31 .TH SETUID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 setuid \- set user identity
34 .SH SYNOPSIS
35 .nf
36 .B #include <unistd.h>
37 .PP
38 .BI "int setuid(uid_t " uid );
39 .fi
40 .SH DESCRIPTION
41 .BR setuid ()
42 sets the effective user ID of the calling process.
43 If the calling process is privileged
44 (more precisely: if the process has the
45 .BR CAP_SETUID
46 capability in its user namespace),
47 the real UID and saved set-user-ID are also set.
48 .PP
49 Under Linux,
50 .BR setuid ()
51 is implemented like the POSIX version with the
52 .B _POSIX_SAVED_IDS
53 feature.
54 This allows a set-user-ID (other than root) program to drop all of its user
55 privileges, do some un-privileged work, and then reengage the original
56 effective user ID in a secure manner.
57 .PP
58 If the user is root or the program is set-user-ID-root, special care must be
59 taken:
60 .BR setuid ()
61 checks the effective user ID of the caller and if it is
62 the superuser, all process-related user ID's are set to
63 .IR uid .
64 After this has occurred, it is impossible for the program to regain root
65 privileges.
66 .PP
67 Thus, a set-user-ID-root program wishing to temporarily drop root
68 privileges, assume the identity of an unprivileged user, and then regain
69 root privileges afterward cannot use
70 .BR setuid ().
71 You can accomplish this with
72 .BR seteuid (2).
73 .SH RETURN VALUE
74 On success, zero is returned.
75 On error, \-1 is returned, and
76 .I errno
77 is set to indicate the error.
78 .PP
79 .IR Note :
80 there are cases where
81 .BR setuid ()
82 can fail even when the caller is UID 0;
83 it is a grave security error to omit checking for a failure return from
84 .BR setuid ().
85 .SH ERRORS
86 .TP
87 .B EAGAIN
88 The call would change the caller's real UID (i.e.,
89 .I uid
90 does not match the caller's real UID),
91 but there was a temporary failure allocating the
92 necessary kernel data structures.
93 .TP
94 .B EAGAIN
95 .I uid
96 does not match the real user ID of the caller and this call would
97 bring the number of processes belonging to the real user ID
98 .I uid
99 over the caller's
100 .B RLIMIT_NPROC
101 resource limit.
102 Since Linux 3.1, this error case no longer occurs
103 (but robust applications should check for this error);
104 see the description of
105 .B EAGAIN
107 .BR execve (2).
109 .B EINVAL
110 The user ID specified in
111 .I uid
112 is not valid in this user namespace.
114 .B EPERM
115 The user is not privileged (Linux: does not have the
116 .B CAP_SETUID
117 capability in its user namespace) and
118 .I uid
119 does not match the real UID or saved set-user-ID of the calling process.
120 .SH CONFORMING TO
121 POSIX.1-2001, POSIX.1-2008, SVr4.
122 Not quite compatible with the 4.4BSD call, which
123 sets all of the real, saved, and effective user IDs.
124 .\" SVr4 documents an additional EINVAL error condition.
125 .SH NOTES
126 Linux has the concept of the filesystem user ID, normally equal to the
127 effective user ID.
129 .BR setuid ()
130 call also sets the filesystem user ID of the calling process.
132 .BR setfsuid (2).
135 .I uid
136 is different from the old effective UID, the process will
137 be forbidden from leaving core dumps.
139 The original Linux
140 .BR setuid ()
141 system call supported only 16-bit user IDs.
142 Subsequently, Linux 2.4 added
143 .BR setuid32 ()
144 supporting 32-bit IDs.
145 The glibc
146 .BR setuid ()
147 wrapper function transparently deals with the variation across kernel versions.
149 .SS C library/kernel differences
150 At the kernel level, user IDs and group IDs are a per-thread attribute.
151 However, POSIX requires that all threads in a process
152 share the same credentials.
153 The NPTL threading implementation handles the POSIX requirements by
154 providing wrapper functions for
155 the various system calls that change process UIDs and GIDs.
156 These wrapper functions (including the one for
157 .BR setuid ())
158 employ a signal-based technique to ensure
159 that when one thread changes credentials,
160 all of the other threads in the process also change their credentials.
161 For details, see
162 .BR nptl (7).
163 .SH SEE ALSO
164 .BR getuid (2),
165 .BR seteuid (2),
166 .BR setfsuid (2),
167 .BR setreuid (2),
168 .BR capabilities (7),
169 .BR credentials (7),
170 .BR user_namespaces (7)