xref: /freebsd/share/man/man7/mitigations.7 (revision e91117fa8ed2d00ae520454d6fecfce8abae0664)
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 January 29, 2025
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.
248Stack clash protection is also enabled,
249if supported by the compiler for the given architecture.
250In userland, SSP adds a per-process randomized canary at the end of every stack
251frame which is checked for corruption upon return from the function,
252and stack probing in
253.Dv PAGE_SIZE
254chunks.
255In the kernel, a single randomized canary is used globally except on aarch64,
256which has a
257.Dv PERTHREAD_SSP
258.Xr config 8
259option to enable per-thread randomized canaries.
260If stack corruption is detected, then the process aborts to avoid potentially
261malicious execution as a result of the corruption.
262SSP may be enabled or disabled when building
263.Fx
264base with the
265.Xr src.conf 5
266SSP knob.
267.Pp
268When
269.Va WITH_SSP
270is enabled, which is the default, world is built with the
271.Fl fstack-protector-strong
272and
273.Fl fstack-clash-protection
274compiler options.
275The kernel is built with the
276.Fl fstack-protector
277option.
278.Pp
279In addition to SSP, a
280.Dq FORTIFY_SOURCE
281implementation is supported up to level 2 by defining
282.Va _FORTIFY_SOURCE
283to
284.Dv 1
285or
286.Dv 2
287before including any
288.Fx
289headers.
290.Fx
291world builds can set
292.Va FORTIFY_SOURCE
293in the environment or
294.Pa /etc/src-env.conf
295to provide a default value for
296.Va _FORTIFY_SOURCE .
297When enabled,
298.Dq FORTIFY_SOURCE
299enables extra bounds checking in various functions that accept buffers to be
300written into.
301These functions currently have extra bounds checking support:
302.Bl -column -offset indent "snprintf()" "memmove()" "strncpy()" "vsnprintf()" "readlink()"
303.It Fn bcopy Ta Fn bzero Ta Fn fgets Ta Fn getcwd Ta Fn gets
304.It Fn memcpy Ta Fn memmove Ta Fn memset Ta Fn read Ta Fn readlink
305.It Fn snprintf Ta Fn sprintf Ta Fn stpcpy Ta Fn stpncpy Ta Fn strcat
306.It Fn strcpy Ta Fn strncat Ta Fn strncpy Ta Fn vsnprintf Ta Fn vsprintf
307.El
308.Pp
309.Dq FORTIFY_SOURCE
310requires compiler support from
311.Xr clang 1
312or
313.Xr gcc 1 ,
314which provide the
315.Xr __builtin_object_size 3
316function that is used to determine the bounds of an object.
317This feature works best at optimization levels
318.Fl O1
319and above, as some object sizes may be less obvious without some data that the
320compiler would collect in an optimization pass.
321.Pp
322Similar to SSP, violating the bounds of an object will cause the program to
323abort in an effort to avoid malicious execution.
324This effectively provides finer-grained protection than SSP for some class of
325function and system calls, along with some protection for buffers allocated as
326part of the program data.
327.\"
328.Ss Supervisor mode memory protection
329Certain processors include features that prevent unintended access to memory
330pages accessible to userspace (non-privileged) code, while in a privileged
331mode.
332One feature prevents execution, intended to mitigate exploitation of kernel
333vulnerabilities from userland.
334Another feature prevents unintended reads from or writes to user space memory
335from the kernel.
336This also provides effective protection against NULL pointer dereferences from
337kernel.
338.Bl -column -offset indent "Architecture" "Feature" "Access Type Prevented"
339.It Sy Architecture Ta Sy Feature Ta Sy Access Type Prevented
340.It amd64       Ta SMAP  Ta Read / Write
341.It amd64       Ta SMEP  Ta Execute
342.It arm64       Ta PAN   Ta Read / Write
343.It arm64       Ta PXN   Ta Execute
344.It riscv       Ta SUM   Ta Read / Write
345.It riscv       Ta -     Ta Execute
346.El
347.Pp
348These features are automatically used by the kernel.
349There is no user-facing configuration.
350.\"
351.Ss Capsicum
352Capsicum is a lightweight OS capability and sandbox framework.
353See
354.Xr capsicum 4
355for more information.
356.Sh HARDWARE VULNERABILITY MITIGATIONS
357.Ss Firmware and Microcode
358Recent years have seen an unending stream of new hardware vulnerabilities,
359notably CPU ones generally caused by detectable microarchitectural side-effects
360of speculative execution which leak private data from some other thread or
361process or sometimes even internal CPU state that is normally inaccessible.
362Hardware vendors usually address these vulnerabilities as they are discovered by
363releasing microcode updates, which may then be bundled into platform firmware
364updates
365.Pq historically called BIOS updates for PCs
366or packages to be updated by the operating system at boot time.
367.Pp
368Platform firmware updates, if available from the manufacturer,
369are the best defense as they provide coverage during early boot.
370Install them with
371.Pa sysutils/flashrom
372from the
373.Fx
374Ports Collection.
375.Pp
376If platform firmware updates are no longer available,
377packaged microcode is available for installation at
378.Pa sysutils/cpu-microcode
379and can be loaded at runtime using
380.Xr loader.conf 5 ,
381see the package message for more details.
382.Pp
383The best defense overall against hardware vulnerabilities is to timely apply
384these updates when available, as early as possible in the boot process,
385and to disable the affected hardware's problematic functionalities when possible
386(e.g., CPU Simultaneous Multi-Threading).
387Software mitigations are only partial substitutes for these, but they can be
388helpful on out-of-support hardware or as complements for just-discovered
389vulnerabilities not yet addressed by vendors.
390Some software mitigations depend on hardware capabilities provided by a
391microcode update.
392.Ss Architectural Vulnerability Mitigations
393.Fx Ap s
394usual policy is to apply by default all OS-level mitigations that do
395not require recompilation, except those the particular hardware it is running on
396is known not to be vulnerable to
397.Pq which sometimes requires firmware updates ,
398or those that are extremely detrimental to performance in proportion to the
399protection they actually provide.
400OS-level mitigations generally can have noticeable performance impacts on
401specific workloads.
402If your threat model allows it, you may want to try disabling some of them in
403order to possibly get better performance.
404Conversely, minimizing the risks may require you to explicitly enable the most
405expensive ones.
406The description of each vulnerability/mitigation indicates whether it is enabled
407or disabled by default and under which conditions.
408It also lists the knobs to tweak to force a particular status.
409.Ss Zenbleed
410The
411.Dq Zenbleed
412vulnerability exclusively affects AMD processors based on the Zen2
413microarchitecture.
414In contrast with, e.g., Meltdown and the different variants of Spectre, which
415leak data by leaving microarchitectural traces, Zenbleed is a genuine hardware
416bug affecting the CPU's architectural state.
417With particular sequences of instructions whose last ones are mispredicted by
418speculative execution, it is possible to make appear in an XMM register data
419previously put in some XMM register by some preceding or concurrent task
420executing on the same physical core
421.Po disabling Simultaneous Muti-Threading
422.Pq SMT
423is thus not a sufficient protection
424.Pc .
425.Pp
426According to the vulnerability's discoverer, all Zen2-based processors are
427affected
428.Po see
429.Lk https://lock.cmpxchg8b.com/zenbleed.html
430.Pc .
431As of August 2023, AMD has not publicly listed any corresponding errata but has
432issued a security bulletin
433.Pq AMD-SB-7008
434entitled
435.Dq Cross-Process Information Leak
436indicating that platform firmware fixing the vulnerability will be distributed
437to manufacturers no sooner than the end of 2023, except for Rome processors for
438which it is already available.
439No standalone CPU microcodes have been announced so far.
440The only readily-applicable fix mentioned by the discoverer is to set a bit of
441an undocumented MSR, which reportedly completely stops XMM register leaks.
442.Pp
443.Fx
444currently sets this bit by default on all Zen2 processors.
445In the future, it might set it by default only on those Zen2 processors whose
446microcode has not been updated to revisions fixing the vulnerability, once such
447microcode updates have been actually released and community-tested.
448To this mitigation are associated the following knobs:
449.Bl -tag -width indent
450.It Va machdep.mitigations.zenbleed.enable
451A read-write integer tunable and sysctl indicating whether the mitigation should
452be forcibly disabled (0), enabled (1) or if it is left to
453.Fx
454to selectively apply it (2).
455Any other integer value is silently converted to and treated as value 2.
456Note that this setting is silently ignored when running on non-Zen2 processors
457to ease applying a common configuration to heterogeneous machines.
458.It Va machdep.mitigations.zenbleed.state
459A read-only string indicating the current mitigation state.
460It can be either
461.Dq Not applicable ,
462if the processor is not Zen2-based,
463.Dq Mitigation enabled
464or
465.Dq Mitigation disabled .
466This state is automatically updated each time the sysctl
467.Va machdep.mitigations.zenbleed.enable
468is written to.
469Note that it can become inaccurate if the chicken bit is set or cleared
470directly via
471.Xr cpuctl 4
472.Po which includes the
473.Xr cpucontrol 8
474utility
475.Pc .
476.El
477.Pp
478The performance impact and threat models related to these mitigations
479should be considered when configuring and deploying them in a
480.Fx
481system.
482.Pp
483Additional mitigation knobs are listed in the
484.Sx KNOBS AND TWEAKS
485section of
486.Xr security 7 .
487.Sh SEE ALSO
488.Xr elfctl 1 ,
489.Xr proccontrol 1 ,
490.Xr rtld 1 ,
491.Xr mmap 2 ,
492.Xr src.conf 5 ,
493.Xr sysctl.conf 5 ,
494.Xr security 7 ,
495.Xr cpucontrol 8 ,
496.Xr sysctl 8
497