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