1 .\" Copyright (C), 1994, Graeme W. Wilford (Wilf).
2 .\" and Copyright (C) 2010, 2014, 2015, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Fri Jul 29th 12:56:44 BST 1994 Wilf. <G.Wilford@ee.surrey.ac.uk>
7 .\" Changes inspired by patch from Richard Kettlewell
8 .\" <richard@greenend.org.uk>, aeb 970616.
9 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Added notes on capability requirements
11 .TH SETUID 2 2022-09-09 "Linux man-pages (unreleased)"
13 setuid \- set user identity
16 .RI ( libc ", " \-lc )
19 .B #include <unistd.h>
21 .BI "int setuid(uid_t " uid );
25 sets the effective user ID of the calling process.
26 If the calling process is privileged
27 (more precisely: if the process has the
29 capability in its user namespace),
30 the real UID and saved set-user-ID are also set.
34 is implemented like the POSIX version with the
37 This allows a set-user-ID (other than root) program to drop all of its user
38 privileges, do some un-privileged work, and then reengage the original
39 effective user ID in a secure manner.
41 If the user is root or the program is set-user-ID-root, special care must be
44 checks the effective user ID of the caller and if it is
45 the superuser, all process-related user ID's are set to
47 After this has occurred, it is impossible for the program to regain root
50 Thus, a set-user-ID-root program wishing to temporarily drop root
51 privileges, assume the identity of an unprivileged user, and then regain
52 root privileges afterward cannot use
54 You can accomplish this with
57 On success, zero is returned.
58 On error, \-1 is returned, and
60 is set to indicate the error.
65 can fail even when the caller is UID 0;
66 it is a grave security error to omit checking for a failure return from
71 The call would change the caller's real UID (i.e.,
73 does not match the caller's real UID),
74 but there was a temporary failure allocating the
75 necessary kernel data structures.
79 does not match the real user ID of the caller and this call would
80 bring the number of processes belonging to the real user ID
85 Since Linux 3.1, this error case no longer occurs
86 (but robust applications should check for this error);
87 see the description of
93 The user ID specified in
95 is not valid in this user namespace.
98 The user is not privileged (Linux: does not have the
100 capability in its user namespace) and
102 does not match the real UID or saved set-user-ID of the calling process.
104 POSIX.1-2001, POSIX.1-2008, SVr4.
105 Not quite compatible with the 4.4BSD call, which
106 sets all of the real, saved, and effective user IDs.
107 .\" SVr4 documents an additional EINVAL error condition.
109 Linux has the concept of the filesystem user ID, normally equal to the
113 call also sets the filesystem user ID of the calling process.
119 is different from the old effective UID, the process will
120 be forbidden from leaving core dumps.
124 system call supported only 16-bit user IDs.
125 Subsequently, Linux 2.4 added
127 supporting 32-bit IDs.
130 wrapper function transparently deals with the variation across kernel versions.
132 .SS C library/kernel differences
133 At the kernel level, user IDs and group IDs are a per-thread attribute.
134 However, POSIX requires that all threads in a process
135 share the same credentials.
136 The NPTL threading implementation handles the POSIX requirements by
137 providing wrapper functions for
138 the various system calls that change process UIDs and GIDs.
139 These wrapper functions (including the one for
141 employ a signal-based technique to ensure
142 that when one thread changes credentials,
143 all of the other threads in the process also change their credentials.
151 .BR capabilities (7),
153 .BR user_namespaces (7)