GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm64 / kernel / probes / kprobes.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch/arm64/kernel/probes/kprobes.c
4  *
5  * Kprobes support for ARM64
6  *
7  * Copyright (C) 2013 Linaro Limited.
8  * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
9  */
10 #include <linux/kasan.h>
11 #include <linux/kernel.h>
12 #include <linux/kprobes.h>
13 #include <linux/extable.h>
14 #include <linux/slab.h>
15 #include <linux/stop_machine.h>
16 #include <linux/sched/debug.h>
17 #include <linux/set_memory.h>
18 #include <linux/stringify.h>
19 #include <linux/vmalloc.h>
20 #include <asm/traps.h>
21 #include <asm/ptrace.h>
22 #include <asm/cacheflush.h>
23 #include <asm/debug-monitors.h>
24 #include <asm/daifflags.h>
25 #include <asm/system_misc.h>
26 #include <asm/insn.h>
27 #include <linux/uaccess.h>
28 #include <asm/irq.h>
29 #include <asm/sections.h>
30
31 #include "decode-insn.h"
32
33 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
34 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
35
36 static void __kprobes
37 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
38
39 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
40 {
41         kprobe_opcode_t *addr = p->ainsn.api.insn;
42         void *addrs[] = {addr, addr + 1};
43         u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
44
45         /* prepare insn slot */
46         aarch64_insn_patch_text(addrs, insns, 2);
47
48         flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
49
50         /*
51          * Needs restoring of return address after stepping xol.
52          */
53         p->ainsn.api.restore = (unsigned long) p->addr +
54           sizeof(kprobe_opcode_t);
55 }
56
57 static void __kprobes arch_prepare_simulate(struct kprobe *p)
58 {
59         /* This instructions is not executed xol. No need to adjust the PC */
60         p->ainsn.api.restore = 0;
61 }
62
63 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
64 {
65         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
66
67         if (p->ainsn.api.handler)
68                 p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
69
70         /* single step simulated, now go for post processing */
71         post_kprobe_handler(kcb, regs);
72 }
73
74 int __kprobes arch_prepare_kprobe(struct kprobe *p)
75 {
76         unsigned long probe_addr = (unsigned long)p->addr;
77
78         if (probe_addr & 0x3)
79                 return -EINVAL;
80
81         /* copy instruction */
82         p->opcode = le32_to_cpu(*p->addr);
83
84         if (search_exception_tables(probe_addr))
85                 return -EINVAL;
86
87         /* decode instruction */
88         switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
89         case INSN_REJECTED:     /* insn not supported */
90                 return -EINVAL;
91
92         case INSN_GOOD_NO_SLOT: /* insn need simulation */
93                 p->ainsn.api.insn = NULL;
94                 break;
95
96         case INSN_GOOD: /* instruction uses slot */
97                 p->ainsn.api.insn = get_insn_slot();
98                 if (!p->ainsn.api.insn)
99                         return -ENOMEM;
100                 break;
101         }
102
103         /* prepare the instruction */
104         if (p->ainsn.api.insn)
105                 arch_prepare_ss_slot(p);
106         else
107                 arch_prepare_simulate(p);
108
109         return 0;
110 }
111
112 void *alloc_insn_page(void)
113 {
114         void *page;
115
116         page = vmalloc_exec(PAGE_SIZE);
117         if (page) {
118                 set_memory_ro((unsigned long)page, 1);
119                 set_vm_flush_reset_perms(page);
120         }
121
122         return page;
123 }
124
125 /* arm kprobe: install breakpoint in text */
126 void __kprobes arch_arm_kprobe(struct kprobe *p)
127 {
128         void *addr = p->addr;
129         u32 insn = BRK64_OPCODE_KPROBES;
130
131         aarch64_insn_patch_text(&addr, &insn, 1);
132 }
133
134 /* disarm kprobe: remove breakpoint from text */
135 void __kprobes arch_disarm_kprobe(struct kprobe *p)
136 {
137         void *addr = p->addr;
138
139         aarch64_insn_patch_text(&addr, &p->opcode, 1);
140 }
141
142 void __kprobes arch_remove_kprobe(struct kprobe *p)
143 {
144         if (p->ainsn.api.insn) {
145                 free_insn_slot(p->ainsn.api.insn, 0);
146                 p->ainsn.api.insn = NULL;
147         }
148 }
149
150 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
151 {
152         kcb->prev_kprobe.kp = kprobe_running();
153         kcb->prev_kprobe.status = kcb->kprobe_status;
154 }
155
156 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
157 {
158         __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
159         kcb->kprobe_status = kcb->prev_kprobe.status;
160 }
161
162 static void __kprobes set_current_kprobe(struct kprobe *p)
163 {
164         __this_cpu_write(current_kprobe, p);
165 }
166
167 /*
168  * Mask all of DAIF while executing the instruction out-of-line, to keep things
169  * simple and avoid nesting exceptions. Interrupts do have to be disabled since
170  * the kprobe state is per-CPU and doesn't get migrated.
171  */
172 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
173                                                 struct pt_regs *regs)
174 {
175         kcb->saved_irqflag = regs->pstate & DAIF_MASK;
176         regs->pstate |= DAIF_MASK;
177 }
178
179 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
180                                                 struct pt_regs *regs)
181 {
182         regs->pstate &= ~DAIF_MASK;
183         regs->pstate |= kcb->saved_irqflag;
184 }
185
186 static void __kprobes
187 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
188 {
189         kcb->ss_ctx.ss_pending = true;
190         kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
191 }
192
193 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
194 {
195         kcb->ss_ctx.ss_pending = false;
196         kcb->ss_ctx.match_addr = 0;
197 }
198
199 static void __kprobes setup_singlestep(struct kprobe *p,
200                                        struct pt_regs *regs,
201                                        struct kprobe_ctlblk *kcb, int reenter)
202 {
203         unsigned long slot;
204
205         if (reenter) {
206                 save_previous_kprobe(kcb);
207                 set_current_kprobe(p);
208                 kcb->kprobe_status = KPROBE_REENTER;
209         } else {
210                 kcb->kprobe_status = KPROBE_HIT_SS;
211         }
212
213
214         if (p->ainsn.api.insn) {
215                 /* prepare for single stepping */
216                 slot = (unsigned long)p->ainsn.api.insn;
217
218                 set_ss_context(kcb, slot);      /* mark pending ss */
219                 kprobes_save_local_irqflag(kcb, regs);
220                 instruction_pointer_set(regs, slot);
221         } else {
222                 /* insn simulation */
223                 arch_simulate_insn(p, regs);
224         }
225 }
226
227 static int __kprobes reenter_kprobe(struct kprobe *p,
228                                     struct pt_regs *regs,
229                                     struct kprobe_ctlblk *kcb)
230 {
231         switch (kcb->kprobe_status) {
232         case KPROBE_HIT_SSDONE:
233         case KPROBE_HIT_ACTIVE:
234                 kprobes_inc_nmissed_count(p);
235                 setup_singlestep(p, regs, kcb, 1);
236                 break;
237         case KPROBE_HIT_SS:
238         case KPROBE_REENTER:
239                 pr_warn("Unrecoverable kprobe detected.\n");
240                 dump_kprobe(p);
241                 BUG();
242                 break;
243         default:
244                 WARN_ON(1);
245                 return 0;
246         }
247
248         return 1;
249 }
250
251 static void __kprobes
252 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
253 {
254         struct kprobe *cur = kprobe_running();
255
256         if (!cur)
257                 return;
258
259         /* return addr restore if non-branching insn */
260         if (cur->ainsn.api.restore != 0)
261                 instruction_pointer_set(regs, cur->ainsn.api.restore);
262
263         /* restore back original saved kprobe variables and continue */
264         if (kcb->kprobe_status == KPROBE_REENTER) {
265                 restore_previous_kprobe(kcb);
266                 return;
267         }
268         /* call post handler */
269         kcb->kprobe_status = KPROBE_HIT_SSDONE;
270         if (cur->post_handler)
271                 cur->post_handler(cur, regs, 0);
272
273         reset_current_kprobe();
274 }
275
276 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
277 {
278         struct kprobe *cur = kprobe_running();
279         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
280
281         switch (kcb->kprobe_status) {
282         case KPROBE_HIT_SS:
283         case KPROBE_REENTER:
284                 /*
285                  * We are here because the instruction being single
286                  * stepped caused a page fault. We reset the current
287                  * kprobe and the ip points back to the probe address
288                  * and allow the page fault handler to continue as a
289                  * normal page fault.
290                  */
291                 instruction_pointer_set(regs, (unsigned long) cur->addr);
292                 if (!instruction_pointer(regs))
293                         BUG();
294
295                 if (kcb->kprobe_status == KPROBE_REENTER)
296                         restore_previous_kprobe(kcb);
297                 else
298                         reset_current_kprobe();
299
300                 break;
301         case KPROBE_HIT_ACTIVE:
302         case KPROBE_HIT_SSDONE:
303                 /*
304                  * We increment the nmissed count for accounting,
305                  * we can also use npre/npostfault count for accounting
306                  * these specific fault cases.
307                  */
308                 kprobes_inc_nmissed_count(cur);
309
310                 /*
311                  * We come here because instructions in the pre/post
312                  * handler caused the page_fault, this could happen
313                  * if handler tries to access user space by
314                  * copy_from_user(), get_user() etc. Let the
315                  * user-specified handler try to fix it first.
316                  */
317                 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
318                         return 1;
319
320                 /*
321                  * In case the user-specified fault handler returned
322                  * zero, try to fix up.
323                  */
324                 if (fixup_exception(regs))
325                         return 1;
326         }
327         return 0;
328 }
329
330 static void __kprobes kprobe_handler(struct pt_regs *regs)
331 {
332         struct kprobe *p, *cur_kprobe;
333         struct kprobe_ctlblk *kcb;
334         unsigned long addr = instruction_pointer(regs);
335
336         kcb = get_kprobe_ctlblk();
337         cur_kprobe = kprobe_running();
338
339         p = get_kprobe((kprobe_opcode_t *) addr);
340
341         if (p) {
342                 if (cur_kprobe) {
343                         if (reenter_kprobe(p, regs, kcb))
344                                 return;
345                 } else {
346                         /* Probe hit */
347                         set_current_kprobe(p);
348                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
349
350                         /*
351                          * If we have no pre-handler or it returned 0, we
352                          * continue with normal processing.  If we have a
353                          * pre-handler and it returned non-zero, it will
354                          * modify the execution path and no need to single
355                          * stepping. Let's just reset current kprobe and exit.
356                          */
357                         if (!p->pre_handler || !p->pre_handler(p, regs)) {
358                                 setup_singlestep(p, regs, kcb, 0);
359                         } else
360                                 reset_current_kprobe();
361                 }
362         }
363         /*
364          * The breakpoint instruction was removed right
365          * after we hit it.  Another cpu has removed
366          * either a probepoint or a debugger breakpoint
367          * at this address.  In either case, no further
368          * handling of this interrupt is appropriate.
369          * Return back to original instruction, and continue.
370          */
371 }
372
373 static int __kprobes
374 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
375 {
376         if ((kcb->ss_ctx.ss_pending)
377             && (kcb->ss_ctx.match_addr == addr)) {
378                 clear_ss_context(kcb);  /* clear pending ss */
379                 return DBG_HOOK_HANDLED;
380         }
381         /* not ours, kprobes should ignore it */
382         return DBG_HOOK_ERROR;
383 }
384
385 static int __kprobes
386 kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned int esr)
387 {
388         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
389         int retval;
390
391         /* return error if this is not our step */
392         retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
393
394         if (retval == DBG_HOOK_HANDLED) {
395                 kprobes_restore_local_irqflag(kcb, regs);
396                 post_kprobe_handler(kcb, regs);
397         }
398
399         return retval;
400 }
401
402 static struct break_hook kprobes_break_ss_hook = {
403         .imm = KPROBES_BRK_SS_IMM,
404         .fn = kprobe_breakpoint_ss_handler,
405 };
406
407 static int __kprobes
408 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
409 {
410         kprobe_handler(regs);
411         return DBG_HOOK_HANDLED;
412 }
413
414 static struct break_hook kprobes_break_hook = {
415         .imm = KPROBES_BRK_IMM,
416         .fn = kprobe_breakpoint_handler,
417 };
418
419 /*
420  * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
421  * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
422  */
423 int __init arch_populate_kprobe_blacklist(void)
424 {
425         int ret;
426
427         ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
428                                         (unsigned long)__entry_text_end);
429         if (ret)
430                 return ret;
431         ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
432                                         (unsigned long)__irqentry_text_end);
433         if (ret)
434                 return ret;
435         ret = kprobe_add_area_blacklist((unsigned long)__exception_text_start,
436                                         (unsigned long)__exception_text_end);
437         if (ret)
438                 return ret;
439         ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
440                                         (unsigned long)__idmap_text_end);
441         if (ret)
442                 return ret;
443         ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
444                                         (unsigned long)__hyp_text_end);
445         if (ret || is_kernel_in_hyp_mode())
446                 return ret;
447         ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
448                                         (unsigned long)__hyp_idmap_text_end);
449         return ret;
450 }
451
452 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
453 {
454         struct kretprobe_instance *ri = NULL;
455         struct hlist_head *head, empty_rp;
456         struct hlist_node *tmp;
457         unsigned long flags, orig_ret_address = 0;
458         unsigned long trampoline_address =
459                 (unsigned long)&kretprobe_trampoline;
460         kprobe_opcode_t *correct_ret_addr = NULL;
461
462         INIT_HLIST_HEAD(&empty_rp);
463         kretprobe_hash_lock(current, &head, &flags);
464
465         /*
466          * It is possible to have multiple instances associated with a given
467          * task either because multiple functions in the call path have
468          * return probes installed on them, and/or more than one
469          * return probe was registered for a target function.
470          *
471          * We can handle this because:
472          *     - instances are always pushed into the head of the list
473          *     - when multiple return probes are registered for the same
474          *       function, the (chronologically) first instance's ret_addr
475          *       will be the real return address, and all the rest will
476          *       point to kretprobe_trampoline.
477          */
478         hlist_for_each_entry_safe(ri, tmp, head, hlist) {
479                 if (ri->task != current)
480                         /* another task is sharing our hash bucket */
481                         continue;
482
483                 orig_ret_address = (unsigned long)ri->ret_addr;
484
485                 if (orig_ret_address != trampoline_address)
486                         /*
487                          * This is the real return address. Any other
488                          * instances associated with this task are for
489                          * other calls deeper on the call stack
490                          */
491                         break;
492         }
493
494         kretprobe_assert(ri, orig_ret_address, trampoline_address);
495
496         correct_ret_addr = ri->ret_addr;
497         hlist_for_each_entry_safe(ri, tmp, head, hlist) {
498                 if (ri->task != current)
499                         /* another task is sharing our hash bucket */
500                         continue;
501
502                 orig_ret_address = (unsigned long)ri->ret_addr;
503                 if (ri->rp && ri->rp->handler) {
504                         __this_cpu_write(current_kprobe, &ri->rp->kp);
505                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
506                         ri->ret_addr = correct_ret_addr;
507                         ri->rp->handler(ri, regs);
508                         __this_cpu_write(current_kprobe, NULL);
509                 }
510
511                 recycle_rp_inst(ri, &empty_rp);
512
513                 if (orig_ret_address != trampoline_address)
514                         /*
515                          * This is the real return address. Any other
516                          * instances associated with this task are for
517                          * other calls deeper on the call stack
518                          */
519                         break;
520         }
521
522         kretprobe_hash_unlock(current, &flags);
523
524         hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
525                 hlist_del(&ri->hlist);
526                 kfree(ri);
527         }
528         return (void *)orig_ret_address;
529 }
530
531 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
532                                       struct pt_regs *regs)
533 {
534         ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
535
536         /* replace return addr (x30) with trampoline */
537         regs->regs[30] = (long)&kretprobe_trampoline;
538 }
539
540 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
541 {
542         return 0;
543 }
544
545 int __init arch_init_kprobes(void)
546 {
547         register_kernel_break_hook(&kprobes_break_hook);
548         register_kernel_break_hook(&kprobes_break_ss_hook);
549
550         return 0;
551 }