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 | c349cdc | 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 | |
| 95 | struct klp_find_arg { |
| 96 | const char *objname; |
| 97 | const char *name; |
| 98 | unsigned long addr; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 99 | unsigned long count; |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 100 | unsigned long pos; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static int klp_find_callback(void *data, const char *name, |
| 104 | struct module *mod, unsigned long addr) |
| 105 | { |
| 106 | struct klp_find_arg *args = data; |
| 107 | |
| 108 | if ((mod && !args->objname) || (!mod && args->objname)) |
| 109 | return 0; |
| 110 | |
| 111 | if (strcmp(args->name, name)) |
| 112 | return 0; |
| 113 | |
| 114 | if (args->objname && strcmp(args->objname, mod->name)) |
| 115 | return 0; |
| 116 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 117 | args->addr = addr; |
| 118 | args->count++; |
| 119 | |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 120 | /* |
| 121 | * Finish the search when the symbol is found for the desired position |
| 122 | * or the position is not defined for a non-unique symbol. |
| 123 | */ |
| 124 | if ((args->pos && (args->count == args->pos)) || |
| 125 | (!args->pos && (args->count > 1))) |
| 126 | return 1; |
| 127 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | 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] | 132 | unsigned long sympos, unsigned long *addr) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 133 | { |
| 134 | struct klp_find_arg args = { |
| 135 | .objname = objname, |
| 136 | .name = name, |
| 137 | .addr = 0, |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 138 | .count = 0, |
| 139 | .pos = sympos, |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 140 | }; |
| 141 | |
Miroslav Benes | 9a1bd63 | 2015-06-01 17:48:37 +0200 | [diff] [blame] | 142 | mutex_lock(&module_mutex); |
Zhou Chengming | 72f04b5 | 2017-03-28 21:10:35 +0800 | [diff] [blame] | 143 | if (objname) |
| 144 | module_kallsyms_on_each_symbol(klp_find_callback, &args); |
| 145 | else |
| 146 | kallsyms_on_each_symbol(klp_find_callback, &args); |
Miroslav Benes | 9a1bd63 | 2015-06-01 17:48:37 +0200 | [diff] [blame] | 147 | mutex_unlock(&module_mutex); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 148 | |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 149 | /* |
| 150 | * Ensure an address was found. If sympos is 0, ensure symbol is unique; |
| 151 | * otherwise ensure the symbol position count matches sympos. |
| 152 | */ |
| 153 | if (args.addr == 0) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 154 | pr_err("symbol '%s' not found in symbol table\n", name); |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 155 | else if (args.count > 1 && sympos == 0) { |
Petr Mladek | f995b5f | 2016-03-09 15:20:59 +0100 | [diff] [blame] | 156 | pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n", |
| 157 | name, objname); |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 158 | } else if (sympos != args.count && sympos > 0) { |
| 159 | pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n", |
| 160 | sympos, name, objname ? objname : "vmlinux"); |
| 161 | } else { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 162 | *addr = args.addr; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | *addr = 0; |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 170 | static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 171 | { |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 172 | int i, cnt, vmlinux, ret; |
| 173 | char objname[MODULE_NAME_LEN]; |
| 174 | char symname[KSYM_NAME_LEN]; |
| 175 | char *strtab = pmod->core_kallsyms.strtab; |
| 176 | Elf_Rela *relas; |
| 177 | Elf_Sym *sym; |
| 178 | unsigned long sympos, addr; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 179 | |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 180 | /* |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 181 | * Since the field widths for objname and symname in the sscanf() |
| 182 | * call are hard-coded and correspond to MODULE_NAME_LEN and |
| 183 | * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN |
| 184 | * and KSYM_NAME_LEN have the values we expect them to have. |
| 185 | * |
| 186 | * Because the value of MODULE_NAME_LEN can differ among architectures, |
| 187 | * we use the smallest/strictest upper bound possible (56, based on |
| 188 | * the current definition of MODULE_NAME_LEN) to prevent overflows. |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 189 | */ |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 190 | BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128); |
| 191 | |
| 192 | relas = (Elf_Rela *) relasec->sh_addr; |
| 193 | /* For each rela in this klp relocation section */ |
| 194 | for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) { |
| 195 | sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info); |
| 196 | if (sym->st_shndx != SHN_LIVEPATCH) { |
Josh Poimboeuf | 77f8f39 | 2017-04-13 17:59:15 -0500 | [diff] [blame] | 197 | pr_err("symbol %s is not marked as a livepatch symbol\n", |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 198 | strtab + sym->st_name); |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
| 202 | /* Format: .klp.sym.objname.symname,sympos */ |
| 203 | cnt = sscanf(strtab + sym->st_name, |
| 204 | ".klp.sym.%55[^.].%127[^,],%lu", |
| 205 | objname, symname, &sympos); |
| 206 | if (cnt != 3) { |
Josh Poimboeuf | 77f8f39 | 2017-04-13 17:59:15 -0500 | [diff] [blame] | 207 | pr_err("symbol %s has an incorrectly formatted name\n", |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 208 | strtab + sym->st_name); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
| 212 | /* klp_find_object_symbol() treats a NULL objname as vmlinux */ |
| 213 | vmlinux = !strcmp(objname, "vmlinux"); |
| 214 | ret = klp_find_object_symbol(vmlinux ? NULL : objname, |
| 215 | symname, sympos, &addr); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | |
| 219 | sym->st_value = addr; |
| 220 | } |
| 221 | |
| 222 | return 0; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int klp_write_object_relocations(struct module *pmod, |
| 226 | struct klp_object *obj) |
| 227 | { |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 228 | int i, cnt, ret = 0; |
| 229 | const char *objname, *secname; |
| 230 | char sec_objname[MODULE_NAME_LEN]; |
| 231 | Elf_Shdr *sec; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 232 | |
| 233 | if (WARN_ON(!klp_is_object_loaded(obj))) |
| 234 | return -EINVAL; |
| 235 | |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 236 | objname = klp_is_module(obj) ? obj->name : "vmlinux"; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 237 | |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 238 | /* For each klp relocation section */ |
| 239 | for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) { |
| 240 | sec = pmod->klp_info->sechdrs + i; |
| 241 | secname = pmod->klp_info->secstrings + sec->sh_name; |
| 242 | if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) |
| 243 | continue; |
Josh Poimboeuf | b56b36e | 2015-12-03 16:33:26 -0600 | [diff] [blame] | 244 | |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 245 | /* |
| 246 | * Format: .klp.rela.sec_objname.section_name |
| 247 | * See comment in klp_resolve_symbols() for an explanation |
| 248 | * of the selected field width value. |
| 249 | */ |
| 250 | cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname); |
| 251 | if (cnt != 1) { |
Josh Poimboeuf | 77f8f39 | 2017-04-13 17:59:15 -0500 | [diff] [blame] | 252 | pr_err("section %s has an incorrectly formatted name\n", |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 253 | secname); |
| 254 | ret = -EINVAL; |
| 255 | break; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 256 | } |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 257 | |
| 258 | if (strcmp(objname, sec_objname)) |
| 259 | continue; |
| 260 | |
| 261 | ret = klp_resolve_symbols(sec, pmod); |
| 262 | if (ret) |
| 263 | break; |
| 264 | |
| 265 | ret = apply_relocate_add(pmod->klp_info->sechdrs, |
| 266 | pmod->core_kallsyms.strtab, |
| 267 | pmod->klp_info->symndx, i, pmod); |
| 268 | if (ret) |
| 269 | break; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 270 | } |
| 271 | |
Josh Poimboeuf | b56b36e | 2015-12-03 16:33:26 -0600 | [diff] [blame] | 272 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 273 | } |
| 274 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 275 | /* |
| 276 | * Sysfs Interface |
| 277 | * |
| 278 | * /sys/kernel/livepatch |
| 279 | * /sys/kernel/livepatch/<patch> |
| 280 | * /sys/kernel/livepatch/<patch>/enabled |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 281 | * /sys/kernel/livepatch/<patch>/transition |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 282 | * /sys/kernel/livepatch/<patch>/signal |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 283 | * /sys/kernel/livepatch/<patch>/force |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 284 | * /sys/kernel/livepatch/<patch>/<object> |
Chris J Arges | 444f9e9 | 2015-12-01 20:40:56 -0600 | [diff] [blame] | 285 | * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 286 | */ |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 287 | static int __klp_disable_patch(struct klp_patch *patch); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 288 | |
| 289 | static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 290 | const char *buf, size_t count) |
| 291 | { |
| 292 | struct klp_patch *patch; |
| 293 | int ret; |
Josh Poimboeuf | 68ae4b2 | 2017-02-13 19:42:38 -0600 | [diff] [blame] | 294 | bool enabled; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 295 | |
Josh Poimboeuf | 68ae4b2 | 2017-02-13 19:42:38 -0600 | [diff] [blame] | 296 | ret = kstrtobool(buf, &enabled); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 297 | if (ret) |
Josh Poimboeuf | 68ae4b2 | 2017-02-13 19:42:38 -0600 | [diff] [blame] | 298 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 299 | |
| 300 | patch = container_of(kobj, struct klp_patch, kobj); |
| 301 | |
| 302 | mutex_lock(&klp_mutex); |
| 303 | |
Josh Poimboeuf | 68ae4b2 | 2017-02-13 19:42:38 -0600 | [diff] [blame] | 304 | if (patch->enabled == enabled) { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 305 | /* already in requested state */ |
| 306 | ret = -EINVAL; |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 307 | goto out; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 308 | } |
| 309 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 310 | /* |
| 311 | * Allow to reverse a pending transition in both ways. It might be |
| 312 | * necessary to complete the transition without forcing and breaking |
| 313 | * the system integrity. |
| 314 | * |
| 315 | * Do not allow to re-enable a disabled patch. |
| 316 | */ |
| 317 | if (patch == klp_transition_patch) |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 318 | klp_reverse_transition(); |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 319 | else if (!enabled) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 320 | ret = __klp_disable_patch(patch); |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 321 | else |
| 322 | ret = -EINVAL; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 323 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 324 | out: |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 325 | mutex_unlock(&klp_mutex); |
| 326 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 327 | if (ret) |
| 328 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 329 | return count; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static ssize_t enabled_show(struct kobject *kobj, |
| 333 | struct kobj_attribute *attr, char *buf) |
| 334 | { |
| 335 | struct klp_patch *patch; |
| 336 | |
| 337 | patch = container_of(kobj, struct klp_patch, kobj); |
Josh Poimboeuf | 0dade9f | 2017-02-13 19:42:35 -0600 | [diff] [blame] | 338 | return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 339 | } |
| 340 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 341 | static ssize_t transition_show(struct kobject *kobj, |
| 342 | struct kobj_attribute *attr, char *buf) |
| 343 | { |
| 344 | struct klp_patch *patch; |
| 345 | |
| 346 | patch = container_of(kobj, struct klp_patch, kobj); |
| 347 | return snprintf(buf, PAGE_SIZE-1, "%d\n", |
| 348 | patch == klp_transition_patch); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 349 | } |
| 350 | |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 351 | static ssize_t signal_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 352 | const char *buf, size_t count) |
| 353 | { |
| 354 | struct klp_patch *patch; |
| 355 | int ret; |
| 356 | bool val; |
| 357 | |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 358 | ret = kstrtobool(buf, &val); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
Miroslav Benes | 8869016 | 2017-12-21 14:40:43 +0100 | [diff] [blame] | 362 | if (!val) |
| 363 | return count; |
| 364 | |
| 365 | mutex_lock(&klp_mutex); |
| 366 | |
| 367 | patch = container_of(kobj, struct klp_patch, kobj); |
| 368 | if (patch != klp_transition_patch) { |
| 369 | mutex_unlock(&klp_mutex); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
| 373 | klp_send_signals(); |
| 374 | |
| 375 | mutex_unlock(&klp_mutex); |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 376 | |
| 377 | return count; |
| 378 | } |
| 379 | |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 380 | static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 381 | const char *buf, size_t count) |
| 382 | { |
| 383 | struct klp_patch *patch; |
| 384 | int ret; |
| 385 | bool val; |
| 386 | |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 387 | ret = kstrtobool(buf, &val); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | |
Miroslav Benes | 8869016 | 2017-12-21 14:40:43 +0100 | [diff] [blame] | 391 | if (!val) |
| 392 | return count; |
| 393 | |
| 394 | mutex_lock(&klp_mutex); |
| 395 | |
| 396 | patch = container_of(kobj, struct klp_patch, kobj); |
| 397 | if (patch != klp_transition_patch) { |
| 398 | mutex_unlock(&klp_mutex); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | klp_force_transition(); |
| 403 | |
| 404 | mutex_unlock(&klp_mutex); |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 405 | |
| 406 | return count; |
| 407 | } |
| 408 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 409 | static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 410 | static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition); |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 411 | static struct kobj_attribute signal_kobj_attr = __ATTR_WO(signal); |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 412 | static struct kobj_attribute force_kobj_attr = __ATTR_WO(force); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 413 | static struct attribute *klp_patch_attrs[] = { |
| 414 | &enabled_kobj_attr.attr, |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 415 | &transition_kobj_attr.attr, |
Miroslav Benes | 43347d5 | 2017-11-15 14:50:13 +0100 | [diff] [blame] | 416 | &signal_kobj_attr.attr, |
Miroslav Benes | c99a2be | 2017-11-22 11:29:21 +0100 | [diff] [blame] | 417 | &force_kobj_attr.attr, |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 418 | NULL |
| 419 | }; |
| 420 | |
| 421 | static void klp_kobj_release_patch(struct kobject *kobj) |
| 422 | { |
Josh Poimboeuf | 3ec2477 | 2017-03-06 11:20:29 -0600 | [diff] [blame] | 423 | struct klp_patch *patch; |
| 424 | |
| 425 | patch = container_of(kobj, struct klp_patch, kobj); |
| 426 | complete(&patch->finish); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static struct kobj_type klp_ktype_patch = { |
| 430 | .release = klp_kobj_release_patch, |
| 431 | .sysfs_ops = &kobj_sysfs_ops, |
| 432 | .default_attrs = klp_patch_attrs, |
| 433 | }; |
| 434 | |
Miroslav Benes | cad706d | 2015-05-19 12:01:18 +0200 | [diff] [blame] | 435 | static void klp_kobj_release_object(struct kobject *kobj) |
| 436 | { |
| 437 | } |
| 438 | |
| 439 | static struct kobj_type klp_ktype_object = { |
| 440 | .release = klp_kobj_release_object, |
| 441 | .sysfs_ops = &kobj_sysfs_ops, |
| 442 | }; |
| 443 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 444 | static void klp_kobj_release_func(struct kobject *kobj) |
| 445 | { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static struct kobj_type klp_ktype_func = { |
| 449 | .release = klp_kobj_release_func, |
| 450 | .sysfs_ops = &kobj_sysfs_ops, |
| 451 | }; |
| 452 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 453 | static void klp_free_funcs(struct klp_object *obj) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 454 | { |
| 455 | struct klp_func *func; |
| 456 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 457 | klp_for_each_func(obj, func) { |
| 458 | /* Might be called from klp_init_patch() error path. */ |
| 459 | if (func->kobj_added) |
| 460 | kobject_put(&func->kobj); |
| 461 | } |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Clean up when a patched object is unloaded */ |
| 465 | static void klp_free_object_loaded(struct klp_object *obj) |
| 466 | { |
| 467 | struct klp_func *func; |
| 468 | |
| 469 | obj->mod = NULL; |
| 470 | |
Jiri Slaby | 8cdd043 | 2015-05-19 12:01:19 +0200 | [diff] [blame] | 471 | klp_for_each_func(obj, func) |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 472 | func->old_func = NULL; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 473 | } |
| 474 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 475 | static void klp_free_objects(struct klp_patch *patch) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 476 | { |
| 477 | struct klp_object *obj; |
| 478 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 479 | klp_for_each_object(patch, obj) { |
| 480 | klp_free_funcs(obj); |
| 481 | |
| 482 | /* Might be called from klp_init_patch() error path. */ |
| 483 | if (obj->kobj_added) |
| 484 | kobject_put(&obj->kobj); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 488 | /* |
| 489 | * This function implements the free operations that can be called safely |
| 490 | * under klp_mutex. |
| 491 | * |
| 492 | * The operation must be completed by calling klp_free_patch_finish() |
| 493 | * outside klp_mutex. |
| 494 | */ |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 495 | void klp_free_patch_start(struct klp_patch *patch) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 496 | { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 497 | if (!list_empty(&patch->list)) |
| 498 | list_del(&patch->list); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 499 | |
| 500 | klp_free_objects(patch); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * This function implements the free part that must be called outside |
| 505 | * klp_mutex. |
| 506 | * |
| 507 | * It must be called after klp_free_patch_start(). And it has to be |
| 508 | * the last function accessing the livepatch structures when the patch |
| 509 | * gets disabled. |
| 510 | */ |
| 511 | static void klp_free_patch_finish(struct klp_patch *patch) |
| 512 | { |
| 513 | /* |
| 514 | * Avoid deadlock with enabled_store() sysfs callback by |
| 515 | * calling this outside klp_mutex. It is safe because |
| 516 | * this is called when the patch gets disabled and it |
| 517 | * cannot get enabled again. |
| 518 | */ |
| 519 | if (patch->kobj_added) { |
| 520 | kobject_put(&patch->kobj); |
| 521 | wait_for_completion(&patch->finish); |
| 522 | } |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 523 | |
| 524 | /* Put the module after the last access to struct klp_patch. */ |
| 525 | if (!patch->forced) |
| 526 | module_put(patch->mod); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * The livepatch might be freed from sysfs interface created by the patch. |
| 531 | * This work allows to wait until the interface is destroyed in a separate |
| 532 | * context. |
| 533 | */ |
| 534 | static void klp_free_patch_work_fn(struct work_struct *work) |
| 535 | { |
| 536 | struct klp_patch *patch = |
| 537 | container_of(work, struct klp_patch, free_work); |
| 538 | |
| 539 | klp_free_patch_finish(patch); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static int klp_init_func(struct klp_object *obj, struct klp_func *func) |
| 543 | { |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 544 | int ret; |
| 545 | |
Miroslav Benes | f09d908 | 2016-04-28 16:34:08 +0200 | [diff] [blame] | 546 | if (!func->old_name || !func->new_func) |
| 547 | return -EINVAL; |
| 548 | |
Kamalesh Babulal | 6e9df95 | 2018-07-20 15:16:42 +0530 | [diff] [blame] | 549 | if (strlen(func->old_name) >= KSYM_NAME_LEN) |
| 550 | return -EINVAL; |
| 551 | |
Josh Poimboeuf | 3c33f5b | 2015-01-20 09:26:19 -0600 | [diff] [blame] | 552 | INIT_LIST_HEAD(&func->stack_node); |
Josh Poimboeuf | 0dade9f | 2017-02-13 19:42:35 -0600 | [diff] [blame] | 553 | func->patched = false; |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 554 | func->transition = false; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 555 | |
Chris J Arges | 444f9e9 | 2015-12-01 20:40:56 -0600 | [diff] [blame] | 556 | /* The format for the sysfs directory is <function,sympos> where sympos |
| 557 | * is the nth occurrence of this symbol in kallsyms for the patched |
| 558 | * object. If the user selects 0 for old_sympos, then 1 will be used |
| 559 | * since a unique symbol will be the first occurrence. |
| 560 | */ |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 561 | ret = kobject_init_and_add(&func->kobj, &klp_ktype_func, |
| 562 | &obj->kobj, "%s,%lu", func->old_name, |
| 563 | func->old_sympos ? func->old_sympos : 1); |
| 564 | if (!ret) |
| 565 | func->kobj_added = true; |
| 566 | |
| 567 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 568 | } |
| 569 | |
Jessica Yu | 255e732 | 2016-08-17 20:58:28 -0400 | [diff] [blame] | 570 | /* Arches may override this to finish any remaining arch-specific tasks */ |
| 571 | void __weak arch_klp_init_object_loaded(struct klp_patch *patch, |
| 572 | struct klp_object *obj) |
| 573 | { |
| 574 | } |
| 575 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 576 | /* parts of the initialization that is done only when the object is loaded */ |
| 577 | static int klp_init_object_loaded(struct klp_patch *patch, |
| 578 | struct klp_object *obj) |
| 579 | { |
| 580 | struct klp_func *func; |
| 581 | int ret; |
| 582 | |
Jessica Yu | 255e732 | 2016-08-17 20:58:28 -0400 | [diff] [blame] | 583 | module_disable_ro(patch->mod); |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 584 | ret = klp_write_object_relocations(patch->mod, obj); |
Jessica Yu | 255e732 | 2016-08-17 20:58:28 -0400 | [diff] [blame] | 585 | if (ret) { |
| 586 | module_enable_ro(patch->mod, true); |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 587 | return ret; |
Jessica Yu | 255e732 | 2016-08-17 20:58:28 -0400 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | arch_klp_init_object_loaded(patch, obj); |
| 591 | module_enable_ro(patch->mod, true); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 592 | |
Jiri Slaby | 8cdd043 | 2015-05-19 12:01:19 +0200 | [diff] [blame] | 593 | klp_for_each_func(obj, func) { |
Chris J Arges | b2b018e | 2015-12-01 20:40:54 -0600 | [diff] [blame] | 594 | ret = klp_find_object_symbol(obj->name, func->old_name, |
| 595 | func->old_sympos, |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 596 | (unsigned long *)&func->old_func); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 597 | if (ret) |
| 598 | return ret; |
Josh Poimboeuf | f5e547f | 2017-02-13 19:42:39 -0600 | [diff] [blame] | 599 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 600 | ret = kallsyms_lookup_size_offset((unsigned long)func->old_func, |
Josh Poimboeuf | f5e547f | 2017-02-13 19:42:39 -0600 | [diff] [blame] | 601 | &func->old_size, NULL); |
| 602 | if (!ret) { |
| 603 | pr_err("kallsyms size lookup failed for '%s'\n", |
| 604 | func->old_name); |
| 605 | return -ENOENT; |
| 606 | } |
| 607 | |
| 608 | ret = kallsyms_lookup_size_offset((unsigned long)func->new_func, |
| 609 | &func->new_size, NULL); |
| 610 | if (!ret) { |
| 611 | pr_err("kallsyms size lookup failed for '%s' replacement\n", |
| 612 | func->old_name); |
| 613 | return -ENOENT; |
| 614 | } |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) |
| 621 | { |
| 622 | struct klp_func *func; |
| 623 | int ret; |
| 624 | const char *name; |
| 625 | |
Kamalesh Babulal | 6e9df95 | 2018-07-20 15:16:42 +0530 | [diff] [blame] | 626 | if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN) |
| 627 | return -EINVAL; |
| 628 | |
Josh Poimboeuf | 0dade9f | 2017-02-13 19:42:35 -0600 | [diff] [blame] | 629 | obj->patched = false; |
Petr Mladek | 8cb2c2d | 2015-03-12 12:55:13 +0100 | [diff] [blame] | 630 | obj->mod = NULL; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 631 | |
| 632 | klp_find_object_module(obj); |
| 633 | |
| 634 | name = klp_is_module(obj) ? obj->name : "vmlinux"; |
Miroslav Benes | cad706d | 2015-05-19 12:01:18 +0200 | [diff] [blame] | 635 | ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object, |
| 636 | &patch->kobj, "%s", name); |
| 637 | if (ret) |
| 638 | return ret; |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 639 | obj->kobj_added = true; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 640 | |
Jiri Slaby | 8cdd043 | 2015-05-19 12:01:19 +0200 | [diff] [blame] | 641 | klp_for_each_func(obj, func) { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 642 | ret = klp_init_func(obj, func); |
| 643 | if (ret) |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 644 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 645 | } |
| 646 | |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 647 | if (klp_is_object_loaded(obj)) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 648 | ret = klp_init_object_loaded(patch, obj); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 649 | |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | static int klp_init_patch_early(struct klp_patch *patch) |
| 654 | { |
| 655 | struct klp_object *obj; |
| 656 | struct klp_func *func; |
| 657 | |
| 658 | if (!patch->objs) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | INIT_LIST_HEAD(&patch->list); |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 662 | INIT_LIST_HEAD(&patch->obj_list); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 663 | patch->kobj_added = false; |
| 664 | patch->enabled = false; |
Petr Mladek | 6800728 | 2019-01-09 13:43:22 +0100 | [diff] [blame] | 665 | patch->forced = false; |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 666 | INIT_WORK(&patch->free_work, klp_free_patch_work_fn); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 667 | init_completion(&patch->finish); |
| 668 | |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 669 | klp_for_each_object_static(patch, obj) { |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 670 | if (!obj->funcs) |
| 671 | return -EINVAL; |
| 672 | |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 673 | INIT_LIST_HEAD(&obj->func_list); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 674 | obj->kobj_added = false; |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 675 | list_add_tail(&obj->node, &patch->obj_list); |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 676 | |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 677 | klp_for_each_func_static(obj, func) { |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 678 | func->kobj_added = false; |
Jason Baron | 20e5502 | 2019-01-09 13:43:24 +0100 | [diff] [blame^] | 679 | list_add_tail(&func->node, &obj->func_list); |
| 680 | } |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 681 | } |
| 682 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 683 | if (!try_module_get(patch->mod)) |
| 684 | return -ENODEV; |
| 685 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 686 | return 0; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int klp_init_patch(struct klp_patch *patch) |
| 690 | { |
| 691 | struct klp_object *obj; |
| 692 | int ret; |
| 693 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 694 | ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, |
Jiri Kosina | e0b561e | 2015-02-15 10:03:20 +0100 | [diff] [blame] | 695 | klp_root_kobj, "%s", patch->mod->name); |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 696 | if (ret) |
Josh Poimboeuf | 3ec2477 | 2017-03-06 11:20:29 -0600 | [diff] [blame] | 697 | return ret; |
Petr Mladek | 0430f78 | 2019-01-09 13:43:21 +0100 | [diff] [blame] | 698 | patch->kobj_added = true; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 699 | |
Jiri Slaby | 8cdd043 | 2015-05-19 12:01:19 +0200 | [diff] [blame] | 700 | klp_for_each_object(patch, obj) { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 701 | ret = klp_init_object(patch, obj); |
| 702 | if (ret) |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 703 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 704 | } |
| 705 | |
Josh Poimboeuf | 99590ba | 2015-01-09 14:03:04 -0600 | [diff] [blame] | 706 | list_add_tail(&patch->list, &klp_patches); |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 707 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 708 | return 0; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 709 | } |
| 710 | |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 711 | static int __klp_disable_patch(struct klp_patch *patch) |
| 712 | { |
| 713 | struct klp_object *obj; |
| 714 | |
| 715 | if (WARN_ON(!patch->enabled)) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | if (klp_transition_patch) |
| 719 | return -EBUSY; |
| 720 | |
| 721 | /* enforce stacking: only the last enabled patch can be disabled */ |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 722 | if (!list_is_last(&patch->list, &klp_patches)) |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 723 | return -EBUSY; |
| 724 | |
| 725 | klp_init_transition(patch, KLP_UNPATCHED); |
| 726 | |
| 727 | klp_for_each_object(patch, obj) |
| 728 | if (obj->patched) |
| 729 | klp_pre_unpatch_callback(obj); |
| 730 | |
| 731 | /* |
| 732 | * Enforce the order of the func->transition writes in |
| 733 | * klp_init_transition() and the TIF_PATCH_PENDING writes in |
| 734 | * klp_start_transition(). In the rare case where klp_ftrace_handler() |
| 735 | * is called shortly after klp_update_patch_state() switches the task, |
| 736 | * this ensures the handler sees that func->transition is set. |
| 737 | */ |
| 738 | smp_wmb(); |
| 739 | |
| 740 | klp_start_transition(); |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 741 | patch->enabled = false; |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 742 | klp_try_complete_transition(); |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 747 | static int __klp_enable_patch(struct klp_patch *patch) |
| 748 | { |
| 749 | struct klp_object *obj; |
| 750 | int ret; |
| 751 | |
| 752 | if (klp_transition_patch) |
| 753 | return -EBUSY; |
| 754 | |
| 755 | if (WARN_ON(patch->enabled)) |
| 756 | return -EINVAL; |
| 757 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 758 | if (!patch->kobj_added) |
| 759 | return -EINVAL; |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 760 | |
| 761 | pr_notice("enabling patch '%s'\n", patch->mod->name); |
| 762 | |
| 763 | klp_init_transition(patch, KLP_PATCHED); |
| 764 | |
| 765 | /* |
| 766 | * Enforce the order of the func->transition writes in |
| 767 | * klp_init_transition() and the ops->func_stack writes in |
| 768 | * klp_patch_object(), so that klp_ftrace_handler() will see the |
| 769 | * func->transition updates before the handler is registered and the |
| 770 | * new funcs become visible to the handler. |
| 771 | */ |
| 772 | smp_wmb(); |
| 773 | |
| 774 | klp_for_each_object(patch, obj) { |
| 775 | if (!klp_is_object_loaded(obj)) |
| 776 | continue; |
| 777 | |
| 778 | ret = klp_pre_patch_callback(obj); |
| 779 | if (ret) { |
| 780 | pr_warn("pre-patch callback failed for object '%s'\n", |
| 781 | klp_is_module(obj) ? obj->name : "vmlinux"); |
| 782 | goto err; |
| 783 | } |
| 784 | |
| 785 | ret = klp_patch_object(obj); |
| 786 | if (ret) { |
| 787 | pr_warn("failed to patch object '%s'\n", |
| 788 | klp_is_module(obj) ? obj->name : "vmlinux"); |
| 789 | goto err; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | klp_start_transition(); |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 794 | patch->enabled = true; |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 795 | klp_try_complete_transition(); |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 796 | |
| 797 | return 0; |
| 798 | err: |
| 799 | pr_warn("failed to enable patch '%s'\n", patch->mod->name); |
| 800 | |
| 801 | klp_cancel_transition(); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | /** |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 806 | * klp_enable_patch() - enable the livepatch |
| 807 | * @patch: patch to be enabled |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 808 | * |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 809 | * Initializes the data structure associated with the patch, creates the sysfs |
| 810 | * interface, performs the needed symbol lookups and code relocations, |
| 811 | * registers the patched functions with ftrace. |
| 812 | * |
| 813 | * This function is supposed to be called from the livepatch module_init() |
| 814 | * callback. |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 815 | * |
| 816 | * Return: 0 on success, otherwise error |
| 817 | */ |
| 818 | int klp_enable_patch(struct klp_patch *patch) |
| 819 | { |
| 820 | int ret; |
| 821 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 822 | if (!patch || !patch->mod) |
| 823 | return -EINVAL; |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 824 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 825 | if (!is_livepatch_module(patch->mod)) { |
| 826 | pr_err("module %s is not marked as a livepatch module\n", |
| 827 | patch->mod->name); |
| 828 | return -EINVAL; |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 831 | if (!klp_initialized()) |
| 832 | return -ENODEV; |
| 833 | |
| 834 | if (!klp_have_reliable_stack()) { |
| 835 | pr_err("This architecture doesn't have support for the livepatch consistency model.\n"); |
| 836 | return -ENOSYS; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | mutex_lock(&klp_mutex); |
| 841 | |
| 842 | ret = klp_init_patch_early(patch); |
| 843 | if (ret) { |
| 844 | mutex_unlock(&klp_mutex); |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | ret = klp_init_patch(patch); |
| 849 | if (ret) |
| 850 | goto err; |
| 851 | |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 852 | ret = __klp_enable_patch(patch); |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 853 | if (ret) |
| 854 | goto err; |
| 855 | |
| 856 | mutex_unlock(&klp_mutex); |
| 857 | |
| 858 | return 0; |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 859 | |
| 860 | err: |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 861 | klp_free_patch_start(patch); |
| 862 | |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 863 | mutex_unlock(&klp_mutex); |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 864 | |
| 865 | klp_free_patch_finish(patch); |
| 866 | |
Petr Mladek | 26c3e98e | 2019-01-09 13:43:20 +0100 | [diff] [blame] | 867 | return ret; |
| 868 | } |
| 869 | EXPORT_SYMBOL_GPL(klp_enable_patch); |
| 870 | |
Joe Lawrence | ef8daf8 | 2017-10-02 11:56:48 -0400 | [diff] [blame] | 871 | /* |
| 872 | * Remove parts of patches that touch a given kernel module. The list of |
| 873 | * patches processed might be limited. When limit is NULL, all patches |
| 874 | * will be handled. |
| 875 | */ |
| 876 | static void klp_cleanup_module_patches_limited(struct module *mod, |
| 877 | struct klp_patch *limit) |
| 878 | { |
| 879 | struct klp_patch *patch; |
| 880 | struct klp_object *obj; |
| 881 | |
| 882 | list_for_each_entry(patch, &klp_patches, list) { |
| 883 | if (patch == limit) |
| 884 | break; |
| 885 | |
| 886 | klp_for_each_object(patch, obj) { |
| 887 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) |
| 888 | continue; |
| 889 | |
| 890 | /* |
| 891 | * Only unpatch the module if the patch is enabled or |
| 892 | * is in transition. |
| 893 | */ |
| 894 | if (patch->enabled || patch == klp_transition_patch) { |
Jiri Kosina | fc41efc18 | 2017-11-15 10:53:24 +0100 | [diff] [blame] | 895 | |
| 896 | if (patch != klp_transition_patch) |
| 897 | klp_pre_unpatch_callback(obj); |
| 898 | |
Joe Lawrence | ef8daf8 | 2017-10-02 11:56:48 -0400 | [diff] [blame] | 899 | pr_notice("reverting patch '%s' on unloading module '%s'\n", |
| 900 | patch->mod->name, obj->mod->name); |
| 901 | klp_unpatch_object(obj); |
Jiri Kosina | fc41efc18 | 2017-11-15 10:53:24 +0100 | [diff] [blame] | 902 | |
| 903 | klp_post_unpatch_callback(obj); |
Joe Lawrence | ef8daf8 | 2017-10-02 11:56:48 -0400 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | klp_free_object_loaded(obj); |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 912 | int klp_module_coming(struct module *mod) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 913 | { |
Minfei Huang | 36e505c | 2015-05-15 10:22:48 +0800 | [diff] [blame] | 914 | int ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 915 | struct klp_patch *patch; |
| 916 | struct klp_object *obj; |
| 917 | |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 918 | if (WARN_ON(mod->state != MODULE_STATE_COMING)) |
| 919 | return -EINVAL; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 920 | |
| 921 | mutex_lock(&klp_mutex); |
Petr Mladek | 8cb2c2d | 2015-03-12 12:55:13 +0100 | [diff] [blame] | 922 | /* |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 923 | * Each module has to know that klp_module_coming() |
| 924 | * has been called. We never know what module will |
| 925 | * get patched by a new patch. |
Petr Mladek | 8cb2c2d | 2015-03-12 12:55:13 +0100 | [diff] [blame] | 926 | */ |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 927 | mod->klp_alive = true; |
Petr Mladek | 8cb2c2d | 2015-03-12 12:55:13 +0100 | [diff] [blame] | 928 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 929 | list_for_each_entry(patch, &klp_patches, list) { |
Jiri Slaby | 8cdd043 | 2015-05-19 12:01:19 +0200 | [diff] [blame] | 930 | klp_for_each_object(patch, obj) { |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 931 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) |
| 932 | continue; |
| 933 | |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 934 | obj->mod = mod; |
| 935 | |
| 936 | ret = klp_init_object_loaded(patch, obj); |
| 937 | if (ret) { |
| 938 | pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n", |
| 939 | patch->mod->name, obj->mod->name, ret); |
| 940 | goto err; |
| 941 | } |
| 942 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 943 | /* |
| 944 | * Only patch the module if the patch is enabled or is |
| 945 | * in transition. |
| 946 | */ |
| 947 | if (!patch->enabled && patch != klp_transition_patch) |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 948 | break; |
| 949 | |
| 950 | pr_notice("applying patch '%s' to loading module '%s'\n", |
| 951 | patch->mod->name, obj->mod->name); |
| 952 | |
Joe Lawrence | 93862e3 | 2017-10-13 15:08:41 -0400 | [diff] [blame] | 953 | ret = klp_pre_patch_callback(obj); |
| 954 | if (ret) { |
| 955 | pr_warn("pre-patch callback failed for object '%s'\n", |
| 956 | obj->name); |
| 957 | goto err; |
| 958 | } |
| 959 | |
Josh Poimboeuf | 0dade9f | 2017-02-13 19:42:35 -0600 | [diff] [blame] | 960 | ret = klp_patch_object(obj); |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 961 | if (ret) { |
| 962 | pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", |
| 963 | patch->mod->name, obj->mod->name, ret); |
Joe Lawrence | 93862e3 | 2017-10-13 15:08:41 -0400 | [diff] [blame] | 964 | |
Petr Mladek | 5aaf1ab | 2017-10-20 16:56:50 +0200 | [diff] [blame] | 965 | klp_post_unpatch_callback(obj); |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 966 | goto err; |
| 967 | } |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 968 | |
Joe Lawrence | 93862e3 | 2017-10-13 15:08:41 -0400 | [diff] [blame] | 969 | if (patch != klp_transition_patch) |
| 970 | klp_post_patch_callback(obj); |
| 971 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 972 | break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | mutex_unlock(&klp_mutex); |
| 977 | |
| 978 | return 0; |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 979 | |
| 980 | err: |
| 981 | /* |
| 982 | * If a patch is unsuccessfully applied, return |
| 983 | * error to the module loader. |
| 984 | */ |
| 985 | pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n", |
| 986 | patch->mod->name, obj->mod->name, obj->mod->name); |
| 987 | mod->klp_alive = false; |
Joe Lawrence | ef8daf8 | 2017-10-02 11:56:48 -0400 | [diff] [blame] | 988 | klp_cleanup_module_patches_limited(mod, patch); |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 989 | mutex_unlock(&klp_mutex); |
| 990 | |
| 991 | return ret; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 992 | } |
| 993 | |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 994 | void klp_module_going(struct module *mod) |
| 995 | { |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 996 | if (WARN_ON(mod->state != MODULE_STATE_GOING && |
| 997 | mod->state != MODULE_STATE_COMING)) |
| 998 | return; |
| 999 | |
| 1000 | mutex_lock(&klp_mutex); |
| 1001 | /* |
| 1002 | * Each module has to know that klp_module_going() |
| 1003 | * has been called. We never know what module will |
| 1004 | * get patched by a new patch. |
| 1005 | */ |
| 1006 | mod->klp_alive = false; |
| 1007 | |
Joe Lawrence | ef8daf8 | 2017-10-02 11:56:48 -0400 | [diff] [blame] | 1008 | klp_cleanup_module_patches_limited(mod, NULL); |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 1009 | |
| 1010 | mutex_unlock(&klp_mutex); |
| 1011 | } |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 1012 | |
Minfei Huang | 26029d8 | 2015-05-22 22:26:29 +0800 | [diff] [blame] | 1013 | static int __init klp_init(void) |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 1014 | { |
| 1015 | int ret; |
| 1016 | |
Jiri Kosina | b9dfe0b | 2015-01-09 10:53:21 +0100 | [diff] [blame] | 1017 | ret = klp_check_compiler_support(); |
| 1018 | if (ret) { |
| 1019 | pr_info("Your compiler is too old; turning off.\n"); |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 1023 | klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj); |
Jessica Yu | 7e545d6 | 2016-03-16 20:55:39 -0400 | [diff] [blame] | 1024 | if (!klp_root_kobj) |
| 1025 | return -ENOMEM; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 1026 | |
| 1027 | return 0; |
Seth Jennings | b700e7f | 2014-12-16 11:58:19 -0600 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | module_init(klp_init); |