Debian Codegraph @

upstream/debian-trixie/man2/setuid.2

raw
1 .\" Copyright (C), 1994, Graeme W. Wilford (Wilf).
2 .\" and Copyright (C) 2010, 2014, 2015, Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" Fri Jul 29th 12:56:44 BST 1994 Wilf. <[email protected]>
7 .\" Changes inspired by patch from Richard Kettlewell
8 .\" <[email protected]>, aeb 970616.
9 .\" Modified, 27 May 2004, Michael Kerrisk <[email protected]>
10 .\" Added notes on capability requirements
11 .TH setuid 2 2024-05-02 "Linux man-pages 6.9.1"
12 .SH NAME
13 setuid \- set user identity
14 .SH LIBRARY
15 Standard C library
16 .RI ( libc ", " \-lc )
17 .SH SYNOPSIS
18 .nf
19 .B #include <unistd.h>
20 .P
21 .BI "int setuid(uid_t " uid );
22 .fi
23 .SH DESCRIPTION
24 .BR setuid ()
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
28 .B CAP_SETUID
29 capability in its user namespace),
30 the real UID and saved set-user-ID are also set.
31 .P
32 Under Linux,
33 .BR setuid ()
34 is implemented like the POSIX version with the
35 .B _POSIX_SAVED_IDS
36 feature.
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.
40 .P
41 If the user is root or the program is set-user-ID-root, special care must be
42 taken:
43 .BR setuid ()
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
46 .IR uid .
47 After this has occurred, it is impossible for the program to regain root
48 privileges.
49 .P
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
53 .BR setuid ().
54 You can accomplish this with
55 .BR seteuid (2).
56 .SH RETURN VALUE
57 On success, zero is returned.
58 On error, \-1 is returned, and
59 .I errno
60 is set to indicate the error.
61 .P
62 .IR Note :
63 there are cases where
64 .BR setuid ()
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
67 .BR setuid ().
68 .SH ERRORS
69 .TP
70 .B EAGAIN
71 The call would change the caller's real UID (i.e.,
72 .I uid
73 does not match the caller's real UID),
74 but there was a temporary failure allocating the
75 necessary kernel data structures.
76 .TP
77 .B EAGAIN
78 .I uid
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
81 .I uid
82 over the caller's
83 .B RLIMIT_NPROC
84 resource limit.
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
88 .B EAGAIN
89 in
90 .BR execve (2).
91 .TP
92 .B EINVAL
93 The user ID specified in
94 .I uid
95 is not valid in this user namespace.
96 .TP
97 .B EPERM
98 The user is not privileged (Linux: does not have the
99 .B CAP_SETUID
100 capability in its user namespace) and
101 .I uid
102 does not match the real UID or saved set-user-ID of the calling process.
103 .SH VERSIONS
104 .SS C library/kernel differences
105 At the kernel level, user IDs and group IDs are a per-thread attribute.
106 However, POSIX requires that all threads in a process
107 share the same credentials.
108 The NPTL threading implementation handles the POSIX requirements by
109 providing wrapper functions for
110 the various system calls that change process UIDs and GIDs.
111 These wrapper functions (including the one for
112 .BR setuid ())
113 employ a signal-based technique to ensure
114 that when one thread changes credentials,
115 all of the other threads in the process also change their credentials.
116 For details, see
117 .BR nptl (7).
118 .SH STANDARDS
119 POSIX.1-2008.
120 .SH HISTORY
121 POSIX.1-2001, SVr4.
122 .P
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 .P
127 The original Linux
128 .BR setuid ()
129 system call supported only 16-bit user IDs.
130 Subsequently, Linux 2.4 added
131 .BR setuid32 ()
132 supporting 32-bit IDs.
133 The glibc
134 .BR setuid ()
135 wrapper function transparently deals with the variation across kernel versions.
136 .SH NOTES
137 Linux has the concept of the filesystem user ID, normally equal to the
138 effective user ID.
139 The
140 .BR setuid ()
141 call also sets the filesystem user ID of the calling process.
142 See
143 .BR setfsuid (2).
144 .P
145 If
146 .I uid
147 is different from the old effective UID, the process will
148 be forbidden from leaving core dumps.
149 .SH SEE ALSO
150 .BR getuid (2),
151 .BR seteuid (2),
152 .BR setfsuid (2),
153 .BR setreuid (2),
154 .BR capabilities (7),
155 .BR credentials (7),
156 .BR user_namespaces (7)