1.\" $NetBSD: security.7,v 1.15 2017/07/03 21:30:59 wiz Exp $ 2.\" 3.\" Copyright (c) 2006, 2011 Elad Efrat <elad@NetBSD.org> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd May 21, 2016 29.Dt SECURITY 7 30.Os 31.Sh NAME 32.Nm security 33.Nd 34.Nx 35security features 36.Sh DESCRIPTION 37.Nx 38supports a variety of security features. 39Below is a brief description of them with some quick usage examples 40that will help you get started. 41.Pp 42Contents: 43.Pp 44.Bl -hyphen -compact -offset indent 45.It 46Veriexec 47.Pq file integrity 48.It 49Exploit mitigation 50.It 51Per-user 52.Pa /tmp 53directory 54.It 55Information filtering 56.It 57Administrative security 58.El 59.Ss Veriexec 60.Em Veriexec 61is a file integrity subsystem. 62.Pp 63For more information about it, and a quick guide on how to use it, please see 64.Xr veriexec 8 . 65.Pp 66In a nutshell, once enabled, 67.Em Veriexec 68can be started as follows: 69.Bd -literal -offset indent 70# veriexecgen && veriexecctl load 71.Ed 72.Ss Exploit mitigation 73.Nx 74incorporates some exploit mitigation features. 75The purpose of exploit mitigation features is to interfere 76with the way exploits work, in order to prevent them from succeeding. 77Due to that, some features may have other impacts on the system, so be sure to 78fully understand the implications of each feature. 79.Pp 80.Nx 81provides the following exploit mitigation features: 82.Pp 83.Bl -hyphen -compact -offset indent 84.It 85.Tn PaX ASLR 86.Pq Address Space Layout Randomization . 87.It 88.Tn PaX MPROTECT 89.Xr ( mprotect 2 90restrictions) 91.It 92.Tn PaX SegvGuard 93.It 94.Xr gcc 1 95stack-smashing protection 96.Pq Tn SSP 97.It 98bounds checked libc functions 99.Pq Tn FORTIFY_SOURCE 100.It 101Protections against 102.Dv NULL 103pointer dereferences 104.El 105.Ss PaX ASLR 106.Em PaX ASLR 107implements Address Space Layout Randomization 108.Pq Tn ASLR , 109meant to complement non-executable mappings. 110Its purpose is to harden prediction of the address space layout, namely 111location of library and application functions that can be used by an attacker 112to circumvent non-executable mappings by using a technique called 113.Dq return to library 114to bypass the need to write new code to (potentially executable) regions of 115memory. 116.Pp 117When 118.Em PaX ASLR 119is used, it is more likely the attacker will fail to predict the addresses of 120such functions, causing the application to segfault. 121To detect cases where an attacker might try and brute-force the return address 122of respawning services, 123.Em PaX Segvguard 124can be used (see below). 125.Pp 126For non-PIE 127.Pq Position Independent Executable 128executables, the 129.Nx 130.Em PaX ASLR 131implementation introduces randomization to the following memory regions: 132.Pp 133.Bl -enum -compact -offset indent 134.It 135The stack 136.El 137.Pp 138For 139.Tn PIE 140executables: 141.Pp 142.Bl -enum -compact -offset indent 143.It 144The program itself (exec base) 145.It 146All shared libraries 147.It 148The data segment 149.It 150The stack 151.El 152.Pp 153While it can be enabled globally, 154.Nx 155provides a tool, 156.Xr paxctl 8 , 157to enable 158.Em PaX ASLR 159on a per-program basis. 160.Pp 161Example usage: 162.Bd -literal -offset indent 163# paxctl +A /usr/sbin/sshd 164.Ed 165.Pp 166Enabling 167.Em PaX ASLR 168globally: 169.Bd -literal -offset indent 170# sysctl -w security.pax.aslr.global=1 171.Ed 172.Ss PaX MPROTECT 173.Em PaX MPROTECT 174implements memory protection restrictions, 175meant to complement non-executable mappings. 176The purpose is to prevent situations where malicious code attempts to mark 177writable memory regions as executable, often by trashing arguments to an 178.Xr mprotect 2 179call. 180.Pp 181While it can be enabled globally, 182.Nx 183provides a tool, 184.Xr paxctl 8 , 185to enable 186.Em PaX MPROTECT 187on a per-program basis. 188.Pp 189Example usage: 190.Bd -literal -offset indent 191# paxctl +M /usr/sbin/sshd 192.Ed 193.Pp 194Enabling 195.Em PaX MPROTECT 196globally: 197.Bd -literal -offset indent 198# sysctl -w security.pax.mprotect.global=1 199.Ed 200.Pp 201PaX MPROTECT affects the following three uses: 202.Bl -bullet -offset indent 203.It 204Processes that utilize code generation (such as the JVM) might need to have 205MPROTECT disabled. 206.It 207Miscompiled programs that have text relocations, will now core dump instead 208of having their relocations corrected. 209You will need to fix those programs (recompile them properly). 210.It 211Debugger breakpoints: 212.Xr gdb 1 213needs to be able to write to the text segment in order to insert and 214delete breakpoints. 215This will not work unless MPROTECT is disabled on the executable. 216.El 217.Ss PaX Segvguard 218.Em PaX Segvguard 219monitors the number of segmentation faults in a program on a per-user basis, 220in an attempt to detect on-going exploitation attempts and possibly prevent 221them. 222For instance, 223.Em PaX Segvguard 224can help detect when an attacker tries to brute-force a function 225return address, when attempting to perform a return-to-lib attack. 226.Pp 227.Em PaX Segvguard 228consumes kernel memory, so use it wisely. 229While it provides rate-limiting protections, records are tracked for all 230users on a per-program basis, meaning that irresponsible use may result in 231tracking all segmentation faults in the system, possibly consuming all kernel 232memory. 233.Pp 234For this reason, it is highly recommended to have 235.Em PaX Segvguard 236enabled explicitly only for network services or 237other processes deemed as critical to system security. 238Enabling 239.Em PaX Segvguard 240explicitly works like this: 241.Bd -literal -offset indent 242# paxctl +G /usr/sbin/sshd 243.Ed 244.Pp 245However, a global knob is still provided, for use in strict environments 246with no local users (for example, some network appliances, embedded devices, 247and firewalls) 248.Bd -literal -offset indent 249# sysctl -w security.pax.segvguard.global=1 250.Ed 251.Pp 252Explicitly disabling 253.Em PaX Segvguard 254is also possible: 255.Bd -literal -offset indent 256# paxctl +g /bin/ls 257.Ed 258.Pp 259In addition, 260.Em PaX Segvguard 261provides several tunable options. 262For example, to limit a program to 5 segmentation faults from the same user in 263a 60 second timeframe: 264.Bd -literal -offset indent 265# sysctl -w security.pax.segvguard.max_crashes=5 266# sysctl -w security.pax.segvguard.expiry_timeout=60 267.Ed 268.Pp 269The number of seconds a user will be suspended from running the culprit 270program is also configurable. 271For example, 10 minutes seem like a sane setting: 272.Bd -literal -offset indent 273# sysctl -w security.pax.segvguard.suspend_timeout=600 274.Ed 275.Ss GCC Stack Smashing Protection ( SSP ) 276As of 277.Nx 4.0 , 278.Xr gcc 1 279includes 280.Em SSP , 281a set of compiler extensions to raise the bar on exploitation attempts by 282detecting corruption of variables and buffer overruns, which may be used to 283affect program control flow. 284.Pp 285Upon detection of a buffer overrun, 286.Em SSP 287will immediately abort execution of the program and send a log message 288to 289.Xr syslog 3 . 290.Pp 291The system (userland and kernel) can be built with 292.Em SSP 293by using the 294.Dq USE_SSP 295flag in 296.Pa /etc/mk.conf : 297.Bd -literal -offset indent 298USE_SSP=yes 299.Ed 300.Pp 301You are encouraged to use 302.Em SSP 303for software you build, by providing one of the 304.Fl fstack-protector 305or 306.Fl fstack-protector-all 307flags to 308.Xr gcc 1 . 309Keep in mind, however, that 310.Em SSP 311will not work for functions that make use of 312.Xr alloca 3 , 313as the latter modifies the stack size during run-time, while 314.Em SSP 315relies on it being a compile-time static. 316.Pp 317Use of 318.Em SSP 319is especially encouraged on platforms without per-page execute bit granularity 320such as i386. 321As of 322.Nx 6.0 , 323.Em SSP 324is used by default on i386 and amd64 architectures. 325.Ss FORTIFY_SOURCE 326The so-called 327.Em FORTIFY_SOURCE 328is a relatively simple technique to detect a subset of buffer overflows 329before these can do damage. 330It is integrated to 331.Xr gcc 1 332together with some common memory and string functions in the standard 333C library of 334.Nx . 335.Pp 336The underlying idea builds on the observation that there are cases where 337the compiler knows the size of a buffer. 338If a buffer overflow is suspected in a function that does little or no 339bounds checking, either a compile time warning can be issued or a 340safer substitute function can be used at runtime. 341Refer to 342.Xr ssp 3 343for additional details. 344.Pp 345The 346.Em FORTIFY_SOURCE 347is enabled by default in some parts of the 348.Nx 349source tree. 350It is also possible to explicitly enable it by defining 351the following in 352.Xr mk.conf 5 : 353.Bd -literal -offset indent 354USE_FORT=yes 355.Ed 356.Ss Protections against NULL pointer dereferences 357A certain class of attacks rely on kernel bugs that dereference 358.Dv NULL 359pointers. 360If user processes are allowed to map the virtual address 0 with 361.Xr mmap 2 362or by other means, there is a risk that code or data 363can be injected into the kernel address space. 364.Pp 365In 366.Nx 367it is possible to restrict whether user processes are 368allowed to make mappings at the zero address. 369By default, address 0 mappings are restricted on all architectures. 370It is however known that some third-party programs 371may not function properly with the restriction. 372Such mappings can be allowed either by using the 373.Dv USER_VA0_DISABLE_DEFAULT 374kernel configuration option or by changing the following variable at runtime: 375.Bd -literal -offset indent 376# sysctl -w vm.user_va0_disable=0 377.Ed 378.Pp 379Note that if 380.Em securelevel 381(see 382.Xr secmodel_securelevel 9 ) 383is greater than zero, it is not possible to change the 384.Xr sysctl 8 385variable. 386.Ss Per-user temporary storage 387It is possible to configure per-user temporary storage to avoid potential 388security issues (race conditions, etc.) in programs that do not make secure 389usage of 390.Pa /tmp . 391.Pp 392To enable per-user temporary storage, add the following line to 393.Xr rc.conf 5 : 394.Bd -literal -offset indent 395per_user_tmp=YES 396.Ed 397.Pp 398If 399.Pa /tmp 400is a mount point, you will also need to update its 401.Xr fstab 5 402entry to use 403.Dq /private/tmp 404(or whatever directory you want, if you override the default using the 405.Dq per_user_tmp_dir 406.Xr rc.conf 5 407keyword) instead of 408.Dq /tmp . 409.Pp 410Following that, run: 411.Bd -literal -offset indent 412# /etc/rc.d/perusertmp start 413.Ed 414.Pp 415The per-user temporary storage is implemented by using 416.Dq magic symlinks . 417These are further described in 418.Xr symlink 7 . 419.Ss Information filtering 420.Nx 421provides administrators the ability to restrict information passed from 422the kernel to userland so that users can only view information they 423.Dq own . 424.Pp 425The hooks that manage this restriction are located in various parts of the 426system and affect programs such as 427.Xr ps 1 , 428.Xr fstat 1 , 429and 430.Xr netstat 1 . 431Information filtering is enabled as follows: 432.Bd -literal -offset indent 433# sysctl -w security.curtain=1 434.Ed 435.Ss Administrative security 436Also certain administrative tasks are related to security. 437For instance, the daily maintenance script includes some basic 438consistency checks; see 439.Xr security.conf 5 440for more details. 441In particular, it is possible to configure 442.Nx 443to automatically audit all third-party packages installed via 444.Xr pkgsrc 7 . 445To audit for any known vulnerabilities on daily basis, set the following in 446.Pa /etc/daily.conf : 447.Bd -literal -offset indent 448fetch_pkg_vulnerabilities=YES 449.Ed 450.Sh SEE ALSO 451.Xr ssp 3 , 452.Xr options 4 , 453.Xr paxctl 8 , 454.Xr sysctl 8 , 455.Xr veriexec 8 , 456.Xr kauth 9 457.\" 458.Rs 459.%A Joseph Kong 460.%B "Designing BSD Rootkits: An Introduction to Kernel Hacking" 461.%D 2007 462.%I "No Starch Press" 463.Re 464.\" 465.Rs 466.%A Enrico Perla 467.%A Massimiliano Oldani 468.%B "A Guide to Kernel Exploitation: Attacking the Core" 469.%D 2010 470.%I "Elsevier" 471.Re 472.\" 473.Rs 474.%A Erik Buchanan 475.%A Ryan Roemer 476.%A Hovav Shacham 477.%A Stefan Savage 478.%T "When Good Instructions Go Bad: \ 479Generalizing Return-Oriented Programming to RISC" 480.%P 27-38 481.%O CCS '08: Proceedings of the 15th ACM Conference \ 482on Computer and Communications Security 483.%I ACM Press 484.%D October 27-31, 2008 485.%U http://cseweb.ucsd.edu/~hovav/dist/sparc.pdf 486.Re 487.\" 488.Rs 489.%A Sebastian Krahmer 490.%T "x86-64 Buffer Overflow Exploits and \ 491the Borrowed Code Chunks Exploitation Technique" 492.%D September 28, 2005 493.%U http://www.suse.de/~krahmer/no-nx.pdf 494.Re 495.Sh AUTHORS 496Many of the security features were pioneered by 497.An Elad Efrat Aq Mt elad@NetBSD.org . 498