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