xref: /openbmc/linux/include/linux/livepatch.h (revision e3ff7c60)
11ccea77eSThomas 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 <sjenning@redhat.com>
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>
16*e3ff7c60SJosh Poimboeuf #include <linux/livepatch_sched.h>
17b700e7f0SSeth Jennings 
187e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH)
197e545d6eSJessica Yu 
20d83a7cb3SJosh Poimboeuf /* task patch states */
21d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED	-1
22d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED	 0
23d83a7cb3SJosh Poimboeuf #define KLP_PATCHED	 1
24d83a7cb3SJosh Poimboeuf 
25b700e7f0SSeth Jennings /**
26b700e7f0SSeth Jennings  * struct klp_func - function structure for live patching
27b700e7f0SSeth Jennings  * @old_name:	name of the function to be patched
28b700e7f0SSeth Jennings  * @new_func:	pointer to the patched function code
29b2b018efSChris J Arges  * @old_sympos: a hint indicating which symbol position the old function
30b2b018efSChris J Arges  *		can be found (optional)
3119514910SPetr Mladek  * @old_func:	pointer to the function being patched
32b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
3320e55025SJason Baron  * @node:	list node for klp_object func_list
343c33f5b9SJosh Poimboeuf  * @stack_node:	list node for klp_ops func_stack list
35f5e547f4SJosh Poimboeuf  * @old_size:	size of the old function
36f5e547f4SJosh Poimboeuf  * @new_size:	size of the new function
37e1452b60SJason Baron  * @nop:        temporary patch to use the original code again; dyn. allocated
380dade9f3SJosh Poimboeuf  * @patched:	the func has been added to the klp_ops list
39d83a7cb3SJosh Poimboeuf  * @transition:	the func is currently being applied or reverted
40d83a7cb3SJosh Poimboeuf  *
41d83a7cb3SJosh Poimboeuf  * The patched and transition variables define the func's patching state.  When
42d83a7cb3SJosh Poimboeuf  * patching, a func is always in one of the following states:
43d83a7cb3SJosh Poimboeuf  *
44d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
45d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary starting state
46d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
47d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
48d83a7cb3SJosh Poimboeuf  *
49d83a7cb3SJosh Poimboeuf  * And when unpatching, it goes in the reverse order:
50d83a7cb3SJosh Poimboeuf  *
51d83a7cb3SJosh Poimboeuf  *   patched=1 transition=0: patched, visible to all tasks
52d83a7cb3SJosh Poimboeuf  *   patched=1 transition=1: patched, may be visible to some tasks
53d83a7cb3SJosh Poimboeuf  *   patched=0 transition=1: unpatched, temporary ending state
54d83a7cb3SJosh Poimboeuf  *   patched=0 transition=0: unpatched
55b700e7f0SSeth Jennings  */
56b700e7f0SSeth Jennings struct klp_func {
57b700e7f0SSeth Jennings 	/* external */
58b700e7f0SSeth Jennings 	const char *old_name;
59b700e7f0SSeth Jennings 	void *new_func;
60b700e7f0SSeth Jennings 	/*
61b2b018efSChris J Arges 	 * The old_sympos field is optional and can be used to resolve
62b2b018efSChris J Arges 	 * duplicate symbol names in livepatch objects. If this field is zero,
63b2b018efSChris J Arges 	 * it is expected the symbol is unique, otherwise patching fails. If
64b2b018efSChris J Arges 	 * this value is greater than zero then that occurrence of the symbol
65b2b018efSChris J Arges 	 * in kallsyms for the given object is used.
66b700e7f0SSeth Jennings 	 */
67b2b018efSChris J Arges 	unsigned long old_sympos;
68b700e7f0SSeth Jennings 
69b700e7f0SSeth Jennings 	/* internal */
7019514910SPetr Mladek 	void *old_func;
71b700e7f0SSeth Jennings 	struct kobject kobj;
7220e55025SJason Baron 	struct list_head node;
733c33f5b9SJosh Poimboeuf 	struct list_head stack_node;
74f5e547f4SJosh Poimboeuf 	unsigned long old_size, new_size;
75e1452b60SJason Baron 	bool nop;
760dade9f3SJosh Poimboeuf 	bool patched;
77d83a7cb3SJosh Poimboeuf 	bool transition;
78b700e7f0SSeth Jennings };
79b700e7f0SSeth Jennings 
8093862e38SJoe Lawrence struct klp_object;
8193862e38SJoe Lawrence 
8293862e38SJoe Lawrence /**
8393862e38SJoe Lawrence  * struct klp_callbacks - pre/post live-(un)patch callback structure
8493862e38SJoe Lawrence  * @pre_patch:		executed before code patching
8593862e38SJoe Lawrence  * @post_patch:		executed after code patching
8693862e38SJoe Lawrence  * @pre_unpatch:	executed before code unpatching
8793862e38SJoe Lawrence  * @post_unpatch:	executed after code unpatching
8893862e38SJoe Lawrence  * @post_unpatch_enabled:	flag indicating if post-unpatch callback
8993862e38SJoe Lawrence  * 				should run
9093862e38SJoe Lawrence  *
9193862e38SJoe Lawrence  * All callbacks are optional.  Only the pre-patch callback, if provided,
9293862e38SJoe Lawrence  * will be unconditionally executed.  If the parent klp_object fails to
9393862e38SJoe Lawrence  * patch for any reason, including a non-zero error status returned from
9493862e38SJoe Lawrence  * the pre-patch callback, no further callbacks will be executed.
9593862e38SJoe Lawrence  */
9693862e38SJoe Lawrence struct klp_callbacks {
9793862e38SJoe Lawrence 	int (*pre_patch)(struct klp_object *obj);
9893862e38SJoe Lawrence 	void (*post_patch)(struct klp_object *obj);
9993862e38SJoe Lawrence 	void (*pre_unpatch)(struct klp_object *obj);
10093862e38SJoe Lawrence 	void (*post_unpatch)(struct klp_object *obj);
10193862e38SJoe Lawrence 	bool post_unpatch_enabled;
10293862e38SJoe Lawrence };
10393862e38SJoe Lawrence 
104b700e7f0SSeth Jennings /**
105b700e7f0SSeth Jennings  * struct klp_object - kernel object structure for live patching
106b700e7f0SSeth Jennings  * @name:	module name (or NULL for vmlinux)
107b700e7f0SSeth Jennings  * @funcs:	function entries for functions to be patched in the object
10893862e38SJoe Lawrence  * @callbacks:	functions to be executed pre/post (un)patching
109b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
11020e55025SJason Baron  * @func_list:	dynamic list of the function entries
11120e55025SJason Baron  * @node:	list node for klp_patch obj_list
112b700e7f0SSeth Jennings  * @mod:	kernel module associated with the patched object
113b700e7f0SSeth Jennings  *		(NULL for vmlinux)
114e1452b60SJason Baron  * @dynamic:    temporary object for nop functions; dynamically allocated
1150dade9f3SJosh Poimboeuf  * @patched:	the object's funcs have been added to the klp_ops list
116b700e7f0SSeth Jennings  */
117b700e7f0SSeth Jennings struct klp_object {
118b700e7f0SSeth Jennings 	/* external */
119b700e7f0SSeth Jennings 	const char *name;
120b700e7f0SSeth Jennings 	struct klp_func *funcs;
12193862e38SJoe Lawrence 	struct klp_callbacks callbacks;
122b700e7f0SSeth Jennings 
123b700e7f0SSeth Jennings 	/* internal */
124cad706dfSMiroslav Benes 	struct kobject kobj;
12520e55025SJason Baron 	struct list_head func_list;
12620e55025SJason Baron 	struct list_head node;
127b700e7f0SSeth Jennings 	struct module *mod;
128e1452b60SJason Baron 	bool dynamic;
1290dade9f3SJosh Poimboeuf 	bool patched;
130b700e7f0SSeth Jennings };
131b700e7f0SSeth Jennings 
132b700e7f0SSeth Jennings /**
13373727f4dSPetr Mladek  * struct klp_state - state of the system modified by the livepatch
13473727f4dSPetr Mladek  * @id:		system state identifier (non-zero)
13592c9abf5SPetr Mladek  * @version:	version of the change
13673727f4dSPetr Mladek  * @data:	custom data
13773727f4dSPetr Mladek  */
13873727f4dSPetr Mladek struct klp_state {
13973727f4dSPetr Mladek 	unsigned long id;
14092c9abf5SPetr Mladek 	unsigned int version;
14173727f4dSPetr Mladek 	void *data;
14273727f4dSPetr Mladek };
14373727f4dSPetr Mladek 
14473727f4dSPetr Mladek /**
145b700e7f0SSeth Jennings  * struct klp_patch - patch structure for live patching
146b700e7f0SSeth Jennings  * @mod:	reference to the live patch module
147b700e7f0SSeth Jennings  * @objs:	object entries for kernel objects to be patched
14873727f4dSPetr Mladek  * @states:	system states that can get modified
149e1452b60SJason Baron  * @replace:	replace all actively used patches
150958ef1e3SPetr Mladek  * @list:	list node for global list of actively used patches
151b700e7f0SSeth Jennings  * @kobj:	kobject for sysfs resources
15220e55025SJason Baron  * @obj_list:	dynamic list of the object entries
1530dade9f3SJosh Poimboeuf  * @enabled:	the patch is enabled (but operation may be incomplete)
15468007289SPetr Mladek  * @forced:	was involved in a forced transition
155958ef1e3SPetr Mladek  * @free_work:	patch cleanup from workqueue-context
1563ec24776SJosh Poimboeuf  * @finish:	for waiting till it is safe to remove the patch module
157b700e7f0SSeth Jennings  */
158b700e7f0SSeth Jennings struct klp_patch {
159b700e7f0SSeth Jennings 	/* external */
160b700e7f0SSeth Jennings 	struct module *mod;
161b700e7f0SSeth Jennings 	struct klp_object *objs;
16273727f4dSPetr Mladek 	struct klp_state *states;
163e1452b60SJason Baron 	bool replace;
164b700e7f0SSeth Jennings 
165b700e7f0SSeth Jennings 	/* internal */
166b700e7f0SSeth Jennings 	struct list_head list;
167b700e7f0SSeth Jennings 	struct kobject kobj;
16820e55025SJason Baron 	struct list_head obj_list;
1690dade9f3SJosh Poimboeuf 	bool enabled;
17068007289SPetr Mladek 	bool forced;
171958ef1e3SPetr Mladek 	struct work_struct free_work;
1723ec24776SJosh Poimboeuf 	struct completion finish;
173b700e7f0SSeth Jennings };
174b700e7f0SSeth Jennings 
17520e55025SJason Baron #define klp_for_each_object_static(patch, obj) \
176f09d9086SMiroslav Benes 	for (obj = patch->objs; obj->funcs || obj->name; obj++)
1778cdd043aSJiri Slaby 
178e1452b60SJason Baron #define klp_for_each_object_safe(patch, obj, tmp_obj)		\
179e1452b60SJason Baron 	list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node)
180e1452b60SJason Baron 
18120e55025SJason Baron #define klp_for_each_object(patch, obj)	\
18220e55025SJason Baron 	list_for_each_entry(obj, &patch->obj_list, node)
18320e55025SJason Baron 
18420e55025SJason Baron #define klp_for_each_func_static(obj, func) \
185f09d9086SMiroslav Benes 	for (func = obj->funcs; \
186f09d9086SMiroslav Benes 	     func->old_name || func->new_func || func->old_sympos; \
187f09d9086SMiroslav Benes 	     func++)
1888cdd043aSJiri Slaby 
189e1452b60SJason Baron #define klp_for_each_func_safe(obj, func, tmp_func)			\
190e1452b60SJason Baron 	list_for_each_entry_safe(func, tmp_func, &obj->func_list, node)
191e1452b60SJason Baron 
19220e55025SJason Baron #define klp_for_each_func(obj, func)	\
19320e55025SJason Baron 	list_for_each_entry(func, &obj->func_list, node)
19420e55025SJason Baron 
1954421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *);
196b700e7f0SSeth Jennings 
1977e545d6eSJessica Yu /* Called from the module loader during module coming/going states */
1987e545d6eSJessica Yu int klp_module_coming(struct module *mod);
1997e545d6eSJessica Yu void klp_module_going(struct module *mod);
2007e545d6eSJessica Yu 
201d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child);
20246c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task);
20346c5a011SJosh Poimboeuf 
klp_patch_pending(struct task_struct * task)204d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task)
205d83a7cb3SJosh Poimboeuf {
206d83a7cb3SJosh Poimboeuf 	return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
207d83a7cb3SJosh Poimboeuf }
208d83a7cb3SJosh Poimboeuf 
klp_have_reliable_stack(void)209d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void)
210d83a7cb3SJosh Poimboeuf {
211d83a7cb3SJosh Poimboeuf 	return IS_ENABLED(CONFIG_STACKTRACE) &&
212d83a7cb3SJosh Poimboeuf 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
213d83a7cb3SJosh Poimboeuf }
214d83a7cb3SJosh Poimboeuf 
215e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj,
216e91c2518SPetr Mladek 				 void *shadow_data,
217e91c2518SPetr Mladek 				 void *ctor_data);
2183b2c77d0SPetr Mladek typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data);
219e91c2518SPetr Mladek 
220439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id);
221e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id,
222e91c2518SPetr Mladek 		       size_t size, gfp_t gfp_flags,
223e91c2518SPetr Mladek 		       klp_shadow_ctor_t ctor, void *ctor_data);
224e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
225e91c2518SPetr Mladek 			      size_t size, gfp_t gfp_flags,
226e91c2518SPetr Mladek 			      klp_shadow_ctor_t ctor, void *ctor_data);
2273b2c77d0SPetr Mladek void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
2283b2c77d0SPetr Mladek void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
229439e7271SJoe Lawrence 
23073727f4dSPetr Mladek struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id);
23173727f4dSPetr Mladek struct klp_state *klp_get_prev_state(unsigned long id);
23273727f4dSPetr Mladek 
2337c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
2347c8e2bddSJosh Poimboeuf 			     const char *shstrtab, const char *strtab,
2357c8e2bddSJosh Poimboeuf 			     unsigned int symindex, unsigned int secindex,
2367c8e2bddSJosh Poimboeuf 			     const char *objname);
2377c8e2bddSJosh Poimboeuf 
2387e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */
2397e545d6eSJessica Yu 
klp_module_coming(struct module * mod)2407e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; }
klp_module_going(struct module * mod)2417e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {}
klp_patch_pending(struct task_struct * task)242d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; }
klp_update_patch_state(struct task_struct * task)24346c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {}
klp_copy_process(struct task_struct * child)244d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {}
2457e545d6eSJessica Yu 
2467c8e2bddSJosh Poimboeuf static inline
klp_apply_section_relocs(struct module * pmod,Elf_Shdr * sechdrs,const char * shstrtab,const char * strtab,unsigned int symindex,unsigned int secindex,const char * objname)2477c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
2487c8e2bddSJosh Poimboeuf 			     const char *shstrtab, const char *strtab,
2497c8e2bddSJosh Poimboeuf 			     unsigned int symindex, unsigned int secindex,
2507c8e2bddSJosh Poimboeuf 			     const char *objname)
2517c8e2bddSJosh Poimboeuf {
2527c8e2bddSJosh Poimboeuf 	return 0;
2537c8e2bddSJosh Poimboeuf }
2547c8e2bddSJosh Poimboeuf 
2557e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */
2567e545d6eSJessica Yu 
257b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */
258