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
11 #define pr_fmt(fmt) "kprobes: " fmt
12
13 #include <linux/execmem.h>
14 #include <linux/extable.h>
15 #include <linux/kasan.h>
16 #include <linux/kernel.h>
17 #include <linux/kprobes.h>
18 #include <linux/sched/debug.h>
19 #include <linux/set_memory.h>
20 #include <linux/slab.h>
21 #include <linux/stop_machine.h>
22 #include <linux/stringify.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25
26 #include <asm/cacheflush.h>
27 #include <asm/daifflags.h>
28 #include <asm/debug-monitors.h>
29 #include <asm/insn.h>
30 #include <asm/irq.h>
31 #include <asm/text-patching.h>
32 #include <asm/ptrace.h>
33 #include <asm/sections.h>
34 #include <asm/system_misc.h>
35 #include <asm/traps.h>
36
37 #include "decode-insn.h"
38
39 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
40 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
41
42 static void __kprobes
43 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
44
alloc_insn_page(void)45 void *alloc_insn_page(void)
46 {
47 void *addr;
48
49 addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
50 if (!addr)
51 return NULL;
52 if (set_memory_rox((unsigned long)addr, 1)) {
53 execmem_free(addr);
54 return NULL;
55 }
56 return addr;
57 }
58
arch_prepare_ss_slot(struct kprobe * p)59 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
60 {
61 kprobe_opcode_t *addr = p->ainsn.xol_insn;
62
63 /*
64 * Prepare insn slot, Mark Rutland points out it depends on a coupe of
65 * subtleties:
66 *
67 * - That the I-cache maintenance for these instructions is complete
68 * *before* the kprobe BRK is written (and aarch64_insn_patch_text_nosync()
69 * ensures this, but just omits causing a Context-Synchronization-Event
70 * on all CPUS).
71 *
72 * - That the kprobe BRK results in an exception (and consequently a
73 * Context-Synchronoization-Event), which ensures that the CPU will
74 * fetch thesingle-step slot instructions *after* this, ensuring that
75 * the new instructions are used
76 *
77 * It supposes to place ISB after patching to guarantee I-cache maintenance
78 * is observed on all CPUS, however, single-step slot is installed in
79 * the BRK exception handler, so it is unnecessary to generate
80 * Contex-Synchronization-Event via ISB again.
81 */
82 aarch64_insn_patch_text_nosync(addr, le32_to_cpu(p->opcode));
83 aarch64_insn_patch_text_nosync(addr + 1, BRK64_OPCODE_KPROBES_SS);
84
85 /*
86 * Needs restoring of return address after stepping xol.
87 */
88 p->ainsn.xol_restore = (unsigned long) p->addr +
89 sizeof(kprobe_opcode_t);
90 }
91
arch_prepare_simulate(struct kprobe * p)92 static void __kprobes arch_prepare_simulate(struct kprobe *p)
93 {
94 /* This instructions is not executed xol. No need to adjust the PC */
95 p->ainsn.xol_restore = 0;
96 }
97
arch_simulate_insn(struct kprobe * p,struct pt_regs * regs)98 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
99 {
100 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
101
102 if (p->ainsn.api.handler)
103 p->ainsn.api.handler(le32_to_cpu(p->opcode), (long)p->addr, regs);
104
105 /* single step simulated, now go for post processing */
106 post_kprobe_handler(p, kcb, regs);
107 }
108
arch_prepare_kprobe(struct kprobe * p)109 int __kprobes arch_prepare_kprobe(struct kprobe *p)
110 {
111 unsigned long probe_addr = (unsigned long)p->addr;
112
113 if (probe_addr & 0x3)
114 return -EINVAL;
115
116 /* copy instruction */
117 p->opcode = *p->addr;
118
119 if (search_exception_tables(probe_addr))
120 return -EINVAL;
121
122 /* decode instruction */
123 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
124 case INSN_REJECTED: /* insn not supported */
125 return -EINVAL;
126
127 case INSN_GOOD_NO_SLOT: /* insn need simulation */
128 p->ainsn.xol_insn = NULL;
129 break;
130
131 case INSN_GOOD: /* instruction uses slot */
132 p->ainsn.xol_insn = get_insn_slot();
133 if (!p->ainsn.xol_insn)
134 return -ENOMEM;
135 break;
136 }
137
138 /* prepare the instruction */
139 if (p->ainsn.xol_insn)
140 arch_prepare_ss_slot(p);
141 else
142 arch_prepare_simulate(p);
143
144 return 0;
145 }
146
147 /* arm kprobe: install breakpoint in text */
arch_arm_kprobe(struct kprobe * p)148 void __kprobes arch_arm_kprobe(struct kprobe *p)
149 {
150 void *addr = p->addr;
151 u32 insn = BRK64_OPCODE_KPROBES;
152
153 aarch64_insn_patch_text(&addr, &insn, 1);
154 }
155
156 /* disarm kprobe: remove breakpoint from text */
arch_disarm_kprobe(struct kprobe * p)157 void __kprobes arch_disarm_kprobe(struct kprobe *p)
158 {
159 void *addr = p->addr;
160 u32 insn = le32_to_cpu(p->opcode);
161
162 aarch64_insn_patch_text(&addr, &insn, 1);
163 }
164
arch_remove_kprobe(struct kprobe * p)165 void __kprobes arch_remove_kprobe(struct kprobe *p)
166 {
167 if (p->ainsn.xol_insn) {
168 free_insn_slot(p->ainsn.xol_insn, 0);
169 p->ainsn.xol_insn = NULL;
170 }
171 }
172
save_previous_kprobe(struct kprobe_ctlblk * kcb)173 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
174 {
175 kcb->prev_kprobe.kp = kprobe_running();
176 kcb->prev_kprobe.status = kcb->kprobe_status;
177
178 /*
179 * Save the outer kprobe's original DAIF flags before the nested
180 * kprobe calls kprobes_save_local_irqflag() and overwrites
181 * kcb->saved_irqflag. Without this, the outer kprobe will restore
182 * the wrong DAIF state and leave interrupts permanently masked.
183 */
184 kcb->prev_kprobe.saved_irqflag = kcb->saved_irqflag;
185 }
186
restore_previous_kprobe(struct kprobe_ctlblk * kcb)187 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
188 {
189 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
190 kcb->kprobe_status = kcb->prev_kprobe.status;
191
192 /*
193 * Restore the outer kprobe's saved_irqflag so that when its
194 * single-step completes, kprobes_restore_local_irqflag() uses
195 * the correct original DAIF value.
196 */
197 kcb->saved_irqflag = kcb->prev_kprobe.saved_irqflag;
198 }
199
set_current_kprobe(struct kprobe * p)200 static void __kprobes set_current_kprobe(struct kprobe *p)
201 {
202 __this_cpu_write(current_kprobe, p);
203 }
204
205 /*
206 * Mask all of DAIF while executing the instruction out-of-line, to keep things
207 * simple and avoid nesting exceptions. Interrupts do have to be disabled since
208 * the kprobe state is per-CPU and doesn't get migrated.
209 */
kprobes_save_local_irqflag(struct kprobe_ctlblk * kcb,struct pt_regs * regs)210 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
211 struct pt_regs *regs)
212 {
213 kcb->saved_irqflag = regs->pstate & DAIF_MASK;
214 regs->pstate |= DAIF_MASK;
215 }
216
kprobes_restore_local_irqflag(struct kprobe_ctlblk * kcb,struct pt_regs * regs)217 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
218 struct pt_regs *regs)
219 {
220 regs->pstate &= ~DAIF_MASK;
221 regs->pstate |= kcb->saved_irqflag;
222 }
223
setup_singlestep(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb,int reenter)224 static void __kprobes setup_singlestep(struct kprobe *p,
225 struct pt_regs *regs,
226 struct kprobe_ctlblk *kcb, int reenter)
227 {
228 unsigned long slot;
229
230 if (reenter) {
231 save_previous_kprobe(kcb);
232 set_current_kprobe(p);
233 kcb->kprobe_status = KPROBE_REENTER;
234 } else {
235 kcb->kprobe_status = KPROBE_HIT_SS;
236 }
237
238
239 if (p->ainsn.xol_insn) {
240 /* prepare for single stepping */
241 slot = (unsigned long)p->ainsn.xol_insn;
242
243 kprobes_save_local_irqflag(kcb, regs);
244 instruction_pointer_set(regs, slot);
245 } else {
246 /* insn simulation */
247 arch_simulate_insn(p, regs);
248 }
249 }
250
reenter_kprobe(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)251 static int __kprobes reenter_kprobe(struct kprobe *p,
252 struct pt_regs *regs,
253 struct kprobe_ctlblk *kcb)
254 {
255 switch (kcb->kprobe_status) {
256 case KPROBE_HIT_SSDONE:
257 case KPROBE_HIT_ACTIVE:
258 case KPROBE_HIT_SS:
259 /*
260 * A probe can be hit while another kprobe is preparing or
261 * executing its XOL single-step instruction. This is still a
262 * recoverable one-level reentry, so handle it in the same way as
263 * reentry from KPROBE_HIT_ACTIVE or KPROBE_HIT_SSDONE.
264 */
265 kprobes_inc_nmissed_count(p);
266 setup_singlestep(p, regs, kcb, 1);
267 break;
268 case KPROBE_REENTER:
269 pr_warn("Failed to recover from reentered kprobes.\n");
270 dump_kprobe(p);
271 BUG();
272 break;
273 default:
274 WARN_ON(1);
275 return 0;
276 }
277
278 return 1;
279 }
280
281 static void __kprobes
post_kprobe_handler(struct kprobe * cur,struct kprobe_ctlblk * kcb,struct pt_regs * regs)282 post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
283 {
284 /* return addr restore if non-branching insn */
285 if (cur->ainsn.xol_restore != 0)
286 instruction_pointer_set(regs, cur->ainsn.xol_restore);
287
288 /* restore back original saved kprobe variables and continue */
289 if (kcb->kprobe_status == KPROBE_REENTER) {
290 restore_previous_kprobe(kcb);
291 return;
292 }
293 /* call post handler */
294 kcb->kprobe_status = KPROBE_HIT_SSDONE;
295 if (cur->post_handler)
296 cur->post_handler(cur, regs, 0);
297
298 reset_current_kprobe();
299 }
300
kprobe_fault_handler(struct pt_regs * regs,unsigned int fsr)301 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
302 {
303 struct kprobe *cur = kprobe_running();
304 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
305
306 /*
307 * Simulated kprobes execute in the debug trap context and have no
308 * XOL slot. Any page fault taken while a simulated kprobe is in
309 * progress cannot have been caused by kprobe single-stepping and
310 * must be left alone for the normal page fault handler, including
311 * fixup_exception.
312 */
313 if (cur && !cur->ainsn.xol_insn)
314 return 0;
315
316 switch (kcb->kprobe_status) {
317 case KPROBE_HIT_SS:
318 case KPROBE_REENTER:
319 /*
320 * A page fault taken while in KPROBE_HIT_SS or
321 * KPROBE_REENTER state is only attributable to kprobe
322 * single-stepping if the faulting PC points to the
323 * current kprobe's XOL instruction. If the fault occurred
324 * elsewhere (e.g. in perf or tracing code invoked from the
325 * debug exception path), leave it for the normal page fault
326 * handler to process.
327 */
328 if (instruction_pointer(regs) != (unsigned long)cur->ainsn.xol_insn)
329 break;
330
331 /*
332 * We are here because the instruction being single
333 * stepped caused a page fault. We reset the current
334 * kprobe and the ip points back to the probe address
335 * and allow the page fault handler to continue as a
336 * normal page fault.
337 */
338 instruction_pointer_set(regs, (unsigned long) cur->addr);
339 BUG_ON(!instruction_pointer(regs));
340
341 if (kcb->kprobe_status == KPROBE_REENTER) {
342 restore_previous_kprobe(kcb);
343 } else {
344 kprobes_restore_local_irqflag(kcb, regs);
345 reset_current_kprobe();
346 }
347
348 break;
349 }
350 return 0;
351 }
352
353 int __kprobes
kprobe_brk_handler(struct pt_regs * regs,unsigned long esr)354 kprobe_brk_handler(struct pt_regs *regs, unsigned long esr)
355 {
356 struct kprobe *p, *cur_kprobe;
357 struct kprobe_ctlblk *kcb;
358 unsigned long addr = instruction_pointer(regs);
359
360 kcb = get_kprobe_ctlblk();
361 cur_kprobe = kprobe_running();
362
363 p = get_kprobe((kprobe_opcode_t *) addr);
364 if (WARN_ON_ONCE(!p)) {
365 /*
366 * Something went wrong. This BRK used an immediate reserved
367 * for kprobes, but we couldn't find any corresponding probe.
368 */
369 return DBG_HOOK_ERROR;
370 }
371
372 if (cur_kprobe) {
373 /* Hit a kprobe inside another kprobe */
374 if (!reenter_kprobe(p, regs, kcb))
375 return DBG_HOOK_ERROR;
376 } else {
377 /* Probe hit */
378 set_current_kprobe(p);
379 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
380
381 /*
382 * If we have no pre-handler or it returned 0, we
383 * continue with normal processing. If we have a
384 * pre-handler and it returned non-zero, it will
385 * modify the execution path and not need to single-step
386 * Let's just reset current kprobe and exit.
387 */
388 if (!p->pre_handler || !p->pre_handler(p, regs))
389 setup_singlestep(p, regs, kcb, 0);
390 else
391 reset_current_kprobe();
392 }
393
394 return DBG_HOOK_HANDLED;
395 }
396
397 int __kprobes
kprobe_ss_brk_handler(struct pt_regs * regs,unsigned long esr)398 kprobe_ss_brk_handler(struct pt_regs *regs, unsigned long esr)
399 {
400 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
401 unsigned long addr = instruction_pointer(regs);
402 struct kprobe *cur = kprobe_running();
403
404 if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
405 ((unsigned long)&cur->ainsn.xol_insn[1] == addr)) {
406 kprobes_restore_local_irqflag(kcb, regs);
407 post_kprobe_handler(cur, kcb, regs);
408
409 return DBG_HOOK_HANDLED;
410 }
411
412 /* not ours, kprobes should ignore it */
413 return DBG_HOOK_ERROR;
414 }
415
416 int __kprobes
kretprobe_brk_handler(struct pt_regs * regs,unsigned long esr)417 kretprobe_brk_handler(struct pt_regs *regs, unsigned long esr)
418 {
419 if (regs->pc != (unsigned long)__kretprobe_trampoline)
420 return DBG_HOOK_ERROR;
421
422 regs->pc = kretprobe_trampoline_handler(regs, (void *)regs->regs[29]);
423 return DBG_HOOK_HANDLED;
424 }
425
426 /*
427 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
428 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
429 */
arch_populate_kprobe_blacklist(void)430 int __init arch_populate_kprobe_blacklist(void)
431 {
432 int ret;
433
434 ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
435 (unsigned long)__entry_text_end);
436 if (ret)
437 return ret;
438 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
439 (unsigned long)__irqentry_text_end);
440 if (ret)
441 return ret;
442 ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
443 (unsigned long)__hyp_text_end);
444 if (ret || is_kernel_in_hyp_mode())
445 return ret;
446 ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
447 (unsigned long)__hyp_idmap_text_end);
448 return ret;
449 }
450
arch_prepare_kretprobe(struct kretprobe_instance * ri,struct pt_regs * regs)451 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
452 struct pt_regs *regs)
453 {
454 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
455 ri->fp = (void *)regs->regs[29];
456
457 /* replace return addr (x30) with trampoline */
458 regs->regs[30] = (long)&__kretprobe_trampoline;
459 }
460
arch_trampoline_kprobe(struct kprobe * p)461 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
462 {
463 return 0;
464 }
465
arch_init_kprobes(void)466 int __init arch_init_kprobes(void)
467 {
468 return 0;
469 }
470