1.\"- 2.\" SPDX-License-Identifer: BSD-2-Clause 3.\" 4.\" Copyright © 2023 The FreeBSD Foundation 5.\" 6.\" This documentation was written by Ed Maste <emaste@freebsd.org>, and 7.\" Olivier Certner <olce.freebsd@certner.fr> at Kumacom SAS, under 8.\" sponsorship of the FreeBSD Foundation. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.Dd July 23, 2024 32.Dt MITIGATIONS 7 33.Os 34.Sh NAME 35.Nm mitigations 36.Nd FreeBSD Security Vulnerability Mitigations 37.Sh SYNOPSIS 38In 39.Fx , 40various security mitigations are employed to limit the impact of 41vulnerabilities and protect the system from malicious attacks. 42Some of these mitigations have run-time controls to enable them on a global 43or per-process basis, some are optionally enabled or disabled at compile time, 44and some are inherent to the implementation and have no controls. 45.Pp 46The following vulnerability mitigations are covered in this document: 47.Pp 48.Bl -bullet -compact 49.It 50Address Space Layout Randomization (ASLR) 51.It 52Position Independent Executable (PIE) 53.It 54Write XOR Execute page protection policy 55.It 56.Dv PROT_MAX 57.It 58Relocation Read-Only (RELRO) 59.It 60Bind Now 61.It 62Stack Overflow Protection 63.It 64Supervisor Mode Memory Protection 65.It 66Capsicum 67.It 68Firmware and Microcode 69.It 70Architectural Vulnerability Mitigations 71.El 72.Pp 73Please note that the effectiveness and availability of these mitigations may 74vary depending on the 75.Fx 76version and system configuration. 77.Sh DESCRIPTION 78Security vulnerability mitigations are techniques employed in 79.Fx 80to limit the potential impact of security vulnerabilities in software and 81hardware. 82It is essential to understand that mitigations do not directly address the 83underlying security issues. 84They are not a substitute for secure coding practices. 85Mitigations serve as an additional layer of defense, helping to reduce the 86likelihood of a successful exploitation of vulnerabilities by making it 87more difficult for attackers to achieve their objectives. 88.Pp 89This manual page describes the security mitigations implemented in 90.Fx 91to enhance the overall security of the operating system. 92Each mitigation is designed to protect against specific types of attacks 93and vulnerabilities. 94.\" 95.Sh SOFTWARE VULNERABILITY MITIGATIONS 96.Ss Address Space Layout Randomization (ASLR) 97Address Space Layout Randomization (ASLR) is a security mitigation technique 98that works by randomizing the memory addresses where system and application 99code, data, and libraries are loaded, making it more challenging for attackers 100to predict the memory layout and exploit vulnerabilities. 101.Pp 102ASLR introduces randomness into the memory layout during process execution, 103reducing the predictability of memory addresses. 104ASLR is intended to make exploitation more difficult in the event that an 105attacker discovers a software vulnerability, such as a buffer overflow. 106.Pp 107ASLR can be enabled on both a global and per-process basis. 108Global control is provided by a separate set of 109.Xr sysctl 8 110knobs for 32- and 64-bit processes. 111It can be or disabled on a per-process basis via 112.Xr proccontrol 1 . 113Note that an ASLR mode change takes effect upon address space change, 114i.e., upon 115.Xr execve 2 . 116.Pp 117Global controls for 32-bit processes: 118.Bl -tag -width kern.elf32.aslr.pie_enable 119.It Va kern.elf32.aslr.enable 120Enable ASLR for 32-bit ELF binaries, other than Position Independent 121Executable (PIE) binaries. 122.It Va kern.elf32.aslr.pie_enable 123Enable ASLR for 32-bit Position Independent Executable (PIE) ELF binaries. 124.It Va kern.elf32.aslr.honor_sbrk 125Reserve the legacy 126.Xr sbrk 2 127region for compatibility with older binaries. 128.It Va kern.elf32.aslr.stack 129Randomize the stack location for 32-bit ELF binaries. 130.El 131.Pp 132Global controls for 64-bit processes: 133.Bl -tag -width kern.elf64.aslr.pie_enable 134.It Va kern.elf64.aslr.enable 135Enable ASLR for 64-bit ELF binaries, other than Position Independent 136Executable (PIE) binaries. 137.It Va kern.elf64.aslr.pie_enable 138Enable ASLR for 64-bit Position Independent Executable (PIE) ELF binaries. 139.It Va kern.elf64.aslr.honor_sbrk 140Reserve the legacy 141.Xr sbrk 2 142region for compatibility with older binaries. 143.It Va kern.elf64.aslr.stack 144Randomize the stack location for 64-bit ELF binaries. 145.El 146.Pp 147To execute a command with ASLR enabled or disabled: 148.Pp 149proccontrol 150.Fl m Ar aslr 151.Op Fl s Ar enable | disable 152.Ar command 153.\" 154.Ss Position Independent Executable (PIE) 155PIE binaries are executable files that do not have a fixed load address. 156They can be loaded at an arbitrary memory address by the 157.Xr rtld 1 158run-time linker. 159With ASLR they are loaded at a random address on each execution. 160.\" 161.Ss Write XOR Execute page protection policy 162Write XOR Execute (W^X) is a vulnerability mitigation strategy that strengthens 163the security of the system by controlling memory access permissions. 164.Pp 165Under the W^X mitigation, memory pages may be writable (W) or executable (E), 166but not both at the same time. 167This means that code execution is prevented in areas of memory that are 168designated as writable, and writing or modification of memory is restricted in 169areas marked for execution. 170Applications that perform Just In Time (JIT) compilation need to be adapted 171to be compatible with W^X. 172.Pp 173There are separate 174.Xr sysctl 8 175knobs to control W^X policy enforcement for 32- and 64-bit processes. 176The W^X policy is enabled by setting the appropriate 177.Dv allow_wx 178sysctl to 0. 179.Bl -tag -width kern.elf64.allow_wx 180.It Va kern.elf32.allow_wx 181Allow 32-bit processes to map pages simultaneously writable and executable. 182.It Va kern.elf64.allow_wx 183Allow 64-bit processes to map pages simultaneously writable and executable. 184.El 185.\" 186.Ss PROT_MAX 187.Dv PROT_MAX 188is a 189.Fx Ns 190-specific extension to 191.Xr mmap 2 . 192.Dv PROT_MAX 193provides the ability to set the maximum protection of a region allocated by 194.Xr mmap 2 195and later altered by 196.Xr mprotect 2 . 197For example, memory allocated originally with an mmap prot argument of 198PROT_MAX(PROT_READ | PROT_WRITE) | PROT_READ 199may be made writable by a future 200.Xr mprotect 2 201call, but may not be made executable. 202.\" 203.Ss Relocation Read-Only (RELRO) 204Relocation Read-Only (RELRO) is a mitigation tool that makes certain portions 205of a program's address space that contain ELF metadata read-only, after 206relocation processing by 207.Xr rtld 1 . 208.Pp 209When enabled in isolation the RELRO option provides 210.Em partial RELRO 211support. 212In this case the Procedure Linkage Table (PLT)-related part of the 213Global Offset Table (GOT) (in the section typically named .got.plt) remains 214writable. 215.Pp 216RELRO is enabled by default. 217The 218.Xr src.conf 5 219build-time option 220.Va WITHOUT_RELRO 221may be used to disable it. 222.Ss BIND_NOW 223The 224.Va WITH_BIND_NOW 225.Xr src.conf 5 226build-time option causes binaries to be built with the 227.Dv DF_BIND_NOW 228flag set. 229The run-time loader 230.Xr rtld 1 231will then perform all relocation processing when the process starts, instead of 232on demand (on the first access to each symbol). 233.Pp 234When enabled in combination with 235.Dv RELRO 236(which is enabled by default) this provides 237.Em full RELRO . 238The entire GOT (.got and .got.plt) are made read-only at program startup, 239preventing attacks on the relocation table. 240Note that this results in a nonstandard Application Binary Interface (ABI), 241and it is possible that some applications may not function correctly. 242.\" 243.Ss Stack Overflow Protection 244.Fx 245supports stack overflow protection using the Stack Smashing Protector 246.Pq SSP 247compiler feature. 248In userland, SSP adds a per-process randomized canary at the end of every stack 249frame which is checked for corruption upon return from the function. 250In the kernel, a single randomized canary is used globally except on aarch64, 251which has a 252.Dv PERTHREAD_SSP 253.Xr config 8 254option to enable per-thread randomized canaries. 255If stack corruption is detected, then the process aborts to avoid potentially 256malicious execution as a result of the corruption. 257SSP may be enabled or disabled when building 258.Fx 259base with the 260.Xr src.conf 5 261SSP knob. 262.Pp 263When 264.Va WITH_SSP 265is enabled, which is the default, world is built with the 266.Fl fstack-protector-strong 267compiler option. 268The kernel is built with the 269.Fl fstack-protector 270option. 271.Pp 272In addition to SSP, a 273.Dq FORTIFY_SOURCE 274implementation is supported up to level 2 by defining 275.Va _FORTIFY_SOURCE 276to 277.Dv 1 278or 279.Dv 2 280before including any 281.Fx 282headers. 283.Fx 284world builds can set 285.Va FORTIFY_SOURCE 286in the environment or 287.Pa /etc/src-env.conf 288to provide a default value for 289.Va _FORTIFY_SOURCE . 290When enabled, 291.Dq FORTIFY_SOURCE 292enables extra bounds checking in various functions that accept buffers to be 293written into. 294These functions currently have extra bounds checking support: 295.Bl -column -offset indent "snprintf()" "memmove()" "strncpy()" "vsnprintf()" "readlink()" 296.It Fn bcopy Ta Fn bzero Ta Fn fgets Ta Fn getcwd Ta Fn gets 297.It Fn memcpy Ta Fn memmove Ta Fn memset Ta Fn read Ta Fn readlink 298.It Fn snprintf Ta Fn sprintf Ta Fn stpcpy Ta Fn stpncpy Ta Fn strcat 299.It Fn strcpy Ta Fn strncat Ta Fn strncpy Ta Fn vsnprintf Ta Fn vsprintf 300.El 301.Pp 302.Dq FORTIFY_SOURCE 303requires compiler support from 304.Xr clang 1 305or 306.Xr gcc 1 , 307which provide the 308.Xr __builtin_object_size 3 309function that is used to determine the bounds of an object. 310This feature works best at optimization levels 311.Fl O1 312and above, as some object sizes may be less obvious without some data that the 313compiler would collect in an optimization pass. 314.Pp 315Similar to SSP, violating the bounds of an object will cause the program to 316abort in an effort to avoid malicious execution. 317This effectively provides finer-grained protection than SSP for some class of 318function and system calls, along with some protection for buffers allocated as 319part of the program data. 320.\" 321.Ss Supervisor mode memory protection 322Certain processors include features that prevent unintended access to memory 323pages accessible to userspace (non-privileged) code, while in a privileged 324mode. 325One feature prevents execution, intended to mitigate exploitation of kernel 326vulnerabilities from userland. 327Another feature prevents unintended reads from or writes to user space memory 328from the kernel. 329This also provides effective protection against NULL pointer dereferences from 330kernel. 331.Bl -column -offset indent "Architecture" "Feature" "Access Type Prevented" 332.It Sy Architecture Ta Sy Feature Ta Sy Access Type Prevented 333.It amd64 Ta SMAP Ta Read / Write 334.It amd64 Ta SMEP Ta Execute 335.It arm64 Ta PAN Ta Read / Write 336.It arm64 Ta PXN Ta Execute 337.It riscv Ta SUM Ta Read / Write 338.It riscv Ta - Ta Execute 339.El 340.Pp 341These features are automatically used by the kernel. 342There is no user-facing configuration. 343.\" 344.Ss Capsicum 345Capsicum is a lightweight OS capability and sandbox framework. 346See 347.Xr capsicum 4 348for more information. 349.Sh HARDWARE VULNERABILITY MITIGATIONS 350.Ss Firmware and Microcode 351Recent years have seen an unending stream of new hardware vulnerabilities, 352notably CPU ones generally caused by detectable microarchitectural side-effects 353of speculative execution which leak private data from some other thread or 354process or sometimes even internal CPU state that is normally inaccessible. 355Hardware vendors usually address these vulnerabilities as they are discovered by 356releasing microcode updates, which may then be bundled into platform firmware 357updates 358.Pq historically called BIOS updates for PCs 359or packages to be updated by the operating system at boot time. 360.Pp 361Platform firmware updates, if available from the manufacturer, 362are the best defense as they provide coverage during early boot. 363Install them with 364.Pa sysutils/flashrom 365from the 366.Fx 367Ports Collection. 368.Pp 369If platform firmware updates are no longer available, 370packaged microcode is available for installation at 371.Pa sysutils/cpu-microcode 372and can be loaded at runtime using 373.Xr loader.conf 5 , 374see the package message for more details. 375.Pp 376The best defense overall against hardware vulnerabilities is to timely apply 377these updates when available, as early as possible in the boot process, 378and to disable the affected hardware's problematic functionalities when possible 379(e.g., CPU Simultaneous Multi-Threading). 380Software mitigations are only partial substitutes for these, but they can be 381helpful on out-of-support hardware or as complements for just-discovered 382vulnerabilities not yet addressed by vendors. 383Some software mitigations depend on hardware capabilities provided by a 384microcode update. 385.Ss Architectural Vulnerability Mitigations 386.Fx Ap s 387usual policy is to apply by default all OS-level mitigations that do 388not require recompilation, except those the particular hardware it is running on 389is known not to be vulnerable to 390.Pq which sometimes requires firmware updates , 391or those that are extremely detrimental to performance in proportion to the 392protection they actually provide. 393OS-level mitigations generally can have noticeable performance impacts on 394specific workloads. 395If your threat model allows it, you may want to try disabling some of them in 396order to possibly get better performance. 397Conversely, minimizing the risks may require you to explicitly enable the most 398expensive ones. 399The description of each vulnerability/mitigation indicates whether it is enabled 400or disabled by default and under which conditions. 401It also lists the knobs to tweak to force a particular status. 402.Ss Zenbleed 403The 404.Dq Zenbleed 405vulnerability exclusively affects AMD processors based on the Zen2 406microarchitecture. 407In contrast with, e.g., Meltdown and the different variants of Spectre, which 408leak data by leaving microarchitectural traces, Zenbleed is a genuine hardware 409bug affecting the CPU's architectural state. 410With particular sequences of instructions whose last ones are mispredicted by 411speculative execution, it is possible to make appear in an XMM register data 412previously put in some XMM register by some preceding or concurrent task 413executing on the same physical core 414.Po disabling Simultaneous Muti-Threading 415.Pq SMT 416is thus not a sufficient protection 417.Pc . 418.Pp 419According to the vulnerability's discoverer, all Zen2-based processors are 420affected 421.Po see 422.Lk https://lock.cmpxchg8b.com/zenbleed.html 423.Pc . 424As of August 2023, AMD has not publicly listed any corresponding errata but has 425issued a security bulletin 426.Pq AMD-SB-7008 427entitled 428.Dq Cross-Process Information Leak 429indicating that platform firmware fixing the vulnerability will be distributed 430to manufacturers no sooner than the end of 2023, except for Rome processors for 431which it is already available. 432No standalone CPU microcodes have been announced so far. 433The only readily-applicable fix mentioned by the discoverer is to set a bit of 434an undocumented MSR, which reportedly completely stops XMM register leaks. 435.Pp 436.Fx 437currently sets this bit by default on all Zen2 processors. 438In the future, it might set it by default only on those Zen2 processors whose 439microcode has not been updated to revisions fixing the vulnerability, once such 440microcode updates have been actually released and community-tested. 441To this mitigation are associated the following knobs: 442.Bl -tag -width indent 443.It Va machdep.mitigations.zenbleed.enable 444A read-write integer tunable and sysctl indicating whether the mitigation should 445be forcibly disabled (0), enabled (1) or if it is left to 446.Fx 447to selectively apply it (2). 448Any other integer value is silently converted to and treated as value 2. 449Note that this setting is silently ignored when running on non-Zen2 processors 450to ease applying a common configuration to heterogeneous machines. 451.It Va machdep.mitigations.zenbleed.state 452A read-only string indicating the current mitigation state. 453It can be either 454.Dq Not applicable , 455if the processor is not Zen2-based, 456.Dq Mitigation enabled 457or 458.Dq Mitigation disabled . 459This state is automatically updated each time the sysctl 460.Va machdep.mitigations.zenbleed.enable 461is written to. 462Note that it can become inaccurate if the chicken bit is set or cleared 463directly via 464.Xr cpuctl 4 465.Po which includes the 466.Xr cpucontrol 8 467utility 468.Pc . 469.El 470.Pp 471The performance impact and threat models related to these mitigations 472should be considered when configuring and deploying them in a 473.Fx 474system. 475.Pp 476Additional mitigation knobs are listed in the 477.Sx KNOBS AND TWEAKS 478section of 479.Xr security 7 . 480.Sh SEE ALSO 481.Xr elfctl 1 , 482.Xr proccontrol 1 , 483.Xr rtld 1 , 484.Xr mmap 2 , 485.Xr src.conf 5 , 486.Xr sysctl.conf 5 , 487.Xr security 7 , 488.Xr cpucontrol 8 , 489.Xr sysctl 8 490