]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setuid.2
_exit.2, access.2, brk.2, chmod.2, clone.2, epoll_wait.2, eventfd.2, fork.2, getgroup...
[thirdparty/man-pages.git] / man2 / setuid.2
CommitLineData
fea681da 1.\" Copyright (C), 1994, Graeme W. Wilford (Wilf).
84cb494f 2.\" and Copyright (C) 2010, 2014, 2015, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da 25.\"
c13182ef 26.\" Fri Jul 29th 12:56:44 BST 1994 Wilf. <G.Wilford@ee.surrey.ac.uk>
fea681da
MK
27.\" Changes inspired by patch from Richard Kettlewell
28.\" <richard@greenend.org.uk>, aeb 970616.
c11b1abf 29.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 30.\" Added notes on capability requirements
1e64c86b 31.TH SETUID 2 2015-03-29 "Linux" "Linux Programmer's Manual"
fea681da
MK
32.SH NAME
33setuid \- 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
e511ffb6 41.BR setuid ()
a1ffe9f5 42sets the effective user ID of the calling process.
599226ab
MK
43If the effective UID of the caller is root
44(more precisely: if the caller has the
45.BR CAP_SETUID
46capability),
d9df8ff8 47the real UID and saved set-user-ID are also set.
fea681da 48.PP
c13182ef 49Under Linux,
e511ffb6 50.BR setuid ()
8c4f34f8
MK
51is implemented like the POSIX version with the
52.B _POSIX_SAVED_IDS
53feature.
880f5b4b 54This allows a set-user-ID (other than root) program to drop all of its user
3b777aff 55privileges, do some un-privileged work, and then reengage the original
fea681da
MK
56effective user ID in a secure manner.
57.PP
880f5b4b 58If the user is root or the program is set-user-ID-root, special care must be
c13182ef
MK
59taken.
60The
e511ffb6 61.BR setuid ()
d3c8b3e9 62function checks the effective user ID of the caller and if it is
28442c8f 63the superuser, all process-related user ID's are set to
c13182ef 64.IR uid .
fea681da
MK
65After this has occurred, it is impossible for the program to regain root
66privileges.
67.PP
880f5b4b 68Thus, a set-user-ID-root program wishing to temporarily drop root
00b08db3 69privileges, assume the identity of an unprivileged user, and then regain
5fab2e7c 70root privileges afterward cannot use
e511ffb6 71.BR setuid ().
821c0356 72You can accomplish this with
0bfa087b 73.BR seteuid (2).
47297adb 74.SH RETURN VALUE
c13182ef
MK
75On success, zero is returned.
76On error, \-1 is returned, and
fea681da
MK
77.I errno
78is set appropriately.
7d8d165a
MK
79
80.IR Note :
81there are cases where
82.BR setuid ()
83can fail even when the caller is UID 0;
29d3bdc4 84it is a grave security error to omit checking for a failure return from
7d8d165a 85.BR setuid ().
fea681da
MK
86.SH ERRORS
87.TP
88.B EAGAIN
25b2ea5f
MK
89The call would change the caller's real UID (i.e.,
90.I uid
91does not match the caller's real UID),
92but there was a temporary failure allocating the
93necessary kernel data structures.
94.TP
95.B EAGAIN
fea681da 96.I uid
7a42bf02
MK
97does not match the real user ID of the caller and this call would
98bring the number of processes belonging to the real user ID
fea681da 99.I uid
7a42bf02 100over the caller's
0daa9e92 101.B RLIMIT_NPROC
2f0af33b 102resource limit.
c4fe0edf
MK
103Since Linux 3.1, this error case no longer occurs
104(but robust applications should check for this error);
105see the description of
106.B EAGAIN
107in
108.BR execve (2).
fea681da 109.TP
0076479c
MK
110.B EINVAL
111The user ID specified in
112.I uid
113is not valid in this user namespace.
114.TP
fea681da
MK
115.B EPERM
116The user is not privileged (Linux: does not have the
117.B CAP_SETUID
118capability) and
119.I uid
d9df8ff8 120does not match the real UID or saved set-user-ID of the calling process.
47297adb 121.SH CONFORMING TO
c13182ef 122SVr4, POSIX.1-2001.
97c1eac8 123Not quite compatible with the 4.4BSD call, which
c13182ef 124sets all of the real, saved, and effective user IDs.
97c1eac8 125.\" SVr4 documents an additional EINVAL error condition.
4fb31341 126.SH NOTES
9ee4a2b6 127Linux has the concept of the filesystem user ID, normally equal to the
c13182ef
MK
128effective user ID.
129The
e511ffb6 130.BR setuid ()
9ee4a2b6 131call also sets the filesystem user ID of the calling process.
fea681da
MK
132See
133.BR setfsuid (2).
134.PP
135If
136.I uid
e6ce2419 137is different from the old effective UID, the process will
fea681da 138be forbidden from leaving core dumps.
dd09a14e
MK
139
140The original Linux
141.BR setuid ()
142system call supported only 16-bit user IDs.
c5662d5d 143Subsequently, Linux 2.4 added
dd09a14e
MK
144.BR setuid32 ()
145supporting 32-bit IDs.
146The glibc
147.BR setuid ()
148wrapper function transparently deals with the variation across kernel versions.
84cb494f 149.\"
0722a578 150.SS C library/kernel differences
84cb494f
MK
151At the kernel level, user IDs and group IDs are a per-thread attribute.
152However, POSIX requires that all threads in a process
153share the same credentials.
154The NPTL threading implementation handles the POSIX requirements by
155providing wrapper functions for
156the various system calls that change process UIDs and GIDs.
157These wrapper functions (including the one for
158.BR setuid ())
159employ a signal-based technique to ensure
160that when one thread changes credentials,
161all of the other threads in the process also change their credentials.
162For details, see
163.BR nptl (7).
47297adb 164.SH SEE ALSO
fea681da
MK
165.BR getuid (2),
166.BR seteuid (2),
167.BR setfsuid (2),
168.BR setreuid (2),
53a1443c 169.BR capabilities (7),
0076479c 170.BR credentials (7),
f58fb24f 171.BR user_namespaces (7)