1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * core.c - Kernel Live Patching Core 4 * 5 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> 6 * Copyright (C) 2014 SUSE 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/mutex.h> 14 #include <linux/slab.h> 15 #include <linux/list.h> 16 #include <linux/kallsyms.h> 17 #include <linux/livepatch.h> 18 #include <linux/elf.h> 19 #include <linux/moduleloader.h> 20 #include <linux/completion.h> 21 #include <linux/memory.h> 22 #include <linux/rcupdate.h> 23 #include <asm/cacheflush.h> 24 #include "core.h" 25 #include "patch.h" 26 #include "state.h" 27 #include "transition.h" 28 29 /* 30 * klp_mutex is a coarse lock which serializes access to klp data. All 31 * accesses to klp-related variables and structures must have mutex protection, 32 * except within the following functions which carefully avoid the need for it: 33 * 34 * - klp_ftrace_handler() 35 * - klp_update_patch_state() 36 * - __klp_sched_try_switch() 37 */ 38 DEFINE_MUTEX(klp_mutex); 39 40 /* 41 * Actively used patches: enabled or in transition. Note that replaced 42 * or disabled patches are not listed even though the related kernel 43 * module still can be loaded. 44 */ 45 LIST_HEAD(klp_patches); 46 47 static struct kobject *klp_root_kobj; 48 49 static bool klp_is_module(struct klp_object *obj) 50 { 51 return obj->name; 52 } 53 54 /* sets obj->mod if object is not vmlinux and module is found */ 55 static void klp_find_object_module(struct klp_object *obj) 56 { 57 struct module *mod; 58 59 if (!klp_is_module(obj)) 60 return; 61 62 rcu_read_lock_sched(); 63 /* 64 * We do not want to block removal of patched modules and therefore 65 * we do not take a reference here. The patches are removed by 66 * klp_module_going() instead. 67 */ 68 mod = find_module(obj->name); 69 /* 70 * Do not mess work of klp_module_coming() and klp_module_going(). 71 * Note that the patch might still be needed before klp_module_going() 72 * is called. Module functions can be called even in the GOING state 73 * until mod->exit() finishes. This is especially important for 74 * patches that modify semantic of the functions. 75 */ 76 if (mod && mod->klp_alive) 77 obj->mod = mod; 78 79 rcu_read_unlock_sched(); 80 } 81 82 static bool klp_initialized(void) 83 { 84 return !!klp_root_kobj; 85 } 86 87 static struct klp_func *klp_find_func(struct klp_object *obj, 88 struct klp_func *old_func) 89 { 90 struct klp_func *func; 91 92 klp_for_each_func(obj, func) { 93 if ((strcmp(old_func->old_name, func->old_name) == 0) && 94 (old_func->old_sympos == func->old_sympos)) { 95 return func; 96 } 97 } 98 99 return NULL; 100 } 101 102 static struct klp_object *klp_find_object(struct klp_patch *patch, 103 struct klp_object *old_obj) 104 { 105 struct klp_object *obj; 106 107 klp_for_each_object(patch, obj) { 108 if (klp_is_module(old_obj)) { 109 if (klp_is_module(obj) && 110 strcmp(old_obj->name, obj->name) == 0) { 111 return obj; 112 } 113 } else if (!klp_is_module(obj)) { 114 return obj; 115 } 116 } 117 118 return NULL; 119 } 120 121 struct klp_find_arg { 122 const char *name; 123 unsigned long addr; 124 unsigned long count; 125 unsigned long pos; 126 }; 127 128 static int klp_match_callback(void *data, unsigned long addr) 129 { 130 struct klp_find_arg *args = data; 131 132 args->addr = addr; 133 args->count++; 134 135 /* 136 * Finish the search when the symbol is found for the desired position 137 * or the position is not defined for a non-unique symbol. 138 */ 139 if ((args->pos && (args->count == args->pos)) || 140 (!args->pos && (args->count > 1))) 141 return 1; 142 143 return 0; 144 } 145 146 static int klp_find_callback(void *data, const char *name, unsigned long addr) 147 { 148 struct klp_find_arg *args = data; 149 150 if (strcmp(args->name, name)) 151 return 0; 152 153 return klp_match_callback(data, addr); 154 } 155 156 static int klp_find_object_symbol(const char *objname, const char *name, 157 unsigned long sympos, unsigned long *addr) 158 { 159 struct klp_find_arg args = { 160 .name = name, 161 .addr = 0, 162 .count = 0, 163 .pos = sympos, 164 }; 165 166 if (objname) 167 module_kallsyms_on_each_symbol(objname, klp_find_callback, &args); 168 else 169 kallsyms_on_each_match_symbol(klp_match_callback, name, &args); 170 171 /* 172 * Ensure an address was found. If sympos is 0, ensure symbol is unique; 173 * otherwise ensure the symbol position count matches sympos. 174 */ 175 if (args.addr == 0) 176 pr_err("symbol '%s' not found in symbol table\n", name); 177 else if (args.count > 1 && sympos == 0) { 178 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n", 179 name, objname); 180 } else if (sympos != args.count && sympos > 0) { 181 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n", 182 sympos, name, objname ? objname : "vmlinux"); 183 } else { 184 *addr = args.addr; 185 return 0; 186 } 187 188 *addr = 0; 189 return -EINVAL; 190 } 191 192 static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab, 193 unsigned int symndx, Elf_Shdr *relasec, 194 const char *sec_objname) 195 { 196 int i, cnt, ret; 197 char sym_objname[MODULE_NAME_LEN]; 198 char sym_name[KSYM_NAME_LEN]; 199 Elf_Rela *relas; 200 Elf_Sym *sym; 201 unsigned long sympos, addr; 202 bool sym_vmlinux; 203 bool sec_vmlinux = !strcmp(sec_objname, "vmlinux"); 204 205 /* 206 * Since the field widths for sym_objname and sym_name in the sscanf() 207 * call are hard-coded and correspond to MODULE_NAME_LEN and 208 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN 209 * and KSYM_NAME_LEN have the values we expect them to have. 210 * 211 * Because the value of MODULE_NAME_LEN can differ among architectures, 212 * we use the smallest/strictest upper bound possible (56, based on 213 * the current definition of MODULE_NAME_LEN) to prevent overflows. 214 */ 215 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 512); 216 217 relas = (Elf_Rela *) relasec->sh_addr; 218 /* For each rela in this klp relocation section */ 219 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) { 220 sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info); 221 if (sym->st_shndx != SHN_LIVEPATCH) { 222 pr_err("symbol %s is not marked as a livepatch symbol\n", 223 strtab + sym->st_name); 224 return -EINVAL; 225 } 226 227 /* Format: .klp.sym.sym_objname.sym_name,sympos */ 228 cnt = sscanf(strtab + sym->st_name, 229 ".klp.sym.%55[^.].%511[^,],%lu", 230 sym_objname, sym_name, &sympos); 231 if (cnt != 3) { 232 pr_err("symbol %s has an incorrectly formatted name\n", 233 strtab + sym->st_name); 234 return -EINVAL; 235 } 236 237 sym_vmlinux = !strcmp(sym_objname, "vmlinux"); 238 239 /* 240 * Prevent module-specific KLP rela sections from referencing 241 * vmlinux symbols. This helps prevent ordering issues with 242 * module special section initializations. Presumably such 243 * symbols are exported and normal relas can be used instead. 244 */ 245 if (!sec_vmlinux && sym_vmlinux) { 246 pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section\n", 247 sym_name); 248 return -EINVAL; 249 } 250 251 /* klp_find_object_symbol() treats a NULL objname as vmlinux */ 252 ret = klp_find_object_symbol(sym_vmlinux ? NULL : sym_objname, 253 sym_name, sympos, &addr); 254 if (ret) 255 return ret; 256 257 sym->st_value = addr; 258 } 259 260 return 0; 261 } 262 263 void __weak clear_relocate_add(Elf_Shdr *sechdrs, 264 const char *strtab, 265 unsigned int symindex, 266 unsigned int relsec, 267 struct module *me) 268 { 269 } 270 271 /* 272 * At a high-level, there are two types of klp relocation sections: those which 273 * reference symbols which live in vmlinux; and those which reference symbols 274 * which live in other modules. This function is called for both types: 275 * 276 * 1) When a klp module itself loads, the module code calls this function to 277 * write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections). 278 * These relocations are written to the klp module text to allow the patched 279 * code/data to reference unexported vmlinux symbols. They're written as 280 * early as possible to ensure that other module init code (.e.g., 281 * jump_label_apply_nops) can access any unexported vmlinux symbols which 282 * might be referenced by the klp module's special sections. 283 * 284 * 2) When a to-be-patched module loads -- or is already loaded when a 285 * corresponding klp module loads -- klp code calls this function to write 286 * module-specific klp relocations (.klp.rela.{module}.* sections). These 287 * are written to the klp module text to allow the patched code/data to 288 * reference symbols which live in the to-be-patched module or one of its 289 * module dependencies. Exported symbols are supported, in addition to 290 * unexported symbols, in order to enable late module patching, which allows 291 * the to-be-patched module to be loaded and patched sometime *after* the 292 * klp module is loaded. 293 */ 294 static int klp_write_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, 295 const char *shstrtab, const char *strtab, 296 unsigned int symndx, unsigned int secndx, 297 const char *objname, bool apply) 298 { 299 int cnt, ret; 300 char sec_objname[MODULE_NAME_LEN]; 301 Elf_Shdr *sec = sechdrs + secndx; 302 303 /* 304 * Format: .klp.rela.sec_objname.section_name 305 * See comment in klp_resolve_symbols() for an explanation 306 * of the selected field width value. 307 */ 308 cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]", 309 sec_objname); 310 if (cnt != 1) { 311 pr_err("section %s has an incorrectly formatted name\n", 312 shstrtab + sec->sh_name); 313 return -EINVAL; 314 } 315 316 if (strcmp(objname ? objname : "vmlinux", sec_objname)) 317 return 0; 318 319 if (apply) { 320 ret = klp_resolve_symbols(sechdrs, strtab, symndx, 321 sec, sec_objname); 322 if (ret) 323 return ret; 324 325 return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod); 326 } 327 328 clear_relocate_add(sechdrs, strtab, symndx, secndx, pmod); 329 return 0; 330 } 331 332 int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, 333 const char *shstrtab, const char *strtab, 334 unsigned int symndx, unsigned int secndx, 335 const char *objname) 336 { 337 return klp_write_section_relocs(pmod, sechdrs, shstrtab, strtab, symndx, 338 secndx, objname, true); 339 } 340 341 /* 342 * Sysfs Interface 343 * 344 * /sys/kernel/livepatch 345 * /sys/kernel/livepatch/<patch> 346 * /sys/kernel/livepatch/<patch>/enabled 347 * /sys/kernel/livepatch/<patch>/transition 348 * /sys/kernel/livepatch/<patch>/force 349 * /sys/kernel/livepatch/<patch>/replace 350 * /sys/kernel/livepatch/<patch>/stack_order 351 * /sys/kernel/livepatch/<patch>/<object> 352 * /sys/kernel/livepatch/<patch>/<object>/patched 353 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> 354 */ 355 static int __klp_disable_patch(struct klp_patch *patch); 356 357 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, 358 const char *buf, size_t count) 359 { 360 struct klp_patch *patch; 361 int ret; 362 bool enabled; 363 364 ret = kstrtobool(buf, &enabled); 365 if (ret) 366 return ret; 367 368 patch = container_of(kobj, struct klp_patch, kobj); 369 370 mutex_lock(&klp_mutex); 371 372 if (patch->enabled == enabled) { 373 /* already in requested state */ 374 ret = -EINVAL; 375 goto out; 376 } 377 378 /* 379 * Allow to reverse a pending transition in both ways. It might be 380 * necessary to complete the transition without forcing and breaking 381 * the system integrity. 382 * 383 * Do not allow to re-enable a disabled patch. 384 */ 385 if (patch == klp_transition_patch) 386 klp_reverse_transition(); 387 else if (!enabled) 388 ret = __klp_disable_patch(patch); 389 else 390 ret = -EINVAL; 391 392 out: 393 mutex_unlock(&klp_mutex); 394 395 if (ret) 396 return ret; 397 return count; 398 } 399 400 static ssize_t enabled_show(struct kobject *kobj, 401 struct kobj_attribute *attr, char *buf) 402 { 403 struct klp_patch *patch; 404 405 patch = container_of(kobj, struct klp_patch, kobj); 406 return sysfs_emit(buf, "%d\n", patch->enabled); 407 } 408 409 static ssize_t transition_show(struct kobject *kobj, 410 struct kobj_attribute *attr, char *buf) 411 { 412 struct klp_patch *patch; 413 414 patch = container_of(kobj, struct klp_patch, kobj); 415 return sysfs_emit(buf, "%d\n", patch == klp_transition_patch); 416 } 417 418 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 419 const char *buf, size_t count) 420 { 421 struct klp_patch *patch; 422 int ret; 423 bool val; 424 425 ret = kstrtobool(buf, &val); 426 if (ret) 427 return ret; 428 429 if (!val) 430 return count; 431 432 mutex_lock(&klp_mutex); 433 434 patch = container_of(kobj, struct klp_patch, kobj); 435 if (patch != klp_transition_patch) { 436 mutex_unlock(&klp_mutex); 437 return -EINVAL; 438 } 439 440 klp_force_transition(); 441 442 mutex_unlock(&klp_mutex); 443 444 return count; 445 } 446 447 static ssize_t replace_show(struct kobject *kobj, 448 struct kobj_attribute *attr, char *buf) 449 { 450 struct klp_patch *patch; 451 452 patch = container_of(kobj, struct klp_patch, kobj); 453 return sysfs_emit(buf, "%d\n", patch->replace); 454 } 455 456 static ssize_t stack_order_show(struct kobject *kobj, 457 struct kobj_attribute *attr, char *buf) 458 { 459 struct klp_patch *patch, *this_patch; 460 int stack_order = 0; 461 462 this_patch = container_of(kobj, struct klp_patch, kobj); 463 464 mutex_lock(&klp_mutex); 465 466 klp_for_each_patch(patch) { 467 stack_order++; 468 if (patch == this_patch) 469 break; 470 } 471 472 mutex_unlock(&klp_mutex); 473 474 return sysfs_emit(buf, "%d\n", stack_order); 475 } 476 477 static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); 478 static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition); 479 static struct kobj_attribute force_kobj_attr = __ATTR_WO(force); 480 static struct kobj_attribute replace_kobj_attr = __ATTR_RO(replace); 481 static struct kobj_attribute stack_order_kobj_attr = __ATTR_RO(stack_order); 482 static struct attribute *klp_patch_attrs[] = { 483 &enabled_kobj_attr.attr, 484 &transition_kobj_attr.attr, 485 &force_kobj_attr.attr, 486 &replace_kobj_attr.attr, 487 &stack_order_kobj_attr.attr, 488 NULL 489 }; 490 ATTRIBUTE_GROUPS(klp_patch); 491 492 static ssize_t patched_show(struct kobject *kobj, 493 struct kobj_attribute *attr, char *buf) 494 { 495 struct klp_object *obj; 496 497 obj = container_of(kobj, struct klp_object, kobj); 498 return sysfs_emit(buf, "%d\n", obj->patched); 499 } 500 501 static struct kobj_attribute patched_kobj_attr = __ATTR_RO(patched); 502 static struct attribute *klp_object_attrs[] = { 503 &patched_kobj_attr.attr, 504 NULL, 505 }; 506 ATTRIBUTE_GROUPS(klp_object); 507 508 static void klp_free_object_dynamic(struct klp_object *obj) 509 { 510 kfree(obj->name); 511 kfree(obj); 512 } 513 514 static void klp_init_func_early(struct klp_object *obj, 515 struct klp_func *func); 516 static void klp_init_object_early(struct klp_patch *patch, 517 struct klp_object *obj); 518 519 static struct klp_object *klp_alloc_object_dynamic(const char *name, 520 struct klp_patch *patch) 521 { 522 struct klp_object *obj; 523 524 obj = kzalloc(sizeof(*obj), GFP_KERNEL); 525 if (!obj) 526 return NULL; 527 528 if (name) { 529 obj->name = kstrdup(name, GFP_KERNEL); 530 if (!obj->name) { 531 kfree(obj); 532 return NULL; 533 } 534 } 535 536 klp_init_object_early(patch, obj); 537 obj->dynamic = true; 538 539 return obj; 540 } 541 542 static void klp_free_func_nop(struct klp_func *func) 543 { 544 kfree(func->old_name); 545 kfree(func); 546 } 547 548 static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func, 549 struct klp_object *obj) 550 { 551 struct klp_func *func; 552 553 func = kzalloc(sizeof(*func), GFP_KERNEL); 554 if (!func) 555 return NULL; 556 557 if (old_func->old_name) { 558 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL); 559 if (!func->old_name) { 560 kfree(func); 561 return NULL; 562 } 563 } 564 565 klp_init_func_early(obj, func); 566 /* 567 * func->new_func is same as func->old_func. These addresses are 568 * set when the object is loaded, see klp_init_object_loaded(). 569 */ 570 func->old_sympos = old_func->old_sympos; 571 func->nop = true; 572 573 return func; 574 } 575 576 static int klp_add_object_nops(struct klp_patch *patch, 577 struct klp_object *old_obj) 578 { 579 struct klp_object *obj; 580 struct klp_func *func, *old_func; 581 582 obj = klp_find_object(patch, old_obj); 583 584 if (!obj) { 585 obj = klp_alloc_object_dynamic(old_obj->name, patch); 586 if (!obj) 587 return -ENOMEM; 588 } 589 590 klp_for_each_func(old_obj, old_func) { 591 func = klp_find_func(obj, old_func); 592 if (func) 593 continue; 594 595 func = klp_alloc_func_nop(old_func, obj); 596 if (!func) 597 return -ENOMEM; 598 } 599 600 return 0; 601 } 602 603 /* 604 * Add 'nop' functions which simply return to the caller to run the 605 * original function. 606 * 607 * They are added only when the atomic replace mode is used and only for 608 * functions which are currently livepatched but are no longer included 609 * in the new livepatch. 610 */ 611 static int klp_add_nops(struct klp_patch *patch) 612 { 613 struct klp_patch *old_patch; 614 struct klp_object *old_obj; 615 616 klp_for_each_patch(old_patch) { 617 klp_for_each_object(old_patch, old_obj) { 618 int err; 619 620 err = klp_add_object_nops(patch, old_obj); 621 if (err) 622 return err; 623 } 624 } 625 626 return 0; 627 } 628 629 static void klp_kobj_release_patch(struct kobject *kobj) 630 { 631 struct klp_patch *patch; 632 633 patch = container_of(kobj, struct klp_patch, kobj); 634 complete(&patch->finish); 635 } 636 637 static const struct kobj_type klp_ktype_patch = { 638 .release = klp_kobj_release_patch, 639 .sysfs_ops = &kobj_sysfs_ops, 640 .default_groups = klp_patch_groups, 641 }; 642 643 static void klp_kobj_release_object(struct kobject *kobj) 644 { 645 struct klp_object *obj; 646 647 obj = container_of(kobj, struct klp_object, kobj); 648 649 if (obj->dynamic) 650 klp_free_object_dynamic(obj); 651 } 652 653 static const struct kobj_type klp_ktype_object = { 654 .release = klp_kobj_release_object, 655 .sysfs_ops = &kobj_sysfs_ops, 656 .default_groups = klp_object_groups, 657 }; 658 659 static void klp_kobj_release_func(struct kobject *kobj) 660 { 661 struct klp_func *func; 662 663 func = container_of(kobj, struct klp_func, kobj); 664 665 if (func->nop) 666 klp_free_func_nop(func); 667 } 668 669 static const struct kobj_type klp_ktype_func = { 670 .release = klp_kobj_release_func, 671 .sysfs_ops = &kobj_sysfs_ops, 672 }; 673 674 static void __klp_free_funcs(struct klp_object *obj, bool nops_only) 675 { 676 struct klp_func *func, *tmp_func; 677 678 klp_for_each_func_safe(obj, func, tmp_func) { 679 if (nops_only && !func->nop) 680 continue; 681 682 list_del(&func->node); 683 kobject_put(&func->kobj); 684 } 685 } 686 687 /* Clean up when a patched object is unloaded */ 688 static void klp_free_object_loaded(struct klp_object *obj) 689 { 690 struct klp_func *func; 691 692 obj->mod = NULL; 693 694 klp_for_each_func(obj, func) { 695 func->old_func = NULL; 696 697 if (func->nop) 698 func->new_func = NULL; 699 } 700 } 701 702 static void __klp_free_objects(struct klp_patch *patch, bool nops_only) 703 { 704 struct klp_object *obj, *tmp_obj; 705 706 klp_for_each_object_safe(patch, obj, tmp_obj) { 707 __klp_free_funcs(obj, nops_only); 708 709 if (nops_only && !obj->dynamic) 710 continue; 711 712 list_del(&obj->node); 713 kobject_put(&obj->kobj); 714 } 715 } 716 717 static void klp_free_objects(struct klp_patch *patch) 718 { 719 __klp_free_objects(patch, false); 720 } 721 722 static void klp_free_objects_dynamic(struct klp_patch *patch) 723 { 724 __klp_free_objects(patch, true); 725 } 726 727 /* 728 * This function implements the free operations that can be called safely 729 * under klp_mutex. 730 * 731 * The operation must be completed by calling klp_free_patch_finish() 732 * outside klp_mutex. 733 */ 734 static void klp_free_patch_start(struct klp_patch *patch) 735 { 736 if (!list_empty(&patch->list)) 737 list_del(&patch->list); 738 739 klp_free_objects(patch); 740 } 741 742 /* 743 * This function implements the free part that must be called outside 744 * klp_mutex. 745 * 746 * It must be called after klp_free_patch_start(). And it has to be 747 * the last function accessing the livepatch structures when the patch 748 * gets disabled. 749 */ 750 static void klp_free_patch_finish(struct klp_patch *patch) 751 { 752 /* 753 * Avoid deadlock with enabled_store() sysfs callback by 754 * calling this outside klp_mutex. It is safe because 755 * this is called when the patch gets disabled and it 756 * cannot get enabled again. 757 */ 758 kobject_put(&patch->kobj); 759 wait_for_completion(&patch->finish); 760 761 /* Put the module after the last access to struct klp_patch. */ 762 if (!patch->forced) 763 module_put(patch->mod); 764 } 765 766 /* 767 * The livepatch might be freed from sysfs interface created by the patch. 768 * This work allows to wait until the interface is destroyed in a separate 769 * context. 770 */ 771 static void klp_free_patch_work_fn(struct work_struct *work) 772 { 773 struct klp_patch *patch = 774 container_of(work, struct klp_patch, free_work); 775 776 klp_free_patch_finish(patch); 777 } 778 779 void klp_free_patch_async(struct klp_patch *patch) 780 { 781 klp_free_patch_start(patch); 782 schedule_work(&patch->free_work); 783 } 784 785 void klp_free_replaced_patches_async(struct klp_patch *new_patch) 786 { 787 struct klp_patch *old_patch, *tmp_patch; 788 789 klp_for_each_patch_safe(old_patch, tmp_patch) { 790 if (old_patch == new_patch) 791 return; 792 klp_free_patch_async(old_patch); 793 } 794 } 795 796 static int klp_init_func(struct klp_object *obj, struct klp_func *func) 797 { 798 if (!func->old_name) 799 return -EINVAL; 800 801 /* 802 * NOPs get the address later. The patched module must be loaded, 803 * see klp_init_object_loaded(). 804 */ 805 if (!func->new_func && !func->nop) 806 return -EINVAL; 807 808 if (strlen(func->old_name) >= KSYM_NAME_LEN) 809 return -EINVAL; 810 811 INIT_LIST_HEAD(&func->stack_node); 812 func->patched = false; 813 func->transition = false; 814 815 /* The format for the sysfs directory is <function,sympos> where sympos 816 * is the nth occurrence of this symbol in kallsyms for the patched 817 * object. If the user selects 0 for old_sympos, then 1 will be used 818 * since a unique symbol will be the first occurrence. 819 */ 820 return kobject_add(&func->kobj, &obj->kobj, "%s,%lu", 821 func->old_name, 822 func->old_sympos ? func->old_sympos : 1); 823 } 824 825 static int klp_write_object_relocs(struct klp_patch *patch, 826 struct klp_object *obj, 827 bool apply) 828 { 829 int i, ret; 830 struct klp_modinfo *info = patch->mod->klp_info; 831 832 for (i = 1; i < info->hdr.e_shnum; i++) { 833 Elf_Shdr *sec = info->sechdrs + i; 834 835 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) 836 continue; 837 838 ret = klp_write_section_relocs(patch->mod, info->sechdrs, 839 info->secstrings, 840 patch->mod->core_kallsyms.strtab, 841 info->symndx, i, obj->name, apply); 842 if (ret) 843 return ret; 844 } 845 846 return 0; 847 } 848 849 static int klp_apply_object_relocs(struct klp_patch *patch, 850 struct klp_object *obj) 851 { 852 return klp_write_object_relocs(patch, obj, true); 853 } 854 855 static void klp_clear_object_relocs(struct klp_patch *patch, 856 struct klp_object *obj) 857 { 858 klp_write_object_relocs(patch, obj, false); 859 } 860 861 /* parts of the initialization that is done only when the object is loaded */ 862 static int klp_init_object_loaded(struct klp_patch *patch, 863 struct klp_object *obj) 864 { 865 struct klp_func *func; 866 int ret; 867 868 if (klp_is_module(obj)) { 869 /* 870 * Only write module-specific relocations here 871 * (.klp.rela.{module}.*). vmlinux-specific relocations were 872 * written earlier during the initialization of the klp module 873 * itself. 874 */ 875 ret = klp_apply_object_relocs(patch, obj); 876 if (ret) 877 return ret; 878 } 879 880 klp_for_each_func(obj, func) { 881 ret = klp_find_object_symbol(obj->name, func->old_name, 882 func->old_sympos, 883 (unsigned long *)&func->old_func); 884 if (ret) 885 return ret; 886 887 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func, 888 &func->old_size, NULL); 889 if (!ret) { 890 pr_err("kallsyms size lookup failed for '%s'\n", 891 func->old_name); 892 return -ENOENT; 893 } 894 895 if (func->nop) 896 func->new_func = func->old_func; 897 898 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func, 899 &func->new_size, NULL); 900 if (!ret) { 901 pr_err("kallsyms size lookup failed for '%s' replacement\n", 902 func->old_name); 903 return -ENOENT; 904 } 905 } 906 907 return 0; 908 } 909 910 static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) 911 { 912 struct klp_func *func; 913 int ret; 914 const char *name; 915 916 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN) 917 return -EINVAL; 918 919 obj->patched = false; 920 obj->mod = NULL; 921 922 klp_find_object_module(obj); 923 924 name = klp_is_module(obj) ? obj->name : "vmlinux"; 925 ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name); 926 if (ret) 927 return ret; 928 929 klp_for_each_func(obj, func) { 930 ret = klp_init_func(obj, func); 931 if (ret) 932 return ret; 933 } 934 935 if (klp_is_object_loaded(obj)) 936 ret = klp_init_object_loaded(patch, obj); 937 938 return ret; 939 } 940 941 static void klp_init_func_early(struct klp_object *obj, 942 struct klp_func *func) 943 { 944 kobject_init(&func->kobj, &klp_ktype_func); 945 list_add_tail(&func->node, &obj->func_list); 946 } 947 948 static void klp_init_object_early(struct klp_patch *patch, 949 struct klp_object *obj) 950 { 951 INIT_LIST_HEAD(&obj->func_list); 952 kobject_init(&obj->kobj, &klp_ktype_object); 953 list_add_tail(&obj->node, &patch->obj_list); 954 } 955 956 static void klp_init_patch_early(struct klp_patch *patch) 957 { 958 struct klp_object *obj; 959 struct klp_func *func; 960 961 INIT_LIST_HEAD(&patch->list); 962 INIT_LIST_HEAD(&patch->obj_list); 963 kobject_init(&patch->kobj, &klp_ktype_patch); 964 patch->enabled = false; 965 patch->forced = false; 966 INIT_WORK(&patch->free_work, klp_free_patch_work_fn); 967 init_completion(&patch->finish); 968 969 klp_for_each_object_static(patch, obj) { 970 klp_init_object_early(patch, obj); 971 972 klp_for_each_func_static(obj, func) { 973 klp_init_func_early(obj, func); 974 } 975 } 976 } 977 978 static int klp_init_patch(struct klp_patch *patch) 979 { 980 struct klp_object *obj; 981 int ret; 982 983 ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name); 984 if (ret) 985 return ret; 986 987 if (patch->replace) { 988 ret = klp_add_nops(patch); 989 if (ret) 990 return ret; 991 } 992 993 klp_for_each_object(patch, obj) { 994 ret = klp_init_object(patch, obj); 995 if (ret) 996 return ret; 997 } 998 999 list_add_tail(&patch->list, &klp_patches); 1000 1001 return 0; 1002 } 1003 1004 static int __klp_disable_patch(struct klp_patch *patch) 1005 { 1006 struct klp_object *obj; 1007 1008 if (WARN_ON(!patch->enabled)) 1009 return -EINVAL; 1010 1011 if (klp_transition_patch) 1012 return -EBUSY; 1013 1014 klp_init_transition(patch, KLP_TRANSITION_UNPATCHED); 1015 1016 klp_for_each_object(patch, obj) 1017 if (obj->patched) 1018 klp_pre_unpatch_callback(obj); 1019 1020 /* 1021 * Enforce the order of the func->transition writes in 1022 * klp_init_transition() and the TIF_PATCH_PENDING writes in 1023 * klp_start_transition(). In the rare case where klp_ftrace_handler() 1024 * is called shortly after klp_update_patch_state() switches the task, 1025 * this ensures the handler sees that func->transition is set. 1026 */ 1027 smp_wmb(); 1028 1029 klp_start_transition(); 1030 patch->enabled = false; 1031 klp_try_complete_transition(); 1032 1033 return 0; 1034 } 1035 1036 static int __klp_enable_patch(struct klp_patch *patch) 1037 { 1038 struct klp_object *obj; 1039 int ret; 1040 1041 if (klp_transition_patch) 1042 return -EBUSY; 1043 1044 if (WARN_ON(patch->enabled)) 1045 return -EINVAL; 1046 1047 pr_notice("enabling patch '%s'\n", patch->mod->name); 1048 1049 klp_init_transition(patch, KLP_TRANSITION_PATCHED); 1050 1051 /* 1052 * Enforce the order of the func->transition writes in 1053 * klp_init_transition() and the ops->func_stack writes in 1054 * klp_patch_object(), so that klp_ftrace_handler() will see the 1055 * func->transition updates before the handler is registered and the 1056 * new funcs become visible to the handler. 1057 */ 1058 smp_wmb(); 1059 1060 klp_for_each_object(patch, obj) { 1061 if (!klp_is_object_loaded(obj)) 1062 continue; 1063 1064 ret = klp_pre_patch_callback(obj); 1065 if (ret) { 1066 pr_warn("pre-patch callback failed for object '%s'\n", 1067 klp_is_module(obj) ? obj->name : "vmlinux"); 1068 goto err; 1069 } 1070 1071 ret = klp_patch_object(obj); 1072 if (ret) { 1073 pr_warn("failed to patch object '%s'\n", 1074 klp_is_module(obj) ? obj->name : "vmlinux"); 1075 goto err; 1076 } 1077 } 1078 1079 klp_start_transition(); 1080 patch->enabled = true; 1081 klp_try_complete_transition(); 1082 1083 return 0; 1084 err: 1085 pr_warn("failed to enable patch '%s'\n", patch->mod->name); 1086 1087 klp_cancel_transition(); 1088 return ret; 1089 } 1090 1091 /** 1092 * klp_enable_patch() - enable the livepatch 1093 * @patch: patch to be enabled 1094 * 1095 * Initializes the data structure associated with the patch, creates the sysfs 1096 * interface, performs the needed symbol lookups and code relocations, 1097 * registers the patched functions with ftrace. 1098 * 1099 * This function is supposed to be called from the livepatch module_init() 1100 * callback. 1101 * 1102 * Return: 0 on success, otherwise error 1103 */ 1104 int klp_enable_patch(struct klp_patch *patch) 1105 { 1106 int ret; 1107 struct klp_object *obj; 1108 1109 if (!patch || !patch->mod || !patch->objs) 1110 return -EINVAL; 1111 1112 klp_for_each_object_static(patch, obj) { 1113 if (!obj->funcs) 1114 return -EINVAL; 1115 } 1116 1117 1118 if (!is_livepatch_module(patch->mod)) { 1119 pr_err("module %s is not marked as a livepatch module\n", 1120 patch->mod->name); 1121 return -EINVAL; 1122 } 1123 1124 if (!klp_initialized()) 1125 return -ENODEV; 1126 1127 if (!klp_have_reliable_stack()) { 1128 pr_warn("This architecture doesn't have support for the livepatch consistency model.\n"); 1129 pr_warn("The livepatch transition may never complete.\n"); 1130 } 1131 1132 mutex_lock(&klp_mutex); 1133 1134 if (!klp_is_patch_compatible(patch)) { 1135 pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n", 1136 patch->mod->name); 1137 mutex_unlock(&klp_mutex); 1138 return -EINVAL; 1139 } 1140 1141 if (!try_module_get(patch->mod)) { 1142 mutex_unlock(&klp_mutex); 1143 return -ENODEV; 1144 } 1145 1146 klp_init_patch_early(patch); 1147 1148 ret = klp_init_patch(patch); 1149 if (ret) 1150 goto err; 1151 1152 ret = __klp_enable_patch(patch); 1153 if (ret) 1154 goto err; 1155 1156 mutex_unlock(&klp_mutex); 1157 1158 return 0; 1159 1160 err: 1161 klp_free_patch_start(patch); 1162 1163 mutex_unlock(&klp_mutex); 1164 1165 klp_free_patch_finish(patch); 1166 1167 return ret; 1168 } 1169 EXPORT_SYMBOL_GPL(klp_enable_patch); 1170 1171 /* 1172 * This function unpatches objects from the replaced livepatches. 1173 * 1174 * We could be pretty aggressive here. It is called in the situation where 1175 * these structures are no longer accessed from the ftrace handler. 1176 * All functions are redirected by the klp_transition_patch. They 1177 * use either a new code or they are in the original code because 1178 * of the special nop function patches. 1179 * 1180 * The only exception is when the transition was forced. In this case, 1181 * klp_ftrace_handler() might still see the replaced patch on the stack. 1182 * Fortunately, it is carefully designed to work with removed functions 1183 * thanks to RCU. We only have to keep the patches on the system. Also 1184 * this is handled transparently by patch->module_put. 1185 */ 1186 void klp_unpatch_replaced_patches(struct klp_patch *new_patch) 1187 { 1188 struct klp_patch *old_patch; 1189 1190 klp_for_each_patch(old_patch) { 1191 if (old_patch == new_patch) 1192 return; 1193 1194 old_patch->enabled = false; 1195 klp_unpatch_objects(old_patch); 1196 } 1197 } 1198 1199 /* 1200 * This function removes the dynamically allocated 'nop' functions. 1201 * 1202 * We could be pretty aggressive. NOPs do not change the existing 1203 * behavior except for adding unnecessary delay by the ftrace handler. 1204 * 1205 * It is safe even when the transition was forced. The ftrace handler 1206 * will see a valid ops->func_stack entry thanks to RCU. 1207 * 1208 * We could even free the NOPs structures. They must be the last entry 1209 * in ops->func_stack. Therefore unregister_ftrace_function() is called. 1210 * It does the same as klp_synchronize_transition() to make sure that 1211 * nobody is inside the ftrace handler once the operation finishes. 1212 * 1213 * IMPORTANT: It must be called right after removing the replaced patches! 1214 */ 1215 void klp_discard_nops(struct klp_patch *new_patch) 1216 { 1217 klp_unpatch_objects_dynamic(klp_transition_patch); 1218 klp_free_objects_dynamic(klp_transition_patch); 1219 } 1220 1221 /* 1222 * Remove parts of patches that touch a given kernel module. The list of 1223 * patches processed might be limited. When limit is NULL, all patches 1224 * will be handled. 1225 */ 1226 static void klp_cleanup_module_patches_limited(struct module *mod, 1227 struct klp_patch *limit) 1228 { 1229 struct klp_patch *patch; 1230 struct klp_object *obj; 1231 1232 klp_for_each_patch(patch) { 1233 if (patch == limit) 1234 break; 1235 1236 klp_for_each_object(patch, obj) { 1237 if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 1238 continue; 1239 1240 if (patch != klp_transition_patch) 1241 klp_pre_unpatch_callback(obj); 1242 1243 pr_notice("reverting patch '%s' on unloading module '%s'\n", 1244 patch->mod->name, obj->mod->name); 1245 klp_unpatch_object(obj); 1246 1247 klp_post_unpatch_callback(obj); 1248 klp_clear_object_relocs(patch, obj); 1249 klp_free_object_loaded(obj); 1250 break; 1251 } 1252 } 1253 } 1254 1255 int klp_module_coming(struct module *mod) 1256 { 1257 int ret; 1258 struct klp_patch *patch; 1259 struct klp_object *obj; 1260 1261 if (WARN_ON(mod->state != MODULE_STATE_COMING)) 1262 return -EINVAL; 1263 1264 if (!strcmp(mod->name, "vmlinux")) { 1265 pr_err("vmlinux.ko: invalid module name\n"); 1266 return -EINVAL; 1267 } 1268 1269 mutex_lock(&klp_mutex); 1270 /* 1271 * Each module has to know that klp_module_coming() 1272 * has been called. We never know what module will 1273 * get patched by a new patch. 1274 */ 1275 mod->klp_alive = true; 1276 1277 klp_for_each_patch(patch) { 1278 klp_for_each_object(patch, obj) { 1279 if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 1280 continue; 1281 1282 obj->mod = mod; 1283 1284 ret = klp_init_object_loaded(patch, obj); 1285 if (ret) { 1286 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", 1287 patch->mod->name, obj->mod->name, ret); 1288 goto err; 1289 } 1290 1291 pr_notice("applying patch '%s' to loading module '%s'\n", 1292 patch->mod->name, obj->mod->name); 1293 1294 ret = klp_pre_patch_callback(obj); 1295 if (ret) { 1296 pr_warn("pre-patch callback failed for object '%s'\n", 1297 obj->name); 1298 goto err; 1299 } 1300 1301 ret = klp_patch_object(obj); 1302 if (ret) { 1303 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", 1304 patch->mod->name, obj->mod->name, ret); 1305 1306 klp_post_unpatch_callback(obj); 1307 goto err; 1308 } 1309 1310 if (patch != klp_transition_patch) 1311 klp_post_patch_callback(obj); 1312 1313 break; 1314 } 1315 } 1316 1317 mutex_unlock(&klp_mutex); 1318 1319 return 0; 1320 1321 err: 1322 /* 1323 * If a patch is unsuccessfully applied, return 1324 * error to the module loader. 1325 */ 1326 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n", 1327 patch->mod->name, obj->mod->name, obj->mod->name); 1328 mod->klp_alive = false; 1329 obj->mod = NULL; 1330 klp_cleanup_module_patches_limited(mod, patch); 1331 mutex_unlock(&klp_mutex); 1332 1333 return ret; 1334 } 1335 1336 void klp_module_going(struct module *mod) 1337 { 1338 if (WARN_ON(mod->state != MODULE_STATE_GOING && 1339 mod->state != MODULE_STATE_COMING)) 1340 return; 1341 1342 mutex_lock(&klp_mutex); 1343 /* 1344 * Each module has to know that klp_module_going() 1345 * has been called. We never know what module will 1346 * get patched by a new patch. 1347 */ 1348 mod->klp_alive = false; 1349 1350 klp_cleanup_module_patches_limited(mod, NULL); 1351 1352 mutex_unlock(&klp_mutex); 1353 } 1354 1355 static int __init klp_init(void) 1356 { 1357 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj); 1358 if (!klp_root_kobj) 1359 return -ENOMEM; 1360 1361 return 0; 1362 } 1363 1364 module_init(klp_init); 1365