summaryrefslogtreecommitdiffstats
path: root/Documentation/security
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2017-05-13 07:51:41 -0400
committerJonathan Corbet <corbet@lwn.net>2017-05-18 12:30:23 -0400
commitc2ed6743434d1d9ef49b044c6bdfd6ac1ce140a2 (patch)
tree3474f0927898a416ceb19b3ef0a5588ea0b7da11 /Documentation/security
parentaf777cd1b83e95138e7285fde87c795ef0ae7c4d (diff)
doc: ReSTify self-protection.txt
This updates the credentials API documentation to ReST markup and moves it under the security subsection of kernel API documentation. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/security')
-rw-r--r--Documentation/security/index.rst1
-rw-r--r--Documentation/security/self-protection.rst (renamed from Documentation/security/self-protection.txt)99
2 files changed, 64 insertions, 36 deletions
diff --git a/Documentation/security/index.rst b/Documentation/security/index.rst
index 415be8e0b013..4212d7ac58b6 100644
--- a/Documentation/security/index.rst
+++ b/Documentation/security/index.rst
@@ -7,4 +7,5 @@ Security Documentation
7 7
8 credentials 8 credentials
9 IMA-templates 9 IMA-templates
10 self-protection
10 tpm/index 11 tpm/index
diff --git a/Documentation/security/self-protection.txt b/Documentation/security/self-protection.rst
index 141acfebe6ef..60c8bd8b77bf 100644
--- a/Documentation/security/self-protection.txt
+++ b/Documentation/security/self-protection.rst
@@ -1,4 +1,6 @@
1# Kernel Self-Protection 1======================
2Kernel Self-Protection
3======================
2 4
3Kernel self-protection is the design and implementation of systems and 5Kernel self-protection is the design and implementation of systems and
4structures within the Linux kernel to protect against security flaws in 6structures within the Linux kernel to protect against security flaws in
@@ -26,7 +28,8 @@ mentioning them, since these aspects need to be explored, dealt with,
26and/or accepted. 28and/or accepted.
27 29
28 30
29## Attack Surface Reduction 31Attack Surface Reduction
32========================
30 33
31The most fundamental defense against security exploits is to reduce the 34The most fundamental defense against security exploits is to reduce the
32areas of the kernel that can be used to redirect execution. This ranges 35areas of the kernel that can be used to redirect execution. This ranges
@@ -34,13 +37,15 @@ from limiting the exposed APIs available to userspace, making in-kernel
34APIs hard to use incorrectly, minimizing the areas of writable kernel 37APIs hard to use incorrectly, minimizing the areas of writable kernel
35memory, etc. 38memory, etc.
36 39
37### Strict kernel memory permissions 40Strict kernel memory permissions
41--------------------------------
38 42
39When all of kernel memory is writable, it becomes trivial for attacks 43When all of kernel memory is writable, it becomes trivial for attacks
40to redirect execution flow. To reduce the availability of these targets 44to redirect execution flow. To reduce the availability of these targets
41the kernel needs to protect its memory with a tight set of permissions. 45the kernel needs to protect its memory with a tight set of permissions.
42 46
43#### Executable code and read-only data must not be writable 47Executable code and read-only data must not be writable
48~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 49
45Any areas of the kernel with executable memory must not be writable. 50Any areas of the kernel with executable memory must not be writable.
46While this obviously includes the kernel text itself, we must consider 51While this obviously includes the kernel text itself, we must consider
@@ -51,18 +56,19 @@ kernel, they are implemented in a way where the memory is temporarily
51made writable during the update, and then returned to the original 56made writable during the update, and then returned to the original
52permissions.) 57permissions.)
53 58
54In support of this are CONFIG_STRICT_KERNEL_RWX and 59In support of this are ``CONFIG_STRICT_KERNEL_RWX`` and
55CONFIG_STRICT_MODULE_RWX, which seek to make sure that code is not 60``CONFIG_STRICT_MODULE_RWX``, which seek to make sure that code is not
56writable, data is not executable, and read-only data is neither writable 61writable, data is not executable, and read-only data is neither writable
57nor executable. 62nor executable.
58 63
59Most architectures have these options on by default and not user selectable. 64Most architectures have these options on by default and not user selectable.
60For some architectures like arm that wish to have these be selectable, 65For some architectures like arm that wish to have these be selectable,
61the architecture Kconfig can select ARCH_OPTIONAL_KERNEL_RWX to enable 66the architecture Kconfig can select ARCH_OPTIONAL_KERNEL_RWX to enable
62a Kconfig prompt. CONFIG_ARCH_OPTIONAL_KERNEL_RWX_DEFAULT determines 67a Kconfig prompt. ``CONFIG_ARCH_OPTIONAL_KERNEL_RWX_DEFAULT`` determines
63the default setting when ARCH_OPTIONAL_KERNEL_RWX is enabled. 68the default setting when ARCH_OPTIONAL_KERNEL_RWX is enabled.
64 69
65#### Function pointers and sensitive variables must not be writable 70Function pointers and sensitive variables must not be writable
71~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 72
67Vast areas of kernel memory contain function pointers that are looked 73Vast areas of kernel memory contain function pointers that are looked
68up by the kernel and used to continue execution (e.g. descriptor/vector 74up by the kernel and used to continue execution (e.g. descriptor/vector
@@ -74,8 +80,8 @@ so that they live in the .rodata section instead of the .data section
74of the kernel, gaining the protection of the kernel's strict memory 80of the kernel, gaining the protection of the kernel's strict memory
75permissions as described above. 81permissions as described above.
76 82
77For variables that are initialized once at __init time, these can 83For variables that are initialized once at ``__init`` time, these can
78be marked with the (new and under development) __ro_after_init 84be marked with the (new and under development) ``__ro_after_init``
79attribute. 85attribute.
80 86
81What remains are variables that are updated rarely (e.g. GDT). These 87What remains are variables that are updated rarely (e.g. GDT). These
@@ -85,7 +91,8 @@ of their lifetime read-only. (For example, when being updated, only the
85CPU thread performing the update would be given uninterruptible write 91CPU thread performing the update would be given uninterruptible write
86access to the memory.) 92access to the memory.)
87 93
88#### Segregation of kernel memory from userspace memory 94Segregation of kernel memory from userspace memory
95~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 96
90The kernel must never execute userspace memory. The kernel must also never 97The kernel must never execute userspace memory. The kernel must also never
91access userspace memory without explicit expectation to do so. These 98access userspace memory without explicit expectation to do so. These
@@ -95,10 +102,11 @@ By blocking userspace memory in this way, execution and data parsing
95cannot be passed to trivially-controlled userspace memory, forcing 102cannot be passed to trivially-controlled userspace memory, forcing
96attacks to operate entirely in kernel memory. 103attacks to operate entirely in kernel memory.
97 104
98### Reduced access to syscalls 105Reduced access to syscalls
106--------------------------
99 107
100One trivial way to eliminate many syscalls for 64-bit systems is building 108One trivial way to eliminate many syscalls for 64-bit systems is building
101without CONFIG_COMPAT. However, this is rarely a feasible scenario. 109without ``CONFIG_COMPAT``. However, this is rarely a feasible scenario.
102 110
103The "seccomp" system provides an opt-in feature made available to 111The "seccomp" system provides an opt-in feature made available to
104userspace, which provides a way to reduce the number of kernel entry 112userspace, which provides a way to reduce the number of kernel entry
@@ -112,7 +120,8 @@ to trusted processes. This would keep the scope of kernel entry points
112restricted to the more regular set of normally available to unprivileged 120restricted to the more regular set of normally available to unprivileged
113userspace. 121userspace.
114 122
115### Restricting access to kernel modules 123Restricting access to kernel modules
124------------------------------------
116 125
117The kernel should never allow an unprivileged user the ability to 126The kernel should never allow an unprivileged user the ability to
118load specific kernel modules, since that would provide a facility to 127load specific kernel modules, since that would provide a facility to
@@ -127,11 +136,12 @@ for debate in some scenarios.)
127To protect against even privileged users, systems may need to either 136To protect against even privileged users, systems may need to either
128disable module loading entirely (e.g. monolithic kernel builds or 137disable module loading entirely (e.g. monolithic kernel builds or
129modules_disabled sysctl), or provide signed modules (e.g. 138modules_disabled sysctl), or provide signed modules (e.g.
130CONFIG_MODULE_SIG_FORCE, or dm-crypt with LoadPin), to keep from having 139``CONFIG_MODULE_SIG_FORCE``, or dm-crypt with LoadPin), to keep from having
131root load arbitrary kernel code via the module loader interface. 140root load arbitrary kernel code via the module loader interface.
132 141
133 142
134## Memory integrity 143Memory integrity
144================
135 145
136There are many memory structures in the kernel that are regularly abused 146There are many memory structures in the kernel that are regularly abused
137to gain execution control during an attack, By far the most commonly 147to gain execution control during an attack, By far the most commonly
@@ -139,16 +149,18 @@ understood is that of the stack buffer overflow in which the return
139address stored on the stack is overwritten. Many other examples of this 149address stored on the stack is overwritten. Many other examples of this
140kind of attack exist, and protections exist to defend against them. 150kind of attack exist, and protections exist to defend against them.
141 151
142### Stack buffer overflow 152Stack buffer overflow
153---------------------
143 154
144The classic stack buffer overflow involves writing past the expected end 155The classic stack buffer overflow involves writing past the expected end
145of a variable stored on the stack, ultimately writing a controlled value 156of a variable stored on the stack, ultimately writing a controlled value
146to the stack frame's stored return address. The most widely used defense 157to the stack frame's stored return address. The most widely used defense
147is the presence of a stack canary between the stack variables and the 158is the presence of a stack canary between the stack variables and the
148return address (CONFIG_CC_STACKPROTECTOR), which is verified just before 159return address (``CONFIG_CC_STACKPROTECTOR``), which is verified just before
149the function returns. Other defenses include things like shadow stacks. 160the function returns. Other defenses include things like shadow stacks.
150 161
151### Stack depth overflow 162Stack depth overflow
163--------------------
152 164
153A less well understood attack is using a bug that triggers the 165A less well understood attack is using a bug that triggers the
154kernel to consume stack memory with deep function calls or large stack 166kernel to consume stack memory with deep function calls or large stack
@@ -158,27 +170,31 @@ important changes need to be made for better protections: moving the
158sensitive thread_info structure elsewhere, and adding a faulting memory 170sensitive thread_info structure elsewhere, and adding a faulting memory
159hole at the bottom of the stack to catch these overflows. 171hole at the bottom of the stack to catch these overflows.
160 172
161### Heap memory integrity 173Heap memory integrity
174---------------------
162 175
163The structures used to track heap free lists can be sanity-checked during 176The structures used to track heap free lists can be sanity-checked during
164allocation and freeing to make sure they aren't being used to manipulate 177allocation and freeing to make sure they aren't being used to manipulate
165other memory areas. 178other memory areas.
166 179
167### Counter integrity 180Counter integrity
181-----------------
168 182
169Many places in the kernel use atomic counters to track object references 183Many places in the kernel use atomic counters to track object references
170or perform similar lifetime management. When these counters can be made 184or perform similar lifetime management. When these counters can be made
171to wrap (over or under) this traditionally exposes a use-after-free 185to wrap (over or under) this traditionally exposes a use-after-free
172flaw. By trapping atomic wrapping, this class of bug vanishes. 186flaw. By trapping atomic wrapping, this class of bug vanishes.
173 187
174### Size calculation overflow detection 188Size calculation overflow detection
189-----------------------------------
175 190
176Similar to counter overflow, integer overflows (usually size calculations) 191Similar to counter overflow, integer overflows (usually size calculations)
177need to be detected at runtime to kill this class of bug, which 192need to be detected at runtime to kill this class of bug, which
178traditionally leads to being able to write past the end of kernel buffers. 193traditionally leads to being able to write past the end of kernel buffers.
179 194
180 195
181## Statistical defenses 196Probabilistic defenses
197======================
182 198
183While many protections can be considered deterministic (e.g. read-only 199While many protections can be considered deterministic (e.g. read-only
184memory cannot be written to), some protections provide only statistical 200memory cannot be written to), some protections provide only statistical
@@ -186,7 +202,8 @@ defense, in that an attack must gather enough information about a
186running system to overcome the defense. While not perfect, these do 202running system to overcome the defense. While not perfect, these do
187provide meaningful defenses. 203provide meaningful defenses.
188 204
189### Canaries, blinding, and other secrets 205Canaries, blinding, and other secrets
206-------------------------------------
190 207
191It should be noted that things like the stack canary discussed earlier 208It should be noted that things like the stack canary discussed earlier
192are technically statistical defenses, since they rely on a secret value, 209are technically statistical defenses, since they rely on a secret value,
@@ -201,7 +218,8 @@ It is critical that the secret values used must be separate (e.g.
201different canary per stack) and high entropy (e.g. is the RNG actually 218different canary per stack) and high entropy (e.g. is the RNG actually
202working?) in order to maximize their success. 219working?) in order to maximize their success.
203 220
204### Kernel Address Space Layout Randomization (KASLR) 221Kernel Address Space Layout Randomization (KASLR)
222-------------------------------------------------
205 223
206Since the location of kernel memory is almost always instrumental in 224Since the location of kernel memory is almost always instrumental in
207mounting a successful attack, making the location non-deterministic 225mounting a successful attack, making the location non-deterministic
@@ -209,22 +227,25 @@ raises the difficulty of an exploit. (Note that this in turn makes
209the value of information exposures higher, since they may be used to 227the value of information exposures higher, since they may be used to
210discover desired memory locations.) 228discover desired memory locations.)
211 229
212#### Text and module base 230Text and module base
231~~~~~~~~~~~~~~~~~~~~
213 232
214By relocating the physical and virtual base address of the kernel at 233By relocating the physical and virtual base address of the kernel at
215boot-time (CONFIG_RANDOMIZE_BASE), attacks needing kernel code will be 234boot-time (``CONFIG_RANDOMIZE_BASE``), attacks needing kernel code will be
216frustrated. Additionally, offsetting the module loading base address 235frustrated. Additionally, offsetting the module loading base address
217means that even systems that load the same set of modules in the same 236means that even systems that load the same set of modules in the same
218order every boot will not share a common base address with the rest of 237order every boot will not share a common base address with the rest of
219the kernel text. 238the kernel text.
220 239
221#### Stack base 240Stack base
241~~~~~~~~~~
222 242
223If the base address of the kernel stack is not the same between processes, 243If the base address of the kernel stack is not the same between processes,
224or even not the same between syscalls, targets on or beyond the stack 244or even not the same between syscalls, targets on or beyond the stack
225become more difficult to locate. 245become more difficult to locate.
226 246
227#### Dynamic memory base 247Dynamic memory base
248~~~~~~~~~~~~~~~~~~~
228 249
229Much of the kernel's dynamic memory (e.g. kmalloc, vmalloc, etc) ends up 250Much of the kernel's dynamic memory (e.g. kmalloc, vmalloc, etc) ends up
230being relatively deterministic in layout due to the order of early-boot 251being relatively deterministic in layout due to the order of early-boot
@@ -232,7 +253,8 @@ initializations. If the base address of these areas is not the same
232between boots, targeting them is frustrated, requiring an information 253between boots, targeting them is frustrated, requiring an information
233exposure specific to the region. 254exposure specific to the region.
234 255
235#### Structure layout 256Structure layout
257~~~~~~~~~~~~~~~~
236 258
237By performing a per-build randomization of the layout of sensitive 259By performing a per-build randomization of the layout of sensitive
238structures, attacks must either be tuned to known kernel builds or expose 260structures, attacks must either be tuned to known kernel builds or expose
@@ -240,26 +262,30 @@ enough kernel memory to determine structure layouts before manipulating
240them. 262them.
241 263
242 264
243## Preventing Information Exposures 265Preventing Information Exposures
266================================
244 267
245Since the locations of sensitive structures are the primary target for 268Since the locations of sensitive structures are the primary target for
246attacks, it is important to defend against exposure of both kernel memory 269attacks, it is important to defend against exposure of both kernel memory
247addresses and kernel memory contents (since they may contain kernel 270addresses and kernel memory contents (since they may contain kernel
248addresses or other sensitive things like canary values). 271addresses or other sensitive things like canary values).
249 272
250### Unique identifiers 273Unique identifiers
274------------------
251 275
252Kernel memory addresses must never be used as identifiers exposed to 276Kernel memory addresses must never be used as identifiers exposed to
253userspace. Instead, use an atomic counter, an idr, or similar unique 277userspace. Instead, use an atomic counter, an idr, or similar unique
254identifier. 278identifier.
255 279
256### Memory initialization 280Memory initialization
281---------------------
257 282
258Memory copied to userspace must always be fully initialized. If not 283Memory copied to userspace must always be fully initialized. If not
259explicitly memset(), this will require changes to the compiler to make 284explicitly memset(), this will require changes to the compiler to make
260sure structure holes are cleared. 285sure structure holes are cleared.
261 286
262### Memory poisoning 287Memory poisoning
288----------------
263 289
264When releasing memory, it is best to poison the contents (clear stack on 290When releasing memory, it is best to poison the contents (clear stack on
265syscall return, wipe heap memory on a free), to avoid reuse attacks that 291syscall return, wipe heap memory on a free), to avoid reuse attacks that
@@ -267,9 +293,10 @@ rely on the old contents of memory. This frustrates many uninitialized
267variable attacks, stack content exposures, heap content exposures, and 293variable attacks, stack content exposures, heap content exposures, and
268use-after-free attacks. 294use-after-free attacks.
269 295
270### Destination tracking 296Destination tracking
297--------------------
271 298
272To help kill classes of bugs that result in kernel addresses being 299To help kill classes of bugs that result in kernel addresses being
273written to userspace, the destination of writes needs to be tracked. If 300written to userspace, the destination of writes needs to be tracked. If
274the buffer is destined for userspace (e.g. seq_file backed /proc files), 301the buffer is destined for userspace (e.g. seq_file backed ``/proc`` files),
275it should automatically censor sensitive values. 302it should automatically censor sensitive values.