blob: 26df09d56f7cc351196257c4408c749ecddcc548 [file] [log] [blame]
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001/*
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
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060032/**
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
Seth Jenningsb700e7f2014-12-16 11:58:19 -060044 */
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060045struct klp_ops {
46 struct list_head node;
47 struct list_head func_stack;
48 struct ftrace_ops fops;
49};
Seth Jenningsb700e7f2014-12-16 11:58:19 -060050
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060051/*
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 */
Seth Jenningsb700e7f2014-12-16 11:58:19 -060057static DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060058
Seth Jenningsb700e7f2014-12-16 11:58:19 -060059static LIST_HEAD(klp_patches);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060060static LIST_HEAD(klp_ops);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060061
62static struct kobject *klp_root_kobj;
63
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060064static 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
Seth Jenningsb700e7f2014-12-16 11:58:19 -060079static bool klp_is_module(struct klp_object *obj)
80{
81 return obj->name;
82}
83
84static 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 */
90static void klp_find_object_module(struct klp_object *obj)
91{
92 if (!klp_is_module(obj))
93 return;
94
95 mutex_lock(&module_mutex);
96 /*
97 * We don't need to take a reference on the module here because we have
98 * the klp_mutex, which is also taken by the module notifier. This
99 * prevents any module from unloading until we release the klp_mutex.
100 */
101 obj->mod = find_module(obj->name);
102 mutex_unlock(&module_mutex);
103}
104
105/* klp_mutex must be held by caller */
106static bool klp_is_patch_registered(struct klp_patch *patch)
107{
108 struct klp_patch *mypatch;
109
110 list_for_each_entry(mypatch, &klp_patches, list)
111 if (mypatch == patch)
112 return true;
113
114 return false;
115}
116
117static bool klp_initialized(void)
118{
119 return klp_root_kobj;
120}
121
122struct klp_find_arg {
123 const char *objname;
124 const char *name;
125 unsigned long addr;
126 /*
127 * If count == 0, the symbol was not found. If count == 1, a unique
128 * match was found and addr is set. If count > 1, there is
129 * unresolvable ambiguity among "count" number of symbols with the same
130 * name in the same object.
131 */
132 unsigned long count;
133};
134
135static int klp_find_callback(void *data, const char *name,
136 struct module *mod, unsigned long addr)
137{
138 struct klp_find_arg *args = data;
139
140 if ((mod && !args->objname) || (!mod && args->objname))
141 return 0;
142
143 if (strcmp(args->name, name))
144 return 0;
145
146 if (args->objname && strcmp(args->objname, mod->name))
147 return 0;
148
149 /*
150 * args->addr might be overwritten if another match is found
151 * but klp_find_object_symbol() handles this and only returns the
152 * addr if count == 1.
153 */
154 args->addr = addr;
155 args->count++;
156
157 return 0;
158}
159
160static int klp_find_object_symbol(const char *objname, const char *name,
161 unsigned long *addr)
162{
163 struct klp_find_arg args = {
164 .objname = objname,
165 .name = name,
166 .addr = 0,
167 .count = 0
168 };
169
170 kallsyms_on_each_symbol(klp_find_callback, &args);
171
172 if (args.count == 0)
173 pr_err("symbol '%s' not found in symbol table\n", name);
174 else if (args.count > 1)
175 pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
176 args.count, name, objname);
177 else {
178 *addr = args.addr;
179 return 0;
180 }
181
182 *addr = 0;
183 return -EINVAL;
184}
185
186struct klp_verify_args {
187 const char *name;
188 const unsigned long addr;
189};
190
191static int klp_verify_callback(void *data, const char *name,
192 struct module *mod, unsigned long addr)
193{
194 struct klp_verify_args *args = data;
195
196 if (!mod &&
197 !strcmp(args->name, name) &&
198 args->addr == addr)
199 return 1;
200
201 return 0;
202}
203
204static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
205{
206 struct klp_verify_args args = {
207 .name = name,
208 .addr = addr,
209 };
210
211 if (kallsyms_on_each_symbol(klp_verify_callback, &args))
212 return 0;
213
Josh Poimboeuff638f4d2015-02-06 10:36:32 -0600214 pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?\n",
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600215 name, addr);
216 return -EINVAL;
217}
218
219static int klp_find_verify_func_addr(struct klp_object *obj,
220 struct klp_func *func)
221{
222 int ret;
223
224#if defined(CONFIG_RANDOMIZE_BASE)
225 /* KASLR is enabled, disregard old_addr from user */
226 func->old_addr = 0;
227#endif
228
229 if (!func->old_addr || klp_is_module(obj))
230 ret = klp_find_object_symbol(obj->name, func->old_name,
231 &func->old_addr);
232 else
233 ret = klp_verify_vmlinux_symbol(func->old_name,
234 func->old_addr);
235
236 return ret;
237}
238
239/*
240 * external symbols are located outside the parent object (where the parent
241 * object is either vmlinux or the kmod being patched).
242 */
243static int klp_find_external_symbol(struct module *pmod, const char *name,
244 unsigned long *addr)
245{
246 const struct kernel_symbol *sym;
247
248 /* first, check if it's an exported symbol */
249 preempt_disable();
250 sym = find_symbol(name, NULL, NULL, true, true);
251 preempt_enable();
252 if (sym) {
253 *addr = sym->value;
254 return 0;
255 }
256
257 /* otherwise check if it's in another .o within the patch module */
258 return klp_find_object_symbol(pmod->name, name, addr);
259}
260
261static int klp_write_object_relocations(struct module *pmod,
262 struct klp_object *obj)
263{
264 int ret;
265 struct klp_reloc *reloc;
266
267 if (WARN_ON(!klp_is_object_loaded(obj)))
268 return -EINVAL;
269
270 if (WARN_ON(!obj->relocs))
271 return -EINVAL;
272
273 for (reloc = obj->relocs; reloc->name; reloc++) {
274 if (!klp_is_module(obj)) {
275 ret = klp_verify_vmlinux_symbol(reloc->name,
276 reloc->val);
277 if (ret)
278 return ret;
279 } else {
280 /* module, reloc->val needs to be discovered */
281 if (reloc->external)
282 ret = klp_find_external_symbol(pmod,
283 reloc->name,
284 &reloc->val);
285 else
286 ret = klp_find_object_symbol(obj->mod->name,
287 reloc->name,
288 &reloc->val);
289 if (ret)
290 return ret;
291 }
292 ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
293 reloc->val + reloc->addend);
294 if (ret) {
295 pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
296 reloc->name, reloc->val, ret);
297 return ret;
298 }
299 }
300
301 return 0;
302}
303
304static void notrace klp_ftrace_handler(unsigned long ip,
305 unsigned long parent_ip,
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600306 struct ftrace_ops *fops,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600307 struct pt_regs *regs)
308{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600309 struct klp_ops *ops;
310 struct klp_func *func;
311
312 ops = container_of(fops, struct klp_ops, fops);
313
314 rcu_read_lock();
315 func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
316 stack_node);
317 rcu_read_unlock();
318
319 if (WARN_ON_ONCE(!func))
320 return;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600321
Li Binb5bfc512014-12-19 14:11:17 +0800322 klp_arch_set_pc(regs, (unsigned long)func->new_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600323}
324
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600325static void klp_disable_func(struct klp_func *func)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600326{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600327 struct klp_ops *ops;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600328
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600329 WARN_ON(func->state != KLP_ENABLED);
330 WARN_ON(!func->old_addr);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600331
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600332 ops = klp_find_ops(func->old_addr);
333 if (WARN_ON(!ops))
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600334 return;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600335
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600336 if (list_is_singular(&ops->func_stack)) {
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600337 WARN_ON(unregister_ftrace_function(&ops->fops));
338 WARN_ON(ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0));
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600339
340 list_del_rcu(&func->stack_node);
341 list_del(&ops->node);
342 kfree(ops);
343 } else {
344 list_del_rcu(&func->stack_node);
345 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600346
347 func->state = KLP_DISABLED;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600348}
349
350static int klp_enable_func(struct klp_func *func)
351{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600352 struct klp_ops *ops;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600353 int ret;
354
355 if (WARN_ON(!func->old_addr))
356 return -EINVAL;
357
358 if (WARN_ON(func->state != KLP_DISABLED))
359 return -EINVAL;
360
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600361 ops = klp_find_ops(func->old_addr);
362 if (!ops) {
363 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
364 if (!ops)
365 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600366
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600367 ops->fops.func = klp_ftrace_handler;
368 ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
369 FTRACE_OPS_FL_DYNAMIC |
370 FTRACE_OPS_FL_IPMODIFY;
371
372 list_add(&ops->node, &klp_ops);
373
374 INIT_LIST_HEAD(&ops->func_stack);
375 list_add_rcu(&func->stack_node, &ops->func_stack);
376
377 ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 0, 0);
378 if (ret) {
379 pr_err("failed to set ftrace filter for function '%s' (%d)\n",
380 func->old_name, ret);
381 goto err;
382 }
383
384 ret = register_ftrace_function(&ops->fops);
385 if (ret) {
386 pr_err("failed to register ftrace handler for function '%s' (%d)\n",
387 func->old_name, ret);
388 ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
389 goto err;
390 }
391
392
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600393 } else {
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600394 list_add_rcu(&func->stack_node, &ops->func_stack);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600395 }
396
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600397 func->state = KLP_ENABLED;
398
Josh Poimboeufdbed7dd2015-01-20 16:07:55 -0600399 return 0;
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600400
401err:
402 list_del_rcu(&func->stack_node);
403 list_del(&ops->node);
404 kfree(ops);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600405 return ret;
406}
407
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600408static void klp_disable_object(struct klp_object *obj)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600409{
410 struct klp_func *func;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600411
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600412 for (func = obj->funcs; func->old_name; func++)
413 if (func->state == KLP_ENABLED)
414 klp_disable_func(func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600415
416 obj->state = KLP_DISABLED;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600417}
418
419static int klp_enable_object(struct klp_object *obj)
420{
421 struct klp_func *func;
422 int ret;
423
424 if (WARN_ON(obj->state != KLP_DISABLED))
425 return -EINVAL;
426
427 if (WARN_ON(!klp_is_object_loaded(obj)))
428 return -EINVAL;
429
430 for (func = obj->funcs; func->old_name; func++) {
431 ret = klp_enable_func(func);
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600432 if (ret) {
433 klp_disable_object(obj);
434 return ret;
435 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600436 }
437 obj->state = KLP_ENABLED;
438
439 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600440}
441
442static int __klp_disable_patch(struct klp_patch *patch)
443{
444 struct klp_object *obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600445
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600446 /* enforce stacking: only the last enabled patch can be disabled */
447 if (!list_is_last(&patch->list, &klp_patches) &&
448 list_next_entry(patch, list)->state == KLP_ENABLED)
449 return -EBUSY;
450
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600451 pr_notice("disabling patch '%s'\n", patch->mod->name);
452
453 for (obj = patch->objs; obj->funcs; obj++) {
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600454 if (obj->state == KLP_ENABLED)
455 klp_disable_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600456 }
457
458 patch->state = KLP_DISABLED;
459
460 return 0;
461}
462
463/**
464 * klp_disable_patch() - disables a registered patch
465 * @patch: The registered, enabled patch to be disabled
466 *
467 * Unregisters the patched functions from ftrace.
468 *
469 * Return: 0 on success, otherwise error
470 */
471int klp_disable_patch(struct klp_patch *patch)
472{
473 int ret;
474
475 mutex_lock(&klp_mutex);
476
477 if (!klp_is_patch_registered(patch)) {
478 ret = -EINVAL;
479 goto err;
480 }
481
482 if (patch->state == KLP_DISABLED) {
483 ret = -EINVAL;
484 goto err;
485 }
486
487 ret = __klp_disable_patch(patch);
488
489err:
490 mutex_unlock(&klp_mutex);
491 return ret;
492}
493EXPORT_SYMBOL_GPL(klp_disable_patch);
494
495static int __klp_enable_patch(struct klp_patch *patch)
496{
497 struct klp_object *obj;
498 int ret;
499
500 if (WARN_ON(patch->state != KLP_DISABLED))
501 return -EINVAL;
502
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600503 /* enforce stacking: only the first disabled patch can be enabled */
504 if (patch->list.prev != &klp_patches &&
505 list_prev_entry(patch, list)->state == KLP_DISABLED)
506 return -EBUSY;
507
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600508 pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
509 add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
510
511 pr_notice("enabling patch '%s'\n", patch->mod->name);
512
513 for (obj = patch->objs; obj->funcs; obj++) {
514 klp_find_object_module(obj);
515
516 if (!klp_is_object_loaded(obj))
517 continue;
518
519 ret = klp_enable_object(obj);
520 if (ret)
521 goto unregister;
522 }
523
524 patch->state = KLP_ENABLED;
525
526 return 0;
527
528unregister:
529 WARN_ON(__klp_disable_patch(patch));
530 return ret;
531}
532
533/**
534 * klp_enable_patch() - enables a registered patch
535 * @patch: The registered, disabled patch to be enabled
536 *
537 * Performs the needed symbol lookups and code relocations,
538 * then registers the patched functions with ftrace.
539 *
540 * Return: 0 on success, otherwise error
541 */
542int klp_enable_patch(struct klp_patch *patch)
543{
544 int ret;
545
546 mutex_lock(&klp_mutex);
547
548 if (!klp_is_patch_registered(patch)) {
549 ret = -EINVAL;
550 goto err;
551 }
552
553 ret = __klp_enable_patch(patch);
554
555err:
556 mutex_unlock(&klp_mutex);
557 return ret;
558}
559EXPORT_SYMBOL_GPL(klp_enable_patch);
560
561/*
562 * Sysfs Interface
563 *
564 * /sys/kernel/livepatch
565 * /sys/kernel/livepatch/<patch>
566 * /sys/kernel/livepatch/<patch>/enabled
567 * /sys/kernel/livepatch/<patch>/<object>
568 * /sys/kernel/livepatch/<patch>/<object>/<func>
569 */
570
571static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
572 const char *buf, size_t count)
573{
574 struct klp_patch *patch;
575 int ret;
576 unsigned long val;
577
578 ret = kstrtoul(buf, 10, &val);
579 if (ret)
580 return -EINVAL;
581
582 if (val != KLP_DISABLED && val != KLP_ENABLED)
583 return -EINVAL;
584
585 patch = container_of(kobj, struct klp_patch, kobj);
586
587 mutex_lock(&klp_mutex);
588
589 if (val == patch->state) {
590 /* already in requested state */
591 ret = -EINVAL;
592 goto err;
593 }
594
595 if (val == KLP_ENABLED) {
596 ret = __klp_enable_patch(patch);
597 if (ret)
598 goto err;
599 } else {
600 ret = __klp_disable_patch(patch);
601 if (ret)
602 goto err;
603 }
604
605 mutex_unlock(&klp_mutex);
606
607 return count;
608
609err:
610 mutex_unlock(&klp_mutex);
611 return ret;
612}
613
614static ssize_t enabled_show(struct kobject *kobj,
615 struct kobj_attribute *attr, char *buf)
616{
617 struct klp_patch *patch;
618
619 patch = container_of(kobj, struct klp_patch, kobj);
620 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
621}
622
623static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
624static struct attribute *klp_patch_attrs[] = {
625 &enabled_kobj_attr.attr,
626 NULL
627};
628
629static void klp_kobj_release_patch(struct kobject *kobj)
630{
631 /*
632 * Once we have a consistency model we'll need to module_put() the
633 * patch module here. See klp_register_patch() for more details.
634 */
635}
636
637static struct kobj_type klp_ktype_patch = {
638 .release = klp_kobj_release_patch,
639 .sysfs_ops = &kobj_sysfs_ops,
640 .default_attrs = klp_patch_attrs,
641};
642
643static void klp_kobj_release_func(struct kobject *kobj)
644{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600645}
646
647static struct kobj_type klp_ktype_func = {
648 .release = klp_kobj_release_func,
649 .sysfs_ops = &kobj_sysfs_ops,
650};
651
652/*
653 * Free all functions' kobjects in the array up to some limit. When limit is
654 * NULL, all kobjects are freed.
655 */
656static void klp_free_funcs_limited(struct klp_object *obj,
657 struct klp_func *limit)
658{
659 struct klp_func *func;
660
661 for (func = obj->funcs; func->old_name && func != limit; func++)
662 kobject_put(&func->kobj);
663}
664
665/* Clean up when a patched object is unloaded */
666static void klp_free_object_loaded(struct klp_object *obj)
667{
668 struct klp_func *func;
669
670 obj->mod = NULL;
671
672 for (func = obj->funcs; func->old_name; func++)
673 func->old_addr = 0;
674}
675
676/*
677 * Free all objects' kobjects in the array up to some limit. When limit is
678 * NULL, all kobjects are freed.
679 */
680static void klp_free_objects_limited(struct klp_patch *patch,
681 struct klp_object *limit)
682{
683 struct klp_object *obj;
684
685 for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
686 klp_free_funcs_limited(obj, NULL);
687 kobject_put(obj->kobj);
688 }
689}
690
691static void klp_free_patch(struct klp_patch *patch)
692{
693 klp_free_objects_limited(patch, NULL);
694 if (!list_empty(&patch->list))
695 list_del(&patch->list);
696 kobject_put(&patch->kobj);
697}
698
699static int klp_init_func(struct klp_object *obj, struct klp_func *func)
700{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600701 INIT_LIST_HEAD(&func->stack_node);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600702 func->state = KLP_DISABLED;
703
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600704 return kobject_init_and_add(&func->kobj, &klp_ktype_func,
705 obj->kobj, func->old_name);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600706}
707
708/* parts of the initialization that is done only when the object is loaded */
709static int klp_init_object_loaded(struct klp_patch *patch,
710 struct klp_object *obj)
711{
712 struct klp_func *func;
713 int ret;
714
715 if (obj->relocs) {
716 ret = klp_write_object_relocations(patch->mod, obj);
717 if (ret)
718 return ret;
719 }
720
721 for (func = obj->funcs; func->old_name; func++) {
722 ret = klp_find_verify_func_addr(obj, func);
723 if (ret)
724 return ret;
725 }
726
727 return 0;
728}
729
730static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
731{
732 struct klp_func *func;
733 int ret;
734 const char *name;
735
736 if (!obj->funcs)
737 return -EINVAL;
738
739 obj->state = KLP_DISABLED;
740
741 klp_find_object_module(obj);
742
743 name = klp_is_module(obj) ? obj->name : "vmlinux";
744 obj->kobj = kobject_create_and_add(name, &patch->kobj);
745 if (!obj->kobj)
746 return -ENOMEM;
747
748 for (func = obj->funcs; func->old_name; func++) {
749 ret = klp_init_func(obj, func);
750 if (ret)
751 goto free;
752 }
753
754 if (klp_is_object_loaded(obj)) {
755 ret = klp_init_object_loaded(patch, obj);
756 if (ret)
757 goto free;
758 }
759
760 return 0;
761
762free:
763 klp_free_funcs_limited(obj, func);
764 kobject_put(obj->kobj);
765 return ret;
766}
767
768static int klp_init_patch(struct klp_patch *patch)
769{
770 struct klp_object *obj;
771 int ret;
772
773 if (!patch->objs)
774 return -EINVAL;
775
776 mutex_lock(&klp_mutex);
777
778 patch->state = KLP_DISABLED;
779
780 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
781 klp_root_kobj, patch->mod->name);
782 if (ret)
783 goto unlock;
784
785 for (obj = patch->objs; obj->funcs; obj++) {
786 ret = klp_init_object(patch, obj);
787 if (ret)
788 goto free;
789 }
790
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600791 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600792
793 mutex_unlock(&klp_mutex);
794
795 return 0;
796
797free:
798 klp_free_objects_limited(patch, obj);
799 kobject_put(&patch->kobj);
800unlock:
801 mutex_unlock(&klp_mutex);
802 return ret;
803}
804
805/**
806 * klp_unregister_patch() - unregisters a patch
807 * @patch: Disabled patch to be unregistered
808 *
809 * Frees the data structures and removes the sysfs interface.
810 *
811 * Return: 0 on success, otherwise error
812 */
813int klp_unregister_patch(struct klp_patch *patch)
814{
815 int ret = 0;
816
817 mutex_lock(&klp_mutex);
818
819 if (!klp_is_patch_registered(patch)) {
820 ret = -EINVAL;
821 goto out;
822 }
823
824 if (patch->state == KLP_ENABLED) {
825 ret = -EBUSY;
826 goto out;
827 }
828
829 klp_free_patch(patch);
830
831out:
832 mutex_unlock(&klp_mutex);
833 return ret;
834}
835EXPORT_SYMBOL_GPL(klp_unregister_patch);
836
837/**
838 * klp_register_patch() - registers a patch
839 * @patch: Patch to be registered
840 *
841 * Initializes the data structure associated with the patch and
842 * creates the sysfs interface.
843 *
844 * Return: 0 on success, otherwise error
845 */
846int klp_register_patch(struct klp_patch *patch)
847{
848 int ret;
849
850 if (!klp_initialized())
851 return -ENODEV;
852
853 if (!patch || !patch->mod)
854 return -EINVAL;
855
856 /*
857 * A reference is taken on the patch module to prevent it from being
858 * unloaded. Right now, we don't allow patch modules to unload since
859 * there is currently no method to determine if a thread is still
860 * running in the patched code contained in the patch module once
861 * the ftrace registration is successful.
862 */
863 if (!try_module_get(patch->mod))
864 return -ENODEV;
865
866 ret = klp_init_patch(patch);
867 if (ret)
868 module_put(patch->mod);
869
870 return ret;
871}
872EXPORT_SYMBOL_GPL(klp_register_patch);
873
874static void klp_module_notify_coming(struct klp_patch *patch,
875 struct klp_object *obj)
876{
877 struct module *pmod = patch->mod;
878 struct module *mod = obj->mod;
879 int ret;
880
881 ret = klp_init_object_loaded(patch, obj);
882 if (ret)
883 goto err;
884
885 if (patch->state == KLP_DISABLED)
886 return;
887
888 pr_notice("applying patch '%s' to loading module '%s'\n",
889 pmod->name, mod->name);
890
891 ret = klp_enable_object(obj);
892 if (!ret)
893 return;
894
895err:
896 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
897 pmod->name, mod->name, ret);
898}
899
900static void klp_module_notify_going(struct klp_patch *patch,
901 struct klp_object *obj)
902{
903 struct module *pmod = patch->mod;
904 struct module *mod = obj->mod;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600905
906 if (patch->state == KLP_DISABLED)
907 goto disabled;
908
909 pr_notice("reverting patch '%s' on unloading module '%s'\n",
910 pmod->name, mod->name);
911
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600912 klp_disable_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600913
914disabled:
915 klp_free_object_loaded(obj);
916}
917
918static int klp_module_notify(struct notifier_block *nb, unsigned long action,
919 void *data)
920{
921 struct module *mod = data;
922 struct klp_patch *patch;
923 struct klp_object *obj;
924
925 if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
926 return 0;
927
928 mutex_lock(&klp_mutex);
929
930 list_for_each_entry(patch, &klp_patches, list) {
931 for (obj = patch->objs; obj->funcs; obj++) {
932 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
933 continue;
934
935 if (action == MODULE_STATE_COMING) {
936 obj->mod = mod;
937 klp_module_notify_coming(patch, obj);
938 } else /* MODULE_STATE_GOING */
939 klp_module_notify_going(patch, obj);
940
941 break;
942 }
943 }
944
945 mutex_unlock(&klp_mutex);
946
947 return 0;
948}
949
950static struct notifier_block klp_module_nb = {
951 .notifier_call = klp_module_notify,
952 .priority = INT_MIN+1, /* called late but before ftrace notifier */
953};
954
955static int klp_init(void)
956{
957 int ret;
958
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +0100959 ret = klp_check_compiler_support();
960 if (ret) {
961 pr_info("Your compiler is too old; turning off.\n");
962 return -EINVAL;
963 }
964
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600965 ret = register_module_notifier(&klp_module_nb);
966 if (ret)
967 return ret;
968
969 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
970 if (!klp_root_kobj) {
971 ret = -ENOMEM;
972 goto unregister;
973 }
974
975 return 0;
976
977unregister:
978 unregister_module_notifier(&klp_module_nb);
979 return ret;
980}
981
982module_init(klp_init);