History

livepatch: allow removal of a disabled patch

Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code. The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe. We can safely let the patch module go after that. Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit 942e443127e9 ("module: Fix mod->mkobj.kobj potentially freed too early"). Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm. With this change a call to try_module_get() is moved to __klp_enable_patch from klp_register_patch to make module reference counting symmetric (module_put() is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled. Finally, we need to be very careful about possible races between klp_unregister_patch(), kobject_put() functions and operations on the related sysfs files. kobject_put(&patch->kobj) must be called without klp_mutex. Otherwise, it might be blocked by enabled_store() that needs the mutex as well. In addition, enabled_store() must check if the patch was not unregisted in the meantime. There is no need to do the same for other kobject_put() callsites at the moment. Their sysfs operations neither take the lock nor they access any data that might be freed in the meantime. There was an attempt to use kobjects the right way and prevent these races by design. But it made the patch definition more complicated and opened another can of worms. See https://lkml.kernel.org/r/1464018848-4303-1-git-send-email-pmladek@suse.com [Thanks to Petr Mladek for improving the commit message.]

Josh Poimboeuf committed 9 years ago

Commit 3ec24771 parent 7c23b335 files · +96 −53Browse files

Showing the changes to kernel/livepatch (2 of 5 files). Show all

Unified · Split

2 files
  1. kernel/livepatch/core.c +50 −30
  2. kernel/livepatch/transition.c +34 −3
kernel/livepatch/core.c+50−30
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929 #include <linux/livepatch.h>
3030 #include <linux/elf.h>
3131 #include <linux/moduleloader.h>
32+#include <linux/completion.h>
3233 #include <asm/cacheflush.h>
3334 #include "patch.h"
3435 #include "transition.h"
@@ -354,6 +355,18 @@ static int __klp_enable_patch(struct klp_patch *patch) · 319 unmodified lines
354355 !list_prev_entry(patch, list)->enabled)
355356 return -EBUSY;
356357
358+ /*
359+ * A reference is taken on the patch module to prevent it from being
360+ * unloaded.
361+ *
362+ * Note: For immediate (no consistency model) patches we don't allow
363+ * patch modules to unload since there is no safe/sane method to
364+ * determine if a thread is still running in the patched code contained
365+ * in the patch module once the ftrace registration is successful.
366+ */
367+ if (!try_module_get(patch->mod))
368+ return -ENODEV;
369+
357370 pr_notice("enabling patch '%s'\n", patch->mod->name);
358371
359372 klp_init_transition(patch, KLP_PATCHED);
@@ -442,6 +455,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, · 82 unmodified lines
442455
443456 mutex_lock(&klp_mutex);
444457
458+ if (!klp_is_patch_registered(patch)) {
459+ /*
460+ * Module with the patch could either disappear meanwhile or is
461+ * not properly initialized yet.
462+ */
463+ ret = -EINVAL;
464+ goto err;
465+ }
466+
445467 if (patch->enabled == enabled) {
446468 /* already in requested state */
447469 ret = -EINVAL;
@@ -498,10 +520,10 @@ static struct attribute *klp_patch_attrs[] = { · 50 unmodified lines
498520
499521 static void klp_kobj_release_patch(struct kobject *kobj)
500522 {
501− /*
502− * Once we have a consistency model we'll need to module_put() the
503− * patch module here. See klp_register_patch() for more details.
504− */
523+ struct klp_patch *patch;
524+
525+ patch = container_of(kobj, struct klp_patch, kobj);
526+ complete(&patch->finish);
505527 }
506528
507529 static struct kobj_type klp_ktype_patch = {
@@ -572,7 +594,6 @@ static void klp_free_patch(struct klp_patch *patch) · 64 unmodified lines
572594 klp_free_objects_limited(patch, NULL);
573595 if (!list_empty(&patch->list))
574596 list_del(&patch->list);
575− kobject_put(&patch->kobj);
576597 }
577598
578599 static int klp_init_func(struct klp_object *obj, struct klp_func *func)
@@ -695,11 +716,14 @@ static int klp_init_patch(struct klp_patch *patch) · 116 unmodified lines
695716 mutex_lock(&klp_mutex);
696717
697718 patch->enabled = false;
719+ init_completion(&patch->finish);
698720
699721 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
700722 klp_root_kobj, "%s", patch->mod->name);
701− if (ret)
702− goto unlock;
723+ if (ret) {
724+ mutex_unlock(&klp_mutex);
725+ return ret;
726+ }
703727
704728 klp_for_each_object(patch, obj) {
705729 ret = klp_init_object(patch, obj);
@@ -715,9 +739,12 @@ static int klp_init_patch(struct klp_patch *patch) · 9 unmodified lines
715739
716740 free:
717741 klp_free_objects_limited(patch, obj);
718− kobject_put(&patch->kobj);
719−unlock:
742+
720743 mutex_unlock(&klp_mutex);
744+
745+ kobject_put(&patch->kobj);
746+ wait_for_completion(&patch->finish);
747+
721748 return ret;
722749 }
723750
@@ -731,23 +758,29 @@ unlock: · 7 unmodified lines
731758 */
732759 int klp_unregister_patch(struct klp_patch *patch)
733760 {
734− int ret = 0;
761+ int ret;
735762
736763 mutex_lock(&klp_mutex);
737764
738765 if (!klp_is_patch_registered(patch)) {
739766 ret = -EINVAL;
740− goto out;
767+ goto err;
741768 }
742769
743770 if (patch->enabled) {
744771 ret = -EBUSY;
745− goto out;
772+ goto err;
746773 }
747774
748775 klp_free_patch(patch);
749776
750−out:
777+ mutex_unlock(&klp_mutex);
778+
779+ kobject_put(&patch->kobj);
780+ wait_for_completion(&patch->finish);
781+
782+ return 0;
783+err:
751784 mutex_unlock(&klp_mutex);
752785 return ret;
753786 }
@@ -760,12 +793,13 @@ EXPORT_SYMBOL_GPL(klp_unregister_patch); · 6 unmodified lines
760793 * Initializes the data structure associated with the patch and
761794 * creates the sysfs interface.
762795 *
796+ * There is no need to take the reference on the patch module here. It is done
797+ * later when the patch is enabled.
798+ *
763799 * Return: 0 on success, otherwise error
764800 */
765801 int klp_register_patch(struct klp_patch *patch)
766802 {
767− int ret;
768−
769803 if (!patch || !patch->mod)
770804 return -EINVAL;
771805
@@ -788,21 +822,7 @@ int klp_register_patch(struct klp_patch *patch) · 16 unmodified lines
788822 return -ENOSYS;
789823 }
790824
791− /*
792− * A reference is taken on the patch module to prevent it from being
793− * unloaded. Right now, we don't allow patch modules to unload since
794− * there is currently no method to determine if a thread is still
795− * running in the patched code contained in the patch module once
796− * the ftrace registration is successful.
797− */
798− if (!try_module_get(patch->mod))
799− return -ENODEV;
800−
801− ret = klp_init_patch(patch);
802− if (ret)
803− module_put(patch->mod);
804−
805− return ret;
825+ return klp_init_patch(patch);
806826 }
807827 EXPORT_SYMBOL_GPL(klp_register_patch);
808828
kernel/livepatch/transition.c+34−3
@@ -59,6 +59,7 @@ static void klp_complete_transition(void) · 58 unmodified lines
5959 struct klp_func *func;
6060 struct task_struct *g, *task;
6161 unsigned int cpu;
62+ bool immediate_func = false;
6263
6364 if (klp_target_state == KLP_UNPATCHED) {
6465 /*
@@ -79,9 +80,16 @@ static void klp_complete_transition(void) · 14 unmodified lines
7980 if (klp_transition_patch->immediate)
8081 goto done;
8182
82− klp_for_each_object(klp_transition_patch, obj)
83− klp_for_each_func(obj, func)
83+ klp_for_each_object(klp_transition_patch, obj) {
84+ klp_for_each_func(obj, func) {
8485 func->transition = false;
86+ if (func->immediate)
87+ immediate_func = true;
88+ }
89+ }
90+
91+ if (klp_target_state == KLP_UNPATCHED && !immediate_func)
92+ module_put(klp_transition_patch->mod);
8593
8694 /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */
8795 if (klp_target_state == KLP_PATCHED)
@@ -113,8 +121,31 @@ done: · 25 unmodified lines
113121 */
114122 void klp_cancel_transition(void)
115123 {
116− klp_target_state = !klp_target_state;
124+ struct klp_patch *patch = klp_transition_patch;
125+ struct klp_object *obj;
126+ struct klp_func *func;
127+ bool immediate_func = false;
128+
129+ if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED))
130+ return;
131+
132+ klp_target_state = KLP_UNPATCHED;
117133 klp_complete_transition();
134+
135+ /*
136+ * In the enable error path, even immediate patches can be safely
137+ * removed because the transition hasn't been started yet.
138+ *
139+ * klp_complete_transition() doesn't have a module_put() for immediate
140+ * patches, so do it here.
141+ */
142+ klp_for_each_object(patch, obj)
143+ klp_for_each_func(obj, func)
144+ if (func->immediate)
145+ immediate_func = true;
146+
147+ if (patch->immediate || immediate_func)
148+ module_put(patch->mod);
118149 }
119150
120151 /*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t