patch.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "core.h"
  30. #include "patch.h"
  31. #include "transition.h"
  32. static LIST_HEAD(klp_ops);
  33. struct klp_ops *klp_find_ops(unsigned long old_addr)
  34. {
  35. struct klp_ops *ops;
  36. struct klp_func *func;
  37. list_for_each_entry(ops, &klp_ops, node) {
  38. func = list_first_entry(&ops->func_stack, struct klp_func,
  39. stack_node);
  40. if (func->old_addr == old_addr)
  41. return ops;
  42. }
  43. return NULL;
  44. }
  45. static void notrace klp_ftrace_handler(unsigned long ip,
  46. unsigned long parent_ip,
  47. struct ftrace_ops *fops,
  48. struct pt_regs *regs)
  49. {
  50. struct klp_ops *ops;
  51. struct klp_func *func;
  52. int patch_state;
  53. ops = container_of(fops, struct klp_ops, fops);
  54. /*
  55. * A variant of synchronize_sched() is used to allow patching functions
  56. * where RCU is not watching, see klp_synchronize_transition().
  57. */
  58. preempt_disable_notrace();
  59. func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
  60. stack_node);
  61. /*
  62. * func should never be NULL because preemption should be disabled here
  63. * and unregister_ftrace_function() does the equivalent of a
  64. * synchronize_sched() before the func_stack removal.
  65. */
  66. if (WARN_ON_ONCE(!func))
  67. goto unlock;
  68. /*
  69. * In the enable path, enforce the order of the ops->func_stack and
  70. * func->transition reads. The corresponding write barrier is in
  71. * __klp_enable_patch().
  72. *
  73. * (Note that this barrier technically isn't needed in the disable
  74. * path. In the rare case where klp_update_patch_state() runs before
  75. * this handler, its TIF_PATCH_PENDING read and this func->transition
  76. * read need to be ordered. But klp_update_patch_state() already
  77. * enforces that.)
  78. */
  79. smp_rmb();
  80. if (unlikely(func->transition)) {
  81. /*
  82. * Enforce the order of the func->transition and
  83. * current->patch_state reads. Otherwise we could read an
  84. * out-of-date task state and pick the wrong function. The
  85. * corresponding write barrier is in klp_init_transition().
  86. */
  87. smp_rmb();
  88. patch_state = current->patch_state;
  89. WARN_ON_ONCE(patch_state == KLP_UNDEFINED);
  90. if (patch_state == KLP_UNPATCHED) {
  91. /*
  92. * Use the previously patched version of the function.
  93. * If no previous patches exist, continue with the
  94. * original function.
  95. */
  96. func = list_entry_rcu(func->stack_node.next,
  97. struct klp_func, stack_node);
  98. if (&func->stack_node == &ops->func_stack)
  99. goto unlock;
  100. }
  101. }
  102. klp_arch_set_pc(regs, (unsigned long)func->new_func);
  103. unlock:
  104. preempt_enable_notrace();
  105. }
  106. /*
  107. * Convert a function address into the appropriate ftrace location.
  108. *
  109. * Usually this is just the address of the function, but on some architectures
  110. * it's more complicated so allow them to provide a custom behaviour.
  111. */
  112. #ifndef klp_get_ftrace_location
  113. static unsigned long klp_get_ftrace_location(unsigned long faddr)
  114. {
  115. return faddr;
  116. }
  117. #endif
  118. static void klp_unpatch_func(struct klp_func *func)
  119. {
  120. struct klp_ops *ops;
  121. if (WARN_ON(!func->patched))
  122. return;
  123. if (WARN_ON(!func->old_addr))
  124. return;
  125. ops = klp_find_ops(func->old_addr);
  126. if (WARN_ON(!ops))
  127. return;
  128. if (list_is_singular(&ops->func_stack)) {
  129. unsigned long ftrace_loc;
  130. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  131. if (WARN_ON(!ftrace_loc))
  132. return;
  133. WARN_ON(unregister_ftrace_function(&ops->fops));
  134. WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
  135. list_del_rcu(&func->stack_node);
  136. list_del(&ops->node);
  137. kfree(ops);
  138. } else {
  139. list_del_rcu(&func->stack_node);
  140. }
  141. func->patched = false;
  142. }
  143. static int klp_patch_func(struct klp_func *func)
  144. {
  145. struct klp_ops *ops;
  146. int ret;
  147. if (WARN_ON(!func->old_addr))
  148. return -EINVAL;
  149. if (WARN_ON(func->patched))
  150. return -EINVAL;
  151. ops = klp_find_ops(func->old_addr);
  152. if (!ops) {
  153. unsigned long ftrace_loc;
  154. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  155. if (!ftrace_loc) {
  156. pr_err("failed to find location for function '%s'\n",
  157. func->old_name);
  158. return -EINVAL;
  159. }
  160. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  161. if (!ops)
  162. return -ENOMEM;
  163. ops->fops.func = klp_ftrace_handler;
  164. ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
  165. FTRACE_OPS_FL_DYNAMIC |
  166. FTRACE_OPS_FL_IPMODIFY;
  167. list_add(&ops->node, &klp_ops);
  168. INIT_LIST_HEAD(&ops->func_stack);
  169. list_add_rcu(&func->stack_node, &ops->func_stack);
  170. ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0);
  171. if (ret) {
  172. pr_err("failed to set ftrace filter for function '%s' (%d)\n",
  173. func->old_name, ret);
  174. goto err;
  175. }
  176. ret = register_ftrace_function(&ops->fops);
  177. if (ret) {
  178. pr_err("failed to register ftrace handler for function '%s' (%d)\n",
  179. func->old_name, ret);
  180. ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0);
  181. goto err;
  182. }
  183. } else {
  184. list_add_rcu(&func->stack_node, &ops->func_stack);
  185. }
  186. func->patched = true;
  187. return 0;
  188. err:
  189. list_del_rcu(&func->stack_node);
  190. list_del(&ops->node);
  191. kfree(ops);
  192. return ret;
  193. }
  194. void klp_unpatch_object(struct klp_object *obj)
  195. {
  196. struct klp_func *func;
  197. klp_for_each_func(obj, func)
  198. if (func->patched)
  199. klp_unpatch_func(func);
  200. obj->patched = false;
  201. }
  202. int klp_patch_object(struct klp_object *obj)
  203. {
  204. struct klp_func *func;
  205. int ret;
  206. if (WARN_ON(obj->patched))
  207. return -EINVAL;
  208. klp_for_each_func(obj, func) {
  209. ret = klp_patch_func(func);
  210. if (ret) {
  211. klp_unpatch_object(obj);
  212. return ret;
  213. }
  214. }
  215. obj->patched = true;
  216. return 0;
  217. }
  218. void klp_unpatch_objects(struct klp_patch *patch)
  219. {
  220. struct klp_object *obj;
  221. klp_for_each_object(patch, obj)
  222. if (obj->patched)
  223. klp_unpatch_object(obj);
  224. }