xref: /linux-6.15/include/linux/livepatch.h (revision 439e7271)
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)
43d83a7cb3SJosh Poimboeuf  * @immediate:  patch the func immediately, bypassing safety mechanisms
44b2b018efSChris J Arges  * @old_addr:	the address of the function being patched
45b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
463c33f5b9SJosh Poimboeuf  * @stack_node:	list node for klp_ops func_stack list
47f5e547f4SJosh Poimboeuf  * @old_size:	size of the old function
48f5e547f4SJosh Poimboeuf  * @new_size:	size of the new function
490dade9f3SJosh Poimboeuf  * @patched:	the func has been added to the klp_ops list
50d83a7cb3SJosh Poimboeuf  * @transition:	the func is currently being applied or reverted
51d83a7cb3SJosh Poimboeuf  *
52d83a7cb3SJosh Poimboeuf  * The patched and transition variables define the func's patching state.  When
53d83a7cb3SJosh Poimboeuf  * patching, a func is always in one of the following states:
54d83a7cb3SJosh Poimboeuf  *
55d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
56d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary starting state
57d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
58d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
59d83a7cb3SJosh Poimboeuf  *
60d83a7cb3SJosh Poimboeuf  * And when unpatching, it goes in the reverse order:
61d83a7cb3SJosh Poimboeuf  *
62d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
63d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
64d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary ending state
65d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
66b700e7f0SSeth Jennings  */
67b700e7f0SSeth Jennings struct klp_func {
68b700e7f0SSeth Jennings 	/* external */
69b700e7f0SSeth Jennings 	const char *old_name;
70b700e7f0SSeth Jennings 	void *new_func;
71b700e7f0SSeth Jennings 	/*
72b2b018efSChris J Arges 	 * The old_sympos field is optional and can be used to resolve
73b2b018efSChris J Arges 	 * duplicate symbol names in livepatch objects. If this field is zero,
74b2b018efSChris J Arges 	 * it is expected the symbol is unique, otherwise patching fails. If
75b2b018efSChris J Arges 	 * this value is greater than zero then that occurrence of the symbol
76b2b018efSChris J Arges 	 * in kallsyms for the given object is used.
77b700e7f0SSeth Jennings 	 */
78b2b018efSChris J Arges 	unsigned long old_sympos;
79d83a7cb3SJosh Poimboeuf 	bool immediate;
80b700e7f0SSeth Jennings 
81b700e7f0SSeth Jennings 	/* internal */
82b2b018efSChris J Arges 	unsigned long old_addr;
83b700e7f0SSeth Jennings 	struct kobject kobj;
843c33f5b9SJosh Poimboeuf 	struct list_head stack_node;
85f5e547f4SJosh Poimboeuf 	unsigned long old_size, new_size;
860dade9f3SJosh Poimboeuf 	bool patched;
87d83a7cb3SJosh Poimboeuf 	bool transition;
88b700e7f0SSeth Jennings };
89b700e7f0SSeth Jennings 
90b700e7f0SSeth Jennings /**
91b700e7f0SSeth Jennings  * struct klp_object - kernel object structure for live patching
92b700e7f0SSeth Jennings  * @name:	module name (or NULL for vmlinux)
93b700e7f0SSeth Jennings  * @funcs:	function entries for functions to be patched in the object
94b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
95b700e7f0SSeth Jennings  * @mod:	kernel module associated with the patched object
96b700e7f0SSeth Jennings  *		(NULL for vmlinux)
970dade9f3SJosh Poimboeuf  * @patched:	the object's funcs have been added to the klp_ops list
98b700e7f0SSeth Jennings  */
99b700e7f0SSeth Jennings struct klp_object {
100b700e7f0SSeth Jennings 	/* external */
101b700e7f0SSeth Jennings 	const char *name;
102b700e7f0SSeth Jennings 	struct klp_func *funcs;
103b700e7f0SSeth Jennings 
104b700e7f0SSeth Jennings 	/* internal */
105cad706dfSMiroslav Benes 	struct kobject kobj;
106b700e7f0SSeth Jennings 	struct module *mod;
1070dade9f3SJosh Poimboeuf 	bool patched;
108b700e7f0SSeth Jennings };
109b700e7f0SSeth Jennings 
110b700e7f0SSeth Jennings /**
111b700e7f0SSeth Jennings  * struct klp_patch - patch structure for live patching
112b700e7f0SSeth Jennings  * @mod:	reference to the live patch module
113b700e7f0SSeth Jennings  * @objs:	object entries for kernel objects to be patched
114d83a7cb3SJosh Poimboeuf  * @immediate:  patch all funcs immediately, bypassing safety mechanisms
115b700e7f0SSeth Jennings  * @list:	list node for global list of registered patches
116b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
1170dade9f3SJosh Poimboeuf  * @enabled:	the patch is enabled (but operation may be incomplete)
1183ec24776SJosh Poimboeuf  * @finish:	for waiting till it is safe to remove the patch module
119b700e7f0SSeth Jennings  */
120b700e7f0SSeth Jennings struct klp_patch {
121b700e7f0SSeth Jennings 	/* external */
122b700e7f0SSeth Jennings 	struct module *mod;
123b700e7f0SSeth Jennings 	struct klp_object *objs;
124d83a7cb3SJosh Poimboeuf 	bool immediate;
125b700e7f0SSeth Jennings 
126b700e7f0SSeth Jennings 	/* internal */
127b700e7f0SSeth Jennings 	struct list_head list;
128b700e7f0SSeth Jennings 	struct kobject kobj;
1290dade9f3SJosh Poimboeuf 	bool enabled;
1303ec24776SJosh Poimboeuf 	struct completion finish;
131b700e7f0SSeth Jennings };
132b700e7f0SSeth Jennings 
1338cdd043aSJiri Slaby #define klp_for_each_object(patch, obj) \
134f09d9086SMiroslav Benes 	for (obj = patch->objs; obj->funcs || obj->name; obj++)
1358cdd043aSJiri Slaby 
1368cdd043aSJiri Slaby #define klp_for_each_func(obj, func) \
137f09d9086SMiroslav Benes 	for (func = obj->funcs; \
138f09d9086SMiroslav Benes 	     func->old_name || func->new_func || func->old_sympos; \
139f09d9086SMiroslav Benes 	     func++)
1408cdd043aSJiri Slaby 
1414421f8f0SMiroslav Benes int klp_register_patch(struct klp_patch *);
1424421f8f0SMiroslav Benes int klp_unregister_patch(struct klp_patch *);
1434421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *);
1444421f8f0SMiroslav Benes int klp_disable_patch(struct klp_patch *);
145b700e7f0SSeth Jennings 
146255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch,
147255e732cSJessica Yu 				 struct klp_object *obj);
148255e732cSJessica Yu 
1497e545d6eSJessica Yu /* Called from the module loader during module coming/going states */
1507e545d6eSJessica Yu int klp_module_coming(struct module *mod);
1517e545d6eSJessica Yu void klp_module_going(struct module *mod);
1527e545d6eSJessica Yu 
153d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child);
15446c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task);
15546c5a011SJosh Poimboeuf 
156d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task)
157d83a7cb3SJosh Poimboeuf {
158d83a7cb3SJosh Poimboeuf 	return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
159d83a7cb3SJosh Poimboeuf }
160d83a7cb3SJosh Poimboeuf 
161d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void)
162d83a7cb3SJosh Poimboeuf {
163d83a7cb3SJosh Poimboeuf 	return IS_ENABLED(CONFIG_STACKTRACE) &&
164d83a7cb3SJosh Poimboeuf 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
165d83a7cb3SJosh Poimboeuf }
166d83a7cb3SJosh Poimboeuf 
167*439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id);
168*439e7271SJoe Lawrence void *klp_shadow_alloc(void *obj, unsigned long id, void *data,
169*439e7271SJoe Lawrence 		       size_t size, gfp_t gfp_flags);
170*439e7271SJoe Lawrence void *klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
171*439e7271SJoe Lawrence 			      size_t size, gfp_t gfp_flags);
172*439e7271SJoe Lawrence void klp_shadow_free(void *obj, unsigned long id);
173*439e7271SJoe Lawrence void klp_shadow_free_all(unsigned long id);
174*439e7271SJoe Lawrence 
1757e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */
1767e545d6eSJessica Yu 
1777e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; }
1787e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {}
179d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; }
18046c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {}
181d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {}
1827e545d6eSJessica Yu 
1837e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */
1847e545d6eSJessica Yu 
185b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */
186