Skip to content

Commit e1c70f3

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching
Pull livepatching updates from Jiri Kosina: - handle 'infinitely'-long sleeping tasks, from Miroslav Benes - remove 'immediate' feature, as it turns out it doesn't provide the originally expected semantics, and brings more issues than value * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching: livepatch: add locking to force and signal functions livepatch: Remove immediate feature livepatch: force transition to finish livepatch: send a fake signal to all blocking tasks
2 parents 183b636 + d05b695 commit e1c70f3

File tree

13 files changed

+227
-187
lines changed

13 files changed

+227
-187
lines changed

Documentation/ABI/testing/sysfs-kernel-livepatch

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ Description:
3333
An attribute which indicates whether the patch is currently in
3434
transition.
3535

36+
What: /sys/kernel/livepatch/<patch>/signal
37+
Date: Nov 2017
38+
KernelVersion: 4.15.0
39+
40+
Description:
41+
A writable attribute that allows administrator to affect the
42+
course of an existing transition. Writing 1 sends a fake
43+
signal to all remaining blocking tasks. The fake signal
44+
means that no proper signal is delivered (there is no data in
45+
signal pending structures). Tasks are interrupted or woken up,
46+
and forced to change their patched state.
47+
48+
What: /sys/kernel/livepatch/<patch>/force
49+
Date: Nov 2017
50+
KernelVersion: 4.15.0
51+
52+
Description:
53+
A writable attribute that allows administrator to affect the
54+
course of an existing transition. Writing 1 clears
55+
TIF_PATCH_PENDING flag of all tasks and thus forces the tasks to
56+
the patched or unpatched state. Administrator should not
57+
use this feature without a clearance from a patch
58+
distributor. Removal (rmmod) of patch modules is permanently
59+
disabled when the feature is used. See
60+
Documentation/livepatch/livepatch.txt for more information.
61+
3662
What: /sys/kernel/livepatch/<patch>/<object>
3763
Date: Nov 2014
3864
KernelVersion: 3.19.0

Documentation/livepatch/livepatch.txt

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ 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. (This can be done by setting the
76-
'immediate' flag in the klp_patch struct.)
75+
be updated independently one by one.
7776

7877
But there are more complex fixes. For example, a patch might change
7978
ordering of locking in multiple functions at the same time. Or a patch
@@ -125,40 +124,23 @@ safe to patch tasks:
125124
b) Patching CPU-bound user tasks. If the task is highly CPU-bound
126125
then it will get patched the next time it gets interrupted by an
127126
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.
134127

135128
3. For idle "swapper" tasks, since they don't ever exit the kernel, they
136129
instead have a klp_update_patch_state() call in the idle loop which
137130
allows them to be patched before the CPU enters the idle state.
138131

139132
(Note there's not yet such an approach for kthreads.)
140133

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.
147140

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.
162144

163145
The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
164146
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
176158
unpatched and 1 to indicate it's patched. Otherwise, if no patch is in
177159
transition, it shows -1. Any tasks which are blocking the transition
178160
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.
181186

182187
3.1 Adding consistency model support to new architectures
183188
---------------------------------------------------------
@@ -216,13 +221,6 @@ few options:
216221
a good backup option for those architectures which don't have
217222
reliable stack traces yet.
218223

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-
226224

227225
4. Livepatch module
228226
===================
@@ -278,9 +276,6 @@ into three levels:
278276
only for a particular object ( vmlinux or a kernel module ). Note that
279277
kallsyms allows for searching symbols according to the object name.
280278

281-
There's also an 'immediate' flag which, when set, patches the
282-
function immediately, bypassing the consistency model safety checks.
283-
284279
+ struct klp_object defines an array of patched functions (struct
285280
klp_func) in the same object. Where the object is either vmlinux
286281
(NULL) or a module name.
@@ -299,9 +294,6 @@ into three levels:
299294
symbols are found. The only exception are symbols from objects
300295
(kernel modules) that have not been loaded yet.
301296

302-
Setting the 'immediate' flag applies the patch to all tasks
303-
immediately, bypassing the consistency model safety checks.
304-
305297
For more details on how the patch is applied on a per-task basis,
306298
see the "Consistency model" section.
307299

@@ -316,14 +308,12 @@ section "Livepatch life-cycle" below for more details about these
316308
two operations.
317309

318310
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.
327317

328318

329319
5. Livepatch life-cycle
@@ -337,19 +327,12 @@ First, the patch is applied only when all patched symbols for already
337327
loaded objects are found. The error handling is much easier if this
338328
check is done before particular functions get redirected.
339329

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.
353336

354337

355338
5.1. Registration
@@ -435,6 +418,9 @@ Information about the registered patches can be found under
435418
/sys/kernel/livepatch. The patches could be enabled and disabled
436419
by writing there.
437420

421+
/sys/kernel/livepatch/<patch>/signal and /sys/kernel/livepatch/<patch>/force
422+
attributes allow administrator to affect a patching operation.
423+
438424
See Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
439425

440426

arch/powerpc/kernel/signal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
153153
if (thread_info_flags & _TIF_UPROBE)
154154
uprobe_notify_resume(regs);
155155

156+
if (thread_info_flags & _TIF_PATCH_PENDING)
157+
klp_update_patch_state(current);
158+
156159
if (thread_info_flags & _TIF_SIGPENDING) {
157160
BUG_ON(regs != current->thread.regs);
158161
do_signal(current);
@@ -163,9 +166,6 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
163166
tracehook_notify_resume(regs);
164167
}
165168

166-
if (thread_info_flags & _TIF_PATCH_PENDING)
167-
klp_update_patch_state(current);
168-
169169
user_enter();
170170
}
171171

arch/x86/entry/common.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
153153
if (cached_flags & _TIF_UPROBE)
154154
uprobe_notify_resume(regs);
155155

156+
if (cached_flags & _TIF_PATCH_PENDING)
157+
klp_update_patch_state(current);
158+
156159
/* deal with pending signal delivery */
157160
if (cached_flags & _TIF_SIGPENDING)
158161
do_signal(regs);
@@ -165,9 +168,6 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
165168
if (cached_flags & _TIF_USER_RETURN_NOTIFY)
166169
fire_user_return_notifiers();
167170

168-
if (cached_flags & _TIF_PATCH_PENDING)
169-
klp_update_patch_state(current);
170-
171171
/* Disable IRQs and retry */
172172
local_irq_disable();
173173

include/linux/livepatch.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
* @new_func: pointer to the patched function code
4141
* @old_sympos: a hint indicating which symbol position the old function
4242
* can be found (optional)
43-
* @immediate: patch the func immediately, bypassing safety mechanisms
4443
* @old_addr: the address of the function being patched
4544
* @kobj: kobject for sysfs resources
4645
* @stack_node: list node for klp_ops func_stack list
@@ -76,7 +75,6 @@ struct klp_func {
7675
* in kallsyms for the given object is used.
7776
*/
7877
unsigned long old_sympos;
79-
bool immediate;
8078

8179
/* internal */
8280
unsigned long old_addr;
@@ -137,7 +135,6 @@ struct klp_object {
137135
* struct klp_patch - patch structure for live patching
138136
* @mod: reference to the live patch module
139137
* @objs: object entries for kernel objects to be patched
140-
* @immediate: patch all funcs immediately, bypassing safety mechanisms
141138
* @list: list node for global list of registered patches
142139
* @kobj: kobject for sysfs resources
143140
* @enabled: the patch is enabled (but operation may be incomplete)
@@ -147,7 +144,6 @@ struct klp_patch {
147144
/* external */
148145
struct module *mod;
149146
struct klp_object *objs;
150-
bool immediate;
151147

152148
/* internal */
153149
struct list_head list;

kernel/livepatch/core.c

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,6 @@ static int __klp_enable_patch(struct klp_patch *patch)
366366
/*
367367
* A reference is taken on the patch module to prevent it from being
368368
* unloaded.
369-
*
370-
* Note: For immediate (no consistency model) patches we don't allow
371-
* patch modules to unload since there is no safe/sane method to
372-
* determine if a thread is still running in the patched code contained
373-
* in the patch module once the ftrace registration is successful.
374369
*/
375370
if (!try_module_get(patch->mod))
376371
return -ENODEV;
@@ -454,6 +449,8 @@ EXPORT_SYMBOL_GPL(klp_enable_patch);
454449
* /sys/kernel/livepatch/<patch>
455450
* /sys/kernel/livepatch/<patch>/enabled
456451
* /sys/kernel/livepatch/<patch>/transition
452+
* /sys/kernel/livepatch/<patch>/signal
453+
* /sys/kernel/livepatch/<patch>/force
457454
* /sys/kernel/livepatch/<patch>/<object>
458455
* /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
459456
*/
@@ -528,11 +525,73 @@ static ssize_t transition_show(struct kobject *kobj,
528525
patch == klp_transition_patch);
529526
}
530527

528+
static ssize_t signal_store(struct kobject *kobj, struct kobj_attribute *attr,
529+
const char *buf, size_t count)
530+
{
531+
struct klp_patch *patch;
532+
int ret;
533+
bool val;
534+
535+
ret = kstrtobool(buf, &val);
536+
if (ret)
537+
return ret;
538+
539+
if (!val)
540+
return count;
541+
542+
mutex_lock(&klp_mutex);
543+
544+
patch = container_of(kobj, struct klp_patch, kobj);
545+
if (patch != klp_transition_patch) {
546+
mutex_unlock(&klp_mutex);
547+
return -EINVAL;
548+
}
549+
550+
klp_send_signals();
551+
552+
mutex_unlock(&klp_mutex);
553+
554+
return count;
555+
}
556+
557+
static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
558+
const char *buf, size_t count)
559+
{
560+
struct klp_patch *patch;
561+
int ret;
562+
bool val;
563+
564+
ret = kstrtobool(buf, &val);
565+
if (ret)
566+
return ret;
567+
568+
if (!val)
569+
return count;
570+
571+
mutex_lock(&klp_mutex);
572+
573+
patch = container_of(kobj, struct klp_patch, kobj);
574+
if (patch != klp_transition_patch) {
575+
mutex_unlock(&klp_mutex);
576+
return -EINVAL;
577+
}
578+
579+
klp_force_transition();
580+
581+
mutex_unlock(&klp_mutex);
582+
583+
return count;
584+
}
585+
531586
static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
532587
static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
588+
static struct kobj_attribute signal_kobj_attr = __ATTR_WO(signal);
589+
static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
533590
static struct attribute *klp_patch_attrs[] = {
534591
&enabled_kobj_attr.attr,
535592
&transition_kobj_attr.attr,
593+
&signal_kobj_attr.attr,
594+
&force_kobj_attr.attr,
536595
NULL
537596
};
538597

@@ -830,12 +889,7 @@ int klp_register_patch(struct klp_patch *patch)
830889
if (!klp_initialized())
831890
return -ENODEV;
832891

833-
/*
834-
* Architectures without reliable stack traces have to set
835-
* patch->immediate because there's currently no way to patch kthreads
836-
* with the consistency model.
837-
*/
838-
if (!klp_have_reliable_stack() && !patch->immediate) {
892+
if (!klp_have_reliable_stack()) {
839893
pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
840894
return -ENOSYS;
841895
}

0 commit comments

Comments
 (0)