]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/setuid.2
setuid.2: wfix
[thirdparty/man-pages.git] / man2 / setuid.2
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 2015-07-23 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 setuid \- set user identity
34 .SH SYNOPSIS
35 .B #include <sys/types.h>
36 .br
37 .B #include <unistd.h>
38 .sp
39 .BI "int setuid(uid_t " uid );
40 .SH DESCRIPTION
41 .BR setuid ()
42 sets the effective user ID of the calling process.
43 If the calling process is prvileged
44 (more precisely: if the process has the
45 .BR CAP_SETUID
46 capability),
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 The
61 .BR setuid ()
62 function checks the effective user ID of the caller and if it is
63 the superuser, all process-related user ID's are set to
64 .IR uid .
65 After this has occurred, it is impossible for the program to regain root
66 privileges.
67 .PP
68 Thus, a set-user-ID-root program wishing to temporarily drop root
69 privileges, assume the identity of an unprivileged user, and then regain
70 root privileges afterward cannot use
71 .BR setuid ().
72 You can accomplish this with
73 .BR seteuid (2).
74 .SH RETURN VALUE
75 On success, zero is returned.
76 On error, \-1 is returned, and
77 .I errno
78 is set appropriately.
79
80 .IR Note :
81 there are cases where
82 .BR setuid ()
83 can fail even when the caller is UID 0;
84 it is a grave security error to omit checking for a failure return from
85 .BR setuid ().
86 .SH ERRORS
87 .TP
88 .B EAGAIN
89 The call would change the caller's real UID (i.e.,
90 .I uid
91 does not match the caller's real UID),
92 but there was a temporary failure allocating the
93 necessary kernel data structures.
94 .TP
95 .B EAGAIN
96 .I uid
97 does not match the real user ID of the caller and this call would
98 bring the number of processes belonging to the real user ID
99 .I uid
100 over the caller's
101 .B RLIMIT_NPROC
102 resource limit.
103 Since Linux 3.1, this error case no longer occurs
104 (but robust applications should check for this error);
105 see the description of
106 .B EAGAIN
107 in
108 .BR execve (2).
109 .TP
110 .B EINVAL
111 The user ID specified in
112 .I uid
113 is not valid in this user namespace.
114 .TP
115 .B EPERM
116 The user is not privileged (Linux: does not have the
117 .B CAP_SETUID
118 capability) and
119 .I uid
120 does not match the real UID or saved set-user-ID of the calling process.
121 .SH CONFORMING TO
122 POSIX.1-2001, POSIX.1-2008, SVr4.
123 Not quite compatible with the 4.4BSD call, which
124 sets all of the real, saved, and effective user IDs.
125 .\" SVr4 documents an additional EINVAL error condition.
126 .SH NOTES
127 Linux has the concept of the filesystem user ID, normally equal to the
128 effective user ID.
129 The
130 .BR setuid ()
131 call also sets the filesystem user ID of the calling process.
132 See
133 .BR setfsuid (2).
134 .PP
135 If
136 .I uid
137 is different from the old effective UID, the process will
138 be forbidden from leaving core dumps.
139
140 The original Linux
141 .BR setuid ()
142 system call supported only 16-bit user IDs.
143 Subsequently, Linux 2.4 added
144 .BR setuid32 ()
145 supporting 32-bit IDs.
146 The glibc
147 .BR setuid ()
148 wrapper function transparently deals with the variation across kernel versions.
149 .\"
150 .SS C library/kernel differences
151 At the kernel level, user IDs and group IDs are a per-thread attribute.
152 However, POSIX requires that all threads in a process
153 share the same credentials.
154 The NPTL threading implementation handles the POSIX requirements by
155 providing wrapper functions for
156 the various system calls that change process UIDs and GIDs.
157 These wrapper functions (including the one for
158 .BR setuid ())
159 employ a signal-based technique to ensure
160 that when one thread changes credentials,
161 all of the other threads in the process also change their credentials.
162 For details, see
163 .BR nptl (7).
164 .SH SEE ALSO
165 .BR getuid (2),
166 .BR seteuid (2),
167 .BR setfsuid (2),
168 .BR setreuid (2),
169 .BR capabilities (7),
170 .BR credentials (7),
171 .BR user_namespaces (7)