xref: /freebsd/lib/libsys/setuid.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd December 15, 2015
29*8269e767SBrooks Davis.Dt SETUID 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm setuid ,
33*8269e767SBrooks Davis.Nm seteuid ,
34*8269e767SBrooks Davis.Nm setgid ,
35*8269e767SBrooks Davis.Nm setegid
36*8269e767SBrooks Davis.Nd set user and group ID
37*8269e767SBrooks Davis.Sh LIBRARY
38*8269e767SBrooks Davis.Lb libc
39*8269e767SBrooks Davis.Sh SYNOPSIS
40*8269e767SBrooks Davis.In unistd.h
41*8269e767SBrooks Davis.Ft int
42*8269e767SBrooks Davis.Fn setuid "uid_t uid"
43*8269e767SBrooks Davis.Ft int
44*8269e767SBrooks Davis.Fn seteuid "uid_t euid"
45*8269e767SBrooks Davis.Ft int
46*8269e767SBrooks Davis.Fn setgid "gid_t gid"
47*8269e767SBrooks Davis.Ft int
48*8269e767SBrooks Davis.Fn setegid "gid_t egid"
49*8269e767SBrooks Davis.Sh DESCRIPTION
50*8269e767SBrooks DavisThe
51*8269e767SBrooks Davis.Fn setuid
52*8269e767SBrooks Davissystem call
53*8269e767SBrooks Davissets the real and effective
54*8269e767SBrooks Davisuser IDs and the saved set-user-ID of the current process
55*8269e767SBrooks Davisto the specified value.
56*8269e767SBrooks Davis.\" Comment out next block for !_POSIX_SAVED_IDS
57*8269e767SBrooks Davis.\" The real user ID and the saved set-user-ID are changed only if the
58*8269e767SBrooks Davis.\" effective user ID is that of the super user.
59*8269e767SBrooks Davis.\" I.e.
60*8269e767SBrooks Davis.\" .Fn setuid
61*8269e767SBrooks Davis.\" system call is equal to
62*8269e767SBrooks Davis.\" .Fn seteuid
63*8269e767SBrooks Davis.\" system call if the effective user ID is not that of the super user.
64*8269e767SBrooks Davis.\" End of block
65*8269e767SBrooks DavisThe
66*8269e767SBrooks Davis.Fn setuid
67*8269e767SBrooks Davissystem call is permitted if the specified ID is equal to the real user ID
68*8269e767SBrooks Davis.\" Comment out next line for !_POSIX_SAVED_IDS
69*8269e767SBrooks Davis.\" or the saved set-user-ID
70*8269e767SBrooks Davis.\" Next line is for Appendix B.4.2.2 case.
71*8269e767SBrooks Davisor the effective user ID
72*8269e767SBrooks Davisof the process, or if the effective user ID is that of the super user.
73*8269e767SBrooks Davis.Pp
74*8269e767SBrooks DavisThe
75*8269e767SBrooks Davis.Fn setgid
76*8269e767SBrooks Davissystem call
77*8269e767SBrooks Davissets the real and effective
78*8269e767SBrooks Davisgroup IDs and the saved set-group-ID of the current process
79*8269e767SBrooks Davisto the specified value.
80*8269e767SBrooks Davis.\" Comment out next block for !_POSIX_SAVED_IDS
81*8269e767SBrooks Davis.\" The real group ID and the saved set-group-ID are changed only if the
82*8269e767SBrooks Davis.\" effective user ID is that of the super user.
83*8269e767SBrooks Davis.\" I.e.
84*8269e767SBrooks Davis.\" .Fn setgid
85*8269e767SBrooks Davis.\" system call is equal to
86*8269e767SBrooks Davis.\" .Fn setegid
87*8269e767SBrooks Davis.\" system call if the effective user ID is not that of the super user.
88*8269e767SBrooks Davis.\" End of block
89*8269e767SBrooks DavisThe
90*8269e767SBrooks Davis.Fn setgid
91*8269e767SBrooks Davissystem call is permitted if the specified ID is equal to the real group ID
92*8269e767SBrooks Davis.\" Comment out next line for !_POSIX_SAVED_IDS
93*8269e767SBrooks Davis.\" or the saved set-group-ID
94*8269e767SBrooks Davis.\" Next line is for Appendix B.4.2.2 case.
95*8269e767SBrooks Davisor the effective group ID
96*8269e767SBrooks Davisof the process, or if the effective user ID is that of the super user.
97*8269e767SBrooks Davis.Pp
98*8269e767SBrooks DavisThe
99*8269e767SBrooks Davis.Fn seteuid
100*8269e767SBrooks Davissystem call
101*8269e767SBrooks Davis.Pq Fn setegid
102*8269e767SBrooks Davissets the effective user ID (group ID) of the
103*8269e767SBrooks Daviscurrent process.
104*8269e767SBrooks DavisThe effective user ID may be set to the value
105*8269e767SBrooks Davisof the real user ID or the saved set-user-ID (see
106*8269e767SBrooks Davis.Xr intro 2
107*8269e767SBrooks Davisand
108*8269e767SBrooks Davis.Xr execve 2 ) ;
109*8269e767SBrooks Davisin this way, the effective user ID of a set-user-ID executable
110*8269e767SBrooks Davismay be toggled by switching to the real user ID, then re-enabled
111*8269e767SBrooks Davisby reverting to the set-user-ID value.
112*8269e767SBrooks DavisSimilarly, the effective group ID may be set to the value
113*8269e767SBrooks Davisof the real group ID or the saved set-group-ID.
114*8269e767SBrooks Davis.Sh RETURN VALUES
115*8269e767SBrooks Davis.Rv -std
116*8269e767SBrooks Davis.Sh ERRORS
117*8269e767SBrooks DavisThe system calls will fail if:
118*8269e767SBrooks Davis.Bl -tag -width Er
119*8269e767SBrooks Davis.It Bq Er EPERM
120*8269e767SBrooks DavisThe user is not the super user and the ID
121*8269e767SBrooks Davisspecified is not the real, effective ID, or saved ID.
122*8269e767SBrooks Davis.El
123*8269e767SBrooks Davis.Sh SEE ALSO
124*8269e767SBrooks Davis.Xr getgid 2 ,
125*8269e767SBrooks Davis.Xr getuid 2 ,
126*8269e767SBrooks Davis.Xr issetugid 2 ,
127*8269e767SBrooks Davis.Xr setregid 2 ,
128*8269e767SBrooks Davis.Xr setreuid 2
129*8269e767SBrooks Davis.Sh STANDARDS
130*8269e767SBrooks DavisThe
131*8269e767SBrooks Davis.Fn setuid
132*8269e767SBrooks Davisand
133*8269e767SBrooks Davis.Fn setgid
134*8269e767SBrooks Davissystem calls are compliant with the
135*8269e767SBrooks Davis.St -p1003.1-90
136*8269e767SBrooks Davisspecification with
137*8269e767SBrooks Davis.Li _POSIX_SAVED_IDS
138*8269e767SBrooks Davis.\" Uncomment next line for !_POSIX_SAVED_IDS
139*8269e767SBrooks Davisnot
140*8269e767SBrooks Davisdefined with the permitted extensions from Appendix B.4.2.2.
141*8269e767SBrooks DavisThe
142*8269e767SBrooks Davis.Fn seteuid
143*8269e767SBrooks Davisand
144*8269e767SBrooks Davis.Fn setegid
145*8269e767SBrooks Davissystem calls are extensions based on the
146*8269e767SBrooks Davis.Tn POSIX
147*8269e767SBrooks Davisconcept of
148*8269e767SBrooks Davis.Li _POSIX_SAVED_IDS ,
149*8269e767SBrooks Davisand have been proposed for a future revision of the standard.
150*8269e767SBrooks Davis.Sh HISTORY
151*8269e767SBrooks DavisThe
152*8269e767SBrooks Davis.Fn setuid
153*8269e767SBrooks Davisfunction appeared in
154*8269e767SBrooks Davis.At v1 .
155*8269e767SBrooks DavisThe
156*8269e767SBrooks Davis.Fn setgid
157*8269e767SBrooks Davisfunction appeared in
158*8269e767SBrooks Davis.At v4 .
159*8269e767SBrooks Davis.Sh SECURITY CONSIDERATIONS
160*8269e767SBrooks DavisRead and write permissions to files are determined upon a call to
161*8269e767SBrooks Davis.Xr open 2 .
162*8269e767SBrooks DavisOnce a file descriptor is open, dropping privilege does not affect
163*8269e767SBrooks Davisthe process's read/write permissions, even if the user ID specified
164*8269e767SBrooks Davishas no read or write permissions to the file.
165*8269e767SBrooks DavisThese files normally remain open in any new process executed,
166*8269e767SBrooks Davisresulting in a user being able to read or modify
167*8269e767SBrooks Davispotentially sensitive data.
168*8269e767SBrooks Davis.Pp
169*8269e767SBrooks DavisTo prevent these files from remaining open after an
170*8269e767SBrooks Davis.Xr exec 3
171*8269e767SBrooks Daviscall, be sure to set the close-on-exec flag:
172*8269e767SBrooks Davis.Bd -literal
173*8269e767SBrooks Davisvoid
174*8269e767SBrooks Davispseudocode(void)
175*8269e767SBrooks Davis{
176*8269e767SBrooks Davis	int fd;
177*8269e767SBrooks Davis	/* ... */
178*8269e767SBrooks Davis
179*8269e767SBrooks Davis	fd = open("/path/to/sensitive/data", O_RDWR | O_CLOEXEC);
180*8269e767SBrooks Davis	if (fd == -1)
181*8269e767SBrooks Davis		err(1, "open");
182*8269e767SBrooks Davis
183*8269e767SBrooks Davis	/* ... */
184*8269e767SBrooks Davis	execve(path, argv, environ);
185*8269e767SBrooks Davis}
186*8269e767SBrooks Davis.Ed
187