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