xref: /linux-6.15/kernel/livepatch/core.c (revision e0b561ee)
1b700e7f0SSeth Jennings /*
2b700e7f0SSeth Jennings  * core.c - Kernel Live Patching Core
3b700e7f0SSeth Jennings  *
4b700e7f0SSeth Jennings  * Copyright (C) 2014 Seth Jennings <[email protected]>
5b700e7f0SSeth Jennings  * Copyright (C) 2014 SUSE
6b700e7f0SSeth Jennings  *
7b700e7f0SSeth Jennings  * This program is free software; you can redistribute it and/or
8b700e7f0SSeth Jennings  * modify it under the terms of the GNU General Public License
9b700e7f0SSeth Jennings  * as published by the Free Software Foundation; either version 2
10b700e7f0SSeth Jennings  * of the License, or (at your option) any later version.
11b700e7f0SSeth Jennings  *
12b700e7f0SSeth Jennings  * This program is distributed in the hope that it will be useful,
13b700e7f0SSeth Jennings  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14b700e7f0SSeth Jennings  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15b700e7f0SSeth Jennings  * GNU General Public License for more details.
16b700e7f0SSeth Jennings  *
17b700e7f0SSeth Jennings  * You should have received a copy of the GNU General Public License
18b700e7f0SSeth Jennings  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19b700e7f0SSeth Jennings  */
20b700e7f0SSeth Jennings 
21b700e7f0SSeth Jennings #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22b700e7f0SSeth Jennings 
23b700e7f0SSeth Jennings #include <linux/module.h>
24b700e7f0SSeth Jennings #include <linux/kernel.h>
25b700e7f0SSeth Jennings #include <linux/mutex.h>
26b700e7f0SSeth Jennings #include <linux/slab.h>
27b700e7f0SSeth Jennings #include <linux/ftrace.h>
28b700e7f0SSeth Jennings #include <linux/list.h>
29b700e7f0SSeth Jennings #include <linux/kallsyms.h>
30b700e7f0SSeth Jennings #include <linux/livepatch.h>
31b700e7f0SSeth Jennings 
323c33f5b9SJosh Poimboeuf /**
333c33f5b9SJosh Poimboeuf  * struct klp_ops - structure for tracking registered ftrace ops structs
343c33f5b9SJosh Poimboeuf  *
353c33f5b9SJosh Poimboeuf  * A single ftrace_ops is shared between all enabled replacement functions
363c33f5b9SJosh Poimboeuf  * (klp_func structs) which have the same old_addr.  This allows the switch
373c33f5b9SJosh Poimboeuf  * between function versions to happen instantaneously by updating the klp_ops
383c33f5b9SJosh Poimboeuf  * struct's func_stack list.  The winner is the klp_func at the top of the
393c33f5b9SJosh Poimboeuf  * func_stack (front of the list).
403c33f5b9SJosh Poimboeuf  *
413c33f5b9SJosh Poimboeuf  * @node:	node for the global klp_ops list
423c33f5b9SJosh Poimboeuf  * @func_stack:	list head for the stack of klp_func's (active func is on top)
433c33f5b9SJosh Poimboeuf  * @fops:	registered ftrace ops struct
44b700e7f0SSeth Jennings  */
453c33f5b9SJosh Poimboeuf struct klp_ops {
463c33f5b9SJosh Poimboeuf 	struct list_head node;
473c33f5b9SJosh Poimboeuf 	struct list_head func_stack;
483c33f5b9SJosh Poimboeuf 	struct ftrace_ops fops;
493c33f5b9SJosh Poimboeuf };
50b700e7f0SSeth Jennings 
513c33f5b9SJosh Poimboeuf /*
523c33f5b9SJosh Poimboeuf  * The klp_mutex protects the global lists and state transitions of any
533c33f5b9SJosh Poimboeuf  * structure reachable from them.  References to any structure must be obtained
543c33f5b9SJosh Poimboeuf  * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
553c33f5b9SJosh Poimboeuf  * ensure it gets consistent data).
563c33f5b9SJosh Poimboeuf  */
57b700e7f0SSeth Jennings static DEFINE_MUTEX(klp_mutex);
583c33f5b9SJosh Poimboeuf 
59b700e7f0SSeth Jennings static LIST_HEAD(klp_patches);
603c33f5b9SJosh Poimboeuf static LIST_HEAD(klp_ops);
61b700e7f0SSeth Jennings 
62b700e7f0SSeth Jennings static struct kobject *klp_root_kobj;
63b700e7f0SSeth Jennings 
643c33f5b9SJosh Poimboeuf static struct klp_ops *klp_find_ops(unsigned long old_addr)
653c33f5b9SJosh Poimboeuf {
663c33f5b9SJosh Poimboeuf 	struct klp_ops *ops;
673c33f5b9SJosh Poimboeuf 	struct klp_func *func;
683c33f5b9SJosh Poimboeuf 
693c33f5b9SJosh Poimboeuf 	list_for_each_entry(ops, &klp_ops, node) {
703c33f5b9SJosh Poimboeuf 		func = list_first_entry(&ops->func_stack, struct klp_func,
713c33f5b9SJosh Poimboeuf 					stack_node);
723c33f5b9SJosh Poimboeuf 		if (func->old_addr == old_addr)
733c33f5b9SJosh Poimboeuf 			return ops;
743c33f5b9SJosh Poimboeuf 	}
753c33f5b9SJosh Poimboeuf 
763c33f5b9SJosh Poimboeuf 	return NULL;
773c33f5b9SJosh Poimboeuf }
783c33f5b9SJosh Poimboeuf 
79b700e7f0SSeth Jennings static bool klp_is_module(struct klp_object *obj)
80b700e7f0SSeth Jennings {
81b700e7f0SSeth Jennings 	return obj->name;
82b700e7f0SSeth Jennings }
83b700e7f0SSeth Jennings 
84b700e7f0SSeth Jennings static bool klp_is_object_loaded(struct klp_object *obj)
85b700e7f0SSeth Jennings {
86b700e7f0SSeth Jennings 	return !obj->name || obj->mod;
87b700e7f0SSeth Jennings }
88b700e7f0SSeth Jennings 
89b700e7f0SSeth Jennings /* sets obj->mod if object is not vmlinux and module is found */
90b700e7f0SSeth Jennings static void klp_find_object_module(struct klp_object *obj)
91b700e7f0SSeth Jennings {
92b700e7f0SSeth Jennings 	if (!klp_is_module(obj))
93b700e7f0SSeth Jennings 		return;
94b700e7f0SSeth Jennings 
95b700e7f0SSeth Jennings 	mutex_lock(&module_mutex);
96b700e7f0SSeth Jennings 	/*
97b700e7f0SSeth Jennings 	 * We don't need to take a reference on the module here because we have
98b700e7f0SSeth Jennings 	 * the klp_mutex, which is also taken by the module notifier.  This
99b700e7f0SSeth Jennings 	 * prevents any module from unloading until we release the klp_mutex.
100b700e7f0SSeth Jennings 	 */
101b700e7f0SSeth Jennings 	obj->mod = find_module(obj->name);
102b700e7f0SSeth Jennings 	mutex_unlock(&module_mutex);
103b700e7f0SSeth Jennings }
104b700e7f0SSeth Jennings 
105b700e7f0SSeth Jennings /* klp_mutex must be held by caller */
106b700e7f0SSeth Jennings static bool klp_is_patch_registered(struct klp_patch *patch)
107b700e7f0SSeth Jennings {
108b700e7f0SSeth Jennings 	struct klp_patch *mypatch;
109b700e7f0SSeth Jennings 
110b700e7f0SSeth Jennings 	list_for_each_entry(mypatch, &klp_patches, list)
111b700e7f0SSeth Jennings 		if (mypatch == patch)
112b700e7f0SSeth Jennings 			return true;
113b700e7f0SSeth Jennings 
114b700e7f0SSeth Jennings 	return false;
115b700e7f0SSeth Jennings }
116b700e7f0SSeth Jennings 
117b700e7f0SSeth Jennings static bool klp_initialized(void)
118b700e7f0SSeth Jennings {
119b700e7f0SSeth Jennings 	return klp_root_kobj;
120b700e7f0SSeth Jennings }
121b700e7f0SSeth Jennings 
122b700e7f0SSeth Jennings struct klp_find_arg {
123b700e7f0SSeth Jennings 	const char *objname;
124b700e7f0SSeth Jennings 	const char *name;
125b700e7f0SSeth Jennings 	unsigned long addr;
126b700e7f0SSeth Jennings 	/*
127b700e7f0SSeth Jennings 	 * If count == 0, the symbol was not found. If count == 1, a unique
128b700e7f0SSeth Jennings 	 * match was found and addr is set.  If count > 1, there is
129b700e7f0SSeth Jennings 	 * unresolvable ambiguity among "count" number of symbols with the same
130b700e7f0SSeth Jennings 	 * name in the same object.
131b700e7f0SSeth Jennings 	 */
132b700e7f0SSeth Jennings 	unsigned long count;
133b700e7f0SSeth Jennings };
134b700e7f0SSeth Jennings 
135b700e7f0SSeth Jennings static int klp_find_callback(void *data, const char *name,
136b700e7f0SSeth Jennings 			     struct module *mod, unsigned long addr)
137b700e7f0SSeth Jennings {
138b700e7f0SSeth Jennings 	struct klp_find_arg *args = data;
139b700e7f0SSeth Jennings 
140b700e7f0SSeth Jennings 	if ((mod && !args->objname) || (!mod && args->objname))
141b700e7f0SSeth Jennings 		return 0;
142b700e7f0SSeth Jennings 
143b700e7f0SSeth Jennings 	if (strcmp(args->name, name))
144b700e7f0SSeth Jennings 		return 0;
145b700e7f0SSeth Jennings 
146b700e7f0SSeth Jennings 	if (args->objname && strcmp(args->objname, mod->name))
147b700e7f0SSeth Jennings 		return 0;
148b700e7f0SSeth Jennings 
149b700e7f0SSeth Jennings 	/*
150b700e7f0SSeth Jennings 	 * args->addr might be overwritten if another match is found
151b700e7f0SSeth Jennings 	 * but klp_find_object_symbol() handles this and only returns the
152b700e7f0SSeth Jennings 	 * addr if count == 1.
153b700e7f0SSeth Jennings 	 */
154b700e7f0SSeth Jennings 	args->addr = addr;
155b700e7f0SSeth Jennings 	args->count++;
156b700e7f0SSeth Jennings 
157b700e7f0SSeth Jennings 	return 0;
158b700e7f0SSeth Jennings }
159b700e7f0SSeth Jennings 
160b700e7f0SSeth Jennings static int klp_find_object_symbol(const char *objname, const char *name,
161b700e7f0SSeth Jennings 				  unsigned long *addr)
162b700e7f0SSeth Jennings {
163b700e7f0SSeth Jennings 	struct klp_find_arg args = {
164b700e7f0SSeth Jennings 		.objname = objname,
165b700e7f0SSeth Jennings 		.name = name,
166b700e7f0SSeth Jennings 		.addr = 0,
167b700e7f0SSeth Jennings 		.count = 0
168b700e7f0SSeth Jennings 	};
169b700e7f0SSeth Jennings 
170b700e7f0SSeth Jennings 	kallsyms_on_each_symbol(klp_find_callback, &args);
171b700e7f0SSeth Jennings 
172b700e7f0SSeth Jennings 	if (args.count == 0)
173b700e7f0SSeth Jennings 		pr_err("symbol '%s' not found in symbol table\n", name);
174b700e7f0SSeth Jennings 	else if (args.count > 1)
175b700e7f0SSeth Jennings 		pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
176b700e7f0SSeth Jennings 		       args.count, name, objname);
177b700e7f0SSeth Jennings 	else {
178b700e7f0SSeth Jennings 		*addr = args.addr;
179b700e7f0SSeth Jennings 		return 0;
180b700e7f0SSeth Jennings 	}
181b700e7f0SSeth Jennings 
182b700e7f0SSeth Jennings 	*addr = 0;
183b700e7f0SSeth Jennings 	return -EINVAL;
184b700e7f0SSeth Jennings }
185b700e7f0SSeth Jennings 
186b700e7f0SSeth Jennings struct klp_verify_args {
187b700e7f0SSeth Jennings 	const char *name;
188b700e7f0SSeth Jennings 	const unsigned long addr;
189b700e7f0SSeth Jennings };
190b700e7f0SSeth Jennings 
191b700e7f0SSeth Jennings static int klp_verify_callback(void *data, const char *name,
192b700e7f0SSeth Jennings 			       struct module *mod, unsigned long addr)
193b700e7f0SSeth Jennings {
194b700e7f0SSeth Jennings 	struct klp_verify_args *args = data;
195b700e7f0SSeth Jennings 
196b700e7f0SSeth Jennings 	if (!mod &&
197b700e7f0SSeth Jennings 	    !strcmp(args->name, name) &&
198b700e7f0SSeth Jennings 	    args->addr == addr)
199b700e7f0SSeth Jennings 		return 1;
200b700e7f0SSeth Jennings 
201b700e7f0SSeth Jennings 	return 0;
202b700e7f0SSeth Jennings }
203b700e7f0SSeth Jennings 
204b700e7f0SSeth Jennings static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
205b700e7f0SSeth Jennings {
206b700e7f0SSeth Jennings 	struct klp_verify_args args = {
207b700e7f0SSeth Jennings 		.name = name,
208b700e7f0SSeth Jennings 		.addr = addr,
209b700e7f0SSeth Jennings 	};
210b700e7f0SSeth Jennings 
211b700e7f0SSeth Jennings 	if (kallsyms_on_each_symbol(klp_verify_callback, &args))
212b700e7f0SSeth Jennings 		return 0;
213b700e7f0SSeth Jennings 
214f638f4dcSJosh Poimboeuf 	pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?\n",
215b700e7f0SSeth Jennings 		name, addr);
216b700e7f0SSeth Jennings 	return -EINVAL;
217b700e7f0SSeth Jennings }
218b700e7f0SSeth Jennings 
219b700e7f0SSeth Jennings static int klp_find_verify_func_addr(struct klp_object *obj,
220b700e7f0SSeth Jennings 				     struct klp_func *func)
221b700e7f0SSeth Jennings {
222b700e7f0SSeth Jennings 	int ret;
223b700e7f0SSeth Jennings 
224b700e7f0SSeth Jennings #if defined(CONFIG_RANDOMIZE_BASE)
225b700e7f0SSeth Jennings 	/* KASLR is enabled, disregard old_addr from user */
226b700e7f0SSeth Jennings 	func->old_addr = 0;
227b700e7f0SSeth Jennings #endif
228b700e7f0SSeth Jennings 
229b700e7f0SSeth Jennings 	if (!func->old_addr || klp_is_module(obj))
230b700e7f0SSeth Jennings 		ret = klp_find_object_symbol(obj->name, func->old_name,
231b700e7f0SSeth Jennings 					     &func->old_addr);
232b700e7f0SSeth Jennings 	else
233b700e7f0SSeth Jennings 		ret = klp_verify_vmlinux_symbol(func->old_name,
234b700e7f0SSeth Jennings 						func->old_addr);
235b700e7f0SSeth Jennings 
236b700e7f0SSeth Jennings 	return ret;
237b700e7f0SSeth Jennings }
238b700e7f0SSeth Jennings 
239b700e7f0SSeth Jennings /*
240b700e7f0SSeth Jennings  * external symbols are located outside the parent object (where the parent
241b700e7f0SSeth Jennings  * object is either vmlinux or the kmod being patched).
242b700e7f0SSeth Jennings  */
243b700e7f0SSeth Jennings static int klp_find_external_symbol(struct module *pmod, const char *name,
244b700e7f0SSeth Jennings 				    unsigned long *addr)
245b700e7f0SSeth Jennings {
246b700e7f0SSeth Jennings 	const struct kernel_symbol *sym;
247b700e7f0SSeth Jennings 
248b700e7f0SSeth Jennings 	/* first, check if it's an exported symbol */
249b700e7f0SSeth Jennings 	preempt_disable();
250b700e7f0SSeth Jennings 	sym = find_symbol(name, NULL, NULL, true, true);
251b700e7f0SSeth Jennings 	preempt_enable();
252b700e7f0SSeth Jennings 	if (sym) {
253b700e7f0SSeth Jennings 		*addr = sym->value;
254b700e7f0SSeth Jennings 		return 0;
255b700e7f0SSeth Jennings 	}
256b700e7f0SSeth Jennings 
257b700e7f0SSeth Jennings 	/* otherwise check if it's in another .o within the patch module */
258b700e7f0SSeth Jennings 	return klp_find_object_symbol(pmod->name, name, addr);
259b700e7f0SSeth Jennings }
260b700e7f0SSeth Jennings 
261b700e7f0SSeth Jennings static int klp_write_object_relocations(struct module *pmod,
262b700e7f0SSeth Jennings 					struct klp_object *obj)
263b700e7f0SSeth Jennings {
264b700e7f0SSeth Jennings 	int ret;
265b700e7f0SSeth Jennings 	struct klp_reloc *reloc;
266b700e7f0SSeth Jennings 
267b700e7f0SSeth Jennings 	if (WARN_ON(!klp_is_object_loaded(obj)))
268b700e7f0SSeth Jennings 		return -EINVAL;
269b700e7f0SSeth Jennings 
270b700e7f0SSeth Jennings 	if (WARN_ON(!obj->relocs))
271b700e7f0SSeth Jennings 		return -EINVAL;
272b700e7f0SSeth Jennings 
273b700e7f0SSeth Jennings 	for (reloc = obj->relocs; reloc->name; reloc++) {
274b700e7f0SSeth Jennings 		if (!klp_is_module(obj)) {
275b700e7f0SSeth Jennings 			ret = klp_verify_vmlinux_symbol(reloc->name,
276b700e7f0SSeth Jennings 							reloc->val);
277b700e7f0SSeth Jennings 			if (ret)
278b700e7f0SSeth Jennings 				return ret;
279b700e7f0SSeth Jennings 		} else {
280b700e7f0SSeth Jennings 			/* module, reloc->val needs to be discovered */
281b700e7f0SSeth Jennings 			if (reloc->external)
282b700e7f0SSeth Jennings 				ret = klp_find_external_symbol(pmod,
283b700e7f0SSeth Jennings 							       reloc->name,
284b700e7f0SSeth Jennings 							       &reloc->val);
285b700e7f0SSeth Jennings 			else
286b700e7f0SSeth Jennings 				ret = klp_find_object_symbol(obj->mod->name,
287b700e7f0SSeth Jennings 							     reloc->name,
288b700e7f0SSeth Jennings 							     &reloc->val);
289b700e7f0SSeth Jennings 			if (ret)
290b700e7f0SSeth Jennings 				return ret;
291b700e7f0SSeth Jennings 		}
292b700e7f0SSeth Jennings 		ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
293b700e7f0SSeth Jennings 					     reloc->val + reloc->addend);
294b700e7f0SSeth Jennings 		if (ret) {
295b700e7f0SSeth Jennings 			pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
296b700e7f0SSeth Jennings 			       reloc->name, reloc->val, ret);
297b700e7f0SSeth Jennings 			return ret;
298b700e7f0SSeth Jennings 		}
299b700e7f0SSeth Jennings 	}
300b700e7f0SSeth Jennings 
301b700e7f0SSeth Jennings 	return 0;
302b700e7f0SSeth Jennings }
303b700e7f0SSeth Jennings 
304b700e7f0SSeth Jennings static void notrace klp_ftrace_handler(unsigned long ip,
305b700e7f0SSeth Jennings 				       unsigned long parent_ip,
3063c33f5b9SJosh Poimboeuf 				       struct ftrace_ops *fops,
307b700e7f0SSeth Jennings 				       struct pt_regs *regs)
308b700e7f0SSeth Jennings {
3093c33f5b9SJosh Poimboeuf 	struct klp_ops *ops;
3103c33f5b9SJosh Poimboeuf 	struct klp_func *func;
3113c33f5b9SJosh Poimboeuf 
3123c33f5b9SJosh Poimboeuf 	ops = container_of(fops, struct klp_ops, fops);
3133c33f5b9SJosh Poimboeuf 
3143c33f5b9SJosh Poimboeuf 	rcu_read_lock();
3153c33f5b9SJosh Poimboeuf 	func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
3163c33f5b9SJosh Poimboeuf 				      stack_node);
3173c33f5b9SJosh Poimboeuf 	rcu_read_unlock();
3183c33f5b9SJosh Poimboeuf 
3193c33f5b9SJosh Poimboeuf 	if (WARN_ON_ONCE(!func))
3203c33f5b9SJosh Poimboeuf 		return;
321b700e7f0SSeth Jennings 
322b5bfc517SLi Bin 	klp_arch_set_pc(regs, (unsigned long)func->new_func);
323b700e7f0SSeth Jennings }
324b700e7f0SSeth Jennings 
325b700e7f0SSeth Jennings static int klp_disable_func(struct klp_func *func)
326b700e7f0SSeth Jennings {
3273c33f5b9SJosh Poimboeuf 	struct klp_ops *ops;
328b700e7f0SSeth Jennings 	int ret;
329b700e7f0SSeth Jennings 
330b700e7f0SSeth Jennings 	if (WARN_ON(func->state != KLP_ENABLED))
331b700e7f0SSeth Jennings 		return -EINVAL;
332b700e7f0SSeth Jennings 
333b700e7f0SSeth Jennings 	if (WARN_ON(!func->old_addr))
334b700e7f0SSeth Jennings 		return -EINVAL;
335b700e7f0SSeth Jennings 
3363c33f5b9SJosh Poimboeuf 	ops = klp_find_ops(func->old_addr);
3373c33f5b9SJosh Poimboeuf 	if (WARN_ON(!ops))
3383c33f5b9SJosh Poimboeuf 		return -EINVAL;
3393c33f5b9SJosh Poimboeuf 
3403c33f5b9SJosh Poimboeuf 	if (list_is_singular(&ops->func_stack)) {
3413c33f5b9SJosh Poimboeuf 		ret = unregister_ftrace_function(&ops->fops);
342b700e7f0SSeth Jennings 		if (ret) {
343b700e7f0SSeth Jennings 			pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
344b700e7f0SSeth Jennings 			       func->old_name, ret);
345b700e7f0SSeth Jennings 			return ret;
346b700e7f0SSeth Jennings 		}
347b700e7f0SSeth Jennings 
3483c33f5b9SJosh Poimboeuf 		ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
349b700e7f0SSeth Jennings 		if (ret)
350b700e7f0SSeth Jennings 			pr_warn("function unregister succeeded but failed to clear the filter\n");
351b700e7f0SSeth Jennings 
3523c33f5b9SJosh Poimboeuf 		list_del_rcu(&func->stack_node);
3533c33f5b9SJosh Poimboeuf 		list_del(&ops->node);
3543c33f5b9SJosh Poimboeuf 		kfree(ops);
3553c33f5b9SJosh Poimboeuf 	} else {
3563c33f5b9SJosh Poimboeuf 		list_del_rcu(&func->stack_node);
3573c33f5b9SJosh Poimboeuf 	}
3583c33f5b9SJosh Poimboeuf 
359b700e7f0SSeth Jennings 	func->state = KLP_DISABLED;
360b700e7f0SSeth Jennings 
361b700e7f0SSeth Jennings 	return 0;
362b700e7f0SSeth Jennings }
363b700e7f0SSeth Jennings 
364b700e7f0SSeth Jennings static int klp_enable_func(struct klp_func *func)
365b700e7f0SSeth Jennings {
3663c33f5b9SJosh Poimboeuf 	struct klp_ops *ops;
367b700e7f0SSeth Jennings 	int ret;
368b700e7f0SSeth Jennings 
369b700e7f0SSeth Jennings 	if (WARN_ON(!func->old_addr))
370b700e7f0SSeth Jennings 		return -EINVAL;
371b700e7f0SSeth Jennings 
372b700e7f0SSeth Jennings 	if (WARN_ON(func->state != KLP_DISABLED))
373b700e7f0SSeth Jennings 		return -EINVAL;
374b700e7f0SSeth Jennings 
3753c33f5b9SJosh Poimboeuf 	ops = klp_find_ops(func->old_addr);
3763c33f5b9SJosh Poimboeuf 	if (!ops) {
3773c33f5b9SJosh Poimboeuf 		ops = kzalloc(sizeof(*ops), GFP_KERNEL);
3783c33f5b9SJosh Poimboeuf 		if (!ops)
3793c33f5b9SJosh Poimboeuf 			return -ENOMEM;
3803c33f5b9SJosh Poimboeuf 
3813c33f5b9SJosh Poimboeuf 		ops->fops.func = klp_ftrace_handler;
3823c33f5b9SJosh Poimboeuf 		ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
3833c33f5b9SJosh Poimboeuf 				  FTRACE_OPS_FL_DYNAMIC |
3843c33f5b9SJosh Poimboeuf 				  FTRACE_OPS_FL_IPMODIFY;
3853c33f5b9SJosh Poimboeuf 
3863c33f5b9SJosh Poimboeuf 		list_add(&ops->node, &klp_ops);
3873c33f5b9SJosh Poimboeuf 
3883c33f5b9SJosh Poimboeuf 		INIT_LIST_HEAD(&ops->func_stack);
3893c33f5b9SJosh Poimboeuf 		list_add_rcu(&func->stack_node, &ops->func_stack);
3903c33f5b9SJosh Poimboeuf 
3913c33f5b9SJosh Poimboeuf 		ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 0, 0);
392b700e7f0SSeth Jennings 		if (ret) {
393b700e7f0SSeth Jennings 			pr_err("failed to set ftrace filter for function '%s' (%d)\n",
394b700e7f0SSeth Jennings 			       func->old_name, ret);
3953c33f5b9SJosh Poimboeuf 			goto err;
396b700e7f0SSeth Jennings 		}
397b700e7f0SSeth Jennings 
3983c33f5b9SJosh Poimboeuf 		ret = register_ftrace_function(&ops->fops);
399b700e7f0SSeth Jennings 		if (ret) {
400b700e7f0SSeth Jennings 			pr_err("failed to register ftrace handler for function '%s' (%d)\n",
401b700e7f0SSeth Jennings 			       func->old_name, ret);
4023c33f5b9SJosh Poimboeuf 			ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
4033c33f5b9SJosh Poimboeuf 			goto err;
404b700e7f0SSeth Jennings 		}
405b700e7f0SSeth Jennings 
4063c33f5b9SJosh Poimboeuf 
4073c33f5b9SJosh Poimboeuf 	} else {
4083c33f5b9SJosh Poimboeuf 		list_add_rcu(&func->stack_node, &ops->func_stack);
4093c33f5b9SJosh Poimboeuf 	}
4103c33f5b9SJosh Poimboeuf 
4113c33f5b9SJosh Poimboeuf 	func->state = KLP_ENABLED;
4123c33f5b9SJosh Poimboeuf 
413dbed7ddaSJosh Poimboeuf 	return 0;
4143c33f5b9SJosh Poimboeuf 
4153c33f5b9SJosh Poimboeuf err:
4163c33f5b9SJosh Poimboeuf 	list_del_rcu(&func->stack_node);
4173c33f5b9SJosh Poimboeuf 	list_del(&ops->node);
4183c33f5b9SJosh Poimboeuf 	kfree(ops);
419b700e7f0SSeth Jennings 	return ret;
420b700e7f0SSeth Jennings }
421b700e7f0SSeth Jennings 
422b700e7f0SSeth Jennings static int klp_disable_object(struct klp_object *obj)
423b700e7f0SSeth Jennings {
424b700e7f0SSeth Jennings 	struct klp_func *func;
425b700e7f0SSeth Jennings 	int ret;
426b700e7f0SSeth Jennings 
427b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name; func++) {
428b700e7f0SSeth Jennings 		if (func->state != KLP_ENABLED)
429b700e7f0SSeth Jennings 			continue;
430b700e7f0SSeth Jennings 
431b700e7f0SSeth Jennings 		ret = klp_disable_func(func);
432b700e7f0SSeth Jennings 		if (ret)
433b700e7f0SSeth Jennings 			return ret;
434b700e7f0SSeth Jennings 	}
435b700e7f0SSeth Jennings 
436b700e7f0SSeth Jennings 	obj->state = KLP_DISABLED;
437b700e7f0SSeth Jennings 
438b700e7f0SSeth Jennings 	return 0;
439b700e7f0SSeth Jennings }
440b700e7f0SSeth Jennings 
441b700e7f0SSeth Jennings static int klp_enable_object(struct klp_object *obj)
442b700e7f0SSeth Jennings {
443b700e7f0SSeth Jennings 	struct klp_func *func;
444b700e7f0SSeth Jennings 	int ret;
445b700e7f0SSeth Jennings 
446b700e7f0SSeth Jennings 	if (WARN_ON(obj->state != KLP_DISABLED))
447b700e7f0SSeth Jennings 		return -EINVAL;
448b700e7f0SSeth Jennings 
449b700e7f0SSeth Jennings 	if (WARN_ON(!klp_is_object_loaded(obj)))
450b700e7f0SSeth Jennings 		return -EINVAL;
451b700e7f0SSeth Jennings 
452b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name; func++) {
453b700e7f0SSeth Jennings 		ret = klp_enable_func(func);
454b700e7f0SSeth Jennings 		if (ret)
455b700e7f0SSeth Jennings 			goto unregister;
456b700e7f0SSeth Jennings 	}
457b700e7f0SSeth Jennings 	obj->state = KLP_ENABLED;
458b700e7f0SSeth Jennings 
459b700e7f0SSeth Jennings 	return 0;
460b700e7f0SSeth Jennings 
461b700e7f0SSeth Jennings unregister:
462b700e7f0SSeth Jennings 	WARN_ON(klp_disable_object(obj));
463b700e7f0SSeth Jennings 	return ret;
464b700e7f0SSeth Jennings }
465b700e7f0SSeth Jennings 
466b700e7f0SSeth Jennings static int __klp_disable_patch(struct klp_patch *patch)
467b700e7f0SSeth Jennings {
468b700e7f0SSeth Jennings 	struct klp_object *obj;
469b700e7f0SSeth Jennings 	int ret;
470b700e7f0SSeth Jennings 
47183a90bb1SJosh Poimboeuf 	/* enforce stacking: only the last enabled patch can be disabled */
47283a90bb1SJosh Poimboeuf 	if (!list_is_last(&patch->list, &klp_patches) &&
47383a90bb1SJosh Poimboeuf 	    list_next_entry(patch, list)->state == KLP_ENABLED)
47483a90bb1SJosh Poimboeuf 		return -EBUSY;
47583a90bb1SJosh Poimboeuf 
476b700e7f0SSeth Jennings 	pr_notice("disabling patch '%s'\n", patch->mod->name);
477b700e7f0SSeth Jennings 
478b700e7f0SSeth Jennings 	for (obj = patch->objs; obj->funcs; obj++) {
479b700e7f0SSeth Jennings 		if (obj->state != KLP_ENABLED)
480b700e7f0SSeth Jennings 			continue;
481b700e7f0SSeth Jennings 
482b700e7f0SSeth Jennings 		ret = klp_disable_object(obj);
483b700e7f0SSeth Jennings 		if (ret)
484b700e7f0SSeth Jennings 			return ret;
485b700e7f0SSeth Jennings 	}
486b700e7f0SSeth Jennings 
487b700e7f0SSeth Jennings 	patch->state = KLP_DISABLED;
488b700e7f0SSeth Jennings 
489b700e7f0SSeth Jennings 	return 0;
490b700e7f0SSeth Jennings }
491b700e7f0SSeth Jennings 
492b700e7f0SSeth Jennings /**
493b700e7f0SSeth Jennings  * klp_disable_patch() - disables a registered patch
494b700e7f0SSeth Jennings  * @patch:	The registered, enabled patch to be disabled
495b700e7f0SSeth Jennings  *
496b700e7f0SSeth Jennings  * Unregisters the patched functions from ftrace.
497b700e7f0SSeth Jennings  *
498b700e7f0SSeth Jennings  * Return: 0 on success, otherwise error
499b700e7f0SSeth Jennings  */
500b700e7f0SSeth Jennings int klp_disable_patch(struct klp_patch *patch)
501b700e7f0SSeth Jennings {
502b700e7f0SSeth Jennings 	int ret;
503b700e7f0SSeth Jennings 
504b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
505b700e7f0SSeth Jennings 
506b700e7f0SSeth Jennings 	if (!klp_is_patch_registered(patch)) {
507b700e7f0SSeth Jennings 		ret = -EINVAL;
508b700e7f0SSeth Jennings 		goto err;
509b700e7f0SSeth Jennings 	}
510b700e7f0SSeth Jennings 
511b700e7f0SSeth Jennings 	if (patch->state == KLP_DISABLED) {
512b700e7f0SSeth Jennings 		ret = -EINVAL;
513b700e7f0SSeth Jennings 		goto err;
514b700e7f0SSeth Jennings 	}
515b700e7f0SSeth Jennings 
516b700e7f0SSeth Jennings 	ret = __klp_disable_patch(patch);
517b700e7f0SSeth Jennings 
518b700e7f0SSeth Jennings err:
519b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
520b700e7f0SSeth Jennings 	return ret;
521b700e7f0SSeth Jennings }
522b700e7f0SSeth Jennings EXPORT_SYMBOL_GPL(klp_disable_patch);
523b700e7f0SSeth Jennings 
524b700e7f0SSeth Jennings static int __klp_enable_patch(struct klp_patch *patch)
525b700e7f0SSeth Jennings {
526b700e7f0SSeth Jennings 	struct klp_object *obj;
527b700e7f0SSeth Jennings 	int ret;
528b700e7f0SSeth Jennings 
529b700e7f0SSeth Jennings 	if (WARN_ON(patch->state != KLP_DISABLED))
530b700e7f0SSeth Jennings 		return -EINVAL;
531b700e7f0SSeth Jennings 
53283a90bb1SJosh Poimboeuf 	/* enforce stacking: only the first disabled patch can be enabled */
53383a90bb1SJosh Poimboeuf 	if (patch->list.prev != &klp_patches &&
53483a90bb1SJosh Poimboeuf 	    list_prev_entry(patch, list)->state == KLP_DISABLED)
53583a90bb1SJosh Poimboeuf 		return -EBUSY;
53683a90bb1SJosh Poimboeuf 
537b700e7f0SSeth Jennings 	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
538b700e7f0SSeth Jennings 	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
539b700e7f0SSeth Jennings 
540b700e7f0SSeth Jennings 	pr_notice("enabling patch '%s'\n", patch->mod->name);
541b700e7f0SSeth Jennings 
542b700e7f0SSeth Jennings 	for (obj = patch->objs; obj->funcs; obj++) {
543b700e7f0SSeth Jennings 		klp_find_object_module(obj);
544b700e7f0SSeth Jennings 
545b700e7f0SSeth Jennings 		if (!klp_is_object_loaded(obj))
546b700e7f0SSeth Jennings 			continue;
547b700e7f0SSeth Jennings 
548b700e7f0SSeth Jennings 		ret = klp_enable_object(obj);
549b700e7f0SSeth Jennings 		if (ret)
550b700e7f0SSeth Jennings 			goto unregister;
551b700e7f0SSeth Jennings 	}
552b700e7f0SSeth Jennings 
553b700e7f0SSeth Jennings 	patch->state = KLP_ENABLED;
554b700e7f0SSeth Jennings 
555b700e7f0SSeth Jennings 	return 0;
556b700e7f0SSeth Jennings 
557b700e7f0SSeth Jennings unregister:
558b700e7f0SSeth Jennings 	WARN_ON(__klp_disable_patch(patch));
559b700e7f0SSeth Jennings 	return ret;
560b700e7f0SSeth Jennings }
561b700e7f0SSeth Jennings 
562b700e7f0SSeth Jennings /**
563b700e7f0SSeth Jennings  * klp_enable_patch() - enables a registered patch
564b700e7f0SSeth Jennings  * @patch:	The registered, disabled patch to be enabled
565b700e7f0SSeth Jennings  *
566b700e7f0SSeth Jennings  * Performs the needed symbol lookups and code relocations,
567b700e7f0SSeth Jennings  * then registers the patched functions with ftrace.
568b700e7f0SSeth Jennings  *
569b700e7f0SSeth Jennings  * Return: 0 on success, otherwise error
570b700e7f0SSeth Jennings  */
571b700e7f0SSeth Jennings int klp_enable_patch(struct klp_patch *patch)
572b700e7f0SSeth Jennings {
573b700e7f0SSeth Jennings 	int ret;
574b700e7f0SSeth Jennings 
575b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
576b700e7f0SSeth Jennings 
577b700e7f0SSeth Jennings 	if (!klp_is_patch_registered(patch)) {
578b700e7f0SSeth Jennings 		ret = -EINVAL;
579b700e7f0SSeth Jennings 		goto err;
580b700e7f0SSeth Jennings 	}
581b700e7f0SSeth Jennings 
582b700e7f0SSeth Jennings 	ret = __klp_enable_patch(patch);
583b700e7f0SSeth Jennings 
584b700e7f0SSeth Jennings err:
585b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
586b700e7f0SSeth Jennings 	return ret;
587b700e7f0SSeth Jennings }
588b700e7f0SSeth Jennings EXPORT_SYMBOL_GPL(klp_enable_patch);
589b700e7f0SSeth Jennings 
590b700e7f0SSeth Jennings /*
591b700e7f0SSeth Jennings  * Sysfs Interface
592b700e7f0SSeth Jennings  *
593b700e7f0SSeth Jennings  * /sys/kernel/livepatch
594b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>
595b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/enabled
596b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/<object>
597b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/<object>/<func>
598b700e7f0SSeth Jennings  */
599b700e7f0SSeth Jennings 
600b700e7f0SSeth Jennings static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
601b700e7f0SSeth Jennings 			     const char *buf, size_t count)
602b700e7f0SSeth Jennings {
603b700e7f0SSeth Jennings 	struct klp_patch *patch;
604b700e7f0SSeth Jennings 	int ret;
605b700e7f0SSeth Jennings 	unsigned long val;
606b700e7f0SSeth Jennings 
607b700e7f0SSeth Jennings 	ret = kstrtoul(buf, 10, &val);
608b700e7f0SSeth Jennings 	if (ret)
609b700e7f0SSeth Jennings 		return -EINVAL;
610b700e7f0SSeth Jennings 
611b700e7f0SSeth Jennings 	if (val != KLP_DISABLED && val != KLP_ENABLED)
612b700e7f0SSeth Jennings 		return -EINVAL;
613b700e7f0SSeth Jennings 
614b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
615b700e7f0SSeth Jennings 
616b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
617b700e7f0SSeth Jennings 
618b700e7f0SSeth Jennings 	if (val == patch->state) {
619b700e7f0SSeth Jennings 		/* already in requested state */
620b700e7f0SSeth Jennings 		ret = -EINVAL;
621b700e7f0SSeth Jennings 		goto err;
622b700e7f0SSeth Jennings 	}
623b700e7f0SSeth Jennings 
624b700e7f0SSeth Jennings 	if (val == KLP_ENABLED) {
625b700e7f0SSeth Jennings 		ret = __klp_enable_patch(patch);
626b700e7f0SSeth Jennings 		if (ret)
627b700e7f0SSeth Jennings 			goto err;
628b700e7f0SSeth Jennings 	} else {
629b700e7f0SSeth Jennings 		ret = __klp_disable_patch(patch);
630b700e7f0SSeth Jennings 		if (ret)
631b700e7f0SSeth Jennings 			goto err;
632b700e7f0SSeth Jennings 	}
633b700e7f0SSeth Jennings 
634b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
635b700e7f0SSeth Jennings 
636b700e7f0SSeth Jennings 	return count;
637b700e7f0SSeth Jennings 
638b700e7f0SSeth Jennings err:
639b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
640b700e7f0SSeth Jennings 	return ret;
641b700e7f0SSeth Jennings }
642b700e7f0SSeth Jennings 
643b700e7f0SSeth Jennings static ssize_t enabled_show(struct kobject *kobj,
644b700e7f0SSeth Jennings 			    struct kobj_attribute *attr, char *buf)
645b700e7f0SSeth Jennings {
646b700e7f0SSeth Jennings 	struct klp_patch *patch;
647b700e7f0SSeth Jennings 
648b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
649b700e7f0SSeth Jennings 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
650b700e7f0SSeth Jennings }
651b700e7f0SSeth Jennings 
652b700e7f0SSeth Jennings static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
653b700e7f0SSeth Jennings static struct attribute *klp_patch_attrs[] = {
654b700e7f0SSeth Jennings 	&enabled_kobj_attr.attr,
655b700e7f0SSeth Jennings 	NULL
656b700e7f0SSeth Jennings };
657b700e7f0SSeth Jennings 
658b700e7f0SSeth Jennings static void klp_kobj_release_patch(struct kobject *kobj)
659b700e7f0SSeth Jennings {
660b700e7f0SSeth Jennings 	/*
661b700e7f0SSeth Jennings 	 * Once we have a consistency model we'll need to module_put() the
662b700e7f0SSeth Jennings 	 * patch module here.  See klp_register_patch() for more details.
663b700e7f0SSeth Jennings 	 */
664b700e7f0SSeth Jennings }
665b700e7f0SSeth Jennings 
666b700e7f0SSeth Jennings static struct kobj_type klp_ktype_patch = {
667b700e7f0SSeth Jennings 	.release = klp_kobj_release_patch,
668b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
669b700e7f0SSeth Jennings 	.default_attrs = klp_patch_attrs,
670b700e7f0SSeth Jennings };
671b700e7f0SSeth Jennings 
672b700e7f0SSeth Jennings static void klp_kobj_release_func(struct kobject *kobj)
673b700e7f0SSeth Jennings {
674b700e7f0SSeth Jennings }
675b700e7f0SSeth Jennings 
676b700e7f0SSeth Jennings static struct kobj_type klp_ktype_func = {
677b700e7f0SSeth Jennings 	.release = klp_kobj_release_func,
678b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
679b700e7f0SSeth Jennings };
680b700e7f0SSeth Jennings 
681b700e7f0SSeth Jennings /*
682b700e7f0SSeth Jennings  * Free all functions' kobjects in the array up to some limit. When limit is
683b700e7f0SSeth Jennings  * NULL, all kobjects are freed.
684b700e7f0SSeth Jennings  */
685b700e7f0SSeth Jennings static void klp_free_funcs_limited(struct klp_object *obj,
686b700e7f0SSeth Jennings 				   struct klp_func *limit)
687b700e7f0SSeth Jennings {
688b700e7f0SSeth Jennings 	struct klp_func *func;
689b700e7f0SSeth Jennings 
690b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name && func != limit; func++)
691b700e7f0SSeth Jennings 		kobject_put(&func->kobj);
692b700e7f0SSeth Jennings }
693b700e7f0SSeth Jennings 
694b700e7f0SSeth Jennings /* Clean up when a patched object is unloaded */
695b700e7f0SSeth Jennings static void klp_free_object_loaded(struct klp_object *obj)
696b700e7f0SSeth Jennings {
697b700e7f0SSeth Jennings 	struct klp_func *func;
698b700e7f0SSeth Jennings 
699b700e7f0SSeth Jennings 	obj->mod = NULL;
700b700e7f0SSeth Jennings 
701b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name; func++)
702b700e7f0SSeth Jennings 		func->old_addr = 0;
703b700e7f0SSeth Jennings }
704b700e7f0SSeth Jennings 
705b700e7f0SSeth Jennings /*
706b700e7f0SSeth Jennings  * Free all objects' kobjects in the array up to some limit. When limit is
707b700e7f0SSeth Jennings  * NULL, all kobjects are freed.
708b700e7f0SSeth Jennings  */
709b700e7f0SSeth Jennings static void klp_free_objects_limited(struct klp_patch *patch,
710b700e7f0SSeth Jennings 				     struct klp_object *limit)
711b700e7f0SSeth Jennings {
712b700e7f0SSeth Jennings 	struct klp_object *obj;
713b700e7f0SSeth Jennings 
714b700e7f0SSeth Jennings 	for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
715b700e7f0SSeth Jennings 		klp_free_funcs_limited(obj, NULL);
716b700e7f0SSeth Jennings 		kobject_put(obj->kobj);
717b700e7f0SSeth Jennings 	}
718b700e7f0SSeth Jennings }
719b700e7f0SSeth Jennings 
720b700e7f0SSeth Jennings static void klp_free_patch(struct klp_patch *patch)
721b700e7f0SSeth Jennings {
722b700e7f0SSeth Jennings 	klp_free_objects_limited(patch, NULL);
723b700e7f0SSeth Jennings 	if (!list_empty(&patch->list))
724b700e7f0SSeth Jennings 		list_del(&patch->list);
725b700e7f0SSeth Jennings 	kobject_put(&patch->kobj);
726b700e7f0SSeth Jennings }
727b700e7f0SSeth Jennings 
728b700e7f0SSeth Jennings static int klp_init_func(struct klp_object *obj, struct klp_func *func)
729b700e7f0SSeth Jennings {
7303c33f5b9SJosh Poimboeuf 	INIT_LIST_HEAD(&func->stack_node);
731b700e7f0SSeth Jennings 	func->state = KLP_DISABLED;
732b700e7f0SSeth Jennings 
7333c33f5b9SJosh Poimboeuf 	return kobject_init_and_add(&func->kobj, &klp_ktype_func,
734*e0b561eeSJiri Kosina 				    obj->kobj, "%s", func->old_name);
735b700e7f0SSeth Jennings }
736b700e7f0SSeth Jennings 
737b700e7f0SSeth Jennings /* parts of the initialization that is done only when the object is loaded */
738b700e7f0SSeth Jennings static int klp_init_object_loaded(struct klp_patch *patch,
739b700e7f0SSeth Jennings 				  struct klp_object *obj)
740b700e7f0SSeth Jennings {
741b700e7f0SSeth Jennings 	struct klp_func *func;
742b700e7f0SSeth Jennings 	int ret;
743b700e7f0SSeth Jennings 
744b700e7f0SSeth Jennings 	if (obj->relocs) {
745b700e7f0SSeth Jennings 		ret = klp_write_object_relocations(patch->mod, obj);
746b700e7f0SSeth Jennings 		if (ret)
747b700e7f0SSeth Jennings 			return ret;
748b700e7f0SSeth Jennings 	}
749b700e7f0SSeth Jennings 
750b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name; func++) {
751b700e7f0SSeth Jennings 		ret = klp_find_verify_func_addr(obj, func);
752b700e7f0SSeth Jennings 		if (ret)
753b700e7f0SSeth Jennings 			return ret;
754b700e7f0SSeth Jennings 	}
755b700e7f0SSeth Jennings 
756b700e7f0SSeth Jennings 	return 0;
757b700e7f0SSeth Jennings }
758b700e7f0SSeth Jennings 
759b700e7f0SSeth Jennings static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
760b700e7f0SSeth Jennings {
761b700e7f0SSeth Jennings 	struct klp_func *func;
762b700e7f0SSeth Jennings 	int ret;
763b700e7f0SSeth Jennings 	const char *name;
764b700e7f0SSeth Jennings 
765b700e7f0SSeth Jennings 	if (!obj->funcs)
766b700e7f0SSeth Jennings 		return -EINVAL;
767b700e7f0SSeth Jennings 
768b700e7f0SSeth Jennings 	obj->state = KLP_DISABLED;
769b700e7f0SSeth Jennings 
770b700e7f0SSeth Jennings 	klp_find_object_module(obj);
771b700e7f0SSeth Jennings 
772b700e7f0SSeth Jennings 	name = klp_is_module(obj) ? obj->name : "vmlinux";
773b700e7f0SSeth Jennings 	obj->kobj = kobject_create_and_add(name, &patch->kobj);
774b700e7f0SSeth Jennings 	if (!obj->kobj)
775b700e7f0SSeth Jennings 		return -ENOMEM;
776b700e7f0SSeth Jennings 
777b700e7f0SSeth Jennings 	for (func = obj->funcs; func->old_name; func++) {
778b700e7f0SSeth Jennings 		ret = klp_init_func(obj, func);
779b700e7f0SSeth Jennings 		if (ret)
780b700e7f0SSeth Jennings 			goto free;
781b700e7f0SSeth Jennings 	}
782b700e7f0SSeth Jennings 
783b700e7f0SSeth Jennings 	if (klp_is_object_loaded(obj)) {
784b700e7f0SSeth Jennings 		ret = klp_init_object_loaded(patch, obj);
785b700e7f0SSeth Jennings 		if (ret)
786b700e7f0SSeth Jennings 			goto free;
787b700e7f0SSeth Jennings 	}
788b700e7f0SSeth Jennings 
789b700e7f0SSeth Jennings 	return 0;
790b700e7f0SSeth Jennings 
791b700e7f0SSeth Jennings free:
792b700e7f0SSeth Jennings 	klp_free_funcs_limited(obj, func);
793b700e7f0SSeth Jennings 	kobject_put(obj->kobj);
794b700e7f0SSeth Jennings 	return ret;
795b700e7f0SSeth Jennings }
796b700e7f0SSeth Jennings 
797b700e7f0SSeth Jennings static int klp_init_patch(struct klp_patch *patch)
798b700e7f0SSeth Jennings {
799b700e7f0SSeth Jennings 	struct klp_object *obj;
800b700e7f0SSeth Jennings 	int ret;
801b700e7f0SSeth Jennings 
802b700e7f0SSeth Jennings 	if (!patch->objs)
803b700e7f0SSeth Jennings 		return -EINVAL;
804b700e7f0SSeth Jennings 
805b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
806b700e7f0SSeth Jennings 
807b700e7f0SSeth Jennings 	patch->state = KLP_DISABLED;
808b700e7f0SSeth Jennings 
809b700e7f0SSeth Jennings 	ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
810*e0b561eeSJiri Kosina 				   klp_root_kobj, "%s", patch->mod->name);
811b700e7f0SSeth Jennings 	if (ret)
812b700e7f0SSeth Jennings 		goto unlock;
813b700e7f0SSeth Jennings 
814b700e7f0SSeth Jennings 	for (obj = patch->objs; obj->funcs; obj++) {
815b700e7f0SSeth Jennings 		ret = klp_init_object(patch, obj);
816b700e7f0SSeth Jennings 		if (ret)
817b700e7f0SSeth Jennings 			goto free;
818b700e7f0SSeth Jennings 	}
819b700e7f0SSeth Jennings 
82099590ba5SJosh Poimboeuf 	list_add_tail(&patch->list, &klp_patches);
821b700e7f0SSeth Jennings 
822b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
823b700e7f0SSeth Jennings 
824b700e7f0SSeth Jennings 	return 0;
825b700e7f0SSeth Jennings 
826b700e7f0SSeth Jennings free:
827b700e7f0SSeth Jennings 	klp_free_objects_limited(patch, obj);
828b700e7f0SSeth Jennings 	kobject_put(&patch->kobj);
829b700e7f0SSeth Jennings unlock:
830b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
831b700e7f0SSeth Jennings 	return ret;
832b700e7f0SSeth Jennings }
833b700e7f0SSeth Jennings 
834b700e7f0SSeth Jennings /**
835b700e7f0SSeth Jennings  * klp_unregister_patch() - unregisters a patch
836b700e7f0SSeth Jennings  * @patch:	Disabled patch to be unregistered
837b700e7f0SSeth Jennings  *
838b700e7f0SSeth Jennings  * Frees the data structures and removes the sysfs interface.
839b700e7f0SSeth Jennings  *
840b700e7f0SSeth Jennings  * Return: 0 on success, otherwise error
841b700e7f0SSeth Jennings  */
842b700e7f0SSeth Jennings int klp_unregister_patch(struct klp_patch *patch)
843b700e7f0SSeth Jennings {
844b700e7f0SSeth Jennings 	int ret = 0;
845b700e7f0SSeth Jennings 
846b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
847b700e7f0SSeth Jennings 
848b700e7f0SSeth Jennings 	if (!klp_is_patch_registered(patch)) {
849b700e7f0SSeth Jennings 		ret = -EINVAL;
850b700e7f0SSeth Jennings 		goto out;
851b700e7f0SSeth Jennings 	}
852b700e7f0SSeth Jennings 
853b700e7f0SSeth Jennings 	if (patch->state == KLP_ENABLED) {
854b700e7f0SSeth Jennings 		ret = -EBUSY;
855b700e7f0SSeth Jennings 		goto out;
856b700e7f0SSeth Jennings 	}
857b700e7f0SSeth Jennings 
858b700e7f0SSeth Jennings 	klp_free_patch(patch);
859b700e7f0SSeth Jennings 
860b700e7f0SSeth Jennings out:
861b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
862b700e7f0SSeth Jennings 	return ret;
863b700e7f0SSeth Jennings }
864b700e7f0SSeth Jennings EXPORT_SYMBOL_GPL(klp_unregister_patch);
865b700e7f0SSeth Jennings 
866b700e7f0SSeth Jennings /**
867b700e7f0SSeth Jennings  * klp_register_patch() - registers a patch
868b700e7f0SSeth Jennings  * @patch:	Patch to be registered
869b700e7f0SSeth Jennings  *
870b700e7f0SSeth Jennings  * Initializes the data structure associated with the patch and
871b700e7f0SSeth Jennings  * creates the sysfs interface.
872b700e7f0SSeth Jennings  *
873b700e7f0SSeth Jennings  * Return: 0 on success, otherwise error
874b700e7f0SSeth Jennings  */
875b700e7f0SSeth Jennings int klp_register_patch(struct klp_patch *patch)
876b700e7f0SSeth Jennings {
877b700e7f0SSeth Jennings 	int ret;
878b700e7f0SSeth Jennings 
879b700e7f0SSeth Jennings 	if (!klp_initialized())
880b700e7f0SSeth Jennings 		return -ENODEV;
881b700e7f0SSeth Jennings 
882b700e7f0SSeth Jennings 	if (!patch || !patch->mod)
883b700e7f0SSeth Jennings 		return -EINVAL;
884b700e7f0SSeth Jennings 
885b700e7f0SSeth Jennings 	/*
886b700e7f0SSeth Jennings 	 * A reference is taken on the patch module to prevent it from being
887b700e7f0SSeth Jennings 	 * unloaded.  Right now, we don't allow patch modules to unload since
888b700e7f0SSeth Jennings 	 * there is currently no method to determine if a thread is still
889b700e7f0SSeth Jennings 	 * running in the patched code contained in the patch module once
890b700e7f0SSeth Jennings 	 * the ftrace registration is successful.
891b700e7f0SSeth Jennings 	 */
892b700e7f0SSeth Jennings 	if (!try_module_get(patch->mod))
893b700e7f0SSeth Jennings 		return -ENODEV;
894b700e7f0SSeth Jennings 
895b700e7f0SSeth Jennings 	ret = klp_init_patch(patch);
896b700e7f0SSeth Jennings 	if (ret)
897b700e7f0SSeth Jennings 		module_put(patch->mod);
898b700e7f0SSeth Jennings 
899b700e7f0SSeth Jennings 	return ret;
900b700e7f0SSeth Jennings }
901b700e7f0SSeth Jennings EXPORT_SYMBOL_GPL(klp_register_patch);
902b700e7f0SSeth Jennings 
903b700e7f0SSeth Jennings static void klp_module_notify_coming(struct klp_patch *patch,
904b700e7f0SSeth Jennings 				     struct klp_object *obj)
905b700e7f0SSeth Jennings {
906b700e7f0SSeth Jennings 	struct module *pmod = patch->mod;
907b700e7f0SSeth Jennings 	struct module *mod = obj->mod;
908b700e7f0SSeth Jennings 	int ret;
909b700e7f0SSeth Jennings 
910b700e7f0SSeth Jennings 	ret = klp_init_object_loaded(patch, obj);
911b700e7f0SSeth Jennings 	if (ret)
912b700e7f0SSeth Jennings 		goto err;
913b700e7f0SSeth Jennings 
914b700e7f0SSeth Jennings 	if (patch->state == KLP_DISABLED)
915b700e7f0SSeth Jennings 		return;
916b700e7f0SSeth Jennings 
917b700e7f0SSeth Jennings 	pr_notice("applying patch '%s' to loading module '%s'\n",
918b700e7f0SSeth Jennings 		  pmod->name, mod->name);
919b700e7f0SSeth Jennings 
920b700e7f0SSeth Jennings 	ret = klp_enable_object(obj);
921b700e7f0SSeth Jennings 	if (!ret)
922b700e7f0SSeth Jennings 		return;
923b700e7f0SSeth Jennings 
924b700e7f0SSeth Jennings err:
925b700e7f0SSeth Jennings 	pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
926b700e7f0SSeth Jennings 		pmod->name, mod->name, ret);
927b700e7f0SSeth Jennings }
928b700e7f0SSeth Jennings 
929b700e7f0SSeth Jennings static void klp_module_notify_going(struct klp_patch *patch,
930b700e7f0SSeth Jennings 				    struct klp_object *obj)
931b700e7f0SSeth Jennings {
932b700e7f0SSeth Jennings 	struct module *pmod = patch->mod;
933b700e7f0SSeth Jennings 	struct module *mod = obj->mod;
934b700e7f0SSeth Jennings 	int ret;
935b700e7f0SSeth Jennings 
936b700e7f0SSeth Jennings 	if (patch->state == KLP_DISABLED)
937b700e7f0SSeth Jennings 		goto disabled;
938b700e7f0SSeth Jennings 
939b700e7f0SSeth Jennings 	pr_notice("reverting patch '%s' on unloading module '%s'\n",
940b700e7f0SSeth Jennings 		  pmod->name, mod->name);
941b700e7f0SSeth Jennings 
942b700e7f0SSeth Jennings 	ret = klp_disable_object(obj);
943b700e7f0SSeth Jennings 	if (ret)
944b700e7f0SSeth Jennings 		pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
945b700e7f0SSeth Jennings 			pmod->name, mod->name, ret);
946b700e7f0SSeth Jennings 
947b700e7f0SSeth Jennings disabled:
948b700e7f0SSeth Jennings 	klp_free_object_loaded(obj);
949b700e7f0SSeth Jennings }
950b700e7f0SSeth Jennings 
951b700e7f0SSeth Jennings static int klp_module_notify(struct notifier_block *nb, unsigned long action,
952b700e7f0SSeth Jennings 			     void *data)
953b700e7f0SSeth Jennings {
954b700e7f0SSeth Jennings 	struct module *mod = data;
955b700e7f0SSeth Jennings 	struct klp_patch *patch;
956b700e7f0SSeth Jennings 	struct klp_object *obj;
957b700e7f0SSeth Jennings 
958b700e7f0SSeth Jennings 	if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
959b700e7f0SSeth Jennings 		return 0;
960b700e7f0SSeth Jennings 
961b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
962b700e7f0SSeth Jennings 
963b700e7f0SSeth Jennings 	list_for_each_entry(patch, &klp_patches, list) {
964b700e7f0SSeth Jennings 		for (obj = patch->objs; obj->funcs; obj++) {
965b700e7f0SSeth Jennings 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
966b700e7f0SSeth Jennings 				continue;
967b700e7f0SSeth Jennings 
968b700e7f0SSeth Jennings 			if (action == MODULE_STATE_COMING) {
969b700e7f0SSeth Jennings 				obj->mod = mod;
970b700e7f0SSeth Jennings 				klp_module_notify_coming(patch, obj);
971b700e7f0SSeth Jennings 			} else /* MODULE_STATE_GOING */
972b700e7f0SSeth Jennings 				klp_module_notify_going(patch, obj);
973b700e7f0SSeth Jennings 
974b700e7f0SSeth Jennings 			break;
975b700e7f0SSeth Jennings 		}
976b700e7f0SSeth Jennings 	}
977b700e7f0SSeth Jennings 
978b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
979b700e7f0SSeth Jennings 
980b700e7f0SSeth Jennings 	return 0;
981b700e7f0SSeth Jennings }
982b700e7f0SSeth Jennings 
983b700e7f0SSeth Jennings static struct notifier_block klp_module_nb = {
984b700e7f0SSeth Jennings 	.notifier_call = klp_module_notify,
985b700e7f0SSeth Jennings 	.priority = INT_MIN+1, /* called late but before ftrace notifier */
986b700e7f0SSeth Jennings };
987b700e7f0SSeth Jennings 
988b700e7f0SSeth Jennings static int klp_init(void)
989b700e7f0SSeth Jennings {
990b700e7f0SSeth Jennings 	int ret;
991b700e7f0SSeth Jennings 
992b9dfe0beSJiri Kosina 	ret = klp_check_compiler_support();
993b9dfe0beSJiri Kosina 	if (ret) {
994b9dfe0beSJiri Kosina 		pr_info("Your compiler is too old; turning off.\n");
995b9dfe0beSJiri Kosina 		return -EINVAL;
996b9dfe0beSJiri Kosina 	}
997b9dfe0beSJiri Kosina 
998b700e7f0SSeth Jennings 	ret = register_module_notifier(&klp_module_nb);
999b700e7f0SSeth Jennings 	if (ret)
1000b700e7f0SSeth Jennings 		return ret;
1001b700e7f0SSeth Jennings 
1002b700e7f0SSeth Jennings 	klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
1003b700e7f0SSeth Jennings 	if (!klp_root_kobj) {
1004b700e7f0SSeth Jennings 		ret = -ENOMEM;
1005b700e7f0SSeth Jennings 		goto unregister;
1006b700e7f0SSeth Jennings 	}
1007b700e7f0SSeth Jennings 
1008b700e7f0SSeth Jennings 	return 0;
1009b700e7f0SSeth Jennings 
1010b700e7f0SSeth Jennings unregister:
1011b700e7f0SSeth Jennings 	unregister_module_notifier(&klp_module_nb);
1012b700e7f0SSeth Jennings 	return ret;
1013b700e7f0SSeth Jennings }
1014b700e7f0SSeth Jennings 
1015b700e7f0SSeth Jennings module_init(klp_init);
1016