@@ -72,8 +72,7 @@ example, they add a NULL pointer or a boundary check, fix a race by adding
72
72
a missing memory barrier, or add some locking around a critical section.
73
73
Most of these changes are self contained and the function presents itself
74
74
the same way to the rest of the system. In this case, the functions might
75
- be updated independently one by one. (This can be done by setting the
76
- 'immediate' flag in the klp_patch struct.)
75
+ be updated independently one by one.
77
76
78
77
But there are more complex fixes. For example, a patch might change
79
78
ordering of locking in multiple functions at the same time. Or a patch
@@ -125,40 +124,23 @@ safe to patch tasks:
125
124
b) Patching CPU-bound user tasks. If the task is highly CPU-bound
126
125
then it will get patched the next time it gets interrupted by an
127
126
IRQ.
128
- c) In the future it could be useful for applying patches for
129
- architectures which don't yet have HAVE_RELIABLE_STACKTRACE. In
130
- this case you would have to signal most of the tasks on the
131
- system. However this isn't supported yet because there's
132
- currently no way to patch kthreads without
133
- HAVE_RELIABLE_STACKTRACE.
134
127
135
128
3. For idle "swapper" tasks, since they don't ever exit the kernel, they
136
129
instead have a klp_update_patch_state() call in the idle loop which
137
130
allows them to be patched before the CPU enters the idle state.
138
131
139
132
(Note there's not yet such an approach for kthreads.)
140
133
141
- All the above approaches may be skipped by setting the 'immediate' flag
142
- in the 'klp_patch' struct, which will disable per-task consistency and
143
- patch all tasks immediately. This can be useful if the patch doesn't
144
- change any function or data semantics. Note that, even with this flag
145
- set, it's possible that some tasks may still be running with an old
146
- version of the function, until that function returns .
134
+ Architectures which don't have HAVE_RELIABLE_STACKTRACE solely rely on
135
+ the second approach. It's highly likely that some tasks may still be
136
+ running with an old version of the function, until that function
137
+ returns. In this case you would have to signal the tasks. This
138
+ especially applies to kthreads. They may not be woken up and would need
139
+ to be forced. See below for more information .
147
140
148
- There's also an 'immediate' flag in the 'klp_func' struct which allows
149
- you to specify that certain functions in the patch can be applied
150
- without per-task consistency. This might be useful if you want to patch
151
- a common function like schedule(), and the function change doesn't need
152
- consistency but the rest of the patch does.
153
-
154
- For architectures which don't have HAVE_RELIABLE_STACKTRACE, the user
155
- must set patch->immediate which causes all tasks to be patched
156
- immediately. This option should be used with care, only when the patch
157
- doesn't change any function or data semantics.
158
-
159
- In the future, architectures which don't have HAVE_RELIABLE_STACKTRACE
160
- may be allowed to use per-task consistency if we can come up with
161
- another way to patch kthreads.
141
+ Unless we can come up with another way to patch kthreads, architectures
142
+ without HAVE_RELIABLE_STACKTRACE are not considered fully supported by
143
+ the kernel livepatching.
162
144
163
145
The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
164
146
is in transition. Only a single patch (the topmost patch on the stack)
@@ -176,8 +158,31 @@ If a patch is in transition, this file shows 0 to indicate the task is
176
158
unpatched and 1 to indicate it's patched. Otherwise, if no patch is in
177
159
transition, it shows -1. Any tasks which are blocking the transition
178
160
can be signaled with SIGSTOP and SIGCONT to force them to change their
179
- patched state.
180
-
161
+ patched state. This may be harmful to the system though.
162
+ /sys/kernel/livepatch/<patch>/signal attribute provides a better alternative.
163
+ Writing 1 to the attribute sends a fake signal to all remaining blocking
164
+ tasks. No proper signal is actually delivered (there is no data in signal
165
+ pending structures). Tasks are interrupted or woken up, and forced to change
166
+ their patched state.
167
+
168
+ Administrator can also affect a transition through
169
+ /sys/kernel/livepatch/<patch>/force attribute. Writing 1 there clears
170
+ TIF_PATCH_PENDING flag of all tasks and thus forces the tasks to the patched
171
+ state. Important note! The force attribute is intended for cases when the
172
+ transition gets stuck for a long time because of a blocking task. Administrator
173
+ is expected to collect all necessary data (namely stack traces of such blocking
174
+ tasks) and request a clearance from a patch distributor to force the transition.
175
+ Unauthorized usage may cause harm to the system. It depends on the nature of the
176
+ patch, which functions are (un)patched, and which functions the blocking tasks
177
+ are sleeping in (/proc/<pid>/stack may help here). Removal (rmmod) of patch
178
+ modules is permanently disabled when the force feature is used. It cannot be
179
+ guaranteed there is no task sleeping in such module. It implies unbounded
180
+ reference count if a patch module is disabled and enabled in a loop.
181
+
182
+ Moreover, the usage of force may also affect future applications of live
183
+ patches and cause even more harm to the system. Administrator should first
184
+ consider to simply cancel a transition (see above). If force is used, reboot
185
+ should be planned and no more live patches applied.
181
186
182
187
3.1 Adding consistency model support to new architectures
183
188
---------------------------------------------------------
@@ -216,13 +221,6 @@ few options:
216
221
a good backup option for those architectures which don't have
217
222
reliable stack traces yet.
218
223
219
- In the meantime, patches for such architectures can bypass the
220
- consistency model by setting klp_patch.immediate to true. This option
221
- is perfectly fine for patches which don't change the semantics of the
222
- patched functions. In practice, this is usable for ~90% of security
223
- fixes. Use of this option also means the patch can't be unloaded after
224
- it has been disabled.
225
-
226
224
227
225
4. Livepatch module
228
226
===================
@@ -278,9 +276,6 @@ into three levels:
278
276
only for a particular object ( vmlinux or a kernel module ). Note that
279
277
kallsyms allows for searching symbols according to the object name.
280
278
281
- There's also an 'immediate' flag which, when set, patches the
282
- function immediately, bypassing the consistency model safety checks.
283
-
284
279
+ struct klp_object defines an array of patched functions (struct
285
280
klp_func) in the same object. Where the object is either vmlinux
286
281
(NULL) or a module name.
@@ -299,9 +294,6 @@ into three levels:
299
294
symbols are found. The only exception are symbols from objects
300
295
(kernel modules) that have not been loaded yet.
301
296
302
- Setting the 'immediate' flag applies the patch to all tasks
303
- immediately, bypassing the consistency model safety checks.
304
-
305
297
For more details on how the patch is applied on a per-task basis,
306
298
see the "Consistency model" section.
307
299
@@ -316,14 +308,12 @@ section "Livepatch life-cycle" below for more details about these
316
308
two operations.
317
309
318
310
Module removal is only safe when there are no users of the underlying
319
- functions. The immediate consistency model is not able to detect this. The
320
- code just redirects the functions at the very beginning and it does not
321
- check if the functions are in use. In other words, it knows when the
322
- functions get called but it does not know when the functions return.
323
- Therefore it cannot be decided when the livepatch module can be safely
324
- removed. This is solved by a hybrid consistency model. When the system is
325
- transitioned to a new patch state (patched/unpatched) it is guaranteed that
326
- no task sleeps or runs in the old code.
311
+ functions. This is the reason why the force feature permanently disables
312
+ the removal. The forced tasks entered the functions but we cannot say
313
+ that they returned back. Therefore it cannot be decided when the
314
+ livepatch module can be safely removed. When the system is successfully
315
+ transitioned to a new patch state (patched/unpatched) without being
316
+ forced it is guaranteed that no task sleeps or runs in the old code.
327
317
328
318
329
319
5. Livepatch life-cycle
@@ -337,19 +327,12 @@ First, the patch is applied only when all patched symbols for already
337
327
loaded objects are found. The error handling is much easier if this
338
328
check is done before particular functions get redirected.
339
329
340
- Second, the immediate consistency model does not guarantee that anyone is not
341
- sleeping in the new code after the patch is reverted. This means that the new
342
- code needs to stay around "forever". If the code is there, one could apply it
343
- again. Therefore it makes sense to separate the operations that might be done
344
- once and those that need to be repeated when the patch is enabled (applied)
345
- again.
346
-
347
- Third, it might take some time until the entire system is migrated
348
- when a more complex consistency model is used. The patch revert might
349
- block the livepatch module removal for too long. Therefore it is useful
350
- to revert the patch using a separate operation that might be called
351
- explicitly. But it does not make sense to remove all information
352
- until the livepatch module is really removed.
330
+ Second, it might take some time until the entire system is migrated with
331
+ the hybrid consistency model being used. The patch revert might block
332
+ the livepatch module removal for too long. Therefore it is useful to
333
+ revert the patch using a separate operation that might be called
334
+ explicitly. But it does not make sense to remove all information until
335
+ the livepatch module is really removed.
353
336
354
337
355
338
5.1. Registration
@@ -435,6 +418,9 @@ Information about the registered patches can be found under
435
418
/sys/kernel/livepatch. The patches could be enabled and disabled
436
419
by writing there.
437
420
421
+ /sys/kernel/livepatch/<patch>/signal and /sys/kernel/livepatch/<patch>/force
422
+ attributes allow administrator to affect a patching operation.
423
+
438
424
See Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
439
425
440
426
0 commit comments