Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | .. Copyright (C) 2020, Google LLC. |
| 3 | |
| 4 | Kernel Electric-Fence (KFENCE) |
| 5 | ============================== |
| 6 | |
| 7 | Kernel Electric-Fence (KFENCE) is a low-overhead sampling-based memory safety |
| 8 | error detector. KFENCE detects heap out-of-bounds access, use-after-free, and |
| 9 | invalid-free errors. |
| 10 | |
| 11 | KFENCE is designed to be enabled in production kernels, and has near zero |
| 12 | performance overhead. Compared to KASAN, KFENCE trades performance for |
| 13 | precision. The main motivation behind KFENCE's design, is that with enough |
| 14 | total uptime KFENCE will detect bugs in code paths not typically exercised by |
| 15 | non-production test workloads. One way to quickly achieve a large enough total |
| 16 | uptime is when the tool is deployed across a large fleet of machines. |
| 17 | |
| 18 | Usage |
| 19 | ----- |
| 20 | |
| 21 | To enable KFENCE, configure the kernel with:: |
| 22 | |
| 23 | CONFIG_KFENCE=y |
| 24 | |
| 25 | To build a kernel with KFENCE support, but disabled by default (to enable, set |
| 26 | ``kfence.sample_interval`` to non-zero value), configure the kernel with:: |
| 27 | |
| 28 | CONFIG_KFENCE=y |
| 29 | CONFIG_KFENCE_SAMPLE_INTERVAL=0 |
| 30 | |
| 31 | KFENCE provides several other configuration options to customize behaviour (see |
| 32 | the respective help text in ``lib/Kconfig.kfence`` for more info). |
| 33 | |
| 34 | Tuning performance |
| 35 | ~~~~~~~~~~~~~~~~~~ |
| 36 | |
| 37 | The most important parameter is KFENCE's sample interval, which can be set via |
| 38 | the kernel boot parameter ``kfence.sample_interval`` in milliseconds. The |
| 39 | sample interval determines the frequency with which heap allocations will be |
| 40 | guarded by KFENCE. The default is configurable via the Kconfig option |
| 41 | ``CONFIG_KFENCE_SAMPLE_INTERVAL``. Setting ``kfence.sample_interval=0`` |
| 42 | disables KFENCE. |
| 43 | |
| 44 | The KFENCE memory pool is of fixed size, and if the pool is exhausted, no |
| 45 | further KFENCE allocations occur. With ``CONFIG_KFENCE_NUM_OBJECTS`` (default |
| 46 | 255), the number of available guarded objects can be controlled. Each object |
| 47 | requires 2 pages, one for the object itself and the other one used as a guard |
| 48 | page; object pages are interleaved with guard pages, and every object page is |
| 49 | therefore surrounded by two guard pages. |
| 50 | |
| 51 | The total memory dedicated to the KFENCE memory pool can be computed as:: |
| 52 | |
| 53 | ( #objects + 1 ) * 2 * PAGE_SIZE |
| 54 | |
| 55 | Using the default config, and assuming a page size of 4 KiB, results in |
| 56 | dedicating 2 MiB to the KFENCE memory pool. |
| 57 | |
| 58 | Note: On architectures that support huge pages, KFENCE will ensure that the |
| 59 | pool is using pages of size ``PAGE_SIZE``. This will result in additional page |
| 60 | tables being allocated. |
| 61 | |
| 62 | Error reports |
| 63 | ~~~~~~~~~~~~~ |
| 64 | |
| 65 | A typical out-of-bounds access looks like this:: |
| 66 | |
| 67 | ================================================================== |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 68 | BUG: KFENCE: out-of-bounds read in test_out_of_bounds_read+0xa6/0x234 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 69 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 70 | Out-of-bounds read at 0xffff8c3f2e291fff (1B left of kfence-#72): |
| 71 | test_out_of_bounds_read+0xa6/0x234 |
| 72 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 73 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 74 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 75 | ret_from_fork+0x22/0x30 |
| 76 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 77 | kfence-#72: 0xffff8c3f2e292000-0xffff8c3f2e29201f, size=32, cache=kmalloc-32 |
| 78 | |
| 79 | allocated by task 484 on cpu 0 at 32.919330s: |
| 80 | test_alloc+0xfe/0x738 |
| 81 | test_out_of_bounds_read+0x9b/0x234 |
| 82 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 83 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 84 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 85 | ret_from_fork+0x22/0x30 |
| 86 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 87 | CPU: 0 PID: 484 Comm: kunit_try_catch Not tainted 5.13.0-rc3+ #7 |
| 88 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 89 | ================================================================== |
| 90 | |
| 91 | The header of the report provides a short summary of the function involved in |
| 92 | the access. It is followed by more detailed information about the access and |
Marco Elver | 35beccf | 2021-02-25 17:19:40 -0800 | [diff] [blame] | 93 | its origin. Note that, real kernel addresses are only shown when using the |
| 94 | kernel command line option ``no_hash_pointers``. |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 95 | |
| 96 | Use-after-free accesses are reported as:: |
| 97 | |
| 98 | ================================================================== |
Marco Elver | bc8fbc5 | 2021-02-25 17:19:31 -0800 | [diff] [blame] | 99 | BUG: KFENCE: use-after-free read in test_use_after_free_read+0xb3/0x143 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 100 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 101 | Use-after-free read at 0xffff8c3f2e2a0000 (in kfence-#79): |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 102 | test_use_after_free_read+0xb3/0x143 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 103 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 104 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 105 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 106 | ret_from_fork+0x22/0x30 |
| 107 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 108 | kfence-#79: 0xffff8c3f2e2a0000-0xffff8c3f2e2a001f, size=32, cache=kmalloc-32 |
| 109 | |
| 110 | allocated by task 488 on cpu 2 at 33.871326s: |
| 111 | test_alloc+0xfe/0x738 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 112 | test_use_after_free_read+0x76/0x143 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 113 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 114 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 115 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 116 | ret_from_fork+0x22/0x30 |
| 117 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 118 | freed by task 488 on cpu 2 at 33.871358s: |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 119 | test_use_after_free_read+0xa8/0x143 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 120 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 121 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 122 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 123 | ret_from_fork+0x22/0x30 |
| 124 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 125 | CPU: 2 PID: 488 Comm: kunit_try_catch Tainted: G B 5.13.0-rc3+ #7 |
| 126 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 127 | ================================================================== |
| 128 | |
| 129 | KFENCE also reports on invalid frees, such as double-frees:: |
| 130 | |
| 131 | ================================================================== |
| 132 | BUG: KFENCE: invalid free in test_double_free+0xdc/0x171 |
| 133 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 134 | Invalid free of 0xffff8c3f2e2a4000 (in kfence-#81): |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 135 | test_double_free+0xdc/0x171 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 136 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 137 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 138 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 139 | ret_from_fork+0x22/0x30 |
| 140 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 141 | kfence-#81: 0xffff8c3f2e2a4000-0xffff8c3f2e2a401f, size=32, cache=kmalloc-32 |
| 142 | |
| 143 | allocated by task 490 on cpu 1 at 34.175321s: |
| 144 | test_alloc+0xfe/0x738 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 145 | test_double_free+0x76/0x171 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 146 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 147 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 148 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 149 | ret_from_fork+0x22/0x30 |
| 150 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 151 | freed by task 490 on cpu 1 at 34.175348s: |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 152 | test_double_free+0xa8/0x171 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 153 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 154 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 155 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 156 | ret_from_fork+0x22/0x30 |
| 157 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 158 | CPU: 1 PID: 490 Comm: kunit_try_catch Tainted: G B 5.13.0-rc3+ #7 |
| 159 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 160 | ================================================================== |
| 161 | |
| 162 | KFENCE also uses pattern-based redzones on the other side of an object's guard |
| 163 | page, to detect out-of-bounds writes on the unprotected side of the object. |
| 164 | These are reported on frees:: |
| 165 | |
| 166 | ================================================================== |
| 167 | BUG: KFENCE: memory corruption in test_kmalloc_aligned_oob_write+0xef/0x184 |
| 168 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 169 | Corrupted memory at 0xffff8c3f2e33aff9 [ 0xac . . . . . . ] (in kfence-#156): |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 170 | test_kmalloc_aligned_oob_write+0xef/0x184 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 171 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 172 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 173 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 174 | ret_from_fork+0x22/0x30 |
| 175 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 176 | kfence-#156: 0xffff8c3f2e33afb0-0xffff8c3f2e33aff8, size=73, cache=kmalloc-96 |
| 177 | |
| 178 | allocated by task 502 on cpu 7 at 42.159302s: |
| 179 | test_alloc+0xfe/0x738 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 180 | test_kmalloc_aligned_oob_write+0x57/0x184 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 181 | kunit_try_run_case+0x61/0xa0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 182 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 183 | kthread+0x176/0x1b0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 184 | ret_from_fork+0x22/0x30 |
| 185 | |
Marco Elver | 4bbf04a | 2021-09-07 19:56:21 -0700 | [diff] [blame] | 186 | CPU: 7 PID: 502 Comm: kunit_try_catch Tainted: G B 5.13.0-rc3+ #7 |
| 187 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 188 | ================================================================== |
| 189 | |
| 190 | For such errors, the address where the corruption occurred as well as the |
| 191 | invalidly written bytes (offset from the address) are shown; in this |
| 192 | representation, '.' denote untouched bytes. In the example above ``0xac`` is |
| 193 | the value written to the invalid address at offset 0, and the remaining '.' |
| 194 | denote that no following bytes have been touched. Note that, real values are |
Marco Elver | 35beccf | 2021-02-25 17:19:40 -0800 | [diff] [blame] | 195 | only shown if the kernel was booted with ``no_hash_pointers``; to avoid |
| 196 | information disclosure otherwise, '!' is used instead to denote invalidly |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 197 | written bytes. |
| 198 | |
| 199 | And finally, KFENCE may also report on invalid accesses to any protected page |
| 200 | where it was not possible to determine an associated object, e.g. if adjacent |
| 201 | object pages had not yet been allocated:: |
| 202 | |
| 203 | ================================================================== |
Marco Elver | bc8fbc5 | 2021-02-25 17:19:31 -0800 | [diff] [blame] | 204 | BUG: KFENCE: invalid read in test_invalid_access+0x26/0xe0 |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 205 | |
Marco Elver | bc8fbc5 | 2021-02-25 17:19:31 -0800 | [diff] [blame] | 206 | Invalid read at 0xffffffffb670b00a: |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 207 | test_invalid_access+0x26/0xe0 |
| 208 | kunit_try_run_case+0x51/0x85 |
| 209 | kunit_generic_run_threadfn_adapter+0x16/0x30 |
| 210 | kthread+0x137/0x160 |
| 211 | ret_from_fork+0x22/0x30 |
| 212 | |
| 213 | CPU: 4 PID: 124 Comm: kunit_try_catch Tainted: G W 5.8.0-rc6+ #7 |
| 214 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1 04/01/2014 |
| 215 | ================================================================== |
| 216 | |
| 217 | DebugFS interface |
| 218 | ~~~~~~~~~~~~~~~~~ |
| 219 | |
| 220 | Some debugging information is exposed via debugfs: |
| 221 | |
| 222 | * The file ``/sys/kernel/debug/kfence/stats`` provides runtime statistics. |
| 223 | |
| 224 | * The file ``/sys/kernel/debug/kfence/objects`` provides a list of objects |
| 225 | allocated via KFENCE, including those already freed but protected. |
| 226 | |
| 227 | Implementation Details |
| 228 | ---------------------- |
| 229 | |
| 230 | Guarded allocations are set up based on the sample interval. After expiration |
| 231 | of the sample interval, the next allocation through the main allocator (SLAB or |
| 232 | SLUB) returns a guarded allocation from the KFENCE object pool (allocation |
| 233 | sizes up to PAGE_SIZE are supported). At this point, the timer is reset, and |
Marco Elver | 4f612ed | 2021-11-05 13:45:49 -0700 | [diff] [blame] | 234 | the next allocation is set up after the expiration of the interval. |
| 235 | |
| 236 | When using ``CONFIG_KFENCE_STATIC_KEYS=y``, KFENCE allocations are "gated" |
| 237 | through the main allocator's fast-path by relying on static branches via the |
| 238 | static keys infrastructure. The static branch is toggled to redirect the |
| 239 | allocation to KFENCE. Depending on sample interval, target workloads, and |
| 240 | system architecture, this may perform better than the simple dynamic branch. |
| 241 | Careful benchmarking is recommended. |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 242 | |
| 243 | KFENCE objects each reside on a dedicated page, at either the left or right |
| 244 | page boundaries selected at random. The pages to the left and right of the |
| 245 | object page are "guard pages", whose attributes are changed to a protected |
| 246 | state, and cause page faults on any attempted access. Such page faults are then |
| 247 | intercepted by KFENCE, which handles the fault gracefully by reporting an |
| 248 | out-of-bounds access, and marking the page as accessible so that the faulting |
| 249 | code can (wrongly) continue executing (set ``panic_on_warn`` to panic instead). |
| 250 | |
| 251 | To detect out-of-bounds writes to memory within the object's page itself, |
| 252 | KFENCE also uses pattern-based redzones. For each object page, a redzone is set |
| 253 | up for all non-object memory. For typical alignments, the redzone is only |
| 254 | required on the unguarded side of an object. Because KFENCE must honor the |
| 255 | cache's requested alignment, special alignments may result in unprotected gaps |
| 256 | on either side of an object, all of which are redzoned. |
| 257 | |
| 258 | The following figure illustrates the page layout:: |
| 259 | |
| 260 | ---+-----------+-----------+-----------+-----------+-----------+--- |
| 261 | | xxxxxxxxx | O : | xxxxxxxxx | : O | xxxxxxxxx | |
| 262 | | xxxxxxxxx | B : | xxxxxxxxx | : B | xxxxxxxxx | |
| 263 | | x GUARD x | J : RED- | x GUARD x | RED- : J | x GUARD x | |
| 264 | | xxxxxxxxx | E : ZONE | xxxxxxxxx | ZONE : E | xxxxxxxxx | |
| 265 | | xxxxxxxxx | C : | xxxxxxxxx | : C | xxxxxxxxx | |
| 266 | | xxxxxxxxx | T : | xxxxxxxxx | : T | xxxxxxxxx | |
| 267 | ---+-----------+-----------+-----------+-----------+-----------+--- |
| 268 | |
| 269 | Upon deallocation of a KFENCE object, the object's page is again protected and |
| 270 | the object is marked as freed. Any further access to the object causes a fault |
| 271 | and KFENCE reports a use-after-free access. Freed objects are inserted at the |
| 272 | tail of KFENCE's freelist, so that the least recently freed objects are reused |
| 273 | first, and the chances of detecting use-after-frees of recently freed objects |
| 274 | is increased. |
| 275 | |
Marco Elver | 5cc906b | 2021-11-05 13:45:37 -0700 | [diff] [blame] | 276 | If pool utilization reaches 75% (default) or above, to reduce the risk of the |
| 277 | pool eventually being fully occupied by allocated objects yet ensure diverse |
| 278 | coverage of allocations, KFENCE limits currently covered allocations of the |
| 279 | same source from further filling up the pool. The "source" of an allocation is |
| 280 | based on its partial allocation stack trace. A side-effect is that this also |
| 281 | limits frequent long-lived allocations (e.g. pagecache) of the same source |
| 282 | filling up the pool permanently, which is the most common risk for the pool |
| 283 | becoming full and the sampled allocation rate dropping to zero. The threshold |
| 284 | at which to start limiting currently covered allocations can be configured via |
| 285 | the boot parameter ``kfence.skip_covered_thresh`` (pool usage%). |
| 286 | |
Marco Elver | 10efe55 | 2021-02-25 17:19:26 -0800 | [diff] [blame] | 287 | Interface |
| 288 | --------- |
| 289 | |
| 290 | The following describes the functions which are used by allocators as well as |
| 291 | page handling code to set up and deal with KFENCE allocations. |
| 292 | |
| 293 | .. kernel-doc:: include/linux/kfence.h |
| 294 | :functions: is_kfence_address |
| 295 | kfence_shutdown_cache |
| 296 | kfence_alloc kfence_free __kfence_free |
| 297 | kfence_ksize kfence_object_start |
| 298 | kfence_handle_page_fault |
| 299 | |
| 300 | Related Tools |
| 301 | ------------- |
| 302 | |
| 303 | In userspace, a similar approach is taken by `GWP-ASan |
| 304 | <http://llvm.org/docs/GwpAsan.html>`_. GWP-ASan also relies on guard pages and |
| 305 | a sampling strategy to detect memory unsafety bugs at scale. KFENCE's design is |
| 306 | directly influenced by GWP-ASan, and can be seen as its kernel sibling. Another |
| 307 | similar but non-sampling approach, that also inspired the name "KFENCE", can be |
| 308 | found in the userspace `Electric Fence Malloc Debugger |
| 309 | <https://linux.die.net/man/3/efence>`_. |
| 310 | |
| 311 | In the kernel, several tools exist to debug memory access errors, and in |
| 312 | particular KASAN can detect all bug classes that KFENCE can detect. While KASAN |
| 313 | is more precise, relying on compiler instrumentation, this comes at a |
| 314 | performance cost. |
| 315 | |
| 316 | It is worth highlighting that KASAN and KFENCE are complementary, with |
| 317 | different target environments. For instance, KASAN is the better debugging-aid, |
| 318 | where test cases or reproducers exists: due to the lower chance to detect the |
| 319 | error, it would require more effort using KFENCE to debug. Deployments at scale |
| 320 | that cannot afford to enable KASAN, however, would benefit from using KFENCE to |
| 321 | discover bugs due to code paths not exercised by test cases or fuzzers. |