xref: /freebsd/share/man/man7/sprog.7 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1ef73da63SEric Melville.\"
2ef73da63SEric Melville.\" Copyright (c) 2001 Eric Melville <eric@FreeBSD.org>
3ef73da63SEric Melville.\" All rights reserved.
4ef73da63SEric Melville.\"
5ef73da63SEric Melville.\" Redistribution and use in source and binary forms, with or without
6ef73da63SEric Melville.\" modification, are permitted provided that the following conditions
7ef73da63SEric Melville.\" are met:
8ef73da63SEric Melville.\" 1. Redistributions of source code must retain the above copyright
9ef73da63SEric Melville.\"    notice, this list of conditions and the following disclaimer.
10ef73da63SEric Melville.\" 2. Redistributions in binary form must reproduce the above copyright
11ef73da63SEric Melville.\"    notice, this list of conditions and the following disclaimer in the
12ef73da63SEric Melville.\"    documentation and/or other materials provided with the distribution.
13ef73da63SEric Melville.\"
14ef73da63SEric Melville.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ef73da63SEric Melville.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ef73da63SEric Melville.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ef73da63SEric Melville.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ef73da63SEric Melville.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ef73da63SEric Melville.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ef73da63SEric Melville.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ef73da63SEric Melville.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ef73da63SEric Melville.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ef73da63SEric Melville.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ef73da63SEric Melville.\" SUCH DAMAGE.
25ef73da63SEric Melville.\"
26ef73da63SEric Melville.Dd June 3, 2001
27ef73da63SEric Melville.Dt SPROG 7
28ef73da63SEric Melville.Os
29ef73da63SEric Melville.Sh NAME
30ef73da63SEric Melville.Nm sprog
31ef73da63SEric Melville.Nd secure programming practices
32ef73da63SEric Melville.Sh DESCRIPTION
33ef73da63SEric MelvilleSecurity issues have crept into many systems over the years.
34ef73da63SEric MelvilleThis document is a guide for programming practices that prevent these problems.
35ef73da63SEric Melville.Ss Overview
36ef73da63SEric MelvilleWriting secure applications takes a very scrutinous and pessimistic outlook.
37ef73da63SEric MelvilleApplications should be run with the principle of
38ef73da63SEric Melville.Dq Li least privilege
39ef73da63SEric Melvilleso that no process is ever running with more than the bare minimum access it
40ef73da63SEric Melvilleneeds to accomplish its function.
41ef73da63SEric MelvillePreviously tested code should be reused whenever possible.
42ef73da63SEric MelvilleGenerally, anything beyond the control of a program should never be trusted.
43ef73da63SEric MelvilleThis includes all forms of user input, system resources, interprocess
44ef73da63SEric Melvillecommunication, and the timing of events.
45ef73da63SEric Melville.Ss Buffer Overflows
46ef73da63SEric MelvilleOne of the most common types of security problems is the buffer overflow.
47ef73da63SEric MelvilleIn short, if a program is not careful with the data it receives, it may be
48ef73da63SEric Melvillepossible for this data to be written across memory, overwriting the return
49ef73da63SEric Melvilleaddress for a function call, and the program will be forced to run code that
50ef73da63SEric Melvilledoes unfriendly things.
51ef73da63SEric Melville.Pp
52ef73da63SEric MelvilleA good number of functions in the standard C library make it difficult or
53ef73da63SEric Melvilleeven impossible to prevent buffer overflows when used.
54ef73da63SEric MelvilleThese include
55ef73da63SEric Melville.Xr fscanf 3 ,
56ef73da63SEric Melville.Xr gets 3 ,
57ef73da63SEric Melville.Xr getwd 3 ,
58ef73da63SEric Melville.Xr realpath 3 ,
59ef73da63SEric Melville.Xr scanf 3 ,
60ef73da63SEric Melville.Xr sprintf 3 ,
61ef73da63SEric Melville.Xr strcat 3 ,
62ef73da63SEric Melville.Xr strcpy 3 ,
63ef73da63SEric Melville.Xr vscanf 3 ,
64ef73da63SEric Melvilleand
65ef73da63SEric Melville.Xr vsprintf 3 .
66ef73da63SEric Melville.Pp
67ef73da63SEric MelvilleMany other functions that deal with strings can also open up a potential
68ef73da63SEric Melvillebuffer overflow when not used carefully.
69ef73da63SEric MelvilleFor example,
70ef73da63SEric Melville.Xr strncat 3
71ef73da63SEric Melvilledoes not go out of its way to provide
72ef73da63SEric Melville.Tn NUL
73ef73da63SEric Melvillecharacter termination.
74ef73da63SEric MelvilleOf course, the proper length must always be specified.
75ef73da63SEric MelvilleUsage of
76ef73da63SEric Melville.Xr strlcat 3
77ef73da63SEric Melvilleand
78ef73da63SEric Melville.Xr strlcpy 3
79ef73da63SEric Melvilleensure that strings are null terminated and of the specified length.
80ef73da63SEric Melville.Pp
81ef73da63SEric MelvilleFunctions that receive a string format must also be used carefully.
82ef73da63SEric MelvilleIt is possible for a string to contain additional format specifiers, which
83ef73da63SEric Melvilleopen up another possibility for a buffer overflow.
84ef73da63SEric MelvilleNever pass a string with untrusted data without using
85ef73da63SEric Melville.Ql %s .
86ef73da63SEric MelvilleAlways use the proper secure idiom:
87ef73da63SEric Melville.Pp
88ef73da63SEric Melville.Dl function("%s", string);
89ef73da63SEric Melville.Pp
90ef73da63SEric MelvilleThere are mechanisms that provide a backstop for these problems at the
91ef73da63SEric Melvillelibrary and compiler levels, however, there is no substitute for simply
92ef73da63SEric Melvillewriting good code.
93ef73da63SEric Melville.Ss Set-user-ID Issues
94ef73da63SEric MelvilleIn many cases, it may be necessary for a program to operate with an increased
95ef73da63SEric Melvilleset of permissions.
96ef73da63SEric MelvilleReasons for this include binding to protected sockets, reading and writing
97ef73da63SEric Melvillecertain files and directories, and access to various resources.
98ef73da63SEric MelvilleUsing a setuid program is frequently the solution.
99ef73da63SEric MelvilleHowever, it is important that programs give up these privileges as soon as
100ef73da63SEric Melvillepossible.
101ef73da63SEric MelvilleFor example, if a program is binding to a protected socket, it should give
102ef73da63SEric Melvilleup its privileges as soon as it has finished binding to that socket.
103ef73da63SEric MelvilleThis is accomplished with the
104ef73da63SEric Melville.Xr setuid 2
105ef73da63SEric Melvillefamily of system calls.
106ef73da63SEric Melville.Ss Limited Environments
107ef73da63SEric MelvilleThe traditional method of restricting a process is with the
108ef73da63SEric Melville.Xr chroot 2
109ef73da63SEric Melvillesystem call.
110ef73da63SEric MelvilleThis system call changes the root directory from which all other paths are
111ef73da63SEric Melvillereferenced for a process and any child processes.
112ef73da63SEric MelvilleOf course, the process must have access to this path to begin with.
113ef73da63SEric MelvilleThe new environment does not actually take effect until
114ef73da63SEric Melville.Xr chdir 2
115ef73da63SEric Melvilleis called to place the process into the new environment.
116ef73da63SEric MelvilleUnfortunately, a process can break out of this environment if root access is
117ef73da63SEric Melvilleobtained.
118ef73da63SEric Melville.Pp
119ef73da63SEric MelvilleOften,
120ef73da63SEric Melville.Xr jail 2
121ef73da63SEric Melvillecan be used to create a more complete and enclosed environment than
122ef73da63SEric Melville.Xr chroot 2
123ef73da63SEric Melvillecan provide.
124ef73da63SEric MelvilleA jail limits all processes inside that environment, including processes with
125ef73da63SEric Melvillesuperuser privileges.
126ef73da63SEric Melville.Pp
127ef73da63SEric MelvilleFine grained privileges, as described by
128ef73da63SEric Melville.Tn POSIX Ns .1e
129ef73da63SEric Melvilleextensions, are currently a work in progress, and the focus of the
130ef73da63SEric Melville.Tn TrustedBSD
131ef73da63SEric MelvilleProject.
132ef73da63SEric MelvilleMore information can be found at
133ef73da63SEric Melville.Pa http://www.TrustedBSD.org/ .
134ef73da63SEric Melville.Ss Trust
135ef73da63SEric MelvillePrograms should not make assumptions about the environment in which they are
136ef73da63SEric Melvillerunning.
137ef73da63SEric MelvilleThis includes user input, signals, environment variables, system resources,
138ef73da63SEric Melvilleinterprocess communications, and shared memory, amongst other things that are
139ef73da63SEric Melvillebeyond the control of the program.
140ef73da63SEric MelvilleThey should not assume that all forms of invalid data can be detected either.
141ef73da63SEric MelvilleInstead, they should use positive filtering, and only allow a specific subset
142ef73da63SEric Melvilleof inputs that are known to be safe.
143ef73da63SEric MelvilleThis is the same logic that an administrator should apply to a firewall, that
144ef73da63SEric Melvilleis, deny by default and specify what is to be accepted.
145ef73da63SEric Melville.Ss Race Conditions
146ef73da63SEric MelvilleA race condition is anomalous behavior caused by the relative timing of
147ef73da63SEric Melvilleevents.
148ef73da63SEric MelvillePrograms should not assume that a particular event will occur before another.
149ef73da63SEric MelvilleThe most common causes of race conditions are signals, access checks, and
150ef73da63SEric Melvillefile reads.
151ef73da63SEric MelvilleSignals are asynchronous by nature, so special care must be taken
152ef73da63SEric Melvillewhile dealing with them.
153ef73da63SEric MelvilleAttempting to check access with sequential non-atomic operations is a very
154ef73da63SEric Melvillebad idea, as files can be moved and changed at any given time.
155ef73da63SEric MelvilleInstead of using a sequence of
156ef73da63SEric Melville.Xr access 2
157ef73da63SEric Melvilleand
158ef73da63SEric Melville.Xr open 2 ,
159ef73da63SEric Melvilleuse
160ef73da63SEric Melville.Xr seteuid 2
161ef73da63SEric Melvilleand then call
162ef73da63SEric Melville.Xr open 2
163ef73da63SEric Melvilledirectly.
164ef73da63SEric MelvilleSet
165ef73da63SEric Melville.Xr umask 2
166ef73da63SEric Melvilleproperly beforehand.
167ef73da63SEric Melville.Sh SEE ALSO
168ef73da63SEric Melville.Xr jail 2 ,
169ef73da63SEric Melville.Xr setuid 2 ,
170ef73da63SEric Melville.Xr strlcat 3 ,
171ef73da63SEric Melville.Xr strlcpy 3
172ef73da63SEric Melville.Sh AUTHORS
173ef73da63SEric Melville.An -nosplit
174*a63d6c94SBaptiste Daroussin.An Eric Melville Aq Mt eric@FreeBSD.org
175ef73da63SEric Melvilleoriginally wrote this document based on a chapter of the
176ef73da63SEric Melville.%B "FreeBSD Developer's Handbook"
177ef73da63SEric Melvillewritten by
178*a63d6c94SBaptiste Daroussin.An Murray Stokely Aq Mt murray@FreeBSD.org .
179