blob: 34a8338657d2d8854c369641574a32331c453b75 [file] [log] [blame]
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001/*
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 Jenningsb700e7f2014-12-16 11:58:19 -060027#include <linux/list.h>
28#include <linux/kallsyms.h>
29#include <linux/livepatch.h>
Jessica Yu425595a2016-03-22 20:03:18 -040030#include <linux/elf.h>
31#include <linux/moduleloader.h>
Josh Poimboeuf3ec24772017-03-06 11:20:29 -060032#include <linux/completion.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060033#include <asm/cacheflush.h>
Jiri Kosina10517422017-03-08 14:27:05 +010034#include "core.h"
Josh Poimboeufc349cdca2017-02-13 19:42:37 -060035#include "patch.h"
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060036#include "transition.h"
Seth Jenningsb700e7f2014-12-16 11:58:19 -060037
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060038/*
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060039 * 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 Poimboeuf3c33f5b2015-01-20 09:26:19 -060045 */
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060046DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060047
Petr Mladek958ef1e2019-01-09 13:43:23 +010048/*
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 Mladek68007282019-01-09 13:43:22 +010053LIST_HEAD(klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060054
55static struct kobject *klp_root_kobj;
56
57static bool klp_is_module(struct klp_object *obj)
58{
59 return obj->name;
60}
61
Seth Jenningsb700e7f2014-12-16 11:58:19 -060062/* sets obj->mod if object is not vmlinux and module is found */
63static void klp_find_object_module(struct klp_object *obj)
64{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010065 struct module *mod;
66
Seth Jenningsb700e7f2014-12-16 11:58:19 -060067 if (!klp_is_module(obj))
68 return;
69
70 mutex_lock(&module_mutex);
71 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010072 * 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 Yu7e545d62016-03-16 20:55:39 -040074 * klp_module_going() instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -060075 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010076 mod = find_module(obj->name);
77 /*
Jessica Yu7e545d62016-03-16 20:55:39 -040078 * 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 Mladek8cb2c2d2015-03-12 12:55:13 +010080 * 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 Jenningsb700e7f2014-12-16 11:58:19 -060087 mutex_unlock(&module_mutex);
88}
89
Seth Jenningsb700e7f2014-12-16 11:58:19 -060090static bool klp_initialized(void)
91{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +020092 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -060093}
94
Jason Barone1452b62019-01-09 13:43:25 +010095static struct klp_func *klp_find_func(struct klp_object *obj,
96 struct klp_func *old_func)
97{
98 struct klp_func *func;
99
100 klp_for_each_func(obj, func) {
101 if ((strcmp(old_func->old_name, func->old_name) == 0) &&
102 (old_func->old_sympos == func->old_sympos)) {
103 return func;
104 }
105 }
106
107 return NULL;
108}
109
110static struct klp_object *klp_find_object(struct klp_patch *patch,
111 struct klp_object *old_obj)
112{
113 struct klp_object *obj;
114
115 klp_for_each_object(patch, obj) {
116 if (klp_is_module(old_obj)) {
117 if (klp_is_module(obj) &&
118 strcmp(old_obj->name, obj->name) == 0) {
119 return obj;
120 }
121 } else if (!klp_is_module(obj)) {
122 return obj;
123 }
124 }
125
126 return NULL;
127}
128
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600129struct klp_find_arg {
130 const char *objname;
131 const char *name;
132 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600133 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600134 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600135};
136
137static int klp_find_callback(void *data, const char *name,
138 struct module *mod, unsigned long addr)
139{
140 struct klp_find_arg *args = data;
141
142 if ((mod && !args->objname) || (!mod && args->objname))
143 return 0;
144
145 if (strcmp(args->name, name))
146 return 0;
147
148 if (args->objname && strcmp(args->objname, mod->name))
149 return 0;
150
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600151 args->addr = addr;
152 args->count++;
153
Chris J Argesb2b018e2015-12-01 20:40:54 -0600154 /*
155 * Finish the search when the symbol is found for the desired position
156 * or the position is not defined for a non-unique symbol.
157 */
158 if ((args->pos && (args->count == args->pos)) ||
159 (!args->pos && (args->count > 1)))
160 return 1;
161
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600162 return 0;
163}
164
165static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600166 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600167{
168 struct klp_find_arg args = {
169 .objname = objname,
170 .name = name,
171 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600172 .count = 0,
173 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600174 };
175
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200176 mutex_lock(&module_mutex);
Zhou Chengming72f04b52017-03-28 21:10:35 +0800177 if (objname)
178 module_kallsyms_on_each_symbol(klp_find_callback, &args);
179 else
180 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200181 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600182
Chris J Argesb2b018e2015-12-01 20:40:54 -0600183 /*
184 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
185 * otherwise ensure the symbol position count matches sympos.
186 */
187 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600188 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600189 else if (args.count > 1 && sympos == 0) {
Petr Mladekf995b5f2016-03-09 15:20:59 +0100190 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
191 name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600192 } else if (sympos != args.count && sympos > 0) {
193 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
194 sympos, name, objname ? objname : "vmlinux");
195 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600196 *addr = args.addr;
197 return 0;
198 }
199
200 *addr = 0;
201 return -EINVAL;
202}
203
Jessica Yu425595a2016-03-22 20:03:18 -0400204static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600205{
Jessica Yu425595a2016-03-22 20:03:18 -0400206 int i, cnt, vmlinux, ret;
207 char objname[MODULE_NAME_LEN];
208 char symname[KSYM_NAME_LEN];
209 char *strtab = pmod->core_kallsyms.strtab;
210 Elf_Rela *relas;
211 Elf_Sym *sym;
212 unsigned long sympos, addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600213
Chris J Argesb2b018e2015-12-01 20:40:54 -0600214 /*
Jessica Yu425595a2016-03-22 20:03:18 -0400215 * Since the field widths for objname and symname in the sscanf()
216 * call are hard-coded and correspond to MODULE_NAME_LEN and
217 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
218 * and KSYM_NAME_LEN have the values we expect them to have.
219 *
220 * Because the value of MODULE_NAME_LEN can differ among architectures,
221 * we use the smallest/strictest upper bound possible (56, based on
222 * the current definition of MODULE_NAME_LEN) to prevent overflows.
Chris J Argesb2b018e2015-12-01 20:40:54 -0600223 */
Jessica Yu425595a2016-03-22 20:03:18 -0400224 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
225
226 relas = (Elf_Rela *) relasec->sh_addr;
227 /* For each rela in this klp relocation section */
228 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
229 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
230 if (sym->st_shndx != SHN_LIVEPATCH) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500231 pr_err("symbol %s is not marked as a livepatch symbol\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400232 strtab + sym->st_name);
233 return -EINVAL;
234 }
235
236 /* Format: .klp.sym.objname.symname,sympos */
237 cnt = sscanf(strtab + sym->st_name,
238 ".klp.sym.%55[^.].%127[^,],%lu",
239 objname, symname, &sympos);
240 if (cnt != 3) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500241 pr_err("symbol %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400242 strtab + sym->st_name);
243 return -EINVAL;
244 }
245
246 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
247 vmlinux = !strcmp(objname, "vmlinux");
248 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
249 symname, sympos, &addr);
250 if (ret)
251 return ret;
252
253 sym->st_value = addr;
254 }
255
256 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600257}
258
259static int klp_write_object_relocations(struct module *pmod,
260 struct klp_object *obj)
261{
Jessica Yu425595a2016-03-22 20:03:18 -0400262 int i, cnt, ret = 0;
263 const char *objname, *secname;
264 char sec_objname[MODULE_NAME_LEN];
265 Elf_Shdr *sec;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600266
267 if (WARN_ON(!klp_is_object_loaded(obj)))
268 return -EINVAL;
269
Jessica Yu425595a2016-03-22 20:03:18 -0400270 objname = klp_is_module(obj) ? obj->name : "vmlinux";
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600271
Jessica Yu425595a2016-03-22 20:03:18 -0400272 /* For each klp relocation section */
273 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
274 sec = pmod->klp_info->sechdrs + i;
275 secname = pmod->klp_info->secstrings + sec->sh_name;
276 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
277 continue;
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600278
Jessica Yu425595a2016-03-22 20:03:18 -0400279 /*
280 * Format: .klp.rela.sec_objname.section_name
281 * See comment in klp_resolve_symbols() for an explanation
282 * of the selected field width value.
283 */
284 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
285 if (cnt != 1) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500286 pr_err("section %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400287 secname);
288 ret = -EINVAL;
289 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600290 }
Jessica Yu425595a2016-03-22 20:03:18 -0400291
292 if (strcmp(objname, sec_objname))
293 continue;
294
295 ret = klp_resolve_symbols(sec, pmod);
296 if (ret)
297 break;
298
299 ret = apply_relocate_add(pmod->klp_info->sechdrs,
300 pmod->core_kallsyms.strtab,
301 pmod->klp_info->symndx, i, pmod);
302 if (ret)
303 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600304 }
305
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600306 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600307}
308
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600309/*
310 * Sysfs Interface
311 *
312 * /sys/kernel/livepatch
313 * /sys/kernel/livepatch/<patch>
314 * /sys/kernel/livepatch/<patch>/enabled
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600315 * /sys/kernel/livepatch/<patch>/transition
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100316 * /sys/kernel/livepatch/<patch>/force
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600317 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600318 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600319 */
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100320static int __klp_disable_patch(struct klp_patch *patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600321
322static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
323 const char *buf, size_t count)
324{
325 struct klp_patch *patch;
326 int ret;
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600327 bool enabled;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600328
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600329 ret = kstrtobool(buf, &enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600330 if (ret)
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600331 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600332
333 patch = container_of(kobj, struct klp_patch, kobj);
334
335 mutex_lock(&klp_mutex);
336
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600337 if (patch->enabled == enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600338 /* already in requested state */
339 ret = -EINVAL;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100340 goto out;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600341 }
342
Petr Mladek958ef1e2019-01-09 13:43:23 +0100343 /*
344 * Allow to reverse a pending transition in both ways. It might be
345 * necessary to complete the transition without forcing and breaking
346 * the system integrity.
347 *
348 * Do not allow to re-enable a disabled patch.
349 */
350 if (patch == klp_transition_patch)
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600351 klp_reverse_transition();
Petr Mladek958ef1e2019-01-09 13:43:23 +0100352 else if (!enabled)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600353 ret = __klp_disable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100354 else
355 ret = -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600356
Petr Mladek958ef1e2019-01-09 13:43:23 +0100357out:
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600358 mutex_unlock(&klp_mutex);
359
Petr Mladek958ef1e2019-01-09 13:43:23 +0100360 if (ret)
361 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600362 return count;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600363}
364
365static ssize_t enabled_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf)
367{
368 struct klp_patch *patch;
369
370 patch = container_of(kobj, struct klp_patch, kobj);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600371 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600372}
373
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600374static ssize_t transition_show(struct kobject *kobj,
375 struct kobj_attribute *attr, char *buf)
376{
377 struct klp_patch *patch;
378
379 patch = container_of(kobj, struct klp_patch, kobj);
380 return snprintf(buf, PAGE_SIZE-1, "%d\n",
381 patch == klp_transition_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600382}
383
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100384static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct klp_patch *patch;
388 int ret;
389 bool val;
390
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100391 ret = kstrtobool(buf, &val);
392 if (ret)
393 return ret;
394
Miroslav Benes88690162017-12-21 14:40:43 +0100395 if (!val)
396 return count;
397
398 mutex_lock(&klp_mutex);
399
400 patch = container_of(kobj, struct klp_patch, kobj);
401 if (patch != klp_transition_patch) {
402 mutex_unlock(&klp_mutex);
403 return -EINVAL;
404 }
405
406 klp_force_transition();
407
408 mutex_unlock(&klp_mutex);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100409
410 return count;
411}
412
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600413static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600414static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100415static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600416static struct attribute *klp_patch_attrs[] = {
417 &enabled_kobj_attr.attr,
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600418 &transition_kobj_attr.attr,
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100419 &force_kobj_attr.attr,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600420 NULL
421};
Kimberly Brown70283452019-04-01 22:51:58 -0400422ATTRIBUTE_GROUPS(klp_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600423
Jason Barone1452b62019-01-09 13:43:25 +0100424static void klp_free_object_dynamic(struct klp_object *obj)
425{
426 kfree(obj->name);
427 kfree(obj);
428}
429
430static struct klp_object *klp_alloc_object_dynamic(const char *name)
431{
432 struct klp_object *obj;
433
434 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
435 if (!obj)
436 return NULL;
437
438 if (name) {
439 obj->name = kstrdup(name, GFP_KERNEL);
440 if (!obj->name) {
441 kfree(obj);
442 return NULL;
443 }
444 }
445
446 INIT_LIST_HEAD(&obj->func_list);
447 obj->dynamic = true;
448
449 return obj;
450}
451
452static void klp_free_func_nop(struct klp_func *func)
453{
454 kfree(func->old_name);
455 kfree(func);
456}
457
458static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
459 struct klp_object *obj)
460{
461 struct klp_func *func;
462
463 func = kzalloc(sizeof(*func), GFP_KERNEL);
464 if (!func)
465 return NULL;
466
467 if (old_func->old_name) {
468 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
469 if (!func->old_name) {
470 kfree(func);
471 return NULL;
472 }
473 }
474
475 /*
476 * func->new_func is same as func->old_func. These addresses are
477 * set when the object is loaded, see klp_init_object_loaded().
478 */
479 func->old_sympos = old_func->old_sympos;
480 func->nop = true;
481
482 return func;
483}
484
485static int klp_add_object_nops(struct klp_patch *patch,
486 struct klp_object *old_obj)
487{
488 struct klp_object *obj;
489 struct klp_func *func, *old_func;
490
491 obj = klp_find_object(patch, old_obj);
492
493 if (!obj) {
494 obj = klp_alloc_object_dynamic(old_obj->name);
495 if (!obj)
496 return -ENOMEM;
497
498 list_add_tail(&obj->node, &patch->obj_list);
499 }
500
501 klp_for_each_func(old_obj, old_func) {
502 func = klp_find_func(obj, old_func);
503 if (func)
504 continue;
505
506 func = klp_alloc_func_nop(old_func, obj);
507 if (!func)
508 return -ENOMEM;
509
510 list_add_tail(&func->node, &obj->func_list);
511 }
512
513 return 0;
514}
515
516/*
517 * Add 'nop' functions which simply return to the caller to run
518 * the original function. The 'nop' functions are added to a
519 * patch to facilitate a 'replace' mode.
520 */
521static int klp_add_nops(struct klp_patch *patch)
522{
523 struct klp_patch *old_patch;
524 struct klp_object *old_obj;
525
Petr Mladekecba29f2019-02-04 14:56:50 +0100526 klp_for_each_patch(old_patch) {
Jason Barone1452b62019-01-09 13:43:25 +0100527 klp_for_each_object(old_patch, old_obj) {
528 int err;
529
530 err = klp_add_object_nops(patch, old_obj);
531 if (err)
532 return err;
533 }
534 }
535
536 return 0;
537}
538
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600539static void klp_kobj_release_patch(struct kobject *kobj)
540{
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600541 struct klp_patch *patch;
542
543 patch = container_of(kobj, struct klp_patch, kobj);
544 complete(&patch->finish);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600545}
546
547static struct kobj_type klp_ktype_patch = {
548 .release = klp_kobj_release_patch,
549 .sysfs_ops = &kobj_sysfs_ops,
Kimberly Brown70283452019-04-01 22:51:58 -0400550 .default_groups = klp_patch_groups,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600551};
552
Miroslav Benescad706d2015-05-19 12:01:18 +0200553static void klp_kobj_release_object(struct kobject *kobj)
554{
Jason Barone1452b62019-01-09 13:43:25 +0100555 struct klp_object *obj;
556
557 obj = container_of(kobj, struct klp_object, kobj);
558
559 if (obj->dynamic)
560 klp_free_object_dynamic(obj);
Miroslav Benescad706d2015-05-19 12:01:18 +0200561}
562
563static struct kobj_type klp_ktype_object = {
564 .release = klp_kobj_release_object,
565 .sysfs_ops = &kobj_sysfs_ops,
566};
567
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600568static void klp_kobj_release_func(struct kobject *kobj)
569{
Jason Barone1452b62019-01-09 13:43:25 +0100570 struct klp_func *func;
571
572 func = container_of(kobj, struct klp_func, kobj);
573
574 if (func->nop)
575 klp_free_func_nop(func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600576}
577
578static struct kobj_type klp_ktype_func = {
579 .release = klp_kobj_release_func,
580 .sysfs_ops = &kobj_sysfs_ops,
581};
582
Petr Mladekd697bad2019-01-09 13:43:26 +0100583static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600584{
Jason Barone1452b62019-01-09 13:43:25 +0100585 struct klp_func *func, *tmp_func;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600586
Jason Barone1452b62019-01-09 13:43:25 +0100587 klp_for_each_func_safe(obj, func, tmp_func) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100588 if (nops_only && !func->nop)
589 continue;
590
591 list_del(&func->node);
592
Petr Mladek0430f782019-01-09 13:43:21 +0100593 /* Might be called from klp_init_patch() error path. */
Jason Barone1452b62019-01-09 13:43:25 +0100594 if (func->kobj_added) {
Petr Mladek0430f782019-01-09 13:43:21 +0100595 kobject_put(&func->kobj);
Jason Barone1452b62019-01-09 13:43:25 +0100596 } else if (func->nop) {
597 klp_free_func_nop(func);
598 }
Petr Mladek0430f782019-01-09 13:43:21 +0100599 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600600}
601
602/* Clean up when a patched object is unloaded */
603static void klp_free_object_loaded(struct klp_object *obj)
604{
605 struct klp_func *func;
606
607 obj->mod = NULL;
608
Jason Barone1452b62019-01-09 13:43:25 +0100609 klp_for_each_func(obj, func) {
Petr Mladek19514912019-01-09 13:43:19 +0100610 func->old_func = NULL;
Jason Barone1452b62019-01-09 13:43:25 +0100611
612 if (func->nop)
613 func->new_func = NULL;
614 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600615}
616
Petr Mladekd697bad2019-01-09 13:43:26 +0100617static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600618{
Jason Barone1452b62019-01-09 13:43:25 +0100619 struct klp_object *obj, *tmp_obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600620
Jason Barone1452b62019-01-09 13:43:25 +0100621 klp_for_each_object_safe(patch, obj, tmp_obj) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100622 __klp_free_funcs(obj, nops_only);
623
624 if (nops_only && !obj->dynamic)
625 continue;
626
627 list_del(&obj->node);
Petr Mladek0430f782019-01-09 13:43:21 +0100628
629 /* Might be called from klp_init_patch() error path. */
Jason Barone1452b62019-01-09 13:43:25 +0100630 if (obj->kobj_added) {
Petr Mladek0430f782019-01-09 13:43:21 +0100631 kobject_put(&obj->kobj);
Jason Barone1452b62019-01-09 13:43:25 +0100632 } else if (obj->dynamic) {
633 klp_free_object_dynamic(obj);
634 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600635 }
636}
637
Petr Mladekd697bad2019-01-09 13:43:26 +0100638static void klp_free_objects(struct klp_patch *patch)
639{
640 __klp_free_objects(patch, false);
641}
642
643static void klp_free_objects_dynamic(struct klp_patch *patch)
644{
645 __klp_free_objects(patch, true);
646}
647
Petr Mladek0430f782019-01-09 13:43:21 +0100648/*
649 * This function implements the free operations that can be called safely
650 * under klp_mutex.
651 *
652 * The operation must be completed by calling klp_free_patch_finish()
653 * outside klp_mutex.
654 */
Petr Mladek958ef1e2019-01-09 13:43:23 +0100655void klp_free_patch_start(struct klp_patch *patch)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600656{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600657 if (!list_empty(&patch->list))
658 list_del(&patch->list);
Petr Mladek0430f782019-01-09 13:43:21 +0100659
660 klp_free_objects(patch);
661}
662
663/*
664 * This function implements the free part that must be called outside
665 * klp_mutex.
666 *
667 * It must be called after klp_free_patch_start(). And it has to be
668 * the last function accessing the livepatch structures when the patch
669 * gets disabled.
670 */
671static void klp_free_patch_finish(struct klp_patch *patch)
672{
673 /*
674 * Avoid deadlock with enabled_store() sysfs callback by
675 * calling this outside klp_mutex. It is safe because
676 * this is called when the patch gets disabled and it
677 * cannot get enabled again.
678 */
679 if (patch->kobj_added) {
680 kobject_put(&patch->kobj);
681 wait_for_completion(&patch->finish);
682 }
Petr Mladek958ef1e2019-01-09 13:43:23 +0100683
684 /* Put the module after the last access to struct klp_patch. */
685 if (!patch->forced)
686 module_put(patch->mod);
687}
688
689/*
690 * The livepatch might be freed from sysfs interface created by the patch.
691 * This work allows to wait until the interface is destroyed in a separate
692 * context.
693 */
694static void klp_free_patch_work_fn(struct work_struct *work)
695{
696 struct klp_patch *patch =
697 container_of(work, struct klp_patch, free_work);
698
699 klp_free_patch_finish(patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600700}
701
702static int klp_init_func(struct klp_object *obj, struct klp_func *func)
703{
Petr Mladek0430f782019-01-09 13:43:21 +0100704 int ret;
705
Jason Barone1452b62019-01-09 13:43:25 +0100706 if (!func->old_name)
707 return -EINVAL;
708
709 /*
710 * NOPs get the address later. The patched module must be loaded,
711 * see klp_init_object_loaded().
712 */
713 if (!func->new_func && !func->nop)
Miroslav Benesf09d9082016-04-28 16:34:08 +0200714 return -EINVAL;
715
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530716 if (strlen(func->old_name) >= KSYM_NAME_LEN)
717 return -EINVAL;
718
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600719 INIT_LIST_HEAD(&func->stack_node);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600720 func->patched = false;
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600721 func->transition = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600722
Chris J Arges444f9e92015-12-01 20:40:56 -0600723 /* The format for the sysfs directory is <function,sympos> where sympos
724 * is the nth occurrence of this symbol in kallsyms for the patched
725 * object. If the user selects 0 for old_sympos, then 1 will be used
726 * since a unique symbol will be the first occurrence.
727 */
Petr Mladek0430f782019-01-09 13:43:21 +0100728 ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
729 &obj->kobj, "%s,%lu", func->old_name,
730 func->old_sympos ? func->old_sympos : 1);
731 if (!ret)
732 func->kobj_added = true;
733
734 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600735}
736
Jessica Yu255e7322016-08-17 20:58:28 -0400737/* Arches may override this to finish any remaining arch-specific tasks */
738void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
739 struct klp_object *obj)
740{
741}
742
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600743/* parts of the initialization that is done only when the object is loaded */
744static int klp_init_object_loaded(struct klp_patch *patch,
745 struct klp_object *obj)
746{
747 struct klp_func *func;
748 int ret;
749
Jessica Yu255e7322016-08-17 20:58:28 -0400750 module_disable_ro(patch->mod);
Jessica Yu425595a2016-03-22 20:03:18 -0400751 ret = klp_write_object_relocations(patch->mod, obj);
Jessica Yu255e7322016-08-17 20:58:28 -0400752 if (ret) {
753 module_enable_ro(patch->mod, true);
Jessica Yu425595a2016-03-22 20:03:18 -0400754 return ret;
Jessica Yu255e7322016-08-17 20:58:28 -0400755 }
756
757 arch_klp_init_object_loaded(patch, obj);
758 module_enable_ro(patch->mod, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600759
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200760 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600761 ret = klp_find_object_symbol(obj->name, func->old_name,
762 func->old_sympos,
Petr Mladek19514912019-01-09 13:43:19 +0100763 (unsigned long *)&func->old_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600764 if (ret)
765 return ret;
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600766
Petr Mladek19514912019-01-09 13:43:19 +0100767 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600768 &func->old_size, NULL);
769 if (!ret) {
770 pr_err("kallsyms size lookup failed for '%s'\n",
771 func->old_name);
772 return -ENOENT;
773 }
774
Jason Barone1452b62019-01-09 13:43:25 +0100775 if (func->nop)
776 func->new_func = func->old_func;
777
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600778 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
779 &func->new_size, NULL);
780 if (!ret) {
781 pr_err("kallsyms size lookup failed for '%s' replacement\n",
782 func->old_name);
783 return -ENOENT;
784 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600785 }
786
787 return 0;
788}
789
790static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
791{
792 struct klp_func *func;
793 int ret;
794 const char *name;
795
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530796 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
797 return -EINVAL;
798
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600799 obj->patched = false;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100800 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600801
802 klp_find_object_module(obj);
803
804 name = klp_is_module(obj) ? obj->name : "vmlinux";
Miroslav Benescad706d2015-05-19 12:01:18 +0200805 ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
806 &patch->kobj, "%s", name);
807 if (ret)
808 return ret;
Petr Mladek0430f782019-01-09 13:43:21 +0100809 obj->kobj_added = true;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600810
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200811 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600812 ret = klp_init_func(obj, func);
813 if (ret)
Petr Mladek0430f782019-01-09 13:43:21 +0100814 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600815 }
816
Petr Mladek0430f782019-01-09 13:43:21 +0100817 if (klp_is_object_loaded(obj))
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600818 ret = klp_init_object_loaded(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100819
820 return ret;
821}
822
823static int klp_init_patch_early(struct klp_patch *patch)
824{
825 struct klp_object *obj;
826 struct klp_func *func;
827
828 if (!patch->objs)
829 return -EINVAL;
830
831 INIT_LIST_HEAD(&patch->list);
Jason Baron20e55022019-01-09 13:43:24 +0100832 INIT_LIST_HEAD(&patch->obj_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100833 patch->kobj_added = false;
834 patch->enabled = false;
Petr Mladek68007282019-01-09 13:43:22 +0100835 patch->forced = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100836 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
Petr Mladek0430f782019-01-09 13:43:21 +0100837 init_completion(&patch->finish);
838
Jason Baron20e55022019-01-09 13:43:24 +0100839 klp_for_each_object_static(patch, obj) {
Petr Mladek0430f782019-01-09 13:43:21 +0100840 if (!obj->funcs)
841 return -EINVAL;
842
Jason Baron20e55022019-01-09 13:43:24 +0100843 INIT_LIST_HEAD(&obj->func_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100844 obj->kobj_added = false;
Jason Baron20e55022019-01-09 13:43:24 +0100845 list_add_tail(&obj->node, &patch->obj_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100846
Jason Baron20e55022019-01-09 13:43:24 +0100847 klp_for_each_func_static(obj, func) {
Petr Mladek0430f782019-01-09 13:43:21 +0100848 func->kobj_added = false;
Jason Baron20e55022019-01-09 13:43:24 +0100849 list_add_tail(&func->node, &obj->func_list);
850 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600851 }
852
Petr Mladek958ef1e2019-01-09 13:43:23 +0100853 if (!try_module_get(patch->mod))
854 return -ENODEV;
855
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600856 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600857}
858
859static int klp_init_patch(struct klp_patch *patch)
860{
861 struct klp_object *obj;
862 int ret;
863
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600864 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
Jiri Kosinae0b561e2015-02-15 10:03:20 +0100865 klp_root_kobj, "%s", patch->mod->name);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100866 if (ret)
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600867 return ret;
Petr Mladek0430f782019-01-09 13:43:21 +0100868 patch->kobj_added = true;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600869
Jason Barone1452b62019-01-09 13:43:25 +0100870 if (patch->replace) {
871 ret = klp_add_nops(patch);
872 if (ret)
873 return ret;
874 }
875
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200876 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600877 ret = klp_init_object(patch, obj);
878 if (ret)
Petr Mladek958ef1e2019-01-09 13:43:23 +0100879 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600880 }
881
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600882 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600883
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600884 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600885}
886
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100887static int __klp_disable_patch(struct klp_patch *patch)
888{
889 struct klp_object *obj;
890
891 if (WARN_ON(!patch->enabled))
892 return -EINVAL;
893
894 if (klp_transition_patch)
895 return -EBUSY;
896
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100897 klp_init_transition(patch, KLP_UNPATCHED);
898
899 klp_for_each_object(patch, obj)
900 if (obj->patched)
901 klp_pre_unpatch_callback(obj);
902
903 /*
904 * Enforce the order of the func->transition writes in
905 * klp_init_transition() and the TIF_PATCH_PENDING writes in
906 * klp_start_transition(). In the rare case where klp_ftrace_handler()
907 * is called shortly after klp_update_patch_state() switches the task,
908 * this ensures the handler sees that func->transition is set.
909 */
910 smp_wmb();
911
912 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100913 patch->enabled = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100914 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100915
916 return 0;
917}
918
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100919static int __klp_enable_patch(struct klp_patch *patch)
920{
921 struct klp_object *obj;
922 int ret;
923
924 if (klp_transition_patch)
925 return -EBUSY;
926
927 if (WARN_ON(patch->enabled))
928 return -EINVAL;
929
Petr Mladek958ef1e2019-01-09 13:43:23 +0100930 if (!patch->kobj_added)
931 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100932
933 pr_notice("enabling patch '%s'\n", patch->mod->name);
934
935 klp_init_transition(patch, KLP_PATCHED);
936
937 /*
938 * Enforce the order of the func->transition writes in
939 * klp_init_transition() and the ops->func_stack writes in
940 * klp_patch_object(), so that klp_ftrace_handler() will see the
941 * func->transition updates before the handler is registered and the
942 * new funcs become visible to the handler.
943 */
944 smp_wmb();
945
946 klp_for_each_object(patch, obj) {
947 if (!klp_is_object_loaded(obj))
948 continue;
949
950 ret = klp_pre_patch_callback(obj);
951 if (ret) {
952 pr_warn("pre-patch callback failed for object '%s'\n",
953 klp_is_module(obj) ? obj->name : "vmlinux");
954 goto err;
955 }
956
957 ret = klp_patch_object(obj);
958 if (ret) {
959 pr_warn("failed to patch object '%s'\n",
960 klp_is_module(obj) ? obj->name : "vmlinux");
961 goto err;
962 }
963 }
964
965 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100966 patch->enabled = true;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100967 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100968
969 return 0;
970err:
971 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
972
973 klp_cancel_transition();
974 return ret;
975}
976
977/**
Petr Mladek958ef1e2019-01-09 13:43:23 +0100978 * klp_enable_patch() - enable the livepatch
979 * @patch: patch to be enabled
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100980 *
Petr Mladek958ef1e2019-01-09 13:43:23 +0100981 * Initializes the data structure associated with the patch, creates the sysfs
982 * interface, performs the needed symbol lookups and code relocations,
983 * registers the patched functions with ftrace.
984 *
985 * This function is supposed to be called from the livepatch module_init()
986 * callback.
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100987 *
988 * Return: 0 on success, otherwise error
989 */
990int klp_enable_patch(struct klp_patch *patch)
991{
992 int ret;
993
Petr Mladek958ef1e2019-01-09 13:43:23 +0100994 if (!patch || !patch->mod)
995 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100996
Petr Mladek958ef1e2019-01-09 13:43:23 +0100997 if (!is_livepatch_module(patch->mod)) {
998 pr_err("module %s is not marked as a livepatch module\n",
999 patch->mod->name);
1000 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001001 }
1002
Petr Mladek958ef1e2019-01-09 13:43:23 +01001003 if (!klp_initialized())
1004 return -ENODEV;
1005
1006 if (!klp_have_reliable_stack()) {
1007 pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
Alice Ferrazzi375bfca2019-02-05 03:33:28 +09001008 return -EOPNOTSUPP;
Petr Mladek958ef1e2019-01-09 13:43:23 +01001009 }
1010
1011
1012 mutex_lock(&klp_mutex);
1013
1014 ret = klp_init_patch_early(patch);
1015 if (ret) {
1016 mutex_unlock(&klp_mutex);
1017 return ret;
1018 }
1019
1020 ret = klp_init_patch(patch);
1021 if (ret)
1022 goto err;
1023
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001024 ret = __klp_enable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001025 if (ret)
1026 goto err;
1027
1028 mutex_unlock(&klp_mutex);
1029
1030 return 0;
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001031
1032err:
Petr Mladek958ef1e2019-01-09 13:43:23 +01001033 klp_free_patch_start(patch);
1034
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001035 mutex_unlock(&klp_mutex);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001036
1037 klp_free_patch_finish(patch);
1038
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001039 return ret;
1040}
1041EXPORT_SYMBOL_GPL(klp_enable_patch);
1042
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001043/*
Jason Barone1452b62019-01-09 13:43:25 +01001044 * This function removes replaced patches.
1045 *
1046 * We could be pretty aggressive here. It is called in the situation where
1047 * these structures are no longer accessible. All functions are redirected
1048 * by the klp_transition_patch. They use either a new code or they are in
1049 * the original code because of the special nop function patches.
1050 *
1051 * The only exception is when the transition was forced. In this case,
1052 * klp_ftrace_handler() might still see the replaced patch on the stack.
1053 * Fortunately, it is carefully designed to work with removed functions
1054 * thanks to RCU. We only have to keep the patches on the system. Also
1055 * this is handled transparently by patch->module_put.
1056 */
1057void klp_discard_replaced_patches(struct klp_patch *new_patch)
1058{
1059 struct klp_patch *old_patch, *tmp_patch;
1060
Petr Mladekecba29f2019-02-04 14:56:50 +01001061 klp_for_each_patch_safe(old_patch, tmp_patch) {
Jason Barone1452b62019-01-09 13:43:25 +01001062 if (old_patch == new_patch)
1063 return;
1064
1065 old_patch->enabled = false;
1066 klp_unpatch_objects(old_patch);
1067 klp_free_patch_start(old_patch);
1068 schedule_work(&old_patch->free_work);
1069 }
1070}
1071
1072/*
Petr Mladekd697bad2019-01-09 13:43:26 +01001073 * This function removes the dynamically allocated 'nop' functions.
1074 *
1075 * We could be pretty aggressive. NOPs do not change the existing
1076 * behavior except for adding unnecessary delay by the ftrace handler.
1077 *
1078 * It is safe even when the transition was forced. The ftrace handler
1079 * will see a valid ops->func_stack entry thanks to RCU.
1080 *
1081 * We could even free the NOPs structures. They must be the last entry
1082 * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1083 * It does the same as klp_synchronize_transition() to make sure that
1084 * nobody is inside the ftrace handler once the operation finishes.
1085 *
1086 * IMPORTANT: It must be called right after removing the replaced patches!
1087 */
1088void klp_discard_nops(struct klp_patch *new_patch)
1089{
1090 klp_unpatch_objects_dynamic(klp_transition_patch);
1091 klp_free_objects_dynamic(klp_transition_patch);
1092}
1093
1094/*
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001095 * Remove parts of patches that touch a given kernel module. The list of
1096 * patches processed might be limited. When limit is NULL, all patches
1097 * will be handled.
1098 */
1099static void klp_cleanup_module_patches_limited(struct module *mod,
1100 struct klp_patch *limit)
1101{
1102 struct klp_patch *patch;
1103 struct klp_object *obj;
1104
Petr Mladekecba29f2019-02-04 14:56:50 +01001105 klp_for_each_patch(patch) {
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001106 if (patch == limit)
1107 break;
1108
1109 klp_for_each_object(patch, obj) {
1110 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1111 continue;
1112
Petr Mladeka087cdd2019-02-04 14:56:53 +01001113 if (patch != klp_transition_patch)
1114 klp_pre_unpatch_callback(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001115
Petr Mladeka087cdd2019-02-04 14:56:53 +01001116 pr_notice("reverting patch '%s' on unloading module '%s'\n",
1117 patch->mod->name, obj->mod->name);
1118 klp_unpatch_object(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001119
Petr Mladeka087cdd2019-02-04 14:56:53 +01001120 klp_post_unpatch_callback(obj);
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001121
1122 klp_free_object_loaded(obj);
1123 break;
1124 }
1125 }
1126}
1127
Jessica Yu7e545d62016-03-16 20:55:39 -04001128int klp_module_coming(struct module *mod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001129{
Minfei Huang36e505c2015-05-15 10:22:48 +08001130 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001131 struct klp_patch *patch;
1132 struct klp_object *obj;
1133
Jessica Yu7e545d62016-03-16 20:55:39 -04001134 if (WARN_ON(mod->state != MODULE_STATE_COMING))
1135 return -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001136
1137 mutex_lock(&klp_mutex);
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001138 /*
Jessica Yu7e545d62016-03-16 20:55:39 -04001139 * Each module has to know that klp_module_coming()
1140 * has been called. We never know what module will
1141 * get patched by a new patch.
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001142 */
Jessica Yu7e545d62016-03-16 20:55:39 -04001143 mod->klp_alive = true;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001144
Petr Mladekecba29f2019-02-04 14:56:50 +01001145 klp_for_each_patch(patch) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +02001146 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001147 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1148 continue;
1149
Jessica Yu7e545d62016-03-16 20:55:39 -04001150 obj->mod = mod;
1151
1152 ret = klp_init_object_loaded(patch, obj);
1153 if (ret) {
1154 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
1155 patch->mod->name, obj->mod->name, ret);
1156 goto err;
1157 }
1158
Jessica Yu7e545d62016-03-16 20:55:39 -04001159 pr_notice("applying patch '%s' to loading module '%s'\n",
1160 patch->mod->name, obj->mod->name);
1161
Joe Lawrence93862e32017-10-13 15:08:41 -04001162 ret = klp_pre_patch_callback(obj);
1163 if (ret) {
1164 pr_warn("pre-patch callback failed for object '%s'\n",
1165 obj->name);
1166 goto err;
1167 }
1168
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -06001169 ret = klp_patch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001170 if (ret) {
1171 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
1172 patch->mod->name, obj->mod->name, ret);
Joe Lawrence93862e32017-10-13 15:08:41 -04001173
Petr Mladek5aaf1ab2017-10-20 16:56:50 +02001174 klp_post_unpatch_callback(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001175 goto err;
1176 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001177
Joe Lawrence93862e32017-10-13 15:08:41 -04001178 if (patch != klp_transition_patch)
1179 klp_post_patch_callback(obj);
1180
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001181 break;
1182 }
1183 }
1184
1185 mutex_unlock(&klp_mutex);
1186
1187 return 0;
Jessica Yu7e545d62016-03-16 20:55:39 -04001188
1189err:
1190 /*
1191 * If a patch is unsuccessfully applied, return
1192 * error to the module loader.
1193 */
1194 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1195 patch->mod->name, obj->mod->name, obj->mod->name);
1196 mod->klp_alive = false;
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001197 klp_cleanup_module_patches_limited(mod, patch);
Jessica Yu7e545d62016-03-16 20:55:39 -04001198 mutex_unlock(&klp_mutex);
1199
1200 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001201}
1202
Jessica Yu7e545d62016-03-16 20:55:39 -04001203void klp_module_going(struct module *mod)
1204{
Jessica Yu7e545d62016-03-16 20:55:39 -04001205 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
1206 mod->state != MODULE_STATE_COMING))
1207 return;
1208
1209 mutex_lock(&klp_mutex);
1210 /*
1211 * Each module has to know that klp_module_going()
1212 * has been called. We never know what module will
1213 * get patched by a new patch.
1214 */
1215 mod->klp_alive = false;
1216
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001217 klp_cleanup_module_patches_limited(mod, NULL);
Jessica Yu7e545d62016-03-16 20:55:39 -04001218
1219 mutex_unlock(&klp_mutex);
1220}
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001221
Minfei Huang26029d82015-05-22 22:26:29 +08001222static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001223{
1224 int ret;
1225
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +01001226 ret = klp_check_compiler_support();
1227 if (ret) {
1228 pr_info("Your compiler is too old; turning off.\n");
1229 return -EINVAL;
1230 }
1231
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001232 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001233 if (!klp_root_kobj)
1234 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001235
1236 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001237}
1238
1239module_init(klp_init);