patch.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * patch.c - livepatch patching functions
  3. *
  4. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  5. * Copyright (C) 2014 SUSE
  6. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/livepatch.h>
  23. #include <linux/list.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/rculist.h>
  26. #include <linux/slab.h>
  27. #include <linux/bug.h>
  28. #include <linux/printk.h>
  29. #include "patch.h"
  30. #include "transition.h"
  31. static LIST_HEAD(klp_ops);
  32. struct klp_ops *klp_find_ops(unsigned long old_addr)
  33. {
  34. struct klp_ops *ops;
  35. struct klp_func *func;
  36. list_for_each_entry(ops, &klp_ops, node) {
  37. func = list_first_entry(&ops->func_stack, struct klp_func,
  38. stack_node);
  39. if (func->old_addr == old_addr)
  40. return ops;
  41. }
  42. return NULL;
  43. }
  44. static void notrace klp_ftrace_handler(unsigned long ip,
  45. unsigned long parent_ip,
  46. struct ftrace_ops *fops,
  47. struct pt_regs *regs)
  48. {
  49. struct klp_ops *ops;
  50. struct klp_func *func;
  51. int patch_state;
  52. ops = container_of(fops, struct klp_ops, fops);
  53. rcu_read_lock();
  54. func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
  55. stack_node);
  56. /*
  57. * func should never be NULL because preemption should be disabled here
  58. * and unregister_ftrace_function() does the equivalent of a
  59. * synchronize_sched() before the func_stack removal.
  60. */
  61. if (WARN_ON_ONCE(!func))
  62. goto unlock;
  63. /*
  64. * In the enable path, enforce the order of the ops->func_stack and
  65. * func->transition reads. The corresponding write barrier is in
  66. * __klp_enable_patch().
  67. *
  68. * (Note that this barrier technically isn't needed in the disable
  69. * path. In the rare case where klp_update_patch_state() runs before
  70. * this handler, its TIF_PATCH_PENDING read and this func->transition
  71. * read need to be ordered. But klp_update_patch_state() already
  72. * enforces that.)
  73. */
  74. smp_rmb();
  75. if (unlikely(func->transition)) {
  76. /*
  77. * Enforce the order of the func->transition and
  78. * current->patch_state reads. Otherwise we could read an
  79. * out-of-date task state and pick the wrong function. The
  80. * corresponding write barrier is in klp_init_transition().
  81. */
  82. smp_rmb();
  83. patch_state = current->patch_state;
  84. WARN_ON_ONCE(patch_state == KLP_UNDEFINED);
  85. if (patch_state == KLP_UNPATCHED) {
  86. /*
  87. * Use the previously patched version of the function.
  88. * If no previous patches exist, continue with the
  89. * original function.
  90. */
  91. func = list_entry_rcu(func->stack_node.next,
  92. struct klp_func, stack_node);
  93. if (&func->stack_node == &ops->func_stack)
  94. goto unlock;
  95. }
  96. }
  97. klp_arch_set_pc(regs, (unsigned long)func->new_func);
  98. unlock:
  99. rcu_read_unlock();
  100. }
  101. /*
  102. * Convert a function address into the appropriate ftrace location.
  103. *
  104. * Usually this is just the address of the function, but on some architectures
  105. * it's more complicated so allow them to provide a custom behaviour.
  106. */
  107. #ifndef klp_get_ftrace_location
  108. static unsigned long klp_get_ftrace_location(unsigned long faddr)
  109. {
  110. return faddr;
  111. }
  112. #endif
  113. static void klp_unpatch_func(struct klp_func *func)
  114. {
  115. struct klp_ops *ops;
  116. if (WARN_ON(!func->patched))
  117. return;
  118. if (WARN_ON(!func->old_addr))
  119. return;
  120. ops = klp_find_ops(func->old_addr);
  121. if (WARN_ON(!ops))
  122. return;
  123. if (list_is_singular(&ops->func_stack)) {
  124. unsigned long ftrace_loc;
  125. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  126. if (WARN_ON(!ftrace_loc))
  127. return;
  128. WARN_ON(unregister_ftrace_function(&ops->fops));
  129. WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
  130. list_del_rcu(&func->stack_node);
  131. list_del(&ops->node);
  132. kfree(ops);
  133. } else {
  134. list_del_rcu(&func->stack_node);
  135. }
  136. func->patched = false;
  137. }
  138. static int klp_patch_func(struct klp_func *func)
  139. {
  140. struct klp_ops *ops;
  141. int ret;
  142. if (WARN_ON(!func->old_addr))
  143. return -EINVAL;
  144. if (WARN_ON(func->patched))
  145. return -EINVAL;
  146. ops = klp_find_ops(func->old_addr);
  147. if (!ops) {
  148. unsigned long ftrace_loc;
  149. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  150. if (!ftrace_loc) {
  151. pr_err("failed to find location for function '%s'\n",
  152. func->old_name);
  153. return -EINVAL;
  154. }
  155. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  156. if (!ops)
  157. return -ENOMEM;
  158. ops->fops.func = klp_ftrace_handler;
  159. ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
  160. FTRACE_OPS_FL_DYNAMIC |
  161. FTRACE_OPS_FL_IPMODIFY;
  162. list_add(&ops->node, &klp_ops);
  163. INIT_LIST_HEAD(&ops->func_stack);
  164. list_add_rcu(&func->stack_node, &ops->func_stack);
  165. ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0);
  166. if (ret) {
  167. pr_err("failed to set ftrace filter for function '%s' (%d)\n",
  168. func->old_name, ret);
  169. goto err;
  170. }
  171. ret = register_ftrace_function(&ops->fops);
  172. if (ret) {
  173. pr_err("failed to register ftrace handler for function '%s' (%d)\n",
  174. func->old_name, ret);
  175. ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0);
  176. goto err;
  177. }
  178. } else {
  179. list_add_rcu(&func->stack_node, &ops->func_stack);
  180. }
  181. func->patched = true;
  182. return 0;
  183. err:
  184. list_del_rcu(&func->stack_node);
  185. list_del(&ops->node);
  186. kfree(ops);
  187. return ret;
  188. }
  189. void klp_unpatch_object(struct klp_object *obj)
  190. {
  191. struct klp_func *func;
  192. klp_for_each_func(obj, func)
  193. if (func->patched)
  194. klp_unpatch_func(func);
  195. obj->patched = false;
  196. }
  197. int klp_patch_object(struct klp_object *obj)
  198. {
  199. struct klp_func *func;
  200. int ret;
  201. if (WARN_ON(obj->patched))
  202. return -EINVAL;
  203. klp_for_each_func(obj, func) {
  204. ret = klp_patch_func(func);
  205. if (ret) {
  206. klp_unpatch_object(obj);
  207. return ret;
  208. }
  209. }
  210. obj->patched = true;
  211. return 0;
  212. }
  213. void klp_unpatch_objects(struct klp_patch *patch)
  214. {
  215. struct klp_object *obj;
  216. klp_for_each_object(patch, obj)
  217. if (obj->patched)
  218. klp_unpatch_object(obj);
  219. }