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> 27b700e7f0SSeth Jennings 287e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH) 297e545d6eSJessica Yu 30b700e7f0SSeth Jennings #include <asm/livepatch.h> 31b700e7f0SSeth Jennings 32d83a7cb3SJosh Poimboeuf /* task patch states */ 33d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED -1 34d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED 0 35d83a7cb3SJosh Poimboeuf #define KLP_PATCHED 1 36d83a7cb3SJosh Poimboeuf 37b700e7f0SSeth Jennings /** 38b700e7f0SSeth Jennings * struct klp_func - function structure for live patching 39b700e7f0SSeth Jennings * @old_name: name of the function to be patched 40b700e7f0SSeth Jennings * @new_func: pointer to the patched function code 41b2b018efSChris J Arges * @old_sympos: a hint indicating which symbol position the old function 42b2b018efSChris J Arges * can be found (optional) 4319514910SPetr Mladek * @old_func: pointer to the function being patched 44b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 453c33f5b9SJosh Poimboeuf * @stack_node: list node for klp_ops func_stack list 46f5e547f4SJosh Poimboeuf * @old_size: size of the old function 47f5e547f4SJosh Poimboeuf * @new_size: size of the new function 48*0430f78bSPetr Mladek * @kobj_added: @kobj has been added and needs freeing 490dade9f3SJosh Poimboeuf * @patched: the func has been added to the klp_ops list 50d83a7cb3SJosh Poimboeuf * @transition: the func is currently being applied or reverted 51d83a7cb3SJosh Poimboeuf * 52d83a7cb3SJosh Poimboeuf * The patched and transition variables define the func's patching state. When 53d83a7cb3SJosh Poimboeuf * patching, a func is always in one of the following states: 54d83a7cb3SJosh Poimboeuf * 55d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 56d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary starting state 57d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 58d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 59d83a7cb3SJosh Poimboeuf * 60d83a7cb3SJosh Poimboeuf * And when unpatching, it goes in the reverse order: 61d83a7cb3SJosh Poimboeuf * 62d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 63d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 64d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary ending state 65d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 66b700e7f0SSeth Jennings */ 67b700e7f0SSeth Jennings struct klp_func { 68b700e7f0SSeth Jennings /* external */ 69b700e7f0SSeth Jennings const char *old_name; 70b700e7f0SSeth Jennings void *new_func; 71b700e7f0SSeth Jennings /* 72b2b018efSChris J Arges * The old_sympos field is optional and can be used to resolve 73b2b018efSChris J Arges * duplicate symbol names in livepatch objects. If this field is zero, 74b2b018efSChris J Arges * it is expected the symbol is unique, otherwise patching fails. If 75b2b018efSChris J Arges * this value is greater than zero then that occurrence of the symbol 76b2b018efSChris J Arges * in kallsyms for the given object is used. 77b700e7f0SSeth Jennings */ 78b2b018efSChris J Arges unsigned long old_sympos; 79b700e7f0SSeth Jennings 80b700e7f0SSeth Jennings /* internal */ 8119514910SPetr Mladek void *old_func; 82b700e7f0SSeth Jennings struct kobject kobj; 833c33f5b9SJosh Poimboeuf struct list_head stack_node; 84f5e547f4SJosh Poimboeuf unsigned long old_size, new_size; 85*0430f78bSPetr Mladek bool kobj_added; 860dade9f3SJosh Poimboeuf bool patched; 87d83a7cb3SJosh Poimboeuf bool transition; 88b700e7f0SSeth Jennings }; 89b700e7f0SSeth Jennings 9093862e38SJoe Lawrence struct klp_object; 9193862e38SJoe Lawrence 9293862e38SJoe Lawrence /** 9393862e38SJoe Lawrence * struct klp_callbacks - pre/post live-(un)patch callback structure 9493862e38SJoe Lawrence * @pre_patch: executed before code patching 9593862e38SJoe Lawrence * @post_patch: executed after code patching 9693862e38SJoe Lawrence * @pre_unpatch: executed before code unpatching 9793862e38SJoe Lawrence * @post_unpatch: executed after code unpatching 9893862e38SJoe Lawrence * @post_unpatch_enabled: flag indicating if post-unpatch callback 9993862e38SJoe Lawrence * should run 10093862e38SJoe Lawrence * 10193862e38SJoe Lawrence * All callbacks are optional. Only the pre-patch callback, if provided, 10293862e38SJoe Lawrence * will be unconditionally executed. If the parent klp_object fails to 10393862e38SJoe Lawrence * patch for any reason, including a non-zero error status returned from 10493862e38SJoe Lawrence * the pre-patch callback, no further callbacks will be executed. 10593862e38SJoe Lawrence */ 10693862e38SJoe Lawrence struct klp_callbacks { 10793862e38SJoe Lawrence int (*pre_patch)(struct klp_object *obj); 10893862e38SJoe Lawrence void (*post_patch)(struct klp_object *obj); 10993862e38SJoe Lawrence void (*pre_unpatch)(struct klp_object *obj); 11093862e38SJoe Lawrence void (*post_unpatch)(struct klp_object *obj); 11193862e38SJoe Lawrence bool post_unpatch_enabled; 11293862e38SJoe Lawrence }; 11393862e38SJoe Lawrence 114b700e7f0SSeth Jennings /** 115b700e7f0SSeth Jennings * struct klp_object - kernel object structure for live patching 116b700e7f0SSeth Jennings * @name: module name (or NULL for vmlinux) 117b700e7f0SSeth Jennings * @funcs: function entries for functions to be patched in the object 11893862e38SJoe Lawrence * @callbacks: functions to be executed pre/post (un)patching 119b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 120b700e7f0SSeth Jennings * @mod: kernel module associated with the patched object 121b700e7f0SSeth Jennings * (NULL for vmlinux) 122*0430f78bSPetr Mladek * @kobj_added: @kobj has been added and needs freeing 1230dade9f3SJosh Poimboeuf * @patched: the object's funcs have been added to the klp_ops list 124b700e7f0SSeth Jennings */ 125b700e7f0SSeth Jennings struct klp_object { 126b700e7f0SSeth Jennings /* external */ 127b700e7f0SSeth Jennings const char *name; 128b700e7f0SSeth Jennings struct klp_func *funcs; 12993862e38SJoe Lawrence struct klp_callbacks callbacks; 130b700e7f0SSeth Jennings 131b700e7f0SSeth Jennings /* internal */ 132cad706dfSMiroslav Benes struct kobject kobj; 133b700e7f0SSeth Jennings struct module *mod; 134*0430f78bSPetr Mladek bool kobj_added; 1350dade9f3SJosh Poimboeuf bool patched; 136b700e7f0SSeth Jennings }; 137b700e7f0SSeth Jennings 138b700e7f0SSeth Jennings /** 139b700e7f0SSeth Jennings * struct klp_patch - patch structure for live patching 140b700e7f0SSeth Jennings * @mod: reference to the live patch module 141b700e7f0SSeth Jennings * @objs: object entries for kernel objects to be patched 142b700e7f0SSeth Jennings * @list: list node for global list of registered patches 143b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 144*0430f78bSPetr Mladek * @kobj_added: @kobj has been added and needs freeing 1450dade9f3SJosh Poimboeuf * @enabled: the patch is enabled (but operation may be incomplete) 1463ec24776SJosh Poimboeuf * @finish: for waiting till it is safe to remove the patch module 147b700e7f0SSeth Jennings */ 148b700e7f0SSeth Jennings struct klp_patch { 149b700e7f0SSeth Jennings /* external */ 150b700e7f0SSeth Jennings struct module *mod; 151b700e7f0SSeth Jennings struct klp_object *objs; 152b700e7f0SSeth Jennings 153b700e7f0SSeth Jennings /* internal */ 154b700e7f0SSeth Jennings struct list_head list; 155b700e7f0SSeth Jennings struct kobject kobj; 156*0430f78bSPetr Mladek bool kobj_added; 1570dade9f3SJosh Poimboeuf bool enabled; 1583ec24776SJosh Poimboeuf struct completion finish; 159b700e7f0SSeth Jennings }; 160b700e7f0SSeth Jennings 1618cdd043aSJiri Slaby #define klp_for_each_object(patch, obj) \ 162f09d9086SMiroslav Benes for (obj = patch->objs; obj->funcs || obj->name; obj++) 1638cdd043aSJiri Slaby 1648cdd043aSJiri Slaby #define klp_for_each_func(obj, func) \ 165f09d9086SMiroslav Benes for (func = obj->funcs; \ 166f09d9086SMiroslav Benes func->old_name || func->new_func || func->old_sympos; \ 167f09d9086SMiroslav Benes func++) 1688cdd043aSJiri Slaby 1694421f8f0SMiroslav Benes int klp_register_patch(struct klp_patch *); 1704421f8f0SMiroslav Benes int klp_unregister_patch(struct klp_patch *); 1714421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *); 1724421f8f0SMiroslav Benes int klp_disable_patch(struct klp_patch *); 173b700e7f0SSeth Jennings 174255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch, 175255e732cSJessica Yu struct klp_object *obj); 176255e732cSJessica Yu 1777e545d6eSJessica Yu /* Called from the module loader during module coming/going states */ 1787e545d6eSJessica Yu int klp_module_coming(struct module *mod); 1797e545d6eSJessica Yu void klp_module_going(struct module *mod); 1807e545d6eSJessica Yu 181d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child); 18246c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task); 18346c5a011SJosh Poimboeuf 184d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) 185d83a7cb3SJosh Poimboeuf { 186d83a7cb3SJosh Poimboeuf return test_tsk_thread_flag(task, TIF_PATCH_PENDING); 187d83a7cb3SJosh Poimboeuf } 188d83a7cb3SJosh Poimboeuf 189d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void) 190d83a7cb3SJosh Poimboeuf { 191d83a7cb3SJosh Poimboeuf return IS_ENABLED(CONFIG_STACKTRACE) && 192d83a7cb3SJosh Poimboeuf IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE); 193d83a7cb3SJosh Poimboeuf } 194d83a7cb3SJosh Poimboeuf 195e91c2518SPetr Mladek typedef int (*klp_shadow_ctor_t)(void *obj, 196e91c2518SPetr Mladek void *shadow_data, 197e91c2518SPetr Mladek void *ctor_data); 1983b2c77d0SPetr Mladek typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data); 199e91c2518SPetr Mladek 200439e7271SJoe Lawrence void *klp_shadow_get(void *obj, unsigned long id); 201e91c2518SPetr Mladek void *klp_shadow_alloc(void *obj, unsigned long id, 202e91c2518SPetr Mladek size_t size, gfp_t gfp_flags, 203e91c2518SPetr Mladek klp_shadow_ctor_t ctor, void *ctor_data); 204e91c2518SPetr Mladek void *klp_shadow_get_or_alloc(void *obj, unsigned long id, 205e91c2518SPetr Mladek size_t size, gfp_t gfp_flags, 206e91c2518SPetr Mladek klp_shadow_ctor_t ctor, void *ctor_data); 2073b2c77d0SPetr Mladek void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor); 2083b2c77d0SPetr Mladek void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor); 209439e7271SJoe Lawrence 2107e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */ 2117e545d6eSJessica Yu 2127e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; } 2137e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {} 214d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; } 21546c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {} 216d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {} 2177e545d6eSJessica Yu 2187e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */ 2197e545d6eSJessica Yu 220b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */ 221