xref: /linux-6.15/include/linux/livepatch.h (revision e1452b60)
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>
2720e55025SJason Baron #include <linux/list.h>
28b700e7f0SSeth Jennings 
297e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH)
307e545d6eSJessica Yu 
31b700e7f0SSeth Jennings #include <asm/livepatch.h>
32b700e7f0SSeth Jennings 
33d83a7cb3SJosh Poimboeuf /* task patch states */
34d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED	-1
35d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED	 0
36d83a7cb3SJosh Poimboeuf #define KLP_PATCHED	 1
37d83a7cb3SJosh Poimboeuf 
38b700e7f0SSeth Jennings /**
39b700e7f0SSeth Jennings  * struct klp_func - function structure for live patching
40b700e7f0SSeth Jennings  * @old_name:	name of the function to be patched
41b700e7f0SSeth Jennings  * @new_func:	pointer to the patched function code
42b2b018efSChris J Arges  * @old_sympos: a hint indicating which symbol position the old function
43b2b018efSChris J Arges  *		can be found (optional)
4419514910SPetr Mladek  * @old_func:	pointer to the function being patched
45b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
4620e55025SJason Baron  * @node:	list node for klp_object func_list
473c33f5b9SJosh Poimboeuf  * @stack_node:	list node for klp_ops func_stack list
48f5e547f4SJosh Poimboeuf  * @old_size:	size of the old function
49f5e547f4SJosh Poimboeuf  * @new_size:	size of the new function
500430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
51*e1452b60SJason Baron  * @nop:        temporary patch to use the original code again; dyn. allocated
520dade9f3SJosh Poimboeuf  * @patched:	the func has been added to the klp_ops list
53d83a7cb3SJosh Poimboeuf  * @transition:	the func is currently being applied or reverted
54d83a7cb3SJosh Poimboeuf  *
55d83a7cb3SJosh Poimboeuf  * The patched and transition variables define the func's patching state.  When
56d83a7cb3SJosh Poimboeuf  * patching, a func is always in one of the following states:
57d83a7cb3SJosh Poimboeuf  *
58d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
59d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary starting state
60d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
61d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
62d83a7cb3SJosh Poimboeuf  *
63d83a7cb3SJosh Poimboeuf  * And when unpatching, it goes in the reverse order:
64d83a7cb3SJosh Poimboeuf  *
65d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
66d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
67d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary ending state
68d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
69b700e7f0SSeth Jennings  */
70b700e7f0SSeth Jennings struct klp_func {
71b700e7f0SSeth Jennings 	/* external */
72b700e7f0SSeth Jennings 	const char *old_name;
73b700e7f0SSeth Jennings 	void *new_func;
74b700e7f0SSeth Jennings 	/*
75b2b018efSChris J Arges 	 * The old_sympos field is optional and can be used to resolve
76b2b018efSChris J Arges 	 * duplicate symbol names in livepatch objects. If this field is zero,
77b2b018efSChris J Arges 	 * it is expected the symbol is unique, otherwise patching fails. If
78b2b018efSChris J Arges 	 * this value is greater than zero then that occurrence of the symbol
79b2b018efSChris J Arges 	 * in kallsyms for the given object is used.
80b700e7f0SSeth Jennings 	 */
81b2b018efSChris J Arges 	unsigned long old_sympos;
82b700e7f0SSeth Jennings 
83b700e7f0SSeth Jennings 	/* internal */
8419514910SPetr Mladek 	void *old_func;
85b700e7f0SSeth Jennings 	struct kobject kobj;
8620e55025SJason Baron 	struct list_head node;
873c33f5b9SJosh Poimboeuf 	struct list_head stack_node;
88f5e547f4SJosh Poimboeuf 	unsigned long old_size, new_size;
890430f78bSPetr Mladek 	bool kobj_added;
90*e1452b60SJason Baron 	bool nop;
910dade9f3SJosh Poimboeuf 	bool patched;
92d83a7cb3SJosh Poimboeuf 	bool transition;
93b700e7f0SSeth Jennings };
94b700e7f0SSeth Jennings 
9593862e38SJoe Lawrence struct klp_object;
9693862e38SJoe Lawrence 
9793862e38SJoe Lawrence /**
9893862e38SJoe Lawrence  * struct klp_callbacks - pre/post live-(un)patch callback structure
9993862e38SJoe Lawrence  * @pre_patch:		executed before code patching
10093862e38SJoe Lawrence  * @post_patch:		executed after code patching
10193862e38SJoe Lawrence  * @pre_unpatch:	executed before code unpatching
10293862e38SJoe Lawrence  * @post_unpatch:	executed after code unpatching
10393862e38SJoe Lawrence  * @post_unpatch_enabled:	flag indicating if post-unpatch callback
10493862e38SJoe Lawrence  * 				should run
10593862e38SJoe Lawrence  *
10693862e38SJoe Lawrence  * All callbacks are optional.  Only the pre-patch callback, if provided,
10793862e38SJoe Lawrence  * will be unconditionally executed.  If the parent klp_object fails to
10893862e38SJoe Lawrence  * patch for any reason, including a non-zero error status returned from
10993862e38SJoe Lawrence  * the pre-patch callback, no further callbacks will be executed.
11093862e38SJoe Lawrence  */
11193862e38SJoe Lawrence struct klp_callbacks {
11293862e38SJoe Lawrence 	int (*pre_patch)(struct klp_object *obj);
11393862e38SJoe Lawrence 	void (*post_patch)(struct klp_object *obj);
11493862e38SJoe Lawrence 	void (*pre_unpatch)(struct klp_object *obj);
11593862e38SJoe Lawrence 	void (*post_unpatch)(struct klp_object *obj);
11693862e38SJoe Lawrence 	bool post_unpatch_enabled;
11793862e38SJoe Lawrence };
11893862e38SJoe Lawrence 
119b700e7f0SSeth Jennings /**
120b700e7f0SSeth Jennings  * struct klp_object - kernel object structure for live patching
121b700e7f0SSeth Jennings  * @name:	module name (or NULL for vmlinux)
122b700e7f0SSeth Jennings  * @funcs:	function entries for functions to be patched in the object
12393862e38SJoe Lawrence  * @callbacks:	functions to be executed pre/post (un)patching
124b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
12520e55025SJason Baron  * @func_list:	dynamic list of the function entries
12620e55025SJason Baron  * @node:	list node for klp_patch obj_list
127b700e7f0SSeth Jennings  * @mod:	kernel module associated with the patched object
128b700e7f0SSeth Jennings  *		(NULL for vmlinux)
1290430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
130*e1452b60SJason Baron  * @dynamic:    temporary object for nop functions; dynamically allocated
1310dade9f3SJosh Poimboeuf  * @patched:	the object's funcs have been added to the klp_ops list
132b700e7f0SSeth Jennings  */
133b700e7f0SSeth Jennings struct klp_object {
134b700e7f0SSeth Jennings 	/* external */
135b700e7f0SSeth Jennings 	const char *name;
136b700e7f0SSeth Jennings 	struct klp_func *funcs;
13793862e38SJoe Lawrence 	struct klp_callbacks callbacks;
138b700e7f0SSeth Jennings 
139b700e7f0SSeth Jennings 	/* internal */
140cad706dfSMiroslav Benes 	struct kobject kobj;
14120e55025SJason Baron 	struct list_head func_list;
14220e55025SJason Baron 	struct list_head node;
143b700e7f0SSeth Jennings 	struct module *mod;
1440430f78bSPetr Mladek 	bool kobj_added;
145*e1452b60SJason Baron 	bool dynamic;
1460dade9f3SJosh Poimboeuf 	bool patched;
147b700e7f0SSeth Jennings };
148b700e7f0SSeth Jennings 
149b700e7f0SSeth Jennings /**
150b700e7f0SSeth Jennings  * struct klp_patch - patch structure for live patching
151b700e7f0SSeth Jennings  * @mod:	reference to the live patch module
152b700e7f0SSeth Jennings  * @objs:	object entries for kernel objects to be patched
153*e1452b60SJason Baron  * @replace:	replace all actively used patches
154958ef1e3SPetr Mladek  * @list:	list node for global list of actively used patches
155b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
15620e55025SJason Baron  * @obj_list:	dynamic list of the object entries
1570430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
1580dade9f3SJosh Poimboeuf  * @enabled:	the patch is enabled (but operation may be incomplete)
15968007289SPetr Mladek  * @forced:	was involved in a forced transition
160958ef1e3SPetr Mladek  * @free_work:	patch cleanup from workqueue-context
1613ec24776SJosh Poimboeuf  * @finish:	for waiting till it is safe to remove the patch module
162b700e7f0SSeth Jennings  */
163b700e7f0SSeth Jennings struct klp_patch {
164b700e7f0SSeth Jennings 	/* external */
165b700e7f0SSeth Jennings 	struct module *mod;
166b700e7f0SSeth Jennings 	struct klp_object *objs;
167*e1452b60SJason Baron 	bool replace;
168b700e7f0SSeth Jennings 
169b700e7f0SSeth Jennings 	/* internal */
170b700e7f0SSeth Jennings 	struct list_head list;
171b700e7f0SSeth Jennings 	struct kobject kobj;
17220e55025SJason Baron 	struct list_head obj_list;
1730430f78bSPetr Mladek 	bool kobj_added;
1740dade9f3SJosh Poimboeuf 	bool enabled;
17568007289SPetr Mladek 	bool forced;
176958ef1e3SPetr Mladek 	struct work_struct free_work;
1773ec24776SJosh Poimboeuf 	struct completion finish;
178b700e7f0SSeth Jennings };
179b700e7f0SSeth Jennings 
18020e55025SJason Baron #define klp_for_each_object_static(patch, obj) \
181f09d9086SMiroslav Benes 	for (obj = patch->objs; obj->funcs || obj->name; obj++)
1828cdd043aSJiri Slaby 
183*e1452b60SJason Baron #define klp_for_each_object_safe(patch, obj, tmp_obj)		\
184*e1452b60SJason Baron 	list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node)
185*e1452b60SJason Baron 
18620e55025SJason Baron #define klp_for_each_object(patch, obj)	\
18720e55025SJason Baron 	list_for_each_entry(obj, &patch->obj_list, node)
18820e55025SJason Baron 
18920e55025SJason Baron #define klp_for_each_func_static(obj, func) \
190f09d9086SMiroslav Benes 	for (func = obj->funcs; \
191f09d9086SMiroslav Benes 	     func->old_name || func->new_func || func->old_sympos; \
192f09d9086SMiroslav Benes 	     func++)
1938cdd043aSJiri Slaby 
194*e1452b60SJason Baron #define klp_for_each_func_safe(obj, func, tmp_func)			\
195*e1452b60SJason Baron 	list_for_each_entry_safe(func, tmp_func, &obj->func_list, node)
196*e1452b60SJason Baron 
19720e55025SJason Baron #define klp_for_each_func(obj, func)	\
19820e55025SJason Baron 	list_for_each_entry(func, &obj->func_list, node)
19920e55025SJason Baron 
2004421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *);
201b700e7f0SSeth Jennings 
202255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch,
203255e732cSJessica Yu 				 struct klp_object *obj);
204255e732cSJessica Yu 
2057e545d6eSJessica Yu /* Called from the module loader during module coming/going states */
2067e545d6eSJessica Yu int klp_module_coming(struct module *mod);
2077e545d6eSJessica Yu void klp_module_going(struct module *mod);
2087e545d6eSJessica Yu 
209d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child);
21046c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task);
21146c5a011SJosh Poimboeuf 
212d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task)
213d83a7cb3SJosh Poimboeuf {
214d83a7cb3SJosh Poimboeuf 	return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
215d83a7cb3SJosh Poimboeuf }
216d83a7cb3SJosh Poimboeuf 
217d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void)
218d83a7cb3SJosh Poimboeuf {
219d83a7cb3SJosh Poimboeuf 	return IS_ENABLED(CONFIG_STACKTRACE) &&
220d83a7cb3SJosh Poimboeuf 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
221d83a7cb3SJosh Poimboeuf }
222d83a7cb3SJosh Poimboeuf 
223e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj,
224e91c2518SPetr Mladek 				 void *shadow_data,
225e91c2518SPetr Mladek 				 void *ctor_data);
2263b2c77d0SPetr Mladek typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data);
227e91c2518SPetr Mladek 
228439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id);
229e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id,
230e91c2518SPetr Mladek 		       size_t size, gfp_t gfp_flags,
231e91c2518SPetr Mladek 		       klp_shadow_ctor_t ctor, void *ctor_data);
232e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
233e91c2518SPetr Mladek 			      size_t size, gfp_t gfp_flags,
234e91c2518SPetr Mladek 			      klp_shadow_ctor_t ctor, void *ctor_data);
2353b2c77d0SPetr Mladek void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
2363b2c77d0SPetr Mladek void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
237439e7271SJoe Lawrence 
2387e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */
2397e545d6eSJessica Yu 
2407e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; }
2417e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {}
242d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; }
24346c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {}
244d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {}
2457e545d6eSJessica Yu 
2467e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */
2477e545d6eSJessica Yu 
248b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */
249