xref: /openbmc/linux/kernel/livepatch/core.c (revision 8c0b9ee8)
1 /*
2  * core.c - Kernel Live Patching Core
3  *
4  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5  * Copyright (C) 2014 SUSE
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/ftrace.h>
28 #include <linux/list.h>
29 #include <linux/kallsyms.h>
30 #include <linux/livepatch.h>
31 
32 /**
33  * struct klp_ops - structure for tracking registered ftrace ops structs
34  *
35  * A single ftrace_ops is shared between all enabled replacement functions
36  * (klp_func structs) which have the same old_addr.  This allows the switch
37  * between function versions to happen instantaneously by updating the klp_ops
38  * struct's func_stack list.  The winner is the klp_func at the top of the
39  * func_stack (front of the list).
40  *
41  * @node:	node for the global klp_ops list
42  * @func_stack:	list head for the stack of klp_func's (active func is on top)
43  * @fops:	registered ftrace ops struct
44  */
45 struct klp_ops {
46 	struct list_head node;
47 	struct list_head func_stack;
48 	struct ftrace_ops fops;
49 };
50 
51 /*
52  * The klp_mutex protects the global lists and state transitions of any
53  * structure reachable from them.  References to any structure must be obtained
54  * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
55  * ensure it gets consistent data).
56  */
57 static DEFINE_MUTEX(klp_mutex);
58 
59 static LIST_HEAD(klp_patches);
60 static LIST_HEAD(klp_ops);
61 
62 static struct kobject *klp_root_kobj;
63 
64 static struct klp_ops *klp_find_ops(unsigned long old_addr)
65 {
66 	struct klp_ops *ops;
67 	struct klp_func *func;
68 
69 	list_for_each_entry(ops, &klp_ops, node) {
70 		func = list_first_entry(&ops->func_stack, struct klp_func,
71 					stack_node);
72 		if (func->old_addr == old_addr)
73 			return ops;
74 	}
75 
76 	return NULL;
77 }
78 
79 static bool klp_is_module(struct klp_object *obj)
80 {
81 	return obj->name;
82 }
83 
84 static bool klp_is_object_loaded(struct klp_object *obj)
85 {
86 	return !obj->name || obj->mod;
87 }
88 
89 /* sets obj->mod if object is not vmlinux and module is found */
90 static void klp_find_object_module(struct klp_object *obj)
91 {
92 	struct module *mod;
93 
94 	if (!klp_is_module(obj))
95 		return;
96 
97 	mutex_lock(&module_mutex);
98 	/*
99 	 * We do not want to block removal of patched modules and therefore
100 	 * we do not take a reference here. The patches are removed by
101 	 * a going module handler instead.
102 	 */
103 	mod = find_module(obj->name);
104 	/*
105 	 * Do not mess work of the module coming and going notifiers.
106 	 * Note that the patch might still be needed before the going handler
107 	 * is called. Module functions can be called even in the GOING state
108 	 * until mod->exit() finishes. This is especially important for
109 	 * patches that modify semantic of the functions.
110 	 */
111 	if (mod && mod->klp_alive)
112 		obj->mod = mod;
113 
114 	mutex_unlock(&module_mutex);
115 }
116 
117 /* klp_mutex must be held by caller */
118 static bool klp_is_patch_registered(struct klp_patch *patch)
119 {
120 	struct klp_patch *mypatch;
121 
122 	list_for_each_entry(mypatch, &klp_patches, list)
123 		if (mypatch == patch)
124 			return true;
125 
126 	return false;
127 }
128 
129 static bool klp_initialized(void)
130 {
131 	return klp_root_kobj;
132 }
133 
134 struct klp_find_arg {
135 	const char *objname;
136 	const char *name;
137 	unsigned long addr;
138 	/*
139 	 * If count == 0, the symbol was not found. If count == 1, a unique
140 	 * match was found and addr is set.  If count > 1, there is
141 	 * unresolvable ambiguity among "count" number of symbols with the same
142 	 * name in the same object.
143 	 */
144 	unsigned long count;
145 };
146 
147 static int klp_find_callback(void *data, const char *name,
148 			     struct module *mod, unsigned long addr)
149 {
150 	struct klp_find_arg *args = data;
151 
152 	if ((mod && !args->objname) || (!mod && args->objname))
153 		return 0;
154 
155 	if (strcmp(args->name, name))
156 		return 0;
157 
158 	if (args->objname && strcmp(args->objname, mod->name))
159 		return 0;
160 
161 	/*
162 	 * args->addr might be overwritten if another match is found
163 	 * but klp_find_object_symbol() handles this and only returns the
164 	 * addr if count == 1.
165 	 */
166 	args->addr = addr;
167 	args->count++;
168 
169 	return 0;
170 }
171 
172 static int klp_find_object_symbol(const char *objname, const char *name,
173 				  unsigned long *addr)
174 {
175 	struct klp_find_arg args = {
176 		.objname = objname,
177 		.name = name,
178 		.addr = 0,
179 		.count = 0
180 	};
181 
182 	kallsyms_on_each_symbol(klp_find_callback, &args);
183 
184 	if (args.count == 0)
185 		pr_err("symbol '%s' not found in symbol table\n", name);
186 	else if (args.count > 1)
187 		pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
188 		       args.count, name, objname);
189 	else {
190 		*addr = args.addr;
191 		return 0;
192 	}
193 
194 	*addr = 0;
195 	return -EINVAL;
196 }
197 
198 struct klp_verify_args {
199 	const char *name;
200 	const unsigned long addr;
201 };
202 
203 static int klp_verify_callback(void *data, const char *name,
204 			       struct module *mod, unsigned long addr)
205 {
206 	struct klp_verify_args *args = data;
207 
208 	if (!mod &&
209 	    !strcmp(args->name, name) &&
210 	    args->addr == addr)
211 		return 1;
212 
213 	return 0;
214 }
215 
216 static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
217 {
218 	struct klp_verify_args args = {
219 		.name = name,
220 		.addr = addr,
221 	};
222 
223 	if (kallsyms_on_each_symbol(klp_verify_callback, &args))
224 		return 0;
225 
226 	pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?\n",
227 		name, addr);
228 	return -EINVAL;
229 }
230 
231 static int klp_find_verify_func_addr(struct klp_object *obj,
232 				     struct klp_func *func)
233 {
234 	int ret;
235 
236 #if defined(CONFIG_RANDOMIZE_BASE)
237 	/* KASLR is enabled, disregard old_addr from user */
238 	func->old_addr = 0;
239 #endif
240 
241 	if (!func->old_addr || klp_is_module(obj))
242 		ret = klp_find_object_symbol(obj->name, func->old_name,
243 					     &func->old_addr);
244 	else
245 		ret = klp_verify_vmlinux_symbol(func->old_name,
246 						func->old_addr);
247 
248 	return ret;
249 }
250 
251 /*
252  * external symbols are located outside the parent object (where the parent
253  * object is either vmlinux or the kmod being patched).
254  */
255 static int klp_find_external_symbol(struct module *pmod, const char *name,
256 				    unsigned long *addr)
257 {
258 	const struct kernel_symbol *sym;
259 
260 	/* first, check if it's an exported symbol */
261 	preempt_disable();
262 	sym = find_symbol(name, NULL, NULL, true, true);
263 	if (sym) {
264 		*addr = sym->value;
265 		preempt_enable();
266 		return 0;
267 	}
268 	preempt_enable();
269 
270 	/* otherwise check if it's in another .o within the patch module */
271 	return klp_find_object_symbol(pmod->name, name, addr);
272 }
273 
274 static int klp_write_object_relocations(struct module *pmod,
275 					struct klp_object *obj)
276 {
277 	int ret;
278 	struct klp_reloc *reloc;
279 
280 	if (WARN_ON(!klp_is_object_loaded(obj)))
281 		return -EINVAL;
282 
283 	if (WARN_ON(!obj->relocs))
284 		return -EINVAL;
285 
286 	for (reloc = obj->relocs; reloc->name; reloc++) {
287 		if (!klp_is_module(obj)) {
288 			ret = klp_verify_vmlinux_symbol(reloc->name,
289 							reloc->val);
290 			if (ret)
291 				return ret;
292 		} else {
293 			/* module, reloc->val needs to be discovered */
294 			if (reloc->external)
295 				ret = klp_find_external_symbol(pmod,
296 							       reloc->name,
297 							       &reloc->val);
298 			else
299 				ret = klp_find_object_symbol(obj->mod->name,
300 							     reloc->name,
301 							     &reloc->val);
302 			if (ret)
303 				return ret;
304 		}
305 		ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
306 					     reloc->val + reloc->addend);
307 		if (ret) {
308 			pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
309 			       reloc->name, reloc->val, ret);
310 			return ret;
311 		}
312 	}
313 
314 	return 0;
315 }
316 
317 static void notrace klp_ftrace_handler(unsigned long ip,
318 				       unsigned long parent_ip,
319 				       struct ftrace_ops *fops,
320 				       struct pt_regs *regs)
321 {
322 	struct klp_ops *ops;
323 	struct klp_func *func;
324 
325 	ops = container_of(fops, struct klp_ops, fops);
326 
327 	rcu_read_lock();
328 	func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
329 				      stack_node);
330 	if (WARN_ON_ONCE(!func))
331 		goto unlock;
332 
333 	klp_arch_set_pc(regs, (unsigned long)func->new_func);
334 unlock:
335 	rcu_read_unlock();
336 }
337 
338 static int klp_disable_func(struct klp_func *func)
339 {
340 	struct klp_ops *ops;
341 	int ret;
342 
343 	if (WARN_ON(func->state != KLP_ENABLED))
344 		return -EINVAL;
345 
346 	if (WARN_ON(!func->old_addr))
347 		return -EINVAL;
348 
349 	ops = klp_find_ops(func->old_addr);
350 	if (WARN_ON(!ops))
351 		return -EINVAL;
352 
353 	if (list_is_singular(&ops->func_stack)) {
354 		ret = unregister_ftrace_function(&ops->fops);
355 		if (ret) {
356 			pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
357 			       func->old_name, ret);
358 			return ret;
359 		}
360 
361 		ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
362 		if (ret)
363 			pr_warn("function unregister succeeded but failed to clear the filter\n");
364 
365 		list_del_rcu(&func->stack_node);
366 		list_del(&ops->node);
367 		kfree(ops);
368 	} else {
369 		list_del_rcu(&func->stack_node);
370 	}
371 
372 	func->state = KLP_DISABLED;
373 
374 	return 0;
375 }
376 
377 static int klp_enable_func(struct klp_func *func)
378 {
379 	struct klp_ops *ops;
380 	int ret;
381 
382 	if (WARN_ON(!func->old_addr))
383 		return -EINVAL;
384 
385 	if (WARN_ON(func->state != KLP_DISABLED))
386 		return -EINVAL;
387 
388 	ops = klp_find_ops(func->old_addr);
389 	if (!ops) {
390 		ops = kzalloc(sizeof(*ops), GFP_KERNEL);
391 		if (!ops)
392 			return -ENOMEM;
393 
394 		ops->fops.func = klp_ftrace_handler;
395 		ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
396 				  FTRACE_OPS_FL_DYNAMIC |
397 				  FTRACE_OPS_FL_IPMODIFY;
398 
399 		list_add(&ops->node, &klp_ops);
400 
401 		INIT_LIST_HEAD(&ops->func_stack);
402 		list_add_rcu(&func->stack_node, &ops->func_stack);
403 
404 		ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 0, 0);
405 		if (ret) {
406 			pr_err("failed to set ftrace filter for function '%s' (%d)\n",
407 			       func->old_name, ret);
408 			goto err;
409 		}
410 
411 		ret = register_ftrace_function(&ops->fops);
412 		if (ret) {
413 			pr_err("failed to register ftrace handler for function '%s' (%d)\n",
414 			       func->old_name, ret);
415 			ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
416 			goto err;
417 		}
418 
419 
420 	} else {
421 		list_add_rcu(&func->stack_node, &ops->func_stack);
422 	}
423 
424 	func->state = KLP_ENABLED;
425 
426 	return 0;
427 
428 err:
429 	list_del_rcu(&func->stack_node);
430 	list_del(&ops->node);
431 	kfree(ops);
432 	return ret;
433 }
434 
435 static int klp_disable_object(struct klp_object *obj)
436 {
437 	struct klp_func *func;
438 	int ret;
439 
440 	for (func = obj->funcs; func->old_name; func++) {
441 		if (func->state != KLP_ENABLED)
442 			continue;
443 
444 		ret = klp_disable_func(func);
445 		if (ret)
446 			return ret;
447 	}
448 
449 	obj->state = KLP_DISABLED;
450 
451 	return 0;
452 }
453 
454 static int klp_enable_object(struct klp_object *obj)
455 {
456 	struct klp_func *func;
457 	int ret;
458 
459 	if (WARN_ON(obj->state != KLP_DISABLED))
460 		return -EINVAL;
461 
462 	if (WARN_ON(!klp_is_object_loaded(obj)))
463 		return -EINVAL;
464 
465 	for (func = obj->funcs; func->old_name; func++) {
466 		ret = klp_enable_func(func);
467 		if (ret)
468 			goto unregister;
469 	}
470 	obj->state = KLP_ENABLED;
471 
472 	return 0;
473 
474 unregister:
475 	WARN_ON(klp_disable_object(obj));
476 	return ret;
477 }
478 
479 static int __klp_disable_patch(struct klp_patch *patch)
480 {
481 	struct klp_object *obj;
482 	int ret;
483 
484 	/* enforce stacking: only the last enabled patch can be disabled */
485 	if (!list_is_last(&patch->list, &klp_patches) &&
486 	    list_next_entry(patch, list)->state == KLP_ENABLED)
487 		return -EBUSY;
488 
489 	pr_notice("disabling patch '%s'\n", patch->mod->name);
490 
491 	for (obj = patch->objs; obj->funcs; obj++) {
492 		if (obj->state != KLP_ENABLED)
493 			continue;
494 
495 		ret = klp_disable_object(obj);
496 		if (ret)
497 			return ret;
498 	}
499 
500 	patch->state = KLP_DISABLED;
501 
502 	return 0;
503 }
504 
505 /**
506  * klp_disable_patch() - disables a registered patch
507  * @patch:	The registered, enabled patch to be disabled
508  *
509  * Unregisters the patched functions from ftrace.
510  *
511  * Return: 0 on success, otherwise error
512  */
513 int klp_disable_patch(struct klp_patch *patch)
514 {
515 	int ret;
516 
517 	mutex_lock(&klp_mutex);
518 
519 	if (!klp_is_patch_registered(patch)) {
520 		ret = -EINVAL;
521 		goto err;
522 	}
523 
524 	if (patch->state == KLP_DISABLED) {
525 		ret = -EINVAL;
526 		goto err;
527 	}
528 
529 	ret = __klp_disable_patch(patch);
530 
531 err:
532 	mutex_unlock(&klp_mutex);
533 	return ret;
534 }
535 EXPORT_SYMBOL_GPL(klp_disable_patch);
536 
537 static int __klp_enable_patch(struct klp_patch *patch)
538 {
539 	struct klp_object *obj;
540 	int ret;
541 
542 	if (WARN_ON(patch->state != KLP_DISABLED))
543 		return -EINVAL;
544 
545 	/* enforce stacking: only the first disabled patch can be enabled */
546 	if (patch->list.prev != &klp_patches &&
547 	    list_prev_entry(patch, list)->state == KLP_DISABLED)
548 		return -EBUSY;
549 
550 	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
551 	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
552 
553 	pr_notice("enabling patch '%s'\n", patch->mod->name);
554 
555 	for (obj = patch->objs; obj->funcs; obj++) {
556 		klp_find_object_module(obj);
557 
558 		if (!klp_is_object_loaded(obj))
559 			continue;
560 
561 		ret = klp_enable_object(obj);
562 		if (ret)
563 			goto unregister;
564 	}
565 
566 	patch->state = KLP_ENABLED;
567 
568 	return 0;
569 
570 unregister:
571 	WARN_ON(__klp_disable_patch(patch));
572 	return ret;
573 }
574 
575 /**
576  * klp_enable_patch() - enables a registered patch
577  * @patch:	The registered, disabled patch to be enabled
578  *
579  * Performs the needed symbol lookups and code relocations,
580  * then registers the patched functions with ftrace.
581  *
582  * Return: 0 on success, otherwise error
583  */
584 int klp_enable_patch(struct klp_patch *patch)
585 {
586 	int ret;
587 
588 	mutex_lock(&klp_mutex);
589 
590 	if (!klp_is_patch_registered(patch)) {
591 		ret = -EINVAL;
592 		goto err;
593 	}
594 
595 	ret = __klp_enable_patch(patch);
596 
597 err:
598 	mutex_unlock(&klp_mutex);
599 	return ret;
600 }
601 EXPORT_SYMBOL_GPL(klp_enable_patch);
602 
603 /*
604  * Sysfs Interface
605  *
606  * /sys/kernel/livepatch
607  * /sys/kernel/livepatch/<patch>
608  * /sys/kernel/livepatch/<patch>/enabled
609  * /sys/kernel/livepatch/<patch>/<object>
610  * /sys/kernel/livepatch/<patch>/<object>/<func>
611  */
612 
613 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
614 			     const char *buf, size_t count)
615 {
616 	struct klp_patch *patch;
617 	int ret;
618 	unsigned long val;
619 
620 	ret = kstrtoul(buf, 10, &val);
621 	if (ret)
622 		return -EINVAL;
623 
624 	if (val != KLP_DISABLED && val != KLP_ENABLED)
625 		return -EINVAL;
626 
627 	patch = container_of(kobj, struct klp_patch, kobj);
628 
629 	mutex_lock(&klp_mutex);
630 
631 	if (val == patch->state) {
632 		/* already in requested state */
633 		ret = -EINVAL;
634 		goto err;
635 	}
636 
637 	if (val == KLP_ENABLED) {
638 		ret = __klp_enable_patch(patch);
639 		if (ret)
640 			goto err;
641 	} else {
642 		ret = __klp_disable_patch(patch);
643 		if (ret)
644 			goto err;
645 	}
646 
647 	mutex_unlock(&klp_mutex);
648 
649 	return count;
650 
651 err:
652 	mutex_unlock(&klp_mutex);
653 	return ret;
654 }
655 
656 static ssize_t enabled_show(struct kobject *kobj,
657 			    struct kobj_attribute *attr, char *buf)
658 {
659 	struct klp_patch *patch;
660 
661 	patch = container_of(kobj, struct klp_patch, kobj);
662 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
663 }
664 
665 static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
666 static struct attribute *klp_patch_attrs[] = {
667 	&enabled_kobj_attr.attr,
668 	NULL
669 };
670 
671 static void klp_kobj_release_patch(struct kobject *kobj)
672 {
673 	/*
674 	 * Once we have a consistency model we'll need to module_put() the
675 	 * patch module here.  See klp_register_patch() for more details.
676 	 */
677 }
678 
679 static struct kobj_type klp_ktype_patch = {
680 	.release = klp_kobj_release_patch,
681 	.sysfs_ops = &kobj_sysfs_ops,
682 	.default_attrs = klp_patch_attrs,
683 };
684 
685 static void klp_kobj_release_func(struct kobject *kobj)
686 {
687 }
688 
689 static struct kobj_type klp_ktype_func = {
690 	.release = klp_kobj_release_func,
691 	.sysfs_ops = &kobj_sysfs_ops,
692 };
693 
694 /*
695  * Free all functions' kobjects in the array up to some limit. When limit is
696  * NULL, all kobjects are freed.
697  */
698 static void klp_free_funcs_limited(struct klp_object *obj,
699 				   struct klp_func *limit)
700 {
701 	struct klp_func *func;
702 
703 	for (func = obj->funcs; func->old_name && func != limit; func++)
704 		kobject_put(&func->kobj);
705 }
706 
707 /* Clean up when a patched object is unloaded */
708 static void klp_free_object_loaded(struct klp_object *obj)
709 {
710 	struct klp_func *func;
711 
712 	obj->mod = NULL;
713 
714 	for (func = obj->funcs; func->old_name; func++)
715 		func->old_addr = 0;
716 }
717 
718 /*
719  * Free all objects' kobjects in the array up to some limit. When limit is
720  * NULL, all kobjects are freed.
721  */
722 static void klp_free_objects_limited(struct klp_patch *patch,
723 				     struct klp_object *limit)
724 {
725 	struct klp_object *obj;
726 
727 	for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
728 		klp_free_funcs_limited(obj, NULL);
729 		kobject_put(obj->kobj);
730 	}
731 }
732 
733 static void klp_free_patch(struct klp_patch *patch)
734 {
735 	klp_free_objects_limited(patch, NULL);
736 	if (!list_empty(&patch->list))
737 		list_del(&patch->list);
738 	kobject_put(&patch->kobj);
739 }
740 
741 static int klp_init_func(struct klp_object *obj, struct klp_func *func)
742 {
743 	INIT_LIST_HEAD(&func->stack_node);
744 	func->state = KLP_DISABLED;
745 
746 	return kobject_init_and_add(&func->kobj, &klp_ktype_func,
747 				    obj->kobj, "%s", func->old_name);
748 }
749 
750 /* parts of the initialization that is done only when the object is loaded */
751 static int klp_init_object_loaded(struct klp_patch *patch,
752 				  struct klp_object *obj)
753 {
754 	struct klp_func *func;
755 	int ret;
756 
757 	if (obj->relocs) {
758 		ret = klp_write_object_relocations(patch->mod, obj);
759 		if (ret)
760 			return ret;
761 	}
762 
763 	for (func = obj->funcs; func->old_name; func++) {
764 		ret = klp_find_verify_func_addr(obj, func);
765 		if (ret)
766 			return ret;
767 	}
768 
769 	return 0;
770 }
771 
772 static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
773 {
774 	struct klp_func *func;
775 	int ret;
776 	const char *name;
777 
778 	if (!obj->funcs)
779 		return -EINVAL;
780 
781 	obj->state = KLP_DISABLED;
782 	obj->mod = NULL;
783 
784 	klp_find_object_module(obj);
785 
786 	name = klp_is_module(obj) ? obj->name : "vmlinux";
787 	obj->kobj = kobject_create_and_add(name, &patch->kobj);
788 	if (!obj->kobj)
789 		return -ENOMEM;
790 
791 	for (func = obj->funcs; func->old_name; func++) {
792 		ret = klp_init_func(obj, func);
793 		if (ret)
794 			goto free;
795 	}
796 
797 	if (klp_is_object_loaded(obj)) {
798 		ret = klp_init_object_loaded(patch, obj);
799 		if (ret)
800 			goto free;
801 	}
802 
803 	return 0;
804 
805 free:
806 	klp_free_funcs_limited(obj, func);
807 	kobject_put(obj->kobj);
808 	return ret;
809 }
810 
811 static int klp_init_patch(struct klp_patch *patch)
812 {
813 	struct klp_object *obj;
814 	int ret;
815 
816 	if (!patch->objs)
817 		return -EINVAL;
818 
819 	mutex_lock(&klp_mutex);
820 
821 	patch->state = KLP_DISABLED;
822 
823 	ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
824 				   klp_root_kobj, "%s", patch->mod->name);
825 	if (ret)
826 		goto unlock;
827 
828 	for (obj = patch->objs; obj->funcs; obj++) {
829 		ret = klp_init_object(patch, obj);
830 		if (ret)
831 			goto free;
832 	}
833 
834 	list_add_tail(&patch->list, &klp_patches);
835 
836 	mutex_unlock(&klp_mutex);
837 
838 	return 0;
839 
840 free:
841 	klp_free_objects_limited(patch, obj);
842 	kobject_put(&patch->kobj);
843 unlock:
844 	mutex_unlock(&klp_mutex);
845 	return ret;
846 }
847 
848 /**
849  * klp_unregister_patch() - unregisters a patch
850  * @patch:	Disabled patch to be unregistered
851  *
852  * Frees the data structures and removes the sysfs interface.
853  *
854  * Return: 0 on success, otherwise error
855  */
856 int klp_unregister_patch(struct klp_patch *patch)
857 {
858 	int ret = 0;
859 
860 	mutex_lock(&klp_mutex);
861 
862 	if (!klp_is_patch_registered(patch)) {
863 		ret = -EINVAL;
864 		goto out;
865 	}
866 
867 	if (patch->state == KLP_ENABLED) {
868 		ret = -EBUSY;
869 		goto out;
870 	}
871 
872 	klp_free_patch(patch);
873 
874 out:
875 	mutex_unlock(&klp_mutex);
876 	return ret;
877 }
878 EXPORT_SYMBOL_GPL(klp_unregister_patch);
879 
880 /**
881  * klp_register_patch() - registers a patch
882  * @patch:	Patch to be registered
883  *
884  * Initializes the data structure associated with the patch and
885  * creates the sysfs interface.
886  *
887  * Return: 0 on success, otherwise error
888  */
889 int klp_register_patch(struct klp_patch *patch)
890 {
891 	int ret;
892 
893 	if (!klp_initialized())
894 		return -ENODEV;
895 
896 	if (!patch || !patch->mod)
897 		return -EINVAL;
898 
899 	/*
900 	 * A reference is taken on the patch module to prevent it from being
901 	 * unloaded.  Right now, we don't allow patch modules to unload since
902 	 * there is currently no method to determine if a thread is still
903 	 * running in the patched code contained in the patch module once
904 	 * the ftrace registration is successful.
905 	 */
906 	if (!try_module_get(patch->mod))
907 		return -ENODEV;
908 
909 	ret = klp_init_patch(patch);
910 	if (ret)
911 		module_put(patch->mod);
912 
913 	return ret;
914 }
915 EXPORT_SYMBOL_GPL(klp_register_patch);
916 
917 static void klp_module_notify_coming(struct klp_patch *patch,
918 				     struct klp_object *obj)
919 {
920 	struct module *pmod = patch->mod;
921 	struct module *mod = obj->mod;
922 	int ret;
923 
924 	ret = klp_init_object_loaded(patch, obj);
925 	if (ret)
926 		goto err;
927 
928 	if (patch->state == KLP_DISABLED)
929 		return;
930 
931 	pr_notice("applying patch '%s' to loading module '%s'\n",
932 		  pmod->name, mod->name);
933 
934 	ret = klp_enable_object(obj);
935 	if (!ret)
936 		return;
937 
938 err:
939 	pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
940 		pmod->name, mod->name, ret);
941 }
942 
943 static void klp_module_notify_going(struct klp_patch *patch,
944 				    struct klp_object *obj)
945 {
946 	struct module *pmod = patch->mod;
947 	struct module *mod = obj->mod;
948 	int ret;
949 
950 	if (patch->state == KLP_DISABLED)
951 		goto disabled;
952 
953 	pr_notice("reverting patch '%s' on unloading module '%s'\n",
954 		  pmod->name, mod->name);
955 
956 	ret = klp_disable_object(obj);
957 	if (ret)
958 		pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
959 			pmod->name, mod->name, ret);
960 
961 disabled:
962 	klp_free_object_loaded(obj);
963 }
964 
965 static int klp_module_notify(struct notifier_block *nb, unsigned long action,
966 			     void *data)
967 {
968 	struct module *mod = data;
969 	struct klp_patch *patch;
970 	struct klp_object *obj;
971 
972 	if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
973 		return 0;
974 
975 	mutex_lock(&klp_mutex);
976 
977 	/*
978 	 * Each module has to know that the notifier has been called.
979 	 * We never know what module will get patched by a new patch.
980 	 */
981 	if (action == MODULE_STATE_COMING)
982 		mod->klp_alive = true;
983 	else /* MODULE_STATE_GOING */
984 		mod->klp_alive = false;
985 
986 	list_for_each_entry(patch, &klp_patches, list) {
987 		for (obj = patch->objs; obj->funcs; obj++) {
988 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
989 				continue;
990 
991 			if (action == MODULE_STATE_COMING) {
992 				obj->mod = mod;
993 				klp_module_notify_coming(patch, obj);
994 			} else /* MODULE_STATE_GOING */
995 				klp_module_notify_going(patch, obj);
996 
997 			break;
998 		}
999 	}
1000 
1001 	mutex_unlock(&klp_mutex);
1002 
1003 	return 0;
1004 }
1005 
1006 static struct notifier_block klp_module_nb = {
1007 	.notifier_call = klp_module_notify,
1008 	.priority = INT_MIN+1, /* called late but before ftrace notifier */
1009 };
1010 
1011 static int klp_init(void)
1012 {
1013 	int ret;
1014 
1015 	ret = klp_check_compiler_support();
1016 	if (ret) {
1017 		pr_info("Your compiler is too old; turning off.\n");
1018 		return -EINVAL;
1019 	}
1020 
1021 	ret = register_module_notifier(&klp_module_nb);
1022 	if (ret)
1023 		return ret;
1024 
1025 	klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
1026 	if (!klp_root_kobj) {
1027 		ret = -ENOMEM;
1028 		goto unregister;
1029 	}
1030 
1031 	return 0;
1032 
1033 unregister:
1034 	unregister_module_notifier(&klp_module_nb);
1035 	return ret;
1036 }
1037 
1038 module_init(klp_init);
1039