]>
Commit | Line | Data |
---|---|---|
b700e7f0 SJ |
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> | |
b700e7f0 SJ |
27 | #include <linux/list.h> |
28 | #include <linux/kallsyms.h> | |
29 | #include <linux/livepatch.h> | |
425595a7 JY |
30 | #include <linux/elf.h> |
31 | #include <linux/moduleloader.h> | |
b56b36ee | 32 | #include <asm/cacheflush.h> |
c349cdca | 33 | #include "patch.h" |
b700e7f0 | 34 | |
3c33f5b9 JP |
35 | /* |
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 | */ | |
b700e7f0 | 41 | static DEFINE_MUTEX(klp_mutex); |
3c33f5b9 | 42 | |
b700e7f0 SJ |
43 | static LIST_HEAD(klp_patches); |
44 | ||
45 | static struct kobject *klp_root_kobj; | |
46 | ||
46c5a011 JP |
47 | /* TODO: temporary stub */ |
48 | void klp_update_patch_state(struct task_struct *task) {} | |
49 | ||
b700e7f0 SJ |
50 | static bool klp_is_module(struct klp_object *obj) |
51 | { | |
52 | return obj->name; | |
53 | } | |
54 | ||
55 | static 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 */ | |
61 | static void klp_find_object_module(struct klp_object *obj) | |
62 | { | |
8cb2c2dc PM |
63 | struct module *mod; |
64 | ||
b700e7f0 SJ |
65 | if (!klp_is_module(obj)) |
66 | return; | |
67 | ||
68 | mutex_lock(&module_mutex); | |
69 | /* | |
8cb2c2dc PM |
70 | * 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 | |
7e545d6e | 72 | * klp_module_going() instead. |
8cb2c2dc PM |
73 | */ |
74 | mod = find_module(obj->name); | |
75 | /* | |
7e545d6e JY |
76 | * 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() | |
8cb2c2dc PM |
78 | * 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. | |
b700e7f0 | 81 | */ |
8cb2c2dc PM |
82 | if (mod && mod->klp_alive) |
83 | obj->mod = mod; | |
84 | ||
b700e7f0 SJ |
85 | mutex_unlock(&module_mutex); |
86 | } | |
87 | ||
88 | /* klp_mutex must be held by caller */ | |
89 | static 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 | ||
100 | static bool klp_initialized(void) | |
101 | { | |
e76ff06a | 102 | return !!klp_root_kobj; |
b700e7f0 SJ |
103 | } |
104 | ||
105 | struct klp_find_arg { | |
106 | const char *objname; | |
107 | const char *name; | |
108 | unsigned long addr; | |
b700e7f0 | 109 | unsigned long count; |
b2b018ef | 110 | unsigned long pos; |
b700e7f0 SJ |
111 | }; |
112 | ||
113 | static 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 | ||
b700e7f0 SJ |
127 | args->addr = addr; |
128 | args->count++; | |
129 | ||
b2b018ef CA |
130 | /* |
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 | ||
b700e7f0 SJ |
138 | return 0; |
139 | } | |
140 | ||
141 | static int klp_find_object_symbol(const char *objname, const char *name, | |
b2b018ef | 142 | unsigned long sympos, unsigned long *addr) |
b700e7f0 SJ |
143 | { |
144 | struct klp_find_arg args = { | |
145 | .objname = objname, | |
146 | .name = name, | |
147 | .addr = 0, | |
b2b018ef CA |
148 | .count = 0, |
149 | .pos = sympos, | |
b700e7f0 SJ |
150 | }; |
151 | ||
9a1bd63c | 152 | mutex_lock(&module_mutex); |
b700e7f0 | 153 | kallsyms_on_each_symbol(klp_find_callback, &args); |
9a1bd63c | 154 | mutex_unlock(&module_mutex); |
b700e7f0 | 155 | |
b2b018ef CA |
156 | /* |
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) | |
b700e7f0 | 161 | pr_err("symbol '%s' not found in symbol table\n", name); |
b2b018ef | 162 | else if (args.count > 1 && sympos == 0) { |
f995b5f7 PM |
163 | pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n", |
164 | name, objname); | |
b2b018ef CA |
165 | } 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 { | |
b700e7f0 SJ |
169 | *addr = args.addr; |
170 | return 0; | |
171 | } | |
172 | ||
173 | *addr = 0; | |
174 | return -EINVAL; | |
175 | } | |
176 | ||
425595a7 | 177 | static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod) |
b700e7f0 | 178 | { |
425595a7 JY |
179 | 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; | |
b700e7f0 | 186 | |
b2b018ef | 187 | /* |
425595a7 JY |
188 | * 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. | |
b2b018ef | 196 | */ |
425595a7 JY |
197 | 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; | |
b700e7f0 SJ |
230 | } |
231 | ||
232 | static int klp_write_object_relocations(struct module *pmod, | |
233 | struct klp_object *obj) | |
234 | { | |
425595a7 JY |
235 | int i, cnt, ret = 0; |
236 | const char *objname, *secname; | |
237 | char sec_objname[MODULE_NAME_LEN]; | |
238 | Elf_Shdr *sec; | |
b700e7f0 SJ |
239 | |
240 | if (WARN_ON(!klp_is_object_loaded(obj))) | |
241 | return -EINVAL; | |
242 | ||
425595a7 | 243 | objname = klp_is_module(obj) ? obj->name : "vmlinux"; |
b700e7f0 | 244 | |
425595a7 JY |
245 | /* 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; | |
b56b36ee | 251 | |
425595a7 JY |
252 | /* |
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; | |
263 | } | |
b56b36ee | 264 | |
425595a7 JY |
265 | if (strcmp(objname, sec_objname)) |
266 | continue; | |
b56b36ee | 267 | |
425595a7 | 268 | ret = klp_resolve_symbols(sec, pmod); |
064c89df | 269 | if (ret) |
425595a7 | 270 | break; |
064c89df | 271 | |
425595a7 JY |
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; | |
b700e7f0 SJ |
277 | } |
278 | ||
b56b36ee | 279 | return ret; |
b700e7f0 SJ |
280 | } |
281 | ||
b700e7f0 SJ |
282 | static int __klp_disable_patch(struct klp_patch *patch) |
283 | { | |
284 | struct klp_object *obj; | |
b700e7f0 | 285 | |
83a90bb1 JP |
286 | /* enforce stacking: only the last enabled patch can be disabled */ |
287 | if (!list_is_last(&patch->list, &klp_patches) && | |
0dade9f3 | 288 | list_next_entry(patch, list)->enabled) |
83a90bb1 JP |
289 | return -EBUSY; |
290 | ||
b700e7f0 SJ |
291 | pr_notice("disabling patch '%s'\n", patch->mod->name); |
292 | ||
8cdd043a | 293 | klp_for_each_object(patch, obj) { |
0dade9f3 JP |
294 | if (obj->patched) |
295 | klp_unpatch_object(obj); | |
b700e7f0 SJ |
296 | } |
297 | ||
0dade9f3 | 298 | patch->enabled = false; |
b700e7f0 SJ |
299 | |
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 | */ | |
311 | int 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 | ||
0dade9f3 | 322 | if (!patch->enabled) { |
b700e7f0 SJ |
323 | ret = -EINVAL; |
324 | goto err; | |
325 | } | |
326 | ||
327 | ret = __klp_disable_patch(patch); | |
328 | ||
329 | err: | |
330 | mutex_unlock(&klp_mutex); | |
331 | return ret; | |
332 | } | |
333 | EXPORT_SYMBOL_GPL(klp_disable_patch); | |
334 | ||
335 | static int __klp_enable_patch(struct klp_patch *patch) | |
336 | { | |
337 | struct klp_object *obj; | |
338 | int ret; | |
339 | ||
0dade9f3 | 340 | if (WARN_ON(patch->enabled)) |
b700e7f0 SJ |
341 | return -EINVAL; |
342 | ||
83a90bb1 JP |
343 | /* enforce stacking: only the first disabled patch can be enabled */ |
344 | if (patch->list.prev != &klp_patches && | |
0dade9f3 | 345 | !list_prev_entry(patch, list)->enabled) |
83a90bb1 JP |
346 | return -EBUSY; |
347 | ||
b700e7f0 SJ |
348 | pr_notice("enabling patch '%s'\n", patch->mod->name); |
349 | ||
8cdd043a | 350 | klp_for_each_object(patch, obj) { |
b700e7f0 SJ |
351 | if (!klp_is_object_loaded(obj)) |
352 | continue; | |
353 | ||
0dade9f3 | 354 | ret = klp_patch_object(obj); |
b700e7f0 SJ |
355 | if (ret) |
356 | goto unregister; | |
357 | } | |
358 | ||
0dade9f3 | 359 | patch->enabled = true; |
b700e7f0 SJ |
360 | |
361 | return 0; | |
362 | ||
363 | unregister: | |
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 | */ | |
377 | int 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 | ||
390 | err: | |
391 | mutex_unlock(&klp_mutex); | |
392 | return ret; | |
393 | } | |
394 | EXPORT_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> | |
444f9e99 | 403 | * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> |
b700e7f0 SJ |
404 | */ |
405 | ||
406 | static 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; | |
411 | unsigned long val; | |
412 | ||
413 | ret = kstrtoul(buf, 10, &val); | |
414 | if (ret) | |
415 | return -EINVAL; | |
416 | ||
0dade9f3 | 417 | if (val > 1) |
b700e7f0 SJ |
418 | return -EINVAL; |
419 | ||
420 | patch = container_of(kobj, struct klp_patch, kobj); | |
421 | ||
422 | mutex_lock(&klp_mutex); | |
423 | ||
0dade9f3 | 424 | if (patch->enabled == val) { |
b700e7f0 SJ |
425 | /* already in requested state */ |
426 | ret = -EINVAL; | |
427 | goto err; | |
428 | } | |
429 | ||
0dade9f3 | 430 | if (val) { |
b700e7f0 SJ |
431 | ret = __klp_enable_patch(patch); |
432 | if (ret) | |
433 | goto err; | |
434 | } else { | |
435 | ret = __klp_disable_patch(patch); | |
436 | if (ret) | |
437 | goto err; | |
438 | } | |
439 | ||
440 | mutex_unlock(&klp_mutex); | |
441 | ||
442 | return count; | |
443 | ||
444 | err: | |
445 | mutex_unlock(&klp_mutex); | |
446 | return ret; | |
447 | } | |
448 | ||
449 | static ssize_t enabled_show(struct kobject *kobj, | |
450 | struct kobj_attribute *attr, char *buf) | |
451 | { | |
452 | struct klp_patch *patch; | |
453 | ||
454 | patch = container_of(kobj, struct klp_patch, kobj); | |
0dade9f3 | 455 | return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled); |
b700e7f0 SJ |
456 | } |
457 | ||
458 | static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); | |
459 | static struct attribute *klp_patch_attrs[] = { | |
460 | &enabled_kobj_attr.attr, | |
461 | NULL | |
462 | }; | |
463 | ||
464 | static void klp_kobj_release_patch(struct kobject *kobj) | |
465 | { | |
466 | /* | |
467 | * Once we have a consistency model we'll need to module_put() the | |
468 | * patch module here. See klp_register_patch() for more details. | |
469 | */ | |
470 | } | |
471 | ||
472 | static struct kobj_type klp_ktype_patch = { | |
473 | .release = klp_kobj_release_patch, | |
474 | .sysfs_ops = &kobj_sysfs_ops, | |
475 | .default_attrs = klp_patch_attrs, | |
476 | }; | |
477 | ||
cad706df MB |
478 | static void klp_kobj_release_object(struct kobject *kobj) |
479 | { | |
480 | } | |
481 | ||
482 | static struct kobj_type klp_ktype_object = { | |
483 | .release = klp_kobj_release_object, | |
484 | .sysfs_ops = &kobj_sysfs_ops, | |
485 | }; | |
486 | ||
b700e7f0 SJ |
487 | static void klp_kobj_release_func(struct kobject *kobj) |
488 | { | |
b700e7f0 SJ |
489 | } |
490 | ||
491 | static struct kobj_type klp_ktype_func = { | |
492 | .release = klp_kobj_release_func, | |
493 | .sysfs_ops = &kobj_sysfs_ops, | |
494 | }; | |
495 | ||
496 | /* | |
497 | * Free all functions' kobjects in the array up to some limit. When limit is | |
498 | * NULL, all kobjects are freed. | |
499 | */ | |
500 | static void klp_free_funcs_limited(struct klp_object *obj, | |
501 | struct klp_func *limit) | |
502 | { | |
503 | struct klp_func *func; | |
504 | ||
505 | for (func = obj->funcs; func->old_name && func != limit; func++) | |
506 | kobject_put(&func->kobj); | |
507 | } | |
508 | ||
509 | /* Clean up when a patched object is unloaded */ | |
510 | static void klp_free_object_loaded(struct klp_object *obj) | |
511 | { | |
512 | struct klp_func *func; | |
513 | ||
514 | obj->mod = NULL; | |
515 | ||
8cdd043a | 516 | klp_for_each_func(obj, func) |
b700e7f0 SJ |
517 | func->old_addr = 0; |
518 | } | |
519 | ||
520 | /* | |
521 | * Free all objects' kobjects in the array up to some limit. When limit is | |
522 | * NULL, all kobjects are freed. | |
523 | */ | |
524 | static void klp_free_objects_limited(struct klp_patch *patch, | |
525 | struct klp_object *limit) | |
526 | { | |
527 | struct klp_object *obj; | |
528 | ||
529 | for (obj = patch->objs; obj->funcs && obj != limit; obj++) { | |
530 | klp_free_funcs_limited(obj, NULL); | |
cad706df | 531 | kobject_put(&obj->kobj); |
b700e7f0 SJ |
532 | } |
533 | } | |
534 | ||
535 | static void klp_free_patch(struct klp_patch *patch) | |
536 | { | |
537 | klp_free_objects_limited(patch, NULL); | |
538 | if (!list_empty(&patch->list)) | |
539 | list_del(&patch->list); | |
540 | kobject_put(&patch->kobj); | |
541 | } | |
542 | ||
543 | static int klp_init_func(struct klp_object *obj, struct klp_func *func) | |
544 | { | |
f09d9086 MB |
545 | if (!func->old_name || !func->new_func) |
546 | return -EINVAL; | |
547 | ||
3c33f5b9 | 548 | INIT_LIST_HEAD(&func->stack_node); |
0dade9f3 | 549 | func->patched = false; |
b700e7f0 | 550 | |
444f9e99 CA |
551 | /* The format for the sysfs directory is <function,sympos> where sympos |
552 | * is the nth occurrence of this symbol in kallsyms for the patched | |
553 | * object. If the user selects 0 for old_sympos, then 1 will be used | |
554 | * since a unique symbol will be the first occurrence. | |
555 | */ | |
3c33f5b9 | 556 | return kobject_init_and_add(&func->kobj, &klp_ktype_func, |
444f9e99 CA |
557 | &obj->kobj, "%s,%lu", func->old_name, |
558 | func->old_sympos ? func->old_sympos : 1); | |
b700e7f0 SJ |
559 | } |
560 | ||
255e732c JY |
561 | /* Arches may override this to finish any remaining arch-specific tasks */ |
562 | void __weak arch_klp_init_object_loaded(struct klp_patch *patch, | |
563 | struct klp_object *obj) | |
564 | { | |
565 | } | |
566 | ||
b700e7f0 SJ |
567 | /* parts of the initialization that is done only when the object is loaded */ |
568 | static int klp_init_object_loaded(struct klp_patch *patch, | |
569 | struct klp_object *obj) | |
570 | { | |
571 | struct klp_func *func; | |
572 | int ret; | |
573 | ||
255e732c | 574 | module_disable_ro(patch->mod); |
425595a7 | 575 | ret = klp_write_object_relocations(patch->mod, obj); |
255e732c JY |
576 | if (ret) { |
577 | module_enable_ro(patch->mod, true); | |
425595a7 | 578 | return ret; |
255e732c JY |
579 | } |
580 | ||
581 | arch_klp_init_object_loaded(patch, obj); | |
582 | module_enable_ro(patch->mod, true); | |
b700e7f0 | 583 | |
8cdd043a | 584 | klp_for_each_func(obj, func) { |
b2b018ef CA |
585 | ret = klp_find_object_symbol(obj->name, func->old_name, |
586 | func->old_sympos, | |
587 | &func->old_addr); | |
b700e7f0 SJ |
588 | if (ret) |
589 | return ret; | |
590 | } | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) | |
596 | { | |
597 | struct klp_func *func; | |
598 | int ret; | |
599 | const char *name; | |
600 | ||
601 | if (!obj->funcs) | |
602 | return -EINVAL; | |
603 | ||
0dade9f3 | 604 | obj->patched = false; |
8cb2c2dc | 605 | obj->mod = NULL; |
b700e7f0 SJ |
606 | |
607 | klp_find_object_module(obj); | |
608 | ||
609 | name = klp_is_module(obj) ? obj->name : "vmlinux"; | |
cad706df MB |
610 | ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object, |
611 | &patch->kobj, "%s", name); | |
612 | if (ret) | |
613 | return ret; | |
b700e7f0 | 614 | |
8cdd043a | 615 | klp_for_each_func(obj, func) { |
b700e7f0 SJ |
616 | ret = klp_init_func(obj, func); |
617 | if (ret) | |
618 | goto free; | |
619 | } | |
620 | ||
621 | if (klp_is_object_loaded(obj)) { | |
622 | ret = klp_init_object_loaded(patch, obj); | |
623 | if (ret) | |
624 | goto free; | |
625 | } | |
626 | ||
627 | return 0; | |
628 | ||
629 | free: | |
630 | klp_free_funcs_limited(obj, func); | |
cad706df | 631 | kobject_put(&obj->kobj); |
b700e7f0 SJ |
632 | return ret; |
633 | } | |
634 | ||
635 | static int klp_init_patch(struct klp_patch *patch) | |
636 | { | |
637 | struct klp_object *obj; | |
638 | int ret; | |
639 | ||
640 | if (!patch->objs) | |
641 | return -EINVAL; | |
642 | ||
643 | mutex_lock(&klp_mutex); | |
644 | ||
0dade9f3 | 645 | patch->enabled = false; |
b700e7f0 SJ |
646 | |
647 | ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, | |
e0b561ee | 648 | klp_root_kobj, "%s", patch->mod->name); |
b700e7f0 SJ |
649 | if (ret) |
650 | goto unlock; | |
651 | ||
8cdd043a | 652 | klp_for_each_object(patch, obj) { |
b700e7f0 SJ |
653 | ret = klp_init_object(patch, obj); |
654 | if (ret) | |
655 | goto free; | |
656 | } | |
657 | ||
99590ba5 | 658 | list_add_tail(&patch->list, &klp_patches); |
b700e7f0 SJ |
659 | |
660 | mutex_unlock(&klp_mutex); | |
661 | ||
662 | return 0; | |
663 | ||
664 | free: | |
665 | klp_free_objects_limited(patch, obj); | |
666 | kobject_put(&patch->kobj); | |
667 | unlock: | |
668 | mutex_unlock(&klp_mutex); | |
669 | return ret; | |
670 | } | |
671 | ||
672 | /** | |
673 | * klp_unregister_patch() - unregisters a patch | |
674 | * @patch: Disabled patch to be unregistered | |
675 | * | |
676 | * Frees the data structures and removes the sysfs interface. | |
677 | * | |
678 | * Return: 0 on success, otherwise error | |
679 | */ | |
680 | int klp_unregister_patch(struct klp_patch *patch) | |
681 | { | |
682 | int ret = 0; | |
683 | ||
684 | mutex_lock(&klp_mutex); | |
685 | ||
686 | if (!klp_is_patch_registered(patch)) { | |
687 | ret = -EINVAL; | |
688 | goto out; | |
689 | } | |
690 | ||
0dade9f3 | 691 | if (patch->enabled) { |
b700e7f0 SJ |
692 | ret = -EBUSY; |
693 | goto out; | |
694 | } | |
695 | ||
696 | klp_free_patch(patch); | |
697 | ||
698 | out: | |
699 | mutex_unlock(&klp_mutex); | |
700 | return ret; | |
701 | } | |
702 | EXPORT_SYMBOL_GPL(klp_unregister_patch); | |
703 | ||
704 | /** | |
705 | * klp_register_patch() - registers a patch | |
706 | * @patch: Patch to be registered | |
707 | * | |
708 | * Initializes the data structure associated with the patch and | |
709 | * creates the sysfs interface. | |
710 | * | |
711 | * Return: 0 on success, otherwise error | |
712 | */ | |
713 | int klp_register_patch(struct klp_patch *patch) | |
714 | { | |
715 | int ret; | |
716 | ||
b700e7f0 SJ |
717 | if (!patch || !patch->mod) |
718 | return -EINVAL; | |
719 | ||
425595a7 JY |
720 | if (!is_livepatch_module(patch->mod)) { |
721 | pr_err("module %s is not marked as a livepatch module", | |
722 | patch->mod->name); | |
723 | return -EINVAL; | |
724 | } | |
725 | ||
b700e7f0 SJ |
726 | if (!klp_initialized()) |
727 | return -ENODEV; | |
728 | ||
b700e7f0 SJ |
729 | /* |
730 | * A reference is taken on the patch module to prevent it from being | |
731 | * unloaded. Right now, we don't allow patch modules to unload since | |
732 | * there is currently no method to determine if a thread is still | |
733 | * running in the patched code contained in the patch module once | |
734 | * the ftrace registration is successful. | |
735 | */ | |
736 | if (!try_module_get(patch->mod)) | |
737 | return -ENODEV; | |
738 | ||
739 | ret = klp_init_patch(patch); | |
740 | if (ret) | |
741 | module_put(patch->mod); | |
742 | ||
743 | return ret; | |
744 | } | |
745 | EXPORT_SYMBOL_GPL(klp_register_patch); | |
746 | ||
7e545d6e | 747 | int klp_module_coming(struct module *mod) |
b700e7f0 | 748 | { |
b700e7f0 | 749 | int ret; |
7e545d6e JY |
750 | struct klp_patch *patch; |
751 | struct klp_object *obj; | |
b700e7f0 | 752 | |
7e545d6e JY |
753 | if (WARN_ON(mod->state != MODULE_STATE_COMING)) |
754 | return -EINVAL; | |
b700e7f0 | 755 | |
7e545d6e JY |
756 | mutex_lock(&klp_mutex); |
757 | /* | |
758 | * Each module has to know that klp_module_coming() | |
759 | * has been called. We never know what module will | |
760 | * get patched by a new patch. | |
761 | */ | |
762 | mod->klp_alive = true; | |
b700e7f0 | 763 | |
7e545d6e JY |
764 | list_for_each_entry(patch, &klp_patches, list) { |
765 | klp_for_each_object(patch, obj) { | |
766 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) | |
767 | continue; | |
b700e7f0 | 768 | |
7e545d6e | 769 | obj->mod = mod; |
b700e7f0 | 770 | |
7e545d6e JY |
771 | ret = klp_init_object_loaded(patch, obj); |
772 | if (ret) { | |
773 | pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", | |
774 | patch->mod->name, obj->mod->name, ret); | |
775 | goto err; | |
776 | } | |
b700e7f0 | 777 | |
0dade9f3 | 778 | if (!patch->enabled) |
7e545d6e JY |
779 | break; |
780 | ||
781 | pr_notice("applying patch '%s' to loading module '%s'\n", | |
782 | patch->mod->name, obj->mod->name); | |
783 | ||
0dade9f3 | 784 | ret = klp_patch_object(obj); |
7e545d6e JY |
785 | if (ret) { |
786 | pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", | |
787 | patch->mod->name, obj->mod->name, ret); | |
788 | goto err; | |
789 | } | |
790 | ||
791 | break; | |
792 | } | |
793 | } | |
b700e7f0 | 794 | |
7e545d6e | 795 | mutex_unlock(&klp_mutex); |
b700e7f0 | 796 | |
7e545d6e | 797 | return 0; |
b700e7f0 | 798 | |
7e545d6e JY |
799 | err: |
800 | /* | |
801 | * If a patch is unsuccessfully applied, return | |
802 | * error to the module loader. | |
803 | */ | |
804 | pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n", | |
805 | patch->mod->name, obj->mod->name, obj->mod->name); | |
806 | mod->klp_alive = false; | |
b700e7f0 | 807 | klp_free_object_loaded(obj); |
7e545d6e JY |
808 | mutex_unlock(&klp_mutex); |
809 | ||
810 | return ret; | |
b700e7f0 SJ |
811 | } |
812 | ||
7e545d6e | 813 | void klp_module_going(struct module *mod) |
b700e7f0 | 814 | { |
b700e7f0 SJ |
815 | struct klp_patch *patch; |
816 | struct klp_object *obj; | |
817 | ||
7e545d6e JY |
818 | if (WARN_ON(mod->state != MODULE_STATE_GOING && |
819 | mod->state != MODULE_STATE_COMING)) | |
820 | return; | |
b700e7f0 SJ |
821 | |
822 | mutex_lock(&klp_mutex); | |
8cb2c2dc | 823 | /* |
7e545d6e JY |
824 | * Each module has to know that klp_module_going() |
825 | * has been called. We never know what module will | |
826 | * get patched by a new patch. | |
8cb2c2dc | 827 | */ |
7e545d6e | 828 | mod->klp_alive = false; |
8cb2c2dc | 829 | |
b700e7f0 | 830 | list_for_each_entry(patch, &klp_patches, list) { |
8cdd043a | 831 | klp_for_each_object(patch, obj) { |
b700e7f0 SJ |
832 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) |
833 | continue; | |
834 | ||
0dade9f3 | 835 | if (patch->enabled) { |
7e545d6e JY |
836 | pr_notice("reverting patch '%s' on unloading module '%s'\n", |
837 | patch->mod->name, obj->mod->name); | |
0dade9f3 | 838 | klp_unpatch_object(obj); |
7e545d6e | 839 | } |
b700e7f0 | 840 | |
7e545d6e | 841 | klp_free_object_loaded(obj); |
b700e7f0 SJ |
842 | break; |
843 | } | |
844 | } | |
845 | ||
846 | mutex_unlock(&klp_mutex); | |
b700e7f0 SJ |
847 | } |
848 | ||
26029d88 | 849 | static int __init klp_init(void) |
b700e7f0 SJ |
850 | { |
851 | int ret; | |
852 | ||
b9dfe0be JK |
853 | ret = klp_check_compiler_support(); |
854 | if (ret) { | |
855 | pr_info("Your compiler is too old; turning off.\n"); | |
856 | return -EINVAL; | |
857 | } | |
858 | ||
b700e7f0 | 859 | klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj); |
7e545d6e JY |
860 | if (!klp_root_kobj) |
861 | return -ENOMEM; | |
b700e7f0 SJ |
862 | |
863 | return 0; | |
b700e7f0 SJ |
864 | } |
865 | ||
866 | module_init(klp_init); |