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