livepatch.h 8.2 KB

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