Skip to content

Commit a084160

Browse files
author
Jiri Kosina
committed
Merge branches 'for-4.12/upstream' and 'for-4.12/klp-hybrid-consistency-model' into for-linus
2 parents 77f8f39 + e679af6 commit a084160

30 files changed

Lines changed: 1540 additions & 354 deletions

File tree

Documentation/ABI/testing/sysfs-kernel-livepatch

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Description:
2525
code is currently applied. Writing 0 will disable the patch
2626
while writing 1 will re-enable the patch.
2727

28+
What: /sys/kernel/livepatch/<patch>/transition
29+
Date: Feb 2017
30+
KernelVersion: 4.12.0
31+
Contact: live-patching@vger.kernel.org
32+
Description:
33+
An attribute which indicates whether the patch is currently in
34+
transition.
35+
2836
What: /sys/kernel/livepatch/<patch>/<object>
2937
Date: Nov 2014
3038
KernelVersion: 3.19.0

Documentation/filesystems/proc.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Table of Contents
4444
3.8 /proc/<pid>/fdinfo/<fd> - Information about opened file
4545
3.9 /proc/<pid>/map_files - Information about memory mapped files
4646
3.10 /proc/<pid>/timerslack_ns - Task timerslack value
47+
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
4748

4849
4 Configuring procfs
4950
4.1 Mount options
@@ -1887,6 +1888,23 @@ Valid values are from 0 - ULLONG_MAX
18871888
An application setting the value must have PTRACE_MODE_ATTACH_FSCREDS level
18881889
permissions on the task specified to change its timerslack_ns value.
18891890

1891+
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
1892+
-----------------------------------------------------------------
1893+
When CONFIG_LIVEPATCH is enabled, this file displays the value of the
1894+
patch state for the task.
1895+
1896+
A value of '-1' indicates that no patch is in transition.
1897+
1898+
A value of '0' indicates that a patch is in transition and the task is
1899+
unpatched. If the patch is being enabled, then the task hasn't been
1900+
patched yet. If the patch is being disabled, then the task has already
1901+
been unpatched.
1902+
1903+
A value of '1' indicates that a patch is in transition and the task is
1904+
patched. If the patch is being enabled, then the task has already been
1905+
patched. If the patch is being disabled, then the task hasn't been
1906+
unpatched yet.
1907+
18901908

18911909
------------------------------------------------------------------------------
18921910
Configuring procfs

Documentation/livepatch/livepatch.txt

Lines changed: 172 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ example, they add a NULL pointer or a boundary check, fix a race by adding
7272
a missing memory barrier, or add some locking around a critical section.
7373
Most of these changes are self contained and the function presents itself
7474
the 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

7778
But there are more complex fixes. For example, a patch might change
7879
ordering 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.
8687
The theory about how to apply functions a safe way is rather complex.
8788
The aim is to define a so-called consistency model. It attempts to define
8889
conditions 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

105227
4. Livepatch module
@@ -134,7 +256,7 @@ Documentation/livepatch/module-elf-format.txt for more details.
134256

135257

136258
4.2. Metadata
137-
------------
259+
-------------
138260

139261
The patch is described by several structures that split the information
140262
into 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

181309
4.3. Livepatch module handling
@@ -188,8 +316,15 @@ section "Livepatch life-cycle" below for more details about these
188316
two operations.
189317

190318
Module 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

194329
5. Livepatch life-cycle
195330
=======================
@@ -239,9 +374,15 @@ Registered patches might be enabled either by calling klp_enable_patch() or
239374
by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will
240375
start 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

246387
Functions might be patched multiple times. The ftrace handler is registered
247388
only 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
261402
either the code from the previously enabled patch or even the original
262403
code 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+
264411
Here all the functions (struct klp_func) associated with the to-be-disabled
265412
patch are removed from the corresponding struct klp_ops. The ftrace handler
266413
is 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.

arch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,12 @@ config HAVE_STACK_VALIDATION
713713
Architecture supports the 'objtool check' host tool command, which
714714
performs compile-time stack metadata validation.
715715

716+
config HAVE_RELIABLE_STACKTRACE
717+
bool
718+
help
719+
Architecture has a save_stack_trace_tsk_reliable() function which
720+
only returns a stack trace if it can guarantee the trace is reliable.
721+
716722
config HAVE_ARCH_HASH
717723
bool
718724
default n

arch/powerpc/include/asm/thread_info.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static inline struct thread_info *current_thread_info(void)
9292
TIF_NEED_RESCHED */
9393
#define TIF_32BIT 4 /* 32 bit binary */
9494
#define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
95+
#define TIF_PATCH_PENDING 6 /* pending live patching update */
9596
#define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
9697
#define TIF_SINGLESTEP 8 /* singlestepping active */
9798
#define TIF_NOHZ 9 /* in adaptive nohz mode */
@@ -115,6 +116,7 @@ static inline struct thread_info *current_thread_info(void)
115116
#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)
116117
#define _TIF_32BIT (1<<TIF_32BIT)
117118
#define _TIF_RESTORE_TM (1<<TIF_RESTORE_TM)
119+
#define _TIF_PATCH_PENDING (1<<TIF_PATCH_PENDING)
118120
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
119121
#define _TIF_SINGLESTEP (1<<TIF_SINGLESTEP)
120122
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
@@ -131,7 +133,7 @@ static inline struct thread_info *current_thread_info(void)
131133

132134
#define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
133135
_TIF_NOTIFY_RESUME | _TIF_UPROBE | \
134-
_TIF_RESTORE_TM)
136+
_TIF_RESTORE_TM | _TIF_PATCH_PENDING)
135137
#define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
136138

137139
/* Bits in local_flags */

arch/powerpc/kernel/signal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/uprobes.h>
1515
#include <linux/key.h>
1616
#include <linux/context_tracking.h>
17+
#include <linux/livepatch.h>
1718
#include <asm/hw_breakpoint.h>
1819
#include <linux/uaccess.h>
1920
#include <asm/unistd.h>
@@ -162,6 +163,9 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
162163
tracehook_notify_resume(regs);
163164
}
164165

166+
if (thread_info_flags & _TIF_PATCH_PENDING)
167+
klp_update_patch_state(current);
168+
165169
user_enter();
166170
}
167171

0 commit comments

Comments
 (0)