1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/cpu.h>
23#include <linux/stacktrace.h>
24#include "core.h"
25#include "patch.h"
26#include "transition.h"
27#include "../sched/sched.h"
28
29#define MAX_STACK_ENTRIES 100
30#define STACK_ERR_BUF_SIZE 128
31
32struct klp_patch *klp_transition_patch;
33
34static int klp_target_state = KLP_UNDEFINED;
35
36
37
38
39
40static void klp_transition_work_fn(struct work_struct *work)
41{
42 mutex_lock(&klp_mutex);
43
44 if (klp_transition_patch)
45 klp_try_complete_transition();
46
47 mutex_unlock(&klp_mutex);
48}
49static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
50
51
52
53
54
55
56static void klp_sync(struct work_struct *work)
57{
58}
59
60
61
62
63
64
65
66
67
68static void klp_synchronize_transition(void)
69{
70 schedule_on_each_cpu(klp_sync);
71}
72
73
74
75
76
77static void klp_complete_transition(void)
78{
79 struct klp_object *obj;
80 struct klp_func *func;
81 struct task_struct *g, *task;
82 unsigned int cpu;
83 bool immediate_func = false;
84
85 if (klp_target_state == KLP_UNPATCHED) {
86
87
88
89
90 klp_unpatch_objects(klp_transition_patch);
91
92
93
94
95
96
97
98 klp_synchronize_transition();
99 }
100
101 if (klp_transition_patch->immediate)
102 goto done;
103
104 klp_for_each_object(klp_transition_patch, obj) {
105 klp_for_each_func(obj, func) {
106 func->transition = false;
107 if (func->immediate)
108 immediate_func = true;
109 }
110 }
111
112 if (klp_target_state == KLP_UNPATCHED && !immediate_func)
113 module_put(klp_transition_patch->mod);
114
115
116 if (klp_target_state == KLP_PATCHED)
117 klp_synchronize_transition();
118
119 read_lock(&tasklist_lock);
120 for_each_process_thread(g, task) {
121 WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
122 task->patch_state = KLP_UNDEFINED;
123 }
124 read_unlock(&tasklist_lock);
125
126 for_each_possible_cpu(cpu) {
127 task = idle_task(cpu);
128 WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
129 task->patch_state = KLP_UNDEFINED;
130 }
131
132done:
133 klp_target_state = KLP_UNDEFINED;
134 klp_transition_patch = NULL;
135}
136
137
138
139
140
141
142
143void klp_cancel_transition(void)
144{
145 if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED))
146 return;
147
148 klp_target_state = KLP_UNPATCHED;
149 klp_complete_transition();
150}
151
152
153
154
155
156
157
158
159void klp_update_patch_state(struct task_struct *task)
160{
161
162
163
164
165 preempt_disable_notrace();
166
167
168
169
170
171
172
173
174
175
176
177
178
179 if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING))
180 task->patch_state = READ_ONCE(klp_target_state);
181
182 preempt_enable_notrace();
183}
184
185
186
187
188
189static int klp_check_stack_func(struct klp_func *func,
190 struct stack_trace *trace)
191{
192 unsigned long func_addr, func_size, address;
193 struct klp_ops *ops;
194 int i;
195
196 if (func->immediate)
197 return 0;
198
199 for (i = 0; i < trace->nr_entries; i++) {
200 address = trace->entries[i];
201
202 if (klp_target_state == KLP_UNPATCHED) {
203
204
205
206
207 func_addr = (unsigned long)func->new_func;
208 func_size = func->new_size;
209 } else {
210
211
212
213
214 ops = klp_find_ops(func->old_addr);
215
216 if (list_is_singular(&ops->func_stack)) {
217
218 func_addr = func->old_addr;
219 func_size = func->old_size;
220 } else {
221
222 struct klp_func *prev;
223
224 prev = list_next_entry(func, stack_node);
225 func_addr = (unsigned long)prev->new_func;
226 func_size = prev->new_size;
227 }
228 }
229
230 if (address >= func_addr && address < func_addr + func_size)
231 return -EAGAIN;
232 }
233
234 return 0;
235}
236
237
238
239
240
241static int klp_check_stack(struct task_struct *task, char *err_buf)
242{
243 static unsigned long entries[MAX_STACK_ENTRIES];
244 struct stack_trace trace;
245 struct klp_object *obj;
246 struct klp_func *func;
247 int ret;
248
249 trace.skip = 0;
250 trace.nr_entries = 0;
251 trace.max_entries = MAX_STACK_ENTRIES;
252 trace.entries = entries;
253 ret = save_stack_trace_tsk_reliable(task, &trace);
254 WARN_ON_ONCE(ret == -ENOSYS);
255 if (ret) {
256 snprintf(err_buf, STACK_ERR_BUF_SIZE,
257 "%s: %s:%d has an unreliable stack\n",
258 __func__, task->comm, task->pid);
259 return ret;
260 }
261
262 klp_for_each_object(klp_transition_patch, obj) {
263 if (!obj->patched)
264 continue;
265 klp_for_each_func(obj, func) {
266 ret = klp_check_stack_func(func, &trace);
267 if (ret) {
268 snprintf(err_buf, STACK_ERR_BUF_SIZE,
269 "%s: %s:%d is sleeping on function %s\n",
270 __func__, task->comm, task->pid,
271 func->old_name);
272 return ret;
273 }
274 }
275 }
276
277 return 0;
278}
279
280
281
282
283
284
285static bool klp_try_switch_task(struct task_struct *task)
286{
287 struct rq *rq;
288 struct rq_flags flags;
289 int ret;
290 bool success = false;
291 char err_buf[STACK_ERR_BUF_SIZE];
292
293 err_buf[0] = '\0';
294
295
296 if (task->patch_state == klp_target_state)
297 return true;
298
299
300
301
302
303 if (!klp_have_reliable_stack())
304 return false;
305
306
307
308
309
310
311 rq = task_rq_lock(task, &flags);
312
313 if (task_running(rq, task) && task != current) {
314 snprintf(err_buf, STACK_ERR_BUF_SIZE,
315 "%s: %s:%d is running\n", __func__, task->comm,
316 task->pid);
317 goto done;
318 }
319
320 ret = klp_check_stack(task, err_buf);
321 if (ret)
322 goto done;
323
324 success = true;
325
326 clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
327 task->patch_state = klp_target_state;
328
329done:
330 task_rq_unlock(rq, task, &flags);
331
332
333
334
335
336
337 if (err_buf[0] != '\0')
338 pr_debug("%s", err_buf);
339
340 return success;
341
342}
343
344
345
346
347
348
349
350
351
352void klp_try_complete_transition(void)
353{
354 unsigned int cpu;
355 struct task_struct *g, *task;
356 bool complete = true;
357
358 WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
359
360
361
362
363
364 if (klp_transition_patch->immediate)
365 goto success;
366
367
368
369
370
371
372
373
374
375
376 read_lock(&tasklist_lock);
377 for_each_process_thread(g, task)
378 if (!klp_try_switch_task(task))
379 complete = false;
380 read_unlock(&tasklist_lock);
381
382
383
384
385 get_online_cpus();
386 for_each_possible_cpu(cpu) {
387 task = idle_task(cpu);
388 if (cpu_online(cpu)) {
389 if (!klp_try_switch_task(task))
390 complete = false;
391 } else if (task->patch_state != klp_target_state) {
392
393 clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
394 task->patch_state = klp_target_state;
395 }
396 }
397 put_online_cpus();
398
399 if (!complete) {
400
401
402
403
404
405 schedule_delayed_work(&klp_transition_work,
406 round_jiffies_relative(HZ));
407 return;
408 }
409
410success:
411 pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name,
412 klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
413
414
415 klp_complete_transition();
416}
417
418
419
420
421
422void klp_start_transition(void)
423{
424 struct task_struct *g, *task;
425 unsigned int cpu;
426
427 WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
428
429 pr_notice("'%s': %s...\n", klp_transition_patch->mod->name,
430 klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
431
432
433
434
435
436 if (klp_transition_patch->immediate)
437 return;
438
439
440
441
442
443
444 read_lock(&tasklist_lock);
445 for_each_process_thread(g, task)
446 if (task->patch_state != klp_target_state)
447 set_tsk_thread_flag(task, TIF_PATCH_PENDING);
448 read_unlock(&tasklist_lock);
449
450
451
452
453
454
455 for_each_possible_cpu(cpu) {
456 task = idle_task(cpu);
457 if (task->patch_state != klp_target_state)
458 set_tsk_thread_flag(task, TIF_PATCH_PENDING);
459 }
460}
461
462
463
464
465
466
467void klp_init_transition(struct klp_patch *patch, int state)
468{
469 struct task_struct *g, *task;
470 unsigned int cpu;
471 struct klp_object *obj;
472 struct klp_func *func;
473 int initial_state = !state;
474
475 WARN_ON_ONCE(klp_target_state != KLP_UNDEFINED);
476
477 klp_transition_patch = patch;
478
479
480
481
482
483 klp_target_state = state;
484
485
486
487
488
489 if (patch->immediate)
490 return;
491
492
493
494
495
496 read_lock(&tasklist_lock);
497 for_each_process_thread(g, task) {
498 WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED);
499 task->patch_state = initial_state;
500 }
501 read_unlock(&tasklist_lock);
502
503
504
505
506 for_each_possible_cpu(cpu) {
507 task = idle_task(cpu);
508 WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED);
509 task->patch_state = initial_state;
510 }
511
512
513
514
515
516
517
518
519
520
521 smp_wmb();
522
523
524
525
526
527
528
529
530
531
532
533
534 klp_for_each_object(patch, obj)
535 klp_for_each_func(obj, func)
536 func->transition = true;
537}
538
539
540
541
542
543
544
545void klp_reverse_transition(void)
546{
547 unsigned int cpu;
548 struct task_struct *g, *task;
549
550 klp_transition_patch->enabled = !klp_transition_patch->enabled;
551
552 klp_target_state = !klp_target_state;
553
554
555
556
557
558
559 read_lock(&tasklist_lock);
560 for_each_process_thread(g, task)
561 clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
562 read_unlock(&tasklist_lock);
563
564 for_each_possible_cpu(cpu)
565 clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING);
566
567
568 klp_synchronize_transition();
569
570 klp_start_transition();
571}
572
573
574void klp_copy_process(struct task_struct *child)
575{
576 child->patch_state = current->patch_state;
577
578
579}
580