1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * livepatch.h - Kernel Live Patching Core
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
6*4882a593Smuzhiyun * Copyright (C) 2014 SUSE
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifndef _LINUX_LIVEPATCH_H_
10*4882a593Smuzhiyun #define _LINUX_LIVEPATCH_H_
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/ftrace.h>
14*4882a593Smuzhiyun #include <linux/completion.h>
15*4882a593Smuzhiyun #include <linux/list.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_LIVEPATCH)
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <asm/livepatch.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /* task patch states */
22*4882a593Smuzhiyun #define KLP_UNDEFINED -1
23*4882a593Smuzhiyun #define KLP_UNPATCHED 0
24*4882a593Smuzhiyun #define KLP_PATCHED 1
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun * struct klp_func - function structure for live patching
28*4882a593Smuzhiyun * @old_name: name of the function to be patched
29*4882a593Smuzhiyun * @new_func: pointer to the patched function code
30*4882a593Smuzhiyun * @old_sympos: a hint indicating which symbol position the old function
31*4882a593Smuzhiyun * can be found (optional)
32*4882a593Smuzhiyun * @old_func: pointer to the function being patched
33*4882a593Smuzhiyun * @kobj: kobject for sysfs resources
34*4882a593Smuzhiyun * @node: list node for klp_object func_list
35*4882a593Smuzhiyun * @stack_node: list node for klp_ops func_stack list
36*4882a593Smuzhiyun * @old_size: size of the old function
37*4882a593Smuzhiyun * @new_size: size of the new function
38*4882a593Smuzhiyun * @nop: temporary patch to use the original code again; dyn. allocated
39*4882a593Smuzhiyun * @patched: the func has been added to the klp_ops list
40*4882a593Smuzhiyun * @transition: the func is currently being applied or reverted
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * The patched and transition variables define the func's patching state. When
43*4882a593Smuzhiyun * patching, a func is always in one of the following states:
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * patched=0 transition=0: unpatched
46*4882a593Smuzhiyun * patched=0 transition=1: unpatched, temporary starting state
47*4882a593Smuzhiyun * patched=1 transition=1: patched, may be visible to some tasks
48*4882a593Smuzhiyun * patched=1 transition=0: patched, visible to all tasks
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * And when unpatching, it goes in the reverse order:
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * patched=1 transition=0: patched, visible to all tasks
53*4882a593Smuzhiyun * patched=1 transition=1: patched, may be visible to some tasks
54*4882a593Smuzhiyun * patched=0 transition=1: unpatched, temporary ending state
55*4882a593Smuzhiyun * patched=0 transition=0: unpatched
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun struct klp_func {
58*4882a593Smuzhiyun /* external */
59*4882a593Smuzhiyun const char *old_name;
60*4882a593Smuzhiyun void *new_func;
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * The old_sympos field is optional and can be used to resolve
63*4882a593Smuzhiyun * duplicate symbol names in livepatch objects. If this field is zero,
64*4882a593Smuzhiyun * it is expected the symbol is unique, otherwise patching fails. If
65*4882a593Smuzhiyun * this value is greater than zero then that occurrence of the symbol
66*4882a593Smuzhiyun * in kallsyms for the given object is used.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun unsigned long old_sympos;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* internal */
71*4882a593Smuzhiyun void *old_func;
72*4882a593Smuzhiyun struct kobject kobj;
73*4882a593Smuzhiyun struct list_head node;
74*4882a593Smuzhiyun struct list_head stack_node;
75*4882a593Smuzhiyun unsigned long old_size, new_size;
76*4882a593Smuzhiyun bool nop;
77*4882a593Smuzhiyun bool patched;
78*4882a593Smuzhiyun bool transition;
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun struct klp_object;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun * struct klp_callbacks - pre/post live-(un)patch callback structure
85*4882a593Smuzhiyun * @pre_patch: executed before code patching
86*4882a593Smuzhiyun * @post_patch: executed after code patching
87*4882a593Smuzhiyun * @pre_unpatch: executed before code unpatching
88*4882a593Smuzhiyun * @post_unpatch: executed after code unpatching
89*4882a593Smuzhiyun * @post_unpatch_enabled: flag indicating if post-unpatch callback
90*4882a593Smuzhiyun * should run
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * All callbacks are optional. Only the pre-patch callback, if provided,
93*4882a593Smuzhiyun * will be unconditionally executed. If the parent klp_object fails to
94*4882a593Smuzhiyun * patch for any reason, including a non-zero error status returned from
95*4882a593Smuzhiyun * the pre-patch callback, no further callbacks will be executed.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun struct klp_callbacks {
98*4882a593Smuzhiyun int (*pre_patch)(struct klp_object *obj);
99*4882a593Smuzhiyun void (*post_patch)(struct klp_object *obj);
100*4882a593Smuzhiyun void (*pre_unpatch)(struct klp_object *obj);
101*4882a593Smuzhiyun void (*post_unpatch)(struct klp_object *obj);
102*4882a593Smuzhiyun bool post_unpatch_enabled;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun * struct klp_object - kernel object structure for live patching
107*4882a593Smuzhiyun * @name: module name (or NULL for vmlinux)
108*4882a593Smuzhiyun * @funcs: function entries for functions to be patched in the object
109*4882a593Smuzhiyun * @callbacks: functions to be executed pre/post (un)patching
110*4882a593Smuzhiyun * @kobj: kobject for sysfs resources
111*4882a593Smuzhiyun * @func_list: dynamic list of the function entries
112*4882a593Smuzhiyun * @node: list node for klp_patch obj_list
113*4882a593Smuzhiyun * @mod: kernel module associated with the patched object
114*4882a593Smuzhiyun * (NULL for vmlinux)
115*4882a593Smuzhiyun * @dynamic: temporary object for nop functions; dynamically allocated
116*4882a593Smuzhiyun * @patched: the object's funcs have been added to the klp_ops list
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun struct klp_object {
119*4882a593Smuzhiyun /* external */
120*4882a593Smuzhiyun const char *name;
121*4882a593Smuzhiyun struct klp_func *funcs;
122*4882a593Smuzhiyun struct klp_callbacks callbacks;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* internal */
125*4882a593Smuzhiyun struct kobject kobj;
126*4882a593Smuzhiyun struct list_head func_list;
127*4882a593Smuzhiyun struct list_head node;
128*4882a593Smuzhiyun struct module *mod;
129*4882a593Smuzhiyun bool dynamic;
130*4882a593Smuzhiyun bool patched;
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun * struct klp_state - state of the system modified by the livepatch
135*4882a593Smuzhiyun * @id: system state identifier (non-zero)
136*4882a593Smuzhiyun * @version: version of the change
137*4882a593Smuzhiyun * @data: custom data
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun struct klp_state {
140*4882a593Smuzhiyun unsigned long id;
141*4882a593Smuzhiyun unsigned int version;
142*4882a593Smuzhiyun void *data;
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * struct klp_patch - patch structure for live patching
147*4882a593Smuzhiyun * @mod: reference to the live patch module
148*4882a593Smuzhiyun * @objs: object entries for kernel objects to be patched
149*4882a593Smuzhiyun * @states: system states that can get modified
150*4882a593Smuzhiyun * @replace: replace all actively used patches
151*4882a593Smuzhiyun * @list: list node for global list of actively used patches
152*4882a593Smuzhiyun * @kobj: kobject for sysfs resources
153*4882a593Smuzhiyun * @obj_list: dynamic list of the object entries
154*4882a593Smuzhiyun * @enabled: the patch is enabled (but operation may be incomplete)
155*4882a593Smuzhiyun * @forced: was involved in a forced transition
156*4882a593Smuzhiyun * @free_work: patch cleanup from workqueue-context
157*4882a593Smuzhiyun * @finish: for waiting till it is safe to remove the patch module
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun struct klp_patch {
160*4882a593Smuzhiyun /* external */
161*4882a593Smuzhiyun struct module *mod;
162*4882a593Smuzhiyun struct klp_object *objs;
163*4882a593Smuzhiyun struct klp_state *states;
164*4882a593Smuzhiyun bool replace;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* internal */
167*4882a593Smuzhiyun struct list_head list;
168*4882a593Smuzhiyun struct kobject kobj;
169*4882a593Smuzhiyun struct list_head obj_list;
170*4882a593Smuzhiyun bool enabled;
171*4882a593Smuzhiyun bool forced;
172*4882a593Smuzhiyun struct work_struct free_work;
173*4882a593Smuzhiyun struct completion finish;
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun #define klp_for_each_object_static(patch, obj) \
177*4882a593Smuzhiyun for (obj = patch->objs; obj->funcs || obj->name; obj++)
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun #define klp_for_each_object_safe(patch, obj, tmp_obj) \
180*4882a593Smuzhiyun list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node)
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun #define klp_for_each_object(patch, obj) \
183*4882a593Smuzhiyun list_for_each_entry(obj, &patch->obj_list, node)
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun #define klp_for_each_func_static(obj, func) \
186*4882a593Smuzhiyun for (func = obj->funcs; \
187*4882a593Smuzhiyun func->old_name || func->new_func || func->old_sympos; \
188*4882a593Smuzhiyun func++)
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun #define klp_for_each_func_safe(obj, func, tmp_func) \
191*4882a593Smuzhiyun list_for_each_entry_safe(func, tmp_func, &obj->func_list, node)
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun #define klp_for_each_func(obj, func) \
194*4882a593Smuzhiyun list_for_each_entry(func, &obj->func_list, node)
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun int klp_enable_patch(struct klp_patch *);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* Called from the module loader during module coming/going states */
199*4882a593Smuzhiyun int klp_module_coming(struct module *mod);
200*4882a593Smuzhiyun void klp_module_going(struct module *mod);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun void klp_copy_process(struct task_struct *child);
203*4882a593Smuzhiyun void klp_update_patch_state(struct task_struct *task);
204*4882a593Smuzhiyun
klp_patch_pending(struct task_struct * task)205*4882a593Smuzhiyun static inline bool klp_patch_pending(struct task_struct *task)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
klp_have_reliable_stack(void)210*4882a593Smuzhiyun static inline bool klp_have_reliable_stack(void)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun return IS_ENABLED(CONFIG_STACKTRACE) &&
213*4882a593Smuzhiyun IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun typedef int (*klp_shadow_ctor_t)(void *obj,
217*4882a593Smuzhiyun void *shadow_data,
218*4882a593Smuzhiyun void *ctor_data);
219*4882a593Smuzhiyun typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun void *klp_shadow_get(void *obj, unsigned long id);
222*4882a593Smuzhiyun void *klp_shadow_alloc(void *obj, unsigned long id,
223*4882a593Smuzhiyun size_t size, gfp_t gfp_flags,
224*4882a593Smuzhiyun klp_shadow_ctor_t ctor, void *ctor_data);
225*4882a593Smuzhiyun void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
226*4882a593Smuzhiyun size_t size, gfp_t gfp_flags,
227*4882a593Smuzhiyun klp_shadow_ctor_t ctor, void *ctor_data);
228*4882a593Smuzhiyun void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
229*4882a593Smuzhiyun void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id);
232*4882a593Smuzhiyun struct klp_state *klp_get_prev_state(unsigned long id);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
235*4882a593Smuzhiyun const char *shstrtab, const char *strtab,
236*4882a593Smuzhiyun unsigned int symindex, unsigned int secindex,
237*4882a593Smuzhiyun const char *objname);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun #else /* !CONFIG_LIVEPATCH */
240*4882a593Smuzhiyun
klp_module_coming(struct module * mod)241*4882a593Smuzhiyun static inline int klp_module_coming(struct module *mod) { return 0; }
klp_module_going(struct module * mod)242*4882a593Smuzhiyun static inline void klp_module_going(struct module *mod) {}
klp_patch_pending(struct task_struct * task)243*4882a593Smuzhiyun static inline bool klp_patch_pending(struct task_struct *task) { return false; }
klp_update_patch_state(struct task_struct * task)244*4882a593Smuzhiyun static inline void klp_update_patch_state(struct task_struct *task) {}
klp_copy_process(struct task_struct * child)245*4882a593Smuzhiyun static inline void klp_copy_process(struct task_struct *child) {}
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun 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)248*4882a593Smuzhiyun int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
249*4882a593Smuzhiyun const char *shstrtab, const char *strtab,
250*4882a593Smuzhiyun unsigned int symindex, unsigned int secindex,
251*4882a593Smuzhiyun const char *objname)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun return 0;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun #endif /* CONFIG_LIVEPATCH */
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun #endif /* _LINUX_LIVEPATCH_H_ */
259