Skip to content

Commit e1452b6

Browse files
almostivanJiri Kosina
authored andcommitted
livepatch: Add atomic replace
Sometimes we would like to revert a particular fix. Currently, this is not easy because we want to keep all other fixes active and we could revert only the last applied patch. One solution would be to apply new patch that implemented all the reverted functions like in the original code. It would work as expected but there will be unnecessary redirections. In addition, it would also require knowing which functions need to be reverted at build time. Another problem is when there are many patches that touch the same functions. There might be dependencies between patches that are not enforced on the kernel side. Also it might be pretty hard to actually prepare the patch and ensure compatibility with the other patches. Atomic replace && cumulative patches: A better solution would be to create cumulative patch and say that it replaces all older ones. This patch adds a new "replace" flag to struct klp_patch. When it is enabled, a set of 'nop' klp_func will be dynamically created for all functions that are already being patched but that will no longer be modified by the new patch. They are used as a new target during the patch transition. The idea is to handle Nops' structures like the static ones. When the dynamic structures are allocated, we initialize all values that are normally statically defined. The only exception is "new_func" in struct klp_func. It has to point to the original function and the address is known only when the object (module) is loaded. Note that we really need to set it. The address is used, for example, in klp_check_stack_func(). Nevertheless we still need to distinguish the dynamically allocated structures in some operations. For this, we add "nop" flag into struct klp_func and "dynamic" flag into struct klp_object. They need special handling in the following situations: + The structures are added into the lists of objects and functions immediately. In fact, the lists were created for this purpose. + The address of the original function is known only when the patched object (module) is loaded. Therefore it is copied later in klp_init_object_loaded(). + The ftrace handler must not set PC to func->new_func. It would cause infinite loop because the address points back to the beginning of the original function. + The various free() functions must free the structure itself. Note that other ways to detect the dynamic structures are not considered safe. For example, even the statically defined struct klp_object might include empty funcs array. It might be there just to run some callbacks. Also note that the safe iterator must be used in the free() functions. Otherwise already freed structures might get accessed. Special callbacks handling: The callbacks from the replaced patches are _not_ called by intention. It would be pretty hard to define a reasonable semantic and implement it. It might even be counter-productive. The new patch is cumulative. It is supposed to include most of the changes from older patches. In most cases, it will not want to call pre_unpatch() post_unpatch() callbacks from the replaced patches. It would disable/break things for no good reasons. Also it should be easier to handle various scenarios in a single script in the new patch than think about interactions caused by running many scripts from older patches. Not to say that the old scripts even would not expect to be called in this situation. Removing replaced patches: One nice effect of the cumulative patches is that the code from the older patches is no longer used. Therefore the replaced patches can be removed. It has several advantages: + Nops' structs will no longer be necessary and might be removed. This would save memory, restore performance (no ftrace handler), allow clear view on what is really patched. + Disabling the patch will cause using the original code everywhere. Therefore the livepatch callbacks could handle only one scenario. Note that the complication is already complex enough when the patch gets enabled. It is currently solved by calling callbacks only from the new cumulative patch. + The state is clean in both the sysfs interface and lsmod. The modules with the replaced livepatches might even get removed from the system. Some people actually expected this behavior from the beginning. After all a cumulative patch is supposed to "completely" replace an existing one. It is like when a new version of an application replaces an older one. This patch does the first step. It removes the replaced patches from the list of patches. It is safe. The consistency model ensures that they are no longer used. By other words, each process works only with the structures from klp_transition_patch. The removal is done by a special function. It combines actions done by __disable_patch() and klp_complete_transition(). But it is a fast track without all the transaction-related stuff. Signed-off-by: Jason Baron <[email protected]> [[email protected]: Split, reuse existing code, simplified] Signed-off-by: Petr Mladek <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Jessica Yu <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Miroslav Benes <[email protected]> Acked-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 20e5502 commit e1452b6

File tree

6 files changed

+273
-14
lines changed

6 files changed

+273
-14
lines changed

Documentation/livepatch/livepatch.txt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Table of Contents:
1515
5. Livepatch life-cycle
1616
5.1. Loading
1717
5.2. Enabling
18-
5.3. Disabling
19-
5.4. Removing
18+
5.3. Replacing
19+
5.4. Disabling
20+
5.5. Removing
2021
6. Sysfs
2122
7. Limitations
2223

@@ -300,8 +301,12 @@ into three levels:
300301
5. Livepatch life-cycle
301302
=======================
302303

303-
Livepatching can be described by four basic operations:
304-
loading, enabling, disabling, removing.
304+
Livepatching can be described by five basic operations:
305+
loading, enabling, replacing, disabling, removing.
306+
307+
Where the replacing and the disabling operations are mutually
308+
exclusive. They have the same result for the given patch but
309+
not for the system.
305310

306311

307312
5.1. Loading
@@ -347,7 +352,21 @@ to '0'.
347352
the "Consistency model" section.
348353

349354

350-
5.3. Disabling
355+
5.3. Replacing
356+
--------------
357+
358+
All enabled patches might get replaced by a cumulative patch that
359+
has the .replace flag set.
360+
361+
Once the new patch is enabled and the 'transition' finishes then
362+
all the functions (struct klp_func) associated with the replaced
363+
patches are removed from the corresponding struct klp_ops. Also
364+
the ftrace handler is unregistered and the struct klp_ops is
365+
freed when the related function is not modified by the new patch
366+
and func_stack list becomes empty.
367+
368+
369+
5.4. Disabling
351370
--------------
352371

353372
Enabled patches might get disabled by writing '0' to
@@ -372,7 +391,7 @@ Note that patches must be disabled in exactly the reverse order in which
372391
they were enabled. It makes the problem and the implementation much easier.
373392

374393

375-
5.4. Removing
394+
5.5. Removing
376395
-------------
377396

378397
Module removal is only safe when there are no users of functions provided

include/linux/livepatch.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @old_size: size of the old function
4949
* @new_size: size of the new function
5050
* @kobj_added: @kobj has been added and needs freeing
51+
* @nop: temporary patch to use the original code again; dyn. allocated
5152
* @patched: the func has been added to the klp_ops list
5253
* @transition: the func is currently being applied or reverted
5354
*
@@ -86,6 +87,7 @@ struct klp_func {
8687
struct list_head stack_node;
8788
unsigned long old_size, new_size;
8889
bool kobj_added;
90+
bool nop;
8991
bool patched;
9092
bool transition;
9193
};
@@ -125,6 +127,7 @@ struct klp_callbacks {
125127
* @mod: kernel module associated with the patched object
126128
* (NULL for vmlinux)
127129
* @kobj_added: @kobj has been added and needs freeing
130+
* @dynamic: temporary object for nop functions; dynamically allocated
128131
* @patched: the object's funcs have been added to the klp_ops list
129132
*/
130133
struct klp_object {
@@ -139,13 +142,15 @@ struct klp_object {
139142
struct list_head node;
140143
struct module *mod;
141144
bool kobj_added;
145+
bool dynamic;
142146
bool patched;
143147
};
144148

145149
/**
146150
* struct klp_patch - patch structure for live patching
147151
* @mod: reference to the live patch module
148152
* @objs: object entries for kernel objects to be patched
153+
* @replace: replace all actively used patches
149154
* @list: list node for global list of actively used patches
150155
* @kobj: kobject for sysfs resources
151156
* @obj_list: dynamic list of the object entries
@@ -159,6 +164,7 @@ struct klp_patch {
159164
/* external */
160165
struct module *mod;
161166
struct klp_object *objs;
167+
bool replace;
162168

163169
/* internal */
164170
struct list_head list;
@@ -174,6 +180,9 @@ struct klp_patch {
174180
#define klp_for_each_object_static(patch, obj) \
175181
for (obj = patch->objs; obj->funcs || obj->name; obj++)
176182

183+
#define klp_for_each_object_safe(patch, obj, tmp_obj) \
184+
list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node)
185+
177186
#define klp_for_each_object(patch, obj) \
178187
list_for_each_entry(obj, &patch->obj_list, node)
179188

@@ -182,6 +191,9 @@ struct klp_patch {
182191
func->old_name || func->new_func || func->old_sympos; \
183192
func++)
184193

194+
#define klp_for_each_func_safe(obj, func, tmp_func) \
195+
list_for_each_entry_safe(func, tmp_func, &obj->func_list, node)
196+
185197
#define klp_for_each_func(obj, func) \
186198
list_for_each_entry(func, &obj->func_list, node)
187199

0 commit comments

Comments
 (0)