1.\" $NetBSD: setuid.7,v 1.7 2014/03/18 18:20:40 riastradh Exp $ 2.\" 3.\" Copyright (c) 2003 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Henry Spencer <henry@spsystems.net>. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 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. 17.\" 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. 29.\" 30.Dd February 26, 2009 31.Dt SETUID 7 32.Os 33.Sh NAME 34.Nm setuid 35.Nd checklist for security of setuid programs 36.Sh DESCRIPTION 37.Em Please note : 38This manual page was written long ago, and is in need of updating to 39match today's systems. 40We think it is valuable enough to include, even though parts of it 41are outdated. 42A carefully-researched updated version 43would be very useful, if anyone is feeling enthusiastic... 44.Pp 45Writing a secure setuid (or setgid) program is tricky. 46There are a number of possible ways of subverting such a program. 47The most conspicuous security holes occur when a setuid program is 48not sufficiently careful to avoid giving away access to resources 49it legitimately has the use of. 50Most of the other attacks are basically a matter of altering the program's 51environment in unexpected ways and hoping it will fail in some 52security-breaching manner. 53There are generally three categories of environment manipulation: 54supplying a legal but unexpected environment that may cause the 55program to directly do something insecure, 56arranging for error conditions that the program may not handle correctly, 57and the specialized subcategory of giving the program inadequate 58resources in hopes that it won't respond properly. 59.Pp 60The following are general considerations of security when writing 61a setuid program. 62.Bl -bullet 63.It 64The program should run with the weakest userid possible, preferably 65one used only by itself. 66A security hole in a setuid program running with a highly-privileged 67userid can compromise an entire system. 68Security-critical programs like 69.Xr passwd 1 70should always have private userids, to minimize possible damage 71from penetrations elsewhere. 72.It 73The result of 74.Xr getlogin 2 75or 76.Xr ttyname 3 77may be wrong if the descriptors have been meddled with. 78There is 79.Em no 80foolproof way to determine the controlling terminal 81or the login name (as opposed to uid) on V7. 82.It 83On some systems, the setuid bit may not be honored if 84the program is run by root, 85so the program may find itself running as root. 86.It 87Programs that attempt to use 88.Xr creat 3 89for locking can foul up when run by root; 90use of 91.Xr link 2 92is preferred when implementing locking. 93Using 94.Xr chmod 2 95for locking is an obvious disaster. 96.It 97Breaking an existing lock is very dangerous; the breakdown of a locking 98protocol may be symptomatic of far worse problems. 99Doing so on the basis of the lock being 100.Sq old 101is sometimes necessary, 102but programs can run for surprising lengths of time on heavily-loaded 103systems. 104.It 105Care must be taken that user requests for I/O are checked for 106permissions using the user's permissions, not the program's. 107Use of 108.Xr access 2 109is recommended. 110.It 111Programs executed at user request (e.g. shell escapes) must 112not receive the setuid program's permissions; 113use of daughter processes and 114.Dq setuid(getuid()) 115plus 116.Dq setgid(getgid()) 117after 118.Xr fork 2 119but before 120.Xr exec 3 121is vital. 122.It 123Similarly, programs executed at user request must not receive other 124sensitive resources, notably file descriptors. 125Use of 126.Xr fcntl 2 127.Dv F_CLOSEM , 128.Dv FILENO_STDERR + 1 129(close all fd's greater than stderr) 130and/or 131.Xr fcntl 2 132.Dv F_SETFD , 133.Dv FD_CLOEXEC 134(close-on-exec) arrangements 135on systems which have them 136is recommended. 137.Pp 138Other resources should also be examined for sanity and possibly set to 139desired settings, such as the current working directory, signal disposition, 140resource limits, environment, umask, group membership, chroot. 141.Pp 142Programs activated by one user but handling traffic on behalf of 143others (e.g. daemons) should avoid doing 144.Dq setuid(getuid()) 145or 146.Dq setgid(getgid()) , 147since the original invoker's identity is almost certainly inappropriate. 148On systems which permit it, use of 149.Dq setuid(geteuid()) 150and 151.Dq setgid(getegid()) 152is recommended when performing work on behalf of the system as 153opposed to a specific user. 154.It 155There are inherent permission problems when a setuid program executes 156another setuid program, 157since the permissions are not additive. 158Care should be taken that created files are not owned by the wrong person. 159Use of 160.Dq setuid(geteuid()) 161and its gid counterpart can help, if the system allows them. 162.It 163Care should be taken that newly-created files do not have the wrong 164permission or ownership even momentarily. 165Permissions should be arranged by using 166.Xr umask 2 167in advance, rather than by creating the file wide-open and then using 168.Xr chmod 2 . 169Ownership can get sticky due to the limitations of the setuid concept, 170although using a daughter process connected by a pipe can help. 171.It 172Setuid programs should be especially careful about error checking, 173and the normal response to a strange situation should be termination, 174rather than an attempt to carry on. 175.El 176.Pp 177The following are ways in which the program may be induced to carelessly 178give away its special privileges. 179.Bl -bullet 180.It 181The directory the program is started in, or directories it may 182plausibly 183.Xr chdir 2 184to, may contain programs with the same names as system programs, 185placed there in hopes that the program will activate a shell with 186a permissive 187.Ev PATH 188setting. 189.Ev PATH 190should 191.Em always 192be standardized before invoking a shell 193(either directly or via 194.Xr popen 3 195or 196.Xr execvp 3 197or 198.Xr execlp 3 ) . 199.It 200Similarly, a bizarre 201.Ev IFS 202setting may alter the interpretation of a shell command in really 203strange ways, possibly causing a user-supplied program to be invoked. 204.Ev IFS 205too should always be standardized before invoking a shell. 206.It 207Environment variables in general cannot be trusted. 208Their contents should never be taken for granted. 209.It 210Setuid shell files (on systems which implement such) simply cannot 211cope adequately with some of these problems. 212They also have some nasty problems like trying to run a 213.Pa \&.profile 214when run under a suitable name. 215They are terminally insecure, and must be avoided. 216.It 217Relying on the contents of files placed in publically-writable 218directories, such as 219.Pa /tmp , 220is a nearly-incurable security problem. 221Setuid programs should avoid using 222.Pa /tmp 223entirely, if humanly possible. 224The sticky-directories modification (sticky bit on for a directory means 225only owner of a file can remove it) helps, 226but is not a complete solution. 227.It 228A related problem is that 229spool directories, holding information that the program will trust 230later, must never be publically writable even if the files in the 231directory are protected. 232Among other sinister manipulations that can be performed, note that 233on many Unixes, a core dump of a setuid program is owned 234by the program's owner and not by the user running it. 235.El 236.Pp 237The following are unusual but possible error conditions that the 238program should cope with properly (resource-exhaustion questions 239are considered separately, see below). 240.Bl -bullet 241.It 242The value of 243.Ar argc 244might be 0. 245.It 246The setting of the 247.Xr umask 2 248might not be sensible. 249In any case, it should be standardized when creating files 250not intended to be owned by the user. 251.It 252One or more of the standard descriptors might be closed, so that 253an opened file might get (say) descriptor 1, causing chaos if the 254program tries to do a 255.Xr printf 3 . 256.It 257The current directory (or any of its parents) 258may be unreadable and unsearchable. 259On many systems 260.Xr pwd 1 261does not run setuid-root, 262so it can fail under such conditions. 263.It 264Descriptors shared by other processes (i.e., any that are open 265on startup) may be manipulated in strange ways by said processes. 266.It 267The standard descriptors may refer to a terminal which has a bizarre 268mode setting, or which cannot be opened again, 269or which gives end-of-file on any read attempt, or which cannot 270be read or written successfully. 271.It 272The process may be hit by interrupt, quit, hangup, or broken-pipe signals, 273singly or in fast succession. 274The user may deliberately exploit the race conditions inherent 275in catching signals; 276ignoring signals is safe, but catching them is not. 277.It 278Although non-keyboard signals cannot be sent by ordinary users in V7, 279they may perhaps be sent by the system authorities (e.g. to 280indicate that the system is about to shut down), 281so the possibility cannot be ignored. 282.It 283On some systems there may be an 284.Xr alarm 3 285signal pending on startup. 286.It 287The program may have children it did not create. 288This is normal when the process is part of a pipeline. 289.It 290In some non-V7 systems, users can change the ownerships of their files. 291Setuid programs should avoid trusting the owner identification of a file. 292.It 293User-supplied arguments and input data 294.Em must 295be checked meticulously. 296Overly-long input stored in an array without proper bound checking 297can easily breach security. 298When software depends on a file being in a specific format, user-supplied 299data should never be inserted into the file without being checked first. 300Meticulous checking includes allowing for the possibility of non-ASCII 301characters. 302.It 303Temporary files left in public directories like 304.Pa /tmp 305might vanish at inconvenient times. 306.El 307.Pp 308The following are resource-exhaustion possibilities that the 309program should respond properly to. 310.Bl -bullet 311.It 312The user might have used up all of his allowed processes, so 313any attempt to create a new one (via 314.Xr fork 2 315or 316.Xr popen 3 ) 317will fail. 318.It 319There might be many files open, exhausting the supply of descriptors. 320Running 321.Xr fcntl 2 322.Dv F_CLOSEM 323on systems which have it, 324is recommended. 325.It 326There might be many arguments. 327.It 328The arguments and the environment together might occupy a great deal 329of space. 330.El 331.Pp 332Systems which impose other resource limitations can open setuid 333programs to similar resource-exhaustion attacks. 334.Pp 335Setuid programs which execute ordinary programs without reducing 336authority pass all the above problems on to such unprepared children. 337Standardizing the execution environment is only a partial solution. 338.Sh SEE ALSO 339.Xr passwd 1 , 340.Xr pwd 1 , 341.Xr access 2 , 342.Xr chdir 2 , 343.Xr chroot 2 , 344.Xr execve 2 , 345.Xr fcntl 2 , 346.Xr fork 2 , 347.Xr getlogin 2 , 348.Xr link 2 , 349.Xr setegid 2 , 350.Xr seteuid 2 , 351.Xr setgid 2 , 352.Xr setgroups 2 , 353.Xr setrlimit 2 , 354.Xr setuid 2 , 355.Xr sigaction 2 , 356.Xr umask 2 , 357.Xr alarm 3 , 358.Xr creat 3 , 359.Xr execvp 3 , 360.Xr popen 3 , 361.Xr printf 3 , 362.Xr ttyname 3 363.Sh HISTORY 364Written by Henry Spencer, and based on additional outside contributions. 365.Sh AUTHORS 366.An Henry Spencer Aq Mt henry@spsystems.net 367.Sh BUGS 368The list really is rather long... 369and probably incomplete. 370