xref: /linux-6.15/include/linux/livepatch.h (revision 1ccea77e)
1*1ccea77eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b700e7f0SSeth Jennings /*
3b700e7f0SSeth Jennings  * livepatch.h - Kernel Live Patching Core
4b700e7f0SSeth Jennings  *
5b700e7f0SSeth Jennings  * Copyright (C) 2014 Seth Jennings <[email protected]>
6b700e7f0SSeth Jennings  * Copyright (C) 2014 SUSE
7b700e7f0SSeth Jennings  */
8b700e7f0SSeth Jennings 
9b700e7f0SSeth Jennings #ifndef _LINUX_LIVEPATCH_H_
10b700e7f0SSeth Jennings #define _LINUX_LIVEPATCH_H_
11b700e7f0SSeth Jennings 
12b700e7f0SSeth Jennings #include <linux/module.h>
13b700e7f0SSeth Jennings #include <linux/ftrace.h>
143ec24776SJosh Poimboeuf #include <linux/completion.h>
1520e55025SJason Baron #include <linux/list.h>
16b700e7f0SSeth Jennings 
177e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH)
187e545d6eSJessica Yu 
19b700e7f0SSeth Jennings #include <asm/livepatch.h>
20b700e7f0SSeth Jennings 
21d83a7cb3SJosh Poimboeuf /* task patch states */
22d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED	-1
23d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED	 0
24d83a7cb3SJosh Poimboeuf #define KLP_PATCHED	 1
25d83a7cb3SJosh Poimboeuf 
26b700e7f0SSeth Jennings /**
27b700e7f0SSeth Jennings  * struct klp_func - function structure for live patching
28b700e7f0SSeth Jennings  * @old_name:	name of the function to be patched
29b700e7f0SSeth Jennings  * @new_func:	pointer to the patched function code
30b2b018efSChris J Arges  * @old_sympos: a hint indicating which symbol position the old function
31b2b018efSChris J Arges  *		can be found (optional)
3219514910SPetr Mladek  * @old_func:	pointer to the function being patched
33b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
3420e55025SJason Baron  * @node:	list node for klp_object func_list
353c33f5b9SJosh Poimboeuf  * @stack_node:	list node for klp_ops func_stack list
36f5e547f4SJosh Poimboeuf  * @old_size:	size of the old function
37f5e547f4SJosh Poimboeuf  * @new_size:	size of the new function
380430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
39e1452b60SJason Baron  * @nop:        temporary patch to use the original code again; dyn. allocated
400dade9f3SJosh Poimboeuf  * @patched:	the func has been added to the klp_ops list
41d83a7cb3SJosh Poimboeuf  * @transition:	the func is currently being applied or reverted
42d83a7cb3SJosh Poimboeuf  *
43d83a7cb3SJosh Poimboeuf  * The patched and transition variables define the func's patching state.  When
44d83a7cb3SJosh Poimboeuf  * patching, a func is always in one of the following states:
45d83a7cb3SJosh Poimboeuf  *
46d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
47d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary starting state
48d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
49d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
50d83a7cb3SJosh Poimboeuf  *
51d83a7cb3SJosh Poimboeuf  * And when unpatching, it goes in the reverse order:
52d83a7cb3SJosh Poimboeuf  *
53d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
54d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
55d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary ending state
56d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
57b700e7f0SSeth Jennings  */
58b700e7f0SSeth Jennings struct klp_func {
59b700e7f0SSeth Jennings 	/* external */
60b700e7f0SSeth Jennings 	const char *old_name;
61b700e7f0SSeth Jennings 	void *new_func;
62b700e7f0SSeth Jennings 	/*
63b2b018efSChris J Arges 	 * The old_sympos field is optional and can be used to resolve
64b2b018efSChris J Arges 	 * duplicate symbol names in livepatch objects. If this field is zero,
65b2b018efSChris J Arges 	 * it is expected the symbol is unique, otherwise patching fails. If
66b2b018efSChris J Arges 	 * this value is greater than zero then that occurrence of the symbol
67b2b018efSChris J Arges 	 * in kallsyms for the given object is used.
68b700e7f0SSeth Jennings 	 */
69b2b018efSChris J Arges 	unsigned long old_sympos;
70b700e7f0SSeth Jennings 
71b700e7f0SSeth Jennings 	/* internal */
7219514910SPetr Mladek 	void *old_func;
73b700e7f0SSeth Jennings 	struct kobject kobj;
7420e55025SJason Baron 	struct list_head node;
753c33f5b9SJosh Poimboeuf 	struct list_head stack_node;
76f5e547f4SJosh Poimboeuf 	unsigned long old_size, new_size;
77e1452b60SJason Baron 	bool nop;
780dade9f3SJosh Poimboeuf 	bool patched;
79d83a7cb3SJosh Poimboeuf 	bool transition;
80b700e7f0SSeth Jennings };
81b700e7f0SSeth Jennings 
8293862e38SJoe Lawrence struct klp_object;
8393862e38SJoe Lawrence 
8493862e38SJoe Lawrence /**
8593862e38SJoe Lawrence  * struct klp_callbacks - pre/post live-(un)patch callback structure
8693862e38SJoe Lawrence  * @pre_patch:		executed before code patching
8793862e38SJoe Lawrence  * @post_patch:		executed after code patching
8893862e38SJoe Lawrence  * @pre_unpatch:	executed before code unpatching
8993862e38SJoe Lawrence  * @post_unpatch:	executed after code unpatching
9093862e38SJoe Lawrence  * @post_unpatch_enabled:	flag indicating if post-unpatch callback
9193862e38SJoe Lawrence  * 				should run
9293862e38SJoe Lawrence  *
9393862e38SJoe Lawrence  * All callbacks are optional.  Only the pre-patch callback, if provided,
9493862e38SJoe Lawrence  * will be unconditionally executed.  If the parent klp_object fails to
9593862e38SJoe Lawrence  * patch for any reason, including a non-zero error status returned from
9693862e38SJoe Lawrence  * the pre-patch callback, no further callbacks will be executed.
9793862e38SJoe Lawrence  */
9893862e38SJoe Lawrence struct klp_callbacks {
9993862e38SJoe Lawrence 	int (*pre_patch)(struct klp_object *obj);
10093862e38SJoe Lawrence 	void (*post_patch)(struct klp_object *obj);
10193862e38SJoe Lawrence 	void (*pre_unpatch)(struct klp_object *obj);
10293862e38SJoe Lawrence 	void (*post_unpatch)(struct klp_object *obj);
10393862e38SJoe Lawrence 	bool post_unpatch_enabled;
10493862e38SJoe Lawrence };
10593862e38SJoe Lawrence 
106b700e7f0SSeth Jennings /**
107b700e7f0SSeth Jennings  * struct klp_object - kernel object structure for live patching
108b700e7f0SSeth Jennings  * @name:	module name (or NULL for vmlinux)
109b700e7f0SSeth Jennings  * @funcs:	function entries for functions to be patched in the object
11093862e38SJoe Lawrence  * @callbacks:	functions to be executed pre/post (un)patching
111b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
11220e55025SJason Baron  * @func_list:	dynamic list of the function entries
11320e55025SJason Baron  * @node:	list node for klp_patch obj_list
114b700e7f0SSeth Jennings  * @mod:	kernel module associated with the patched object
115b700e7f0SSeth Jennings  *		(NULL for vmlinux)
1160430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
117e1452b60SJason Baron  * @dynamic:    temporary object for nop functions; dynamically allocated
1180dade9f3SJosh Poimboeuf  * @patched:	the object's funcs have been added to the klp_ops list
119b700e7f0SSeth Jennings  */
120b700e7f0SSeth Jennings struct klp_object {
121b700e7f0SSeth Jennings 	/* external */
122b700e7f0SSeth Jennings 	const char *name;
123b700e7f0SSeth Jennings 	struct klp_func *funcs;
12493862e38SJoe Lawrence 	struct klp_callbacks callbacks;
125b700e7f0SSeth Jennings 
126b700e7f0SSeth Jennings 	/* internal */
127cad706dfSMiroslav Benes 	struct kobject kobj;
12820e55025SJason Baron 	struct list_head func_list;
12920e55025SJason Baron 	struct list_head node;
130b700e7f0SSeth Jennings 	struct module *mod;
131e1452b60SJason Baron 	bool dynamic;
1320dade9f3SJosh Poimboeuf 	bool patched;
133b700e7f0SSeth Jennings };
134b700e7f0SSeth Jennings 
135b700e7f0SSeth Jennings /**
136b700e7f0SSeth Jennings  * struct klp_patch - patch structure for live patching
137b700e7f0SSeth Jennings  * @mod:	reference to the live patch module
138b700e7f0SSeth Jennings  * @objs:	object entries for kernel objects to be patched
139e1452b60SJason Baron  * @replace:	replace all actively used patches
140958ef1e3SPetr Mladek  * @list:	list node for global list of actively used patches
141b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
14220e55025SJason Baron  * @obj_list:	dynamic list of the object entries
1430430f78bSPetr Mladek  * @kobj_added: @kobj has been added and needs freeing
1440dade9f3SJosh Poimboeuf  * @enabled:	the patch is enabled (but operation may be incomplete)
14568007289SPetr Mladek  * @forced:	was involved in a forced transition
146958ef1e3SPetr Mladek  * @free_work:	patch cleanup from workqueue-context
1473ec24776SJosh Poimboeuf  * @finish:	for waiting till it is safe to remove the patch module
148b700e7f0SSeth Jennings  */
149b700e7f0SSeth Jennings struct klp_patch {
150b700e7f0SSeth Jennings 	/* external */
151b700e7f0SSeth Jennings 	struct module *mod;
152b700e7f0SSeth Jennings 	struct klp_object *objs;
153e1452b60SJason Baron 	bool replace;
154b700e7f0SSeth Jennings 
155b700e7f0SSeth Jennings 	/* internal */
156b700e7f0SSeth Jennings 	struct list_head list;
157b700e7f0SSeth Jennings 	struct kobject kobj;
15820e55025SJason Baron 	struct list_head obj_list;
1590dade9f3SJosh Poimboeuf 	bool enabled;
16068007289SPetr Mladek 	bool forced;
161958ef1e3SPetr Mladek 	struct work_struct free_work;
1623ec24776SJosh Poimboeuf 	struct completion finish;
163b700e7f0SSeth Jennings };
164b700e7f0SSeth Jennings 
16520e55025SJason Baron #define klp_for_each_object_static(patch, obj) \
166f09d9086SMiroslav Benes 	for (obj = patch->objs; obj->funcs || obj->name; obj++)
1678cdd043aSJiri Slaby 
168e1452b60SJason Baron #define klp_for_each_object_safe(patch, obj, tmp_obj)		\
169e1452b60SJason Baron 	list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node)
170e1452b60SJason Baron 
17120e55025SJason Baron #define klp_for_each_object(patch, obj)	\
17220e55025SJason Baron 	list_for_each_entry(obj, &patch->obj_list, node)
17320e55025SJason Baron 
17420e55025SJason Baron #define klp_for_each_func_static(obj, func) \
175f09d9086SMiroslav Benes 	for (func = obj->funcs; \
176f09d9086SMiroslav Benes 	     func->old_name || func->new_func || func->old_sympos; \
177f09d9086SMiroslav Benes 	     func++)
1788cdd043aSJiri Slaby 
179e1452b60SJason Baron #define klp_for_each_func_safe(obj, func, tmp_func)			\
180e1452b60SJason Baron 	list_for_each_entry_safe(func, tmp_func, &obj->func_list, node)
181e1452b60SJason Baron 
18220e55025SJason Baron #define klp_for_each_func(obj, func)	\
18320e55025SJason Baron 	list_for_each_entry(func, &obj->func_list, node)
18420e55025SJason Baron 
1854421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *);
186b700e7f0SSeth Jennings 
187255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch,
188255e732cSJessica Yu 				 struct klp_object *obj);
189255e732cSJessica Yu 
1907e545d6eSJessica Yu /* Called from the module loader during module coming/going states */
1917e545d6eSJessica Yu int klp_module_coming(struct module *mod);
1927e545d6eSJessica Yu void klp_module_going(struct module *mod);
1937e545d6eSJessica Yu 
194d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child);
19546c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task);
19646c5a011SJosh Poimboeuf 
197d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task)
198d83a7cb3SJosh Poimboeuf {
199d83a7cb3SJosh Poimboeuf 	return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
200d83a7cb3SJosh Poimboeuf }
201d83a7cb3SJosh Poimboeuf 
202d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void)
203d83a7cb3SJosh Poimboeuf {
204d83a7cb3SJosh Poimboeuf 	return IS_ENABLED(CONFIG_STACKTRACE) &&
205d83a7cb3SJosh Poimboeuf 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
206d83a7cb3SJosh Poimboeuf }
207d83a7cb3SJosh Poimboeuf 
208e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj,
209e91c2518SPetr Mladek 				 void *shadow_data,
210e91c2518SPetr Mladek 				 void *ctor_data);
2113b2c77d0SPetr Mladek typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data);
212e91c2518SPetr Mladek 
213439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id);
214e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id,
215e91c2518SPetr Mladek 		       size_t size, gfp_t gfp_flags,
216e91c2518SPetr Mladek 		       klp_shadow_ctor_t ctor, void *ctor_data);
217e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
218e91c2518SPetr Mladek 			      size_t size, gfp_t gfp_flags,
219e91c2518SPetr Mladek 			      klp_shadow_ctor_t ctor, void *ctor_data);
2203b2c77d0SPetr Mladek void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
2213b2c77d0SPetr Mladek void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
222439e7271SJoe Lawrence 
2237e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */
2247e545d6eSJessica Yu 
2257e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; }
2267e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {}
227d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; }
22846c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {}
229d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {}
2307e545d6eSJessica Yu 
2317e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */
2327e545d6eSJessica Yu 
233b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */
234