blob: 2d17e6e364b592cab80a487fa4403a8b70896fd2 [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 Poimboeuf9f255b62019-06-13 20:07:22 -050033#include <linux/memory.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060034#include <asm/cacheflush.h>
Jiri Kosina10517422017-03-08 14:27:05 +010035#include "core.h"
Josh Poimboeufc349cdca2017-02-13 19:42:37 -060036#include "patch.h"
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060037#include "transition.h"
Seth Jenningsb700e7f2014-12-16 11:58:19 -060038
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060039/*
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060040 * klp_mutex is a coarse lock which serializes access to klp data. All
41 * accesses to klp-related variables and structures must have mutex protection,
42 * except within the following functions which carefully avoid the need for it:
43 *
44 * - klp_ftrace_handler()
45 * - klp_update_patch_state()
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060046 */
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060047DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060048
Petr Mladek958ef1e2019-01-09 13:43:23 +010049/*
50 * Actively used patches: enabled or in transition. Note that replaced
51 * or disabled patches are not listed even though the related kernel
52 * module still can be loaded.
53 */
Petr Mladek68007282019-01-09 13:43:22 +010054LIST_HEAD(klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060055
56static struct kobject *klp_root_kobj;
57
58static bool klp_is_module(struct klp_object *obj)
59{
60 return obj->name;
61}
62
Seth Jenningsb700e7f2014-12-16 11:58:19 -060063/* sets obj->mod if object is not vmlinux and module is found */
64static void klp_find_object_module(struct klp_object *obj)
65{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010066 struct module *mod;
67
Seth Jenningsb700e7f2014-12-16 11:58:19 -060068 if (!klp_is_module(obj))
69 return;
70
71 mutex_lock(&module_mutex);
72 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010073 * We do not want to block removal of patched modules and therefore
74 * we do not take a reference here. The patches are removed by
Jessica Yu7e545d62016-03-16 20:55:39 -040075 * klp_module_going() instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -060076 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010077 mod = find_module(obj->name);
78 /*
Jessica Yu7e545d62016-03-16 20:55:39 -040079 * Do not mess work of klp_module_coming() and klp_module_going().
80 * Note that the patch might still be needed before klp_module_going()
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010081 * is called. Module functions can be called even in the GOING state
82 * until mod->exit() finishes. This is especially important for
83 * patches that modify semantic of the functions.
84 */
85 if (mod && mod->klp_alive)
86 obj->mod = mod;
87
Seth Jenningsb700e7f2014-12-16 11:58:19 -060088 mutex_unlock(&module_mutex);
89}
90
Seth Jenningsb700e7f2014-12-16 11:58:19 -060091static bool klp_initialized(void)
92{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +020093 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -060094}
95
Jason Barone1452b62019-01-09 13:43:25 +010096static struct klp_func *klp_find_func(struct klp_object *obj,
97 struct klp_func *old_func)
98{
99 struct klp_func *func;
100
101 klp_for_each_func(obj, func) {
102 if ((strcmp(old_func->old_name, func->old_name) == 0) &&
103 (old_func->old_sympos == func->old_sympos)) {
104 return func;
105 }
106 }
107
108 return NULL;
109}
110
111static struct klp_object *klp_find_object(struct klp_patch *patch,
112 struct klp_object *old_obj)
113{
114 struct klp_object *obj;
115
116 klp_for_each_object(patch, obj) {
117 if (klp_is_module(old_obj)) {
118 if (klp_is_module(obj) &&
119 strcmp(old_obj->name, obj->name) == 0) {
120 return obj;
121 }
122 } else if (!klp_is_module(obj)) {
123 return obj;
124 }
125 }
126
127 return NULL;
128}
129
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600130struct klp_find_arg {
131 const char *objname;
132 const char *name;
133 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600134 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600135 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600136};
137
138static int klp_find_callback(void *data, const char *name,
139 struct module *mod, unsigned long addr)
140{
141 struct klp_find_arg *args = data;
142
143 if ((mod && !args->objname) || (!mod && args->objname))
144 return 0;
145
146 if (strcmp(args->name, name))
147 return 0;
148
149 if (args->objname && strcmp(args->objname, mod->name))
150 return 0;
151
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600152 args->addr = addr;
153 args->count++;
154
Chris J Argesb2b018e2015-12-01 20:40:54 -0600155 /*
156 * Finish the search when the symbol is found for the desired position
157 * or the position is not defined for a non-unique symbol.
158 */
159 if ((args->pos && (args->count == args->pos)) ||
160 (!args->pos && (args->count > 1)))
161 return 1;
162
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600163 return 0;
164}
165
166static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600167 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600168{
169 struct klp_find_arg args = {
170 .objname = objname,
171 .name = name,
172 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600173 .count = 0,
174 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600175 };
176
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200177 mutex_lock(&module_mutex);
Zhou Chengming72f04b52017-03-28 21:10:35 +0800178 if (objname)
179 module_kallsyms_on_each_symbol(klp_find_callback, &args);
180 else
181 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200182 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600183
Chris J Argesb2b018e2015-12-01 20:40:54 -0600184 /*
185 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
186 * otherwise ensure the symbol position count matches sympos.
187 */
188 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600189 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600190 else if (args.count > 1 && sympos == 0) {
Petr Mladekf995b5f2016-03-09 15:20:59 +0100191 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
192 name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600193 } else if (sympos != args.count && sympos > 0) {
194 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
195 sympos, name, objname ? objname : "vmlinux");
196 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600197 *addr = args.addr;
198 return 0;
199 }
200
201 *addr = 0;
202 return -EINVAL;
203}
204
Jessica Yu425595a2016-03-22 20:03:18 -0400205static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600206{
Jessica Yu425595a2016-03-22 20:03:18 -0400207 int i, cnt, vmlinux, ret;
208 char objname[MODULE_NAME_LEN];
209 char symname[KSYM_NAME_LEN];
210 char *strtab = pmod->core_kallsyms.strtab;
211 Elf_Rela *relas;
212 Elf_Sym *sym;
213 unsigned long sympos, addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600214
Chris J Argesb2b018e2015-12-01 20:40:54 -0600215 /*
Jessica Yu425595a2016-03-22 20:03:18 -0400216 * Since the field widths for objname and symname in the sscanf()
217 * call are hard-coded and correspond to MODULE_NAME_LEN and
218 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
219 * and KSYM_NAME_LEN have the values we expect them to have.
220 *
221 * Because the value of MODULE_NAME_LEN can differ among architectures,
222 * we use the smallest/strictest upper bound possible (56, based on
223 * the current definition of MODULE_NAME_LEN) to prevent overflows.
Chris J Argesb2b018e2015-12-01 20:40:54 -0600224 */
Jessica Yu425595a2016-03-22 20:03:18 -0400225 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
226
227 relas = (Elf_Rela *) relasec->sh_addr;
228 /* For each rela in this klp relocation section */
229 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
230 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
231 if (sym->st_shndx != SHN_LIVEPATCH) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500232 pr_err("symbol %s is not marked as a livepatch symbol\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400233 strtab + sym->st_name);
234 return -EINVAL;
235 }
236
237 /* Format: .klp.sym.objname.symname,sympos */
238 cnt = sscanf(strtab + sym->st_name,
239 ".klp.sym.%55[^.].%127[^,],%lu",
240 objname, symname, &sympos);
241 if (cnt != 3) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500242 pr_err("symbol %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400243 strtab + sym->st_name);
244 return -EINVAL;
245 }
246
247 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
248 vmlinux = !strcmp(objname, "vmlinux");
249 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
250 symname, sympos, &addr);
251 if (ret)
252 return ret;
253
254 sym->st_value = addr;
255 }
256
257 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600258}
259
260static int klp_write_object_relocations(struct module *pmod,
261 struct klp_object *obj)
262{
Jessica Yu425595a2016-03-22 20:03:18 -0400263 int i, cnt, ret = 0;
264 const char *objname, *secname;
265 char sec_objname[MODULE_NAME_LEN];
266 Elf_Shdr *sec;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600267
268 if (WARN_ON(!klp_is_object_loaded(obj)))
269 return -EINVAL;
270
Jessica Yu425595a2016-03-22 20:03:18 -0400271 objname = klp_is_module(obj) ? obj->name : "vmlinux";
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600272
Jessica Yu425595a2016-03-22 20:03:18 -0400273 /* For each klp relocation section */
274 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
275 sec = pmod->klp_info->sechdrs + i;
276 secname = pmod->klp_info->secstrings + sec->sh_name;
277 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
278 continue;
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600279
Jessica Yu425595a2016-03-22 20:03:18 -0400280 /*
281 * Format: .klp.rela.sec_objname.section_name
282 * See comment in klp_resolve_symbols() for an explanation
283 * of the selected field width value.
284 */
285 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
286 if (cnt != 1) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500287 pr_err("section %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400288 secname);
289 ret = -EINVAL;
290 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600291 }
Jessica Yu425595a2016-03-22 20:03:18 -0400292
293 if (strcmp(objname, sec_objname))
294 continue;
295
296 ret = klp_resolve_symbols(sec, pmod);
297 if (ret)
298 break;
299
300 ret = apply_relocate_add(pmod->klp_info->sechdrs,
301 pmod->core_kallsyms.strtab,
302 pmod->klp_info->symndx, i, pmod);
303 if (ret)
304 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600305 }
306
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600307 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600308}
309
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600310/*
311 * Sysfs Interface
312 *
313 * /sys/kernel/livepatch
314 * /sys/kernel/livepatch/<patch>
315 * /sys/kernel/livepatch/<patch>/enabled
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600316 * /sys/kernel/livepatch/<patch>/transition
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100317 * /sys/kernel/livepatch/<patch>/force
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600318 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600319 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600320 */
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100321static int __klp_disable_patch(struct klp_patch *patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600322
323static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
324 const char *buf, size_t count)
325{
326 struct klp_patch *patch;
327 int ret;
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600328 bool enabled;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600329
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600330 ret = kstrtobool(buf, &enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600331 if (ret)
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600332 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600333
334 patch = container_of(kobj, struct klp_patch, kobj);
335
336 mutex_lock(&klp_mutex);
337
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600338 if (patch->enabled == enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600339 /* already in requested state */
340 ret = -EINVAL;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100341 goto out;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600342 }
343
Petr Mladek958ef1e2019-01-09 13:43:23 +0100344 /*
345 * Allow to reverse a pending transition in both ways. It might be
346 * necessary to complete the transition without forcing and breaking
347 * the system integrity.
348 *
349 * Do not allow to re-enable a disabled patch.
350 */
351 if (patch == klp_transition_patch)
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600352 klp_reverse_transition();
Petr Mladek958ef1e2019-01-09 13:43:23 +0100353 else if (!enabled)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600354 ret = __klp_disable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100355 else
356 ret = -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600357
Petr Mladek958ef1e2019-01-09 13:43:23 +0100358out:
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600359 mutex_unlock(&klp_mutex);
360
Petr Mladek958ef1e2019-01-09 13:43:23 +0100361 if (ret)
362 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600363 return count;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600364}
365
366static ssize_t enabled_show(struct kobject *kobj,
367 struct kobj_attribute *attr, char *buf)
368{
369 struct klp_patch *patch;
370
371 patch = container_of(kobj, struct klp_patch, kobj);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600372 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600373}
374
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600375static ssize_t transition_show(struct kobject *kobj,
376 struct kobj_attribute *attr, char *buf)
377{
378 struct klp_patch *patch;
379
380 patch = container_of(kobj, struct klp_patch, kobj);
381 return snprintf(buf, PAGE_SIZE-1, "%d\n",
382 patch == klp_transition_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600383}
384
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100385static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
386 const char *buf, size_t count)
387{
388 struct klp_patch *patch;
389 int ret;
390 bool val;
391
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100392 ret = kstrtobool(buf, &val);
393 if (ret)
394 return ret;
395
Miroslav Benes88690162017-12-21 14:40:43 +0100396 if (!val)
397 return count;
398
399 mutex_lock(&klp_mutex);
400
401 patch = container_of(kobj, struct klp_patch, kobj);
402 if (patch != klp_transition_patch) {
403 mutex_unlock(&klp_mutex);
404 return -EINVAL;
405 }
406
407 klp_force_transition();
408
409 mutex_unlock(&klp_mutex);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100410
411 return count;
412}
413
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600414static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600415static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100416static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600417static struct attribute *klp_patch_attrs[] = {
418 &enabled_kobj_attr.attr,
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600419 &transition_kobj_attr.attr,
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100420 &force_kobj_attr.attr,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600421 NULL
422};
Kimberly Brown70283452019-04-01 22:51:58 -0400423ATTRIBUTE_GROUPS(klp_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600424
Jason Barone1452b62019-01-09 13:43:25 +0100425static void klp_free_object_dynamic(struct klp_object *obj)
426{
427 kfree(obj->name);
428 kfree(obj);
429}
430
Petr Mladekf68d67c2019-05-03 15:26:25 +0200431static void klp_init_func_early(struct klp_object *obj,
432 struct klp_func *func);
433static void klp_init_object_early(struct klp_patch *patch,
434 struct klp_object *obj);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200435
Petr Mladekf68d67c2019-05-03 15:26:25 +0200436static struct klp_object *klp_alloc_object_dynamic(const char *name,
437 struct klp_patch *patch)
Jason Barone1452b62019-01-09 13:43:25 +0100438{
439 struct klp_object *obj;
440
441 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
442 if (!obj)
443 return NULL;
444
445 if (name) {
446 obj->name = kstrdup(name, GFP_KERNEL);
447 if (!obj->name) {
448 kfree(obj);
449 return NULL;
450 }
451 }
452
Petr Mladekf68d67c2019-05-03 15:26:25 +0200453 klp_init_object_early(patch, obj);
Jason Barone1452b62019-01-09 13:43:25 +0100454 obj->dynamic = true;
455
456 return obj;
457}
458
459static void klp_free_func_nop(struct klp_func *func)
460{
461 kfree(func->old_name);
462 kfree(func);
463}
464
465static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
466 struct klp_object *obj)
467{
468 struct klp_func *func;
469
470 func = kzalloc(sizeof(*func), GFP_KERNEL);
471 if (!func)
472 return NULL;
473
474 if (old_func->old_name) {
475 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
476 if (!func->old_name) {
477 kfree(func);
478 return NULL;
479 }
480 }
481
Petr Mladekf68d67c2019-05-03 15:26:25 +0200482 klp_init_func_early(obj, func);
Jason Barone1452b62019-01-09 13:43:25 +0100483 /*
484 * func->new_func is same as func->old_func. These addresses are
485 * set when the object is loaded, see klp_init_object_loaded().
486 */
487 func->old_sympos = old_func->old_sympos;
488 func->nop = true;
489
490 return func;
491}
492
493static int klp_add_object_nops(struct klp_patch *patch,
494 struct klp_object *old_obj)
495{
496 struct klp_object *obj;
497 struct klp_func *func, *old_func;
498
499 obj = klp_find_object(patch, old_obj);
500
501 if (!obj) {
Petr Mladekf68d67c2019-05-03 15:26:25 +0200502 obj = klp_alloc_object_dynamic(old_obj->name, patch);
Jason Barone1452b62019-01-09 13:43:25 +0100503 if (!obj)
504 return -ENOMEM;
Jason Barone1452b62019-01-09 13:43:25 +0100505 }
506
507 klp_for_each_func(old_obj, old_func) {
508 func = klp_find_func(obj, old_func);
509 if (func)
510 continue;
511
512 func = klp_alloc_func_nop(old_func, obj);
513 if (!func)
514 return -ENOMEM;
Jason Barone1452b62019-01-09 13:43:25 +0100515 }
516
517 return 0;
518}
519
520/*
521 * Add 'nop' functions which simply return to the caller to run
522 * the original function. The 'nop' functions are added to a
523 * patch to facilitate a 'replace' mode.
524 */
525static int klp_add_nops(struct klp_patch *patch)
526{
527 struct klp_patch *old_patch;
528 struct klp_object *old_obj;
529
Petr Mladekecba29f2019-02-04 14:56:50 +0100530 klp_for_each_patch(old_patch) {
Jason Barone1452b62019-01-09 13:43:25 +0100531 klp_for_each_object(old_patch, old_obj) {
532 int err;
533
534 err = klp_add_object_nops(patch, old_obj);
535 if (err)
536 return err;
537 }
538 }
539
540 return 0;
541}
542
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600543static void klp_kobj_release_patch(struct kobject *kobj)
544{
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600545 struct klp_patch *patch;
546
547 patch = container_of(kobj, struct klp_patch, kobj);
548 complete(&patch->finish);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600549}
550
551static struct kobj_type klp_ktype_patch = {
552 .release = klp_kobj_release_patch,
553 .sysfs_ops = &kobj_sysfs_ops,
Kimberly Brown70283452019-04-01 22:51:58 -0400554 .default_groups = klp_patch_groups,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600555};
556
Miroslav Benescad706d2015-05-19 12:01:18 +0200557static void klp_kobj_release_object(struct kobject *kobj)
558{
Jason Barone1452b62019-01-09 13:43:25 +0100559 struct klp_object *obj;
560
561 obj = container_of(kobj, struct klp_object, kobj);
562
563 if (obj->dynamic)
564 klp_free_object_dynamic(obj);
Miroslav Benescad706d2015-05-19 12:01:18 +0200565}
566
567static struct kobj_type klp_ktype_object = {
568 .release = klp_kobj_release_object,
569 .sysfs_ops = &kobj_sysfs_ops,
570};
571
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600572static void klp_kobj_release_func(struct kobject *kobj)
573{
Jason Barone1452b62019-01-09 13:43:25 +0100574 struct klp_func *func;
575
576 func = container_of(kobj, struct klp_func, kobj);
577
578 if (func->nop)
579 klp_free_func_nop(func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600580}
581
582static struct kobj_type klp_ktype_func = {
583 .release = klp_kobj_release_func,
584 .sysfs_ops = &kobj_sysfs_ops,
585};
586
Petr Mladekd697bad2019-01-09 13:43:26 +0100587static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600588{
Jason Barone1452b62019-01-09 13:43:25 +0100589 struct klp_func *func, *tmp_func;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600590
Jason Barone1452b62019-01-09 13:43:25 +0100591 klp_for_each_func_safe(obj, func, tmp_func) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100592 if (nops_only && !func->nop)
593 continue;
594
595 list_del(&func->node);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200596 kobject_put(&func->kobj);
Petr Mladek0430f782019-01-09 13:43:21 +0100597 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600598}
599
600/* Clean up when a patched object is unloaded */
601static void klp_free_object_loaded(struct klp_object *obj)
602{
603 struct klp_func *func;
604
605 obj->mod = NULL;
606
Jason Barone1452b62019-01-09 13:43:25 +0100607 klp_for_each_func(obj, func) {
Petr Mladek19514912019-01-09 13:43:19 +0100608 func->old_func = NULL;
Jason Barone1452b62019-01-09 13:43:25 +0100609
610 if (func->nop)
611 func->new_func = NULL;
612 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600613}
614
Petr Mladekd697bad2019-01-09 13:43:26 +0100615static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600616{
Jason Barone1452b62019-01-09 13:43:25 +0100617 struct klp_object *obj, *tmp_obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600618
Jason Barone1452b62019-01-09 13:43:25 +0100619 klp_for_each_object_safe(patch, obj, tmp_obj) {
Petr Mladekd697bad2019-01-09 13:43:26 +0100620 __klp_free_funcs(obj, nops_only);
621
622 if (nops_only && !obj->dynamic)
623 continue;
624
625 list_del(&obj->node);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200626 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600627 }
628}
629
Petr Mladekd697bad2019-01-09 13:43:26 +0100630static void klp_free_objects(struct klp_patch *patch)
631{
632 __klp_free_objects(patch, false);
633}
634
635static void klp_free_objects_dynamic(struct klp_patch *patch)
636{
637 __klp_free_objects(patch, true);
638}
639
Petr Mladek0430f782019-01-09 13:43:21 +0100640/*
641 * This function implements the free operations that can be called safely
642 * under klp_mutex.
643 *
644 * The operation must be completed by calling klp_free_patch_finish()
645 * outside klp_mutex.
646 */
Petr Mladek958ef1e2019-01-09 13:43:23 +0100647void klp_free_patch_start(struct klp_patch *patch)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600648{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600649 if (!list_empty(&patch->list))
650 list_del(&patch->list);
Petr Mladek0430f782019-01-09 13:43:21 +0100651
652 klp_free_objects(patch);
653}
654
655/*
656 * This function implements the free part that must be called outside
657 * klp_mutex.
658 *
659 * It must be called after klp_free_patch_start(). And it has to be
660 * the last function accessing the livepatch structures when the patch
661 * gets disabled.
662 */
663static void klp_free_patch_finish(struct klp_patch *patch)
664{
665 /*
666 * Avoid deadlock with enabled_store() sysfs callback by
667 * calling this outside klp_mutex. It is safe because
668 * this is called when the patch gets disabled and it
669 * cannot get enabled again.
670 */
Petr Mladek4d141ab2019-05-03 15:26:24 +0200671 kobject_put(&patch->kobj);
672 wait_for_completion(&patch->finish);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100673
674 /* Put the module after the last access to struct klp_patch. */
675 if (!patch->forced)
676 module_put(patch->mod);
677}
678
679/*
680 * The livepatch might be freed from sysfs interface created by the patch.
681 * This work allows to wait until the interface is destroyed in a separate
682 * context.
683 */
684static void klp_free_patch_work_fn(struct work_struct *work)
685{
686 struct klp_patch *patch =
687 container_of(work, struct klp_patch, free_work);
688
689 klp_free_patch_finish(patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600690}
691
692static int klp_init_func(struct klp_object *obj, struct klp_func *func)
693{
Jason Barone1452b62019-01-09 13:43:25 +0100694 if (!func->old_name)
695 return -EINVAL;
696
697 /*
698 * NOPs get the address later. The patched module must be loaded,
699 * see klp_init_object_loaded().
700 */
701 if (!func->new_func && !func->nop)
Miroslav Benesf09d9082016-04-28 16:34:08 +0200702 return -EINVAL;
703
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530704 if (strlen(func->old_name) >= KSYM_NAME_LEN)
705 return -EINVAL;
706
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600707 INIT_LIST_HEAD(&func->stack_node);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600708 func->patched = false;
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600709 func->transition = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600710
Chris J Arges444f9e92015-12-01 20:40:56 -0600711 /* The format for the sysfs directory is <function,sympos> where sympos
712 * is the nth occurrence of this symbol in kallsyms for the patched
713 * object. If the user selects 0 for old_sympos, then 1 will be used
714 * since a unique symbol will be the first occurrence.
715 */
Petr Mladek4d141ab2019-05-03 15:26:24 +0200716 return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
717 func->old_name,
718 func->old_sympos ? func->old_sympos : 1);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600719}
720
Jessica Yu255e7322016-08-17 20:58:28 -0400721/* Arches may override this to finish any remaining arch-specific tasks */
722void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
723 struct klp_object *obj)
724{
725}
726
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600727/* parts of the initialization that is done only when the object is loaded */
728static int klp_init_object_loaded(struct klp_patch *patch,
729 struct klp_object *obj)
730{
731 struct klp_func *func;
732 int ret;
733
Josh Poimboeuf9f255b62019-06-13 20:07:22 -0500734 mutex_lock(&text_mutex);
735
Jessica Yu255e7322016-08-17 20:58:28 -0400736 module_disable_ro(patch->mod);
Jessica Yu425595a2016-03-22 20:03:18 -0400737 ret = klp_write_object_relocations(patch->mod, obj);
Jessica Yu255e7322016-08-17 20:58:28 -0400738 if (ret) {
739 module_enable_ro(patch->mod, true);
Josh Poimboeuf9f255b62019-06-13 20:07:22 -0500740 mutex_unlock(&text_mutex);
Jessica Yu425595a2016-03-22 20:03:18 -0400741 return ret;
Jessica Yu255e7322016-08-17 20:58:28 -0400742 }
743
744 arch_klp_init_object_loaded(patch, obj);
745 module_enable_ro(patch->mod, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600746
Josh Poimboeuf9f255b62019-06-13 20:07:22 -0500747 mutex_unlock(&text_mutex);
748
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200749 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600750 ret = klp_find_object_symbol(obj->name, func->old_name,
751 func->old_sympos,
Petr Mladek19514912019-01-09 13:43:19 +0100752 (unsigned long *)&func->old_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600753 if (ret)
754 return ret;
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600755
Petr Mladek19514912019-01-09 13:43:19 +0100756 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600757 &func->old_size, NULL);
758 if (!ret) {
759 pr_err("kallsyms size lookup failed for '%s'\n",
760 func->old_name);
761 return -ENOENT;
762 }
763
Jason Barone1452b62019-01-09 13:43:25 +0100764 if (func->nop)
765 func->new_func = func->old_func;
766
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600767 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
768 &func->new_size, NULL);
769 if (!ret) {
770 pr_err("kallsyms size lookup failed for '%s' replacement\n",
771 func->old_name);
772 return -ENOENT;
773 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600774 }
775
776 return 0;
777}
778
779static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
780{
781 struct klp_func *func;
782 int ret;
783 const char *name;
784
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530785 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
786 return -EINVAL;
787
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600788 obj->patched = false;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100789 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600790
791 klp_find_object_module(obj);
792
793 name = klp_is_module(obj) ? obj->name : "vmlinux";
Petr Mladek4d141ab2019-05-03 15:26:24 +0200794 ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
Miroslav Benescad706d2015-05-19 12:01:18 +0200795 if (ret)
796 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600797
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200798 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600799 ret = klp_init_func(obj, func);
800 if (ret)
Petr Mladek0430f782019-01-09 13:43:21 +0100801 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600802 }
803
Petr Mladek0430f782019-01-09 13:43:21 +0100804 if (klp_is_object_loaded(obj))
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600805 ret = klp_init_object_loaded(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100806
807 return ret;
808}
809
Petr Mladekf68d67c2019-05-03 15:26:25 +0200810static void klp_init_func_early(struct klp_object *obj,
811 struct klp_func *func)
812{
813 kobject_init(&func->kobj, &klp_ktype_func);
814 list_add_tail(&func->node, &obj->func_list);
815}
816
817static void klp_init_object_early(struct klp_patch *patch,
818 struct klp_object *obj)
819{
820 INIT_LIST_HEAD(&obj->func_list);
821 kobject_init(&obj->kobj, &klp_ktype_object);
822 list_add_tail(&obj->node, &patch->obj_list);
823}
824
Petr Mladek0430f782019-01-09 13:43:21 +0100825static int klp_init_patch_early(struct klp_patch *patch)
826{
827 struct klp_object *obj;
828 struct klp_func *func;
829
830 if (!patch->objs)
831 return -EINVAL;
832
833 INIT_LIST_HEAD(&patch->list);
Jason Baron20e55022019-01-09 13:43:24 +0100834 INIT_LIST_HEAD(&patch->obj_list);
Petr Mladek4d141ab2019-05-03 15:26:24 +0200835 kobject_init(&patch->kobj, &klp_ktype_patch);
Petr Mladek0430f782019-01-09 13:43:21 +0100836 patch->enabled = false;
Petr Mladek68007282019-01-09 13:43:22 +0100837 patch->forced = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100838 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
Petr Mladek0430f782019-01-09 13:43:21 +0100839 init_completion(&patch->finish);
840
Jason Baron20e55022019-01-09 13:43:24 +0100841 klp_for_each_object_static(patch, obj) {
Petr Mladek0430f782019-01-09 13:43:21 +0100842 if (!obj->funcs)
843 return -EINVAL;
844
Petr Mladekf68d67c2019-05-03 15:26:25 +0200845 klp_init_object_early(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100846
Jason Baron20e55022019-01-09 13:43:24 +0100847 klp_for_each_func_static(obj, func) {
Petr Mladekf68d67c2019-05-03 15:26:25 +0200848 klp_init_func_early(obj, func);
Jason Baron20e55022019-01-09 13:43:24 +0100849 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600850 }
851
Petr Mladek958ef1e2019-01-09 13:43:23 +0100852 if (!try_module_get(patch->mod))
853 return -ENODEV;
854
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600855 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600856}
857
858static int klp_init_patch(struct klp_patch *patch)
859{
860 struct klp_object *obj;
861 int ret;
862
Petr Mladek4d141ab2019-05-03 15:26:24 +0200863 ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100864 if (ret)
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600865 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600866
Jason Barone1452b62019-01-09 13:43:25 +0100867 if (patch->replace) {
868 ret = klp_add_nops(patch);
869 if (ret)
870 return ret;
871 }
872
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200873 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600874 ret = klp_init_object(patch, obj);
875 if (ret)
Petr Mladek958ef1e2019-01-09 13:43:23 +0100876 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600877 }
878
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600879 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600880
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600881 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600882}
883
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100884static int __klp_disable_patch(struct klp_patch *patch)
885{
886 struct klp_object *obj;
887
888 if (WARN_ON(!patch->enabled))
889 return -EINVAL;
890
891 if (klp_transition_patch)
892 return -EBUSY;
893
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100894 klp_init_transition(patch, KLP_UNPATCHED);
895
896 klp_for_each_object(patch, obj)
897 if (obj->patched)
898 klp_pre_unpatch_callback(obj);
899
900 /*
901 * Enforce the order of the func->transition writes in
902 * klp_init_transition() and the TIF_PATCH_PENDING writes in
903 * klp_start_transition(). In the rare case where klp_ftrace_handler()
904 * is called shortly after klp_update_patch_state() switches the task,
905 * this ensures the handler sees that func->transition is set.
906 */
907 smp_wmb();
908
909 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100910 patch->enabled = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100911 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100912
913 return 0;
914}
915
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100916static int __klp_enable_patch(struct klp_patch *patch)
917{
918 struct klp_object *obj;
919 int ret;
920
921 if (klp_transition_patch)
922 return -EBUSY;
923
924 if (WARN_ON(patch->enabled))
925 return -EINVAL;
926
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100927 pr_notice("enabling patch '%s'\n", patch->mod->name);
928
929 klp_init_transition(patch, KLP_PATCHED);
930
931 /*
932 * Enforce the order of the func->transition writes in
933 * klp_init_transition() and the ops->func_stack writes in
934 * klp_patch_object(), so that klp_ftrace_handler() will see the
935 * func->transition updates before the handler is registered and the
936 * new funcs become visible to the handler.
937 */
938 smp_wmb();
939
940 klp_for_each_object(patch, obj) {
941 if (!klp_is_object_loaded(obj))
942 continue;
943
944 ret = klp_pre_patch_callback(obj);
945 if (ret) {
946 pr_warn("pre-patch callback failed for object '%s'\n",
947 klp_is_module(obj) ? obj->name : "vmlinux");
948 goto err;
949 }
950
951 ret = klp_patch_object(obj);
952 if (ret) {
953 pr_warn("failed to patch object '%s'\n",
954 klp_is_module(obj) ? obj->name : "vmlinux");
955 goto err;
956 }
957 }
958
959 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100960 patch->enabled = true;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100961 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100962
963 return 0;
964err:
965 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
966
967 klp_cancel_transition();
968 return ret;
969}
970
971/**
Petr Mladek958ef1e2019-01-09 13:43:23 +0100972 * klp_enable_patch() - enable the livepatch
973 * @patch: patch to be enabled
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100974 *
Petr Mladek958ef1e2019-01-09 13:43:23 +0100975 * Initializes the data structure associated with the patch, creates the sysfs
976 * interface, performs the needed symbol lookups and code relocations,
977 * registers the patched functions with ftrace.
978 *
979 * This function is supposed to be called from the livepatch module_init()
980 * callback.
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100981 *
982 * Return: 0 on success, otherwise error
983 */
984int klp_enable_patch(struct klp_patch *patch)
985{
986 int ret;
987
Petr Mladek958ef1e2019-01-09 13:43:23 +0100988 if (!patch || !patch->mod)
989 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100990
Petr Mladek958ef1e2019-01-09 13:43:23 +0100991 if (!is_livepatch_module(patch->mod)) {
992 pr_err("module %s is not marked as a livepatch module\n",
993 patch->mod->name);
994 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100995 }
996
Petr Mladek958ef1e2019-01-09 13:43:23 +0100997 if (!klp_initialized())
998 return -ENODEV;
999
1000 if (!klp_have_reliable_stack()) {
Petr Mladek31adf232019-04-24 10:55:48 +02001001 pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
1002 pr_warn("The livepatch transition may never complete.\n");
Petr Mladek958ef1e2019-01-09 13:43:23 +01001003 }
1004
Petr Mladek958ef1e2019-01-09 13:43:23 +01001005 mutex_lock(&klp_mutex);
1006
1007 ret = klp_init_patch_early(patch);
1008 if (ret) {
1009 mutex_unlock(&klp_mutex);
1010 return ret;
1011 }
1012
1013 ret = klp_init_patch(patch);
1014 if (ret)
1015 goto err;
1016
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001017 ret = __klp_enable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001018 if (ret)
1019 goto err;
1020
1021 mutex_unlock(&klp_mutex);
1022
1023 return 0;
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001024
1025err:
Petr Mladek958ef1e2019-01-09 13:43:23 +01001026 klp_free_patch_start(patch);
1027
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001028 mutex_unlock(&klp_mutex);
Petr Mladek958ef1e2019-01-09 13:43:23 +01001029
1030 klp_free_patch_finish(patch);
1031
Petr Mladek26c3e98e2019-01-09 13:43:20 +01001032 return ret;
1033}
1034EXPORT_SYMBOL_GPL(klp_enable_patch);
1035
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001036/*
Jason Barone1452b62019-01-09 13:43:25 +01001037 * This function removes replaced patches.
1038 *
1039 * We could be pretty aggressive here. It is called in the situation where
1040 * these structures are no longer accessible. All functions are redirected
1041 * by the klp_transition_patch. They use either a new code or they are in
1042 * the original code because of the special nop function patches.
1043 *
1044 * The only exception is when the transition was forced. In this case,
1045 * klp_ftrace_handler() might still see the replaced patch on the stack.
1046 * Fortunately, it is carefully designed to work with removed functions
1047 * thanks to RCU. We only have to keep the patches on the system. Also
1048 * this is handled transparently by patch->module_put.
1049 */
1050void klp_discard_replaced_patches(struct klp_patch *new_patch)
1051{
1052 struct klp_patch *old_patch, *tmp_patch;
1053
Petr Mladekecba29f2019-02-04 14:56:50 +01001054 klp_for_each_patch_safe(old_patch, tmp_patch) {
Jason Barone1452b62019-01-09 13:43:25 +01001055 if (old_patch == new_patch)
1056 return;
1057
1058 old_patch->enabled = false;
1059 klp_unpatch_objects(old_patch);
1060 klp_free_patch_start(old_patch);
1061 schedule_work(&old_patch->free_work);
1062 }
1063}
1064
1065/*
Petr Mladekd697bad2019-01-09 13:43:26 +01001066 * This function removes the dynamically allocated 'nop' functions.
1067 *
1068 * We could be pretty aggressive. NOPs do not change the existing
1069 * behavior except for adding unnecessary delay by the ftrace handler.
1070 *
1071 * It is safe even when the transition was forced. The ftrace handler
1072 * will see a valid ops->func_stack entry thanks to RCU.
1073 *
1074 * We could even free the NOPs structures. They must be the last entry
1075 * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1076 * It does the same as klp_synchronize_transition() to make sure that
1077 * nobody is inside the ftrace handler once the operation finishes.
1078 *
1079 * IMPORTANT: It must be called right after removing the replaced patches!
1080 */
1081void klp_discard_nops(struct klp_patch *new_patch)
1082{
1083 klp_unpatch_objects_dynamic(klp_transition_patch);
1084 klp_free_objects_dynamic(klp_transition_patch);
1085}
1086
1087/*
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001088 * Remove parts of patches that touch a given kernel module. The list of
1089 * patches processed might be limited. When limit is NULL, all patches
1090 * will be handled.
1091 */
1092static void klp_cleanup_module_patches_limited(struct module *mod,
1093 struct klp_patch *limit)
1094{
1095 struct klp_patch *patch;
1096 struct klp_object *obj;
1097
Petr Mladekecba29f2019-02-04 14:56:50 +01001098 klp_for_each_patch(patch) {
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001099 if (patch == limit)
1100 break;
1101
1102 klp_for_each_object(patch, obj) {
1103 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1104 continue;
1105
Petr Mladeka087cdd2019-02-04 14:56:53 +01001106 if (patch != klp_transition_patch)
1107 klp_pre_unpatch_callback(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001108
Petr Mladeka087cdd2019-02-04 14:56:53 +01001109 pr_notice("reverting patch '%s' on unloading module '%s'\n",
1110 patch->mod->name, obj->mod->name);
1111 klp_unpatch_object(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +01001112
Petr Mladeka087cdd2019-02-04 14:56:53 +01001113 klp_post_unpatch_callback(obj);
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001114
1115 klp_free_object_loaded(obj);
1116 break;
1117 }
1118 }
1119}
1120
Jessica Yu7e545d62016-03-16 20:55:39 -04001121int klp_module_coming(struct module *mod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001122{
Minfei Huang36e505c2015-05-15 10:22:48 +08001123 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001124 struct klp_patch *patch;
1125 struct klp_object *obj;
1126
Jessica Yu7e545d62016-03-16 20:55:39 -04001127 if (WARN_ON(mod->state != MODULE_STATE_COMING))
1128 return -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001129
1130 mutex_lock(&klp_mutex);
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001131 /*
Jessica Yu7e545d62016-03-16 20:55:39 -04001132 * Each module has to know that klp_module_coming()
1133 * has been called. We never know what module will
1134 * get patched by a new patch.
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001135 */
Jessica Yu7e545d62016-03-16 20:55:39 -04001136 mod->klp_alive = true;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +01001137
Petr Mladekecba29f2019-02-04 14:56:50 +01001138 klp_for_each_patch(patch) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +02001139 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001140 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1141 continue;
1142
Jessica Yu7e545d62016-03-16 20:55:39 -04001143 obj->mod = mod;
1144
1145 ret = klp_init_object_loaded(patch, obj);
1146 if (ret) {
1147 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
1148 patch->mod->name, obj->mod->name, ret);
1149 goto err;
1150 }
1151
Jessica Yu7e545d62016-03-16 20:55:39 -04001152 pr_notice("applying patch '%s' to loading module '%s'\n",
1153 patch->mod->name, obj->mod->name);
1154
Joe Lawrence93862e32017-10-13 15:08:41 -04001155 ret = klp_pre_patch_callback(obj);
1156 if (ret) {
1157 pr_warn("pre-patch callback failed for object '%s'\n",
1158 obj->name);
1159 goto err;
1160 }
1161
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -06001162 ret = klp_patch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001163 if (ret) {
1164 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
1165 patch->mod->name, obj->mod->name, ret);
Joe Lawrence93862e32017-10-13 15:08:41 -04001166
Petr Mladek5aaf1ab2017-10-20 16:56:50 +02001167 klp_post_unpatch_callback(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001168 goto err;
1169 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001170
Joe Lawrence93862e32017-10-13 15:08:41 -04001171 if (patch != klp_transition_patch)
1172 klp_post_patch_callback(obj);
1173
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001174 break;
1175 }
1176 }
1177
1178 mutex_unlock(&klp_mutex);
1179
1180 return 0;
Jessica Yu7e545d62016-03-16 20:55:39 -04001181
1182err:
1183 /*
1184 * If a patch is unsuccessfully applied, return
1185 * error to the module loader.
1186 */
1187 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1188 patch->mod->name, obj->mod->name, obj->mod->name);
1189 mod->klp_alive = false;
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001190 klp_cleanup_module_patches_limited(mod, patch);
Jessica Yu7e545d62016-03-16 20:55:39 -04001191 mutex_unlock(&klp_mutex);
1192
1193 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001194}
1195
Jessica Yu7e545d62016-03-16 20:55:39 -04001196void klp_module_going(struct module *mod)
1197{
Jessica Yu7e545d62016-03-16 20:55:39 -04001198 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
1199 mod->state != MODULE_STATE_COMING))
1200 return;
1201
1202 mutex_lock(&klp_mutex);
1203 /*
1204 * Each module has to know that klp_module_going()
1205 * has been called. We never know what module will
1206 * get patched by a new patch.
1207 */
1208 mod->klp_alive = false;
1209
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001210 klp_cleanup_module_patches_limited(mod, NULL);
Jessica Yu7e545d62016-03-16 20:55:39 -04001211
1212 mutex_unlock(&klp_mutex);
1213}
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001214
Minfei Huang26029d82015-05-22 22:26:29 +08001215static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001216{
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001217 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001218 if (!klp_root_kobj)
1219 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001220
1221 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001222}
1223
1224module_init(klp_init);