@@ -72,7 +72,8 @@ example, they add a NULL pointer or a boundary check, fix a race by adding
7272a missing memory barrier, or add some locking around a critical section.
7373Most of these changes are self contained and the function presents itself
7474the same way to the rest of the system. In this case, the functions might
75- be updated independently one by one.
75+ be updated independently one by one. (This can be done by setting the
76+ 'immediate' flag in the klp_patch struct.)
7677
7778But there are more complex fixes. For example, a patch might change
7879ordering of locking in multiple functions at the same time. Or a patch
@@ -86,20 +87,141 @@ or no data are stored in the modified structures at the moment.
8687The theory about how to apply functions a safe way is rather complex.
8788The aim is to define a so-called consistency model. It attempts to define
8889conditions when the new implementation could be used so that the system
89- stays consistent. The theory is not yet finished. See the discussion at
90- https://lkml.kernel.org/r/20141107140458.GA21774@suse.cz
91-
92- The current consistency model is very simple. It guarantees that either
93- the old or the new function is called. But various functions get redirected
94- one by one without any synchronization.
95-
96- In other words, the current implementation _never_ modifies the behavior
97- in the middle of the call. It is because it does _not_ rewrite the entire
98- function in the memory. Instead, the function gets redirected at the
99- very beginning. But this redirection is used immediately even when
100- some other functions from the same patch have not been redirected yet.
101-
102- See also the section "Limitations" below.
90+ stays consistent.
91+
92+ Livepatch has a consistency model which is a hybrid of kGraft and
93+ kpatch: it uses kGraft's per-task consistency and syscall barrier
94+ switching combined with kpatch's stack trace switching. There are also
95+ a number of fallback options which make it quite flexible.
96+
97+ Patches are applied on a per-task basis, when the task is deemed safe to
98+ switch over. When a patch is enabled, livepatch enters into a
99+ transition state where tasks are converging to the patched state.
100+ Usually this transition state can complete in a few seconds. The same
101+ sequence occurs when a patch is disabled, except the tasks converge from
102+ the patched state to the unpatched state.
103+
104+ An interrupt handler inherits the patched state of the task it
105+ interrupts. The same is true for forked tasks: the child inherits the
106+ patched state of the parent.
107+
108+ Livepatch uses several complementary approaches to determine when it's
109+ safe to patch tasks:
110+
111+ 1. The first and most effective approach is stack checking of sleeping
112+ tasks. If no affected functions are on the stack of a given task,
113+ the task is patched. In most cases this will patch most or all of
114+ the tasks on the first try. Otherwise it'll keep trying
115+ periodically. This option is only available if the architecture has
116+ reliable stacks (HAVE_RELIABLE_STACKTRACE).
117+
118+ 2. The second approach, if needed, is kernel exit switching. A
119+ task is switched when it returns to user space from a system call, a
120+ user space IRQ, or a signal. It's useful in the following cases:
121+
122+ a) Patching I/O-bound user tasks which are sleeping on an affected
123+ function. In this case you have to send SIGSTOP and SIGCONT to
124+ force it to exit the kernel and be patched.
125+ b) Patching CPU-bound user tasks. If the task is highly CPU-bound
126+ then it will get patched the next time it gets interrupted by an
127+ 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+
135+ 3. For idle "swapper" tasks, since they don't ever exit the kernel, they
136+ instead have a klp_update_patch_state() call in the idle loop which
137+ allows them to be patched before the CPU enters the idle state.
138+
139+ (Note there's not yet such an approach for kthreads.)
140+
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.
147+
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.
162+
163+ The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
164+ is in transition. Only a single patch (the topmost patch on the stack)
165+ can be in transition at a given time. A patch can remain in transition
166+ indefinitely, if any of the tasks are stuck in the initial patch state.
167+
168+ A transition can be reversed and effectively canceled by writing the
169+ opposite value to the /sys/kernel/livepatch/<patch>/enabled file while
170+ the transition is in progress. Then all the tasks will attempt to
171+ converge back to the original patch state.
172+
173+ There's also a /proc/<pid>/patch_state file which can be used to
174+ determine which tasks are blocking completion of a patching operation.
175+ If a patch is in transition, this file shows 0 to indicate the task is
176+ unpatched and 1 to indicate it's patched. Otherwise, if no patch is in
177+ transition, it shows -1. Any tasks which are blocking the transition
178+ can be signaled with SIGSTOP and SIGCONT to force them to change their
179+ patched state.
180+
181+
182+ 3.1 Adding consistency model support to new architectures
183+ ---------------------------------------------------------
184+
185+ For adding consistency model support to new architectures, there are a
186+ few options:
187+
188+ 1) Add CONFIG_HAVE_RELIABLE_STACKTRACE. This means porting objtool, and
189+ for non-DWARF unwinders, also making sure there's a way for the stack
190+ tracing code to detect interrupts on the stack.
191+
192+ 2) Alternatively, ensure that every kthread has a call to
193+ klp_update_patch_state() in a safe location. Kthreads are typically
194+ in an infinite loop which does some action repeatedly. The safe
195+ location to switch the kthread's patch state would be at a designated
196+ point in the loop where there are no locks taken and all data
197+ structures are in a well-defined state.
198+
199+ The location is clear when using workqueues or the kthread worker
200+ API. These kthreads process independent actions in a generic loop.
201+
202+ It's much more complicated with kthreads which have a custom loop.
203+ There the safe location must be carefully selected on a case-by-case
204+ basis.
205+
206+ In that case, arches without HAVE_RELIABLE_STACKTRACE would still be
207+ able to use the non-stack-checking parts of the consistency model:
208+
209+ a) patching user tasks when they cross the kernel/user space
210+ boundary; and
211+
212+ b) patching kthreads and idle tasks at their designated patch points.
213+
214+ This option isn't as good as option 1 because it requires signaling
215+ user tasks and waking kthreads to patch them. But it could still be
216+ a good backup option for those architectures which don't have
217+ reliable stack traces yet.
218+
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.
103225
104226
1052274. Livepatch module
@@ -134,7 +256,7 @@ Documentation/livepatch/module-elf-format.txt for more details.
134256
135257
1362584.2. Metadata
137- ------------
259+ -------------
138260
139261The patch is described by several structures that split the information
140262into three levels:
@@ -156,6 +278,9 @@ into three levels:
156278 only for a particular object ( vmlinux or a kernel module ). Note that
157279 kallsyms allows for searching symbols according to the object name.
158280
281+ There's also an 'immediate' flag which, when set, patches the
282+ function immediately, bypassing the consistency model safety checks.
283+
159284 + struct klp_object defines an array of patched functions (struct
160285 klp_func) in the same object. Where the object is either vmlinux
161286 (NULL) or a module name.
@@ -172,10 +297,13 @@ into three levels:
172297 This structure handles all patched functions consistently and eventually,
173298 synchronously. The whole patch is applied only when all patched
174299 symbols are found. The only exception are symbols from objects
175- (kernel modules) that have not been loaded yet. Also if a more complex
176- consistency model is supported then a selected unit (thread,
177- kernel as a whole) will see the new code from the entire patch
178- only when it is in a safe state.
300+ (kernel modules) that have not been loaded yet.
301+
302+ Setting the 'immediate' flag applies the patch to all tasks
303+ immediately, bypassing the consistency model safety checks.
304+
305+ For more details on how the patch is applied on a per-task basis,
306+ see the "Consistency model" section.
179307
180308
1813094.3. Livepatch module handling
@@ -188,8 +316,15 @@ section "Livepatch life-cycle" below for more details about these
188316two operations.
189317
190318Module removal is only safe when there are no users of the underlying
191- functions. The immediate consistency model is not able to detect this;
192- therefore livepatch modules cannot be removed. See "Limitations" below.
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.
327+
193328
1943295. Livepatch life-cycle
195330=======================
@@ -239,9 +374,15 @@ Registered patches might be enabled either by calling klp_enable_patch() or
239374by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will
240375start using the new implementation of the patched functions at this stage.
241376
242- In particular, if an original function is patched for the first time, a
243- function specific struct klp_ops is created and an universal ftrace handler
244- is registered.
377+ When a patch is enabled, livepatch enters into a transition state where
378+ tasks are converging to the patched state. This is indicated by a value
379+ of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks have
380+ been patched, the 'transition' value changes to '0'. For more
381+ information about this process, see the "Consistency model" section.
382+
383+ If an original function is patched for the first time, a function
384+ specific struct klp_ops is created and an universal ftrace handler is
385+ registered.
245386
246387Functions might be patched multiple times. The ftrace handler is registered
247388only once for the given function. Further patches just add an entry to the
@@ -261,6 +402,12 @@ by writing '0' to /sys/kernel/livepatch/<name>/enabled. At this stage
261402either the code from the previously enabled patch or even the original
262403code gets used.
263404
405+ When a patch is disabled, livepatch enters into a transition state where
406+ tasks are converging to the unpatched state. This is indicated by a
407+ value of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks
408+ have been unpatched, the 'transition' value changes to '0'. For more
409+ information about this process, see the "Consistency model" section.
410+
264411Here all the functions (struct klp_func) associated with the to-be-disabled
265412patch are removed from the corresponding struct klp_ops. The ftrace handler
266413is unregistered and the struct klp_ops is freed when the func_stack list
@@ -329,23 +476,6 @@ The current Livepatch implementation has several limitations:
329476 by "notrace".
330477
331478
332- + Livepatch modules can not be removed.
333-
334- The current implementation just redirects the functions at the very
335- beginning. It does not check if the functions are in use. In other
336- words, it knows when the functions get called but it does not
337- know when the functions return. Therefore it can not decide when
338- the livepatch module can be safely removed.
339-
340- This will get most likely solved once a more complex consistency model
341- is supported. The idea is that a safe state for patching should also
342- mean a safe state for removing the patch.
343-
344- Note that the patch itself might get disabled by writing zero
345- to /sys/kernel/livepatch/<patch>/enabled. It causes that the new
346- code will not longer get called. But it does not guarantee
347- that anyone is not sleeping anywhere in the new code.
348-
349479
350480 + Livepatch works reliably only when the dynamic ftrace is located at
351481 the very beginning of the function.
0 commit comments