Skip to content

Commit c2ed674

Browse files
keesJonathan Corbet
authored andcommitted
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>
1 parent af777cd commit c2ed674

2 files changed

Lines changed: 64 additions & 36 deletions

File tree

‎Documentation/security/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Security Documentation
77

88
credentials
99
IMA-templates
10+
self-protection
1011
tpm/index

Documentation/security/self-protection.txt renamed to Documentation/security/self-protection.rst

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Kernel Self-Protection
1+
======================
2+
Kernel Self-Protection
3+
======================
24

35
Kernel self-protection is the design and implementation of systems and
46
structures within the Linux kernel to protect against security flaws in
@@ -26,21 +28,24 @@ mentioning them, since these aspects need to be explored, dealt with,
2628
and/or accepted.
2729

2830

29-
## Attack Surface Reduction
31+
Attack Surface Reduction
32+
========================
3033

3134
The most fundamental defense against security exploits is to reduce the
3235
areas of the kernel that can be used to redirect execution. This ranges
3336
from limiting the exposed APIs available to userspace, making in-kernel
3437
APIs hard to use incorrectly, minimizing the areas of writable kernel
3538
memory, etc.
3639

37-
### Strict kernel memory permissions
40+
Strict kernel memory permissions
41+
--------------------------------
3842

3943
When all of kernel memory is writable, it becomes trivial for attacks
4044
to redirect execution flow. To reduce the availability of these targets
4145
the kernel needs to protect its memory with a tight set of permissions.
4246

43-
#### Executable code and read-only data must not be writable
47+
Executable code and read-only data must not be writable
48+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4449

4550
Any areas of the kernel with executable memory must not be writable.
4651
While 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
5156
made writable during the update, and then returned to the original
5257
permissions.)
5358

54-
In support of this are CONFIG_STRICT_KERNEL_RWX and
55-
CONFIG_STRICT_MODULE_RWX, which seek to make sure that code is not
59+
In support of this are ``CONFIG_STRICT_KERNEL_RWX`` and
60+
``CONFIG_STRICT_MODULE_RWX``, which seek to make sure that code is not
5661
writable, data is not executable, and read-only data is neither writable
5762
nor executable.
5863

5964
Most architectures have these options on by default and not user selectable.
6065
For some architectures like arm that wish to have these be selectable,
6166
the architecture Kconfig can select ARCH_OPTIONAL_KERNEL_RWX to enable
62-
a Kconfig prompt. CONFIG_ARCH_OPTIONAL_KERNEL_RWX_DEFAULT determines
67+
a Kconfig prompt. ``CONFIG_ARCH_OPTIONAL_KERNEL_RWX_DEFAULT`` determines
6368
the default setting when ARCH_OPTIONAL_KERNEL_RWX is enabled.
6469

65-
#### Function pointers and sensitive variables must not be writable
70+
Function pointers and sensitive variables must not be writable
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6672

6773
Vast areas of kernel memory contain function pointers that are looked
6874
up 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
7480
of the kernel, gaining the protection of the kernel's strict memory
7581
permissions as described above.
7682

77-
For variables that are initialized once at __init time, these can
78-
be marked with the (new and under development) __ro_after_init
83+
For variables that are initialized once at ``__init`` time, these can
84+
be marked with the (new and under development) ``__ro_after_init``
7985
attribute.
8086

8187
What 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
8591
CPU thread performing the update would be given uninterruptible write
8692
access to the memory.)
8793

88-
#### Segregation of kernel memory from userspace memory
94+
Segregation of kernel memory from userspace memory
95+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8996

9097
The kernel must never execute userspace memory. The kernel must also never
9198
access userspace memory without explicit expectation to do so. These
@@ -95,10 +102,11 @@ By blocking userspace memory in this way, execution and data parsing
95102
cannot be passed to trivially-controlled userspace memory, forcing
96103
attacks to operate entirely in kernel memory.
97104

98-
### Reduced access to syscalls
105+
Reduced access to syscalls
106+
--------------------------
99107

100108
One trivial way to eliminate many syscalls for 64-bit systems is building
101-
without CONFIG_COMPAT. However, this is rarely a feasible scenario.
109+
without ``CONFIG_COMPAT``. However, this is rarely a feasible scenario.
102110

103111
The "seccomp" system provides an opt-in feature made available to
104112
userspace, 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
112120
restricted to the more regular set of normally available to unprivileged
113121
userspace.
114122

115-
### Restricting access to kernel modules
123+
Restricting access to kernel modules
124+
------------------------------------
116125

117126
The kernel should never allow an unprivileged user the ability to
118127
load specific kernel modules, since that would provide a facility to
@@ -127,28 +136,31 @@ for debate in some scenarios.)
127136
To protect against even privileged users, systems may need to either
128137
disable module loading entirely (e.g. monolithic kernel builds or
129138
modules_disabled sysctl), or provide signed modules (e.g.
130-
CONFIG_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
131140
root load arbitrary kernel code via the module loader interface.
132141

133142

134-
## Memory integrity
143+
Memory integrity
144+
================
135145

136146
There are many memory structures in the kernel that are regularly abused
137147
to gain execution control during an attack, By far the most commonly
138148
understood is that of the stack buffer overflow in which the return
139149
address stored on the stack is overwritten. Many other examples of this
140150
kind of attack exist, and protections exist to defend against them.
141151

142-
### Stack buffer overflow
152+
Stack buffer overflow
153+
---------------------
143154

144155
The classic stack buffer overflow involves writing past the expected end
145156
of a variable stored on the stack, ultimately writing a controlled value
146157
to the stack frame's stored return address. The most widely used defense
147158
is the presence of a stack canary between the stack variables and the
148-
return address (CONFIG_CC_STACKPROTECTOR), which is verified just before
159+
return address (``CONFIG_CC_STACKPROTECTOR``), which is verified just before
149160
the function returns. Other defenses include things like shadow stacks.
150161

151-
### Stack depth overflow
162+
Stack depth overflow
163+
--------------------
152164

153165
A less well understood attack is using a bug that triggers the
154166
kernel to consume stack memory with deep function calls or large stack
@@ -158,35 +170,40 @@ important changes need to be made for better protections: moving the
158170
sensitive thread_info structure elsewhere, and adding a faulting memory
159171
hole at the bottom of the stack to catch these overflows.
160172

161-
### Heap memory integrity
173+
Heap memory integrity
174+
---------------------
162175

163176
The structures used to track heap free lists can be sanity-checked during
164177
allocation and freeing to make sure they aren't being used to manipulate
165178
other memory areas.
166179

167-
### Counter integrity
180+
Counter integrity
181+
-----------------
168182

169183
Many places in the kernel use atomic counters to track object references
170184
or perform similar lifetime management. When these counters can be made
171185
to wrap (over or under) this traditionally exposes a use-after-free
172186
flaw. By trapping atomic wrapping, this class of bug vanishes.
173187

174-
### Size calculation overflow detection
188+
Size calculation overflow detection
189+
-----------------------------------
175190

176191
Similar to counter overflow, integer overflows (usually size calculations)
177192
need to be detected at runtime to kill this class of bug, which
178193
traditionally leads to being able to write past the end of kernel buffers.
179194

180195

181-
## Statistical defenses
196+
Probabilistic defenses
197+
======================
182198

183199
While many protections can be considered deterministic (e.g. read-only
184200
memory cannot be written to), some protections provide only statistical
185201
defense, in that an attack must gather enough information about a
186202
running system to overcome the defense. While not perfect, these do
187203
provide meaningful defenses.
188204

189-
### Canaries, blinding, and other secrets
205+
Canaries, blinding, and other secrets
206+
-------------------------------------
190207

191208
It should be noted that things like the stack canary discussed earlier
192209
are technically statistical defenses, since they rely on a secret value,
@@ -201,75 +218,85 @@ It is critical that the secret values used must be separate (e.g.
201218
different canary per stack) and high entropy (e.g. is the RNG actually
202219
working?) in order to maximize their success.
203220

204-
### Kernel Address Space Layout Randomization (KASLR)
221+
Kernel Address Space Layout Randomization (KASLR)
222+
-------------------------------------------------
205223

206224
Since the location of kernel memory is almost always instrumental in
207225
mounting a successful attack, making the location non-deterministic
208226
raises the difficulty of an exploit. (Note that this in turn makes
209227
the value of information exposures higher, since they may be used to
210228
discover desired memory locations.)
211229

212-
#### Text and module base
230+
Text and module base
231+
~~~~~~~~~~~~~~~~~~~~
213232

214233
By relocating the physical and virtual base address of the kernel at
215-
boot-time (CONFIG_RANDOMIZE_BASE), attacks needing kernel code will be
234+
boot-time (``CONFIG_RANDOMIZE_BASE``), attacks needing kernel code will be
216235
frustrated. Additionally, offsetting the module loading base address
217236
means that even systems that load the same set of modules in the same
218237
order every boot will not share a common base address with the rest of
219238
the kernel text.
220239

221-
#### Stack base
240+
Stack base
241+
~~~~~~~~~~
222242

223243
If the base address of the kernel stack is not the same between processes,
224244
or even not the same between syscalls, targets on or beyond the stack
225245
become more difficult to locate.
226246

227-
#### Dynamic memory base
247+
Dynamic memory base
248+
~~~~~~~~~~~~~~~~~~~
228249

229250
Much of the kernel's dynamic memory (e.g. kmalloc, vmalloc, etc) ends up
230251
being relatively deterministic in layout due to the order of early-boot
231252
initializations. If the base address of these areas is not the same
232253
between boots, targeting them is frustrated, requiring an information
233254
exposure specific to the region.
234255

235-
#### Structure layout
256+
Structure layout
257+
~~~~~~~~~~~~~~~~
236258

237259
By performing a per-build randomization of the layout of sensitive
238260
structures, attacks must either be tuned to known kernel builds or expose
239261
enough kernel memory to determine structure layouts before manipulating
240262
them.
241263

242264

243-
## Preventing Information Exposures
265+
Preventing Information Exposures
266+
================================
244267

245268
Since the locations of sensitive structures are the primary target for
246269
attacks, it is important to defend against exposure of both kernel memory
247270
addresses and kernel memory contents (since they may contain kernel
248271
addresses or other sensitive things like canary values).
249272

250-
### Unique identifiers
273+
Unique identifiers
274+
------------------
251275

252276
Kernel memory addresses must never be used as identifiers exposed to
253277
userspace. Instead, use an atomic counter, an idr, or similar unique
254278
identifier.
255279

256-
### Memory initialization
280+
Memory initialization
281+
---------------------
257282

258283
Memory copied to userspace must always be fully initialized. If not
259284
explicitly memset(), this will require changes to the compiler to make
260285
sure structure holes are cleared.
261286

262-
### Memory poisoning
287+
Memory poisoning
288+
----------------
263289

264290
When releasing memory, it is best to poison the contents (clear stack on
265291
syscall return, wipe heap memory on a free), to avoid reuse attacks that
266292
rely on the old contents of memory. This frustrates many uninitialized
267293
variable attacks, stack content exposures, heap content exposures, and
268294
use-after-free attacks.
269295

270-
### Destination tracking
296+
Destination tracking
297+
--------------------
271298

272299
To help kill classes of bugs that result in kernel addresses being
273300
written to userspace, the destination of writes needs to be tracked. If
274-
the buffer is destined for userspace (e.g. seq_file backed /proc files),
301+
the buffer is destined for userspace (e.g. seq_file backed ``/proc`` files),
275302
it should automatically censor sensitive values.

0 commit comments

Comments
 (0)