xref: /linux/kernel/livepatch/patch.c (revision d19ad0775dcd64b49eecf4fa79c17959ebfbd26b)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c349cdcaSJosh Poimboeuf /*
3c349cdcaSJosh Poimboeuf  * patch.c - livepatch patching functions
4c349cdcaSJosh Poimboeuf  *
5c349cdcaSJosh Poimboeuf  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
6c349cdcaSJosh Poimboeuf  * Copyright (C) 2014 SUSE
7c349cdcaSJosh Poimboeuf  * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
8c349cdcaSJosh Poimboeuf  */
9c349cdcaSJosh Poimboeuf 
10c349cdcaSJosh Poimboeuf #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11c349cdcaSJosh Poimboeuf 
12c349cdcaSJosh Poimboeuf #include <linux/livepatch.h>
13c349cdcaSJosh Poimboeuf #include <linux/list.h>
14c349cdcaSJosh Poimboeuf #include <linux/ftrace.h>
15c349cdcaSJosh Poimboeuf #include <linux/rculist.h>
16c349cdcaSJosh Poimboeuf #include <linux/slab.h>
17c349cdcaSJosh Poimboeuf #include <linux/bug.h>
18c349cdcaSJosh Poimboeuf #include <linux/printk.h>
1993862e38SJoe Lawrence #include "core.h"
20c349cdcaSJosh Poimboeuf #include "patch.h"
21d83a7cb3SJosh Poimboeuf #include "transition.h"
22c349cdcaSJosh Poimboeuf 
23c349cdcaSJosh Poimboeuf static LIST_HEAD(klp_ops);
24c349cdcaSJosh Poimboeuf 
2519514910SPetr Mladek struct klp_ops *klp_find_ops(void *old_func)
26c349cdcaSJosh Poimboeuf {
27c349cdcaSJosh Poimboeuf 	struct klp_ops *ops;
28c349cdcaSJosh Poimboeuf 	struct klp_func *func;
29c349cdcaSJosh Poimboeuf 
30c349cdcaSJosh Poimboeuf 	list_for_each_entry(ops, &klp_ops, node) {
31c349cdcaSJosh Poimboeuf 		func = list_first_entry(&ops->func_stack, struct klp_func,
32c349cdcaSJosh Poimboeuf 					stack_node);
3319514910SPetr Mladek 		if (func->old_func == old_func)
34c349cdcaSJosh Poimboeuf 			return ops;
35c349cdcaSJosh Poimboeuf 	}
36c349cdcaSJosh Poimboeuf 
37c349cdcaSJosh Poimboeuf 	return NULL;
38c349cdcaSJosh Poimboeuf }
39c349cdcaSJosh Poimboeuf 
40c349cdcaSJosh Poimboeuf static void notrace klp_ftrace_handler(unsigned long ip,
41c349cdcaSJosh Poimboeuf 				       unsigned long parent_ip,
42c349cdcaSJosh Poimboeuf 				       struct ftrace_ops *fops,
43*d19ad077SSteven Rostedt (VMware) 				       struct ftrace_regs *fregs)
44c349cdcaSJosh Poimboeuf {
45*d19ad077SSteven Rostedt (VMware) 	struct pt_regs *regs = ftrace_get_regs(fregs);
46c349cdcaSJosh Poimboeuf 	struct klp_ops *ops;
47c349cdcaSJosh Poimboeuf 	struct klp_func *func;
48d83a7cb3SJosh Poimboeuf 	int patch_state;
4913f3ea9aSSteven Rostedt (VMware) 	int bit;
50c349cdcaSJosh Poimboeuf 
51c349cdcaSJosh Poimboeuf 	ops = container_of(fops, struct klp_ops, fops);
52c349cdcaSJosh Poimboeuf 
53773c1670SSteven Rostedt (VMware) 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
544b750b57SSteven Rostedt (VMware) 	if (WARN_ON_ONCE(bit < 0))
5513f3ea9aSSteven Rostedt (VMware) 		return;
56842c0884SPetr Mladek 	/*
576932689eSPaul E. McKenney 	 * A variant of synchronize_rcu() is used to allow patching functions
58842c0884SPetr Mladek 	 * where RCU is not watching, see klp_synchronize_transition().
59842c0884SPetr Mladek 	 */
60842c0884SPetr Mladek 	preempt_disable_notrace();
61d83a7cb3SJosh Poimboeuf 
62c349cdcaSJosh Poimboeuf 	func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
63c349cdcaSJosh Poimboeuf 				      stack_node);
64d83a7cb3SJosh Poimboeuf 
65d83a7cb3SJosh Poimboeuf 	/*
66d83a7cb3SJosh Poimboeuf 	 * func should never be NULL because preemption should be disabled here
67d83a7cb3SJosh Poimboeuf 	 * and unregister_ftrace_function() does the equivalent of a
686932689eSPaul E. McKenney 	 * synchronize_rcu() before the func_stack removal.
69d83a7cb3SJosh Poimboeuf 	 */
70c349cdcaSJosh Poimboeuf 	if (WARN_ON_ONCE(!func))
71c349cdcaSJosh Poimboeuf 		goto unlock;
72c349cdcaSJosh Poimboeuf 
73d83a7cb3SJosh Poimboeuf 	/*
74d83a7cb3SJosh Poimboeuf 	 * In the enable path, enforce the order of the ops->func_stack and
75d83a7cb3SJosh Poimboeuf 	 * func->transition reads.  The corresponding write barrier is in
76d83a7cb3SJosh Poimboeuf 	 * __klp_enable_patch().
77d83a7cb3SJosh Poimboeuf 	 *
78d83a7cb3SJosh Poimboeuf 	 * (Note that this barrier technically isn't needed in the disable
79d83a7cb3SJosh Poimboeuf 	 * path.  In the rare case where klp_update_patch_state() runs before
80d83a7cb3SJosh Poimboeuf 	 * this handler, its TIF_PATCH_PENDING read and this func->transition
81d83a7cb3SJosh Poimboeuf 	 * read need to be ordered.  But klp_update_patch_state() already
82d83a7cb3SJosh Poimboeuf 	 * enforces that.)
83d83a7cb3SJosh Poimboeuf 	 */
84d83a7cb3SJosh Poimboeuf 	smp_rmb();
85d83a7cb3SJosh Poimboeuf 
86d83a7cb3SJosh Poimboeuf 	if (unlikely(func->transition)) {
87d83a7cb3SJosh Poimboeuf 
88d83a7cb3SJosh Poimboeuf 		/*
89d83a7cb3SJosh Poimboeuf 		 * Enforce the order of the func->transition and
90d83a7cb3SJosh Poimboeuf 		 * current->patch_state reads.  Otherwise we could read an
91d83a7cb3SJosh Poimboeuf 		 * out-of-date task state and pick the wrong function.  The
92d83a7cb3SJosh Poimboeuf 		 * corresponding write barrier is in klp_init_transition().
93d83a7cb3SJosh Poimboeuf 		 */
94d83a7cb3SJosh Poimboeuf 		smp_rmb();
95d83a7cb3SJosh Poimboeuf 
96d83a7cb3SJosh Poimboeuf 		patch_state = current->patch_state;
97d83a7cb3SJosh Poimboeuf 
98d83a7cb3SJosh Poimboeuf 		WARN_ON_ONCE(patch_state == KLP_UNDEFINED);
99d83a7cb3SJosh Poimboeuf 
100d83a7cb3SJosh Poimboeuf 		if (patch_state == KLP_UNPATCHED) {
101d83a7cb3SJosh Poimboeuf 			/*
102d83a7cb3SJosh Poimboeuf 			 * Use the previously patched version of the function.
103d83a7cb3SJosh Poimboeuf 			 * If no previous patches exist, continue with the
104d83a7cb3SJosh Poimboeuf 			 * original function.
105d83a7cb3SJosh Poimboeuf 			 */
106d83a7cb3SJosh Poimboeuf 			func = list_entry_rcu(func->stack_node.next,
107d83a7cb3SJosh Poimboeuf 					      struct klp_func, stack_node);
108d83a7cb3SJosh Poimboeuf 
109d83a7cb3SJosh Poimboeuf 			if (&func->stack_node == &ops->func_stack)
110d83a7cb3SJosh Poimboeuf 				goto unlock;
111d83a7cb3SJosh Poimboeuf 		}
112d83a7cb3SJosh Poimboeuf 	}
113d83a7cb3SJosh Poimboeuf 
114e1452b60SJason Baron 	/*
115e1452b60SJason Baron 	 * NOPs are used to replace existing patches with original code.
116e1452b60SJason Baron 	 * Do nothing! Setting pc would cause an infinite loop.
117e1452b60SJason Baron 	 */
118e1452b60SJason Baron 	if (func->nop)
119e1452b60SJason Baron 		goto unlock;
120e1452b60SJason Baron 
121c349cdcaSJosh Poimboeuf 	klp_arch_set_pc(regs, (unsigned long)func->new_func);
122e1452b60SJason Baron 
123c349cdcaSJosh Poimboeuf unlock:
124842c0884SPetr Mladek 	preempt_enable_notrace();
12513f3ea9aSSteven Rostedt (VMware) 	ftrace_test_recursion_unlock(bit);
126c349cdcaSJosh Poimboeuf }
127c349cdcaSJosh Poimboeuf 
128c349cdcaSJosh Poimboeuf /*
129c349cdcaSJosh Poimboeuf  * Convert a function address into the appropriate ftrace location.
130c349cdcaSJosh Poimboeuf  *
131c349cdcaSJosh Poimboeuf  * Usually this is just the address of the function, but on some architectures
132c349cdcaSJosh Poimboeuf  * it's more complicated so allow them to provide a custom behaviour.
133c349cdcaSJosh Poimboeuf  */
134c349cdcaSJosh Poimboeuf #ifndef klp_get_ftrace_location
135c349cdcaSJosh Poimboeuf static unsigned long klp_get_ftrace_location(unsigned long faddr)
136c349cdcaSJosh Poimboeuf {
137c349cdcaSJosh Poimboeuf 	return faddr;
138c349cdcaSJosh Poimboeuf }
139c349cdcaSJosh Poimboeuf #endif
140c349cdcaSJosh Poimboeuf 
141c349cdcaSJosh Poimboeuf static void klp_unpatch_func(struct klp_func *func)
142c349cdcaSJosh Poimboeuf {
143c349cdcaSJosh Poimboeuf 	struct klp_ops *ops;
144c349cdcaSJosh Poimboeuf 
145c349cdcaSJosh Poimboeuf 	if (WARN_ON(!func->patched))
146c349cdcaSJosh Poimboeuf 		return;
14719514910SPetr Mladek 	if (WARN_ON(!func->old_func))
148c349cdcaSJosh Poimboeuf 		return;
149c349cdcaSJosh Poimboeuf 
15019514910SPetr Mladek 	ops = klp_find_ops(func->old_func);
151c349cdcaSJosh Poimboeuf 	if (WARN_ON(!ops))
152c349cdcaSJosh Poimboeuf 		return;
153c349cdcaSJosh Poimboeuf 
154c349cdcaSJosh Poimboeuf 	if (list_is_singular(&ops->func_stack)) {
155c349cdcaSJosh Poimboeuf 		unsigned long ftrace_loc;
156c349cdcaSJosh Poimboeuf 
15719514910SPetr Mladek 		ftrace_loc =
15819514910SPetr Mladek 			klp_get_ftrace_location((unsigned long)func->old_func);
159c349cdcaSJosh Poimboeuf 		if (WARN_ON(!ftrace_loc))
160c349cdcaSJosh Poimboeuf 			return;
161c349cdcaSJosh Poimboeuf 
162c349cdcaSJosh Poimboeuf 		WARN_ON(unregister_ftrace_function(&ops->fops));
163c349cdcaSJosh Poimboeuf 		WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
164c349cdcaSJosh Poimboeuf 
165c349cdcaSJosh Poimboeuf 		list_del_rcu(&func->stack_node);
166c349cdcaSJosh Poimboeuf 		list_del(&ops->node);
167c349cdcaSJosh Poimboeuf 		kfree(ops);
168c349cdcaSJosh Poimboeuf 	} else {
169c349cdcaSJosh Poimboeuf 		list_del_rcu(&func->stack_node);
170c349cdcaSJosh Poimboeuf 	}
171c349cdcaSJosh Poimboeuf 
172c349cdcaSJosh Poimboeuf 	func->patched = false;
173c349cdcaSJosh Poimboeuf }
174c349cdcaSJosh Poimboeuf 
175c349cdcaSJosh Poimboeuf static int klp_patch_func(struct klp_func *func)
176c349cdcaSJosh Poimboeuf {
177c349cdcaSJosh Poimboeuf 	struct klp_ops *ops;
178c349cdcaSJosh Poimboeuf 	int ret;
179c349cdcaSJosh Poimboeuf 
18019514910SPetr Mladek 	if (WARN_ON(!func->old_func))
181c349cdcaSJosh Poimboeuf 		return -EINVAL;
182c349cdcaSJosh Poimboeuf 
183c349cdcaSJosh Poimboeuf 	if (WARN_ON(func->patched))
184c349cdcaSJosh Poimboeuf 		return -EINVAL;
185c349cdcaSJosh Poimboeuf 
18619514910SPetr Mladek 	ops = klp_find_ops(func->old_func);
187c349cdcaSJosh Poimboeuf 	if (!ops) {
188c349cdcaSJosh Poimboeuf 		unsigned long ftrace_loc;
189c349cdcaSJosh Poimboeuf 
19019514910SPetr Mladek 		ftrace_loc =
19119514910SPetr Mladek 			klp_get_ftrace_location((unsigned long)func->old_func);
192c349cdcaSJosh Poimboeuf 		if (!ftrace_loc) {
193c349cdcaSJosh Poimboeuf 			pr_err("failed to find location for function '%s'\n",
194c349cdcaSJosh Poimboeuf 				func->old_name);
195c349cdcaSJosh Poimboeuf 			return -EINVAL;
196c349cdcaSJosh Poimboeuf 		}
197c349cdcaSJosh Poimboeuf 
198c349cdcaSJosh Poimboeuf 		ops = kzalloc(sizeof(*ops), GFP_KERNEL);
199c349cdcaSJosh Poimboeuf 		if (!ops)
200c349cdcaSJosh Poimboeuf 			return -ENOMEM;
201c349cdcaSJosh Poimboeuf 
202c349cdcaSJosh Poimboeuf 		ops->fops.func = klp_ftrace_handler;
203c349cdcaSJosh Poimboeuf 		ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
204c349cdcaSJosh Poimboeuf 				  FTRACE_OPS_FL_DYNAMIC |
2057162431dSMiroslav Benes 				  FTRACE_OPS_FL_IPMODIFY |
2067162431dSMiroslav Benes 				  FTRACE_OPS_FL_PERMANENT;
207c349cdcaSJosh Poimboeuf 
208c349cdcaSJosh Poimboeuf 		list_add(&ops->node, &klp_ops);
209c349cdcaSJosh Poimboeuf 
210c349cdcaSJosh Poimboeuf 		INIT_LIST_HEAD(&ops->func_stack);
211c349cdcaSJosh Poimboeuf 		list_add_rcu(&func->stack_node, &ops->func_stack);
212c349cdcaSJosh Poimboeuf 
213c349cdcaSJosh Poimboeuf 		ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0);
214c349cdcaSJosh Poimboeuf 		if (ret) {
215c349cdcaSJosh Poimboeuf 			pr_err("failed to set ftrace filter for function '%s' (%d)\n",
216c349cdcaSJosh Poimboeuf 			       func->old_name, ret);
217c349cdcaSJosh Poimboeuf 			goto err;
218c349cdcaSJosh Poimboeuf 		}
219c349cdcaSJosh Poimboeuf 
220c349cdcaSJosh Poimboeuf 		ret = register_ftrace_function(&ops->fops);
221c349cdcaSJosh Poimboeuf 		if (ret) {
222c349cdcaSJosh Poimboeuf 			pr_err("failed to register ftrace handler for function '%s' (%d)\n",
223c349cdcaSJosh Poimboeuf 			       func->old_name, ret);
224c349cdcaSJosh Poimboeuf 			ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0);
225c349cdcaSJosh Poimboeuf 			goto err;
226c349cdcaSJosh Poimboeuf 		}
227c349cdcaSJosh Poimboeuf 
228c349cdcaSJosh Poimboeuf 
229c349cdcaSJosh Poimboeuf 	} else {
230c349cdcaSJosh Poimboeuf 		list_add_rcu(&func->stack_node, &ops->func_stack);
231c349cdcaSJosh Poimboeuf 	}
232c349cdcaSJosh Poimboeuf 
233c349cdcaSJosh Poimboeuf 	func->patched = true;
234c349cdcaSJosh Poimboeuf 
235c349cdcaSJosh Poimboeuf 	return 0;
236c349cdcaSJosh Poimboeuf 
237c349cdcaSJosh Poimboeuf err:
238c349cdcaSJosh Poimboeuf 	list_del_rcu(&func->stack_node);
239c349cdcaSJosh Poimboeuf 	list_del(&ops->node);
240c349cdcaSJosh Poimboeuf 	kfree(ops);
241c349cdcaSJosh Poimboeuf 	return ret;
242c349cdcaSJosh Poimboeuf }
243c349cdcaSJosh Poimboeuf 
244d697bad5SPetr Mladek static void __klp_unpatch_object(struct klp_object *obj, bool nops_only)
245c349cdcaSJosh Poimboeuf {
246c349cdcaSJosh Poimboeuf 	struct klp_func *func;
247c349cdcaSJosh Poimboeuf 
248d697bad5SPetr Mladek 	klp_for_each_func(obj, func) {
249d697bad5SPetr Mladek 		if (nops_only && !func->nop)
250d697bad5SPetr Mladek 			continue;
251d697bad5SPetr Mladek 
252c349cdcaSJosh Poimboeuf 		if (func->patched)
253c349cdcaSJosh Poimboeuf 			klp_unpatch_func(func);
254d697bad5SPetr Mladek 	}
255c349cdcaSJosh Poimboeuf 
256d697bad5SPetr Mladek 	if (obj->dynamic || !nops_only)
257c349cdcaSJosh Poimboeuf 		obj->patched = false;
258c349cdcaSJosh Poimboeuf }
259c349cdcaSJosh Poimboeuf 
260d697bad5SPetr Mladek 
261d697bad5SPetr Mladek void klp_unpatch_object(struct klp_object *obj)
262d697bad5SPetr Mladek {
263d697bad5SPetr Mladek 	__klp_unpatch_object(obj, false);
264d697bad5SPetr Mladek }
265d697bad5SPetr Mladek 
266c349cdcaSJosh Poimboeuf int klp_patch_object(struct klp_object *obj)
267c349cdcaSJosh Poimboeuf {
268c349cdcaSJosh Poimboeuf 	struct klp_func *func;
269c349cdcaSJosh Poimboeuf 	int ret;
270c349cdcaSJosh Poimboeuf 
271c349cdcaSJosh Poimboeuf 	if (WARN_ON(obj->patched))
272c349cdcaSJosh Poimboeuf 		return -EINVAL;
273c349cdcaSJosh Poimboeuf 
274c349cdcaSJosh Poimboeuf 	klp_for_each_func(obj, func) {
275c349cdcaSJosh Poimboeuf 		ret = klp_patch_func(func);
276c349cdcaSJosh Poimboeuf 		if (ret) {
277c349cdcaSJosh Poimboeuf 			klp_unpatch_object(obj);
278c349cdcaSJosh Poimboeuf 			return ret;
279c349cdcaSJosh Poimboeuf 		}
280c349cdcaSJosh Poimboeuf 	}
281c349cdcaSJosh Poimboeuf 	obj->patched = true;
282c349cdcaSJosh Poimboeuf 
283c349cdcaSJosh Poimboeuf 	return 0;
284c349cdcaSJosh Poimboeuf }
285d83a7cb3SJosh Poimboeuf 
286d697bad5SPetr Mladek static void __klp_unpatch_objects(struct klp_patch *patch, bool nops_only)
287d83a7cb3SJosh Poimboeuf {
288d83a7cb3SJosh Poimboeuf 	struct klp_object *obj;
289d83a7cb3SJosh Poimboeuf 
290d83a7cb3SJosh Poimboeuf 	klp_for_each_object(patch, obj)
291d83a7cb3SJosh Poimboeuf 		if (obj->patched)
292d697bad5SPetr Mladek 			__klp_unpatch_object(obj, nops_only);
293d697bad5SPetr Mladek }
294d697bad5SPetr Mladek 
295d697bad5SPetr Mladek void klp_unpatch_objects(struct klp_patch *patch)
296d697bad5SPetr Mladek {
297d697bad5SPetr Mladek 	__klp_unpatch_objects(patch, false);
298d697bad5SPetr Mladek }
299d697bad5SPetr Mladek 
300d697bad5SPetr Mladek void klp_unpatch_objects_dynamic(struct klp_patch *patch)
301d697bad5SPetr Mladek {
302d697bad5SPetr Mladek 	__klp_unpatch_objects(patch, true);
303d83a7cb3SJosh Poimboeuf }
304