1- # Kernel Self-Protection
1+ ======================
2+ Kernel Self-Protection
3+ ======================
24
35Kernel self-protection is the design and implementation of systems and
46structures 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,
2628and/or accepted.
2729
2830
29- ## Attack Surface Reduction
31+ Attack Surface Reduction
32+ ========================
3033
3134The most fundamental defense against security exploits is to reduce the
3235areas of the kernel that can be used to redirect execution. This ranges
3336from limiting the exposed APIs available to userspace, making in-kernel
3437APIs hard to use incorrectly, minimizing the areas of writable kernel
3538memory, etc.
3639
37- ### Strict kernel memory permissions
40+ Strict kernel memory permissions
41+ --------------------------------
3842
3943When all of kernel memory is writable, it becomes trivial for attacks
4044to redirect execution flow. To reduce the availability of these targets
4145the 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
4550Any areas of the kernel with executable memory must not be writable.
4651While 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
5156made writable during the update, and then returned to the original
5257permissions.)
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
5661writable, data is not executable, and read-only data is neither writable
5762nor executable.
5863
5964Most architectures have these options on by default and not user selectable.
6065For some architectures like arm that wish to have these be selectable,
6166the 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
6368the 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
6773Vast areas of kernel memory contain function pointers that are looked
6874up 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
7480of the kernel, gaining the protection of the kernel's strict memory
7581permissions 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 ``
7985attribute.
8086
8187What 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
8591CPU thread performing the update would be given uninterruptible write
8692access to the memory.)
8793
88- #### Segregation of kernel memory from userspace memory
94+ Segregation of kernel memory from userspace memory
95+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8996
9097The kernel must never execute userspace memory. The kernel must also never
9198access userspace memory without explicit expectation to do so. These
@@ -95,10 +102,11 @@ By blocking userspace memory in this way, execution and data parsing
95102cannot be passed to trivially-controlled userspace memory, forcing
96103attacks to operate entirely in kernel memory.
97104
98- ### Reduced access to syscalls
105+ Reduced access to syscalls
106+ --------------------------
99107
100108One 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
103111The "seccomp" system provides an opt-in feature made available to
104112userspace, 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
112120restricted to the more regular set of normally available to unprivileged
113121userspace.
114122
115- ### Restricting access to kernel modules
123+ Restricting access to kernel modules
124+ ------------------------------------
116125
117126The kernel should never allow an unprivileged user the ability to
118127load specific kernel modules, since that would provide a facility to
@@ -127,28 +136,31 @@ for debate in some scenarios.)
127136To protect against even privileged users, systems may need to either
128137disable module loading entirely (e.g. monolithic kernel builds or
129138modules_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
131140root load arbitrary kernel code via the module loader interface.
132141
133142
134- ## Memory integrity
143+ Memory integrity
144+ ================
135145
136146There are many memory structures in the kernel that are regularly abused
137147to gain execution control during an attack, By far the most commonly
138148understood is that of the stack buffer overflow in which the return
139149address stored on the stack is overwritten. Many other examples of this
140150kind of attack exist, and protections exist to defend against them.
141151
142- ### Stack buffer overflow
152+ Stack buffer overflow
153+ ---------------------
143154
144155The classic stack buffer overflow involves writing past the expected end
145156of a variable stored on the stack, ultimately writing a controlled value
146157to the stack frame's stored return address. The most widely used defense
147158is 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
149160the function returns. Other defenses include things like shadow stacks.
150161
151- ### Stack depth overflow
162+ Stack depth overflow
163+ --------------------
152164
153165A less well understood attack is using a bug that triggers the
154166kernel 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
158170sensitive thread_info structure elsewhere, and adding a faulting memory
159171hole at the bottom of the stack to catch these overflows.
160172
161- ### Heap memory integrity
173+ Heap memory integrity
174+ ---------------------
162175
163176The structures used to track heap free lists can be sanity-checked during
164177allocation and freeing to make sure they aren't being used to manipulate
165178other memory areas.
166179
167- ### Counter integrity
180+ Counter integrity
181+ -----------------
168182
169183Many places in the kernel use atomic counters to track object references
170184or perform similar lifetime management. When these counters can be made
171185to wrap (over or under) this traditionally exposes a use-after-free
172186flaw. By trapping atomic wrapping, this class of bug vanishes.
173187
174- ### Size calculation overflow detection
188+ Size calculation overflow detection
189+ -----------------------------------
175190
176191Similar to counter overflow, integer overflows (usually size calculations)
177192need to be detected at runtime to kill this class of bug, which
178193traditionally leads to being able to write past the end of kernel buffers.
179194
180195
181- ## Statistical defenses
196+ Probabilistic defenses
197+ ======================
182198
183199While many protections can be considered deterministic (e.g. read-only
184200memory cannot be written to), some protections provide only statistical
185201defense, in that an attack must gather enough information about a
186202running system to overcome the defense. While not perfect, these do
187203provide meaningful defenses.
188204
189- ### Canaries, blinding, and other secrets
205+ Canaries, blinding, and other secrets
206+ -------------------------------------
190207
191208It should be noted that things like the stack canary discussed earlier
192209are 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.
201218different canary per stack) and high entropy (e.g. is the RNG actually
202219working?) in order to maximize their success.
203220
204- ### Kernel Address Space Layout Randomization (KASLR)
221+ Kernel Address Space Layout Randomization (KASLR)
222+ -------------------------------------------------
205223
206224Since the location of kernel memory is almost always instrumental in
207225mounting a successful attack, making the location non-deterministic
208226raises the difficulty of an exploit. (Note that this in turn makes
209227the value of information exposures higher, since they may be used to
210228discover desired memory locations.)
211229
212- #### Text and module base
230+ Text and module base
231+ ~~~~~~~~~~~~~~~~~~~~
213232
214233By 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
216235frustrated. Additionally, offsetting the module loading base address
217236means that even systems that load the same set of modules in the same
218237order every boot will not share a common base address with the rest of
219238the kernel text.
220239
221- #### Stack base
240+ Stack base
241+ ~~~~~~~~~~
222242
223243If the base address of the kernel stack is not the same between processes,
224244or even not the same between syscalls, targets on or beyond the stack
225245become more difficult to locate.
226246
227- #### Dynamic memory base
247+ Dynamic memory base
248+ ~~~~~~~~~~~~~~~~~~~
228249
229250Much of the kernel's dynamic memory (e.g. kmalloc, vmalloc, etc) ends up
230251being relatively deterministic in layout due to the order of early-boot
231252initializations. If the base address of these areas is not the same
232253between boots, targeting them is frustrated, requiring an information
233254exposure specific to the region.
234255
235- #### Structure layout
256+ Structure layout
257+ ~~~~~~~~~~~~~~~~
236258
237259By performing a per-build randomization of the layout of sensitive
238260structures, attacks must either be tuned to known kernel builds or expose
239261enough kernel memory to determine structure layouts before manipulating
240262them.
241263
242264
243- ## Preventing Information Exposures
265+ Preventing Information Exposures
266+ ================================
244267
245268Since the locations of sensitive structures are the primary target for
246269attacks, it is important to defend against exposure of both kernel memory
247270addresses and kernel memory contents (since they may contain kernel
248271addresses or other sensitive things like canary values).
249272
250- ### Unique identifiers
273+ Unique identifiers
274+ ------------------
251275
252276Kernel memory addresses must never be used as identifiers exposed to
253277userspace. Instead, use an atomic counter, an idr, or similar unique
254278identifier.
255279
256- ### Memory initialization
280+ Memory initialization
281+ ---------------------
257282
258283Memory copied to userspace must always be fully initialized. If not
259284explicitly memset(), this will require changes to the compiler to make
260285sure structure holes are cleared.
261286
262- ### Memory poisoning
287+ Memory poisoning
288+ ----------------
263289
264290When releasing memory, it is best to poison the contents (clear stack on
265291syscall return, wipe heap memory on a free), to avoid reuse attacks that
266292rely on the old contents of memory. This frustrates many uninitialized
267293variable attacks, stack content exposures, heap content exposures, and
268294use-after-free attacks.
269295
270- ### Destination tracking
296+ Destination tracking
297+ --------------------
271298
272299To help kill classes of bugs that result in kernel addresses being
273300written 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),
275302it should automatically censor sensitive values.
0 commit comments