livepatch.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @immediate: patch the func immediately, bypassing safety mechanisms
  38. * @old_addr: the address of the function being patched
  39. * @kobj: kobject for sysfs resources
  40. * @stack_node: list node for klp_ops func_stack list
  41. * @old_size: size of the old function
  42. * @new_size: size of the new function
  43. * @patched: the func has been added to the klp_ops list
  44. * @transition: the func is currently being applied or reverted
  45. *
  46. * The patched and transition variables define the func's patching state. When
  47. * patching, a func is always in one of the following states:
  48. *
  49. * patched=0 transition=0: unpatched
  50. * patched=0 transition=1: unpatched, temporary starting state
  51. * patched=1 transition=1: patched, may be visible to some tasks
  52. * patched=1 transition=0: patched, visible to all tasks
  53. *
  54. * And when unpatching, it goes in the reverse order:
  55. *
  56. * patched=1 transition=0: patched, visible to all tasks
  57. * patched=1 transition=1: patched, may be visible to some tasks
  58. * patched=0 transition=1: unpatched, temporary ending state
  59. * patched=0 transition=0: unpatched
  60. */
  61. struct klp_func {
  62. /* external */
  63. const char *old_name;
  64. void *new_func;
  65. /*
  66. * The old_sympos field is optional and can be used to resolve
  67. * duplicate symbol names in livepatch objects. If this field is zero,
  68. * it is expected the symbol is unique, otherwise patching fails. If
  69. * this value is greater than zero then that occurrence of the symbol
  70. * in kallsyms for the given object is used.
  71. */
  72. unsigned long old_sympos;
  73. bool immediate;
  74. /* internal */
  75. unsigned long old_addr;
  76. struct kobject kobj;
  77. struct list_head stack_node;
  78. unsigned long old_size, new_size;
  79. bool patched;
  80. bool transition;
  81. };
  82. /**
  83. * struct klp_object - kernel object structure for live patching
  84. * @name: module name (or NULL for vmlinux)
  85. * @funcs: function entries for functions to be patched in the object
  86. * @kobj: kobject for sysfs resources
  87. * @mod: kernel module associated with the patched object
  88. * (NULL for vmlinux)
  89. * @patched: the object's funcs have been added to the klp_ops list
  90. */
  91. struct klp_object {
  92. /* external */
  93. const char *name;
  94. struct klp_func *funcs;
  95. /* internal */
  96. struct kobject kobj;
  97. struct module *mod;
  98. bool patched;
  99. };
  100. /**
  101. * struct klp_patch - patch structure for live patching
  102. * @mod: reference to the live patch module
  103. * @objs: object entries for kernel objects to be patched
  104. * @immediate: patch all funcs immediately, bypassing safety mechanisms
  105. * @list: list node for global list of registered patches
  106. * @kobj: kobject for sysfs resources
  107. * @enabled: the patch is enabled (but operation may be incomplete)
  108. * @finish: for waiting till it is safe to remove the patch module
  109. */
  110. struct klp_patch {
  111. /* external */
  112. struct module *mod;
  113. struct klp_object *objs;
  114. bool immediate;
  115. /* internal */
  116. struct list_head list;
  117. struct kobject kobj;
  118. bool enabled;
  119. struct completion finish;
  120. };
  121. #define klp_for_each_object(patch, obj) \
  122. for (obj = patch->objs; obj->funcs || obj->name; obj++)
  123. #define klp_for_each_func(obj, func) \
  124. for (func = obj->funcs; \
  125. func->old_name || func->new_func || func->old_sympos; \
  126. func++)
  127. int klp_register_patch(struct klp_patch *);
  128. int klp_unregister_patch(struct klp_patch *);
  129. int klp_enable_patch(struct klp_patch *);
  130. int klp_disable_patch(struct klp_patch *);
  131. void arch_klp_init_object_loaded(struct klp_patch *patch,
  132. struct klp_object *obj);
  133. /* Called from the module loader during module coming/going states */
  134. int klp_module_coming(struct module *mod);
  135. void klp_module_going(struct module *mod);
  136. void klp_copy_process(struct task_struct *child);
  137. void klp_update_patch_state(struct task_struct *task);
  138. static inline bool klp_patch_pending(struct task_struct *task)
  139. {
  140. return test_tsk_thread_flag(task, TIF_PATCH_PENDING);
  141. }
  142. static inline bool klp_have_reliable_stack(void)
  143. {
  144. return IS_ENABLED(CONFIG_STACKTRACE) &&
  145. IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
  146. }
  147. void *klp_shadow_get(void *obj, unsigned long id);
  148. void *klp_shadow_alloc(void *obj, unsigned long id, void *data,
  149. size_t size, gfp_t gfp_flags);
  150. void *klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data,
  151. size_t size, gfp_t gfp_flags);
  152. void klp_shadow_free(void *obj, unsigned long id);
  153. void klp_shadow_free_all(unsigned long id);
  154. #else /* !CONFIG_LIVEPATCH */
  155. static inline int klp_module_coming(struct module *mod) { return 0; }
  156. static inline void klp_module_going(struct module *mod) {}
  157. static inline bool klp_patch_pending(struct task_struct *task) { return false; }
  158. static inline void klp_update_patch_state(struct task_struct *task) {}
  159. static inline void klp_copy_process(struct task_struct *child) {}
  160. #endif /* CONFIG_LIVEPATCH */
  161. #endif /* _LINUX_LIVEPATCH_H_ */