xref: /netbsd-src/share/man/man7/setuid.7 (revision 527132cc8c3ee5aa2cf55eb8f430891fac4b6587)
1*527132ccSfcambus.\" $NetBSD: setuid.7,v 1.9 2020/08/29 13:32:27 fcambus Exp $
2fc2b9a57Swiz.\"
3fc2b9a57Swiz.\" Copyright (c) 2003 The NetBSD Foundation, Inc.
4fc2b9a57Swiz.\" All rights reserved.
5fc2b9a57Swiz.\"
6fc2b9a57Swiz.\" This code is derived from software contributed to The NetBSD Foundation
7fc2b9a57Swiz.\" by Henry Spencer <henry@spsystems.net>.
8fc2b9a57Swiz.\"
9fc2b9a57Swiz.\" Redistribution and use in source and binary forms, with or without
10fc2b9a57Swiz.\" modification, are permitted provided that the following conditions
11fc2b9a57Swiz.\" are met:
12fc2b9a57Swiz.\" 1. Redistributions of source code must retain the above copyright
13fc2b9a57Swiz.\"    notice, this list of conditions and the following disclaimer.
14fc2b9a57Swiz.\" 2. Redistributions in binary form must reproduce the above copyright
15fc2b9a57Swiz.\"    notice, this list of conditions and the following disclaimer in the
16fc2b9a57Swiz.\"    documentation and/or other materials provided with the distribution.
17fc2b9a57Swiz.\"
18fc2b9a57Swiz.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19fc2b9a57Swiz.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20fc2b9a57Swiz.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21fc2b9a57Swiz.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22fc2b9a57Swiz.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23fc2b9a57Swiz.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24fc2b9a57Swiz.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25fc2b9a57Swiz.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26fc2b9a57Swiz.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27fc2b9a57Swiz.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28fc2b9a57Swiz.\" POSSIBILITY OF SUCH DAMAGE.
29fc2b9a57Swiz.\"
30fdba8919Schristos.Dd February 26, 2009
31fc2b9a57Swiz.Dt SETUID 7
32997634feSjoerg.Os
33fc2b9a57Swiz.Sh NAME
34fc2b9a57Swiz.Nm setuid
35fc2b9a57Swiz.Nd checklist for security of setuid programs
36fc2b9a57Swiz.Sh DESCRIPTION
37fc2b9a57Swiz.Em Please note :
38fc2b9a57SwizThis manual page was written long ago, and is in need of updating to
39fc2b9a57Swizmatch today's systems.
40fc2b9a57SwizWe think it is valuable enough to include, even though parts of it
41fc2b9a57Swizare outdated.
42fc2b9a57SwizA carefully-researched updated version
43fc2b9a57Swizwould be very useful, if anyone is feeling enthusiastic...
44fc2b9a57Swiz.Pp
45fc2b9a57SwizWriting a secure setuid (or setgid) program is tricky.
46fc2b9a57SwizThere are a number of possible ways of subverting such a program.
47fc2b9a57SwizThe most conspicuous security holes occur when a setuid program is
48fc2b9a57Swiznot sufficiently careful to avoid giving away access to resources
49fc2b9a57Swizit legitimately has the use of.
50fc2b9a57SwizMost of the other attacks are basically a matter of altering the program's
51fc2b9a57Swizenvironment in unexpected ways and hoping it will fail in some
52fc2b9a57Swizsecurity-breaching manner.
53fc2b9a57SwizThere are generally three categories of environment manipulation:
54fc2b9a57Swizsupplying a legal but unexpected environment that may cause the
55fc2b9a57Swizprogram to directly do something insecure,
56fc2b9a57Swizarranging for error conditions that the program may not handle correctly,
57fc2b9a57Swizand the specialized subcategory of giving the program inadequate
58fc2b9a57Swizresources in hopes that it won't respond properly.
59fc2b9a57Swiz.Pp
60fc2b9a57SwizThe following are general considerations of security when writing
61fc2b9a57Swiza setuid program.
62fc2b9a57Swiz.Bl -bullet
63fc2b9a57Swiz.It
64fc2b9a57SwizThe program should run with the weakest userid possible, preferably
65fc2b9a57Swizone used only by itself.
66fc2b9a57SwizA security hole in a setuid program running with a highly-privileged
67fc2b9a57Swizuserid can compromise an entire system.
68fc2b9a57SwizSecurity-critical programs like
69fc2b9a57Swiz.Xr passwd 1
70fc2b9a57Swizshould always have private userids, to minimize possible damage
71fc2b9a57Swizfrom penetrations elsewhere.
72fc2b9a57Swiz.It
73fc2b9a57SwizThe result of
74fc2b9a57Swiz.Xr getlogin 2
75fc2b9a57Swizor
76fc2b9a57Swiz.Xr ttyname 3
77fc2b9a57Swizmay be wrong if the descriptors have been meddled with.
78fc2b9a57SwizThere is
79fc2b9a57Swiz.Em no
80fc2b9a57Swizfoolproof way to determine the controlling terminal
81fc2b9a57Swizor the login name (as opposed to uid) on V7.
82fc2b9a57Swiz.It
83fc2b9a57SwizOn some systems, the setuid bit may not be honored if
84fc2b9a57Swizthe program is run by root,
85fc2b9a57Swizso the program may find itself running as root.
86fc2b9a57Swiz.It
87fc2b9a57SwizPrograms that attempt to use
88fc2b9a57Swiz.Xr creat 3
89fc2b9a57Swizfor locking can foul up when run by root;
90fc2b9a57Swizuse of
91fc2b9a57Swiz.Xr link 2
92fc2b9a57Swizis preferred when implementing locking.
93fc2b9a57SwizUsing
94fc2b9a57Swiz.Xr chmod 2
95fc2b9a57Swizfor locking is an obvious disaster.
96fc2b9a57Swiz.It
97fc2b9a57SwizBreaking an existing lock is very dangerous; the breakdown of a locking
98fc2b9a57Swizprotocol may be symptomatic of far worse problems.
99fc2b9a57SwizDoing so on the basis of the lock being
100fc2b9a57Swiz.Sq old
101fc2b9a57Swizis sometimes necessary,
102fc2b9a57Swizbut programs can run for surprising lengths of time on heavily-loaded
103fc2b9a57Swizsystems.
104fc2b9a57Swiz.It
105fc2b9a57SwizCare must be taken that user requests for I/O are checked for
106fc2b9a57Swizpermissions using the user's permissions, not the program's.
107fc2b9a57SwizUse of
108fc2b9a57Swiz.Xr access 2
109fc2b9a57Swizis recommended.
110fc2b9a57Swiz.It
111fc2b9a57SwizPrograms executed at user request (e.g. shell escapes) must
112fc2b9a57Swiznot receive the setuid program's permissions;
113fc2b9a57Swizuse of daughter processes and
114fc2b9a57Swiz.Dq setuid(getuid())
115fc2b9a57Swizplus
116fc2b9a57Swiz.Dq setgid(getgid())
117fc2b9a57Swizafter
118fc2b9a57Swiz.Xr fork 2
119fc2b9a57Swizbut before
120fc2b9a57Swiz.Xr exec 3
121fc2b9a57Swizis vital.
122fc2b9a57Swiz.It
123fc2b9a57SwizSimilarly, programs executed at user request must not receive other
124fc2b9a57Swizsensitive resources, notably file descriptors.
125fdba8919SchristosUse of
126fdba8919Schristos.Xr fcntl 2
127fdba8919Schristos.Dv F_CLOSEM ,
128fdba8919Schristos.Dv FILENO_STDERR + 1
129fdba8919Schristos(close all fd's greater than stderr)
130fdba8919Schristosand/or
131fdba8919Schristos.Xr fcntl 2
132fdba8919Schristos.Dv F_SETFD ,
133fdba8919Schristos.Dv FD_CLOEXEC
134fdba8919Schristos(close-on-exec) arrangements
135fdba8919Schristoson systems which have them
136fdba8919Schristosis recommended.
137fdba8919Schristos.Pp
138fdba8919SchristosOther resources should also be examined for sanity and possibly set to
139fdba8919Schristosdesired settings, such as the current working directory, signal disposition,
140fdba8919Schristosresource limits, environment, umask, group membership, chroot.
141fc2b9a57Swiz.Pp
142fc2b9a57SwizPrograms activated by one user but handling traffic on behalf of
143fc2b9a57Swizothers (e.g. daemons) should avoid doing
144fc2b9a57Swiz.Dq setuid(getuid())
145fc2b9a57Swizor
146fc2b9a57Swiz.Dq setgid(getgid()) ,
147fc2b9a57Swizsince the original invoker's identity is almost certainly inappropriate.
148fc2b9a57SwizOn systems which permit it, use of
149fc2b9a57Swiz.Dq setuid(geteuid())
150fc2b9a57Swizand
151fc2b9a57Swiz.Dq setgid(getegid())
152fc2b9a57Swizis recommended when performing work on behalf of the system as
153fc2b9a57Swizopposed to a specific user.
154fc2b9a57Swiz.It
155fc2b9a57SwizThere are inherent permission problems when a setuid program executes
156fc2b9a57Swizanother setuid program,
157fc2b9a57Swizsince the permissions are not additive.
158fc2b9a57SwizCare should be taken that created files are not owned by the wrong person.
159fc2b9a57SwizUse of
160fc2b9a57Swiz.Dq setuid(geteuid())
161fc2b9a57Swizand its gid counterpart can help, if the system allows them.
162fc2b9a57Swiz.It
163fc2b9a57SwizCare should be taken that newly-created files do not have the wrong
164fc2b9a57Swizpermission or ownership even momentarily.
165fc2b9a57SwizPermissions should be arranged by using
166fc2b9a57Swiz.Xr umask 2
167fc2b9a57Swizin advance, rather than by creating the file wide-open and then using
168fc2b9a57Swiz.Xr chmod 2 .
169fc2b9a57SwizOwnership can get sticky due to the limitations of the setuid concept,
170fc2b9a57Swizalthough using a daughter process connected by a pipe can help.
171fc2b9a57Swiz.It
172fc2b9a57SwizSetuid programs should be especially careful about error checking,
173fc2b9a57Swizand the normal response to a strange situation should be termination,
174fc2b9a57Swizrather than an attempt to carry on.
175fc2b9a57Swiz.El
176fc2b9a57Swiz.Pp
177fc2b9a57SwizThe following are ways in which the program may be induced to carelessly
178fc2b9a57Swizgive away its special privileges.
179fc2b9a57Swiz.Bl -bullet
180fc2b9a57Swiz.It
181fc2b9a57SwizThe directory the program is started in, or directories it may
182fc2b9a57Swizplausibly
183fc2b9a57Swiz.Xr chdir 2
184fc2b9a57Swizto, may contain programs with the same names as system programs,
185fc2b9a57Swizplaced there in hopes that the program will activate a shell with
186fc2b9a57Swiza permissive
187fc2b9a57Swiz.Ev PATH
188fc2b9a57Swizsetting.
189fc2b9a57Swiz.Ev PATH
190fc2b9a57Swizshould
191fc2b9a57Swiz.Em always
192fc2b9a57Swizbe standardized before invoking a shell
193fc2b9a57Swiz(either directly or via
194fc2b9a57Swiz.Xr popen 3
195fc2b9a57Swizor
196fc2b9a57Swiz.Xr execvp 3
197fc2b9a57Swizor
198fc2b9a57Swiz.Xr execlp 3 ) .
199fc2b9a57Swiz.It
200fc2b9a57SwizSimilarly, a bizarre
201fc2b9a57Swiz.Ev IFS
202fc2b9a57Swizsetting may alter the interpretation of a shell command in really
203fc2b9a57Swizstrange ways, possibly causing a user-supplied program to be invoked.
204fc2b9a57Swiz.Ev IFS
205fc2b9a57Swiztoo should always be standardized before invoking a shell.
206fc2b9a57Swiz.It
207fc2b9a57SwizEnvironment variables in general cannot be trusted.
208fc2b9a57SwizTheir contents should never be taken for granted.
209fc2b9a57Swiz.It
210fc2b9a57SwizSetuid shell files (on systems which implement such) simply cannot
211fc2b9a57Swizcope adequately with some of these problems.
212fc2b9a57SwizThey also have some nasty problems like trying to run a
213fc2b9a57Swiz.Pa \&.profile
214fc2b9a57Swizwhen run under a suitable name.
215fc2b9a57SwizThey are terminally insecure, and must be avoided.
216fc2b9a57Swiz.It
217*527132ccSfcambusRelying on the contents of files placed in publicly-writable
218fc2b9a57Swizdirectories, such as
219fc2b9a57Swiz.Pa /tmp ,
220fc2b9a57Swizis a nearly-incurable security problem.
221fc2b9a57SwizSetuid programs should avoid using
222fc2b9a57Swiz.Pa /tmp
223fc2b9a57Swizentirely, if humanly possible.
224fc2b9a57SwizThe sticky-directories modification (sticky bit on for a directory means
225fc2b9a57Swizonly owner of a file can remove it) helps,
226fc2b9a57Swizbut is not a complete solution.
227fc2b9a57Swiz.It
228fc2b9a57SwizA related problem is that
229fc2b9a57Swizspool directories, holding information that the program will trust
230*527132ccSfcambuslater, must never be publicly writable even if the files in the
231fc2b9a57Swizdirectory are protected.
232fc2b9a57SwizAmong other sinister manipulations that can be performed, note that
233fc2b9a57Swizon many Unixes, a core dump of a setuid program is owned
234fc2b9a57Swizby the program's owner and not by the user running it.
235fc2b9a57Swiz.El
236fc2b9a57Swiz.Pp
237fc2b9a57SwizThe following are unusual but possible error conditions that the
238fc2b9a57Swizprogram should cope with properly (resource-exhaustion questions
239fc2b9a57Swizare considered separately, see below).
240fc2b9a57Swiz.Bl -bullet
241fc2b9a57Swiz.It
242fc2b9a57SwizThe value of
243fc2b9a57Swiz.Ar argc
244fc2b9a57Swizmight be 0.
245fc2b9a57Swiz.It
246fc2b9a57SwizThe setting of the
247fc2b9a57Swiz.Xr umask 2
248fc2b9a57Swizmight not be sensible.
249fc2b9a57SwizIn any case, it should be standardized when creating files
250fc2b9a57Swiznot intended to be owned by the user.
251fc2b9a57Swiz.It
252fc2b9a57SwizOne or more of the standard descriptors might be closed, so that
253fc2b9a57Swizan opened file might get (say) descriptor 1, causing chaos if the
254fc2b9a57Swizprogram tries to do a
255fc2b9a57Swiz.Xr printf 3 .
256fc2b9a57Swiz.It
257fc2b9a57SwizThe current directory (or any of its parents)
258fc2b9a57Swizmay be unreadable and unsearchable.
259fc2b9a57SwizOn many systems
260fc2b9a57Swiz.Xr pwd 1
261fc2b9a57Swizdoes not run setuid-root,
262fc2b9a57Swizso it can fail under such conditions.
263fc2b9a57Swiz.It
264fc2b9a57SwizDescriptors shared by other processes (i.e., any that are open
265fc2b9a57Swizon startup) may be manipulated in strange ways by said processes.
266fc2b9a57Swiz.It
267fc2b9a57SwizThe standard descriptors may refer to a terminal which has a bizarre
268fc2b9a57Swizmode setting, or which cannot be opened again,
269fc2b9a57Swizor which gives end-of-file on any read attempt, or which cannot
270fc2b9a57Swizbe read or written successfully.
271fc2b9a57Swiz.It
272fc2b9a57SwizThe process may be hit by interrupt, quit, hangup, or broken-pipe signals,
273fc2b9a57Swizsingly or in fast succession.
274fc2b9a57SwizThe user may deliberately exploit the race conditions inherent
275fc2b9a57Swizin catching signals;
276fc2b9a57Swizignoring signals is safe, but catching them is not.
277fc2b9a57Swiz.It
278fc2b9a57SwizAlthough non-keyboard signals cannot be sent by ordinary users in V7,
279fc2b9a57Swizthey may perhaps be sent by the system authorities (e.g. to
280fc2b9a57Swizindicate that the system is about to shut down),
281fc2b9a57Swizso the possibility cannot be ignored.
282fc2b9a57Swiz.It
283fc2b9a57SwizOn some systems there may be an
284fc2b9a57Swiz.Xr alarm 3
285fc2b9a57Swizsignal pending on startup.
286fc2b9a57Swiz.It
287fc2b9a57SwizThe program may have children it did not create.
288fc2b9a57SwizThis is normal when the process is part of a pipeline.
289fc2b9a57Swiz.It
290fc2b9a57SwizIn some non-V7 systems, users can change the ownerships of their files.
291fc2b9a57SwizSetuid programs should avoid trusting the owner identification of a file.
292fc2b9a57Swiz.It
293fc2b9a57SwizUser-supplied arguments and input data
294fc2b9a57Swiz.Em must
295fc2b9a57Swizbe checked meticulously.
296fc2b9a57SwizOverly-long input stored in an array without proper bound checking
297fc2b9a57Swizcan easily breach security.
298fc2b9a57SwizWhen software depends on a file being in a specific format, user-supplied
299fc2b9a57Swizdata should never be inserted into the file without being checked first.
300fc2b9a57SwizMeticulous checking includes allowing for the possibility of non-ASCII
301fc2b9a57Swizcharacters.
302fc2b9a57Swiz.It
303fc2b9a57SwizTemporary files left in public directories like
304fc2b9a57Swiz.Pa /tmp
305fc2b9a57Swizmight vanish at inconvenient times.
306fc2b9a57Swiz.El
307fc2b9a57Swiz.Pp
308fc2b9a57SwizThe following are resource-exhaustion possibilities that the
309fc2b9a57Swizprogram should respond properly to.
310fc2b9a57Swiz.Bl -bullet
311fc2b9a57Swiz.It
312288b76eaSniaThe user might have used up all of their allowed processes, so
313fc2b9a57Swizany attempt to create a new one (via
314fc2b9a57Swiz.Xr fork 2
315fc2b9a57Swizor
316fc2b9a57Swiz.Xr popen 3 )
317fc2b9a57Swizwill fail.
318fc2b9a57Swiz.It
319fc2b9a57SwizThere might be many files open, exhausting the supply of descriptors.
320fdba8919SchristosRunning
321fdba8919Schristos.Xr fcntl 2
322fdba8919Schristos.Dv F_CLOSEM
323fdba8919Schristoson systems which have it,
324fdba8919Schristosis recommended.
325fc2b9a57Swiz.It
326fc2b9a57SwizThere might be many arguments.
327fc2b9a57Swiz.It
328fc2b9a57SwizThe arguments and the environment together might occupy a great deal
329fc2b9a57Swizof space.
330fc2b9a57Swiz.El
331fc2b9a57Swiz.Pp
332fc2b9a57SwizSystems which impose other resource limitations can open setuid
333fc2b9a57Swizprograms to similar resource-exhaustion attacks.
334fc2b9a57Swiz.Pp
335fc2b9a57SwizSetuid programs which execute ordinary programs without reducing
336fc2b9a57Swizauthority pass all the above problems on to such unprepared children.
337fc2b9a57SwizStandardizing the execution environment is only a partial solution.
338fdba8919Schristos.Sh SEE ALSO
339fdba8919Schristos.Xr passwd 1 ,
340fdba8919Schristos.Xr pwd 1 ,
341fdba8919Schristos.Xr access 2 ,
342fdba8919Schristos.Xr chdir 2 ,
343fdba8919Schristos.Xr chroot 2 ,
344fdba8919Schristos.Xr execve 2 ,
345fdba8919Schristos.Xr fcntl 2 ,
346fdba8919Schristos.Xr fork 2 ,
347fdba8919Schristos.Xr getlogin 2 ,
348fdba8919Schristos.Xr link 2 ,
349fdba8919Schristos.Xr setegid 2 ,
350fdba8919Schristos.Xr seteuid 2 ,
351fdba8919Schristos.Xr setgid 2 ,
352fdba8919Schristos.Xr setgroups 2 ,
353fdba8919Schristos.Xr setrlimit 2 ,
35412fad9c4Swiz.Xr setuid 2 ,
355fdba8919Schristos.Xr sigaction 2 ,
356fdba8919Schristos.Xr umask 2 ,
357fdba8919Schristos.Xr alarm 3 ,
35812fad9c4Swiz.Xr creat 3 ,
359fdba8919Schristos.Xr execvp 3 ,
360fdba8919Schristos.Xr popen 3 ,
361fdba8919Schristos.Xr printf 3 ,
362fdba8919Schristos.Xr ttyname 3
363fc2b9a57Swiz.Sh HISTORY
364fc2b9a57SwizWritten by Henry Spencer, and based on additional outside contributions.
365fc2b9a57Swiz.Sh AUTHORS
366a5684d07Swiz.An Henry Spencer Aq Mt henry@spsystems.net
367fc2b9a57Swiz.Sh BUGS
368fc2b9a57SwizThe list really is rather long...
369fc2b9a57Swizand probably incomplete.
370