1 /* 2 * transition.c - Kernel Live Patching transition functions 3 * 4 * Copyright (C) 2015-2016 Josh Poimboeuf <[email protected]> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 21 22 #include <linux/cpu.h> 23 #include <linux/stacktrace.h> 24 #include "patch.h" 25 #include "transition.h" 26 #include "../sched/sched.h" 27 28 #define MAX_STACK_ENTRIES 100 29 #define STACK_ERR_BUF_SIZE 128 30 31 extern struct mutex klp_mutex; 32 33 struct klp_patch *klp_transition_patch; 34 35 static int klp_target_state = KLP_UNDEFINED; 36 37 /* 38 * This work can be performed periodically to finish patching or unpatching any 39 * "straggler" tasks which failed to transition in the first attempt. 40 */ 41 static void klp_transition_work_fn(struct work_struct *work) 42 { 43 mutex_lock(&klp_mutex); 44 45 if (klp_transition_patch) 46 klp_try_complete_transition(); 47 48 mutex_unlock(&klp_mutex); 49 } 50 static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn); 51 52 /* 53 * The transition to the target patch state is complete. Clean up the data 54 * structures. 55 */ 56 static void klp_complete_transition(void) 57 { 58 struct klp_object *obj; 59 struct klp_func *func; 60 struct task_struct *g, *task; 61 unsigned int cpu; 62 bool immediate_func = false; 63 64 if (klp_target_state == KLP_UNPATCHED) { 65 /* 66 * All tasks have transitioned to KLP_UNPATCHED so we can now 67 * remove the new functions from the func_stack. 68 */ 69 klp_unpatch_objects(klp_transition_patch); 70 71 /* 72 * Make sure klp_ftrace_handler() can no longer see functions 73 * from this patch on the ops->func_stack. Otherwise, after 74 * func->transition gets cleared, the handler may choose a 75 * removed function. 76 */ 77 synchronize_rcu(); 78 } 79 80 if (klp_transition_patch->immediate) 81 goto done; 82 83 klp_for_each_object(klp_transition_patch, obj) { 84 klp_for_each_func(obj, func) { 85 func->transition = false; 86 if (func->immediate) 87 immediate_func = true; 88 } 89 } 90 91 if (klp_target_state == KLP_UNPATCHED && !immediate_func) 92 module_put(klp_transition_patch->mod); 93 94 /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */ 95 if (klp_target_state == KLP_PATCHED) 96 synchronize_rcu(); 97 98 read_lock(&tasklist_lock); 99 for_each_process_thread(g, task) { 100 WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)); 101 task->patch_state = KLP_UNDEFINED; 102 } 103 read_unlock(&tasklist_lock); 104 105 for_each_possible_cpu(cpu) { 106 task = idle_task(cpu); 107 WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)); 108 task->patch_state = KLP_UNDEFINED; 109 } 110 111 done: 112 klp_target_state = KLP_UNDEFINED; 113 klp_transition_patch = NULL; 114 } 115 116 /* 117 * This is called in the error path, to cancel a transition before it has 118 * started, i.e. klp_init_transition() has been called but 119 * klp_start_transition() hasn't. If the transition *has* been started, 120 * klp_reverse_transition() should be used instead. 121 */ 122 void klp_cancel_transition(void) 123 { 124 struct klp_patch *patch = klp_transition_patch; 125 struct klp_object *obj; 126 struct klp_func *func; 127 bool immediate_func = false; 128 129 if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED)) 130 return; 131 132 klp_target_state = KLP_UNPATCHED; 133 klp_complete_transition(); 134 135 /* 136 * In the enable error path, even immediate patches can be safely 137 * removed because the transition hasn't been started yet. 138 * 139 * klp_complete_transition() doesn't have a module_put() for immediate 140 * patches, so do it here. 141 */ 142 klp_for_each_object(patch, obj) 143 klp_for_each_func(obj, func) 144 if (func->immediate) 145 immediate_func = true; 146 147 if (patch->immediate || immediate_func) 148 module_put(patch->mod); 149 } 150 151 /* 152 * Switch the patched state of the task to the set of functions in the target 153 * patch state. 154 * 155 * NOTE: If task is not 'current', the caller must ensure the task is inactive. 156 * Otherwise klp_ftrace_handler() might read the wrong 'patch_state' value. 157 */ 158 void klp_update_patch_state(struct task_struct *task) 159 { 160 rcu_read_lock(); 161 162 /* 163 * This test_and_clear_tsk_thread_flag() call also serves as a read 164 * barrier (smp_rmb) for two cases: 165 * 166 * 1) Enforce the order of the TIF_PATCH_PENDING read and the 167 * klp_target_state read. The corresponding write barrier is in 168 * klp_init_transition(). 169 * 170 * 2) Enforce the order of the TIF_PATCH_PENDING read and a future read 171 * of func->transition, if klp_ftrace_handler() is called later on 172 * the same CPU. See __klp_disable_patch(). 173 */ 174 if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING)) 175 task->patch_state = READ_ONCE(klp_target_state); 176 177 rcu_read_unlock(); 178 } 179 180 /* 181 * Determine whether the given stack trace includes any references to a 182 * to-be-patched or to-be-unpatched function. 183 */ 184 static int klp_check_stack_func(struct klp_func *func, 185 struct stack_trace *trace) 186 { 187 unsigned long func_addr, func_size, address; 188 struct klp_ops *ops; 189 int i; 190 191 if (func->immediate) 192 return 0; 193 194 for (i = 0; i < trace->nr_entries; i++) { 195 address = trace->entries[i]; 196 197 if (klp_target_state == KLP_UNPATCHED) { 198 /* 199 * Check for the to-be-unpatched function 200 * (the func itself). 201 */ 202 func_addr = (unsigned long)func->new_func; 203 func_size = func->new_size; 204 } else { 205 /* 206 * Check for the to-be-patched function 207 * (the previous func). 208 */ 209 ops = klp_find_ops(func->old_addr); 210 211 if (list_is_singular(&ops->func_stack)) { 212 /* original function */ 213 func_addr = func->old_addr; 214 func_size = func->old_size; 215 } else { 216 /* previously patched function */ 217 struct klp_func *prev; 218 219 prev = list_next_entry(func, stack_node); 220 func_addr = (unsigned long)prev->new_func; 221 func_size = prev->new_size; 222 } 223 } 224 225 if (address >= func_addr && address < func_addr + func_size) 226 return -EAGAIN; 227 } 228 229 return 0; 230 } 231 232 /* 233 * Determine whether it's safe to transition the task to the target patch state 234 * by looking for any to-be-patched or to-be-unpatched functions on its stack. 235 */ 236 static int klp_check_stack(struct task_struct *task, char *err_buf) 237 { 238 static unsigned long entries[MAX_STACK_ENTRIES]; 239 struct stack_trace trace; 240 struct klp_object *obj; 241 struct klp_func *func; 242 int ret; 243 244 trace.skip = 0; 245 trace.nr_entries = 0; 246 trace.max_entries = MAX_STACK_ENTRIES; 247 trace.entries = entries; 248 ret = save_stack_trace_tsk_reliable(task, &trace); 249 WARN_ON_ONCE(ret == -ENOSYS); 250 if (ret) { 251 snprintf(err_buf, STACK_ERR_BUF_SIZE, 252 "%s: %s:%d has an unreliable stack\n", 253 __func__, task->comm, task->pid); 254 return ret; 255 } 256 257 klp_for_each_object(klp_transition_patch, obj) { 258 if (!obj->patched) 259 continue; 260 klp_for_each_func(obj, func) { 261 ret = klp_check_stack_func(func, &trace); 262 if (ret) { 263 snprintf(err_buf, STACK_ERR_BUF_SIZE, 264 "%s: %s:%d is sleeping on function %s\n", 265 __func__, task->comm, task->pid, 266 func->old_name); 267 return ret; 268 } 269 } 270 } 271 272 return 0; 273 } 274 275 /* 276 * Try to safely switch a task to the target patch state. If it's currently 277 * running, or it's sleeping on a to-be-patched or to-be-unpatched function, or 278 * if the stack is unreliable, return false. 279 */ 280 static bool klp_try_switch_task(struct task_struct *task) 281 { 282 struct rq *rq; 283 struct rq_flags flags; 284 int ret; 285 bool success = false; 286 char err_buf[STACK_ERR_BUF_SIZE]; 287 288 err_buf[0] = '\0'; 289 290 /* check if this task has already switched over */ 291 if (task->patch_state == klp_target_state) 292 return true; 293 294 /* 295 * For arches which don't have reliable stack traces, we have to rely 296 * on other methods (e.g., switching tasks at kernel exit). 297 */ 298 if (!klp_have_reliable_stack()) 299 return false; 300 301 /* 302 * Now try to check the stack for any to-be-patched or to-be-unpatched 303 * functions. If all goes well, switch the task to the target patch 304 * state. 305 */ 306 rq = task_rq_lock(task, &flags); 307 308 if (task_running(rq, task) && task != current) { 309 snprintf(err_buf, STACK_ERR_BUF_SIZE, 310 "%s: %s:%d is running\n", __func__, task->comm, 311 task->pid); 312 goto done; 313 } 314 315 ret = klp_check_stack(task, err_buf); 316 if (ret) 317 goto done; 318 319 success = true; 320 321 clear_tsk_thread_flag(task, TIF_PATCH_PENDING); 322 task->patch_state = klp_target_state; 323 324 done: 325 task_rq_unlock(rq, task, &flags); 326 327 /* 328 * Due to console deadlock issues, pr_debug() can't be used while 329 * holding the task rq lock. Instead we have to use a temporary buffer 330 * and print the debug message after releasing the lock. 331 */ 332 if (err_buf[0] != '\0') 333 pr_debug("%s", err_buf); 334 335 return success; 336 337 } 338 339 /* 340 * Try to switch all remaining tasks to the target patch state by walking the 341 * stacks of sleeping tasks and looking for any to-be-patched or 342 * to-be-unpatched functions. If such functions are found, the task can't be 343 * switched yet. 344 * 345 * If any tasks are still stuck in the initial patch state, schedule a retry. 346 */ 347 void klp_try_complete_transition(void) 348 { 349 unsigned int cpu; 350 struct task_struct *g, *task; 351 bool complete = true; 352 353 WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED); 354 355 /* 356 * If the patch can be applied or reverted immediately, skip the 357 * per-task transitions. 358 */ 359 if (klp_transition_patch->immediate) 360 goto success; 361 362 /* 363 * Try to switch the tasks to the target patch state by walking their 364 * stacks and looking for any to-be-patched or to-be-unpatched 365 * functions. If such functions are found on a stack, or if the stack 366 * is deemed unreliable, the task can't be switched yet. 367 * 368 * Usually this will transition most (or all) of the tasks on a system 369 * unless the patch includes changes to a very common function. 370 */ 371 read_lock(&tasklist_lock); 372 for_each_process_thread(g, task) 373 if (!klp_try_switch_task(task)) 374 complete = false; 375 read_unlock(&tasklist_lock); 376 377 /* 378 * Ditto for the idle "swapper" tasks. 379 */ 380 get_online_cpus(); 381 for_each_possible_cpu(cpu) { 382 task = idle_task(cpu); 383 if (cpu_online(cpu)) { 384 if (!klp_try_switch_task(task)) 385 complete = false; 386 } else if (task->patch_state != klp_target_state) { 387 /* offline idle tasks can be switched immediately */ 388 clear_tsk_thread_flag(task, TIF_PATCH_PENDING); 389 task->patch_state = klp_target_state; 390 } 391 } 392 put_online_cpus(); 393 394 if (!complete) { 395 /* 396 * Some tasks weren't able to be switched over. Try again 397 * later and/or wait for other methods like kernel exit 398 * switching. 399 */ 400 schedule_delayed_work(&klp_transition_work, 401 round_jiffies_relative(HZ)); 402 return; 403 } 404 405 success: 406 pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name, 407 klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); 408 409 /* we're done, now cleanup the data structures */ 410 klp_complete_transition(); 411 } 412 413 /* 414 * Start the transition to the specified target patch state so tasks can begin 415 * switching to it. 416 */ 417 void klp_start_transition(void) 418 { 419 struct task_struct *g, *task; 420 unsigned int cpu; 421 422 WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED); 423 424 pr_notice("'%s': %s...\n", klp_transition_patch->mod->name, 425 klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); 426 427 /* 428 * If the patch can be applied or reverted immediately, skip the 429 * per-task transitions. 430 */ 431 if (klp_transition_patch->immediate) 432 return; 433 434 /* 435 * Mark all normal tasks as needing a patch state update. They'll 436 * switch either in klp_try_complete_transition() or as they exit the 437 * kernel. 438 */ 439 read_lock(&tasklist_lock); 440 for_each_process_thread(g, task) 441 if (task->patch_state != klp_target_state) 442 set_tsk_thread_flag(task, TIF_PATCH_PENDING); 443 read_unlock(&tasklist_lock); 444 445 /* 446 * Mark all idle tasks as needing a patch state update. They'll switch 447 * either in klp_try_complete_transition() or at the idle loop switch 448 * point. 449 */ 450 for_each_possible_cpu(cpu) { 451 task = idle_task(cpu); 452 if (task->patch_state != klp_target_state) 453 set_tsk_thread_flag(task, TIF_PATCH_PENDING); 454 } 455 } 456 457 /* 458 * Initialize the global target patch state and all tasks to the initial patch 459 * state, and initialize all function transition states to true in preparation 460 * for patching or unpatching. 461 */ 462 void klp_init_transition(struct klp_patch *patch, int state) 463 { 464 struct task_struct *g, *task; 465 unsigned int cpu; 466 struct klp_object *obj; 467 struct klp_func *func; 468 int initial_state = !state; 469 470 WARN_ON_ONCE(klp_target_state != KLP_UNDEFINED); 471 472 klp_transition_patch = patch; 473 474 /* 475 * Set the global target patch state which tasks will switch to. This 476 * has no effect until the TIF_PATCH_PENDING flags get set later. 477 */ 478 klp_target_state = state; 479 480 /* 481 * If the patch can be applied or reverted immediately, skip the 482 * per-task transitions. 483 */ 484 if (patch->immediate) 485 return; 486 487 /* 488 * Initialize all tasks to the initial patch state to prepare them for 489 * switching to the target state. 490 */ 491 read_lock(&tasklist_lock); 492 for_each_process_thread(g, task) { 493 WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED); 494 task->patch_state = initial_state; 495 } 496 read_unlock(&tasklist_lock); 497 498 /* 499 * Ditto for the idle "swapper" tasks. 500 */ 501 for_each_possible_cpu(cpu) { 502 task = idle_task(cpu); 503 WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED); 504 task->patch_state = initial_state; 505 } 506 507 /* 508 * Enforce the order of the task->patch_state initializations and the 509 * func->transition updates to ensure that klp_ftrace_handler() doesn't 510 * see a func in transition with a task->patch_state of KLP_UNDEFINED. 511 * 512 * Also enforce the order of the klp_target_state write and future 513 * TIF_PATCH_PENDING writes to ensure klp_update_patch_state() doesn't 514 * set a task->patch_state to KLP_UNDEFINED. 515 */ 516 smp_wmb(); 517 518 /* 519 * Set the func transition states so klp_ftrace_handler() will know to 520 * switch to the transition logic. 521 * 522 * When patching, the funcs aren't yet in the func_stack and will be 523 * made visible to the ftrace handler shortly by the calls to 524 * klp_patch_object(). 525 * 526 * When unpatching, the funcs are already in the func_stack and so are 527 * already visible to the ftrace handler. 528 */ 529 klp_for_each_object(patch, obj) 530 klp_for_each_func(obj, func) 531 func->transition = true; 532 } 533 534 /* 535 * This function can be called in the middle of an existing transition to 536 * reverse the direction of the target patch state. This can be done to 537 * effectively cancel an existing enable or disable operation if there are any 538 * tasks which are stuck in the initial patch state. 539 */ 540 void klp_reverse_transition(void) 541 { 542 unsigned int cpu; 543 struct task_struct *g, *task; 544 545 klp_transition_patch->enabled = !klp_transition_patch->enabled; 546 547 klp_target_state = !klp_target_state; 548 549 /* 550 * Clear all TIF_PATCH_PENDING flags to prevent races caused by 551 * klp_update_patch_state() running in parallel with 552 * klp_start_transition(). 553 */ 554 read_lock(&tasklist_lock); 555 for_each_process_thread(g, task) 556 clear_tsk_thread_flag(task, TIF_PATCH_PENDING); 557 read_unlock(&tasklist_lock); 558 559 for_each_possible_cpu(cpu) 560 clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING); 561 562 /* Let any remaining calls to klp_update_patch_state() complete */ 563 synchronize_rcu(); 564 565 klp_start_transition(); 566 } 567 568 /* Called from copy_process() during fork */ 569 void klp_copy_process(struct task_struct *child) 570 { 571 child->patch_state = current->patch_state; 572 573 /* TIF_PATCH_PENDING gets copied in setup_thread_stack() */ 574 } 575