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
arch_prepare_ss_slot(struct kprobe * p)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
arch_prepare_simulate(struct kprobe * p)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
arch_simulate_insn(struct kprobe * p,struct pt_regs * regs)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
arch_prepare_kprobe(struct kprobe * p)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
alloc_insn_page(void)112 void *alloc_insn_page(void)
113 {
114 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
115 GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
116 NUMA_NO_NODE, __builtin_return_address(0));
117 }
118
119 /* arm kprobe: install breakpoint in text */
arch_arm_kprobe(struct kprobe * p)120 void __kprobes arch_arm_kprobe(struct kprobe *p)
121 {
122 void *addr = p->addr;
123 u32 insn = BRK64_OPCODE_KPROBES;
124
125 aarch64_insn_patch_text(&addr, &insn, 1);
126 }
127
128 /* disarm kprobe: remove breakpoint from text */
arch_disarm_kprobe(struct kprobe * p)129 void __kprobes arch_disarm_kprobe(struct kprobe *p)
130 {
131 void *addr = p->addr;
132
133 aarch64_insn_patch_text(&addr, &p->opcode, 1);
134 }
135
arch_remove_kprobe(struct kprobe * p)136 void __kprobes arch_remove_kprobe(struct kprobe *p)
137 {
138 if (p->ainsn.api.insn) {
139 free_insn_slot(p->ainsn.api.insn, 0);
140 p->ainsn.api.insn = NULL;
141 }
142 }
143
save_previous_kprobe(struct kprobe_ctlblk * kcb)144 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
145 {
146 kcb->prev_kprobe.kp = kprobe_running();
147 kcb->prev_kprobe.status = kcb->kprobe_status;
148 }
149
restore_previous_kprobe(struct kprobe_ctlblk * kcb)150 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
151 {
152 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
153 kcb->kprobe_status = kcb->prev_kprobe.status;
154 }
155
set_current_kprobe(struct kprobe * p)156 static void __kprobes set_current_kprobe(struct kprobe *p)
157 {
158 __this_cpu_write(current_kprobe, p);
159 }
160
161 /*
162 * Mask all of DAIF while executing the instruction out-of-line, to keep things
163 * simple and avoid nesting exceptions. Interrupts do have to be disabled since
164 * the kprobe state is per-CPU and doesn't get migrated.
165 */
kprobes_save_local_irqflag(struct kprobe_ctlblk * kcb,struct pt_regs * regs)166 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
167 struct pt_regs *regs)
168 {
169 kcb->saved_irqflag = regs->pstate & DAIF_MASK;
170 regs->pstate |= DAIF_MASK;
171 }
172
kprobes_restore_local_irqflag(struct kprobe_ctlblk * kcb,struct pt_regs * regs)173 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
174 struct pt_regs *regs)
175 {
176 regs->pstate &= ~DAIF_MASK;
177 regs->pstate |= kcb->saved_irqflag;
178 }
179
180 static void __kprobes
set_ss_context(struct kprobe_ctlblk * kcb,unsigned long addr)181 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
182 {
183 kcb->ss_ctx.ss_pending = true;
184 kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
185 }
186
clear_ss_context(struct kprobe_ctlblk * kcb)187 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
188 {
189 kcb->ss_ctx.ss_pending = false;
190 kcb->ss_ctx.match_addr = 0;
191 }
192
setup_singlestep(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb,int reenter)193 static void __kprobes setup_singlestep(struct kprobe *p,
194 struct pt_regs *regs,
195 struct kprobe_ctlblk *kcb, int reenter)
196 {
197 unsigned long slot;
198
199 if (reenter) {
200 save_previous_kprobe(kcb);
201 set_current_kprobe(p);
202 kcb->kprobe_status = KPROBE_REENTER;
203 } else {
204 kcb->kprobe_status = KPROBE_HIT_SS;
205 }
206
207
208 if (p->ainsn.api.insn) {
209 /* prepare for single stepping */
210 slot = (unsigned long)p->ainsn.api.insn;
211
212 set_ss_context(kcb, slot); /* mark pending ss */
213 kprobes_save_local_irqflag(kcb, regs);
214 instruction_pointer_set(regs, slot);
215 } else {
216 /* insn simulation */
217 arch_simulate_insn(p, regs);
218 }
219 }
220
reenter_kprobe(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)221 static int __kprobes reenter_kprobe(struct kprobe *p,
222 struct pt_regs *regs,
223 struct kprobe_ctlblk *kcb)
224 {
225 switch (kcb->kprobe_status) {
226 case KPROBE_HIT_SSDONE:
227 case KPROBE_HIT_ACTIVE:
228 kprobes_inc_nmissed_count(p);
229 setup_singlestep(p, regs, kcb, 1);
230 break;
231 case KPROBE_HIT_SS:
232 case KPROBE_REENTER:
233 pr_warn("Unrecoverable kprobe detected.\n");
234 dump_kprobe(p);
235 BUG();
236 break;
237 default:
238 WARN_ON(1);
239 return 0;
240 }
241
242 return 1;
243 }
244
245 static void __kprobes
post_kprobe_handler(struct kprobe_ctlblk * kcb,struct pt_regs * regs)246 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
247 {
248 struct kprobe *cur = kprobe_running();
249
250 if (!cur)
251 return;
252
253 /* return addr restore if non-branching insn */
254 if (cur->ainsn.api.restore != 0)
255 instruction_pointer_set(regs, cur->ainsn.api.restore);
256
257 /* restore back original saved kprobe variables and continue */
258 if (kcb->kprobe_status == KPROBE_REENTER) {
259 restore_previous_kprobe(kcb);
260 return;
261 }
262 /* call post handler */
263 kcb->kprobe_status = KPROBE_HIT_SSDONE;
264 if (cur->post_handler)
265 cur->post_handler(cur, regs, 0);
266
267 reset_current_kprobe();
268 }
269
kprobe_fault_handler(struct pt_regs * regs,unsigned int fsr)270 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
271 {
272 struct kprobe *cur = kprobe_running();
273 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
274
275 switch (kcb->kprobe_status) {
276 case KPROBE_HIT_SS:
277 case KPROBE_REENTER:
278 /*
279 * We are here because the instruction being single
280 * stepped caused a page fault. We reset the current
281 * kprobe and the ip points back to the probe address
282 * and allow the page fault handler to continue as a
283 * normal page fault.
284 */
285 instruction_pointer_set(regs, (unsigned long) cur->addr);
286 if (!instruction_pointer(regs))
287 BUG();
288
289 if (kcb->kprobe_status == KPROBE_REENTER) {
290 restore_previous_kprobe(kcb);
291 } else {
292 kprobes_restore_local_irqflag(kcb, regs);
293 reset_current_kprobe();
294 }
295
296 break;
297 case KPROBE_HIT_ACTIVE:
298 case KPROBE_HIT_SSDONE:
299 /*
300 * We increment the nmissed count for accounting,
301 * we can also use npre/npostfault count for accounting
302 * these specific fault cases.
303 */
304 kprobes_inc_nmissed_count(cur);
305
306 /*
307 * We come here because instructions in the pre/post
308 * handler caused the page_fault, this could happen
309 * if handler tries to access user space by
310 * copy_from_user(), get_user() etc. Let the
311 * user-specified handler try to fix it first.
312 */
313 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
314 return 1;
315
316 /*
317 * In case the user-specified fault handler returned
318 * zero, try to fix up.
319 */
320 if (fixup_exception(regs))
321 return 1;
322 }
323 return 0;
324 }
325
kprobe_handler(struct pt_regs * regs)326 static void __kprobes kprobe_handler(struct pt_regs *regs)
327 {
328 struct kprobe *p, *cur_kprobe;
329 struct kprobe_ctlblk *kcb;
330 unsigned long addr = instruction_pointer(regs);
331
332 kcb = get_kprobe_ctlblk();
333 cur_kprobe = kprobe_running();
334
335 p = get_kprobe((kprobe_opcode_t *) addr);
336
337 if (p) {
338 if (cur_kprobe) {
339 if (reenter_kprobe(p, regs, kcb))
340 return;
341 } else {
342 /* Probe hit */
343 set_current_kprobe(p);
344 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
345
346 /*
347 * If we have no pre-handler or it returned 0, we
348 * continue with normal processing. If we have a
349 * pre-handler and it returned non-zero, it will
350 * modify the execution path and no need to single
351 * stepping. Let's just reset current kprobe and exit.
352 */
353 if (!p->pre_handler || !p->pre_handler(p, regs)) {
354 setup_singlestep(p, regs, kcb, 0);
355 } else
356 reset_current_kprobe();
357 }
358 }
359 /*
360 * The breakpoint instruction was removed right
361 * after we hit it. Another cpu has removed
362 * either a probepoint or a debugger breakpoint
363 * at this address. In either case, no further
364 * handling of this interrupt is appropriate.
365 * Return back to original instruction, and continue.
366 */
367 }
368
369 static int __kprobes
kprobe_ss_hit(struct kprobe_ctlblk * kcb,unsigned long addr)370 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
371 {
372 if ((kcb->ss_ctx.ss_pending)
373 && (kcb->ss_ctx.match_addr == addr)) {
374 clear_ss_context(kcb); /* clear pending ss */
375 return DBG_HOOK_HANDLED;
376 }
377 /* not ours, kprobes should ignore it */
378 return DBG_HOOK_ERROR;
379 }
380
381 static int __kprobes
kprobe_breakpoint_ss_handler(struct pt_regs * regs,unsigned int esr)382 kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned int esr)
383 {
384 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
385 int retval;
386
387 /* return error if this is not our step */
388 retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
389
390 if (retval == DBG_HOOK_HANDLED) {
391 kprobes_restore_local_irqflag(kcb, regs);
392 post_kprobe_handler(kcb, regs);
393 }
394
395 return retval;
396 }
397
398 static struct break_hook kprobes_break_ss_hook = {
399 .imm = KPROBES_BRK_SS_IMM,
400 .fn = kprobe_breakpoint_ss_handler,
401 };
402
403 static int __kprobes
kprobe_breakpoint_handler(struct pt_regs * regs,unsigned int esr)404 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
405 {
406 kprobe_handler(regs);
407 return DBG_HOOK_HANDLED;
408 }
409
410 static struct break_hook kprobes_break_hook = {
411 .imm = KPROBES_BRK_IMM,
412 .fn = kprobe_breakpoint_handler,
413 };
414
415 /*
416 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
417 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
418 */
arch_populate_kprobe_blacklist(void)419 int __init arch_populate_kprobe_blacklist(void)
420 {
421 int ret;
422
423 ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
424 (unsigned long)__entry_text_end);
425 if (ret)
426 return ret;
427 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
428 (unsigned long)__irqentry_text_end);
429 if (ret)
430 return ret;
431 ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
432 (unsigned long)__idmap_text_end);
433 if (ret)
434 return ret;
435 ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
436 (unsigned long)__hyp_text_end);
437 if (ret || is_kernel_in_hyp_mode())
438 return ret;
439 ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
440 (unsigned long)__hyp_idmap_text_end);
441 return ret;
442 }
443
trampoline_probe_handler(struct pt_regs * regs)444 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
445 {
446 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
447 (void *)kernel_stack_pointer(regs));
448 }
449
arch_prepare_kretprobe(struct kretprobe_instance * ri,struct pt_regs * regs)450 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
451 struct pt_regs *regs)
452 {
453 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
454 ri->fp = (void *)kernel_stack_pointer(regs);
455
456 /* replace return addr (x30) with trampoline */
457 regs->regs[30] = (long)&kretprobe_trampoline;
458 }
459
arch_trampoline_kprobe(struct kprobe * p)460 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
461 {
462 return 0;
463 }
464
arch_init_kprobes(void)465 int __init arch_init_kprobes(void)
466 {
467 register_kernel_break_hook(&kprobes_break_hook);
468 register_kernel_break_hook(&kprobes_break_ss_hook);
469
470 return 0;
471 }
472