Skip to content

Commit d83a7cb

Browse files
jpoimboeJiri Kosina
authored andcommitted
livepatch: change to a per-task consistency model
Change livepatch to use a basic per-task consistency model. This is the foundation which will eventually enable us to patch those ~10% of security patches which change function or data semantics. This is the biggest remaining piece needed to make livepatch more generally useful. This code stems from the design proposal made by Vojtech [1] in November 2014. It's a hybrid of kGraft and kpatch: it uses kGraft's per-task consistency and syscall barrier switching combined with kpatch's stack trace switching. There are also a number of fallback options which make it quite flexible. Patches are applied on a per-task basis, when the task is deemed safe to switch over. When a patch is enabled, livepatch enters into a transition state where tasks are converging to the patched state. Usually this transition state can complete in a few seconds. The same sequence occurs when a patch is disabled, except the tasks converge from the patched state to the unpatched state. An interrupt handler inherits the patched state of the task it interrupts. The same is true for forked tasks: the child inherits the patched state of the parent. Livepatch uses several complementary approaches to determine when it's safe to patch tasks: 1. The first and most effective approach is stack checking of sleeping tasks. If no affected functions are on the stack of a given task, the task is patched. In most cases this will patch most or all of the tasks on the first try. Otherwise it'll keep trying periodically. This option is only available if the architecture has reliable stacks (HAVE_RELIABLE_STACKTRACE). 2. The second approach, if needed, is kernel exit switching. A task is switched when it returns to user space from a system call, a user space IRQ, or a signal. It's useful in the following cases: a) Patching I/O-bound user tasks which are sleeping on an affected function. In this case you have to send SIGSTOP and SIGCONT to force it to exit the kernel and be patched. b) Patching CPU-bound user tasks. If the task is highly CPU-bound then it will get patched the next time it gets interrupted by an IRQ. c) In the future it could be useful for applying patches for architectures which don't yet have HAVE_RELIABLE_STACKTRACE. In this case you would have to signal most of the tasks on the system. However this isn't supported yet because there's currently no way to patch kthreads without HAVE_RELIABLE_STACKTRACE. 3. For idle "swapper" tasks, since they don't ever exit the kernel, they instead have a klp_update_patch_state() call in the idle loop which allows them to be patched before the CPU enters the idle state. (Note there's not yet such an approach for kthreads.) All the above approaches may be skipped by setting the 'immediate' flag in the 'klp_patch' struct, which will disable per-task consistency and patch all tasks immediately. This can be useful if the patch doesn't change any function or data semantics. Note that, even with this flag set, it's possible that some tasks may still be running with an old version of the function, until that function returns. There's also an 'immediate' flag in the 'klp_func' struct which allows you to specify that certain functions in the patch can be applied without per-task consistency. This might be useful if you want to patch a common function like schedule(), and the function change doesn't need consistency but the rest of the patch does. For architectures which don't have HAVE_RELIABLE_STACKTRACE, the user must set patch->immediate which causes all tasks to be patched immediately. This option should be used with care, only when the patch doesn't change any function or data semantics. In the future, architectures which don't have HAVE_RELIABLE_STACKTRACE may be allowed to use per-task consistency if we can come up with another way to patch kthreads. The /sys/kernel/livepatch/<patch>/transition file shows whether a patch is in transition. Only a single patch (the topmost patch on the stack) can be in transition at a given time. A patch can remain in transition indefinitely, if any of the tasks are stuck in the initial patch state. A transition can be reversed and effectively canceled by writing the opposite value to the /sys/kernel/livepatch/<patch>/enabled file while the transition is in progress. Then all the tasks will attempt to converge back to the original patch state. [1] https://lkml.kernel.org/r/[email protected] Signed-off-by: Josh Poimboeuf <[email protected]> Acked-by: Miroslav Benes <[email protected]> Acked-by: Ingo Molnar <[email protected]> # for the scheduler changes Signed-off-by: Jiri Kosina <[email protected]>
1 parent f5e547f commit d83a7cb

File tree

14 files changed

+947
-49
lines changed

14 files changed

+947
-49
lines changed

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+
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/livepatch/livepatch.txt

Lines changed: 163 additions & 23 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/[email protected]
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
@@ -239,9 +367,15 @@ Registered patches might be enabled either by calling klp_enable_patch() or
239367
by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will
240368
start using the new implementation of the patched functions at this stage.
241369

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.
370+
When a patch is enabled, livepatch enters into a transition state where
371+
tasks are converging to the patched state. This is indicated by a value
372+
of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks have
373+
been patched, the 'transition' value changes to '0'. For more
374+
information about this process, see the "Consistency model" section.
375+
376+
If an original function is patched for the first time, a function
377+
specific struct klp_ops is created and an universal ftrace handler is
378+
registered.
245379

246380
Functions might be patched multiple times. The ftrace handler is registered
247381
only once for the given function. Further patches just add an entry to the
@@ -261,6 +395,12 @@ by writing '0' to /sys/kernel/livepatch/<name>/enabled. At this stage
261395
either the code from the previously enabled patch or even the original
262396
code gets used.
263397

398+
When a patch is disabled, livepatch enters into a transition state where
399+
tasks are converging to the unpatched state. This is indicated by a
400+
value of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks
401+
have been unpatched, the 'transition' value changes to '0'. For more
402+
information about this process, see the "Consistency model" section.
403+
264404
Here all the functions (struct klp_func) associated with the to-be-disabled
265405
patch are removed from the corresponding struct klp_ops. The ftrace handler
266406
is unregistered and the struct klp_ops is freed when the func_stack list

include/linux/init_task.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/sched/autogroup.h>
1616
#include <net/net_namespace.h>
1717
#include <linux/sched/rt.h>
18+
#include <linux/livepatch.h>
1819
#include <linux/mm_types.h>
1920

2021
#include <asm/thread_info.h>
@@ -202,6 +203,13 @@ extern struct cred init_cred;
202203
# define INIT_KASAN(tsk)
203204
#endif
204205

206+
#ifdef CONFIG_LIVEPATCH
207+
# define INIT_LIVEPATCH(tsk) \
208+
.patch_state = KLP_UNDEFINED,
209+
#else
210+
# define INIT_LIVEPATCH(tsk)
211+
#endif
212+
205213
#ifdef CONFIG_THREAD_INFO_IN_TASK
206214
# define INIT_TASK_TI(tsk) \
207215
.thread_info = INIT_THREAD_INFO(tsk), \
@@ -288,6 +296,7 @@ extern struct cred init_cred;
288296
INIT_VTIME(tsk) \
289297
INIT_NUMA_BALANCING(tsk) \
290298
INIT_KASAN(tsk) \
299+
INIT_LIVEPATCH(tsk) \
291300
}
292301

293302

include/linux/livepatch.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,40 @@
2828

2929
#include <asm/livepatch.h>
3030

31+
/* task patch states */
32+
#define KLP_UNDEFINED -1
33+
#define KLP_UNPATCHED 0
34+
#define KLP_PATCHED 1
35+
3136
/**
3237
* struct klp_func - function structure for live patching
3338
* @old_name: name of the function to be patched
3439
* @new_func: pointer to the patched function code
3540
* @old_sympos: a hint indicating which symbol position the old function
3641
* can be found (optional)
42+
* @immediate: patch the func immediately, bypassing safety mechanisms
3743
* @old_addr: the address of the function being patched
3844
* @kobj: kobject for sysfs resources
3945
* @stack_node: list node for klp_ops func_stack list
4046
* @old_size: size of the old function
4147
* @new_size: size of the new function
4248
* @patched: the func has been added to the klp_ops list
49+
* @transition: the func is currently being applied or reverted
50+
*
51+
* The patched and transition variables define the func's patching state. When
52+
* patching, a func is always in one of the following states:
53+
*
54+
* patched=0 transition=0: unpatched
55+
* patched=0 transition=1: unpatched, temporary starting state
56+
* patched=1 transition=1: patched, may be visible to some tasks
57+
* patched=1 transition=0: patched, visible to all tasks
58+
*
59+
* And when unpatching, it goes in the reverse order:
60+
*
61+
* patched=1 transition=0: patched, visible to all tasks
62+
* patched=1 transition=1: patched, may be visible to some tasks
63+
* patched=0 transition=1: unpatched, temporary ending state
64+
* patched=0 transition=0: unpatched
4365
*/
4466
struct klp_func {
4567
/* external */
@@ -53,13 +75,15 @@ struct klp_func {
5375
* in kallsyms for the given object is used.
5476
*/
5577
unsigned long old_sympos;
78+
bool immediate;
5679

5780
/* internal */
5881
unsigned long old_addr;
5982
struct kobject kobj;
6083
struct list_head stack_node;
6184
unsigned long old_size, new_size;
6285
bool patched;
86+
bool transition;
6387
};
6488

6589
/**
@@ -68,7 +92,7 @@ struct klp_func {
6892
* @funcs: function entries for functions to be patched in the object
6993
* @kobj: kobject for sysfs resources
7094
* @mod: kernel module associated with the patched object
71-
* (NULL for vmlinux)
95+
* (NULL for vmlinux)
7296
* @patched: the object's funcs have been added to the klp_ops list
7397
*/
7498
struct klp_object {
@@ -86,6 +110,7 @@ struct klp_object {
86110
* struct klp_patch - patch structure for live patching
87111
* @mod: reference to the live patch module
88112
* @objs: object entries for kernel objects to be patched
113+
* @immediate: patch all funcs immediately, bypassing safety mechanisms
89114
* @list: list node for global list of registered patches
90115
* @kobj: kobject for sysfs resources
91116
* @enabled: the patch is enabled (but operation may be incomplete)
@@ -94,6 +119,7 @@ struct klp_patch {
94119
/* external */
95120
struct module *mod;
96121
struct klp_object *objs;
122+
bool immediate;
97123

98124
/* internal */
99125
struct list_head list;
@@ -121,13 +147,27 @@ void arch_klp_init_object_loaded(struct klp_patch *patch,
121147
int klp_module_coming(struct module *mod);
122148
void klp_module_going(struct module *mod);
123149

150+
void klp_copy_process(struct task_struct *child);
124151
void klp_update_patch_state(struct task_struct *task);
125152

153+
static inline bool klp_patch_pending(struct task_struct *task)
154+
{
155+
return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
156+
}
157+
158+
static inline bool klp_have_reliable_stack(void)
159+
{
160+
return IS_ENABLED(CONFIG_STACKTRACE) &&
161+
IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
162+
}
163+
126164
#else /* !CONFIG_LIVEPATCH */
127165

128166
static inline int klp_module_coming(struct module *mod) { return 0; }
129167
static inline void klp_module_going(struct module *mod) {}
168+
static inline bool klp_patch_pending(struct task_struct *task) { return false; }
130169
static inline void klp_update_patch_state(struct task_struct *task) {}
170+
static inline void klp_copy_process(struct task_struct *child) {}
131171

132172
#endif /* CONFIG_LIVEPATCH */
133173

include/linux/sched.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,9 @@ struct task_struct {
10371037
#ifdef CONFIG_THREAD_INFO_IN_TASK
10381038
/* A live task holds one reference: */
10391039
atomic_t stack_refcount;
1040+
#endif
1041+
#ifdef CONFIG_LIVEPATCH
1042+
int patch_state;
10401043
#endif
10411044
/* CPU-specific state of this task: */
10421045
struct thread_struct thread;

0 commit comments

Comments
 (0)