livepatch.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * livepatch.h - Kernel Live Patching Core
  3. *
  4. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  5. * Copyright (C) 2014 SUSE
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef _LINUX_LIVEPATCH_H_
  21. #define _LINUX_LIVEPATCH_H_
  22. #include <linux/module.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/completion.h>
  25. #if IS_ENABLED(CONFIG_LIVEPATCH)
  26. #include <asm/livepatch.h>
  27. /* task patch states */
  28. #define KLP_UNDEFINED -1
  29. #define KLP_UNPATCHED 0
  30. #define KLP_PATCHED 1
  31. /**
  32. * struct klp_func - function structure for live patching
  33. * @old_name: name of the function to be patched
  34. * @new_func: pointer to the patched function code
  35. * @old_sympos: a hint indicating which symbol position the old function
  36. * can be found (optional)
  37. * @old_addr: the address of the function being patched
  38. * @kobj: kobject for sysfs resources
  39. * @stack_node: list node for klp_ops func_stack list
  40. * @old_size: size of the old function
  41. * @new_size: size of the new function
  42. * @patched: the func has been added to the klp_ops list
  43. * @transition: the func is currently being applied or reverted
  44. *
  45. * The patched and transition variables define the func's patching state. When
  46. * patching, a func is always in one of the following states:
  47. *
  48. * patched=0 transition=0: unpatched
  49. * patched=0 transition=1: unpatched, temporary starting state
  50. * patched=1 transition=1: patched, may be visible to some tasks
  51. * patched=1 transition=0: patched, visible to all tasks
  52. *
  53. * And when unpatching, it goes in the reverse order:
  54. *
  55. * patched=1 transition=0: patched, visible to all tasks
  56. * patched=1 transition=1: patched, may be visible to some tasks
  57. * patched=0 transition=1: unpatched, temporary ending state
  58. * patched=0 transition=0: unpatched
  59. */
  60. struct klp_func {
  61. /* external */
  62. const char *old_name;
  63. void *new_func;
  64. /*
  65. * The old_sympos field is optional and can be used to resolve
  66. * duplicate symbol names in livepatch objects. If this field is zero,
  67. * it is expected the symbol is unique, otherwise patching fails. If
  68. * this value is greater than zero then that occurrence of the symbol
  69. * in kallsyms for the given object is used.
  70. */
  71. unsigned long old_sympos;
  72. /* internal */
  73. unsigned long old_addr;
  74. struct kobject kobj;
  75. struct list_head stack_node;
  76. unsigned long old_size, new_size;
  77. bool patched;
  78. bool transition;
  79. };
  80. struct klp_object;
  81. /**
  82. * struct klp_callbacks - pre/post live-(un)patch callback structure
  83. * @pre_patch: executed before code patching
  84. * @post_patch: executed after code patching
  85. * @pre_unpatch: executed before code unpatching
  86. * @post_unpatch: executed after code unpatching
  87. * @post_unpatch_enabled: flag indicating if post-unpatch callback
  88. * should run
  89. *
  90. * All callbacks are optional. Only the pre-patch callback, if provided,
  91. * will be unconditionally executed. If the parent klp_object fails to
  92. * patch for any reason, including a non-zero error status returned from
  93. * the pre-patch callback, no further callbacks will be executed.
  94. */
  95. struct klp_callbacks {
  96. int (*pre_patch)(struct klp_object *obj);
  97. void (*post_patch)(struct klp_object *obj);
  98. void (*pre_unpatch)(struct klp_object *obj);
  99. void (*post_unpatch)(struct klp_object *obj);
  100. bool post_unpatch_enabled;
  101. };
  102. /**
  103. * struct klp_object - kernel object structure for live patching
  104. * @name: module name (or NULL for vmlinux)
  105. * @funcs: function entries for functions to be patched in the object
  106. * @callbacks: functions to be executed pre/post (un)patching
  107. * @kobj: kobject for sysfs resources
  108. * @mod: kernel module associated with the patched object
  109. * (NULL for vmlinux)
  110. * @patched: the object's funcs have been added to the klp_ops list
  111. */
  112. struct klp_object {
  113. /* external */
  114. const char *name;
  115. struct klp_func *funcs;
  116. struct klp_callbacks callbacks;
  117. /* internal */
  118. struct kobject kobj;
  119. struct module *mod;
  120. bool patched;
  121. };
  122. /**
  123. * struct klp_patch - patch structure for live patching
  124. * @mod: reference to the live patch module
  125. * @objs: object entries for kernel objects to be patched
  126. * @list: list node for global list of registered patches
  127. * @kobj: kobject for sysfs resources
  128. * @enabled: the patch is enabled (but operation may be incomplete)
  129. * @finish: for waiting till it is safe to remove the patch module
  130. */
  131. struct klp_patch {
  132. /* external */
  133. struct module *mod;
  134. struct klp_object *objs;
  135. /* internal */
  136. struct list_head list;
  137. struct kobject kobj;
  138. bool enabled;
  139. struct completion finish;
  140. };
  141. #define klp_for_each_object(patch, obj) \
  142. for (obj = patch->objs; obj->funcs || obj->name; obj++)
  143. #define klp_for_each_func(obj, func) \
  144. for (func = obj->funcs; \
  145. func->old_name || func->new_func || func->old_sympos; \
  146. func++)
  147. int klp_register_patch(struct klp_patch *);
  148. int klp_unregister_patch(struct klp_patch *);
  149. int klp_enable_patch(struct klp_patch *);
  150. int klp_disable_patch(struct klp_patch *);
  151. void arch_klp_init_object_loaded(struct klp_patch *patch,
  152. struct klp_object *obj);
  153. /* Called from the module loader during module coming/going states */
  154. int klp_module_coming(struct module *mod);
  155. void klp_module_going(struct module *mod);
  156. void klp_copy_process(struct task_struct *child);
  157. void klp_update_patch_state(struct task_struct *task);
  158. static inline bool klp_patch_pending(struct task_struct *task)
  159. {
  160. return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
  161. }
  162. static inline bool klp_have_reliable_stack(void)
  163. {
  164. return IS_ENABLED(CONFIG_STACKTRACE) &&
  165. IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
  166. }
  167. typedef int (*klp_shadow_ctor_t)(void *obj,
  168. void *shadow_data,
  169. void *ctor_data);
  170. typedef void (*klp_shadow_dtor_t)(void *obj, void *shadow_data);
  171. void *klp_shadow_get(void *obj, unsigned long id);
  172. void *klp_shadow_alloc(void *obj, unsigned long id,
  173. size_t size, gfp_t gfp_flags,
  174. klp_shadow_ctor_t ctor, void *ctor_data);
  175. void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
  176. size_t size, gfp_t gfp_flags,
  177. klp_shadow_ctor_t ctor, void *ctor_data);
  178. void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor);
  179. void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor);
  180. #else /* !CONFIG_LIVEPATCH */
  181. static inline int klp_module_coming(struct module *mod) { return 0; }
  182. static inline void klp_module_going(struct module *mod) {}
  183. static inline bool klp_patch_pending(struct task_struct *task) { return false; }
  184. static inline void klp_update_patch_state(struct task_struct *task) {}
  185. static inline void klp_copy_process(struct task_struct *child) {}
  186. #endif /* CONFIG_LIVEPATCH */
  187. #endif /* _LINUX_LIVEPATCH_H_ */