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