xref: /linux-6.15/include/linux/livepatch.h (revision e91c2518)
1b700e7f0SSeth Jennings /*
2b700e7f0SSeth Jennings  * livepatch.h - 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 #ifndef _LINUX_LIVEPATCH_H_
22b700e7f0SSeth Jennings #define _LINUX_LIVEPATCH_H_
23b700e7f0SSeth Jennings 
24b700e7f0SSeth Jennings #include <linux/module.h>
25b700e7f0SSeth Jennings #include <linux/ftrace.h>
263ec24776SJosh Poimboeuf #include <linux/completion.h>
27b700e7f0SSeth Jennings 
287e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH)
297e545d6eSJessica Yu 
30b700e7f0SSeth Jennings #include <asm/livepatch.h>
31b700e7f0SSeth Jennings 
32d83a7cb3SJosh Poimboeuf /* task patch states */
33d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED	-1
34d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED	 0
35d83a7cb3SJosh Poimboeuf #define KLP_PATCHED	 1
36d83a7cb3SJosh Poimboeuf 
37b700e7f0SSeth Jennings /**
38b700e7f0SSeth Jennings  * struct klp_func - function structure for live patching
39b700e7f0SSeth Jennings  * @old_name:	name of the function to be patched
40b700e7f0SSeth Jennings  * @new_func:	pointer to the patched function code
41b2b018efSChris J Arges  * @old_sympos: a hint indicating which symbol position the old function
42b2b018efSChris J Arges  *		can be found (optional)
43b2b018efSChris J Arges  * @old_addr:	the address of the function being patched
44b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
453c33f5b9SJosh Poimboeuf  * @stack_node:	list node for klp_ops func_stack list
46f5e547f4SJosh Poimboeuf  * @old_size:	size of the old function
47f5e547f4SJosh Poimboeuf  * @new_size:	size of the new function
480dade9f3SJosh Poimboeuf  * @patched:	the func has been added to the klp_ops list
49d83a7cb3SJosh Poimboeuf  * @transition:	the func is currently being applied or reverted
50d83a7cb3SJosh Poimboeuf  *
51d83a7cb3SJosh Poimboeuf  * The patched and transition variables define the func's patching state.  When
52d83a7cb3SJosh Poimboeuf  * patching, a func is always in one of the following states:
53d83a7cb3SJosh Poimboeuf  *
54d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
55d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary starting state
56d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
57d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
58d83a7cb3SJosh Poimboeuf  *
59d83a7cb3SJosh Poimboeuf  * And when unpatching, it goes in the reverse order:
60d83a7cb3SJosh Poimboeuf  *
61d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
62d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
63d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary ending state
64d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
65b700e7f0SSeth Jennings  */
66b700e7f0SSeth Jennings struct klp_func {
67b700e7f0SSeth Jennings 	/* external */
68b700e7f0SSeth Jennings 	const char *old_name;
69b700e7f0SSeth Jennings 	void *new_func;
70b700e7f0SSeth Jennings 	/*
71b2b018efSChris J Arges 	 * The old_sympos field is optional and can be used to resolve
72b2b018efSChris J Arges 	 * duplicate symbol names in livepatch objects. If this field is zero,
73b2b018efSChris J Arges 	 * it is expected the symbol is unique, otherwise patching fails. If
74b2b018efSChris J Arges 	 * this value is greater than zero then that occurrence of the symbol
75b2b018efSChris J Arges 	 * in kallsyms for the given object is used.
76b700e7f0SSeth Jennings 	 */
77b2b018efSChris J Arges 	unsigned long old_sympos;
78b700e7f0SSeth Jennings 
79b700e7f0SSeth Jennings 	/* internal */
80b2b018efSChris J Arges 	unsigned long old_addr;
81b700e7f0SSeth Jennings 	struct kobject kobj;
823c33f5b9SJosh Poimboeuf 	struct list_head stack_node;
83f5e547f4SJosh Poimboeuf 	unsigned long old_size, new_size;
840dade9f3SJosh Poimboeuf 	bool patched;
85d83a7cb3SJosh Poimboeuf 	bool transition;
86b700e7f0SSeth Jennings };
87b700e7f0SSeth Jennings 
8893862e38SJoe Lawrence struct klp_object;
8993862e38SJoe Lawrence 
9093862e38SJoe Lawrence /**
9193862e38SJoe Lawrence  * struct klp_callbacks - pre/post live-(un)patch callback structure
9293862e38SJoe Lawrence  * @pre_patch:		executed before code patching
9393862e38SJoe Lawrence  * @post_patch:		executed after code patching
9493862e38SJoe Lawrence  * @pre_unpatch:	executed before code unpatching
9593862e38SJoe Lawrence  * @post_unpatch:	executed after code unpatching
9693862e38SJoe Lawrence  * @post_unpatch_enabled:	flag indicating if post-unpatch callback
9793862e38SJoe Lawrence  * 				should run
9893862e38SJoe Lawrence  *
9993862e38SJoe Lawrence  * All callbacks are optional.  Only the pre-patch callback, if provided,
10093862e38SJoe Lawrence  * will be unconditionally executed.  If the parent klp_object fails to
10193862e38SJoe Lawrence  * patch for any reason, including a non-zero error status returned from
10293862e38SJoe Lawrence  * the pre-patch callback, no further callbacks will be executed.
10393862e38SJoe Lawrence  */
10493862e38SJoe Lawrence struct klp_callbacks {
10593862e38SJoe Lawrence 	int (*pre_patch)(struct klp_object *obj);
10693862e38SJoe Lawrence 	void (*post_patch)(struct klp_object *obj);
10793862e38SJoe Lawrence 	void (*pre_unpatch)(struct klp_object *obj);
10893862e38SJoe Lawrence 	void (*post_unpatch)(struct klp_object *obj);
10993862e38SJoe Lawrence 	bool post_unpatch_enabled;
11093862e38SJoe Lawrence };
11193862e38SJoe Lawrence 
112b700e7f0SSeth Jennings /**
113b700e7f0SSeth Jennings  * struct klp_object - kernel object structure for live patching
114b700e7f0SSeth Jennings  * @name:	module name (or NULL for vmlinux)
115b700e7f0SSeth Jennings  * @funcs:	function entries for functions to be patched in the object
11693862e38SJoe Lawrence  * @callbacks:	functions to be executed pre/post (un)patching
117b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
118b700e7f0SSeth Jennings  * @mod:	kernel module associated with the patched object
119b700e7f0SSeth Jennings  *		(NULL for vmlinux)
1200dade9f3SJosh Poimboeuf  * @patched:	the object's funcs have been added to the klp_ops list
121b700e7f0SSeth Jennings  */
122b700e7f0SSeth Jennings struct klp_object {
123b700e7f0SSeth Jennings 	/* external */
124b700e7f0SSeth Jennings 	const char *name;
125b700e7f0SSeth Jennings 	struct klp_func *funcs;
12693862e38SJoe Lawrence 	struct klp_callbacks callbacks;
127b700e7f0SSeth Jennings 
128b700e7f0SSeth Jennings 	/* internal */
129cad706dfSMiroslav Benes 	struct kobject kobj;
130b700e7f0SSeth Jennings 	struct module *mod;
1310dade9f3SJosh Poimboeuf 	bool patched;
132b700e7f0SSeth Jennings };
133b700e7f0SSeth Jennings 
134b700e7f0SSeth Jennings /**
135b700e7f0SSeth Jennings  * struct klp_patch - patch structure for live patching
136b700e7f0SSeth Jennings  * @mod:	reference to the live patch module
137b700e7f0SSeth Jennings  * @objs:	object entries for kernel objects to be patched
138b700e7f0SSeth Jennings  * @list:	list node for global list of registered patches
139b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
1400dade9f3SJosh Poimboeuf  * @enabled:	the patch is enabled (but operation may be incomplete)
1413ec24776SJosh Poimboeuf  * @finish:	for waiting till it is safe to remove the patch module
142b700e7f0SSeth Jennings  */
143b700e7f0SSeth Jennings struct klp_patch {
144b700e7f0SSeth Jennings 	/* external */
145b700e7f0SSeth Jennings 	struct module *mod;
146b700e7f0SSeth Jennings 	struct klp_object *objs;
147b700e7f0SSeth Jennings 
148b700e7f0SSeth Jennings 	/* internal */
149b700e7f0SSeth Jennings 	struct list_head list;
150b700e7f0SSeth Jennings 	struct kobject kobj;
1510dade9f3SJosh Poimboeuf 	bool enabled;
1523ec24776SJosh Poimboeuf 	struct completion finish;
153b700e7f0SSeth Jennings };
154b700e7f0SSeth Jennings 
1558cdd043aSJiri Slaby #define klp_for_each_object(patch, obj) \
156f09d9086SMiroslav Benes 	for (obj = patch->objs; obj->funcs || obj->name; obj++)
1578cdd043aSJiri Slaby 
1588cdd043aSJiri Slaby #define klp_for_each_func(obj, func) \
159f09d9086SMiroslav Benes 	for (func = obj->funcs; \
160f09d9086SMiroslav Benes 	     func->old_name || func->new_func || func->old_sympos; \
161f09d9086SMiroslav Benes 	     func++)
1628cdd043aSJiri Slaby 
1634421f8f0SMiroslav Benes int klp_register_patch(struct klp_patch *);
1644421f8f0SMiroslav Benes int klp_unregister_patch(struct klp_patch *);
1654421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *);
1664421f8f0SMiroslav Benes int klp_disable_patch(struct klp_patch *);
167b700e7f0SSeth Jennings 
168255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch,
169255e732cSJessica Yu 				 struct klp_object *obj);
170255e732cSJessica Yu 
1717e545d6eSJessica Yu /* Called from the module loader during module coming/going states */
1727e545d6eSJessica Yu int klp_module_coming(struct module *mod);
1737e545d6eSJessica Yu void klp_module_going(struct module *mod);
1747e545d6eSJessica Yu 
175d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child);
17646c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task);
17746c5a011SJosh Poimboeuf 
178d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task)
179d83a7cb3SJosh Poimboeuf {
180d83a7cb3SJosh Poimboeuf 	return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
181d83a7cb3SJosh Poimboeuf }
182d83a7cb3SJosh Poimboeuf 
183d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void)
184d83a7cb3SJosh Poimboeuf {
185d83a7cb3SJosh Poimboeuf 	return IS_ENABLED(CONFIG_STACKTRACE) &&
186d83a7cb3SJosh Poimboeuf 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
187d83a7cb3SJosh Poimboeuf }
188d83a7cb3SJosh Poimboeuf 
189*e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj,
190*e91c2518SPetr Mladek 				 void *shadow_data,
191*e91c2518SPetr Mladek 				 void *ctor_data);
192*e91c2518SPetr Mladek 
193439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id);
194*e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id,
195*e91c2518SPetr Mladek 		       size_t size, gfp_t gfp_flags,
196*e91c2518SPetr Mladek 		       klp_shadow_ctor_t ctor, void *ctor_data);
197*e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
198*e91c2518SPetr Mladek 			      size_t size, gfp_t gfp_flags,
199*e91c2518SPetr Mladek 			      klp_shadow_ctor_t ctor, void *ctor_data);
200439e7271SJoe Lawrence void klp_shadow_free(void *obj, unsigned long id);
201439e7271SJoe Lawrence void klp_shadow_free_all(unsigned long id);
202439e7271SJoe Lawrence 
2037e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */
2047e545d6eSJessica Yu 
2057e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; }
2067e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {}
207d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; }
20846c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {}
209d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {}
2107e545d6eSJessica Yu 
2117e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */
2127e545d6eSJessica Yu 
213b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */
214