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 <[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 38e1452b60SJason Baron * @nop: temporary patch to use the original code again; dyn. allocated 390dade9f3SJosh Poimboeuf * @patched: the func has been added to the klp_ops list 40d83a7cb3SJosh Poimboeuf * @transition: the func is currently being applied or reverted 41d83a7cb3SJosh Poimboeuf * 42d83a7cb3SJosh Poimboeuf * The patched and transition variables define the func's patching state. When 43d83a7cb3SJosh Poimboeuf * patching, a func is always in one of the following states: 44d83a7cb3SJosh Poimboeuf * 45d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 46d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary starting state 47d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 48d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 49d83a7cb3SJosh Poimboeuf * 50d83a7cb3SJosh Poimboeuf * And when unpatching, it goes in the reverse order: 51d83a7cb3SJosh Poimboeuf * 52d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 53d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 54d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary ending state 55d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 56b700e7f0SSeth Jennings */ 57b700e7f0SSeth Jennings struct klp_func { 58b700e7f0SSeth Jennings /* external */ 59b700e7f0SSeth Jennings const char *old_name; 60b700e7f0SSeth Jennings void *new_func; 61b700e7f0SSeth Jennings /* 62b2b018efSChris J Arges * The old_sympos field is optional and can be used to resolve 63b2b018efSChris J Arges * duplicate symbol names in livepatch objects. If this field is zero, 64b2b018efSChris J Arges * it is expected the symbol is unique, otherwise patching fails. If 65b2b018efSChris J Arges * this value is greater than zero then that occurrence of the symbol 66b2b018efSChris J Arges * in kallsyms for the given object is used. 67b700e7f0SSeth Jennings */ 68b2b018efSChris J Arges unsigned long old_sympos; 69b700e7f0SSeth Jennings 70b700e7f0SSeth Jennings /* internal */ 7119514910SPetr Mladek void *old_func; 72b700e7f0SSeth Jennings struct kobject kobj; 7320e55025SJason Baron struct list_head node; 743c33f5b9SJosh Poimboeuf struct list_head stack_node; 75f5e547f4SJosh Poimboeuf unsigned long old_size, new_size; 76e1452b60SJason Baron bool nop; 770dade9f3SJosh Poimboeuf bool patched; 78d83a7cb3SJosh Poimboeuf bool transition; 79b700e7f0SSeth Jennings }; 80b700e7f0SSeth Jennings 8193862e38SJoe Lawrence struct klp_object; 8293862e38SJoe Lawrence 8393862e38SJoe Lawrence /** 8493862e38SJoe Lawrence * struct klp_callbacks - pre/post live-(un)patch callback structure 8593862e38SJoe Lawrence * @pre_patch: executed before code patching 8693862e38SJoe Lawrence * @post_patch: executed after code patching 8793862e38SJoe Lawrence * @pre_unpatch: executed before code unpatching 8893862e38SJoe Lawrence * @post_unpatch: executed after code unpatching 8993862e38SJoe Lawrence * @post_unpatch_enabled: flag indicating if post-unpatch callback 9093862e38SJoe Lawrence * should run 9193862e38SJoe Lawrence * 9293862e38SJoe Lawrence * All callbacks are optional. Only the pre-patch callback, if provided, 9393862e38SJoe Lawrence * will be unconditionally executed. If the parent klp_object fails to 9493862e38SJoe Lawrence * patch for any reason, including a non-zero error status returned from 9593862e38SJoe Lawrence * the pre-patch callback, no further callbacks will be executed. 9693862e38SJoe Lawrence */ 9793862e38SJoe Lawrence struct klp_callbacks { 9893862e38SJoe Lawrence int (*pre_patch)(struct klp_object *obj); 9993862e38SJoe Lawrence void (*post_patch)(struct klp_object *obj); 10093862e38SJoe Lawrence void (*pre_unpatch)(struct klp_object *obj); 10193862e38SJoe Lawrence void (*post_unpatch)(struct klp_object *obj); 10293862e38SJoe Lawrence bool post_unpatch_enabled; 10393862e38SJoe Lawrence }; 10493862e38SJoe Lawrence 105b700e7f0SSeth Jennings /** 106b700e7f0SSeth Jennings * struct klp_object - kernel object structure for live patching 107b700e7f0SSeth Jennings * @name: module name (or NULL for vmlinux) 108b700e7f0SSeth Jennings * @funcs: function entries for functions to be patched in the object 10993862e38SJoe Lawrence * @callbacks: functions to be executed pre/post (un)patching 110b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 11120e55025SJason Baron * @func_list: dynamic list of the function entries 11220e55025SJason Baron * @node: list node for klp_patch obj_list 113b700e7f0SSeth Jennings * @mod: kernel module associated with the patched object 114b700e7f0SSeth Jennings * (NULL for vmlinux) 115e1452b60SJason Baron * @dynamic: temporary object for nop functions; dynamically allocated 1160dade9f3SJosh Poimboeuf * @patched: the object's funcs have been added to the klp_ops list 117b700e7f0SSeth Jennings */ 118b700e7f0SSeth Jennings struct klp_object { 119b700e7f0SSeth Jennings /* external */ 120b700e7f0SSeth Jennings const char *name; 121b700e7f0SSeth Jennings struct klp_func *funcs; 12293862e38SJoe Lawrence struct klp_callbacks callbacks; 123b700e7f0SSeth Jennings 124b700e7f0SSeth Jennings /* internal */ 125cad706dfSMiroslav Benes struct kobject kobj; 12620e55025SJason Baron struct list_head func_list; 12720e55025SJason Baron struct list_head node; 128b700e7f0SSeth Jennings struct module *mod; 129e1452b60SJason Baron bool dynamic; 1300dade9f3SJosh Poimboeuf bool patched; 131b700e7f0SSeth Jennings }; 132b700e7f0SSeth Jennings 133b700e7f0SSeth Jennings /** 13473727f4dSPetr Mladek * struct klp_state - state of the system modified by the livepatch 13573727f4dSPetr Mladek * @id: system state identifier (non-zero) 13692c9abf5SPetr Mladek * @version: version of the change 13773727f4dSPetr Mladek * @data: custom data 13873727f4dSPetr Mladek */ 13973727f4dSPetr Mladek struct klp_state { 14073727f4dSPetr Mladek unsigned long id; 14192c9abf5SPetr Mladek unsigned int version; 14273727f4dSPetr Mladek void *data; 14373727f4dSPetr Mladek }; 14473727f4dSPetr Mladek 14573727f4dSPetr Mladek /** 146b700e7f0SSeth Jennings * struct klp_patch - patch structure for live patching 147b700e7f0SSeth Jennings * @mod: reference to the live patch module 148b700e7f0SSeth Jennings * @objs: object entries for kernel objects to be patched 14973727f4dSPetr Mladek * @states: system states that can get modified 150e1452b60SJason Baron * @replace: replace all actively used patches 151958ef1e3SPetr Mladek * @list: list node for global list of actively used patches 152b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 15320e55025SJason Baron * @obj_list: dynamic list of the object entries 1540dade9f3SJosh Poimboeuf * @enabled: the patch is enabled (but operation may be incomplete) 15568007289SPetr Mladek * @forced: was involved in a forced transition 156958ef1e3SPetr Mladek * @free_work: patch cleanup from workqueue-context 1573ec24776SJosh Poimboeuf * @finish: for waiting till it is safe to remove the patch module 158b700e7f0SSeth Jennings */ 159b700e7f0SSeth Jennings struct klp_patch { 160b700e7f0SSeth Jennings /* external */ 161b700e7f0SSeth Jennings struct module *mod; 162b700e7f0SSeth Jennings struct klp_object *objs; 16373727f4dSPetr Mladek struct klp_state *states; 164e1452b60SJason Baron bool replace; 165b700e7f0SSeth Jennings 166b700e7f0SSeth Jennings /* internal */ 167b700e7f0SSeth Jennings struct list_head list; 168b700e7f0SSeth Jennings struct kobject kobj; 16920e55025SJason Baron struct list_head obj_list; 1700dade9f3SJosh Poimboeuf bool enabled; 17168007289SPetr Mladek bool forced; 172958ef1e3SPetr Mladek struct work_struct free_work; 1733ec24776SJosh Poimboeuf struct completion finish; 174b700e7f0SSeth Jennings }; 175b700e7f0SSeth Jennings 17620e55025SJason Baron #define klp_for_each_object_static(patch, obj) \ 177f09d9086SMiroslav Benes for (obj = patch->objs; obj->funcs || obj->name; obj++) 1788cdd043aSJiri Slaby 179e1452b60SJason Baron #define klp_for_each_object_safe(patch, obj, tmp_obj) \ 180e1452b60SJason Baron list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node) 181e1452b60SJason Baron 18220e55025SJason Baron #define klp_for_each_object(patch, obj) \ 18320e55025SJason Baron list_for_each_entry(obj, &patch->obj_list, node) 18420e55025SJason Baron 18520e55025SJason Baron #define klp_for_each_func_static(obj, func) \ 186f09d9086SMiroslav Benes for (func = obj->funcs; \ 187f09d9086SMiroslav Benes func->old_name || func->new_func || func->old_sympos; \ 188f09d9086SMiroslav Benes func++) 1898cdd043aSJiri Slaby 190e1452b60SJason Baron #define klp_for_each_func_safe(obj, func, tmp_func) \ 191e1452b60SJason Baron list_for_each_entry_safe(func, tmp_func, &obj->func_list, node) 192e1452b60SJason Baron 19320e55025SJason Baron #define klp_for_each_func(obj, func) \ 19420e55025SJason Baron list_for_each_entry(func, &obj->func_list, node) 19520e55025SJason Baron 1964421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *); 197b700e7f0SSeth Jennings 198255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch, 199255e732cSJessica Yu struct klp_object *obj); 200255e732cSJessica Yu 2017e545d6eSJessica Yu /* Called from the module loader during module coming/going states */ 2027e545d6eSJessica Yu int klp_module_coming(struct module *mod); 2037e545d6eSJessica Yu void klp_module_going(struct module *mod); 2047e545d6eSJessica Yu 205d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child); 20646c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task); 20746c5a011SJosh Poimboeuf 208d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) 209d83a7cb3SJosh Poimboeuf { 210d83a7cb3SJosh Poimboeuf return test_tsk_thread_flag(task, TIF_PATCH_PENDING); 211d83a7cb3SJosh Poimboeuf } 212d83a7cb3SJosh Poimboeuf 213d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void) 214d83a7cb3SJosh Poimboeuf { 215d83a7cb3SJosh Poimboeuf return IS_ENABLED(CONFIG_STACKTRACE) && 216d83a7cb3SJosh Poimboeuf IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE); 217d83a7cb3SJosh Poimboeuf } 218d83a7cb3SJosh Poimboeuf 219e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj, 220e91c2518SPetr Mladek void *shadow_data, 221e91c2518SPetr Mladek void *ctor_data); 2223b2c77d0SPetr Mladek typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data); 223e91c2518SPetr Mladek 224439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id); 225e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id, 226e91c2518SPetr Mladek size_t size, gfp_t gfp_flags, 227e91c2518SPetr Mladek klp_shadow_ctor_t ctor, void *ctor_data); 228e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id, 229e91c2518SPetr Mladek size_t size, gfp_t gfp_flags, 230e91c2518SPetr Mladek klp_shadow_ctor_t ctor, void *ctor_data); 2313b2c77d0SPetr Mladek void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor); 2323b2c77d0SPetr Mladek void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor); 233439e7271SJoe Lawrence 23473727f4dSPetr Mladek struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id); 23573727f4dSPetr Mladek struct klp_state *klp_get_prev_state(unsigned long id); 23673727f4dSPetr Mladek 237*7c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, 238*7c8e2bddSJosh Poimboeuf const char *shstrtab, const char *strtab, 239*7c8e2bddSJosh Poimboeuf unsigned int symindex, unsigned int secindex, 240*7c8e2bddSJosh Poimboeuf const char *objname); 241*7c8e2bddSJosh Poimboeuf 2427e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */ 2437e545d6eSJessica Yu 2447e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; } 2457e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {} 246d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; } 24746c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {} 248d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {} 2497e545d6eSJessica Yu 250*7c8e2bddSJosh Poimboeuf static inline 251*7c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs, 252*7c8e2bddSJosh Poimboeuf const char *shstrtab, const char *strtab, 253*7c8e2bddSJosh Poimboeuf unsigned int symindex, unsigned int secindex, 254*7c8e2bddSJosh Poimboeuf const char *objname) 255*7c8e2bddSJosh Poimboeuf { 256*7c8e2bddSJosh Poimboeuf return 0; 257*7c8e2bddSJosh Poimboeuf } 258*7c8e2bddSJosh Poimboeuf 2597e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */ 2607e545d6eSJessica Yu 261b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */ 262