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> 26b700e7f0SSeth Jennings 277e545d6eSJessica Yu #if IS_ENABLED(CONFIG_LIVEPATCH) 287e545d6eSJessica Yu 29b700e7f0SSeth Jennings #include <asm/livepatch.h> 30b700e7f0SSeth Jennings 31*d83a7cb3SJosh Poimboeuf /* task patch states */ 32*d83a7cb3SJosh Poimboeuf #define KLP_UNDEFINED -1 33*d83a7cb3SJosh Poimboeuf #define KLP_UNPATCHED 0 34*d83a7cb3SJosh Poimboeuf #define KLP_PATCHED 1 35*d83a7cb3SJosh Poimboeuf 36b700e7f0SSeth Jennings /** 37b700e7f0SSeth Jennings * struct klp_func - function structure for live patching 38b700e7f0SSeth Jennings * @old_name: name of the function to be patched 39b700e7f0SSeth Jennings * @new_func: pointer to the patched function code 40b2b018efSChris J Arges * @old_sympos: a hint indicating which symbol position the old function 41b2b018efSChris J Arges * can be found (optional) 42*d83a7cb3SJosh Poimboeuf * @immediate: patch the func immediately, bypassing safety mechanisms 43b2b018efSChris J Arges * @old_addr: the address of 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 480dade9f3SJosh Poimboeuf * @patched: the func has been added to the klp_ops list 49*d83a7cb3SJosh Poimboeuf * @transition: the func is currently being applied or reverted 50*d83a7cb3SJosh Poimboeuf * 51*d83a7cb3SJosh Poimboeuf * The patched and transition variables define the func's patching state. When 52*d83a7cb3SJosh Poimboeuf * patching, a func is always in one of the following states: 53*d83a7cb3SJosh Poimboeuf * 54*d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 55*d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary starting state 56*d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 57*d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 58*d83a7cb3SJosh Poimboeuf * 59*d83a7cb3SJosh Poimboeuf * And when unpatching, it goes in the reverse order: 60*d83a7cb3SJosh Poimboeuf * 61*d83a7cb3SJosh Poimboeuf * patched=1 transition=0: patched, visible to all tasks 62*d83a7cb3SJosh Poimboeuf * patched=1 transition=1: patched, may be visible to some tasks 63*d83a7cb3SJosh Poimboeuf * patched=0 transition=1: unpatched, temporary ending state 64*d83a7cb3SJosh Poimboeuf * patched=0 transition=0: unpatched 65b700e7f0SSeth Jennings */ 66b700e7f0SSeth Jennings struct klp_func { 67b700e7f0SSeth Jennings /* external */ 68b700e7f0SSeth Jennings const char *old_name; 69b700e7f0SSeth Jennings void *new_func; 70b700e7f0SSeth Jennings /* 71b2b018efSChris J Arges * The old_sympos field is optional and can be used to resolve 72b2b018efSChris J Arges * duplicate symbol names in livepatch objects. If this field is zero, 73b2b018efSChris J Arges * it is expected the symbol is unique, otherwise patching fails. If 74b2b018efSChris J Arges * this value is greater than zero then that occurrence of the symbol 75b2b018efSChris J Arges * in kallsyms for the given object is used. 76b700e7f0SSeth Jennings */ 77b2b018efSChris J Arges unsigned long old_sympos; 78*d83a7cb3SJosh Poimboeuf bool immediate; 79b700e7f0SSeth Jennings 80b700e7f0SSeth Jennings /* internal */ 81b2b018efSChris J Arges unsigned long old_addr; 82b700e7f0SSeth Jennings struct kobject kobj; 833c33f5b9SJosh Poimboeuf struct list_head stack_node; 84f5e547f4SJosh Poimboeuf unsigned long old_size, new_size; 850dade9f3SJosh Poimboeuf bool patched; 86*d83a7cb3SJosh Poimboeuf bool transition; 87b700e7f0SSeth Jennings }; 88b700e7f0SSeth Jennings 89b700e7f0SSeth Jennings /** 90b700e7f0SSeth Jennings * struct klp_object - kernel object structure for live patching 91b700e7f0SSeth Jennings * @name: module name (or NULL for vmlinux) 92b700e7f0SSeth Jennings * @funcs: function entries for functions to be patched in the object 93b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 94b700e7f0SSeth Jennings * @mod: kernel module associated with the patched object 95b700e7f0SSeth Jennings * (NULL for vmlinux) 960dade9f3SJosh Poimboeuf * @patched: the object's funcs have been added to the klp_ops list 97b700e7f0SSeth Jennings */ 98b700e7f0SSeth Jennings struct klp_object { 99b700e7f0SSeth Jennings /* external */ 100b700e7f0SSeth Jennings const char *name; 101b700e7f0SSeth Jennings struct klp_func *funcs; 102b700e7f0SSeth Jennings 103b700e7f0SSeth Jennings /* internal */ 104cad706dfSMiroslav Benes struct kobject kobj; 105b700e7f0SSeth Jennings struct module *mod; 1060dade9f3SJosh Poimboeuf bool patched; 107b700e7f0SSeth Jennings }; 108b700e7f0SSeth Jennings 109b700e7f0SSeth Jennings /** 110b700e7f0SSeth Jennings * struct klp_patch - patch structure for live patching 111b700e7f0SSeth Jennings * @mod: reference to the live patch module 112b700e7f0SSeth Jennings * @objs: object entries for kernel objects to be patched 113*d83a7cb3SJosh Poimboeuf * @immediate: patch all funcs immediately, bypassing safety mechanisms 114b700e7f0SSeth Jennings * @list: list node for global list of registered patches 115b700e7f0SSeth Jennings * @kobj: kobject for sysfs resources 1160dade9f3SJosh Poimboeuf * @enabled: the patch is enabled (but operation may be incomplete) 117b700e7f0SSeth Jennings */ 118b700e7f0SSeth Jennings struct klp_patch { 119b700e7f0SSeth Jennings /* external */ 120b700e7f0SSeth Jennings struct module *mod; 121b700e7f0SSeth Jennings struct klp_object *objs; 122*d83a7cb3SJosh Poimboeuf bool immediate; 123b700e7f0SSeth Jennings 124b700e7f0SSeth Jennings /* internal */ 125b700e7f0SSeth Jennings struct list_head list; 126b700e7f0SSeth Jennings struct kobject kobj; 1270dade9f3SJosh Poimboeuf bool enabled; 128b700e7f0SSeth Jennings }; 129b700e7f0SSeth Jennings 1308cdd043aSJiri Slaby #define klp_for_each_object(patch, obj) \ 131f09d9086SMiroslav Benes for (obj = patch->objs; obj->funcs || obj->name; obj++) 1328cdd043aSJiri Slaby 1338cdd043aSJiri Slaby #define klp_for_each_func(obj, func) \ 134f09d9086SMiroslav Benes for (func = obj->funcs; \ 135f09d9086SMiroslav Benes func->old_name || func->new_func || func->old_sympos; \ 136f09d9086SMiroslav Benes func++) 1378cdd043aSJiri Slaby 1384421f8f0SMiroslav Benes int klp_register_patch(struct klp_patch *); 1394421f8f0SMiroslav Benes int klp_unregister_patch(struct klp_patch *); 1404421f8f0SMiroslav Benes int klp_enable_patch(struct klp_patch *); 1414421f8f0SMiroslav Benes int klp_disable_patch(struct klp_patch *); 142b700e7f0SSeth Jennings 143255e732cSJessica Yu void arch_klp_init_object_loaded(struct klp_patch *patch, 144255e732cSJessica Yu struct klp_object *obj); 145255e732cSJessica Yu 1467e545d6eSJessica Yu /* Called from the module loader during module coming/going states */ 1477e545d6eSJessica Yu int klp_module_coming(struct module *mod); 1487e545d6eSJessica Yu void klp_module_going(struct module *mod); 1497e545d6eSJessica Yu 150*d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child); 15146c5a011SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task); 15246c5a011SJosh Poimboeuf 153*d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) 154*d83a7cb3SJosh Poimboeuf { 155*d83a7cb3SJosh Poimboeuf return test_tsk_thread_flag(task, TIF_PATCH_PENDING); 156*d83a7cb3SJosh Poimboeuf } 157*d83a7cb3SJosh Poimboeuf 158*d83a7cb3SJosh Poimboeuf static inline bool klp_have_reliable_stack(void) 159*d83a7cb3SJosh Poimboeuf { 160*d83a7cb3SJosh Poimboeuf return IS_ENABLED(CONFIG_STACKTRACE) && 161*d83a7cb3SJosh Poimboeuf IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE); 162*d83a7cb3SJosh Poimboeuf } 163*d83a7cb3SJosh Poimboeuf 1647e545d6eSJessica Yu #else /* !CONFIG_LIVEPATCH */ 1657e545d6eSJessica Yu 1667e545d6eSJessica Yu static inline int klp_module_coming(struct module *mod) { return 0; } 1677e545d6eSJessica Yu static inline void klp_module_going(struct module *mod) {} 168*d83a7cb3SJosh Poimboeuf static inline bool klp_patch_pending(struct task_struct *task) { return false; } 16946c5a011SJosh Poimboeuf static inline void klp_update_patch_state(struct task_struct *task) {} 170*d83a7cb3SJosh Poimboeuf static inline void klp_copy_process(struct task_struct *child) {} 1717e545d6eSJessica Yu 1727e545d6eSJessica Yu #endif /* CONFIG_LIVEPATCH */ 1737e545d6eSJessica Yu 174b700e7f0SSeth Jennings #endif /* _LINUX_LIVEPATCH_H_ */ 175