blob: 2398832947c6afbf03d13f5f67357d2d67e44550 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Seth Jenningsb700e7f2014-12-16 11:58:19 -06002/*
3 * core.c - Kernel Live Patching Core
4 *
5 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
6 * Copyright (C) 2014 SUSE
Seth Jenningsb700e7f2014-12-16 11:58:19 -06007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/slab.h>
Seth Jenningsb700e7f2014-12-16 11:58:19 -060015#include <linux/list.h>
16#include <linux/kallsyms.h>
17#include <linux/livepatch.h>
Jessica Yu425595a2016-03-22 20:03:18 -040018#include <linux/elf.h>
19#include <linux/moduleloader.h>
Josh Poimboeuf3ec24772017-03-06 11:20:29 -060020#include <linux/completion.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060021#include <asm/cacheflush.h>
Jiri Kosina10517422017-03-08 14:27:05 +010022#include "core.h"
Josh Poimboeufc349cdca2017-02-13 19:42:37 -060023#include "patch.h"
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060024#include "transition.h"
Seth Jenningsb700e7f2014-12-16 11:58:19 -060025
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060026/*
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060027 * klp_mutex is a coarse lock which serializes access to klp data. All
28 * accesses to klp-related variables and structures must have mutex protection,
29 * except within the following functions which carefully avoid the need for it:
30 *
31 * - klp_ftrace_handler()
32 * - klp_update_patch_state()
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060033 */
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060034DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060035
Petr Mladek958ef1e2019-01-09 13:43:23 +010036/*
37 * Actively used patches: enabled or in transition. Note that replaced
38 * or disabled patches are not listed even though the related kernel
39 * module still can be loaded.
40 */
Petr Mladek68007282019-01-09 13:43:22 +010041LIST_HEAD(klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060042
43static struct kobject *klp_root_kobj;
44
45static bool klp_is_module(struct klp_object *obj)
46{
47 return obj->name;
48}
49
Seth Jenningsb700e7f2014-12-16 11:58:19 -060050/* sets obj->mod if object is not vmlinux and module is found */
51static void klp_find_object_module(struct klp_object *obj)
52{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010053 struct module *mod;
54
Seth Jenningsb700e7f2014-12-16 11:58:19 -060055 if (!klp_is_module(obj))
56 return;
57
58 mutex_lock(&module_mutex);
59 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010060 * We do not want to block removal of patched modules and therefore
61 * we do not take a reference here. The patches are removed by
Jessica Yu7e545d62016-03-16 20:55:39 -040062 * klp_module_going() instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -060063 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010064 mod = find_module(obj->name);
65 /*
Jessica Yu7e545d62016-03-16 20:55:39 -040066 * Do not mess work of klp_module_coming() and klp_module_going().
67 * Note that the patch might still be needed before klp_module_going()
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010068 * is called. Module functions can be called even in the GOING state
69 * until mod->exit() finishes. This is especially important for
70 * patches that modify semantic of the functions.
71 */
72 if (mod && mod->klp_alive)
73 obj->mod = mod;
74
Seth Jenningsb700e7f2014-12-16 11:58:19 -060075 mutex_unlock(&module_mutex);
76}
77
Seth Jenningsb700e7f2014-12-16 11:58:19 -060078static bool klp_initialized(void)
79{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +020080 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -060081}
82
Jason Barone1452b62019-01-09 13:43:25 +010083static struct klp_func *klp_find_func(struct klp_object *obj,
84 struct klp_func *old_func)
85{
86 struct klp_func *func;
87
88 klp_for_each_func(obj, func) {
89 if ((strcmp(old_func->old_name, func->old_name) == 0) &&
90 (old_func->old_sympos == func->old_sympos)) {
91 return func;
92 }
93 }
94
95 return NULL;
96}
97
98static struct klp_object *klp_find_object(struct klp_patch *patch,
99 struct klp_object *old_obj)
100{
101 struct klp_object *obj;
102
103 klp_for_each_object(patch, obj) {
104 if (klp_is_module(old_obj)) {
105 if (klp_is_module(obj) &&
106 strcmp(old_obj->name, obj->name) == 0) {
107 return obj;
108 }
109 } else if (!klp_is_module(obj)) {
110 return obj;
111 }
112 }
113
114 return NULL;
115}
116
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600117struct klp_find_arg {
118 const char *objname;
119 const char *name;
120 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600121 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600122 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600123};
124
125static int klp_find_callback(void *data, const char *name,
126 struct module *mod, unsigned long addr)
127{
128 struct klp_find_arg *args = data;
129
130 if ((mod && !args->objname) || (!mod && args->objname))
131 return 0;
132
133 if (strcmp(args->name, name))
134 return 0;
135
136 if (args->objname && strcmp(args->objname, mod->name))
137 return 0;
138
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600139 args->addr = addr;
140 args->count++;
141
Chris J Argesb2b018e2015-12-01 20:40:54 -0600142 /*
143 * Finish the search when the symbol is found for the desired position
144 * or the position is not defined for a non-unique symbol.
145 */
146 if ((args->pos && (args->count == args->pos)) ||
147 (!args->pos && (args->count > 1)))
148 return 1;
149
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600150 return 0;
151}
152
153static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600154 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600155{
156 struct klp_find_arg args = {
157 .objname = objname,
158 .name = name,
159 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600160 .count = 0,
161 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600162 };
163
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200164 mutex_lock(&module_mutex);
Zhou Chengming72f04b52017-03-28 21:10:35 +0800165 if (objname)
166 module_kallsyms_on_each_symbol(klp_find_callback, &args);
167 else
168 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200169 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600170
Chris J Argesb2b018e2015-12-01 20:40:54 -0600171 /*
172 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
173 * otherwise ensure the symbol position count matches sympos.
174 */
175 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600176 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600177 else if (args.count > 1 && sympos == 0) {
Petr Mladekf995b5f2016-03-09 15:20:59 +0100178 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
179 name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600180 } else if (sympos != args.count && sympos > 0) {
181 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
182 sympos, name, objname ? objname : "vmlinux");
183 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600184 *addr = args.addr;
185 return 0;
186 }
187
188 *addr = 0;
189 return -EINVAL;
190}
191
Jessica Yu425595a2016-03-22 20:03:18 -0400192static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600193{
Jessica Yu425595a2016-03-22 20:03:18 -0400194 int i, cnt, vmlinux, ret;
195 char objname[MODULE_NAME_LEN];
196 char symname[KSYM_NAME_LEN];
197 char *strtab = pmod->core_kallsyms.strtab;
198 Elf_Rela *relas;
199 Elf_Sym *sym;
200 unsigned long sympos, addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600201
Chris J Argesb2b018e2015-12-01 20:40:54 -0600202 /*
Jessica Yu425595a2016-03-22 20:03:18 -0400203 * Since the field widths for objname and symname in the sscanf()
204 * call are hard-coded and correspond to MODULE_NAME_LEN and
205 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
206 * and KSYM_NAME_LEN have the values we expect them to have.
207 *
208 * Because the value of MODULE_NAME_LEN can differ among architectures,
209 * we use the smallest/strictest upper bound possible (56, based on
210 * the current definition of MODULE_NAME_LEN) to prevent overflows.
Chris J Argesb2b018e2015-12-01 20:40:54 -0600211 */
Jessica Yu425595a2016-03-22 20:03:18 -0400212 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
213
214 relas = (Elf_Rela *) relasec->sh_addr;
215 /* For each rela in this klp relocation section */
216 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
217 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
218 if (sym->st_shndx != SHN_LIVEPATCH) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500219 pr_err("symbol %s is not marked as a livepatch symbol\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400220 strtab + sym->st_name);
221 return -EINVAL;
222 }
223
224 /* Format: .klp.sym.objname.symname,sympos */
225 cnt = sscanf(strtab + sym->st_name,
226 ".klp.sym.%55[^.].%127[^,],%lu",
227 objname, symname, &sympos);
228 if (cnt != 3) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500229 pr_err("symbol %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400230 strtab + sym->st_name);
231 return -EINVAL;
232 }
233
234 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
235 vmlinux = !strcmp(objname, "vmlinux");
236 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
237 symname, sympos, &addr);
238 if (ret)
239 return ret;
240
241 sym->st_value = addr;
242 }
243
244 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600245}
246
247static int klp_write_object_relocations(struct module *pmod,
248 struct klp_object *obj)
249{
Jessica Yu425595a2016-03-22 20:03:18 -0400250 int i, cnt, ret = 0;
251 const char *objname, *secname;
252 char sec_objname[MODULE_NAME_LEN];
253 Elf_Shdr *sec;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600254
255 if (WARN_ON(!klp_is_object_loaded(obj)))
256 return -EINVAL;
257
Jessica Yu425595a2016-03-22 20:03:18 -0400258 objname = klp_is_module(obj) ? obj->name : "vmlinux";
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600259
Jessica Yu425595a2016-03-22 20:03:18 -0400260 /* For each klp relocation section */
261 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
262 sec = pmod->klp_info->sechdrs + i;
263 secname = pmod->klp_info->secstrings + sec->sh_name;
264 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
265 continue;
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600266
Jessica Yu425595a2016-03-22 20:03:18 -0400267 /*
268 * Format: .klp.rela.sec_objname.section_name
269 * See comment in klp_resolve_symbols() for an explanation
270 * of the selected field width value.
271 */
272 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
273 if (cnt != 1) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500274 pr_err("section %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400275 secname);
276 ret = -EINVAL;
277 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600278 }
Jessica Yu425595a2016-03-22 20:03:18 -0400279
280 if (strcmp(objname, sec_objname))
281 continue;
282
283 ret = klp_resolve_symbols(sec, pmod);
284 if (ret)
285 break;
286
287 ret = apply_relocate_add(pmod->klp_info->sechdrs,
288 pmod->core_kallsyms.strtab,
289 pmod->klp_info->symndx, i, pmod);
290 if (ret)
291 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600292 }
293
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600294 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600295}
296
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600297/*
298 * Sysfs Interface
299 *
300 * /sys/kernel/livepatch
301 * /sys/kernel/livepatch/<patch>
302 * /sys/kernel/livepatch/<patch>/enabled
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600303 * /sys/kernel/livepatch/<patch>/transition
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100304 * /sys/kernel/livepatch/<patch>/force
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600305 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600306 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600307 */
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100308static int __klp_disable_patch(struct klp_patch *patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600309
310static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
311 const char *buf, size_t count)
312{
313 struct klp_patch *patch;
314 int ret;
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600315 bool enabled;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600316
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600317 ret = kstrtobool(buf, &enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600318 if (ret)
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600319 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600320
321 patch = container_of(kobj, struct klp_patch, kobj);
322
323 mutex_lock(&klp_mutex);
324
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600325 if (patch->enabled == enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600326 /* already in requested state */
327 ret = -EINVAL;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100328 goto out;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600329 }
330
Petr Mladek958ef1e2019-01-09 13:43:23 +0100331 /*
332 * Allow to reverse a pending transition in both ways. It might be
333 * necessary to complete the transition without forcing and breaking
334 * the system integrity.
335 *
336 * Do not allow to re-enable a disabled patch.
337 */
338 if (patch == klp_transition_patch)
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600339 klp_reverse_transition();
Petr Mladek958ef1e2019-01-09 13:43:23 +0100340 else if (!enabled)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600341 ret = __klp_disable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100342 else
343 ret = -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600344
Petr Mladek958ef1e2019-01-09 13:43:23 +0100345out:
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600346 mutex_unlock(&klp_mutex);
347
Petr Mladek958ef1e2019-01-09 13:43:23 +0100348 if (ret)
349 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600350 return count;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600351}
352
353static ssize_t enabled_show(struct kobject *kobj,
354 struct kobj_attribute *attr, char *buf)
355{
356 struct klp_patch *patch;
357
358 patch = container_of(kobj, struct klp_patch, kobj);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600359 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600360}
361
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600362static ssize_t transition_show(struct kobject *kobj,
363 struct kobj_attribute *attr, char *buf)
364{
365 struct klp_patch *patch;
366
367 patch = container_of(kobj, struct klp_patch, kobj);
368 return snprintf(buf, PAGE_SIZE-1, "%d\n",
369 patch == klp_transition_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600370}
371
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100372static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
373 const char *buf, size_t count)
374{
375 struct klp_patch *patch;
376 int ret;
377 bool val;
378
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100379 ret = kstrtobool(buf, &val);
380 if (ret)
381 return ret;
382
Miroslav Benes88690162017-12-21 14:40:43 +0100383 if (!val)
384 return count;
385
386 mutex_lock(&klp_mutex);
387
388 patch = container_of(kobj, struct klp_patch, kobj);
389 if (patch != klp_transition_patch) {
390 mutex_unlock(&klp_mutex);
391 return -EINVAL;
392 }
393
394 klp_force_transition();
395
396 mutex_unlock(&klp_mutex);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100397
398 return count;
399}
400
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600401static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600402static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100403static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600404static struct attribute *klp_patch_attrs[] = {
405 &enabled_kobj_attr.attr,
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600406 &transition_kobj_attr.attr,
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100407 &force_kobj_attr.attr,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600408 NULL
409};
Kimberly Brown70283452019-04-01 22:51:58 -0400410ATTRIBUTE_GROUPS(klp_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600411
Jason Barone1452b62019-01-09 13:43:25 +0100412static void klp_free_object_dynamic(struct klp_object *obj)
413{
414 kfree(obj->name);
415 kfree(obj);
416}
417
Petr Mladekf68d67c2019-05-03 15:26:25 +0200418static void klp_init_func_early(struct klp_object *obj,
419 struct klp_func *func);
420static void klp_init_object_early(struct klp_patch *patch,
421 struct klp_object *obj);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200422
Petr Mladekf68d67c2019-05-03 15:26:25 +0200423static struct klp_object *klp_alloc_object_dynamic(const char *name,
424 struct klp_patch *patch)
Jason Barone1452b62019-01-09 13:43:25 +0100425{
426 struct klp_object *obj;
427
428 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
429 if (!obj)
430 return NULL;
431
432 if (name) {
433 obj->name = kstrdup(name, GFP_KERNEL);
434 if (!obj->name) {
435 kfree(obj);
436 return NULL;
437 }
438 }
439
Petr Mladekf68d67c2019-05-03 15:26:25 +0200440 klp_init_object_early(patch, obj);
Jason Barone1452b62019-01-09 13:43:25 +0100441 obj->dynamic = true;
442
443 return obj;
444}
445
446static void klp_free_func_nop(struct klp_func *func)
447{
448 kfree(func->old_name);
449 kfree(func);
450}
451
452static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
453 struct klp_object *obj)
454{
455 struct klp_func *func;
456
457 func = kzalloc(sizeof(*func), GFP_KERNEL);
458 if (!func)
459 return NULL;
460
461 if (old_func->old_name) {
462 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
463 if (!func->old_name) {
464 kfree(func);
465 return NULL;
466 }
467 }
468
Petr Mladekf68d67c2019-05-03 15:26:25 +0200469 klp_init_func_early(obj, func);
Jason Barone1452b62019-01-09 13:43:25 +0100470 /*
471 * func->new_func is same as func->old_func. These addresses are
472 * set when the object is loaded, see klp_init_object_loaded().
473 */
474 func->old_sympos = old_func->old_sympos;
475 func->nop = true;
476
477 return func;
478}
479
480static int klp_add_object_nops(struct klp_patch *patch,
481 struct klp_object *old_obj)
482{
483 struct klp_object *obj;
484 struct klp_func *func, *old_func;
485
486 obj = klp_find_object(patch, old_obj);
487
488 if (!obj) {
Petr Mladekf68d67c2019-05-03 15:26:25 +0200489 obj = klp_alloc_object_dynamic(old_obj->name, patch);
Jason Barone1452b62019-01-09 13:43:25 +0100490 if (!obj)
491 return -ENOMEM;
Jason Barone1452b62019-01-09 13:43:25 +0100492 }
493
494 klp_for_each_func(old_obj, old_func) {
495 func = klp_find_func(obj, old_func);
496 if (func)
497 continue;
498
499 func = klp_alloc_func_nop(old_func, obj);
500 if (!func)
501 return -ENOMEM;
Jason Barone1452b62019-01-09 13:43:25 +0100502 }
503
504 return 0;
505}
506
507/*
508 * Add 'nop' functions which simply return to the caller to run
509 * the original function. The 'nop' functions are added to a
510 * patch to facilitate a 'replace' mode.
511 */
512static int klp_add_nops(struct klp_patch *patch)
513{
514 struct klp_patch *old_patch;
515 struct klp_object *old_obj;
516
Petr Mladekecba29f2019-02-04 14:56:50 +0100517 klp_for_each_patch(old_patch) {
Jason Barone1452b62019-01-09 13:43:25 +0100518 klp_for_each_object(old_patch, old_obj) {
519 int err;
520
521 err = klp_add_object_nops(patch, old_obj);
522 if (err)
523 return err;
524 }
525 }
526
527 return 0;
528}
529
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600530static void klp_kobj_release_patch(struct kobject *kobj)
531{
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600532 struct klp_patch *patch;
533
534 patch = container_of(kobj, struct klp_patch, kobj);
535 complete(&patch->finish);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600536}
537
538static struct kobj_type klp_ktype_patch = {
539 .release = klp_kobj_release_patch,
540 .sysfs_ops = &kobj_sysfs_ops,
Kimberly Brown70283452019-04-01 22:51:58 -0400541 .default_groups = klp_patch_groups,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600542};
543
Miroslav Benescad706d2015-05-19 12:01:18 +0200544static void klp_kobj_release_object(struct kobject *kobj)
545{
Jason Barone1452b62019-01-09 13:43:25 +0100546 struct klp_object *obj;
547
548 obj = container_of(kobj, struct klp_object, kobj);
549
550 if (obj->dynamic)
551 klp_free_object_dynamic(obj);
Miroslav Benescad706d2015-05-19 12:01:18 +0200552}
553
554static struct kobj_type klp_ktype_object = {
555 .release = klp_kobj_release_object,
556 .sysfs_ops = &kobj_sysfs_ops,
557};
558
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600559static void klp_kobj_release_func(struct kobject *kobj)
560{
Jason Barone1452b62019-01-09 13:43:25 +0100561 struct klp_func *func;
562
563 func = container_of(kobj, struct klp_func, kobj);
564
565 if (func->nop)
566 klp_free_func_nop(func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600567}
568
569static struct kobj_type klp_ktype_func = {
570 .release = klp_kobj_release_func,
571 .sysfs_ops = &kobj_sysfs_ops,
572};
573
Petr Mladekd697bad2019-01-09 13:43:26 +0100574static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600575{
Jason Barone1452b62019-01-09 13:43:25 +0100576 struct klp_func *func, *tmp_func;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600577
Jason Barone1452b62019-01-09 13:43:25 +0100578 klp_for_each_func_safe(obj, func, tmp_func) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100579 if (nops_only && !func->nop)
580 continue;
581
582 list_del(&func->node);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200583 kobject_put(&func->kobj);
Petr Mladek0430f782019-01-09 13:43:21 +0100584 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600585}
586
587/* Clean up when a patched object is unloaded */
588static void klp_free_object_loaded(struct klp_object *obj)
589{
590 struct klp_func *func;
591
592 obj->mod = NULL;
593
Jason Barone1452b62019-01-09 13:43:25 +0100594 klp_for_each_func(obj, func) {
Petr Mladek19514912019-01-09 13:43:19 +0100595 func->old_func = NULL;
Jason Barone1452b62019-01-09 13:43:25 +0100596
597 if (func->nop)
598 func->new_func = NULL;
599 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600600}
601
Petr Mladekd697bad2019-01-09 13:43:26 +0100602static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600603{
Jason Barone1452b62019-01-09 13:43:25 +0100604 struct klp_object *obj, *tmp_obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600605
Jason Barone1452b62019-01-09 13:43:25 +0100606 klp_for_each_object_safe(patch, obj, tmp_obj) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100607 __klp_free_funcs(obj, nops_only);
608
609 if (nops_only && !obj->dynamic)
610 continue;
611
612 list_del(&obj->node);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200613 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600614 }
615}
616
Petr Mladekd697bad2019-01-09 13:43:26 +0100617static void klp_free_objects(struct klp_patch *patch)
618{
619 __klp_free_objects(patch, false);
620}
621
622static void klp_free_objects_dynamic(struct klp_patch *patch)
623{
624 __klp_free_objects(patch, true);
625}
626
Petr Mladek0430f782019-01-09 13:43:21 +0100627/*
628 * This function implements the free operations that can be called safely
629 * under klp_mutex.
630 *
631 * The operation must be completed by calling klp_free_patch_finish()
632 * outside klp_mutex.
633 */
Petr Mladek958ef1e2019-01-09 13:43:23 +0100634void klp_free_patch_start(struct klp_patch *patch)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600635{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600636 if (!list_empty(&patch->list))
637 list_del(&patch->list);
Petr Mladek0430f782019-01-09 13:43:21 +0100638
639 klp_free_objects(patch);
640}
641
642/*
643 * This function implements the free part that must be called outside
644 * klp_mutex.
645 *
646 * It must be called after klp_free_patch_start(). And it has to be
647 * the last function accessing the livepatch structures when the patch
648 * gets disabled.
649 */
650static void klp_free_patch_finish(struct klp_patch *patch)
651{
652 /*
653 * Avoid deadlock with enabled_store() sysfs callback by
654 * calling this outside klp_mutex. It is safe because
655 * this is called when the patch gets disabled and it
656 * cannot get enabled again.
657 */
Petr Mladek4d141ab2019-05-03 15:26:24 +0200658 kobject_put(&patch->kobj);
659 wait_for_completion(&patch->finish);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100660
661 /* Put the module after the last access to struct klp_patch. */
662 if (!patch->forced)
663 module_put(patch->mod);
664}
665
666/*
667 * The livepatch might be freed from sysfs interface created by the patch.
668 * This work allows to wait until the interface is destroyed in a separate
669 * context.
670 */
671static void klp_free_patch_work_fn(struct work_struct *work)
672{
673 struct klp_patch *patch =
674 container_of(work, struct klp_patch, free_work);
675
676 klp_free_patch_finish(patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600677}
678
679static int klp_init_func(struct klp_object *obj, struct klp_func *func)
680{
Jason Barone1452b62019-01-09 13:43:25 +0100681 if (!func->old_name)
682 return -EINVAL;
683
684 /*
685 * NOPs get the address later. The patched module must be loaded,
686 * see klp_init_object_loaded().
687 */
688 if (!func->new_func && !func->nop)
Miroslav Benesf09d9082016-04-28 16:34:08 +0200689 return -EINVAL;
690
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530691 if (strlen(func->old_name) >= KSYM_NAME_LEN)
692 return -EINVAL;
693
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600694 INIT_LIST_HEAD(&func->stack_node);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600695 func->patched = false;
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600696 func->transition = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600697
Chris J Arges444f9e92015-12-01 20:40:56 -0600698 /* The format for the sysfs directory is <function,sympos> where sympos
699 * is the nth occurrence of this symbol in kallsyms for the patched
700 * object. If the user selects 0 for old_sympos, then 1 will be used
701 * since a unique symbol will be the first occurrence.
702 */
Petr Mladek4d141ab2019-05-03 15:26:24 +0200703 return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
704 func->old_name,
705 func->old_sympos ? func->old_sympos : 1);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600706}
707
Jessica Yu255e7322016-08-17 20:58:28 -0400708/* Arches may override this to finish any remaining arch-specific tasks */
709void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
710 struct klp_object *obj)
711{
712}
713
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600714/* parts of the initialization that is done only when the object is loaded */
715static int klp_init_object_loaded(struct klp_patch *patch,
716 struct klp_object *obj)
717{
718 struct klp_func *func;
719 int ret;
720
Jessica Yu255e7322016-08-17 20:58:28 -0400721 module_disable_ro(patch->mod);
Jessica Yu425595a2016-03-22 20:03:18 -0400722 ret = klp_write_object_relocations(patch->mod, obj);
Jessica Yu255e7322016-08-17 20:58:28 -0400723 if (ret) {
724 module_enable_ro(patch->mod, true);
Jessica Yu425595a2016-03-22 20:03:18 -0400725 return ret;
Jessica Yu255e7322016-08-17 20:58:28 -0400726 }
727
728 arch_klp_init_object_loaded(patch, obj);
729 module_enable_ro(patch->mod, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600730
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200731 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600732 ret = klp_find_object_symbol(obj->name, func->old_name,
733 func->old_sympos,
Petr Mladek19514912019-01-09 13:43:19 +0100734 (unsigned long *)&func->old_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600735 if (ret)
736 return ret;
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600737
Petr Mladek19514912019-01-09 13:43:19 +0100738 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600739 &func->old_size, NULL);
740 if (!ret) {
741 pr_err("kallsyms size lookup failed for '%s'\n",
742 func->old_name);
743 return -ENOENT;
744 }
745
Jason Barone1452b62019-01-09 13:43:25 +0100746 if (func->nop)
747 func->new_func = func->old_func;
748
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600749 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
750 &func->new_size, NULL);
751 if (!ret) {
752 pr_err("kallsyms size lookup failed for '%s' replacement\n",
753 func->old_name);
754 return -ENOENT;
755 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600756 }
757
758 return 0;
759}
760
761static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
762{
763 struct klp_func *func;
764 int ret;
765 const char *name;
766
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530767 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
768 return -EINVAL;
769
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600770 obj->patched = false;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100771 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600772
773 klp_find_object_module(obj);
774
775 name = klp_is_module(obj) ? obj->name : "vmlinux";
Petr Mladek4d141ab2019-05-03 15:26:24 +0200776 ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
Miroslav Benescad706d2015-05-19 12:01:18 +0200777 if (ret)
778 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600779
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200780 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600781 ret = klp_init_func(obj, func);
782 if (ret)
Petr Mladek0430f782019-01-09 13:43:21 +0100783 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600784 }
785
Petr Mladek0430f782019-01-09 13:43:21 +0100786 if (klp_is_object_loaded(obj))
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600787 ret = klp_init_object_loaded(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100788
789 return ret;
790}
791
Petr Mladekf68d67c2019-05-03 15:26:25 +0200792static void klp_init_func_early(struct klp_object *obj,
793 struct klp_func *func)
794{
795 kobject_init(&func->kobj, &klp_ktype_func);
796 list_add_tail(&func->node, &obj->func_list);
797}
798
799static void klp_init_object_early(struct klp_patch *patch,
800 struct klp_object *obj)
801{
802 INIT_LIST_HEAD(&obj->func_list);
803 kobject_init(&obj->kobj, &klp_ktype_object);
804 list_add_tail(&obj->node, &patch->obj_list);
805}
806
Petr Mladek0430f782019-01-09 13:43:21 +0100807static int klp_init_patch_early(struct klp_patch *patch)
808{
809 struct klp_object *obj;
810 struct klp_func *func;
811
812 if (!patch->objs)
813 return -EINVAL;
814
815 INIT_LIST_HEAD(&patch->list);
Jason Baron20e55022019-01-09 13:43:24 +0100816 INIT_LIST_HEAD(&patch->obj_list);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200817 kobject_init(&patch->kobj, &klp_ktype_patch);
Petr Mladek0430f782019-01-09 13:43:21 +0100818 patch->enabled = false;
Petr Mladek68007282019-01-09 13:43:22 +0100819 patch->forced = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100820 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
Petr Mladek0430f782019-01-09 13:43:21 +0100821 init_completion(&patch->finish);
822
Jason Baron20e55022019-01-09 13:43:24 +0100823 klp_for_each_object_static(patch, obj) {
Petr Mladek0430f782019-01-09 13:43:21 +0100824 if (!obj->funcs)
825 return -EINVAL;
826
Petr Mladekf68d67c2019-05-03 15:26:25 +0200827 klp_init_object_early(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100828
Jason Baron20e55022019-01-09 13:43:24 +0100829 klp_for_each_func_static(obj, func) {
Petr Mladekf68d67c2019-05-03 15:26:25 +0200830 klp_init_func_early(obj, func);
Jason Baron20e55022019-01-09 13:43:24 +0100831 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600832 }
833
Petr Mladek958ef1e2019-01-09 13:43:23 +0100834 if (!try_module_get(patch->mod))
835 return -ENODEV;
836
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600837 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600838}
839
840static int klp_init_patch(struct klp_patch *patch)
841{
842 struct klp_object *obj;
843 int ret;
844
Petr Mladek4d141ab2019-05-03 15:26:24 +0200845 ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100846 if (ret)
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600847 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600848
Jason Barone1452b62019-01-09 13:43:25 +0100849 if (patch->replace) {
850 ret = klp_add_nops(patch);
851 if (ret)
852 return ret;
853 }
854
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200855 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600856 ret = klp_init_object(patch, obj);
857 if (ret)
Petr Mladek958ef1e2019-01-09 13:43:23 +0100858 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600859 }
860
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600861 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600862
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600863 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600864}
865
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100866static int __klp_disable_patch(struct klp_patch *patch)
867{
868 struct klp_object *obj;
869
870 if (WARN_ON(!patch->enabled))
871 return -EINVAL;
872
873 if (klp_transition_patch)
874 return -EBUSY;
875
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100876 klp_init_transition(patch, KLP_UNPATCHED);
877
878 klp_for_each_object(patch, obj)
879 if (obj->patched)
880 klp_pre_unpatch_callback(obj);
881
882 /*
883 * Enforce the order of the func->transition writes in
884 * klp_init_transition() and the TIF_PATCH_PENDING writes in
885 * klp_start_transition(). In the rare case where klp_ftrace_handler()
886 * is called shortly after klp_update_patch_state() switches the task,
887 * this ensures the handler sees that func->transition is set.
888 */
889 smp_wmb();
890
891 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100892 patch->enabled = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100893 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100894
895 return 0;
896}
897
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100898static int __klp_enable_patch(struct klp_patch *patch)
899{
900 struct klp_object *obj;
901 int ret;
902
903 if (klp_transition_patch)
904 return -EBUSY;
905
906 if (WARN_ON(patch->enabled))
907 return -EINVAL;
908
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100909 pr_notice("enabling patch '%s'\n", patch->mod->name);
910
911 klp_init_transition(patch, KLP_PATCHED);
912
913 /*
914 * Enforce the order of the func->transition writes in
915 * klp_init_transition() and the ops->func_stack writes in
916 * klp_patch_object(), so that klp_ftrace_handler() will see the
917 * func->transition updates before the handler is registered and the
918 * new funcs become visible to the handler.
919 */
920 smp_wmb();
921
922 klp_for_each_object(patch, obj) {
923 if (!klp_is_object_loaded(obj))
924 continue;
925
926 ret = klp_pre_patch_callback(obj);
927 if (ret) {
928 pr_warn("pre-patch callback failed for object '%s'\n",
929 klp_is_module(obj) ? obj->name : "vmlinux");
930 goto err;
931 }
932
933 ret = klp_patch_object(obj);
934 if (ret) {
935 pr_warn("failed to patch object '%s'\n",
936 klp_is_module(obj) ? obj->name : "vmlinux");
937 goto err;
938 }
939 }
940
941 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100942 patch->enabled = true;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100943 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100944
945 return 0;
946err:
947 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
948
949 klp_cancel_transition();
950 return ret;
951}
952
953/**
Petr Mladek958ef1e2019-01-09 13:43:23 +0100954 * klp_enable_patch() - enable the livepatch
955 * @patch: patch to be enabled
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100956 *
Petr Mladek958ef1e2019-01-09 13:43:23 +0100957 * Initializes the data structure associated with the patch, creates the sysfs
958 * interface, performs the needed symbol lookups and code relocations,
959 * registers the patched functions with ftrace.
960 *
961 * This function is supposed to be called from the livepatch module_init()
962 * callback.
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100963 *
964 * Return: 0 on success, otherwise error
965 */
966int klp_enable_patch(struct klp_patch *patch)
967{
968 int ret;
969
Petr Mladek958ef1e2019-01-09 13:43:23 +0100970 if (!patch || !patch->mod)
971 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100972
Petr Mladek958ef1e2019-01-09 13:43:23 +0100973 if (!is_livepatch_module(patch->mod)) {
974 pr_err("module %s is not marked as a livepatch module\n",
975 patch->mod->name);
976 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100977 }
978
Petr Mladek958ef1e2019-01-09 13:43:23 +0100979 if (!klp_initialized())
980 return -ENODEV;
981
982 if (!klp_have_reliable_stack()) {
Petr Mladek31adf232019-04-24 10:55:48 +0200983 pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
984 pr_warn("The livepatch transition may never complete.\n");
Petr Mladek958ef1e2019-01-09 13:43:23 +0100985 }
986
Petr Mladek958ef1e2019-01-09 13:43:23 +0100987 mutex_lock(&klp_mutex);
988
989 ret = klp_init_patch_early(patch);
990 if (ret) {
991 mutex_unlock(&klp_mutex);
992 return ret;
993 }
994
995 ret = klp_init_patch(patch);
996 if (ret)
997 goto err;
998
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100999 ret = __klp_enable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001000 if (ret)
1001 goto err;
1002
1003 mutex_unlock(&klp_mutex);
1004
1005 return 0;
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001006
1007err:
Petr Mladek958ef1e2019-01-09 13:43:23 +01001008 klp_free_patch_start(patch);
1009
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001010 mutex_unlock(&klp_mutex);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001011
1012 klp_free_patch_finish(patch);
1013
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001014 return ret;
1015}
1016EXPORT_SYMBOL_GPL(klp_enable_patch);
1017
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001018/*
Jason Barone1452b62019-01-09 13:43:25 +01001019 * This function removes replaced patches.
1020 *
1021 * We could be pretty aggressive here. It is called in the situation where
1022 * these structures are no longer accessible. All functions are redirected
1023 * by the klp_transition_patch. They use either a new code or they are in
1024 * the original code because of the special nop function patches.
1025 *
1026 * The only exception is when the transition was forced. In this case,
1027 * klp_ftrace_handler() might still see the replaced patch on the stack.
1028 * Fortunately, it is carefully designed to work with removed functions
1029 * thanks to RCU. We only have to keep the patches on the system. Also
1030 * this is handled transparently by patch->module_put.
1031 */
1032void klp_discard_replaced_patches(struct klp_patch *new_patch)
1033{
1034 struct klp_patch *old_patch, *tmp_patch;
1035
Petr Mladekecba29f2019-02-04 14:56:50 +01001036 klp_for_each_patch_safe(old_patch, tmp_patch) {
Jason Barone1452b62019-01-09 13:43:25 +01001037 if (old_patch == new_patch)
1038 return;
1039
1040 old_patch->enabled = false;
1041 klp_unpatch_objects(old_patch);
1042 klp_free_patch_start(old_patch);
1043 schedule_work(&old_patch->free_work);
1044 }
1045}
1046
1047/*
Petr Mladekd697bad2019-01-09 13:43:26 +01001048 * This function removes the dynamically allocated 'nop' functions.
1049 *
1050 * We could be pretty aggressive. NOPs do not change the existing
1051 * behavior except for adding unnecessary delay by the ftrace handler.
1052 *
1053 * It is safe even when the transition was forced. The ftrace handler
1054 * will see a valid ops->func_stack entry thanks to RCU.
1055 *
1056 * We could even free the NOPs structures. They must be the last entry
1057 * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1058 * It does the same as klp_synchronize_transition() to make sure that
1059 * nobody is inside the ftrace handler once the operation finishes.
1060 *
1061 * IMPORTANT: It must be called right after removing the replaced patches!
1062 */
1063void klp_discard_nops(struct klp_patch *new_patch)
1064{
1065 klp_unpatch_objects_dynamic(klp_transition_patch);
1066 klp_free_objects_dynamic(klp_transition_patch);
1067}
1068
1069/*
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001070 * Remove parts of patches that touch a given kernel module. The list of
1071 * patches processed might be limited. When limit is NULL, all patches
1072 * will be handled.
1073 */
1074static void klp_cleanup_module_patches_limited(struct module *mod,
1075 struct klp_patch *limit)
1076{
1077 struct klp_patch *patch;
1078 struct klp_object *obj;
1079
Petr Mladekecba29f2019-02-04 14:56:50 +01001080 klp_for_each_patch(patch) {
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001081 if (patch == limit)
1082 break;
1083
1084 klp_for_each_object(patch, obj) {
1085 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1086 continue;
1087
Petr Mladeka087cdd2019-02-04 14:56:53 +01001088 if (patch != klp_transition_patch)
1089 klp_pre_unpatch_callback(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001090
Petr Mladeka087cdd2019-02-04 14:56:53 +01001091 pr_notice("reverting patch '%s' on unloading module '%s'\n",
1092 patch->mod->name, obj->mod->name);
1093 klp_unpatch_object(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001094
Petr Mladeka087cdd2019-02-04 14:56:53 +01001095 klp_post_unpatch_callback(obj);
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001096
1097 klp_free_object_loaded(obj);
1098 break;
1099 }
1100 }
1101}
1102
Jessica Yu7e545d62016-03-16 20:55:39 -04001103int klp_module_coming(struct module *mod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001104{
Minfei Huang36e505c2015-05-15 10:22:48 +08001105 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001106 struct klp_patch *patch;
1107 struct klp_object *obj;
1108
Jessica Yu7e545d62016-03-16 20:55:39 -04001109 if (WARN_ON(mod->state != MODULE_STATE_COMING))
1110 return -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001111
1112 mutex_lock(&klp_mutex);
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001113 /*
Jessica Yu7e545d62016-03-16 20:55:39 -04001114 * Each module has to know that klp_module_coming()
1115 * has been called. We never know what module will
1116 * get patched by a new patch.
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001117 */
Jessica Yu7e545d62016-03-16 20:55:39 -04001118 mod->klp_alive = true;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001119
Petr Mladekecba29f2019-02-04 14:56:50 +01001120 klp_for_each_patch(patch) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +02001121 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001122 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1123 continue;
1124
Jessica Yu7e545d62016-03-16 20:55:39 -04001125 obj->mod = mod;
1126
1127 ret = klp_init_object_loaded(patch, obj);
1128 if (ret) {
1129 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
1130 patch->mod->name, obj->mod->name, ret);
1131 goto err;
1132 }
1133
Jessica Yu7e545d62016-03-16 20:55:39 -04001134 pr_notice("applying patch '%s' to loading module '%s'\n",
1135 patch->mod->name, obj->mod->name);
1136
Joe Lawrence93862e32017-10-13 15:08:41 -04001137 ret = klp_pre_patch_callback(obj);
1138 if (ret) {
1139 pr_warn("pre-patch callback failed for object '%s'\n",
1140 obj->name);
1141 goto err;
1142 }
1143
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -06001144 ret = klp_patch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001145 if (ret) {
1146 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
1147 patch->mod->name, obj->mod->name, ret);
Joe Lawrence93862e32017-10-13 15:08:41 -04001148
Petr Mladek5aaf1ab2017-10-20 16:56:50 +02001149 klp_post_unpatch_callback(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001150 goto err;
1151 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001152
Joe Lawrence93862e32017-10-13 15:08:41 -04001153 if (patch != klp_transition_patch)
1154 klp_post_patch_callback(obj);
1155
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001156 break;
1157 }
1158 }
1159
1160 mutex_unlock(&klp_mutex);
1161
1162 return 0;
Jessica Yu7e545d62016-03-16 20:55:39 -04001163
1164err:
1165 /*
1166 * If a patch is unsuccessfully applied, return
1167 * error to the module loader.
1168 */
1169 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1170 patch->mod->name, obj->mod->name, obj->mod->name);
1171 mod->klp_alive = false;
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001172 klp_cleanup_module_patches_limited(mod, patch);
Jessica Yu7e545d62016-03-16 20:55:39 -04001173 mutex_unlock(&klp_mutex);
1174
1175 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001176}
1177
Jessica Yu7e545d62016-03-16 20:55:39 -04001178void klp_module_going(struct module *mod)
1179{
Jessica Yu7e545d62016-03-16 20:55:39 -04001180 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
1181 mod->state != MODULE_STATE_COMING))
1182 return;
1183
1184 mutex_lock(&klp_mutex);
1185 /*
1186 * Each module has to know that klp_module_going()
1187 * has been called. We never know what module will
1188 * get patched by a new patch.
1189 */
1190 mod->klp_alive = false;
1191
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001192 klp_cleanup_module_patches_limited(mod, NULL);
Jessica Yu7e545d62016-03-16 20:55:39 -04001193
1194 mutex_unlock(&klp_mutex);
1195}
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001196
Minfei Huang26029d82015-05-22 22:26:29 +08001197static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001198{
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001199 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001200 if (!klp_root_kobj)
1201 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001202
1203 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001204}
1205
1206module_init(klp_init);