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