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