Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * patch.c - livepatch patching functions |
| 4 | * |
| 5 | * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> |
| 6 | * Copyright (C) 2014 SUSE |
| 7 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
| 12 | #include <linux/livepatch.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/ftrace.h> |
| 15 | #include <linux/rculist.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/printk.h> |
Joe Lawrence | 93862e3 | 2017-10-13 15:08:41 -0400 | [diff] [blame] | 19 | #include "core.h" |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 20 | #include "patch.h" |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 21 | #include "transition.h" |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 22 | |
| 23 | static LIST_HEAD(klp_ops); |
| 24 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 25 | struct klp_ops *klp_find_ops(void *old_func) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 26 | { |
| 27 | struct klp_ops *ops; |
| 28 | struct klp_func *func; |
| 29 | |
| 30 | list_for_each_entry(ops, &klp_ops, node) { |
| 31 | func = list_first_entry(&ops->func_stack, struct klp_func, |
| 32 | stack_node); |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 33 | if (func->old_func == old_func) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 34 | return ops; |
| 35 | } |
| 36 | |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | static void notrace klp_ftrace_handler(unsigned long ip, |
| 41 | unsigned long parent_ip, |
| 42 | struct ftrace_ops *fops, |
Steven Rostedt (VMware) | d19ad07 | 2020-10-28 17:42:17 -0400 | [diff] [blame^] | 43 | struct ftrace_regs *fregs) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 44 | { |
Steven Rostedt (VMware) | d19ad07 | 2020-10-28 17:42:17 -0400 | [diff] [blame^] | 45 | struct pt_regs *regs = ftrace_get_regs(fregs); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 46 | struct klp_ops *ops; |
| 47 | struct klp_func *func; |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 48 | int patch_state; |
Steven Rostedt (VMware) | 13f3ea9 | 2020-11-05 21:32:41 -0500 | [diff] [blame] | 49 | int bit; |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 50 | |
| 51 | ops = container_of(fops, struct klp_ops, fops); |
| 52 | |
Steven Rostedt (VMware) | 773c167 | 2020-11-05 21:32:46 -0500 | [diff] [blame] | 53 | bit = ftrace_test_recursion_trylock(ip, parent_ip); |
Steven Rostedt (VMware) | 4b750b5 | 2020-11-05 21:32:42 -0500 | [diff] [blame] | 54 | if (WARN_ON_ONCE(bit < 0)) |
Steven Rostedt (VMware) | 13f3ea9 | 2020-11-05 21:32:41 -0500 | [diff] [blame] | 55 | return; |
Petr Mladek | 842c088 | 2017-06-14 10:54:52 +0200 | [diff] [blame] | 56 | /* |
Paul E. McKenney | 6932689 | 2018-11-07 14:16:57 -0800 | [diff] [blame] | 57 | * A variant of synchronize_rcu() is used to allow patching functions |
Petr Mladek | 842c088 | 2017-06-14 10:54:52 +0200 | [diff] [blame] | 58 | * where RCU is not watching, see klp_synchronize_transition(). |
| 59 | */ |
| 60 | preempt_disable_notrace(); |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 61 | |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 62 | func = list_first_or_null_rcu(&ops->func_stack, struct klp_func, |
| 63 | stack_node); |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * func should never be NULL because preemption should be disabled here |
| 67 | * and unregister_ftrace_function() does the equivalent of a |
Paul E. McKenney | 6932689 | 2018-11-07 14:16:57 -0800 | [diff] [blame] | 68 | * synchronize_rcu() before the func_stack removal. |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 69 | */ |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 70 | if (WARN_ON_ONCE(!func)) |
| 71 | goto unlock; |
| 72 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 73 | /* |
| 74 | * In the enable path, enforce the order of the ops->func_stack and |
| 75 | * func->transition reads. The corresponding write barrier is in |
| 76 | * __klp_enable_patch(). |
| 77 | * |
| 78 | * (Note that this barrier technically isn't needed in the disable |
| 79 | * path. In the rare case where klp_update_patch_state() runs before |
| 80 | * this handler, its TIF_PATCH_PENDING read and this func->transition |
| 81 | * read need to be ordered. But klp_update_patch_state() already |
| 82 | * enforces that.) |
| 83 | */ |
| 84 | smp_rmb(); |
| 85 | |
| 86 | if (unlikely(func->transition)) { |
| 87 | |
| 88 | /* |
| 89 | * Enforce the order of the func->transition and |
| 90 | * current->patch_state reads. Otherwise we could read an |
| 91 | * out-of-date task state and pick the wrong function. The |
| 92 | * corresponding write barrier is in klp_init_transition(). |
| 93 | */ |
| 94 | smp_rmb(); |
| 95 | |
| 96 | patch_state = current->patch_state; |
| 97 | |
| 98 | WARN_ON_ONCE(patch_state == KLP_UNDEFINED); |
| 99 | |
| 100 | if (patch_state == KLP_UNPATCHED) { |
| 101 | /* |
| 102 | * Use the previously patched version of the function. |
| 103 | * If no previous patches exist, continue with the |
| 104 | * original function. |
| 105 | */ |
| 106 | func = list_entry_rcu(func->stack_node.next, |
| 107 | struct klp_func, stack_node); |
| 108 | |
| 109 | if (&func->stack_node == &ops->func_stack) |
| 110 | goto unlock; |
| 111 | } |
| 112 | } |
| 113 | |
Jason Baron | e1452b6 | 2019-01-09 13:43:25 +0100 | [diff] [blame] | 114 | /* |
| 115 | * NOPs are used to replace existing patches with original code. |
| 116 | * Do nothing! Setting pc would cause an infinite loop. |
| 117 | */ |
| 118 | if (func->nop) |
| 119 | goto unlock; |
| 120 | |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 121 | klp_arch_set_pc(regs, (unsigned long)func->new_func); |
Jason Baron | e1452b6 | 2019-01-09 13:43:25 +0100 | [diff] [blame] | 122 | |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 123 | unlock: |
Petr Mladek | 842c088 | 2017-06-14 10:54:52 +0200 | [diff] [blame] | 124 | preempt_enable_notrace(); |
Steven Rostedt (VMware) | 13f3ea9 | 2020-11-05 21:32:41 -0500 | [diff] [blame] | 125 | ftrace_test_recursion_unlock(bit); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Convert a function address into the appropriate ftrace location. |
| 130 | * |
| 131 | * Usually this is just the address of the function, but on some architectures |
| 132 | * it's more complicated so allow them to provide a custom behaviour. |
| 133 | */ |
| 134 | #ifndef klp_get_ftrace_location |
| 135 | static unsigned long klp_get_ftrace_location(unsigned long faddr) |
| 136 | { |
| 137 | return faddr; |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | static void klp_unpatch_func(struct klp_func *func) |
| 142 | { |
| 143 | struct klp_ops *ops; |
| 144 | |
| 145 | if (WARN_ON(!func->patched)) |
| 146 | return; |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 147 | if (WARN_ON(!func->old_func)) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 148 | return; |
| 149 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 150 | ops = klp_find_ops(func->old_func); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 151 | if (WARN_ON(!ops)) |
| 152 | return; |
| 153 | |
| 154 | if (list_is_singular(&ops->func_stack)) { |
| 155 | unsigned long ftrace_loc; |
| 156 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 157 | ftrace_loc = |
| 158 | klp_get_ftrace_location((unsigned long)func->old_func); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 159 | if (WARN_ON(!ftrace_loc)) |
| 160 | return; |
| 161 | |
| 162 | WARN_ON(unregister_ftrace_function(&ops->fops)); |
| 163 | WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); |
| 164 | |
| 165 | list_del_rcu(&func->stack_node); |
| 166 | list_del(&ops->node); |
| 167 | kfree(ops); |
| 168 | } else { |
| 169 | list_del_rcu(&func->stack_node); |
| 170 | } |
| 171 | |
| 172 | func->patched = false; |
| 173 | } |
| 174 | |
| 175 | static int klp_patch_func(struct klp_func *func) |
| 176 | { |
| 177 | struct klp_ops *ops; |
| 178 | int ret; |
| 179 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 180 | if (WARN_ON(!func->old_func)) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 181 | return -EINVAL; |
| 182 | |
| 183 | if (WARN_ON(func->patched)) |
| 184 | return -EINVAL; |
| 185 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 186 | ops = klp_find_ops(func->old_func); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 187 | if (!ops) { |
| 188 | unsigned long ftrace_loc; |
| 189 | |
Petr Mladek | 1951491 | 2019-01-09 13:43:19 +0100 | [diff] [blame] | 190 | ftrace_loc = |
| 191 | klp_get_ftrace_location((unsigned long)func->old_func); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 192 | if (!ftrace_loc) { |
| 193 | pr_err("failed to find location for function '%s'\n", |
| 194 | func->old_name); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); |
| 199 | if (!ops) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | ops->fops.func = klp_ftrace_handler; |
| 203 | ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS | |
| 204 | FTRACE_OPS_FL_DYNAMIC | |
Miroslav Benes | 7162431 | 2019-10-16 13:33:13 +0200 | [diff] [blame] | 205 | FTRACE_OPS_FL_IPMODIFY | |
| 206 | FTRACE_OPS_FL_PERMANENT; |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 207 | |
| 208 | list_add(&ops->node, &klp_ops); |
| 209 | |
| 210 | INIT_LIST_HEAD(&ops->func_stack); |
| 211 | list_add_rcu(&func->stack_node, &ops->func_stack); |
| 212 | |
| 213 | ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0); |
| 214 | if (ret) { |
| 215 | pr_err("failed to set ftrace filter for function '%s' (%d)\n", |
| 216 | func->old_name, ret); |
| 217 | goto err; |
| 218 | } |
| 219 | |
| 220 | ret = register_ftrace_function(&ops->fops); |
| 221 | if (ret) { |
| 222 | pr_err("failed to register ftrace handler for function '%s' (%d)\n", |
| 223 | func->old_name, ret); |
| 224 | ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0); |
| 225 | goto err; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | } else { |
| 230 | list_add_rcu(&func->stack_node, &ops->func_stack); |
| 231 | } |
| 232 | |
| 233 | func->patched = true; |
| 234 | |
| 235 | return 0; |
| 236 | |
| 237 | err: |
| 238 | list_del_rcu(&func->stack_node); |
| 239 | list_del(&ops->node); |
| 240 | kfree(ops); |
| 241 | return ret; |
| 242 | } |
| 243 | |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 244 | static void __klp_unpatch_object(struct klp_object *obj, bool nops_only) |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 245 | { |
| 246 | struct klp_func *func; |
| 247 | |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 248 | klp_for_each_func(obj, func) { |
| 249 | if (nops_only && !func->nop) |
| 250 | continue; |
| 251 | |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 252 | if (func->patched) |
| 253 | klp_unpatch_func(func); |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 254 | } |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 255 | |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 256 | if (obj->dynamic || !nops_only) |
| 257 | obj->patched = false; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | void klp_unpatch_object(struct klp_object *obj) |
| 262 | { |
| 263 | __klp_unpatch_object(obj, false); |
Josh Poimboeuf | c349cdc | 2017-02-13 19:42:37 -0600 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int klp_patch_object(struct klp_object *obj) |
| 267 | { |
| 268 | struct klp_func *func; |
| 269 | int ret; |
| 270 | |
| 271 | if (WARN_ON(obj->patched)) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | klp_for_each_func(obj, func) { |
| 275 | ret = klp_patch_func(func); |
| 276 | if (ret) { |
| 277 | klp_unpatch_object(obj); |
| 278 | return ret; |
| 279 | } |
| 280 | } |
| 281 | obj->patched = true; |
| 282 | |
| 283 | return 0; |
| 284 | } |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 285 | |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 286 | static void __klp_unpatch_objects(struct klp_patch *patch, bool nops_only) |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 287 | { |
| 288 | struct klp_object *obj; |
| 289 | |
| 290 | klp_for_each_object(patch, obj) |
| 291 | if (obj->patched) |
Petr Mladek | d697bad | 2019-01-09 13:43:26 +0100 | [diff] [blame] | 292 | __klp_unpatch_object(obj, nops_only); |
| 293 | } |
| 294 | |
| 295 | void klp_unpatch_objects(struct klp_patch *patch) |
| 296 | { |
| 297 | __klp_unpatch_objects(patch, false); |
| 298 | } |
| 299 | |
| 300 | void klp_unpatch_objects_dynamic(struct klp_patch *patch) |
| 301 | { |
| 302 | __klp_unpatch_objects(patch, true); |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 303 | } |