blob: 10ba3a1578bdf09d6f825a5a2fb8696508b6fd42 [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>
Seth Jenningsb700e7f2014-12-16 11:58:19 -060027#include <linux/list.h>
28#include <linux/kallsyms.h>
29#include <linux/livepatch.h>
Jessica Yu425595a2016-03-22 20:03:18 -040030#include <linux/elf.h>
31#include <linux/moduleloader.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060032#include <asm/cacheflush.h>
Josh Poimboeufc349cdca2017-02-13 19:42:37 -060033#include "patch.h"
Seth Jenningsb700e7f2014-12-16 11:58:19 -060034
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060035/*
36 * The klp_mutex protects the global lists and state transitions of any
37 * structure reachable from them. References to any structure must be obtained
38 * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
39 * ensure it gets consistent data).
40 */
Seth Jenningsb700e7f2014-12-16 11:58:19 -060041static DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060042
Seth Jenningsb700e7f2014-12-16 11:58:19 -060043static LIST_HEAD(klp_patches);
44
45static struct kobject *klp_root_kobj;
46
Josh Poimboeuf46c5a012017-02-13 19:42:30 -060047/* TODO: temporary stub */
48void klp_update_patch_state(struct task_struct *task) {}
49
Seth Jenningsb700e7f2014-12-16 11:58:19 -060050static bool klp_is_module(struct klp_object *obj)
51{
52 return obj->name;
53}
54
55static bool klp_is_object_loaded(struct klp_object *obj)
56{
57 return !obj->name || obj->mod;
58}
59
60/* sets obj->mod if object is not vmlinux and module is found */
61static void klp_find_object_module(struct klp_object *obj)
62{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010063 struct module *mod;
64
Seth Jenningsb700e7f2014-12-16 11:58:19 -060065 if (!klp_is_module(obj))
66 return;
67
68 mutex_lock(&module_mutex);
69 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010070 * We do not want to block removal of patched modules and therefore
71 * we do not take a reference here. The patches are removed by
Jessica Yu7e545d62016-03-16 20:55:39 -040072 * klp_module_going() instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -060073 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010074 mod = find_module(obj->name);
75 /*
Jessica Yu7e545d62016-03-16 20:55:39 -040076 * Do not mess work of klp_module_coming() and klp_module_going().
77 * Note that the patch might still be needed before klp_module_going()
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010078 * is called. Module functions can be called even in the GOING state
79 * until mod->exit() finishes. This is especially important for
80 * patches that modify semantic of the functions.
81 */
82 if (mod && mod->klp_alive)
83 obj->mod = mod;
84
Seth Jenningsb700e7f2014-12-16 11:58:19 -060085 mutex_unlock(&module_mutex);
86}
87
88/* klp_mutex must be held by caller */
89static bool klp_is_patch_registered(struct klp_patch *patch)
90{
91 struct klp_patch *mypatch;
92
93 list_for_each_entry(mypatch, &klp_patches, list)
94 if (mypatch == patch)
95 return true;
96
97 return false;
98}
99
100static bool klp_initialized(void)
101{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +0200102 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600103}
104
105struct klp_find_arg {
106 const char *objname;
107 const char *name;
108 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600109 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600110 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600111};
112
113static int klp_find_callback(void *data, const char *name,
114 struct module *mod, unsigned long addr)
115{
116 struct klp_find_arg *args = data;
117
118 if ((mod && !args->objname) || (!mod && args->objname))
119 return 0;
120
121 if (strcmp(args->name, name))
122 return 0;
123
124 if (args->objname && strcmp(args->objname, mod->name))
125 return 0;
126
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600127 args->addr = addr;
128 args->count++;
129
Chris J Argesb2b018e2015-12-01 20:40:54 -0600130 /*
131 * Finish the search when the symbol is found for the desired position
132 * or the position is not defined for a non-unique symbol.
133 */
134 if ((args->pos && (args->count == args->pos)) ||
135 (!args->pos && (args->count > 1)))
136 return 1;
137
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600138 return 0;
139}
140
141static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600142 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600143{
144 struct klp_find_arg args = {
145 .objname = objname,
146 .name = name,
147 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600148 .count = 0,
149 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600150 };
151
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200152 mutex_lock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600153 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200154 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600155
Chris J Argesb2b018e2015-12-01 20:40:54 -0600156 /*
157 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
158 * otherwise ensure the symbol position count matches sympos.
159 */
160 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600161 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600162 else if (args.count > 1 && sympos == 0) {
Petr Mladekf995b5f2016-03-09 15:20:59 +0100163 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
164 name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600165 } else if (sympos != args.count && sympos > 0) {
166 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
167 sympos, name, objname ? objname : "vmlinux");
168 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600169 *addr = args.addr;
170 return 0;
171 }
172
173 *addr = 0;
174 return -EINVAL;
175}
176
Jessica Yu425595a2016-03-22 20:03:18 -0400177static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600178{
Jessica Yu425595a2016-03-22 20:03:18 -0400179 int i, cnt, vmlinux, ret;
180 char objname[MODULE_NAME_LEN];
181 char symname[KSYM_NAME_LEN];
182 char *strtab = pmod->core_kallsyms.strtab;
183 Elf_Rela *relas;
184 Elf_Sym *sym;
185 unsigned long sympos, addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600186
Chris J Argesb2b018e2015-12-01 20:40:54 -0600187 /*
Jessica Yu425595a2016-03-22 20:03:18 -0400188 * Since the field widths for objname and symname in the sscanf()
189 * call are hard-coded and correspond to MODULE_NAME_LEN and
190 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
191 * and KSYM_NAME_LEN have the values we expect them to have.
192 *
193 * Because the value of MODULE_NAME_LEN can differ among architectures,
194 * we use the smallest/strictest upper bound possible (56, based on
195 * the current definition of MODULE_NAME_LEN) to prevent overflows.
Chris J Argesb2b018e2015-12-01 20:40:54 -0600196 */
Jessica Yu425595a2016-03-22 20:03:18 -0400197 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
198
199 relas = (Elf_Rela *) relasec->sh_addr;
200 /* For each rela in this klp relocation section */
201 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
202 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
203 if (sym->st_shndx != SHN_LIVEPATCH) {
204 pr_err("symbol %s is not marked as a livepatch symbol",
205 strtab + sym->st_name);
206 return -EINVAL;
207 }
208
209 /* Format: .klp.sym.objname.symname,sympos */
210 cnt = sscanf(strtab + sym->st_name,
211 ".klp.sym.%55[^.].%127[^,],%lu",
212 objname, symname, &sympos);
213 if (cnt != 3) {
214 pr_err("symbol %s has an incorrectly formatted name",
215 strtab + sym->st_name);
216 return -EINVAL;
217 }
218
219 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
220 vmlinux = !strcmp(objname, "vmlinux");
221 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
222 symname, sympos, &addr);
223 if (ret)
224 return ret;
225
226 sym->st_value = addr;
227 }
228
229 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600230}
231
232static int klp_write_object_relocations(struct module *pmod,
233 struct klp_object *obj)
234{
Jessica Yu425595a2016-03-22 20:03:18 -0400235 int i, cnt, ret = 0;
236 const char *objname, *secname;
237 char sec_objname[MODULE_NAME_LEN];
238 Elf_Shdr *sec;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600239
240 if (WARN_ON(!klp_is_object_loaded(obj)))
241 return -EINVAL;
242
Jessica Yu425595a2016-03-22 20:03:18 -0400243 objname = klp_is_module(obj) ? obj->name : "vmlinux";
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600244
Jessica Yu425595a2016-03-22 20:03:18 -0400245 /* For each klp relocation section */
246 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
247 sec = pmod->klp_info->sechdrs + i;
248 secname = pmod->klp_info->secstrings + sec->sh_name;
249 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
250 continue;
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600251
Jessica Yu425595a2016-03-22 20:03:18 -0400252 /*
253 * Format: .klp.rela.sec_objname.section_name
254 * See comment in klp_resolve_symbols() for an explanation
255 * of the selected field width value.
256 */
257 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
258 if (cnt != 1) {
259 pr_err("section %s has an incorrectly formatted name",
260 secname);
261 ret = -EINVAL;
262 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600263 }
Jessica Yu425595a2016-03-22 20:03:18 -0400264
265 if (strcmp(objname, sec_objname))
266 continue;
267
268 ret = klp_resolve_symbols(sec, pmod);
269 if (ret)
270 break;
271
272 ret = apply_relocate_add(pmod->klp_info->sechdrs,
273 pmod->core_kallsyms.strtab,
274 pmod->klp_info->symndx, i, pmod);
275 if (ret)
276 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600277 }
278
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600279 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600280}
281
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600282static int __klp_disable_patch(struct klp_patch *patch)
283{
284 struct klp_object *obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600285
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600286 /* enforce stacking: only the last enabled patch can be disabled */
287 if (!list_is_last(&patch->list, &klp_patches) &&
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600288 list_next_entry(patch, list)->enabled)
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600289 return -EBUSY;
290
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600291 pr_notice("disabling patch '%s'\n", patch->mod->name);
292
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200293 klp_for_each_object(patch, obj) {
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600294 if (obj->patched)
295 klp_unpatch_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600296 }
297
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600298 patch->enabled = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600299
300 return 0;
301}
302
303/**
304 * klp_disable_patch() - disables a registered patch
305 * @patch: The registered, enabled patch to be disabled
306 *
307 * Unregisters the patched functions from ftrace.
308 *
309 * Return: 0 on success, otherwise error
310 */
311int klp_disable_patch(struct klp_patch *patch)
312{
313 int ret;
314
315 mutex_lock(&klp_mutex);
316
317 if (!klp_is_patch_registered(patch)) {
318 ret = -EINVAL;
319 goto err;
320 }
321
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600322 if (!patch->enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600323 ret = -EINVAL;
324 goto err;
325 }
326
327 ret = __klp_disable_patch(patch);
328
329err:
330 mutex_unlock(&klp_mutex);
331 return ret;
332}
333EXPORT_SYMBOL_GPL(klp_disable_patch);
334
335static int __klp_enable_patch(struct klp_patch *patch)
336{
337 struct klp_object *obj;
338 int ret;
339
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600340 if (WARN_ON(patch->enabled))
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600341 return -EINVAL;
342
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600343 /* enforce stacking: only the first disabled patch can be enabled */
344 if (patch->list.prev != &klp_patches &&
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600345 !list_prev_entry(patch, list)->enabled)
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600346 return -EBUSY;
347
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600348 pr_notice("enabling patch '%s'\n", patch->mod->name);
349
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200350 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600351 if (!klp_is_object_loaded(obj))
352 continue;
353
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600354 ret = klp_patch_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600355 if (ret)
356 goto unregister;
357 }
358
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600359 patch->enabled = true;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600360
361 return 0;
362
363unregister:
364 WARN_ON(__klp_disable_patch(patch));
365 return ret;
366}
367
368/**
369 * klp_enable_patch() - enables a registered patch
370 * @patch: The registered, disabled patch to be enabled
371 *
372 * Performs the needed symbol lookups and code relocations,
373 * then registers the patched functions with ftrace.
374 *
375 * Return: 0 on success, otherwise error
376 */
377int klp_enable_patch(struct klp_patch *patch)
378{
379 int ret;
380
381 mutex_lock(&klp_mutex);
382
383 if (!klp_is_patch_registered(patch)) {
384 ret = -EINVAL;
385 goto err;
386 }
387
388 ret = __klp_enable_patch(patch);
389
390err:
391 mutex_unlock(&klp_mutex);
392 return ret;
393}
394EXPORT_SYMBOL_GPL(klp_enable_patch);
395
396/*
397 * Sysfs Interface
398 *
399 * /sys/kernel/livepatch
400 * /sys/kernel/livepatch/<patch>
401 * /sys/kernel/livepatch/<patch>/enabled
402 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600403 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600404 */
405
406static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
407 const char *buf, size_t count)
408{
409 struct klp_patch *patch;
410 int ret;
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600411 bool enabled;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600412
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600413 ret = kstrtobool(buf, &enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600414 if (ret)
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600415 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600416
417 patch = container_of(kobj, struct klp_patch, kobj);
418
419 mutex_lock(&klp_mutex);
420
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600421 if (patch->enabled == enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600422 /* already in requested state */
423 ret = -EINVAL;
424 goto err;
425 }
426
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600427 if (enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600428 ret = __klp_enable_patch(patch);
429 if (ret)
430 goto err;
431 } else {
432 ret = __klp_disable_patch(patch);
433 if (ret)
434 goto err;
435 }
436
437 mutex_unlock(&klp_mutex);
438
439 return count;
440
441err:
442 mutex_unlock(&klp_mutex);
443 return ret;
444}
445
446static ssize_t enabled_show(struct kobject *kobj,
447 struct kobj_attribute *attr, char *buf)
448{
449 struct klp_patch *patch;
450
451 patch = container_of(kobj, struct klp_patch, kobj);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600452 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600453}
454
455static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
456static struct attribute *klp_patch_attrs[] = {
457 &enabled_kobj_attr.attr,
458 NULL
459};
460
461static void klp_kobj_release_patch(struct kobject *kobj)
462{
463 /*
464 * Once we have a consistency model we'll need to module_put() the
465 * patch module here. See klp_register_patch() for more details.
466 */
467}
468
469static struct kobj_type klp_ktype_patch = {
470 .release = klp_kobj_release_patch,
471 .sysfs_ops = &kobj_sysfs_ops,
472 .default_attrs = klp_patch_attrs,
473};
474
Miroslav Benescad706d2015-05-19 12:01:18 +0200475static void klp_kobj_release_object(struct kobject *kobj)
476{
477}
478
479static struct kobj_type klp_ktype_object = {
480 .release = klp_kobj_release_object,
481 .sysfs_ops = &kobj_sysfs_ops,
482};
483
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600484static void klp_kobj_release_func(struct kobject *kobj)
485{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600486}
487
488static struct kobj_type klp_ktype_func = {
489 .release = klp_kobj_release_func,
490 .sysfs_ops = &kobj_sysfs_ops,
491};
492
493/*
494 * Free all functions' kobjects in the array up to some limit. When limit is
495 * NULL, all kobjects are freed.
496 */
497static void klp_free_funcs_limited(struct klp_object *obj,
498 struct klp_func *limit)
499{
500 struct klp_func *func;
501
502 for (func = obj->funcs; func->old_name && func != limit; func++)
503 kobject_put(&func->kobj);
504}
505
506/* Clean up when a patched object is unloaded */
507static void klp_free_object_loaded(struct klp_object *obj)
508{
509 struct klp_func *func;
510
511 obj->mod = NULL;
512
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200513 klp_for_each_func(obj, func)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600514 func->old_addr = 0;
515}
516
517/*
518 * Free all objects' kobjects in the array up to some limit. When limit is
519 * NULL, all kobjects are freed.
520 */
521static void klp_free_objects_limited(struct klp_patch *patch,
522 struct klp_object *limit)
523{
524 struct klp_object *obj;
525
526 for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
527 klp_free_funcs_limited(obj, NULL);
Miroslav Benescad706d2015-05-19 12:01:18 +0200528 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600529 }
530}
531
532static void klp_free_patch(struct klp_patch *patch)
533{
534 klp_free_objects_limited(patch, NULL);
535 if (!list_empty(&patch->list))
536 list_del(&patch->list);
537 kobject_put(&patch->kobj);
538}
539
540static int klp_init_func(struct klp_object *obj, struct klp_func *func)
541{
Miroslav Benesf09d9082016-04-28 16:34:08 +0200542 if (!func->old_name || !func->new_func)
543 return -EINVAL;
544
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600545 INIT_LIST_HEAD(&func->stack_node);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600546 func->patched = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600547
Chris J Arges444f9e92015-12-01 20:40:56 -0600548 /* The format for the sysfs directory is <function,sympos> where sympos
549 * is the nth occurrence of this symbol in kallsyms for the patched
550 * object. If the user selects 0 for old_sympos, then 1 will be used
551 * since a unique symbol will be the first occurrence.
552 */
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600553 return kobject_init_and_add(&func->kobj, &klp_ktype_func,
Chris J Arges444f9e92015-12-01 20:40:56 -0600554 &obj->kobj, "%s,%lu", func->old_name,
555 func->old_sympos ? func->old_sympos : 1);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600556}
557
Jessica Yu255e7322016-08-17 20:58:28 -0400558/* Arches may override this to finish any remaining arch-specific tasks */
559void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
560 struct klp_object *obj)
561{
562}
563
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600564/* parts of the initialization that is done only when the object is loaded */
565static int klp_init_object_loaded(struct klp_patch *patch,
566 struct klp_object *obj)
567{
568 struct klp_func *func;
569 int ret;
570
Jessica Yu255e7322016-08-17 20:58:28 -0400571 module_disable_ro(patch->mod);
Jessica Yu425595a2016-03-22 20:03:18 -0400572 ret = klp_write_object_relocations(patch->mod, obj);
Jessica Yu255e7322016-08-17 20:58:28 -0400573 if (ret) {
574 module_enable_ro(patch->mod, true);
Jessica Yu425595a2016-03-22 20:03:18 -0400575 return ret;
Jessica Yu255e7322016-08-17 20:58:28 -0400576 }
577
578 arch_klp_init_object_loaded(patch, obj);
579 module_enable_ro(patch->mod, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600580
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200581 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600582 ret = klp_find_object_symbol(obj->name, func->old_name,
583 func->old_sympos,
584 &func->old_addr);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600585 if (ret)
586 return ret;
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600587
588 ret = kallsyms_lookup_size_offset(func->old_addr,
589 &func->old_size, NULL);
590 if (!ret) {
591 pr_err("kallsyms size lookup failed for '%s'\n",
592 func->old_name);
593 return -ENOENT;
594 }
595
596 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
597 &func->new_size, NULL);
598 if (!ret) {
599 pr_err("kallsyms size lookup failed for '%s' replacement\n",
600 func->old_name);
601 return -ENOENT;
602 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600603 }
604
605 return 0;
606}
607
608static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
609{
610 struct klp_func *func;
611 int ret;
612 const char *name;
613
614 if (!obj->funcs)
615 return -EINVAL;
616
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600617 obj->patched = false;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100618 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600619
620 klp_find_object_module(obj);
621
622 name = klp_is_module(obj) ? obj->name : "vmlinux";
Miroslav Benescad706d2015-05-19 12:01:18 +0200623 ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
624 &patch->kobj, "%s", name);
625 if (ret)
626 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600627
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200628 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600629 ret = klp_init_func(obj, func);
630 if (ret)
631 goto free;
632 }
633
634 if (klp_is_object_loaded(obj)) {
635 ret = klp_init_object_loaded(patch, obj);
636 if (ret)
637 goto free;
638 }
639
640 return 0;
641
642free:
643 klp_free_funcs_limited(obj, func);
Miroslav Benescad706d2015-05-19 12:01:18 +0200644 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600645 return ret;
646}
647
648static int klp_init_patch(struct klp_patch *patch)
649{
650 struct klp_object *obj;
651 int ret;
652
653 if (!patch->objs)
654 return -EINVAL;
655
656 mutex_lock(&klp_mutex);
657
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600658 patch->enabled = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600659
660 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
Jiri Kosinae0b561e2015-02-15 10:03:20 +0100661 klp_root_kobj, "%s", patch->mod->name);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600662 if (ret)
663 goto unlock;
664
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200665 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600666 ret = klp_init_object(patch, obj);
667 if (ret)
668 goto free;
669 }
670
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600671 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600672
673 mutex_unlock(&klp_mutex);
674
675 return 0;
676
677free:
678 klp_free_objects_limited(patch, obj);
679 kobject_put(&patch->kobj);
680unlock:
681 mutex_unlock(&klp_mutex);
682 return ret;
683}
684
685/**
686 * klp_unregister_patch() - unregisters a patch
687 * @patch: Disabled patch to be unregistered
688 *
689 * Frees the data structures and removes the sysfs interface.
690 *
691 * Return: 0 on success, otherwise error
692 */
693int klp_unregister_patch(struct klp_patch *patch)
694{
695 int ret = 0;
696
697 mutex_lock(&klp_mutex);
698
699 if (!klp_is_patch_registered(patch)) {
700 ret = -EINVAL;
701 goto out;
702 }
703
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600704 if (patch->enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600705 ret = -EBUSY;
706 goto out;
707 }
708
709 klp_free_patch(patch);
710
711out:
712 mutex_unlock(&klp_mutex);
713 return ret;
714}
715EXPORT_SYMBOL_GPL(klp_unregister_patch);
716
717/**
718 * klp_register_patch() - registers a patch
719 * @patch: Patch to be registered
720 *
721 * Initializes the data structure associated with the patch and
722 * creates the sysfs interface.
723 *
724 * Return: 0 on success, otherwise error
725 */
726int klp_register_patch(struct klp_patch *patch)
727{
728 int ret;
729
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600730 if (!patch || !patch->mod)
731 return -EINVAL;
732
Jessica Yu425595a2016-03-22 20:03:18 -0400733 if (!is_livepatch_module(patch->mod)) {
734 pr_err("module %s is not marked as a livepatch module",
735 patch->mod->name);
736 return -EINVAL;
737 }
738
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600739 if (!klp_initialized())
740 return -ENODEV;
741
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600742 /*
743 * A reference is taken on the patch module to prevent it from being
744 * unloaded. Right now, we don't allow patch modules to unload since
745 * there is currently no method to determine if a thread is still
746 * running in the patched code contained in the patch module once
747 * the ftrace registration is successful.
748 */
749 if (!try_module_get(patch->mod))
750 return -ENODEV;
751
752 ret = klp_init_patch(patch);
753 if (ret)
754 module_put(patch->mod);
755
756 return ret;
757}
758EXPORT_SYMBOL_GPL(klp_register_patch);
759
Jessica Yu7e545d62016-03-16 20:55:39 -0400760int klp_module_coming(struct module *mod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600761{
Minfei Huang36e505c2015-05-15 10:22:48 +0800762 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600763 struct klp_patch *patch;
764 struct klp_object *obj;
765
Jessica Yu7e545d62016-03-16 20:55:39 -0400766 if (WARN_ON(mod->state != MODULE_STATE_COMING))
767 return -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600768
769 mutex_lock(&klp_mutex);
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100770 /*
Jessica Yu7e545d62016-03-16 20:55:39 -0400771 * Each module has to know that klp_module_coming()
772 * has been called. We never know what module will
773 * get patched by a new patch.
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100774 */
Jessica Yu7e545d62016-03-16 20:55:39 -0400775 mod->klp_alive = true;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100776
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600777 list_for_each_entry(patch, &klp_patches, list) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200778 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600779 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
780 continue;
781
Jessica Yu7e545d62016-03-16 20:55:39 -0400782 obj->mod = mod;
783
784 ret = klp_init_object_loaded(patch, obj);
785 if (ret) {
786 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
787 patch->mod->name, obj->mod->name, ret);
788 goto err;
789 }
790
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600791 if (!patch->enabled)
Jessica Yu7e545d62016-03-16 20:55:39 -0400792 break;
793
794 pr_notice("applying patch '%s' to loading module '%s'\n",
795 patch->mod->name, obj->mod->name);
796
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600797 ret = klp_patch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -0400798 if (ret) {
799 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
800 patch->mod->name, obj->mod->name, ret);
801 goto err;
802 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600803
804 break;
805 }
806 }
807
808 mutex_unlock(&klp_mutex);
809
810 return 0;
Jessica Yu7e545d62016-03-16 20:55:39 -0400811
812err:
813 /*
814 * If a patch is unsuccessfully applied, return
815 * error to the module loader.
816 */
817 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
818 patch->mod->name, obj->mod->name, obj->mod->name);
819 mod->klp_alive = false;
820 klp_free_object_loaded(obj);
821 mutex_unlock(&klp_mutex);
822
823 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600824}
825
Jessica Yu7e545d62016-03-16 20:55:39 -0400826void klp_module_going(struct module *mod)
827{
828 struct klp_patch *patch;
829 struct klp_object *obj;
830
831 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
832 mod->state != MODULE_STATE_COMING))
833 return;
834
835 mutex_lock(&klp_mutex);
836 /*
837 * Each module has to know that klp_module_going()
838 * has been called. We never know what module will
839 * get patched by a new patch.
840 */
841 mod->klp_alive = false;
842
843 list_for_each_entry(patch, &klp_patches, list) {
844 klp_for_each_object(patch, obj) {
845 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
846 continue;
847
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600848 if (patch->enabled) {
Jessica Yu7e545d62016-03-16 20:55:39 -0400849 pr_notice("reverting patch '%s' on unloading module '%s'\n",
850 patch->mod->name, obj->mod->name);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600851 klp_unpatch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -0400852 }
853
854 klp_free_object_loaded(obj);
855 break;
856 }
857 }
858
859 mutex_unlock(&klp_mutex);
860}
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600861
Minfei Huang26029d82015-05-22 22:26:29 +0800862static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600863{
864 int ret;
865
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +0100866 ret = klp_check_compiler_support();
867 if (ret) {
868 pr_info("Your compiler is too old; turning off.\n");
869 return -EINVAL;
870 }
871
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600872 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
Jessica Yu7e545d62016-03-16 20:55:39 -0400873 if (!klp_root_kobj)
874 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600875
876 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600877}
878
879module_init(klp_init);