xref: /openbmc/linux/arch/arm64/kernel/probes/kprobes.c (revision 293d5b43)
1 /*
2  * arch/arm64/kernel/probes/kprobes.c
3  *
4  * Kprobes support for ARM64
5  *
6  * Copyright (C) 2013 Linaro Limited.
7  * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  */
19 #include <linux/kasan.h>
20 #include <linux/kernel.h>
21 #include <linux/kprobes.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/stop_machine.h>
25 #include <linux/stringify.h>
26 #include <asm/traps.h>
27 #include <asm/ptrace.h>
28 #include <asm/cacheflush.h>
29 #include <asm/debug-monitors.h>
30 #include <asm/system_misc.h>
31 #include <asm/insn.h>
32 #include <asm/uaccess.h>
33 #include <asm/irq.h>
34 #include <asm-generic/sections.h>
35 
36 #include "decode-insn.h"
37 
38 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
39 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
40 
41 static void __kprobes
42 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
43 
44 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
45 {
46 	/* prepare insn slot */
47 	p->ainsn.insn[0] = cpu_to_le32(p->opcode);
48 
49 	flush_icache_range((uintptr_t) (p->ainsn.insn),
50 			   (uintptr_t) (p->ainsn.insn) +
51 			   MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
52 
53 	/*
54 	 * Needs restoring of return address after stepping xol.
55 	 */
56 	p->ainsn.restore = (unsigned long) p->addr +
57 	  sizeof(kprobe_opcode_t);
58 }
59 
60 static void __kprobes arch_prepare_simulate(struct kprobe *p)
61 {
62 	/* This instructions is not executed xol. No need to adjust the PC */
63 	p->ainsn.restore = 0;
64 }
65 
66 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
67 {
68 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
69 
70 	if (p->ainsn.handler)
71 		p->ainsn.handler((u32)p->opcode, (long)p->addr, regs);
72 
73 	/* single step simulated, now go for post processing */
74 	post_kprobe_handler(kcb, regs);
75 }
76 
77 int __kprobes arch_prepare_kprobe(struct kprobe *p)
78 {
79 	unsigned long probe_addr = (unsigned long)p->addr;
80 	extern char __start_rodata[];
81 	extern char __end_rodata[];
82 
83 	if (probe_addr & 0x3)
84 		return -EINVAL;
85 
86 	/* copy instruction */
87 	p->opcode = le32_to_cpu(*p->addr);
88 
89 	if (in_exception_text(probe_addr))
90 		return -EINVAL;
91 	if (probe_addr >= (unsigned long) __start_rodata &&
92 	    probe_addr <= (unsigned long) __end_rodata)
93 		return -EINVAL;
94 
95 	/* decode instruction */
96 	switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
97 	case INSN_REJECTED:	/* insn not supported */
98 		return -EINVAL;
99 
100 	case INSN_GOOD_NO_SLOT:	/* insn need simulation */
101 		p->ainsn.insn = NULL;
102 		break;
103 
104 	case INSN_GOOD:	/* instruction uses slot */
105 		p->ainsn.insn = get_insn_slot();
106 		if (!p->ainsn.insn)
107 			return -ENOMEM;
108 		break;
109 	};
110 
111 	/* prepare the instruction */
112 	if (p->ainsn.insn)
113 		arch_prepare_ss_slot(p);
114 	else
115 		arch_prepare_simulate(p);
116 
117 	return 0;
118 }
119 
120 static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
121 {
122 	void *addrs[1];
123 	u32 insns[1];
124 
125 	addrs[0] = (void *)addr;
126 	insns[0] = (u32)opcode;
127 
128 	return aarch64_insn_patch_text(addrs, insns, 1);
129 }
130 
131 /* arm kprobe: install breakpoint in text */
132 void __kprobes arch_arm_kprobe(struct kprobe *p)
133 {
134 	patch_text(p->addr, BRK64_OPCODE_KPROBES);
135 }
136 
137 /* disarm kprobe: remove breakpoint from text */
138 void __kprobes arch_disarm_kprobe(struct kprobe *p)
139 {
140 	patch_text(p->addr, p->opcode);
141 }
142 
143 void __kprobes arch_remove_kprobe(struct kprobe *p)
144 {
145 	if (p->ainsn.insn) {
146 		free_insn_slot(p->ainsn.insn, 0);
147 		p->ainsn.insn = NULL;
148 	}
149 }
150 
151 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
152 {
153 	kcb->prev_kprobe.kp = kprobe_running();
154 	kcb->prev_kprobe.status = kcb->kprobe_status;
155 }
156 
157 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
158 {
159 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
160 	kcb->kprobe_status = kcb->prev_kprobe.status;
161 }
162 
163 static void __kprobes set_current_kprobe(struct kprobe *p)
164 {
165 	__this_cpu_write(current_kprobe, p);
166 }
167 
168 /*
169  * The D-flag (Debug mask) is set (masked) upon debug exception entry.
170  * Kprobes needs to clear (unmask) D-flag -ONLY- in case of recursive
171  * probe i.e. when probe hit from kprobe handler context upon
172  * executing the pre/post handlers. In this case we return with
173  * D-flag clear so that single-stepping can be carried-out.
174  *
175  * Leave D-flag set in all other cases.
176  */
177 static void __kprobes
178 spsr_set_debug_flag(struct pt_regs *regs, int mask)
179 {
180 	unsigned long spsr = regs->pstate;
181 
182 	if (mask)
183 		spsr |= PSR_D_BIT;
184 	else
185 		spsr &= ~PSR_D_BIT;
186 
187 	regs->pstate = spsr;
188 }
189 
190 /*
191  * Interrupts need to be disabled before single-step mode is set, and not
192  * reenabled until after single-step mode ends.
193  * Without disabling interrupt on local CPU, there is a chance of
194  * interrupt occurrence in the period of exception return and  start of
195  * out-of-line single-step, that result in wrongly single stepping
196  * into the interrupt handler.
197  */
198 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
199 						struct pt_regs *regs)
200 {
201 	kcb->saved_irqflag = regs->pstate;
202 	regs->pstate |= PSR_I_BIT;
203 }
204 
205 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
206 						struct pt_regs *regs)
207 {
208 	if (kcb->saved_irqflag & PSR_I_BIT)
209 		regs->pstate |= PSR_I_BIT;
210 	else
211 		regs->pstate &= ~PSR_I_BIT;
212 }
213 
214 static void __kprobes
215 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
216 {
217 	kcb->ss_ctx.ss_pending = true;
218 	kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
219 }
220 
221 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
222 {
223 	kcb->ss_ctx.ss_pending = false;
224 	kcb->ss_ctx.match_addr = 0;
225 }
226 
227 static void __kprobes setup_singlestep(struct kprobe *p,
228 				       struct pt_regs *regs,
229 				       struct kprobe_ctlblk *kcb, int reenter)
230 {
231 	unsigned long slot;
232 
233 	if (reenter) {
234 		save_previous_kprobe(kcb);
235 		set_current_kprobe(p);
236 		kcb->kprobe_status = KPROBE_REENTER;
237 	} else {
238 		kcb->kprobe_status = KPROBE_HIT_SS;
239 	}
240 
241 
242 	if (p->ainsn.insn) {
243 		/* prepare for single stepping */
244 		slot = (unsigned long)p->ainsn.insn;
245 
246 		set_ss_context(kcb, slot);	/* mark pending ss */
247 
248 		if (kcb->kprobe_status == KPROBE_REENTER)
249 			spsr_set_debug_flag(regs, 0);
250 		else
251 			WARN_ON(regs->pstate & PSR_D_BIT);
252 
253 		/* IRQs and single stepping do not mix well. */
254 		kprobes_save_local_irqflag(kcb, regs);
255 		kernel_enable_single_step(regs);
256 		instruction_pointer_set(regs, slot);
257 	} else {
258 		/* insn simulation */
259 		arch_simulate_insn(p, regs);
260 	}
261 }
262 
263 static int __kprobes reenter_kprobe(struct kprobe *p,
264 				    struct pt_regs *regs,
265 				    struct kprobe_ctlblk *kcb)
266 {
267 	switch (kcb->kprobe_status) {
268 	case KPROBE_HIT_SSDONE:
269 	case KPROBE_HIT_ACTIVE:
270 		kprobes_inc_nmissed_count(p);
271 		setup_singlestep(p, regs, kcb, 1);
272 		break;
273 	case KPROBE_HIT_SS:
274 	case KPROBE_REENTER:
275 		pr_warn("Unrecoverable kprobe detected at %p.\n", p->addr);
276 		dump_kprobe(p);
277 		BUG();
278 		break;
279 	default:
280 		WARN_ON(1);
281 		return 0;
282 	}
283 
284 	return 1;
285 }
286 
287 static void __kprobes
288 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
289 {
290 	struct kprobe *cur = kprobe_running();
291 
292 	if (!cur)
293 		return;
294 
295 	/* return addr restore if non-branching insn */
296 	if (cur->ainsn.restore != 0)
297 		instruction_pointer_set(regs, cur->ainsn.restore);
298 
299 	/* restore back original saved kprobe variables and continue */
300 	if (kcb->kprobe_status == KPROBE_REENTER) {
301 		restore_previous_kprobe(kcb);
302 		return;
303 	}
304 	/* call post handler */
305 	kcb->kprobe_status = KPROBE_HIT_SSDONE;
306 	if (cur->post_handler)	{
307 		/* post_handler can hit breakpoint and single step
308 		 * again, so we enable D-flag for recursive exception.
309 		 */
310 		cur->post_handler(cur, regs, 0);
311 	}
312 
313 	reset_current_kprobe();
314 }
315 
316 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
317 {
318 	struct kprobe *cur = kprobe_running();
319 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
320 
321 	switch (kcb->kprobe_status) {
322 	case KPROBE_HIT_SS:
323 	case KPROBE_REENTER:
324 		/*
325 		 * We are here because the instruction being single
326 		 * stepped caused a page fault. We reset the current
327 		 * kprobe and the ip points back to the probe address
328 		 * and allow the page fault handler to continue as a
329 		 * normal page fault.
330 		 */
331 		instruction_pointer_set(regs, (unsigned long) cur->addr);
332 		if (!instruction_pointer(regs))
333 			BUG();
334 
335 		kernel_disable_single_step();
336 		if (kcb->kprobe_status == KPROBE_REENTER)
337 			spsr_set_debug_flag(regs, 1);
338 
339 		if (kcb->kprobe_status == KPROBE_REENTER)
340 			restore_previous_kprobe(kcb);
341 		else
342 			reset_current_kprobe();
343 
344 		break;
345 	case KPROBE_HIT_ACTIVE:
346 	case KPROBE_HIT_SSDONE:
347 		/*
348 		 * We increment the nmissed count for accounting,
349 		 * we can also use npre/npostfault count for accounting
350 		 * these specific fault cases.
351 		 */
352 		kprobes_inc_nmissed_count(cur);
353 
354 		/*
355 		 * We come here because instructions in the pre/post
356 		 * handler caused the page_fault, this could happen
357 		 * if handler tries to access user space by
358 		 * copy_from_user(), get_user() etc. Let the
359 		 * user-specified handler try to fix it first.
360 		 */
361 		if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
362 			return 1;
363 
364 		/*
365 		 * In case the user-specified fault handler returned
366 		 * zero, try to fix up.
367 		 */
368 		if (fixup_exception(regs))
369 			return 1;
370 	}
371 	return 0;
372 }
373 
374 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
375 				       unsigned long val, void *data)
376 {
377 	return NOTIFY_DONE;
378 }
379 
380 static void __kprobes kprobe_handler(struct pt_regs *regs)
381 {
382 	struct kprobe *p, *cur_kprobe;
383 	struct kprobe_ctlblk *kcb;
384 	unsigned long addr = instruction_pointer(regs);
385 
386 	kcb = get_kprobe_ctlblk();
387 	cur_kprobe = kprobe_running();
388 
389 	p = get_kprobe((kprobe_opcode_t *) addr);
390 
391 	if (p) {
392 		if (cur_kprobe) {
393 			if (reenter_kprobe(p, regs, kcb))
394 				return;
395 		} else {
396 			/* Probe hit */
397 			set_current_kprobe(p);
398 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
399 
400 			/*
401 			 * If we have no pre-handler or it returned 0, we
402 			 * continue with normal processing.  If we have a
403 			 * pre-handler and it returned non-zero, it prepped
404 			 * for calling the break_handler below on re-entry,
405 			 * so get out doing nothing more here.
406 			 *
407 			 * pre_handler can hit a breakpoint and can step thru
408 			 * before return, keep PSTATE D-flag enabled until
409 			 * pre_handler return back.
410 			 */
411 			if (!p->pre_handler || !p->pre_handler(p, regs)) {
412 				setup_singlestep(p, regs, kcb, 0);
413 				return;
414 			}
415 		}
416 	} else if ((le32_to_cpu(*(kprobe_opcode_t *) addr) ==
417 	    BRK64_OPCODE_KPROBES) && cur_kprobe) {
418 		/* We probably hit a jprobe.  Call its break handler. */
419 		if (cur_kprobe->break_handler  &&
420 		     cur_kprobe->break_handler(cur_kprobe, regs)) {
421 			setup_singlestep(cur_kprobe, regs, kcb, 0);
422 			return;
423 		}
424 	}
425 	/*
426 	 * The breakpoint instruction was removed right
427 	 * after we hit it.  Another cpu has removed
428 	 * either a probepoint or a debugger breakpoint
429 	 * at this address.  In either case, no further
430 	 * handling of this interrupt is appropriate.
431 	 * Return back to original instruction, and continue.
432 	 */
433 }
434 
435 static int __kprobes
436 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
437 {
438 	if ((kcb->ss_ctx.ss_pending)
439 	    && (kcb->ss_ctx.match_addr == addr)) {
440 		clear_ss_context(kcb);	/* clear pending ss */
441 		return DBG_HOOK_HANDLED;
442 	}
443 	/* not ours, kprobes should ignore it */
444 	return DBG_HOOK_ERROR;
445 }
446 
447 int __kprobes
448 kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr)
449 {
450 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
451 	int retval;
452 
453 	/* return error if this is not our step */
454 	retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
455 
456 	if (retval == DBG_HOOK_HANDLED) {
457 		kprobes_restore_local_irqflag(kcb, regs);
458 		kernel_disable_single_step();
459 
460 		if (kcb->kprobe_status == KPROBE_REENTER)
461 			spsr_set_debug_flag(regs, 1);
462 
463 		post_kprobe_handler(kcb, regs);
464 	}
465 
466 	return retval;
467 }
468 
469 int __kprobes
470 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
471 {
472 	kprobe_handler(regs);
473 	return DBG_HOOK_HANDLED;
474 }
475 
476 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
477 {
478 	struct jprobe *jp = container_of(p, struct jprobe, kp);
479 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
480 
481 	kcb->jprobe_saved_regs = *regs;
482 	/*
483 	 * Since we can't be sure where in the stack frame "stacked"
484 	 * pass-by-value arguments are stored we just don't try to
485 	 * duplicate any of the stack. Do not use jprobes on functions that
486 	 * use more than 64 bytes (after padding each to an 8 byte boundary)
487 	 * of arguments, or pass individual arguments larger than 16 bytes.
488 	 */
489 
490 	instruction_pointer_set(regs, (unsigned long) jp->entry);
491 	preempt_disable();
492 	pause_graph_tracing();
493 	return 1;
494 }
495 
496 void __kprobes jprobe_return(void)
497 {
498 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
499 
500 	/*
501 	 * Jprobe handler return by entering break exception,
502 	 * encoded same as kprobe, but with following conditions
503 	 * -a special PC to identify it from the other kprobes.
504 	 * -restore stack addr to original saved pt_regs
505 	 */
506 	asm volatile("				mov sp, %0	\n"
507 		     "jprobe_return_break:	brk %1		\n"
508 		     :
509 		     : "r" (kcb->jprobe_saved_regs.sp),
510 		       "I" (BRK64_ESR_KPROBES)
511 		     : "memory");
512 
513 	unreachable();
514 }
515 
516 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
517 {
518 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
519 	long stack_addr = kcb->jprobe_saved_regs.sp;
520 	long orig_sp = kernel_stack_pointer(regs);
521 	struct jprobe *jp = container_of(p, struct jprobe, kp);
522 	extern const char jprobe_return_break[];
523 
524 	if (instruction_pointer(regs) != (u64) jprobe_return_break)
525 		return 0;
526 
527 	if (orig_sp != stack_addr) {
528 		struct pt_regs *saved_regs =
529 		    (struct pt_regs *)kcb->jprobe_saved_regs.sp;
530 		pr_err("current sp %lx does not match saved sp %lx\n",
531 		       orig_sp, stack_addr);
532 		pr_err("Saved registers for jprobe %p\n", jp);
533 		show_regs(saved_regs);
534 		pr_err("Current registers\n");
535 		show_regs(regs);
536 		BUG();
537 	}
538 	unpause_graph_tracing();
539 	*regs = kcb->jprobe_saved_regs;
540 	preempt_enable_no_resched();
541 	return 1;
542 }
543 
544 bool arch_within_kprobe_blacklist(unsigned long addr)
545 {
546 	extern char __idmap_text_start[], __idmap_text_end[];
547 	extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
548 
549 	if ((addr >= (unsigned long)__kprobes_text_start &&
550 	    addr < (unsigned long)__kprobes_text_end) ||
551 	    (addr >= (unsigned long)__entry_text_start &&
552 	    addr < (unsigned long)__entry_text_end) ||
553 	    (addr >= (unsigned long)__idmap_text_start &&
554 	    addr < (unsigned long)__idmap_text_end) ||
555 	    !!search_exception_tables(addr))
556 		return true;
557 
558 	if (!is_kernel_in_hyp_mode()) {
559 		if ((addr >= (unsigned long)__hyp_text_start &&
560 		    addr < (unsigned long)__hyp_text_end) ||
561 		    (addr >= (unsigned long)__hyp_idmap_text_start &&
562 		    addr < (unsigned long)__hyp_idmap_text_end))
563 			return true;
564 	}
565 
566 	return false;
567 }
568 
569 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
570 {
571 	struct kretprobe_instance *ri = NULL;
572 	struct hlist_head *head, empty_rp;
573 	struct hlist_node *tmp;
574 	unsigned long flags, orig_ret_address = 0;
575 	unsigned long trampoline_address =
576 		(unsigned long)&kretprobe_trampoline;
577 	kprobe_opcode_t *correct_ret_addr = NULL;
578 
579 	INIT_HLIST_HEAD(&empty_rp);
580 	kretprobe_hash_lock(current, &head, &flags);
581 
582 	/*
583 	 * It is possible to have multiple instances associated with a given
584 	 * task either because multiple functions in the call path have
585 	 * return probes installed on them, and/or more than one
586 	 * return probe was registered for a target function.
587 	 *
588 	 * We can handle this because:
589 	 *     - instances are always pushed into the head of the list
590 	 *     - when multiple return probes are registered for the same
591 	 *	 function, the (chronologically) first instance's ret_addr
592 	 *	 will be the real return address, and all the rest will
593 	 *	 point to kretprobe_trampoline.
594 	 */
595 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
596 		if (ri->task != current)
597 			/* another task is sharing our hash bucket */
598 			continue;
599 
600 		orig_ret_address = (unsigned long)ri->ret_addr;
601 
602 		if (orig_ret_address != trampoline_address)
603 			/*
604 			 * This is the real return address. Any other
605 			 * instances associated with this task are for
606 			 * other calls deeper on the call stack
607 			 */
608 			break;
609 	}
610 
611 	kretprobe_assert(ri, orig_ret_address, trampoline_address);
612 
613 	correct_ret_addr = ri->ret_addr;
614 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
615 		if (ri->task != current)
616 			/* another task is sharing our hash bucket */
617 			continue;
618 
619 		orig_ret_address = (unsigned long)ri->ret_addr;
620 		if (ri->rp && ri->rp->handler) {
621 			__this_cpu_write(current_kprobe, &ri->rp->kp);
622 			get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
623 			ri->ret_addr = correct_ret_addr;
624 			ri->rp->handler(ri, regs);
625 			__this_cpu_write(current_kprobe, NULL);
626 		}
627 
628 		recycle_rp_inst(ri, &empty_rp);
629 
630 		if (orig_ret_address != trampoline_address)
631 			/*
632 			 * This is the real return address. Any other
633 			 * instances associated with this task are for
634 			 * other calls deeper on the call stack
635 			 */
636 			break;
637 	}
638 
639 	kretprobe_hash_unlock(current, &flags);
640 
641 	hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
642 		hlist_del(&ri->hlist);
643 		kfree(ri);
644 	}
645 	return (void *)orig_ret_address;
646 }
647 
648 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
649 				      struct pt_regs *regs)
650 {
651 	ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
652 
653 	/* replace return addr (x30) with trampoline */
654 	regs->regs[30] = (long)&kretprobe_trampoline;
655 }
656 
657 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
658 {
659 	return 0;
660 }
661 
662 int __init arch_init_kprobes(void)
663 {
664 	return 0;
665 }
666