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