11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2b700e7f0SSeth Jennings /* 3b700e7f0SSeth Jennings * core.c - Kernel Live Patching Core 4b700e7f0SSeth Jennings * 5b700e7f0SSeth Jennings * Copyright (C) 2014 Seth Jennings <[email protected]> 6b700e7f0SSeth Jennings * Copyright (C) 2014 SUSE 7b700e7f0SSeth Jennings */ 8b700e7f0SSeth Jennings 9b700e7f0SSeth Jennings #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10b700e7f0SSeth Jennings 11b700e7f0SSeth Jennings #include <linux/module.h> 12b700e7f0SSeth Jennings #include <linux/kernel.h> 13b700e7f0SSeth Jennings #include <linux/mutex.h> 14b700e7f0SSeth Jennings #include <linux/slab.h> 15b700e7f0SSeth Jennings #include <linux/list.h> 16b700e7f0SSeth Jennings #include <linux/kallsyms.h> 17b700e7f0SSeth Jennings #include <linux/livepatch.h> 18425595a7SJessica Yu #include <linux/elf.h> 19425595a7SJessica Yu #include <linux/moduleloader.h> 203ec24776SJosh Poimboeuf #include <linux/completion.h> 219f255b63SJosh Poimboeuf #include <linux/memory.h> 22a0060505SChristoph Hellwig #include <linux/rcupdate.h> 23b56b36eeSJosh Poimboeuf #include <asm/cacheflush.h> 2410517429SJiri Kosina #include "core.h" 25c349cdcaSJosh Poimboeuf #include "patch.h" 2692c9abf5SPetr Mladek #include "state.h" 27d83a7cb3SJosh Poimboeuf #include "transition.h" 28b700e7f0SSeth Jennings 293c33f5b9SJosh Poimboeuf /* 30d83a7cb3SJosh Poimboeuf * klp_mutex is a coarse lock which serializes access to klp data. All 31d83a7cb3SJosh Poimboeuf * accesses to klp-related variables and structures must have mutex protection, 32d83a7cb3SJosh Poimboeuf * except within the following functions which carefully avoid the need for it: 33d83a7cb3SJosh Poimboeuf * 34d83a7cb3SJosh Poimboeuf * - klp_ftrace_handler() 35d83a7cb3SJosh Poimboeuf * - klp_update_patch_state() 363c33f5b9SJosh Poimboeuf */ 37d83a7cb3SJosh Poimboeuf DEFINE_MUTEX(klp_mutex); 383c33f5b9SJosh Poimboeuf 39958ef1e3SPetr Mladek /* 40958ef1e3SPetr Mladek * Actively used patches: enabled or in transition. Note that replaced 41958ef1e3SPetr Mladek * or disabled patches are not listed even though the related kernel 42958ef1e3SPetr Mladek * module still can be loaded. 43958ef1e3SPetr Mladek */ 4468007289SPetr Mladek LIST_HEAD(klp_patches); 45b700e7f0SSeth Jennings 46b700e7f0SSeth Jennings static struct kobject *klp_root_kobj; 47b700e7f0SSeth Jennings 48b700e7f0SSeth Jennings static bool klp_is_module(struct klp_object *obj) 49b700e7f0SSeth Jennings { 50b700e7f0SSeth Jennings return obj->name; 51b700e7f0SSeth Jennings } 52b700e7f0SSeth Jennings 53b700e7f0SSeth Jennings /* sets obj->mod if object is not vmlinux and module is found */ 54b700e7f0SSeth Jennings static void klp_find_object_module(struct klp_object *obj) 55b700e7f0SSeth Jennings { 568cb2c2dcSPetr Mladek struct module *mod; 578cb2c2dcSPetr Mladek 58b700e7f0SSeth Jennings if (!klp_is_module(obj)) 59b700e7f0SSeth Jennings return; 60b700e7f0SSeth Jennings 61a0060505SChristoph Hellwig rcu_read_lock_sched(); 62b700e7f0SSeth Jennings /* 638cb2c2dcSPetr Mladek * We do not want to block removal of patched modules and therefore 648cb2c2dcSPetr Mladek * we do not take a reference here. The patches are removed by 657e545d6eSJessica Yu * klp_module_going() instead. 66b700e7f0SSeth Jennings */ 678cb2c2dcSPetr Mladek mod = find_module(obj->name); 688cb2c2dcSPetr Mladek /* 697e545d6eSJessica Yu * Do not mess work of klp_module_coming() and klp_module_going(). 707e545d6eSJessica Yu * Note that the patch might still be needed before klp_module_going() 718cb2c2dcSPetr Mladek * is called. Module functions can be called even in the GOING state 728cb2c2dcSPetr Mladek * until mod->exit() finishes. This is especially important for 738cb2c2dcSPetr Mladek * patches that modify semantic of the functions. 748cb2c2dcSPetr Mladek */ 758cb2c2dcSPetr Mladek if (mod && mod->klp_alive) 768cb2c2dcSPetr Mladek obj->mod = mod; 778cb2c2dcSPetr Mladek 78a0060505SChristoph Hellwig rcu_read_unlock_sched(); 79b700e7f0SSeth Jennings } 80b700e7f0SSeth Jennings 81b700e7f0SSeth Jennings static bool klp_initialized(void) 82b700e7f0SSeth Jennings { 83e76ff06aSNicholas Mc Guire return !!klp_root_kobj; 84b700e7f0SSeth Jennings } 85b700e7f0SSeth Jennings 86e1452b60SJason Baron static struct klp_func *klp_find_func(struct klp_object *obj, 87e1452b60SJason Baron struct klp_func *old_func) 88e1452b60SJason Baron { 89e1452b60SJason Baron struct klp_func *func; 90e1452b60SJason Baron 91e1452b60SJason Baron klp_for_each_func(obj, func) { 92e1452b60SJason Baron if ((strcmp(old_func->old_name, func->old_name) == 0) && 93e1452b60SJason Baron (old_func->old_sympos == func->old_sympos)) { 94e1452b60SJason Baron return func; 95e1452b60SJason Baron } 96e1452b60SJason Baron } 97e1452b60SJason Baron 98e1452b60SJason Baron return NULL; 99e1452b60SJason Baron } 100e1452b60SJason Baron 101e1452b60SJason Baron static struct klp_object *klp_find_object(struct klp_patch *patch, 102e1452b60SJason Baron struct klp_object *old_obj) 103e1452b60SJason Baron { 104e1452b60SJason Baron struct klp_object *obj; 105e1452b60SJason Baron 106e1452b60SJason Baron klp_for_each_object(patch, obj) { 107e1452b60SJason Baron if (klp_is_module(old_obj)) { 108e1452b60SJason Baron if (klp_is_module(obj) && 109e1452b60SJason Baron strcmp(old_obj->name, obj->name) == 0) { 110e1452b60SJason Baron return obj; 111e1452b60SJason Baron } 112e1452b60SJason Baron } else if (!klp_is_module(obj)) { 113e1452b60SJason Baron return obj; 114e1452b60SJason Baron } 115e1452b60SJason Baron } 116e1452b60SJason Baron 117e1452b60SJason Baron return NULL; 118e1452b60SJason Baron } 119e1452b60SJason Baron 120b700e7f0SSeth Jennings struct klp_find_arg { 121b700e7f0SSeth Jennings const char *objname; 122b700e7f0SSeth Jennings const char *name; 123b700e7f0SSeth Jennings unsigned long addr; 124b700e7f0SSeth Jennings unsigned long count; 125b2b018efSChris J Arges unsigned long pos; 126b700e7f0SSeth Jennings }; 127b700e7f0SSeth Jennings 128b700e7f0SSeth Jennings static int klp_find_callback(void *data, const char *name, 129b700e7f0SSeth Jennings struct module *mod, unsigned long addr) 130b700e7f0SSeth Jennings { 131b700e7f0SSeth Jennings struct klp_find_arg *args = data; 132b700e7f0SSeth Jennings 133b700e7f0SSeth Jennings if ((mod && !args->objname) || (!mod && args->objname)) 134b700e7f0SSeth Jennings return 0; 135b700e7f0SSeth Jennings 136b700e7f0SSeth Jennings if (strcmp(args->name, name)) 137b700e7f0SSeth Jennings return 0; 138b700e7f0SSeth Jennings 139b700e7f0SSeth Jennings if (args->objname && strcmp(args->objname, mod->name)) 140b700e7f0SSeth Jennings return 0; 141b700e7f0SSeth Jennings 142b700e7f0SSeth Jennings args->addr = addr; 143b700e7f0SSeth Jennings args->count++; 144b700e7f0SSeth Jennings 145b2b018efSChris J Arges /* 146b2b018efSChris J Arges * Finish the search when the symbol is found for the desired position 147b2b018efSChris J Arges * or the position is not defined for a non-unique symbol. 148b2b018efSChris J Arges */ 149b2b018efSChris J Arges if ((args->pos && (args->count == args->pos)) || 150b2b018efSChris J Arges (!args->pos && (args->count > 1))) 151b2b018efSChris J Arges return 1; 152b2b018efSChris J Arges 153b700e7f0SSeth Jennings return 0; 154b700e7f0SSeth Jennings } 155b700e7f0SSeth Jennings 156b700e7f0SSeth Jennings static int klp_find_object_symbol(const char *objname, const char *name, 157b2b018efSChris J Arges unsigned long sympos, unsigned long *addr) 158b700e7f0SSeth Jennings { 159b700e7f0SSeth Jennings struct klp_find_arg args = { 160b700e7f0SSeth Jennings .objname = objname, 161b700e7f0SSeth Jennings .name = name, 162b700e7f0SSeth Jennings .addr = 0, 163b2b018efSChris J Arges .count = 0, 164b2b018efSChris J Arges .pos = sympos, 165b700e7f0SSeth Jennings }; 166b700e7f0SSeth Jennings 16772f04b50SZhou Chengming if (objname) 16872f04b50SZhou Chengming module_kallsyms_on_each_symbol(klp_find_callback, &args); 16972f04b50SZhou Chengming else 170b700e7f0SSeth Jennings kallsyms_on_each_symbol(klp_find_callback, &args); 171b700e7f0SSeth Jennings 172b2b018efSChris J Arges /* 173b2b018efSChris J Arges * Ensure an address was found. If sympos is 0, ensure symbol is unique; 174b2b018efSChris J Arges * otherwise ensure the symbol position count matches sympos. 175b2b018efSChris J Arges */ 176b2b018efSChris J Arges if (args.addr == 0) 177b700e7f0SSeth Jennings pr_err("symbol '%s' not found in symbol table\n", name); 178b2b018efSChris J Arges else if (args.count > 1 && sympos == 0) { 179f995b5f7SPetr Mladek pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n", 180f995b5f7SPetr Mladek name, objname); 181b2b018efSChris J Arges } else if (sympos != args.count && sympos > 0) { 182b2b018efSChris J Arges pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n", 183b2b018efSChris J Arges sympos, name, objname ? objname : "vmlinux"); 184b2b018efSChris J Arges } else { 185b700e7f0SSeth Jennings *addr = args.addr; 186b700e7f0SSeth Jennings return 0; 187b700e7f0SSeth Jennings } 188b700e7f0SSeth Jennings 189b700e7f0SSeth Jennings *addr = 0; 190b700e7f0SSeth Jennings return -EINVAL; 191b700e7f0SSeth Jennings } 192b700e7f0SSeth Jennings 1932f293651SChristophe Leroy static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab, 194ca376a93SJosh Poimboeuf unsigned int symndx, Elf_Shdr *relasec, 195ca376a93SJosh Poimboeuf const char *sec_objname) 196b700e7f0SSeth Jennings { 197ca376a93SJosh Poimboeuf int i, cnt, ret; 198ca376a93SJosh Poimboeuf char sym_objname[MODULE_NAME_LEN]; 199ca376a93SJosh Poimboeuf char sym_name[KSYM_NAME_LEN]; 200425595a7SJessica Yu Elf_Rela *relas; 201425595a7SJessica Yu Elf_Sym *sym; 202425595a7SJessica Yu unsigned long sympos, addr; 203ca376a93SJosh Poimboeuf bool sym_vmlinux; 204ca376a93SJosh Poimboeuf bool sec_vmlinux = !strcmp(sec_objname, "vmlinux"); 205b700e7f0SSeth Jennings 206b2b018efSChris J Arges /* 207ca376a93SJosh Poimboeuf * Since the field widths for sym_objname and sym_name in the sscanf() 208425595a7SJessica Yu * call are hard-coded and correspond to MODULE_NAME_LEN and 209425595a7SJessica Yu * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN 210425595a7SJessica Yu * and KSYM_NAME_LEN have the values we expect them to have. 211425595a7SJessica Yu * 212425595a7SJessica Yu * Because the value of MODULE_NAME_LEN can differ among architectures, 213425595a7SJessica Yu * we use the smallest/strictest upper bound possible (56, based on 214425595a7SJessica Yu * the current definition of MODULE_NAME_LEN) to prevent overflows. 215b2b018efSChris J Arges */ 216*b8a94bfbSMiguel Ojeda BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 512); 217425595a7SJessica Yu 218425595a7SJessica Yu relas = (Elf_Rela *) relasec->sh_addr; 219425595a7SJessica Yu /* For each rela in this klp relocation section */ 220425595a7SJessica Yu for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) { 2212f293651SChristophe Leroy sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info); 222425595a7SJessica Yu if (sym->st_shndx != SHN_LIVEPATCH) { 22377f8f39aSJosh Poimboeuf pr_err("symbol %s is not marked as a livepatch symbol\n", 224425595a7SJessica Yu strtab + sym->st_name); 225425595a7SJessica Yu return -EINVAL; 226425595a7SJessica Yu } 227425595a7SJessica Yu 228ca376a93SJosh Poimboeuf /* Format: .klp.sym.sym_objname.sym_name,sympos */ 229425595a7SJessica Yu cnt = sscanf(strtab + sym->st_name, 230*b8a94bfbSMiguel Ojeda ".klp.sym.%55[^.].%511[^,],%lu", 231ca376a93SJosh Poimboeuf sym_objname, sym_name, &sympos); 232425595a7SJessica Yu if (cnt != 3) { 23377f8f39aSJosh Poimboeuf pr_err("symbol %s has an incorrectly formatted name\n", 234425595a7SJessica Yu strtab + sym->st_name); 235425595a7SJessica Yu return -EINVAL; 236425595a7SJessica Yu } 237425595a7SJessica Yu 238ca376a93SJosh Poimboeuf sym_vmlinux = !strcmp(sym_objname, "vmlinux"); 239ca376a93SJosh Poimboeuf 240ca376a93SJosh Poimboeuf /* 241ca376a93SJosh Poimboeuf * Prevent module-specific KLP rela sections from referencing 242ca376a93SJosh Poimboeuf * vmlinux symbols. This helps prevent ordering issues with 243ca376a93SJosh Poimboeuf * module special section initializations. Presumably such 244ca376a93SJosh Poimboeuf * symbols are exported and normal relas can be used instead. 245ca376a93SJosh Poimboeuf */ 246ca376a93SJosh Poimboeuf if (!sec_vmlinux && sym_vmlinux) { 247ca376a93SJosh Poimboeuf pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section", 248ca376a93SJosh Poimboeuf sym_name); 249ca376a93SJosh Poimboeuf return -EINVAL; 250ca376a93SJosh Poimboeuf } 251ca376a93SJosh Poimboeuf 252425595a7SJessica Yu /* klp_find_object_symbol() treats a NULL objname as vmlinux */ 253ca376a93SJosh Poimboeuf ret = klp_find_object_symbol(sym_vmlinux ? NULL : sym_objname, 254ca376a93SJosh Poimboeuf sym_name, sympos, &addr); 255425595a7SJessica Yu if (ret) 256425595a7SJessica Yu return ret; 257425595a7SJessica Yu 258425595a7SJessica Yu sym->st_value = addr; 259425595a7SJessica Yu } 260425595a7SJessica Yu 261425595a7SJessica Yu return 0; 262b700e7f0SSeth Jennings } 263b700e7f0SSeth Jennings 2647c8e2bddSJosh Poimboeuf /* 2657c8e2bddSJosh Poimboeuf * At a high-level, there are two types of klp relocation sections: those which 2667c8e2bddSJosh Poimboeuf * reference symbols which live in vmlinux; and those which reference symbols 2677c8e2bddSJosh Poimboeuf * which live in other modules. This function is called for both types: 2687c8e2bddSJosh Poimboeuf * 2697c8e2bddSJosh Poimboeuf * 1) When a klp module itself loads, the module code calls this function to 2707c8e2bddSJosh Poimboeuf * write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections). 2717c8e2bddSJosh Poimboeuf * These relocations are written to the klp module text to allow the patched 2727c8e2bddSJosh Poimboeuf * code/data to reference unexported vmlinux symbols. They're written as 2737c8e2bddSJosh Poimboeuf * early as possible to ensure that other module init code (.e.g., 2747c8e2bddSJosh Poimboeuf * jump_label_apply_nops) can access any unexported vmlinux symbols which 2757c8e2bddSJosh Poimboeuf * might be referenced by the klp module's special sections. 2767c8e2bddSJosh Poimboeuf * 2777c8e2bddSJosh Poimboeuf * 2) When a to-be-patched module loads -- or is already loaded when a 2787c8e2bddSJosh Poimboeuf * corresponding klp module loads -- klp code calls this function to write 2797c8e2bddSJosh Poimboeuf * module-specific klp relocations (.klp.rela.{module}.* sections). These 2807c8e2bddSJosh Poimboeuf * are written to the klp module text to allow the patched code/data to 2817c8e2bddSJosh Poimboeuf * reference symbols which live in the to-be-patched module or one of its 2827c8e2bddSJosh Poimboeuf * module dependencies. Exported symbols are supported, in addition to 2837c8e2bddSJosh Poimboeuf * unexported symbols, in order to enable late module patching, which allows 2847c8e2bddSJosh Poimboeuf * the to-be-patched module to be loaded and patched sometime *after* the 2857c8e2bddSJosh Poimboeuf * klp module is loaded. 2867c8e2bddSJosh Poimboeuf */ 2877c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, 2887c8e2bddSJosh Poimboeuf const char *shstrtab, const char *strtab, 2897c8e2bddSJosh Poimboeuf unsigned int symndx, unsigned int secndx, 2907c8e2bddSJosh Poimboeuf const char *objname) 291b700e7f0SSeth Jennings { 2927c8e2bddSJosh Poimboeuf int cnt, ret; 293425595a7SJessica Yu char sec_objname[MODULE_NAME_LEN]; 2947c8e2bddSJosh Poimboeuf Elf_Shdr *sec = sechdrs + secndx; 295b56b36eeSJosh Poimboeuf 296425595a7SJessica Yu /* 297425595a7SJessica Yu * Format: .klp.rela.sec_objname.section_name 298425595a7SJessica Yu * See comment in klp_resolve_symbols() for an explanation 299425595a7SJessica Yu * of the selected field width value. 300425595a7SJessica Yu */ 3017c8e2bddSJosh Poimboeuf cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]", 3027c8e2bddSJosh Poimboeuf sec_objname); 303425595a7SJessica Yu if (cnt != 1) { 30477f8f39aSJosh Poimboeuf pr_err("section %s has an incorrectly formatted name\n", 3057c8e2bddSJosh Poimboeuf shstrtab + sec->sh_name); 3067c8e2bddSJosh Poimboeuf return -EINVAL; 307b700e7f0SSeth Jennings } 308425595a7SJessica Yu 3097c8e2bddSJosh Poimboeuf if (strcmp(objname ? objname : "vmlinux", sec_objname)) 3107c8e2bddSJosh Poimboeuf return 0; 311425595a7SJessica Yu 312ca376a93SJosh Poimboeuf ret = klp_resolve_symbols(sechdrs, strtab, symndx, sec, sec_objname); 313064c89dfSChris J Arges if (ret) 314b56b36eeSJosh Poimboeuf return ret; 3157c8e2bddSJosh Poimboeuf 3167c8e2bddSJosh Poimboeuf return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod); 317b700e7f0SSeth Jennings } 318b700e7f0SSeth Jennings 319b700e7f0SSeth Jennings /* 320b700e7f0SSeth Jennings * Sysfs Interface 321b700e7f0SSeth Jennings * 322b700e7f0SSeth Jennings * /sys/kernel/livepatch 323b700e7f0SSeth Jennings * /sys/kernel/livepatch/<patch> 324b700e7f0SSeth Jennings * /sys/kernel/livepatch/<patch>/enabled 325d83a7cb3SJosh Poimboeuf * /sys/kernel/livepatch/<patch>/transition 326c99a2be7SMiroslav Benes * /sys/kernel/livepatch/<patch>/force 327b700e7f0SSeth Jennings * /sys/kernel/livepatch/<patch>/<object> 328444f9e99SChris J Arges * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> 329b700e7f0SSeth Jennings */ 33026c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch); 331b700e7f0SSeth Jennings 332b700e7f0SSeth Jennings static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, 333b700e7f0SSeth Jennings const char *buf, size_t count) 334b700e7f0SSeth Jennings { 335b700e7f0SSeth Jennings struct klp_patch *patch; 336b700e7f0SSeth Jennings int ret; 33768ae4b2bSJosh Poimboeuf bool enabled; 338b700e7f0SSeth Jennings 33968ae4b2bSJosh Poimboeuf ret = kstrtobool(buf, &enabled); 340b700e7f0SSeth Jennings if (ret) 34168ae4b2bSJosh Poimboeuf return ret; 342b700e7f0SSeth Jennings 343b700e7f0SSeth Jennings patch = container_of(kobj, struct klp_patch, kobj); 344b700e7f0SSeth Jennings 345b700e7f0SSeth Jennings mutex_lock(&klp_mutex); 346b700e7f0SSeth Jennings 34768ae4b2bSJosh Poimboeuf if (patch->enabled == enabled) { 348b700e7f0SSeth Jennings /* already in requested state */ 349b700e7f0SSeth Jennings ret = -EINVAL; 350958ef1e3SPetr Mladek goto out; 351b700e7f0SSeth Jennings } 352b700e7f0SSeth Jennings 353958ef1e3SPetr Mladek /* 354958ef1e3SPetr Mladek * Allow to reverse a pending transition in both ways. It might be 355958ef1e3SPetr Mladek * necessary to complete the transition without forcing and breaking 356958ef1e3SPetr Mladek * the system integrity. 357958ef1e3SPetr Mladek * 358958ef1e3SPetr Mladek * Do not allow to re-enable a disabled patch. 359958ef1e3SPetr Mladek */ 360958ef1e3SPetr Mladek if (patch == klp_transition_patch) 361d83a7cb3SJosh Poimboeuf klp_reverse_transition(); 362958ef1e3SPetr Mladek else if (!enabled) 363b700e7f0SSeth Jennings ret = __klp_disable_patch(patch); 364958ef1e3SPetr Mladek else 365958ef1e3SPetr Mladek ret = -EINVAL; 366958ef1e3SPetr Mladek 367958ef1e3SPetr Mladek out: 368958ef1e3SPetr Mladek mutex_unlock(&klp_mutex); 369958ef1e3SPetr Mladek 370b700e7f0SSeth Jennings if (ret) 371b700e7f0SSeth Jennings return ret; 372958ef1e3SPetr Mladek return count; 373b700e7f0SSeth Jennings } 374b700e7f0SSeth Jennings 375b700e7f0SSeth Jennings static ssize_t enabled_show(struct kobject *kobj, 376b700e7f0SSeth Jennings struct kobj_attribute *attr, char *buf) 377b700e7f0SSeth Jennings { 378b700e7f0SSeth Jennings struct klp_patch *patch; 379b700e7f0SSeth Jennings 380b700e7f0SSeth Jennings patch = container_of(kobj, struct klp_patch, kobj); 3810dade9f3SJosh Poimboeuf return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled); 382b700e7f0SSeth Jennings } 383b700e7f0SSeth Jennings 384d83a7cb3SJosh Poimboeuf static ssize_t transition_show(struct kobject *kobj, 385d83a7cb3SJosh Poimboeuf struct kobj_attribute *attr, char *buf) 386d83a7cb3SJosh Poimboeuf { 387d83a7cb3SJosh Poimboeuf struct klp_patch *patch; 388d83a7cb3SJosh Poimboeuf 389d83a7cb3SJosh Poimboeuf patch = container_of(kobj, struct klp_patch, kobj); 390d83a7cb3SJosh Poimboeuf return snprintf(buf, PAGE_SIZE-1, "%d\n", 391d83a7cb3SJosh Poimboeuf patch == klp_transition_patch); 392b700e7f0SSeth Jennings } 393b700e7f0SSeth Jennings 394c99a2be7SMiroslav Benes static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 395c99a2be7SMiroslav Benes const char *buf, size_t count) 396c99a2be7SMiroslav Benes { 397c99a2be7SMiroslav Benes struct klp_patch *patch; 398c99a2be7SMiroslav Benes int ret; 399c99a2be7SMiroslav Benes bool val; 400c99a2be7SMiroslav Benes 401c99a2be7SMiroslav Benes ret = kstrtobool(buf, &val); 402c99a2be7SMiroslav Benes if (ret) 403c99a2be7SMiroslav Benes return ret; 404c99a2be7SMiroslav Benes 4058869016dSMiroslav Benes if (!val) 4068869016dSMiroslav Benes return count; 4078869016dSMiroslav Benes 4088869016dSMiroslav Benes mutex_lock(&klp_mutex); 4098869016dSMiroslav Benes 4108869016dSMiroslav Benes patch = container_of(kobj, struct klp_patch, kobj); 4118869016dSMiroslav Benes if (patch != klp_transition_patch) { 4128869016dSMiroslav Benes mutex_unlock(&klp_mutex); 4138869016dSMiroslav Benes return -EINVAL; 4148869016dSMiroslav Benes } 4158869016dSMiroslav Benes 416c99a2be7SMiroslav Benes klp_force_transition(); 417c99a2be7SMiroslav Benes 4188869016dSMiroslav Benes mutex_unlock(&klp_mutex); 4198869016dSMiroslav Benes 420c99a2be7SMiroslav Benes return count; 421c99a2be7SMiroslav Benes } 422c99a2be7SMiroslav Benes 423b700e7f0SSeth Jennings static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); 424d83a7cb3SJosh Poimboeuf static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition); 425c99a2be7SMiroslav Benes static struct kobj_attribute force_kobj_attr = __ATTR_WO(force); 426b700e7f0SSeth Jennings static struct attribute *klp_patch_attrs[] = { 427b700e7f0SSeth Jennings &enabled_kobj_attr.attr, 428d83a7cb3SJosh Poimboeuf &transition_kobj_attr.attr, 429c99a2be7SMiroslav Benes &force_kobj_attr.attr, 430b700e7f0SSeth Jennings NULL 431b700e7f0SSeth Jennings }; 43270283454SKimberly Brown ATTRIBUTE_GROUPS(klp_patch); 433b700e7f0SSeth Jennings 434e1452b60SJason Baron static void klp_free_object_dynamic(struct klp_object *obj) 435e1452b60SJason Baron { 436e1452b60SJason Baron kfree(obj->name); 437e1452b60SJason Baron kfree(obj); 438e1452b60SJason Baron } 439e1452b60SJason Baron 440f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj, 441f68d67cfSPetr Mladek struct klp_func *func); 442f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch, 443f68d67cfSPetr Mladek struct klp_object *obj); 4444d141ab3SPetr Mladek 445f68d67cfSPetr Mladek static struct klp_object *klp_alloc_object_dynamic(const char *name, 446f68d67cfSPetr Mladek struct klp_patch *patch) 447e1452b60SJason Baron { 448e1452b60SJason Baron struct klp_object *obj; 449e1452b60SJason Baron 450e1452b60SJason Baron obj = kzalloc(sizeof(*obj), GFP_KERNEL); 451e1452b60SJason Baron if (!obj) 452e1452b60SJason Baron return NULL; 453e1452b60SJason Baron 454e1452b60SJason Baron if (name) { 455e1452b60SJason Baron obj->name = kstrdup(name, GFP_KERNEL); 456e1452b60SJason Baron if (!obj->name) { 457e1452b60SJason Baron kfree(obj); 458e1452b60SJason Baron return NULL; 459e1452b60SJason Baron } 460e1452b60SJason Baron } 461e1452b60SJason Baron 462f68d67cfSPetr Mladek klp_init_object_early(patch, obj); 463e1452b60SJason Baron obj->dynamic = true; 464e1452b60SJason Baron 465e1452b60SJason Baron return obj; 466e1452b60SJason Baron } 467e1452b60SJason Baron 468e1452b60SJason Baron static void klp_free_func_nop(struct klp_func *func) 469e1452b60SJason Baron { 470e1452b60SJason Baron kfree(func->old_name); 471e1452b60SJason Baron kfree(func); 472e1452b60SJason Baron } 473e1452b60SJason Baron 474e1452b60SJason Baron static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func, 475e1452b60SJason Baron struct klp_object *obj) 476e1452b60SJason Baron { 477e1452b60SJason Baron struct klp_func *func; 478e1452b60SJason Baron 479e1452b60SJason Baron func = kzalloc(sizeof(*func), GFP_KERNEL); 480e1452b60SJason Baron if (!func) 481e1452b60SJason Baron return NULL; 482e1452b60SJason Baron 483e1452b60SJason Baron if (old_func->old_name) { 484e1452b60SJason Baron func->old_name = kstrdup(old_func->old_name, GFP_KERNEL); 485e1452b60SJason Baron if (!func->old_name) { 486e1452b60SJason Baron kfree(func); 487e1452b60SJason Baron return NULL; 488e1452b60SJason Baron } 489e1452b60SJason Baron } 490e1452b60SJason Baron 491f68d67cfSPetr Mladek klp_init_func_early(obj, func); 492e1452b60SJason Baron /* 493e1452b60SJason Baron * func->new_func is same as func->old_func. These addresses are 494e1452b60SJason Baron * set when the object is loaded, see klp_init_object_loaded(). 495e1452b60SJason Baron */ 496e1452b60SJason Baron func->old_sympos = old_func->old_sympos; 497e1452b60SJason Baron func->nop = true; 498e1452b60SJason Baron 499e1452b60SJason Baron return func; 500e1452b60SJason Baron } 501e1452b60SJason Baron 502e1452b60SJason Baron static int klp_add_object_nops(struct klp_patch *patch, 503e1452b60SJason Baron struct klp_object *old_obj) 504e1452b60SJason Baron { 505e1452b60SJason Baron struct klp_object *obj; 506e1452b60SJason Baron struct klp_func *func, *old_func; 507e1452b60SJason Baron 508e1452b60SJason Baron obj = klp_find_object(patch, old_obj); 509e1452b60SJason Baron 510e1452b60SJason Baron if (!obj) { 511f68d67cfSPetr Mladek obj = klp_alloc_object_dynamic(old_obj->name, patch); 512e1452b60SJason Baron if (!obj) 513e1452b60SJason Baron return -ENOMEM; 514e1452b60SJason Baron } 515e1452b60SJason Baron 516e1452b60SJason Baron klp_for_each_func(old_obj, old_func) { 517e1452b60SJason Baron func = klp_find_func(obj, old_func); 518e1452b60SJason Baron if (func) 519e1452b60SJason Baron continue; 520e1452b60SJason Baron 521e1452b60SJason Baron func = klp_alloc_func_nop(old_func, obj); 522e1452b60SJason Baron if (!func) 523e1452b60SJason Baron return -ENOMEM; 524e1452b60SJason Baron } 525e1452b60SJason Baron 526e1452b60SJason Baron return 0; 527e1452b60SJason Baron } 528e1452b60SJason Baron 529e1452b60SJason Baron /* 530e1452b60SJason Baron * Add 'nop' functions which simply return to the caller to run 531e1452b60SJason Baron * the original function. The 'nop' functions are added to a 532e1452b60SJason Baron * patch to facilitate a 'replace' mode. 533e1452b60SJason Baron */ 534e1452b60SJason Baron static int klp_add_nops(struct klp_patch *patch) 535e1452b60SJason Baron { 536e1452b60SJason Baron struct klp_patch *old_patch; 537e1452b60SJason Baron struct klp_object *old_obj; 538e1452b60SJason Baron 539ecba29f4SPetr Mladek klp_for_each_patch(old_patch) { 540e1452b60SJason Baron klp_for_each_object(old_patch, old_obj) { 541e1452b60SJason Baron int err; 542e1452b60SJason Baron 543e1452b60SJason Baron err = klp_add_object_nops(patch, old_obj); 544e1452b60SJason Baron if (err) 545e1452b60SJason Baron return err; 546e1452b60SJason Baron } 547e1452b60SJason Baron } 548e1452b60SJason Baron 549e1452b60SJason Baron return 0; 550e1452b60SJason Baron } 551e1452b60SJason Baron 552b700e7f0SSeth Jennings static void klp_kobj_release_patch(struct kobject *kobj) 553b700e7f0SSeth Jennings { 5543ec24776SJosh Poimboeuf struct klp_patch *patch; 5553ec24776SJosh Poimboeuf 5563ec24776SJosh Poimboeuf patch = container_of(kobj, struct klp_patch, kobj); 5573ec24776SJosh Poimboeuf complete(&patch->finish); 558b700e7f0SSeth Jennings } 559b700e7f0SSeth Jennings 560b700e7f0SSeth Jennings static struct kobj_type klp_ktype_patch = { 561b700e7f0SSeth Jennings .release = klp_kobj_release_patch, 562b700e7f0SSeth Jennings .sysfs_ops = &kobj_sysfs_ops, 56370283454SKimberly Brown .default_groups = klp_patch_groups, 564b700e7f0SSeth Jennings }; 565b700e7f0SSeth Jennings 566cad706dfSMiroslav Benes static void klp_kobj_release_object(struct kobject *kobj) 567cad706dfSMiroslav Benes { 568e1452b60SJason Baron struct klp_object *obj; 569e1452b60SJason Baron 570e1452b60SJason Baron obj = container_of(kobj, struct klp_object, kobj); 571e1452b60SJason Baron 572e1452b60SJason Baron if (obj->dynamic) 573e1452b60SJason Baron klp_free_object_dynamic(obj); 574cad706dfSMiroslav Benes } 575cad706dfSMiroslav Benes 576cad706dfSMiroslav Benes static struct kobj_type klp_ktype_object = { 577cad706dfSMiroslav Benes .release = klp_kobj_release_object, 578cad706dfSMiroslav Benes .sysfs_ops = &kobj_sysfs_ops, 579cad706dfSMiroslav Benes }; 580cad706dfSMiroslav Benes 581b700e7f0SSeth Jennings static void klp_kobj_release_func(struct kobject *kobj) 582b700e7f0SSeth Jennings { 583e1452b60SJason Baron struct klp_func *func; 584e1452b60SJason Baron 585e1452b60SJason Baron func = container_of(kobj, struct klp_func, kobj); 586e1452b60SJason Baron 587e1452b60SJason Baron if (func->nop) 588e1452b60SJason Baron klp_free_func_nop(func); 589b700e7f0SSeth Jennings } 590b700e7f0SSeth Jennings 591b700e7f0SSeth Jennings static struct kobj_type klp_ktype_func = { 592b700e7f0SSeth Jennings .release = klp_kobj_release_func, 593b700e7f0SSeth Jennings .sysfs_ops = &kobj_sysfs_ops, 594b700e7f0SSeth Jennings }; 595b700e7f0SSeth Jennings 596d697bad5SPetr Mladek static void __klp_free_funcs(struct klp_object *obj, bool nops_only) 597b700e7f0SSeth Jennings { 598e1452b60SJason Baron struct klp_func *func, *tmp_func; 599b700e7f0SSeth Jennings 600e1452b60SJason Baron klp_for_each_func_safe(obj, func, tmp_func) { 601d697bad5SPetr Mladek if (nops_only && !func->nop) 602d697bad5SPetr Mladek continue; 603d697bad5SPetr Mladek 604d697bad5SPetr Mladek list_del(&func->node); 605b700e7f0SSeth Jennings kobject_put(&func->kobj); 606b700e7f0SSeth Jennings } 6070430f78bSPetr Mladek } 608b700e7f0SSeth Jennings 609b700e7f0SSeth Jennings /* Clean up when a patched object is unloaded */ 610b700e7f0SSeth Jennings static void klp_free_object_loaded(struct klp_object *obj) 611b700e7f0SSeth Jennings { 612b700e7f0SSeth Jennings struct klp_func *func; 613b700e7f0SSeth Jennings 614b700e7f0SSeth Jennings obj->mod = NULL; 615b700e7f0SSeth Jennings 616e1452b60SJason Baron klp_for_each_func(obj, func) { 61719514910SPetr Mladek func->old_func = NULL; 618e1452b60SJason Baron 619e1452b60SJason Baron if (func->nop) 620e1452b60SJason Baron func->new_func = NULL; 621e1452b60SJason Baron } 622b700e7f0SSeth Jennings } 623b700e7f0SSeth Jennings 624d697bad5SPetr Mladek static void __klp_free_objects(struct klp_patch *patch, bool nops_only) 625b700e7f0SSeth Jennings { 626e1452b60SJason Baron struct klp_object *obj, *tmp_obj; 627b700e7f0SSeth Jennings 628e1452b60SJason Baron klp_for_each_object_safe(patch, obj, tmp_obj) { 629d697bad5SPetr Mladek __klp_free_funcs(obj, nops_only); 630d697bad5SPetr Mladek 631d697bad5SPetr Mladek if (nops_only && !obj->dynamic) 632d697bad5SPetr Mladek continue; 633d697bad5SPetr Mladek 634d697bad5SPetr Mladek list_del(&obj->node); 635cad706dfSMiroslav Benes kobject_put(&obj->kobj); 636b700e7f0SSeth Jennings } 637b700e7f0SSeth Jennings } 638b700e7f0SSeth Jennings 639d697bad5SPetr Mladek static void klp_free_objects(struct klp_patch *patch) 640d697bad5SPetr Mladek { 641d697bad5SPetr Mladek __klp_free_objects(patch, false); 642d697bad5SPetr Mladek } 643d697bad5SPetr Mladek 644d697bad5SPetr Mladek static void klp_free_objects_dynamic(struct klp_patch *patch) 645d697bad5SPetr Mladek { 646d697bad5SPetr Mladek __klp_free_objects(patch, true); 647d697bad5SPetr Mladek } 648d697bad5SPetr Mladek 6490430f78bSPetr Mladek /* 6500430f78bSPetr Mladek * This function implements the free operations that can be called safely 6510430f78bSPetr Mladek * under klp_mutex. 6520430f78bSPetr Mladek * 6530430f78bSPetr Mladek * The operation must be completed by calling klp_free_patch_finish() 6540430f78bSPetr Mladek * outside klp_mutex. 6550430f78bSPetr Mladek */ 6567e35e4ebSPetr Mladek static void klp_free_patch_start(struct klp_patch *patch) 657b700e7f0SSeth Jennings { 658b700e7f0SSeth Jennings if (!list_empty(&patch->list)) 659b700e7f0SSeth Jennings list_del(&patch->list); 6600430f78bSPetr Mladek 6610430f78bSPetr Mladek klp_free_objects(patch); 6620430f78bSPetr Mladek } 6630430f78bSPetr Mladek 6640430f78bSPetr Mladek /* 6650430f78bSPetr Mladek * This function implements the free part that must be called outside 6660430f78bSPetr Mladek * klp_mutex. 6670430f78bSPetr Mladek * 6680430f78bSPetr Mladek * It must be called after klp_free_patch_start(). And it has to be 6690430f78bSPetr Mladek * the last function accessing the livepatch structures when the patch 6700430f78bSPetr Mladek * gets disabled. 6710430f78bSPetr Mladek */ 6720430f78bSPetr Mladek static void klp_free_patch_finish(struct klp_patch *patch) 6730430f78bSPetr Mladek { 6740430f78bSPetr Mladek /* 6750430f78bSPetr Mladek * Avoid deadlock with enabled_store() sysfs callback by 6760430f78bSPetr Mladek * calling this outside klp_mutex. It is safe because 6770430f78bSPetr Mladek * this is called when the patch gets disabled and it 6780430f78bSPetr Mladek * cannot get enabled again. 6790430f78bSPetr Mladek */ 6800430f78bSPetr Mladek kobject_put(&patch->kobj); 6810430f78bSPetr Mladek wait_for_completion(&patch->finish); 682958ef1e3SPetr Mladek 683958ef1e3SPetr Mladek /* Put the module after the last access to struct klp_patch. */ 684958ef1e3SPetr Mladek if (!patch->forced) 685958ef1e3SPetr Mladek module_put(patch->mod); 686958ef1e3SPetr Mladek } 687958ef1e3SPetr Mladek 688958ef1e3SPetr Mladek /* 689958ef1e3SPetr Mladek * The livepatch might be freed from sysfs interface created by the patch. 690958ef1e3SPetr Mladek * This work allows to wait until the interface is destroyed in a separate 691958ef1e3SPetr Mladek * context. 692958ef1e3SPetr Mladek */ 693958ef1e3SPetr Mladek static void klp_free_patch_work_fn(struct work_struct *work) 694958ef1e3SPetr Mladek { 695958ef1e3SPetr Mladek struct klp_patch *patch = 696958ef1e3SPetr Mladek container_of(work, struct klp_patch, free_work); 697958ef1e3SPetr Mladek 698958ef1e3SPetr Mladek klp_free_patch_finish(patch); 699b700e7f0SSeth Jennings } 700b700e7f0SSeth Jennings 7017e35e4ebSPetr Mladek void klp_free_patch_async(struct klp_patch *patch) 7027e35e4ebSPetr Mladek { 7037e35e4ebSPetr Mladek klp_free_patch_start(patch); 7047e35e4ebSPetr Mladek schedule_work(&patch->free_work); 7057e35e4ebSPetr Mladek } 7067e35e4ebSPetr Mladek 7077e35e4ebSPetr Mladek void klp_free_replaced_patches_async(struct klp_patch *new_patch) 7087e35e4ebSPetr Mladek { 7097e35e4ebSPetr Mladek struct klp_patch *old_patch, *tmp_patch; 7107e35e4ebSPetr Mladek 7117e35e4ebSPetr Mladek klp_for_each_patch_safe(old_patch, tmp_patch) { 7127e35e4ebSPetr Mladek if (old_patch == new_patch) 7137e35e4ebSPetr Mladek return; 7147e35e4ebSPetr Mladek klp_free_patch_async(old_patch); 7157e35e4ebSPetr Mladek } 7167e35e4ebSPetr Mladek } 7177e35e4ebSPetr Mladek 718b700e7f0SSeth Jennings static int klp_init_func(struct klp_object *obj, struct klp_func *func) 719b700e7f0SSeth Jennings { 720e1452b60SJason Baron if (!func->old_name) 721e1452b60SJason Baron return -EINVAL; 722e1452b60SJason Baron 723e1452b60SJason Baron /* 724e1452b60SJason Baron * NOPs get the address later. The patched module must be loaded, 725e1452b60SJason Baron * see klp_init_object_loaded(). 726e1452b60SJason Baron */ 727e1452b60SJason Baron if (!func->new_func && !func->nop) 728f09d9086SMiroslav Benes return -EINVAL; 729f09d9086SMiroslav Benes 7306e9df95bSKamalesh Babulal if (strlen(func->old_name) >= KSYM_NAME_LEN) 7316e9df95bSKamalesh Babulal return -EINVAL; 7326e9df95bSKamalesh Babulal 7333c33f5b9SJosh Poimboeuf INIT_LIST_HEAD(&func->stack_node); 7340dade9f3SJosh Poimboeuf func->patched = false; 735d83a7cb3SJosh Poimboeuf func->transition = false; 736b700e7f0SSeth Jennings 737444f9e99SChris J Arges /* The format for the sysfs directory is <function,sympos> where sympos 738444f9e99SChris J Arges * is the nth occurrence of this symbol in kallsyms for the patched 739444f9e99SChris J Arges * object. If the user selects 0 for old_sympos, then 1 will be used 740444f9e99SChris J Arges * since a unique symbol will be the first occurrence. 741444f9e99SChris J Arges */ 7424d141ab3SPetr Mladek return kobject_add(&func->kobj, &obj->kobj, "%s,%lu", 7434d141ab3SPetr Mladek func->old_name, 744444f9e99SChris J Arges func->old_sympos ? func->old_sympos : 1); 745b700e7f0SSeth Jennings } 746b700e7f0SSeth Jennings 747a4ae16f6SSamuel Zou static int klp_apply_object_relocs(struct klp_patch *patch, 748a4ae16f6SSamuel Zou struct klp_object *obj) 7497c8e2bddSJosh Poimboeuf { 7507c8e2bddSJosh Poimboeuf int i, ret; 7517c8e2bddSJosh Poimboeuf struct klp_modinfo *info = patch->mod->klp_info; 7527c8e2bddSJosh Poimboeuf 7537c8e2bddSJosh Poimboeuf for (i = 1; i < info->hdr.e_shnum; i++) { 7547c8e2bddSJosh Poimboeuf Elf_Shdr *sec = info->sechdrs + i; 7557c8e2bddSJosh Poimboeuf 7567c8e2bddSJosh Poimboeuf if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) 7577c8e2bddSJosh Poimboeuf continue; 7587c8e2bddSJosh Poimboeuf 7597c8e2bddSJosh Poimboeuf ret = klp_apply_section_relocs(patch->mod, info->sechdrs, 7607c8e2bddSJosh Poimboeuf info->secstrings, 7617c8e2bddSJosh Poimboeuf patch->mod->core_kallsyms.strtab, 7627c8e2bddSJosh Poimboeuf info->symndx, i, obj->name); 7637c8e2bddSJosh Poimboeuf if (ret) 7647c8e2bddSJosh Poimboeuf return ret; 7657c8e2bddSJosh Poimboeuf } 7667c8e2bddSJosh Poimboeuf 7677c8e2bddSJosh Poimboeuf return 0; 7687c8e2bddSJosh Poimboeuf } 7697c8e2bddSJosh Poimboeuf 770b700e7f0SSeth Jennings /* parts of the initialization that is done only when the object is loaded */ 771b700e7f0SSeth Jennings static int klp_init_object_loaded(struct klp_patch *patch, 772b700e7f0SSeth Jennings struct klp_object *obj) 773b700e7f0SSeth Jennings { 774b700e7f0SSeth Jennings struct klp_func *func; 775b700e7f0SSeth Jennings int ret; 776b700e7f0SSeth Jennings 7771d05334dSPeter Zijlstra if (klp_is_module(obj)) { 7787c8e2bddSJosh Poimboeuf /* 7797c8e2bddSJosh Poimboeuf * Only write module-specific relocations here 7807c8e2bddSJosh Poimboeuf * (.klp.rela.{module}.*). vmlinux-specific relocations were 7817c8e2bddSJosh Poimboeuf * written earlier during the initialization of the klp module 7827c8e2bddSJosh Poimboeuf * itself. 7837c8e2bddSJosh Poimboeuf */ 7847c8e2bddSJosh Poimboeuf ret = klp_apply_object_relocs(patch, obj); 7851d05334dSPeter Zijlstra if (ret) 786b700e7f0SSeth Jennings return ret; 787255e732cSJessica Yu } 7889f255b63SJosh Poimboeuf 7898cdd043aSJiri Slaby klp_for_each_func(obj, func) { 790b2b018efSChris J Arges ret = klp_find_object_symbol(obj->name, func->old_name, 791b2b018efSChris J Arges func->old_sympos, 79219514910SPetr Mladek (unsigned long *)&func->old_func); 793b700e7f0SSeth Jennings if (ret) 794b700e7f0SSeth Jennings return ret; 795f5e547f4SJosh Poimboeuf 79619514910SPetr Mladek ret = kallsyms_lookup_size_offset((unsigned long)func->old_func, 797f5e547f4SJosh Poimboeuf &func->old_size, NULL); 798f5e547f4SJosh Poimboeuf if (!ret) { 799f5e547f4SJosh Poimboeuf pr_err("kallsyms size lookup failed for '%s'\n", 800f5e547f4SJosh Poimboeuf func->old_name); 801f5e547f4SJosh Poimboeuf return -ENOENT; 802f5e547f4SJosh Poimboeuf } 803f5e547f4SJosh Poimboeuf 804e1452b60SJason Baron if (func->nop) 805e1452b60SJason Baron func->new_func = func->old_func; 806e1452b60SJason Baron 807f5e547f4SJosh Poimboeuf ret = kallsyms_lookup_size_offset((unsigned long)func->new_func, 808f5e547f4SJosh Poimboeuf &func->new_size, NULL); 809f5e547f4SJosh Poimboeuf if (!ret) { 810f5e547f4SJosh Poimboeuf pr_err("kallsyms size lookup failed for '%s' replacement\n", 811f5e547f4SJosh Poimboeuf func->old_name); 812f5e547f4SJosh Poimboeuf return -ENOENT; 813f5e547f4SJosh Poimboeuf } 814b700e7f0SSeth Jennings } 815b700e7f0SSeth Jennings 816b700e7f0SSeth Jennings return 0; 817b700e7f0SSeth Jennings } 818b700e7f0SSeth Jennings 819b700e7f0SSeth Jennings static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) 820b700e7f0SSeth Jennings { 821b700e7f0SSeth Jennings struct klp_func *func; 822b700e7f0SSeth Jennings int ret; 823b700e7f0SSeth Jennings const char *name; 824b700e7f0SSeth Jennings 8256e9df95bSKamalesh Babulal if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN) 8266e9df95bSKamalesh Babulal return -EINVAL; 8276e9df95bSKamalesh Babulal 8280dade9f3SJosh Poimboeuf obj->patched = false; 8298cb2c2dcSPetr Mladek obj->mod = NULL; 830b700e7f0SSeth Jennings 831b700e7f0SSeth Jennings klp_find_object_module(obj); 832b700e7f0SSeth Jennings 833b700e7f0SSeth Jennings name = klp_is_module(obj) ? obj->name : "vmlinux"; 8344d141ab3SPetr Mladek ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name); 835cad706dfSMiroslav Benes if (ret) 836cad706dfSMiroslav Benes return ret; 837b700e7f0SSeth Jennings 8388cdd043aSJiri Slaby klp_for_each_func(obj, func) { 839b700e7f0SSeth Jennings ret = klp_init_func(obj, func); 840b700e7f0SSeth Jennings if (ret) 8410430f78bSPetr Mladek return ret; 842b700e7f0SSeth Jennings } 843b700e7f0SSeth Jennings 8440430f78bSPetr Mladek if (klp_is_object_loaded(obj)) 845b700e7f0SSeth Jennings ret = klp_init_object_loaded(patch, obj); 8460430f78bSPetr Mladek 8470430f78bSPetr Mladek return ret; 8480430f78bSPetr Mladek } 8490430f78bSPetr Mladek 850f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj, 851f68d67cfSPetr Mladek struct klp_func *func) 852f68d67cfSPetr Mladek { 853f68d67cfSPetr Mladek kobject_init(&func->kobj, &klp_ktype_func); 854f68d67cfSPetr Mladek list_add_tail(&func->node, &obj->func_list); 855f68d67cfSPetr Mladek } 856f68d67cfSPetr Mladek 857f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch, 858f68d67cfSPetr Mladek struct klp_object *obj) 859f68d67cfSPetr Mladek { 860f68d67cfSPetr Mladek INIT_LIST_HEAD(&obj->func_list); 861f68d67cfSPetr Mladek kobject_init(&obj->kobj, &klp_ktype_object); 862f68d67cfSPetr Mladek list_add_tail(&obj->node, &patch->obj_list); 863f68d67cfSPetr Mladek } 864f68d67cfSPetr Mladek 8655ef3dd20SDavid Vernet static void klp_init_patch_early(struct klp_patch *patch) 8660430f78bSPetr Mladek { 8670430f78bSPetr Mladek struct klp_object *obj; 8680430f78bSPetr Mladek struct klp_func *func; 8690430f78bSPetr Mladek 8700430f78bSPetr Mladek INIT_LIST_HEAD(&patch->list); 87120e55025SJason Baron INIT_LIST_HEAD(&patch->obj_list); 8724d141ab3SPetr Mladek kobject_init(&patch->kobj, &klp_ktype_patch); 8730430f78bSPetr Mladek patch->enabled = false; 87468007289SPetr Mladek patch->forced = false; 875958ef1e3SPetr Mladek INIT_WORK(&patch->free_work, klp_free_patch_work_fn); 8760430f78bSPetr Mladek init_completion(&patch->finish); 8770430f78bSPetr Mladek 87820e55025SJason Baron klp_for_each_object_static(patch, obj) { 879f68d67cfSPetr Mladek klp_init_object_early(patch, obj); 8800430f78bSPetr Mladek 88120e55025SJason Baron klp_for_each_func_static(obj, func) { 882f68d67cfSPetr Mladek klp_init_func_early(obj, func); 88320e55025SJason Baron } 884b700e7f0SSeth Jennings } 885b700e7f0SSeth Jennings } 886b700e7f0SSeth Jennings 887b700e7f0SSeth Jennings static int klp_init_patch(struct klp_patch *patch) 888b700e7f0SSeth Jennings { 889b700e7f0SSeth Jennings struct klp_object *obj; 890b700e7f0SSeth Jennings int ret; 891b700e7f0SSeth Jennings 8924d141ab3SPetr Mladek ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name); 893958ef1e3SPetr Mladek if (ret) 8943ec24776SJosh Poimboeuf return ret; 895b700e7f0SSeth Jennings 896e1452b60SJason Baron if (patch->replace) { 897e1452b60SJason Baron ret = klp_add_nops(patch); 898e1452b60SJason Baron if (ret) 899e1452b60SJason Baron return ret; 900e1452b60SJason Baron } 901e1452b60SJason Baron 9028cdd043aSJiri Slaby klp_for_each_object(patch, obj) { 903b700e7f0SSeth Jennings ret = klp_init_object(patch, obj); 904b700e7f0SSeth Jennings if (ret) 905958ef1e3SPetr Mladek return ret; 906b700e7f0SSeth Jennings } 907b700e7f0SSeth Jennings 90899590ba5SJosh Poimboeuf list_add_tail(&patch->list, &klp_patches); 909b700e7f0SSeth Jennings 910b700e7f0SSeth Jennings return 0; 911b700e7f0SSeth Jennings } 912b700e7f0SSeth Jennings 91326c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch) 91426c3e98eSPetr Mladek { 91526c3e98eSPetr Mladek struct klp_object *obj; 91626c3e98eSPetr Mladek 91726c3e98eSPetr Mladek if (WARN_ON(!patch->enabled)) 91826c3e98eSPetr Mladek return -EINVAL; 91926c3e98eSPetr Mladek 92026c3e98eSPetr Mladek if (klp_transition_patch) 92126c3e98eSPetr Mladek return -EBUSY; 92226c3e98eSPetr Mladek 92326c3e98eSPetr Mladek klp_init_transition(patch, KLP_UNPATCHED); 92426c3e98eSPetr Mladek 92526c3e98eSPetr Mladek klp_for_each_object(patch, obj) 92626c3e98eSPetr Mladek if (obj->patched) 92726c3e98eSPetr Mladek klp_pre_unpatch_callback(obj); 92826c3e98eSPetr Mladek 92926c3e98eSPetr Mladek /* 93026c3e98eSPetr Mladek * Enforce the order of the func->transition writes in 93126c3e98eSPetr Mladek * klp_init_transition() and the TIF_PATCH_PENDING writes in 93226c3e98eSPetr Mladek * klp_start_transition(). In the rare case where klp_ftrace_handler() 93326c3e98eSPetr Mladek * is called shortly after klp_update_patch_state() switches the task, 93426c3e98eSPetr Mladek * this ensures the handler sees that func->transition is set. 93526c3e98eSPetr Mladek */ 93626c3e98eSPetr Mladek smp_wmb(); 93726c3e98eSPetr Mladek 93826c3e98eSPetr Mladek klp_start_transition(); 93926c3e98eSPetr Mladek patch->enabled = false; 940958ef1e3SPetr Mladek klp_try_complete_transition(); 94126c3e98eSPetr Mladek 94226c3e98eSPetr Mladek return 0; 94326c3e98eSPetr Mladek } 94426c3e98eSPetr Mladek 94526c3e98eSPetr Mladek static int __klp_enable_patch(struct klp_patch *patch) 94626c3e98eSPetr Mladek { 94726c3e98eSPetr Mladek struct klp_object *obj; 94826c3e98eSPetr Mladek int ret; 94926c3e98eSPetr Mladek 95026c3e98eSPetr Mladek if (klp_transition_patch) 95126c3e98eSPetr Mladek return -EBUSY; 95226c3e98eSPetr Mladek 95326c3e98eSPetr Mladek if (WARN_ON(patch->enabled)) 95426c3e98eSPetr Mladek return -EINVAL; 95526c3e98eSPetr Mladek 95626c3e98eSPetr Mladek pr_notice("enabling patch '%s'\n", patch->mod->name); 95726c3e98eSPetr Mladek 95826c3e98eSPetr Mladek klp_init_transition(patch, KLP_PATCHED); 95926c3e98eSPetr Mladek 96026c3e98eSPetr Mladek /* 96126c3e98eSPetr Mladek * Enforce the order of the func->transition writes in 96226c3e98eSPetr Mladek * klp_init_transition() and the ops->func_stack writes in 96326c3e98eSPetr Mladek * klp_patch_object(), so that klp_ftrace_handler() will see the 96426c3e98eSPetr Mladek * func->transition updates before the handler is registered and the 96526c3e98eSPetr Mladek * new funcs become visible to the handler. 96626c3e98eSPetr Mladek */ 96726c3e98eSPetr Mladek smp_wmb(); 96826c3e98eSPetr Mladek 96926c3e98eSPetr Mladek klp_for_each_object(patch, obj) { 97026c3e98eSPetr Mladek if (!klp_is_object_loaded(obj)) 97126c3e98eSPetr Mladek continue; 97226c3e98eSPetr Mladek 97326c3e98eSPetr Mladek ret = klp_pre_patch_callback(obj); 97426c3e98eSPetr Mladek if (ret) { 97526c3e98eSPetr Mladek pr_warn("pre-patch callback failed for object '%s'\n", 97626c3e98eSPetr Mladek klp_is_module(obj) ? obj->name : "vmlinux"); 97726c3e98eSPetr Mladek goto err; 97826c3e98eSPetr Mladek } 97926c3e98eSPetr Mladek 98026c3e98eSPetr Mladek ret = klp_patch_object(obj); 98126c3e98eSPetr Mladek if (ret) { 98226c3e98eSPetr Mladek pr_warn("failed to patch object '%s'\n", 98326c3e98eSPetr Mladek klp_is_module(obj) ? obj->name : "vmlinux"); 98426c3e98eSPetr Mladek goto err; 98526c3e98eSPetr Mladek } 98626c3e98eSPetr Mladek } 98726c3e98eSPetr Mladek 98826c3e98eSPetr Mladek klp_start_transition(); 98926c3e98eSPetr Mladek patch->enabled = true; 990958ef1e3SPetr Mladek klp_try_complete_transition(); 99126c3e98eSPetr Mladek 99226c3e98eSPetr Mladek return 0; 99326c3e98eSPetr Mladek err: 99426c3e98eSPetr Mladek pr_warn("failed to enable patch '%s'\n", patch->mod->name); 99526c3e98eSPetr Mladek 99626c3e98eSPetr Mladek klp_cancel_transition(); 99726c3e98eSPetr Mladek return ret; 99826c3e98eSPetr Mladek } 99926c3e98eSPetr Mladek 100026c3e98eSPetr Mladek /** 1001958ef1e3SPetr Mladek * klp_enable_patch() - enable the livepatch 1002958ef1e3SPetr Mladek * @patch: patch to be enabled 100326c3e98eSPetr Mladek * 1004958ef1e3SPetr Mladek * Initializes the data structure associated with the patch, creates the sysfs 1005958ef1e3SPetr Mladek * interface, performs the needed symbol lookups and code relocations, 1006958ef1e3SPetr Mladek * registers the patched functions with ftrace. 1007958ef1e3SPetr Mladek * 1008958ef1e3SPetr Mladek * This function is supposed to be called from the livepatch module_init() 1009958ef1e3SPetr Mladek * callback. 101026c3e98eSPetr Mladek * 101126c3e98eSPetr Mladek * Return: 0 on success, otherwise error 101226c3e98eSPetr Mladek */ 101326c3e98eSPetr Mladek int klp_enable_patch(struct klp_patch *patch) 101426c3e98eSPetr Mladek { 101526c3e98eSPetr Mladek int ret; 10165ef3dd20SDavid Vernet struct klp_object *obj; 101726c3e98eSPetr Mladek 10185ef3dd20SDavid Vernet if (!patch || !patch->mod || !patch->objs) 1019958ef1e3SPetr Mladek return -EINVAL; 102026c3e98eSPetr Mladek 10215ef3dd20SDavid Vernet klp_for_each_object_static(patch, obj) { 10225ef3dd20SDavid Vernet if (!obj->funcs) 10235ef3dd20SDavid Vernet return -EINVAL; 10245ef3dd20SDavid Vernet } 10255ef3dd20SDavid Vernet 10265ef3dd20SDavid Vernet 1027958ef1e3SPetr Mladek if (!is_livepatch_module(patch->mod)) { 1028958ef1e3SPetr Mladek pr_err("module %s is not marked as a livepatch module\n", 1029958ef1e3SPetr Mladek patch->mod->name); 1030958ef1e3SPetr Mladek return -EINVAL; 103126c3e98eSPetr Mladek } 103226c3e98eSPetr Mladek 1033958ef1e3SPetr Mladek if (!klp_initialized()) 1034958ef1e3SPetr Mladek return -ENODEV; 1035958ef1e3SPetr Mladek 1036958ef1e3SPetr Mladek if (!klp_have_reliable_stack()) { 103731adf230SPetr Mladek pr_warn("This architecture doesn't have support for the livepatch consistency model.\n"); 103831adf230SPetr Mladek pr_warn("The livepatch transition may never complete.\n"); 1039958ef1e3SPetr Mladek } 1040958ef1e3SPetr Mladek 1041958ef1e3SPetr Mladek mutex_lock(&klp_mutex); 1042958ef1e3SPetr Mladek 104392c9abf5SPetr Mladek if (!klp_is_patch_compatible(patch)) { 104492c9abf5SPetr Mladek pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n", 104592c9abf5SPetr Mladek patch->mod->name); 104692c9abf5SPetr Mladek mutex_unlock(&klp_mutex); 104792c9abf5SPetr Mladek return -EINVAL; 104892c9abf5SPetr Mladek } 104992c9abf5SPetr Mladek 105050a0f3f5SYang Yingliang if (!try_module_get(patch->mod)) { 105150a0f3f5SYang Yingliang mutex_unlock(&klp_mutex); 10525ef3dd20SDavid Vernet return -ENODEV; 105350a0f3f5SYang Yingliang } 10545ef3dd20SDavid Vernet 10555ef3dd20SDavid Vernet klp_init_patch_early(patch); 1056958ef1e3SPetr Mladek 1057958ef1e3SPetr Mladek ret = klp_init_patch(patch); 1058958ef1e3SPetr Mladek if (ret) 1059958ef1e3SPetr Mladek goto err; 1060958ef1e3SPetr Mladek 106126c3e98eSPetr Mladek ret = __klp_enable_patch(patch); 1062958ef1e3SPetr Mladek if (ret) 1063958ef1e3SPetr Mladek goto err; 1064958ef1e3SPetr Mladek 1065958ef1e3SPetr Mladek mutex_unlock(&klp_mutex); 1066958ef1e3SPetr Mladek 1067958ef1e3SPetr Mladek return 0; 106826c3e98eSPetr Mladek 106926c3e98eSPetr Mladek err: 1070958ef1e3SPetr Mladek klp_free_patch_start(patch); 1071958ef1e3SPetr Mladek 107226c3e98eSPetr Mladek mutex_unlock(&klp_mutex); 1073958ef1e3SPetr Mladek 1074958ef1e3SPetr Mladek klp_free_patch_finish(patch); 1075958ef1e3SPetr Mladek 107626c3e98eSPetr Mladek return ret; 107726c3e98eSPetr Mladek } 107826c3e98eSPetr Mladek EXPORT_SYMBOL_GPL(klp_enable_patch); 107926c3e98eSPetr Mladek 1080ef8daf8eSJoe Lawrence /* 10817e35e4ebSPetr Mladek * This function unpatches objects from the replaced livepatches. 1082e1452b60SJason Baron * 1083e1452b60SJason Baron * We could be pretty aggressive here. It is called in the situation where 10847e35e4ebSPetr Mladek * these structures are no longer accessed from the ftrace handler. 10857e35e4ebSPetr Mladek * All functions are redirected by the klp_transition_patch. They 10867e35e4ebSPetr Mladek * use either a new code or they are in the original code because 10877e35e4ebSPetr Mladek * of the special nop function patches. 1088e1452b60SJason Baron * 1089e1452b60SJason Baron * The only exception is when the transition was forced. In this case, 1090e1452b60SJason Baron * klp_ftrace_handler() might still see the replaced patch on the stack. 1091e1452b60SJason Baron * Fortunately, it is carefully designed to work with removed functions 1092e1452b60SJason Baron * thanks to RCU. We only have to keep the patches on the system. Also 1093e1452b60SJason Baron * this is handled transparently by patch->module_put. 1094e1452b60SJason Baron */ 10957e35e4ebSPetr Mladek void klp_unpatch_replaced_patches(struct klp_patch *new_patch) 1096e1452b60SJason Baron { 10977e35e4ebSPetr Mladek struct klp_patch *old_patch; 1098e1452b60SJason Baron 10997e35e4ebSPetr Mladek klp_for_each_patch(old_patch) { 1100e1452b60SJason Baron if (old_patch == new_patch) 1101e1452b60SJason Baron return; 1102e1452b60SJason Baron 1103e1452b60SJason Baron old_patch->enabled = false; 1104e1452b60SJason Baron klp_unpatch_objects(old_patch); 1105e1452b60SJason Baron } 1106e1452b60SJason Baron } 1107e1452b60SJason Baron 1108e1452b60SJason Baron /* 1109d697bad5SPetr Mladek * This function removes the dynamically allocated 'nop' functions. 1110d697bad5SPetr Mladek * 1111d697bad5SPetr Mladek * We could be pretty aggressive. NOPs do not change the existing 1112d697bad5SPetr Mladek * behavior except for adding unnecessary delay by the ftrace handler. 1113d697bad5SPetr Mladek * 1114d697bad5SPetr Mladek * It is safe even when the transition was forced. The ftrace handler 1115d697bad5SPetr Mladek * will see a valid ops->func_stack entry thanks to RCU. 1116d697bad5SPetr Mladek * 1117d697bad5SPetr Mladek * We could even free the NOPs structures. They must be the last entry 1118d697bad5SPetr Mladek * in ops->func_stack. Therefore unregister_ftrace_function() is called. 1119d697bad5SPetr Mladek * It does the same as klp_synchronize_transition() to make sure that 1120d697bad5SPetr Mladek * nobody is inside the ftrace handler once the operation finishes. 1121d697bad5SPetr Mladek * 1122d697bad5SPetr Mladek * IMPORTANT: It must be called right after removing the replaced patches! 1123d697bad5SPetr Mladek */ 1124d697bad5SPetr Mladek void klp_discard_nops(struct klp_patch *new_patch) 1125d697bad5SPetr Mladek { 1126d697bad5SPetr Mladek klp_unpatch_objects_dynamic(klp_transition_patch); 1127d697bad5SPetr Mladek klp_free_objects_dynamic(klp_transition_patch); 1128d697bad5SPetr Mladek } 1129d697bad5SPetr Mladek 1130d697bad5SPetr Mladek /* 1131ef8daf8eSJoe Lawrence * Remove parts of patches that touch a given kernel module. The list of 1132ef8daf8eSJoe Lawrence * patches processed might be limited. When limit is NULL, all patches 1133ef8daf8eSJoe Lawrence * will be handled. 1134ef8daf8eSJoe Lawrence */ 1135ef8daf8eSJoe Lawrence static void klp_cleanup_module_patches_limited(struct module *mod, 1136ef8daf8eSJoe Lawrence struct klp_patch *limit) 1137ef8daf8eSJoe Lawrence { 1138ef8daf8eSJoe Lawrence struct klp_patch *patch; 1139ef8daf8eSJoe Lawrence struct klp_object *obj; 1140ef8daf8eSJoe Lawrence 1141ecba29f4SPetr Mladek klp_for_each_patch(patch) { 1142ef8daf8eSJoe Lawrence if (patch == limit) 1143ef8daf8eSJoe Lawrence break; 1144ef8daf8eSJoe Lawrence 1145ef8daf8eSJoe Lawrence klp_for_each_object(patch, obj) { 1146ef8daf8eSJoe Lawrence if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 1147ef8daf8eSJoe Lawrence continue; 1148ef8daf8eSJoe Lawrence 1149fc41efc1SJiri Kosina if (patch != klp_transition_patch) 1150fc41efc1SJiri Kosina klp_pre_unpatch_callback(obj); 1151fc41efc1SJiri Kosina 1152ef8daf8eSJoe Lawrence pr_notice("reverting patch '%s' on unloading module '%s'\n", 1153ef8daf8eSJoe Lawrence patch->mod->name, obj->mod->name); 1154ef8daf8eSJoe Lawrence klp_unpatch_object(obj); 1155fc41efc1SJiri Kosina 1156fc41efc1SJiri Kosina klp_post_unpatch_callback(obj); 1157ef8daf8eSJoe Lawrence 1158ef8daf8eSJoe Lawrence klp_free_object_loaded(obj); 1159ef8daf8eSJoe Lawrence break; 1160ef8daf8eSJoe Lawrence } 1161ef8daf8eSJoe Lawrence } 1162ef8daf8eSJoe Lawrence } 1163ef8daf8eSJoe Lawrence 11647e545d6eSJessica Yu int klp_module_coming(struct module *mod) 1165b700e7f0SSeth Jennings { 116636e505c1SMinfei Huang int ret; 1167b700e7f0SSeth Jennings struct klp_patch *patch; 1168b700e7f0SSeth Jennings struct klp_object *obj; 1169b700e7f0SSeth Jennings 11707e545d6eSJessica Yu if (WARN_ON(mod->state != MODULE_STATE_COMING)) 11717e545d6eSJessica Yu return -EINVAL; 1172b700e7f0SSeth Jennings 1173dcf550e5SJosh Poimboeuf if (!strcmp(mod->name, "vmlinux")) { 1174dcf550e5SJosh Poimboeuf pr_err("vmlinux.ko: invalid module name"); 1175dcf550e5SJosh Poimboeuf return -EINVAL; 1176dcf550e5SJosh Poimboeuf } 1177dcf550e5SJosh Poimboeuf 1178b700e7f0SSeth Jennings mutex_lock(&klp_mutex); 11798cb2c2dcSPetr Mladek /* 11807e545d6eSJessica Yu * Each module has to know that klp_module_coming() 11817e545d6eSJessica Yu * has been called. We never know what module will 11827e545d6eSJessica Yu * get patched by a new patch. 11838cb2c2dcSPetr Mladek */ 11848cb2c2dcSPetr Mladek mod->klp_alive = true; 11858cb2c2dcSPetr Mladek 1186ecba29f4SPetr Mladek klp_for_each_patch(patch) { 11878cdd043aSJiri Slaby klp_for_each_object(patch, obj) { 1188b700e7f0SSeth Jennings if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) 1189b700e7f0SSeth Jennings continue; 1190b700e7f0SSeth Jennings 1191b700e7f0SSeth Jennings obj->mod = mod; 11927e545d6eSJessica Yu 11937e545d6eSJessica Yu ret = klp_init_object_loaded(patch, obj); 119436e505c1SMinfei Huang if (ret) { 11957e545d6eSJessica Yu pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", 11967e545d6eSJessica Yu patch->mod->name, obj->mod->name, ret); 11977e545d6eSJessica Yu goto err; 119836e505c1SMinfei Huang } 11997e545d6eSJessica Yu 12007e545d6eSJessica Yu pr_notice("applying patch '%s' to loading module '%s'\n", 12017e545d6eSJessica Yu patch->mod->name, obj->mod->name); 12027e545d6eSJessica Yu 120393862e38SJoe Lawrence ret = klp_pre_patch_callback(obj); 120493862e38SJoe Lawrence if (ret) { 120593862e38SJoe Lawrence pr_warn("pre-patch callback failed for object '%s'\n", 120693862e38SJoe Lawrence obj->name); 120793862e38SJoe Lawrence goto err; 120893862e38SJoe Lawrence } 120993862e38SJoe Lawrence 12100dade9f3SJosh Poimboeuf ret = klp_patch_object(obj); 12117e545d6eSJessica Yu if (ret) { 12127e545d6eSJessica Yu pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", 12137e545d6eSJessica Yu patch->mod->name, obj->mod->name, ret); 121493862e38SJoe Lawrence 121593862e38SJoe Lawrence klp_post_unpatch_callback(obj); 12167e545d6eSJessica Yu goto err; 12177e545d6eSJessica Yu } 1218b700e7f0SSeth Jennings 121993862e38SJoe Lawrence if (patch != klp_transition_patch) 122093862e38SJoe Lawrence klp_post_patch_callback(obj); 122193862e38SJoe Lawrence 1222b700e7f0SSeth Jennings break; 1223b700e7f0SSeth Jennings } 1224b700e7f0SSeth Jennings } 1225b700e7f0SSeth Jennings 1226b700e7f0SSeth Jennings mutex_unlock(&klp_mutex); 1227b700e7f0SSeth Jennings 1228b700e7f0SSeth Jennings return 0; 12297e545d6eSJessica Yu 12307e545d6eSJessica Yu err: 12317e545d6eSJessica Yu /* 12327e545d6eSJessica Yu * If a patch is unsuccessfully applied, return 12337e545d6eSJessica Yu * error to the module loader. 12347e545d6eSJessica Yu */ 12357e545d6eSJessica Yu pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n", 12367e545d6eSJessica Yu patch->mod->name, obj->mod->name, obj->mod->name); 12377e545d6eSJessica Yu mod->klp_alive = false; 12384ff96fb5SMiroslav Benes obj->mod = NULL; 1239ef8daf8eSJoe Lawrence klp_cleanup_module_patches_limited(mod, patch); 12407e545d6eSJessica Yu mutex_unlock(&klp_mutex); 12417e545d6eSJessica Yu 12427e545d6eSJessica Yu return ret; 1243b700e7f0SSeth Jennings } 1244b700e7f0SSeth Jennings 12457e545d6eSJessica Yu void klp_module_going(struct module *mod) 12467e545d6eSJessica Yu { 12477e545d6eSJessica Yu if (WARN_ON(mod->state != MODULE_STATE_GOING && 12487e545d6eSJessica Yu mod->state != MODULE_STATE_COMING)) 12497e545d6eSJessica Yu return; 12507e545d6eSJessica Yu 12517e545d6eSJessica Yu mutex_lock(&klp_mutex); 12527e545d6eSJessica Yu /* 12537e545d6eSJessica Yu * Each module has to know that klp_module_going() 12547e545d6eSJessica Yu * has been called. We never know what module will 12557e545d6eSJessica Yu * get patched by a new patch. 12567e545d6eSJessica Yu */ 12577e545d6eSJessica Yu mod->klp_alive = false; 12587e545d6eSJessica Yu 1259ef8daf8eSJoe Lawrence klp_cleanup_module_patches_limited(mod, NULL); 12607e545d6eSJessica Yu 12617e545d6eSJessica Yu mutex_unlock(&klp_mutex); 12627e545d6eSJessica Yu } 1263b700e7f0SSeth Jennings 126426029d88SMinfei Huang static int __init klp_init(void) 1265b700e7f0SSeth Jennings { 1266b700e7f0SSeth Jennings klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj); 12677e545d6eSJessica Yu if (!klp_root_kobj) 12687e545d6eSJessica Yu return -ENOMEM; 1269b700e7f0SSeth Jennings 1270b700e7f0SSeth Jennings return 0; 1271b700e7f0SSeth Jennings } 1272b700e7f0SSeth Jennings 1273b700e7f0SSeth Jennings module_init(klp_init); 1274