1 .\" Copyright (C), 1994, Graeme W. Wilford (Wilf).
2 .\" and Copyright (C) 2010, 2014, 2015, Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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"
33 setuid \- set user identity
36 .B #include <unistd.h>
38 .BI "int setuid(uid_t " uid );
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
46 capability in its user namespace),
47 the real UID and saved set-user-ID are also set.
51 is implemented like the POSIX version with the
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.
58 If the user is root or the program is set-user-ID-root, special care must be
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
64 After this has occurred, it is impossible for the program to regain root
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
71 You can accomplish this with
74 On success, zero is returned.
75 On error, \-1 is returned, and
77 is set to indicate the error.
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
88 The call would change the caller's real UID (i.e.,
90 does not match the caller's real UID),
91 but there was a temporary failure allocating the
92 necessary kernel data structures.
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
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
110 The user ID specified in
112 is not valid in this user namespace.
115 The user is not privileged (Linux: does not have the
117 capability in its user namespace) and
119 does not match the real UID or saved set-user-ID of the calling process.
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.
126 Linux has the concept of the filesystem user ID, normally equal to the
130 call also sets the filesystem user ID of the calling process.
136 is different from the old effective UID, the process will
137 be forbidden from leaving core dumps.
141 system call supported only 16-bit user IDs.
142 Subsequently, Linux 2.4 added
144 supporting 32-bit IDs.
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
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.
168 .BR capabilities (7),
170 .BR user_namespaces (7)