blob: 35ee2c5b717663d889c7cbf58227cee5f73569db [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>
27#include <linux/ftrace.h>
28#include <linux/list.h>
29#include <linux/kallsyms.h>
30#include <linux/livepatch.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060031#include <asm/cacheflush.h>
Seth Jenningsb700e7f2014-12-16 11:58:19 -060032
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060033/**
34 * struct klp_ops - structure for tracking registered ftrace ops structs
35 *
36 * A single ftrace_ops is shared between all enabled replacement functions
37 * (klp_func structs) which have the same old_addr. This allows the switch
38 * between function versions to happen instantaneously by updating the klp_ops
39 * struct's func_stack list. The winner is the klp_func at the top of the
40 * func_stack (front of the list).
41 *
42 * @node: node for the global klp_ops list
43 * @func_stack: list head for the stack of klp_func's (active func is on top)
44 * @fops: registered ftrace ops struct
Seth Jenningsb700e7f2014-12-16 11:58:19 -060045 */
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060046struct klp_ops {
47 struct list_head node;
48 struct list_head func_stack;
49 struct ftrace_ops fops;
50};
Seth Jenningsb700e7f2014-12-16 11:58:19 -060051
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060052/*
53 * The klp_mutex protects the global lists and state transitions of any
54 * structure reachable from them. References to any structure must be obtained
55 * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
56 * ensure it gets consistent data).
57 */
Seth Jenningsb700e7f2014-12-16 11:58:19 -060058static DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060059
Seth Jenningsb700e7f2014-12-16 11:58:19 -060060static LIST_HEAD(klp_patches);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060061static LIST_HEAD(klp_ops);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060062
63static struct kobject *klp_root_kobj;
64
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060065static struct klp_ops *klp_find_ops(unsigned long old_addr)
66{
67 struct klp_ops *ops;
68 struct klp_func *func;
69
70 list_for_each_entry(ops, &klp_ops, node) {
71 func = list_first_entry(&ops->func_stack, struct klp_func,
72 stack_node);
73 if (func->old_addr == old_addr)
74 return ops;
75 }
76
77 return NULL;
78}
79
Seth Jenningsb700e7f2014-12-16 11:58:19 -060080static bool klp_is_module(struct klp_object *obj)
81{
82 return obj->name;
83}
84
85static bool klp_is_object_loaded(struct klp_object *obj)
86{
87 return !obj->name || obj->mod;
88}
89
90/* sets obj->mod if object is not vmlinux and module is found */
91static void klp_find_object_module(struct klp_object *obj)
92{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010093 struct module *mod;
94
Seth Jenningsb700e7f2014-12-16 11:58:19 -060095 if (!klp_is_module(obj))
96 return;
97
98 mutex_lock(&module_mutex);
99 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100100 * We do not want to block removal of patched modules and therefore
101 * we do not take a reference here. The patches are removed by
102 * a going module handler instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600103 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100104 mod = find_module(obj->name);
105 /*
106 * Do not mess work of the module coming and going notifiers.
107 * Note that the patch might still be needed before the going handler
108 * is called. Module functions can be called even in the GOING state
109 * until mod->exit() finishes. This is especially important for
110 * patches that modify semantic of the functions.
111 */
112 if (mod && mod->klp_alive)
113 obj->mod = mod;
114
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600115 mutex_unlock(&module_mutex);
116}
117
118/* klp_mutex must be held by caller */
119static bool klp_is_patch_registered(struct klp_patch *patch)
120{
121 struct klp_patch *mypatch;
122
123 list_for_each_entry(mypatch, &klp_patches, list)
124 if (mypatch == patch)
125 return true;
126
127 return false;
128}
129
130static bool klp_initialized(void)
131{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +0200132 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600133}
134
135struct klp_find_arg {
136 const char *objname;
137 const char *name;
138 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600139 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600140 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600141};
142
143static int klp_find_callback(void *data, const char *name,
144 struct module *mod, unsigned long addr)
145{
146 struct klp_find_arg *args = data;
147
148 if ((mod && !args->objname) || (!mod && args->objname))
149 return 0;
150
151 if (strcmp(args->name, name))
152 return 0;
153
154 if (args->objname && strcmp(args->objname, mod->name))
155 return 0;
156
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600157 args->addr = addr;
158 args->count++;
159
Chris J Argesb2b018e2015-12-01 20:40:54 -0600160 /*
161 * Finish the search when the symbol is found for the desired position
162 * or the position is not defined for a non-unique symbol.
163 */
164 if ((args->pos && (args->count == args->pos)) ||
165 (!args->pos && (args->count > 1)))
166 return 1;
167
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600168 return 0;
169}
170
171static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600172 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600173{
174 struct klp_find_arg args = {
175 .objname = objname,
176 .name = name,
177 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600178 .count = 0,
179 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600180 };
181
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200182 mutex_lock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600183 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200184 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600185
Chris J Argesb2b018e2015-12-01 20:40:54 -0600186 /*
187 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
188 * otherwise ensure the symbol position count matches sympos.
189 */
190 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600191 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600192 else if (args.count > 1 && sympos == 0) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600193 pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
194 args.count, name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600195 } else if (sympos != args.count && sympos > 0) {
196 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
197 sympos, name, objname ? objname : "vmlinux");
198 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600199 *addr = args.addr;
200 return 0;
201 }
202
203 *addr = 0;
204 return -EINVAL;
205}
206
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600207/*
208 * external symbols are located outside the parent object (where the parent
209 * object is either vmlinux or the kmod being patched).
210 */
211static int klp_find_external_symbol(struct module *pmod, const char *name,
212 unsigned long *addr)
213{
214 const struct kernel_symbol *sym;
215
216 /* first, check if it's an exported symbol */
217 preempt_disable();
218 sym = find_symbol(name, NULL, NULL, true, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600219 if (sym) {
220 *addr = sym->value;
Peter Zijlstrac064a0d2015-02-28 22:24:48 +0100221 preempt_enable();
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600222 return 0;
223 }
Peter Zijlstrac064a0d2015-02-28 22:24:48 +0100224 preempt_enable();
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600225
Chris J Argesb2b018e2015-12-01 20:40:54 -0600226 /*
227 * Check if it's in another .o within the patch module. This also
228 * checks that the external symbol is unique.
229 */
230 return klp_find_object_symbol(pmod->name, name, 0, addr);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600231}
232
233static int klp_write_object_relocations(struct module *pmod,
234 struct klp_object *obj)
235{
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600236 int ret = 0;
Chris J Arges064c89d2015-12-01 20:40:55 -0600237 unsigned long val;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600238 struct klp_reloc *reloc;
239
240 if (WARN_ON(!klp_is_object_loaded(obj)))
241 return -EINVAL;
242
243 if (WARN_ON(!obj->relocs))
244 return -EINVAL;
245
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600246 module_disable_ro(pmod);
247
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600248 for (reloc = obj->relocs; reloc->name; reloc++) {
Chris J Arges064c89d2015-12-01 20:40:55 -0600249 /* discover the address of the referenced symbol */
250 if (reloc->external) {
251 if (reloc->sympos > 0) {
252 pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
253 reloc->name);
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600254 ret = -EINVAL;
255 goto out;
Chris J Arges064c89d2015-12-01 20:40:55 -0600256 }
257 ret = klp_find_external_symbol(pmod, reloc->name, &val);
258 } else
259 ret = klp_find_object_symbol(obj->name,
260 reloc->name,
261 reloc->sympos,
262 &val);
263 if (ret)
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600264 goto out;
Zhou Chengminge41b1042015-11-06 14:25:00 +0800265
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600266 ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
Chris J Arges064c89d2015-12-01 20:40:55 -0600267 val + reloc->addend);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600268 if (ret) {
269 pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
Chris J Arges064c89d2015-12-01 20:40:55 -0600270 reloc->name, val, ret);
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600271 goto out;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600272 }
273 }
274
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600275out:
276 module_enable_ro(pmod);
277 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600278}
279
280static void notrace klp_ftrace_handler(unsigned long ip,
281 unsigned long parent_ip,
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600282 struct ftrace_ops *fops,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600283 struct pt_regs *regs)
284{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600285 struct klp_ops *ops;
286 struct klp_func *func;
287
288 ops = container_of(fops, struct klp_ops, fops);
289
290 rcu_read_lock();
291 func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
292 stack_node);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600293 if (WARN_ON_ONCE(!func))
Petr Mladekc4ce0da2015-02-18 18:02:13 +0100294 goto unlock;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600295
Li Binb5bfc512014-12-19 14:11:17 +0800296 klp_arch_set_pc(regs, (unsigned long)func->new_func);
Petr Mladekc4ce0da2015-02-18 18:02:13 +0100297unlock:
298 rcu_read_unlock();
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600299}
300
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100301/*
302 * Convert a function address into the appropriate ftrace location.
303 *
304 * Usually this is just the address of the function, but on some architectures
305 * it's more complicated so allow them to provide a custom behaviour.
306 */
307#ifndef klp_get_ftrace_location
308static unsigned long klp_get_ftrace_location(unsigned long faddr)
309{
310 return faddr;
311}
312#endif
313
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600314static void klp_disable_func(struct klp_func *func)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600315{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600316 struct klp_ops *ops;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600317
Minfei Huang225f58f2015-07-14 11:15:37 +0800318 if (WARN_ON(func->state != KLP_ENABLED))
319 return;
320 if (WARN_ON(!func->old_addr))
321 return;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600322
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600323 ops = klp_find_ops(func->old_addr);
324 if (WARN_ON(!ops))
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600325 return;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600326
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600327 if (list_is_singular(&ops->func_stack)) {
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100328 unsigned long ftrace_loc;
329
330 ftrace_loc = klp_get_ftrace_location(func->old_addr);
331 if (WARN_ON(!ftrace_loc))
332 return;
333
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600334 WARN_ON(unregister_ftrace_function(&ops->fops));
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100335 WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600336
337 list_del_rcu(&func->stack_node);
338 list_del(&ops->node);
339 kfree(ops);
340 } else {
341 list_del_rcu(&func->stack_node);
342 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600343
344 func->state = KLP_DISABLED;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600345}
346
347static int klp_enable_func(struct klp_func *func)
348{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600349 struct klp_ops *ops;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600350 int ret;
351
352 if (WARN_ON(!func->old_addr))
353 return -EINVAL;
354
355 if (WARN_ON(func->state != KLP_DISABLED))
356 return -EINVAL;
357
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600358 ops = klp_find_ops(func->old_addr);
359 if (!ops) {
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100360 unsigned long ftrace_loc;
361
362 ftrace_loc = klp_get_ftrace_location(func->old_addr);
363 if (!ftrace_loc) {
364 pr_err("failed to find location for function '%s'\n",
365 func->old_name);
366 return -EINVAL;
367 }
368
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600369 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
370 if (!ops)
371 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600372
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600373 ops->fops.func = klp_ftrace_handler;
374 ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
375 FTRACE_OPS_FL_DYNAMIC |
376 FTRACE_OPS_FL_IPMODIFY;
377
378 list_add(&ops->node, &klp_ops);
379
380 INIT_LIST_HEAD(&ops->func_stack);
381 list_add_rcu(&func->stack_node, &ops->func_stack);
382
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100383 ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600384 if (ret) {
385 pr_err("failed to set ftrace filter for function '%s' (%d)\n",
386 func->old_name, ret);
387 goto err;
388 }
389
390 ret = register_ftrace_function(&ops->fops);
391 if (ret) {
392 pr_err("failed to register ftrace handler for function '%s' (%d)\n",
393 func->old_name, ret);
Michael Ellerman28e7cbd2016-03-24 22:04:02 +1100394 ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600395 goto err;
396 }
397
398
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600399 } else {
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600400 list_add_rcu(&func->stack_node, &ops->func_stack);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600401 }
402
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600403 func->state = KLP_ENABLED;
404
Josh Poimboeufdbed7dd2015-01-20 16:07:55 -0600405 return 0;
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600406
407err:
408 list_del_rcu(&func->stack_node);
409 list_del(&ops->node);
410 kfree(ops);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600411 return ret;
412}
413
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600414static void klp_disable_object(struct klp_object *obj)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600415{
416 struct klp_func *func;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600417
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200418 klp_for_each_func(obj, func)
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600419 if (func->state == KLP_ENABLED)
420 klp_disable_func(func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600421
422 obj->state = KLP_DISABLED;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600423}
424
425static int klp_enable_object(struct klp_object *obj)
426{
427 struct klp_func *func;
428 int ret;
429
430 if (WARN_ON(obj->state != KLP_DISABLED))
431 return -EINVAL;
432
433 if (WARN_ON(!klp_is_object_loaded(obj)))
434 return -EINVAL;
435
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200436 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600437 ret = klp_enable_func(func);
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600438 if (ret) {
439 klp_disable_object(obj);
440 return ret;
441 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600442 }
443 obj->state = KLP_ENABLED;
444
445 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600446}
447
448static int __klp_disable_patch(struct klp_patch *patch)
449{
450 struct klp_object *obj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600451
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600452 /* enforce stacking: only the last enabled patch can be disabled */
453 if (!list_is_last(&patch->list, &klp_patches) &&
454 list_next_entry(patch, list)->state == KLP_ENABLED)
455 return -EBUSY;
456
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600457 pr_notice("disabling patch '%s'\n", patch->mod->name);
458
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200459 klp_for_each_object(patch, obj) {
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600460 if (obj->state == KLP_ENABLED)
461 klp_disable_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600462 }
463
464 patch->state = KLP_DISABLED;
465
466 return 0;
467}
468
469/**
470 * klp_disable_patch() - disables a registered patch
471 * @patch: The registered, enabled patch to be disabled
472 *
473 * Unregisters the patched functions from ftrace.
474 *
475 * Return: 0 on success, otherwise error
476 */
477int klp_disable_patch(struct klp_patch *patch)
478{
479 int ret;
480
481 mutex_lock(&klp_mutex);
482
483 if (!klp_is_patch_registered(patch)) {
484 ret = -EINVAL;
485 goto err;
486 }
487
488 if (patch->state == KLP_DISABLED) {
489 ret = -EINVAL;
490 goto err;
491 }
492
493 ret = __klp_disable_patch(patch);
494
495err:
496 mutex_unlock(&klp_mutex);
497 return ret;
498}
499EXPORT_SYMBOL_GPL(klp_disable_patch);
500
501static int __klp_enable_patch(struct klp_patch *patch)
502{
503 struct klp_object *obj;
504 int ret;
505
506 if (WARN_ON(patch->state != KLP_DISABLED))
507 return -EINVAL;
508
Josh Poimboeuf83a90bb2015-01-20 09:26:18 -0600509 /* enforce stacking: only the first disabled patch can be enabled */
510 if (patch->list.prev != &klp_patches &&
511 list_prev_entry(patch, list)->state == KLP_DISABLED)
512 return -EBUSY;
513
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600514 pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
515 add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
516
517 pr_notice("enabling patch '%s'\n", patch->mod->name);
518
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200519 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600520 if (!klp_is_object_loaded(obj))
521 continue;
522
523 ret = klp_enable_object(obj);
524 if (ret)
525 goto unregister;
526 }
527
528 patch->state = KLP_ENABLED;
529
530 return 0;
531
532unregister:
533 WARN_ON(__klp_disable_patch(patch));
534 return ret;
535}
536
537/**
538 * klp_enable_patch() - enables a registered patch
539 * @patch: The registered, disabled patch to be enabled
540 *
541 * Performs the needed symbol lookups and code relocations,
542 * then registers the patched functions with ftrace.
543 *
544 * Return: 0 on success, otherwise error
545 */
546int klp_enable_patch(struct klp_patch *patch)
547{
548 int ret;
549
550 mutex_lock(&klp_mutex);
551
552 if (!klp_is_patch_registered(patch)) {
553 ret = -EINVAL;
554 goto err;
555 }
556
557 ret = __klp_enable_patch(patch);
558
559err:
560 mutex_unlock(&klp_mutex);
561 return ret;
562}
563EXPORT_SYMBOL_GPL(klp_enable_patch);
564
565/*
566 * Sysfs Interface
567 *
568 * /sys/kernel/livepatch
569 * /sys/kernel/livepatch/<patch>
570 * /sys/kernel/livepatch/<patch>/enabled
571 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600572 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600573 */
574
575static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
576 const char *buf, size_t count)
577{
578 struct klp_patch *patch;
579 int ret;
580 unsigned long val;
581
582 ret = kstrtoul(buf, 10, &val);
583 if (ret)
584 return -EINVAL;
585
586 if (val != KLP_DISABLED && val != KLP_ENABLED)
587 return -EINVAL;
588
589 patch = container_of(kobj, struct klp_patch, kobj);
590
591 mutex_lock(&klp_mutex);
592
593 if (val == patch->state) {
594 /* already in requested state */
595 ret = -EINVAL;
596 goto err;
597 }
598
599 if (val == KLP_ENABLED) {
600 ret = __klp_enable_patch(patch);
601 if (ret)
602 goto err;
603 } else {
604 ret = __klp_disable_patch(patch);
605 if (ret)
606 goto err;
607 }
608
609 mutex_unlock(&klp_mutex);
610
611 return count;
612
613err:
614 mutex_unlock(&klp_mutex);
615 return ret;
616}
617
618static ssize_t enabled_show(struct kobject *kobj,
619 struct kobj_attribute *attr, char *buf)
620{
621 struct klp_patch *patch;
622
623 patch = container_of(kobj, struct klp_patch, kobj);
624 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
625}
626
627static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
628static struct attribute *klp_patch_attrs[] = {
629 &enabled_kobj_attr.attr,
630 NULL
631};
632
633static void klp_kobj_release_patch(struct kobject *kobj)
634{
635 /*
636 * Once we have a consistency model we'll need to module_put() the
637 * patch module here. See klp_register_patch() for more details.
638 */
639}
640
641static struct kobj_type klp_ktype_patch = {
642 .release = klp_kobj_release_patch,
643 .sysfs_ops = &kobj_sysfs_ops,
644 .default_attrs = klp_patch_attrs,
645};
646
Miroslav Benescad706d2015-05-19 12:01:18 +0200647static void klp_kobj_release_object(struct kobject *kobj)
648{
649}
650
651static struct kobj_type klp_ktype_object = {
652 .release = klp_kobj_release_object,
653 .sysfs_ops = &kobj_sysfs_ops,
654};
655
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600656static void klp_kobj_release_func(struct kobject *kobj)
657{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600658}
659
660static struct kobj_type klp_ktype_func = {
661 .release = klp_kobj_release_func,
662 .sysfs_ops = &kobj_sysfs_ops,
663};
664
665/*
666 * Free all functions' kobjects in the array up to some limit. When limit is
667 * NULL, all kobjects are freed.
668 */
669static void klp_free_funcs_limited(struct klp_object *obj,
670 struct klp_func *limit)
671{
672 struct klp_func *func;
673
674 for (func = obj->funcs; func->old_name && func != limit; func++)
675 kobject_put(&func->kobj);
676}
677
678/* Clean up when a patched object is unloaded */
679static void klp_free_object_loaded(struct klp_object *obj)
680{
681 struct klp_func *func;
682
683 obj->mod = NULL;
684
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200685 klp_for_each_func(obj, func)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600686 func->old_addr = 0;
687}
688
689/*
690 * Free all objects' kobjects in the array up to some limit. When limit is
691 * NULL, all kobjects are freed.
692 */
693static void klp_free_objects_limited(struct klp_patch *patch,
694 struct klp_object *limit)
695{
696 struct klp_object *obj;
697
698 for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
699 klp_free_funcs_limited(obj, NULL);
Miroslav Benescad706d2015-05-19 12:01:18 +0200700 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600701 }
702}
703
704static void klp_free_patch(struct klp_patch *patch)
705{
706 klp_free_objects_limited(patch, NULL);
707 if (!list_empty(&patch->list))
708 list_del(&patch->list);
709 kobject_put(&patch->kobj);
710}
711
712static int klp_init_func(struct klp_object *obj, struct klp_func *func)
713{
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600714 INIT_LIST_HEAD(&func->stack_node);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600715 func->state = KLP_DISABLED;
716
Chris J Arges444f9e92015-12-01 20:40:56 -0600717 /* The format for the sysfs directory is <function,sympos> where sympos
718 * is the nth occurrence of this symbol in kallsyms for the patched
719 * object. If the user selects 0 for old_sympos, then 1 will be used
720 * since a unique symbol will be the first occurrence.
721 */
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600722 return kobject_init_and_add(&func->kobj, &klp_ktype_func,
Chris J Arges444f9e92015-12-01 20:40:56 -0600723 &obj->kobj, "%s,%lu", func->old_name,
724 func->old_sympos ? func->old_sympos : 1);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600725}
726
727/* 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
734 if (obj->relocs) {
735 ret = klp_write_object_relocations(patch->mod, obj);
736 if (ret)
737 return ret;
738 }
739
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200740 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600741 ret = klp_find_object_symbol(obj->name, func->old_name,
742 func->old_sympos,
743 &func->old_addr);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600744 if (ret)
745 return ret;
746 }
747
748 return 0;
749}
750
751static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
752{
753 struct klp_func *func;
754 int ret;
755 const char *name;
756
757 if (!obj->funcs)
758 return -EINVAL;
759
760 obj->state = KLP_DISABLED;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100761 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600762
763 klp_find_object_module(obj);
764
765 name = klp_is_module(obj) ? obj->name : "vmlinux";
Miroslav Benescad706d2015-05-19 12:01:18 +0200766 ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
767 &patch->kobj, "%s", name);
768 if (ret)
769 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600770
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200771 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600772 ret = klp_init_func(obj, func);
773 if (ret)
774 goto free;
775 }
776
777 if (klp_is_object_loaded(obj)) {
778 ret = klp_init_object_loaded(patch, obj);
779 if (ret)
780 goto free;
781 }
782
783 return 0;
784
785free:
786 klp_free_funcs_limited(obj, func);
Miroslav Benescad706d2015-05-19 12:01:18 +0200787 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600788 return ret;
789}
790
791static int klp_init_patch(struct klp_patch *patch)
792{
793 struct klp_object *obj;
794 int ret;
795
796 if (!patch->objs)
797 return -EINVAL;
798
799 mutex_lock(&klp_mutex);
800
801 patch->state = KLP_DISABLED;
802
803 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
Jiri Kosinae0b561e2015-02-15 10:03:20 +0100804 klp_root_kobj, "%s", patch->mod->name);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600805 if (ret)
806 goto unlock;
807
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200808 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600809 ret = klp_init_object(patch, obj);
810 if (ret)
811 goto free;
812 }
813
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600814 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600815
816 mutex_unlock(&klp_mutex);
817
818 return 0;
819
820free:
821 klp_free_objects_limited(patch, obj);
822 kobject_put(&patch->kobj);
823unlock:
824 mutex_unlock(&klp_mutex);
825 return ret;
826}
827
828/**
829 * klp_unregister_patch() - unregisters a patch
830 * @patch: Disabled patch to be unregistered
831 *
832 * Frees the data structures and removes the sysfs interface.
833 *
834 * Return: 0 on success, otherwise error
835 */
836int klp_unregister_patch(struct klp_patch *patch)
837{
838 int ret = 0;
839
840 mutex_lock(&klp_mutex);
841
842 if (!klp_is_patch_registered(patch)) {
843 ret = -EINVAL;
844 goto out;
845 }
846
847 if (patch->state == KLP_ENABLED) {
848 ret = -EBUSY;
849 goto out;
850 }
851
852 klp_free_patch(patch);
853
854out:
855 mutex_unlock(&klp_mutex);
856 return ret;
857}
858EXPORT_SYMBOL_GPL(klp_unregister_patch);
859
860/**
861 * klp_register_patch() - registers a patch
862 * @patch: Patch to be registered
863 *
864 * Initializes the data structure associated with the patch and
865 * creates the sysfs interface.
866 *
867 * Return: 0 on success, otherwise error
868 */
869int klp_register_patch(struct klp_patch *patch)
870{
871 int ret;
872
873 if (!klp_initialized())
874 return -ENODEV;
875
876 if (!patch || !patch->mod)
877 return -EINVAL;
878
879 /*
880 * A reference is taken on the patch module to prevent it from being
881 * unloaded. Right now, we don't allow patch modules to unload since
882 * there is currently no method to determine if a thread is still
883 * running in the patched code contained in the patch module once
884 * the ftrace registration is successful.
885 */
886 if (!try_module_get(patch->mod))
887 return -ENODEV;
888
889 ret = klp_init_patch(patch);
890 if (ret)
891 module_put(patch->mod);
892
893 return ret;
894}
895EXPORT_SYMBOL_GPL(klp_register_patch);
896
Minfei Huang36e505c2015-05-15 10:22:48 +0800897static int klp_module_notify_coming(struct klp_patch *patch,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600898 struct klp_object *obj)
899{
900 struct module *pmod = patch->mod;
901 struct module *mod = obj->mod;
902 int ret;
903
904 ret = klp_init_object_loaded(patch, obj);
Minfei Huang36e505c2015-05-15 10:22:48 +0800905 if (ret) {
906 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
907 pmod->name, mod->name, ret);
908 return ret;
909 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600910
911 if (patch->state == KLP_DISABLED)
Minfei Huang36e505c2015-05-15 10:22:48 +0800912 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600913
914 pr_notice("applying patch '%s' to loading module '%s'\n",
915 pmod->name, mod->name);
916
917 ret = klp_enable_object(obj);
Minfei Huang36e505c2015-05-15 10:22:48 +0800918 if (ret)
919 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
920 pmod->name, mod->name, ret);
921 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600922}
923
924static void klp_module_notify_going(struct klp_patch *patch,
925 struct klp_object *obj)
926{
927 struct module *pmod = patch->mod;
928 struct module *mod = obj->mod;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600929
930 if (patch->state == KLP_DISABLED)
931 goto disabled;
932
933 pr_notice("reverting patch '%s' on unloading module '%s'\n",
934 pmod->name, mod->name);
935
Josh Poimboeuf0937e3b2015-02-09 11:31:13 -0600936 klp_disable_object(obj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600937
938disabled:
939 klp_free_object_loaded(obj);
940}
941
942static int klp_module_notify(struct notifier_block *nb, unsigned long action,
943 void *data)
944{
Minfei Huang36e505c2015-05-15 10:22:48 +0800945 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600946 struct module *mod = data;
947 struct klp_patch *patch;
948 struct klp_object *obj;
949
950 if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
951 return 0;
952
953 mutex_lock(&klp_mutex);
954
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100955 /*
956 * Each module has to know that the notifier has been called.
957 * We never know what module will get patched by a new patch.
958 */
959 if (action == MODULE_STATE_COMING)
960 mod->klp_alive = true;
961 else /* MODULE_STATE_GOING */
962 mod->klp_alive = false;
963
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600964 list_for_each_entry(patch, &klp_patches, list) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200965 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600966 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
967 continue;
968
969 if (action == MODULE_STATE_COMING) {
970 obj->mod = mod;
Minfei Huang36e505c2015-05-15 10:22:48 +0800971 ret = klp_module_notify_coming(patch, obj);
972 if (ret) {
973 obj->mod = NULL;
974 pr_warn("patch '%s' is in an inconsistent state!\n",
975 patch->mod->name);
976 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600977 } else /* MODULE_STATE_GOING */
978 klp_module_notify_going(patch, obj);
979
980 break;
981 }
982 }
983
984 mutex_unlock(&klp_mutex);
985
986 return 0;
987}
988
989static struct notifier_block klp_module_nb = {
990 .notifier_call = klp_module_notify,
991 .priority = INT_MIN+1, /* called late but before ftrace notifier */
992};
993
Minfei Huang26029d82015-05-22 22:26:29 +0800994static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600995{
996 int ret;
997
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +0100998 ret = klp_check_compiler_support();
999 if (ret) {
1000 pr_info("Your compiler is too old; turning off.\n");
1001 return -EINVAL;
1002 }
1003
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001004 ret = register_module_notifier(&klp_module_nb);
1005 if (ret)
1006 return ret;
1007
1008 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
1009 if (!klp_root_kobj) {
1010 ret = -ENOMEM;
1011 goto unregister;
1012 }
1013
1014 return 0;
1015
1016unregister:
1017 unregister_module_notifier(&klp_module_nb);
1018 return ret;
1019}
1020
1021module_init(klp_init);