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