1 .\" $NetBSD: setuid.7,v 1.4 2009/02/26 19:05:58 wiz Exp $
3 .\" Copyright (c) 2003 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Henry Spencer <henry@spsystems.net>.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd checklist for security of setuid programs
38 This manual page was written long ago, and is in need of updating to
39 match today's systems.
40 We think it is valuable enough to include, even though parts of it
42 A carefully-researched updated version
43 would be very useful, if anyone is feeling enthusiastic...
45 Writing a secure setuid (or setgid) program is tricky.
46 There are a number of possible ways of subverting such a program.
47 The most conspicuous security holes occur when a setuid program is
48 not sufficiently careful to avoid giving away access to resources
49 it legitimately has the use of.
50 Most of the other attacks are basically a matter of altering the program's
51 environment in unexpected ways and hoping it will fail in some
52 security-breaching manner.
53 There are generally three categories of environment manipulation:
54 supplying a legal but unexpected environment that may cause the
55 program to directly do something insecure,
56 arranging for error conditions that the program may not handle correctly,
57 and the specialized subcategory of giving the program inadequate
58 resources in hopes that it won't respond properly.
60 The following are general considerations of security when writing
64 The program should run with the weakest userid possible, preferably
65 one used only by itself.
66 A security hole in a setuid program running with a highly-privileged
67 userid can compromise an entire system.
68 Security-critical programs like
70 should always have private userids, to minimize possible damage
71 from penetrations elsewhere.
77 may be wrong if the descriptors have been meddled with.
80 foolproof way to determine the controlling terminal
81 or the login name (as opposed to uid) on V7.
83 On some systems, the setuid bit may not be honored if
84 the program is run by root,
85 so the program may find itself running as root.
87 Programs that attempt to use
89 for locking can foul up when run by root;
92 is preferred when implementing locking.
95 for locking is an obvious disaster.
97 Breaking an existing lock is very dangerous; the breakdown of a locking
98 protocol may be symptomatic of far worse problems.
99 Doing so on the basis of the lock being
101 is sometimes necessary,
102 but programs can run for surprising lengths of time on heavily-loaded
105 Care must be taken that user requests for I/O are checked for
106 permissions using the user's permissions, not the program's.
111 Programs executed at user request (e.g. shell escapes) must
112 not receive the setuid program's permissions;
113 use of daughter processes and
123 Similarly, programs executed at user request must not receive other
124 sensitive resources, notably file descriptors.
128 .Dv FILENO_STDERR + 1
129 (close all fd's greater than stderr)
134 (close-on-exec) arrangements
135 on systems which have them
138 Other resources should also be examined for sanity and possibly set to
139 desired settings, such as the current working directory, signal disposition,
140 resource limits, environment, umask, group membership, chroot.
142 Programs activated by one user but handling traffic on behalf of
143 others (e.g. daemons) should avoid doing
146 .Dq setgid(getgid()) ,
147 since the original invoker's identity is almost certainly inappropriate.
148 On systems which permit it, use of
149 .Dq setuid(geteuid())
151 .Dq setgid(getegid())
152 is recommended when performing work on behalf of the system as
153 opposed to a specific user.
155 There are inherent permission problems when a setuid program executes
156 another setuid program,
157 since the permissions are not additive.
158 Care should be taken that created files are not owned by the wrong person.
160 .Dq setuid(geteuid())
161 and its gid counterpart can help, if the system allows them.
163 Care should be taken that newly-created files do not have the wrong
164 permission or ownership even momentarily.
165 Permissions should be arranged by using
167 in advance, rather than by creating the file wide-open and then using
169 Ownership can get sticky due to the limitations of the setuid concept,
170 although using a daughter process connected by a pipe can help.
172 Setuid programs should be especially careful about error checking,
173 and the normal response to a strange situation should be termination,
174 rather than an attempt to carry on.
177 The following are ways in which the program may be induced to carelessly
178 give away its special privileges.
181 The directory the program is started in, or directories it may
184 to, may contain programs with the same names as system programs,
185 placed there in hopes that the program will activate a shell with
192 be standardized before invoking a shell
193 (either directly or via
202 setting may alter the interpretation of a shell command in really
203 strange ways, possibly causing a user-supplied program to be invoked.
205 too should always be standardized before invoking a shell.
207 Environment variables in general cannot be trusted.
208 Their contents should never be taken for granted.
210 Setuid shell files (on systems which implement such) simply cannot
211 cope adequately with some of these problems.
212 They also have some nasty problems like trying to run a
214 when run under a suitable name.
215 They are terminally insecure, and must be avoided.
217 Relying on the contents of files placed in publically-writable
220 is a nearly-incurable security problem.
221 Setuid programs should avoid using
223 entirely, if humanly possible.
224 The sticky-directories modification (sticky bit on for a directory means
225 only owner of a file can remove it) helps,
226 but is not a complete solution.
228 A related problem is that
229 spool directories, holding information that the program will trust
230 later, must never be publically writable even if the files in the
231 directory are protected.
232 Among other sinister manipulations that can be performed, note that
233 on many Unixes, a core dump of a setuid program is owned
234 by the program's owner and not by the user running it.
237 The following are unusual but possible error conditions that the
238 program should cope with properly (resource-exhaustion questions
239 are considered separately, see below).
248 might not be sensible.
249 In any case, it should be standardized when creating files
250 not intended to be owned by the user.
252 One or more of the standard descriptors might be closed, so that
253 an opened file might get (say) descriptor 1, causing chaos if the
254 program tries to do a
257 The current directory (or any of its parents)
258 may be unreadable and unsearchable.
261 does not run setuid-root,
262 so it can fail under such conditions.
264 Descriptors shared by other processes (i.e., any that are open
265 on startup) may be manipulated in strange ways by said processes.
267 The standard descriptors may refer to a terminal which has a bizarre
268 mode setting, or which cannot be opened again,
269 or which gives end-of-file on any read attempt, or which cannot
270 be read or written successfully.
272 The process may be hit by interrupt, quit, hangup, or broken-pipe signals,
273 singly or in fast succession.
274 The user may deliberately exploit the race conditions inherent
276 ignoring signals is safe, but catching them is not.
278 Although non-keyboard signals cannot be sent by ordinary users in V7,
279 they may perhaps be sent by the system authorities (e.g. to
280 indicate that the system is about to shut down),
281 so the possibility cannot be ignored.
283 On some systems there may be an
285 signal pending on startup.
287 The program may have children it did not create.
288 This is normal when the process is part of a pipeline.
290 In some non-V7 systems, users can change the ownerships of their files.
291 Setuid programs should avoid trusting the owner identification of a file.
293 User-supplied arguments and input data
295 be checked meticulously.
296 Overly-long input stored in an array without proper bound checking
297 can easily breach security.
298 When software depends on a file being in a specific format, user-supplied
299 data should never be inserted into the file without being checked first.
300 Meticulous checking includes allowing for the possibility of non-ASCII
303 Temporary files left in public directories like
305 might vanish at inconvenient times.
308 The following are resource-exhaustion possibilities that the
309 program should respond properly to.
312 The user might have used up all of his allowed processes, so
313 any attempt to create a new one (via
319 There might be many files open, exhausting the supply of descriptors.
323 on systems which have it,
326 There might be many arguments.
328 The arguments and the environment together might occupy a great deal
332 Systems which impose other resource limitations can open setuid
333 programs to similar resource-exhaustion attacks.
335 Setuid programs which execute ordinary programs without reducing
336 authority pass all the above problems on to such unprepared children.
337 Standardizing the execution environment is only a partial solution.
364 Written by Henry Spencer, and based on additional outside contributions.
366 .An Henry Spencer Aq henry@spsystems.net
368 The list really is rather long...
369 and probably incomplete.