xref: /freebsd/share/man/man7/security.7 (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1.\" Copyright (c) 1998, Matthew Dillon.  Terms and conditions are those of
2.\" the BSD Copyright as specified in the file "/usr/src/COPYRIGHT" in
3.\" the source tree.
4.\"
5.\"	$Id: security.7,v 1.3 1998/12/25 23:39:01 obrien Exp $
6.\"
7.Dd December 20, 1998
8.Dt SECURITY 7
9.Os
10.Sh NAME
11.Nm security
12.Nd introduction to security under FreeBSD
13.Sh DESCRIPTION
14.Pp
15Security is a function that begins and ends with the system administrator.
16While all
17.Bx
18systems are inherently multi-user capable, the job of building and
19maintaining security mechanisms to keep those users 'honest' is probably
20one of the single largest undertakings of the sysadmin.  Machines are
21only as secure as you make them, and security concerns are ever competing
22with the human necessity for convenience.   UNIX systems,
23in general, are capable of running a huge number of simultaneous processes
24and many of these processes operate as servers - meaning that external entities
25can connect and talk to them.  As yesterday's mini-computers and mainframes
26become today's desktops, and as computers become networked and internetworked,
27security becomes an ever bigger issue.
28.Pp
29Security concerns can be split up into several categories:
30.Bl -enum -offset indent
31.It
32Denial of service attacks
33.It
34User account compromises
35.It
36Root compromise through accessible servers
37.It
38Root compromise via user accounts
39.El
40.Pp
41A denial of service attack is an action that deprives the machine of needed
42resources.  Typically, D.O.S. attacks are brute-force mechanisms that attempt
43to crash or otherwise make a machine unusable by overwhelming its servers or
44network stack.  Some D.O.S. attacks try to take advantages of bugs in the
45networking stack to crash a machine with a single packet.  The latter can
46only be fixed by applying a bug fix to the kernel.  Attacks on servers can
47often be fixed by properly specifying options to servers to limit the load
48they incur on the system under adverse conditions.  Brute-force network
49attacks are harder to deal with.  A spoofed-packet attack, for example, is
50nearly impossible to stop short of cutting your system off from the internet.
51.Pp
52A user account compromise is even more common then a D.O.S. attack.  Many
53sysadmins still run standard telnetd, rlogind, rshd, and ftpd servers on their
54machines.  These servers, by default, do not operate over encrypted
55connections.  The result is that if you have any moderate-sized user base,
56one or more of your users logging into your system from a remote location
57(which is the most common and convenient way to login to a system) will
58have his or her password sniffed.  The attentive system admin will analyze
59his remote access logs occasionally looking for suspicious source addresses
60even for successful logins.
61.Pp
62One must always assume that once an attacker has access to a user account,
63the attacker can break root.  However, the reality is that in a well secured
64and maintained system, access to a user account does not necessarily give the
65attacker access to root.  The distinction is important because without access
66to root the attacker cannot generally hide his tracks and may, at best, be
67able to remove that user's files and crash the machine, but not touch anyone
68else's files.
69.Pp
70System administrators must keep in mind that there are several ways to break
71root on a machine.  The attacker may know the root password, the attacker
72may find a bug in a root-run server and be able to break root over a network
73connection to that server, or the attacker may know of a bug in an suid-root
74program that allows the attacker to break root once he has broken into a
75user's account.
76.Pp
77Security remedies are always implemented in a multi-layered 'onion peel'
78approach and can be categorized as follows:
79.Bl -enum -offset indent
80.It
81Securing root and staff accounts
82.It
83Securing root - root-run servers and suid/sgid binaries
84.It
85Securing user accounts
86.It
87Securing the password file
88.It
89Securing the kernel core, raw devices, and filesystems
90.It
91Checking file integrity: binaries, configuration files, and so forth
92.It
93Paranoia
94.El
95.Sh SECURING THE ROOT ACCOUNT AND SECURING STAFF ACCOUNTS
96.Pp
97Don't bother securing staff accounts if you haven't secured the root
98account.  Most systems have a password assigned to the root account.  The
99first thing you do is assume that the password is 'always' compromised.
100To secure the root account you make sure that it is not possible to login
101to the root account using the root password from a random user account or
102over the network.  If you haven't already, configure telnetd, rlogind, and
103all other servers that handle login operations to refuse root logins, period,
104whether the right password is given or not.  Allow direct root logins only
105via the system console.  The '/etc/ttys' file comes in handy here and is
106secure by default on most systems, but a good sysadmin always checks to make
107sure.
108.Pp
109Of course, as a sysadmin you have to be able to get to root, so we open up
110a few holes.  But we make sure these holes require additional password
111verification to operate.  One way to make root accessible is to add appropriate
112staff accounts to the wheel group (in /etc/group).  The staff members placed
113in the wheel group are allowed to 'su' to root.  You should never give staff
114members native wheel access via their entry in the password file... put staff
115in a 'staff' group or something and only add those that really need root to
116the wheel group.  Unfortunately the wheel mechanism still allows an intruder to
117break root if the intruder has gotten hold of your password file - he need only
118break the root password and the password of one of the staff accounts that
119happens to be in the wheel group.  So while the wheel mechanism is usable,
120it isn't much safer then not having a wheel group at all.
121.Pp
122An indirect way to secure the root account is to secure your staff accounts
123by using an alternative login access method and *'ing out the crypted password
124for the staff accounts.  This way an intruder may be able to steal the password
125file but will not be able to break into any staff accounts (or, indirectly,
126root, even if root has a crypted password associated with it).  Staff members
127get into their staff accounts through a secure login mechanism such as
128kerberos(1) or ssh(1) (see /usr/ports/security/ssh) using a private/public
129key pair.  When you use something like kerberos you generally must secure
130the machines which run the kerberos servers and your desktop workstation.
131When you use a public/private key pair with ssh, you must generally secure
132the machine you are logging in FROM (typically your workstation), but you can
133also add an additional layer of protection to the key pair by password
134protecting the key pair when you create it with ssh-keygen(1).  Being able
135to *-out the passwords for staff accounts also guarantees that staff members
136can only login through secure access methods that you have setup.  You can
137thus force all staff members to use secure, encrypted connections for
138all their sessions which closes an important hole used by many intruders:  That
139of sniffing the network from an unrelated, less secure machine.
140.Pp
141The more indirect security mechanisms also assume that you are logging in
142from a more restrictive server to a less restrictive server.  For example,
143if your main box is running all sorts of servers, your workstation shouldn't
144 be running any.  In order for your workstation to be reasonably secure
145you should run as few servers as possible, up to and including no servers
146at all, and you should run a password-protected screen blanker.
147 Of course, given physical access to
148a workstation an attacker can break any sort of security you put on it.
149This is definitely a problem that you should consider but you should also
150consider the fact that the vast majority of break-ins occur remotely, over
151a network, from people who do not have physical access to your workstation or
152servers.
153.Pp
154Using something like kerberos also gives you the ability to disable or
155change the password for a staff account in one place and have it immediately
156effect all the machine the staff member may have an account on.  If a staff
157member's account gets compromised, the ability to instantly change his
158password on all machines should not be underrated.  With discrete passwords,
159changing a password on N machines can be a mess.  You can also impose
160re-passwording restrictions with kerberos:  not only can a kerberos ticket
161be made to timeout after a while, but the kerberos system can require that
162the user choose a new password after a certain period of time (say, once a
163month).
164.Sh SECURING ROOT - ROOT-RUN SERVERS AND SUID/SGID BINARIES
165.Pp
166The prudent sysadmin only runs the servers he needs to, no more, no less.  Be
167aware that third party servers are often the most bug-prone.  For example,
168running an old version of imapd or popper is like giving a universal root
169ticket out to the entire world.  Never run a server that you have not checked
170out carefully.  Many servers do not need to be run as root.  For example,
171the ntalk, comsat, and finger daemons can be run in special user 'sandboxes'.
172A sandbox isn't perfect unless you go to a large amount of trouble, but the
173onion approach to security still stands:  If someone is able to break in
174through a server running in a sandbox, they still have to break out of the
175sandbox.  The more layers the attacker must break through, the lower the
176likelihood of his success.  Root holes have historically been found in
177virtually every server ever run as root, including basic system servers.
178If you are running a machine through which people only login via sshd and
179never login via telnetd or rshd or rlogind, then turn off those services!
180.Pp
181FreeBSD now defaults to running ntalkd, comsat, and finger in a sandbox.
182Another program which may be a candidate for running in a sandbox is
183named(8).  The default rc.conf includes the arguments necessary to run
184named in a sandbox in a commented-out form.  Depending on whether you
185are installing a new system or upgrading an existing system, the special
186user accounts used by these sandboxes may not be installed.  The prudent
187sysadmin would research and implement sandboxes for servers whenever possible.
188.Pp
189There are a number of other servers that typically do not run in sandboxes:
190sendmail, popper, imapd, ftpd, and others.  There are alternatives to
191some of these, but installing them may require more work then you are willing
192to put (the convenience factor strikes again).  You may have to run these
193servers as root and rely on other mechanisms to detect break-ins that might
194occur through them.
195.Pp
196The other big potential root hole in a system are the suid-root and sgid
197binaries installed on the system.  Most of these binaries, such as rlogin,
198reside in /bin, /sbin, /usr/bin, or /usr/sbin.  While nothing is 100% safe,
199the system-default suid and sgid binaries can be considered reasonably safe.
200Still, root holes are occasionally found in these binaries.  A root hole
201was found in Xlib in 1998 that made xterm (which is typically suid) vulnerable.
202It is better to be safe then sorry and the prudent sysadmin will restrict suid
203binaries that only staff should run to a special group that only staff can
204access, and get rid of (chmod 000) any suid binaries that nobody uses.  A
205server with no display generally does not need an xterm binary.  Sgid binaries
206can be almost as dangerous.  If an intruder can break an sgid-kmem binary the
207intruder might be able to read /dev/kmem and thus read the crypted password
208file, potentially compromising any passworded account.  An intruder that breaks
209the tty group can write to almost user's tty.  If a user is running a terminal
210program or emulator with a talk-back feature, the intruder can potentially
211generate a data stream that causes the user's terminal to echo a command, which
212is then run as that user.
213.Sh SECURING USER ACCOUNTS
214.Pp
215User accounts are usually the most difficult to secure.  While you can impose
216Draconian access restrictions on your staff and *-out their passwords, you
217may not be able to do so with any general user accounts you might have.  If
218you do have sufficient control then you may win out and be able to secure the
219user accounts properly.  If not, you simply have to be more vigilant in your
220monitoring of those accounts.  Use of ssh and kerberos for user accounts is
221more problematic, but still a very good solution compared to a crypted
222password.
223.Sh SECURING THE PASSWORD FILE
224.Pp
225The only sure fire way is to *-out as many passwords as you can and
226use ssh or kerberos for access to those accounts.  Even though the
227crypted password file (/etc/spwd.db) can only be read by root, it may
228be possible for a intruder to obtain read access to that file even if the
229attacker cannot obtain root-write access.
230.Pp
231Your security scripts should always check for and report changes to
232the password file (see 'Checking file integrity' below).
233.Sh SECURING THE KERNEL CORE, RAW DEVICES, AND FILESYSTEMS
234.Pp
235If an attacker breaks root he can do just about anything, but there
236are certain conveniences.  For example, most modern kernels have a
237packet sniffing device driver built in.  Under FreeBSD it is called
238the 'bpf' device.  A intruder will commonly attempt to run a packet sniffer
239on a compromised machine.  You do not need to give the intruder the
240capability and most systems should not have the bpf device compiled in.
241Unfortunately, there is another kernel feature called the Loadable Kernel
242Module interface.  An enterprising intruder can use an LKM to install
243his own bpf device or other sniffing device on a running kernel.  If you
244do not need to use the module loader, turn it off in the kernel configuration
245with the NO_LKM option.
246.Pp
247But even if you turn off the bpf device, and turn off the module loader,
248you still have /dev/mem and /dev/kmem to worry about.  For that matter,
249the intruder can still write raw devices.  To avoid this you have to run
250the kernel at a higher secure level... at least securelevel 1.  The securelevel
251can be set with a sysctl on the kern.securelevel variable.  Once you have
252set the securelevel to 1, write access to raw devices will be denied and
253special chflags flags, such as 'schg', will be enforced.  You must also ensure
254that the 'schg' flag is set on critical startup binaries, directories, and
255script files - everything that gets run up to the point where the securelevel
256is set.  This might be overdoing it, and upgrading the system is much more
257difficult when you operate at a higher secure level.  You may compromise and
258run the system at a higher secure level but not set the schg flag for every
259system file and directory under the sun.
260.Sh CHECKING FILE INTEGRITY: BINARIES, CONFIG FILES, ETC
261.Pp
262When it comes right down to it, you can only protect your core system
263configuration and control files so much before the convenience factor
264rears its ugly head.  The last layer of your security onion is perhaps
265the most important - detection.
266.Pp
267The only correct way to check a system's file integrity is via another,
268more secure system.  It is fairly easy to setup a 'secure' system: you
269simply do not run any services on it.  With a secure system in place you
270can then give it access to other system's root spaces via ssh.  This may
271seem like a security breech, but you have to put your trust somewhere and
272as long as you don't do something stupid like run random servers it really
273is possible to build a secure machine.  When I say 'secure' here, I assuming
274physical access security as well, of course.  Given a secure machine with
275root access on all your other machines, you can then write security scripts
276ON the secure machine to check the other machines on the system.  The most
277common way of checking is to have the security script scp(1) over a find
278and md5 binary and then ssh a shell command to the remote machine to md5
279all the files in the system (or, at least, the /, /var, and /usr partitions!).
280The security machine copies the results to a file and diff's them against
281results from a previous run (or compares the results against its own
282binaries), then emails each staff member a daily report of differences.
283.Pp
284Another way to do this sort of check is to NFS export the major filesystems
285from every other machine to the security machine.  This is somewhat more
286network intensive but also virtually impossible for an intruder to detect
287or spoof.
288.Pp
289A good security script will also check for changes to user and staff members
290access configuration files:  .rhosts, .shosts, .ssh/authorized_keys, and
291so forth... files that might fall outside the prevue of the MD5 check.
292.Pp
293A good security script will check for suid and sgid binaries on all
294filesystems and report their absolute existence as well as a diff against
295the previous report or some baseline (say, make a baseline once a week).
296While you can turn off the ability to run suid and sgid binaries on certain
297filesystems through the 'nosuid' option in fstab/mount, you cannot turn this
298off on root and anyone who breaks root can just install their binary their.
299If you have a huge amount of user disk space, though, it may be useful to
300disallow suid binaries and devices ('nodev' option) on the user partitions
301so you do not have to scan them for such.  I would scan them anyway, though,
302at least once a week, since the object of this onion layer is detection of
303a break-in.
304.Pp
305Process accounting (see accton(1)) is a relatively low-overhead feature of
306the operating system which I recommend using as a post-break-in evaluation
307mechanism.  It is especially useful in tracking down how an intruder has
308actually broken root on a system, assuming the file is still intact after
309the break-in occurs.
310.Pp
311Finally, security scripts should process the log files and the logs themselves
312should be generated in as secured a manner as possible - remote syslog can be
313very useful.  An intruder tries to cover his tracks, and log files are critical
314to the sysadmin trying to track down the time and method of the initial break-in.
315.Sh PARANOIA
316.Pp
317A little paranoia never hurts.  As a rule, a sysadmin can add any number
318of security features as long as they do not effect convenience, and
319can add security features that do effect convenience with some added
320thought.
321.Sh SPECIAL SECTION ON D.O.S. ATTACKS
322.Pp
323This section covers Denial of Service attacks.  A DOS attack is typically
324a packet attack.  While there isn't much you can do about modern spoofed
325packet attacks that saturate your network, you can generally limit the damage
326by ensuring that the attacks cannot take down your servers.
327.Bl -enum -offset indent
328.It
329Limiting server forks
330.It
331Limiting springboard attacks (ICMP response attacks, ping broadcast, etc...)
332.It
333Kernel Route Cache
334.El
335.Pp
336A common DOS attack is against a forking server that attempts to cause the
337server to eat processes, file descriptors, and memory until the machine
338dies.  Inetd (see inetd(8)) has several options to limit this sort of attack.
339It should be noted that while it is possible to prevent a machine from going
340down it is not generally possible to prevent a service from being disrupted
341by the attack.  Read the inetd manual page carefully and pay specific attention
342to the -c, -C, and -R options.  Note that spoofed-IP attacks will circumvent
343the -C option to inetd, so typically a combination of options must be used.
344Some standalone servers have self-fork-limitation parameters.
345.Pp
346Sendmail has its -OMaxDaemonChildren option which tends to work much
347better then trying to use sendmail's load limiting options due to the
348load lag.  You should specify a MaxDaemonChildren parameter when you start
349sendmail high enough to handle your expected load but no so high that the
350computer cannot handle that number of sendmails without falling on its face.
351It is also prudent to run sendmail in queued mode (-ODeliveryMode=queued)
352and to run the daemon (sendmail -bd) separate from the queue-runs
353(sendmail -q15m).   If you still want realtime delivery you can run the queue
354at a much lower interval, such as -q1m, but be sure to specify a reasonable
355MaxDaemonChildren option for that sendmail to prevent cascade failures.
356.Pp
357Syslogd can be attacked directly and it is strongly recommended that you use
358the -s option whenever possible, and the -a option otherwise.
359.Pp
360You should also be fairly careful
361with connect-back services such as tcpwrapper's reverse-identd, which can
362be attacked directly.  You generally do not want to use the reverse-ident
363feature of tcpwrappers for this reason.
364.Pp
365It is a very good idea to protect internal services from external access
366by firewalling them off at your border routers.  The idea here is to prevent
367saturation attacks from outside your LAN, not so much to protect internal
368services from root network-based root compromise.  Always configure an exclusive
369firewall, i.e. 'firewall everything *except* ports A, B, C, D, and M-Z'.   This
370way you can firewall off all of your low ports except for certain specific
371services such as named (if you are primary for a zone), ntalkd, sendmail,
372and other internet-accessible services.
373If you try to configure the firewall the other
374way - as an inclusive or permissive firewall, there is a good chance that you
375will forget to 'close' a couple of services or that you will add a new internal
376service and forget to update the firewall.  You can still open up the
377high-numbered port range on the firewall to allow permissive-like operation
378without compromising your low ports.  Also take note that FreeBSD allows you to
379control the range of port numbers used for dynamic binding via the various
380net.inet.ip.portrange sysctl's (sysctl -a | fgrep portrange), which can also
381ease the complexity of your firewall's configuration.  I usually use a normal
382first/last range of 4000 to 5000, and a hiport range of 49152 to 65535, then
383block everything under 4000 off in my firewall ( except for certain specific
384internet-accessible ports, of course ).
385.Pp
386Another common DOS attack is called a springboard attack - to attack a server
387in a manner that causes the server to generate responses which then overload
388the server, the local network, or some other machine.  The most common attack
389of this nature is the ICMP PING BROADCAST attack.  The attacker spoofed ping
390packets sent to your LAN's broadcast address with the source IP address set
391to the actual machine they wish to attack.  If your border routers are not
392configured to stomp on ping's to broadcast addresses, your LAN winds up
393generating sufficient responses to the spoofed source address to saturate the
394victim, especially when the attacker uses the same trick on several dozen
395broadcast addresses over several dozen different networks at once.  Broadcast
396attacks of over a hundred and twenty megabits have been measured.  A second
397common springboard attack is against the ICMP error reporting system.  By
398constructing packets that generate ICMP error responses, an attacker can
399saturate a server's incoming network and cause the server to saturate its
400outgoing network with ICMP responses.  This type of attack can also crash the
401server by running it out of mbuf's, especially if the server cannot drain the
402ICMP responses it generates fast enough.  The FreeBSD kernel has a new kernel
403compile option called ICMP_BANDLIM which limits the effectiveness of these
404sorts of attacks.  The last major class of springboard attacks is related to
405certain internal inetd services such as the udp echo service.  An attacker
406simply spoofs a UDP packet with the source address being server A's echo port,
407and the destination address being server B's echo port, where server A and B
408are both on your LAN.  The two servers then bounce this one packet back and
409forth between each other.  The attacker can overload both servers and their
410LANs simply by injecting a few packets in this manner.  Similar problems
411exist with the internal chargen port.  A competent sysadmin will turn off all
412of these inetd-internal test services.
413.Pp
414Spoofed packet attacks may also be used to overload the kernel route cache.
415Refer to the net.inet.ip.rtexpire, rtminexpire, and rtmaxcache sysctl
416parameters.  A spoofed packet attack that uses a random source IP will cause
417the kernel to generate a temporary cached route in the route table, viewable
418with 'netstat -rna | fgrep W3'.  These routes typically timeout in 1600
419seconds or so.  If the kernel detects that the cached route table has gotten
420too big it will dynamically reduce the rtexpire but will never decrease it to
421less then rtminexpire.  There are two problems:  (1) The kernel does not react
422quickly enough when a lightly loaded server is suddenly attacked, and (2) The
423rtminexpire is not low enough for the kernel to survive a sustained attack.
424If your servers are connected to the internet via a T3 or better it may be
425prudent to manually override both rtexpire and rtminexpire via sysctl(8).
426Never set either parameter to zero (unless you want to crash the machine :-)).
427Setting both parameters to 2 seconds should be sufficient to protect the route
428table from attack.
429
430.Sh SEE ALSO
431.Pp
432.Xr accton 1 ,
433.Xr chflags 1 ,
434.Xr find 1 ,
435.Xr kerberos 1 ,
436.Xr md5 1 ,
437.Xr ssh 1 ,
438.Xr sshd 1 ,
439.Xr syslogd 1 ,
440.Xr xdm 1 ,
441.Xr sysctl 8
442.Sh HISTORY
443The
444.Nm
445manual page was originally written by Matthew Dillon and first appeared
446in FreeBSD-3.0.1, December 1998.
447