1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Kernel Probes (KProbes)
4 *
5 * Copyright (C) IBM Corporation, 2002, 2004
6 *
7 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
8 * Probes initial implementation ( includes contributions from
9 * Rusty Russell).
10 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
11 * interface to access function arguments.
12 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
13 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
14 * 2005-Mar Roland McGrath <roland@redhat.com>
15 * Fixed to handle %rip-relative addressing mode correctly.
16 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
17 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
18 * <prasanna@in.ibm.com> added function-return probes.
19 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
20 * Added function return probes functionality
21 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
22 * kprobe-booster and kretprobe-booster for i386.
23 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
24 * and kretprobe-booster for x86-64
25 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
26 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
27 * unified x86 kprobes code.
28 */
29 #include <linux/kprobes.h>
30 #include <linux/ptrace.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/hardirq.h>
34 #include <linux/preempt.h>
35 #include <linux/sched/debug.h>
36 #include <linux/perf_event.h>
37 #include <linux/extable.h>
38 #include <linux/kdebug.h>
39 #include <linux/kallsyms.h>
40 #include <linux/kgdb.h>
41 #include <linux/ftrace.h>
42 #include <linux/kasan.h>
43 #include <linux/moduleloader.h>
44 #include <linux/objtool.h>
45 #include <linux/vmalloc.h>
46 #include <linux/pgtable.h>
47
48 #include <asm/text-patching.h>
49 #include <asm/cacheflush.h>
50 #include <asm/desc.h>
51 #include <linux/uaccess.h>
52 #include <asm/alternative.h>
53 #include <asm/insn.h>
54 #include <asm/debugreg.h>
55 #include <asm/set_memory.h>
56
57 #include "common.h"
58
59 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
60 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
61
62 #define stack_addr(regs) ((unsigned long *)regs->sp)
63
64 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
65 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
66 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
67 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
68 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
69 << (row % 32))
70 /*
71 * Undefined/reserved opcodes, conditional jump, Opcode Extension
72 * Groups, and some special opcodes can not boost.
73 * This is non-const and volatile to keep gcc from statically
74 * optimizing it out, as variable_test_bit makes gcc think only
75 * *(unsigned long*) is used.
76 */
77 static volatile u32 twobyte_is_boostable[256 / 32] = {
78 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
79 /* ---------------------------------------------- */
80 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
81 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
82 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
83 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
84 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
85 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
86 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
87 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
88 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
89 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
90 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
91 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
92 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
93 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
94 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
95 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
96 /* ----------------------------------------------- */
97 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
98 };
99 #undef W
100
101 struct kretprobe_blackpoint kretprobe_blacklist[] = {
102 {"__switch_to", }, /* This function switches only current task, but
103 doesn't switch kernel stack.*/
104 {NULL, NULL} /* Terminator */
105 };
106
107 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
108
109 static nokprobe_inline void
__synthesize_relative_insn(void * dest,void * from,void * to,u8 op)110 __synthesize_relative_insn(void *dest, void *from, void *to, u8 op)
111 {
112 struct __arch_relative_insn {
113 u8 op;
114 s32 raddr;
115 } __packed *insn;
116
117 insn = (struct __arch_relative_insn *)dest;
118 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
119 insn->op = op;
120 }
121
122 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
synthesize_reljump(void * dest,void * from,void * to)123 void synthesize_reljump(void *dest, void *from, void *to)
124 {
125 __synthesize_relative_insn(dest, from, to, JMP32_INSN_OPCODE);
126 }
127 NOKPROBE_SYMBOL(synthesize_reljump);
128
129 /* Insert a call instruction at address 'from', which calls address 'to'.*/
synthesize_relcall(void * dest,void * from,void * to)130 void synthesize_relcall(void *dest, void *from, void *to)
131 {
132 __synthesize_relative_insn(dest, from, to, CALL_INSN_OPCODE);
133 }
134 NOKPROBE_SYMBOL(synthesize_relcall);
135
136 /*
137 * Skip the prefixes of the instruction.
138 */
skip_prefixes(kprobe_opcode_t * insn)139 static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
140 {
141 insn_attr_t attr;
142
143 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
144 while (inat_is_legacy_prefix(attr)) {
145 insn++;
146 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
147 }
148 #ifdef CONFIG_X86_64
149 if (inat_is_rex_prefix(attr))
150 insn++;
151 #endif
152 return insn;
153 }
154 NOKPROBE_SYMBOL(skip_prefixes);
155
156 /*
157 * Returns non-zero if INSN is boostable.
158 * RIP relative instructions are adjusted at copying time in 64 bits mode
159 */
can_boost(struct insn * insn,void * addr)160 int can_boost(struct insn *insn, void *addr)
161 {
162 kprobe_opcode_t opcode;
163 insn_byte_t prefix;
164 int i;
165
166 if (search_exception_tables((unsigned long)addr))
167 return 0; /* Page fault may occur on this address. */
168
169 /* 2nd-byte opcode */
170 if (insn->opcode.nbytes == 2)
171 return test_bit(insn->opcode.bytes[1],
172 (unsigned long *)twobyte_is_boostable);
173
174 if (insn->opcode.nbytes != 1)
175 return 0;
176
177 for_each_insn_prefix(insn, i, prefix) {
178 insn_attr_t attr;
179
180 attr = inat_get_opcode_attribute(prefix);
181 /* Can't boost Address-size override prefix and CS override prefix */
182 if (prefix == 0x2e || inat_is_address_size_prefix(attr))
183 return 0;
184 }
185
186 opcode = insn->opcode.bytes[0];
187
188 switch (opcode & 0xf0) {
189 case 0x60:
190 /* can't boost "bound" */
191 return (opcode != 0x62);
192 case 0x70:
193 return 0; /* can't boost conditional jump */
194 case 0x90:
195 return opcode != 0x9a; /* can't boost call far */
196 case 0xc0:
197 /* can't boost software-interruptions */
198 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
199 case 0xd0:
200 /* can boost AA* and XLAT */
201 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
202 case 0xe0:
203 /* can boost in/out and absolute jmps */
204 return ((opcode & 0x04) || opcode == 0xea);
205 case 0xf0:
206 /* clear and set flags are boostable */
207 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
208 default:
209 /* call is not boostable */
210 return opcode != 0x9a;
211 }
212 }
213
214 static unsigned long
__recover_probed_insn(kprobe_opcode_t * buf,unsigned long addr)215 __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
216 {
217 struct kprobe *kp;
218 unsigned long faddr;
219
220 kp = get_kprobe((void *)addr);
221 faddr = ftrace_location(addr);
222 /*
223 * Addresses inside the ftrace location are refused by
224 * arch_check_ftrace_location(). Something went terribly wrong
225 * if such an address is checked here.
226 */
227 if (WARN_ON(faddr && faddr != addr))
228 return 0UL;
229 /*
230 * Use the current code if it is not modified by Kprobe
231 * and it cannot be modified by ftrace.
232 */
233 if (!kp && !faddr)
234 return addr;
235
236 /*
237 * Basically, kp->ainsn.insn has an original instruction.
238 * However, RIP-relative instruction can not do single-stepping
239 * at different place, __copy_instruction() tweaks the displacement of
240 * that instruction. In that case, we can't recover the instruction
241 * from the kp->ainsn.insn.
242 *
243 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
244 * of the first byte of the probed instruction, which is overwritten
245 * by int3. And the instruction at kp->addr is not modified by kprobes
246 * except for the first byte, we can recover the original instruction
247 * from it and kp->opcode.
248 *
249 * In case of Kprobes using ftrace, we do not have a copy of
250 * the original instruction. In fact, the ftrace location might
251 * be modified at anytime and even could be in an inconsistent state.
252 * Fortunately, we know that the original code is the ideal 5-byte
253 * long NOP.
254 */
255 if (copy_from_kernel_nofault(buf, (void *)addr,
256 MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
257 return 0UL;
258
259 if (faddr)
260 memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
261 else
262 buf[0] = kp->opcode;
263 return (unsigned long)buf;
264 }
265
266 /*
267 * Recover the probed instruction at addr for further analysis.
268 * Caller must lock kprobes by kprobe_mutex, or disable preemption
269 * for preventing to release referencing kprobes.
270 * Returns zero if the instruction can not get recovered (or access failed).
271 */
recover_probed_instruction(kprobe_opcode_t * buf,unsigned long addr)272 unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
273 {
274 unsigned long __addr;
275
276 __addr = __recover_optprobed_insn(buf, addr);
277 if (__addr != addr)
278 return __addr;
279
280 return __recover_probed_insn(buf, addr);
281 }
282
283 /* Check if paddr is at an instruction boundary */
can_probe(unsigned long paddr)284 static int can_probe(unsigned long paddr)
285 {
286 unsigned long addr, __addr, offset = 0;
287 struct insn insn;
288 kprobe_opcode_t buf[MAX_INSN_SIZE];
289
290 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
291 return 0;
292
293 /* Decode instructions */
294 addr = paddr - offset;
295 while (addr < paddr) {
296 int ret;
297
298 /*
299 * Check if the instruction has been modified by another
300 * kprobe, in which case we replace the breakpoint by the
301 * original instruction in our buffer.
302 * Also, jump optimization will change the breakpoint to
303 * relative-jump. Since the relative-jump itself is
304 * normally used, we just go through if there is no kprobe.
305 */
306 __addr = recover_probed_instruction(buf, addr);
307 if (!__addr)
308 return 0;
309
310 ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN);
311 if (ret < 0)
312 return 0;
313
314 #ifdef CONFIG_KGDB
315 /*
316 * If there is a dynamically installed kgdb sw breakpoint,
317 * this function should not be probed.
318 */
319 if (insn.opcode.bytes[0] == INT3_INSN_OPCODE &&
320 kgdb_has_hit_break(addr))
321 return 0;
322 #endif
323 addr += insn.length;
324 }
325
326 return (addr == paddr);
327 }
328
329 /*
330 * Returns non-zero if opcode modifies the interrupt flag.
331 */
is_IF_modifier(kprobe_opcode_t * insn)332 static int is_IF_modifier(kprobe_opcode_t *insn)
333 {
334 /* Skip prefixes */
335 insn = skip_prefixes(insn);
336
337 switch (*insn) {
338 case 0xfa: /* cli */
339 case 0xfb: /* sti */
340 case 0xcf: /* iret/iretd */
341 case 0x9d: /* popf/popfd */
342 return 1;
343 }
344
345 return 0;
346 }
347
348 /*
349 * Copy an instruction with recovering modified instruction by kprobes
350 * and adjust the displacement if the instruction uses the %rip-relative
351 * addressing mode. Note that since @real will be the final place of copied
352 * instruction, displacement must be adjust by @real, not @dest.
353 * This returns the length of copied instruction, or 0 if it has an error.
354 */
__copy_instruction(u8 * dest,u8 * src,u8 * real,struct insn * insn)355 int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
356 {
357 kprobe_opcode_t buf[MAX_INSN_SIZE];
358 unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src);
359 int ret;
360
361 if (!recovered_insn || !insn)
362 return 0;
363
364 /* This can access kernel text if given address is not recovered */
365 if (copy_from_kernel_nofault(dest, (void *)recovered_insn,
366 MAX_INSN_SIZE))
367 return 0;
368
369 ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN);
370 if (ret < 0)
371 return 0;
372
373 /* We can not probe force emulate prefixed instruction */
374 if (insn_has_emulate_prefix(insn))
375 return 0;
376
377 /* Another subsystem puts a breakpoint, failed to recover */
378 if (insn->opcode.bytes[0] == INT3_INSN_OPCODE)
379 return 0;
380
381 /* We should not singlestep on the exception masking instructions */
382 if (insn_masking_exception(insn))
383 return 0;
384
385 #ifdef CONFIG_X86_64
386 /* Only x86_64 has RIP relative instructions */
387 if (insn_rip_relative(insn)) {
388 s64 newdisp;
389 u8 *disp;
390 /*
391 * The copied instruction uses the %rip-relative addressing
392 * mode. Adjust the displacement for the difference between
393 * the original location of this instruction and the location
394 * of the copy that will actually be run. The tricky bit here
395 * is making sure that the sign extension happens correctly in
396 * this calculation, since we need a signed 32-bit result to
397 * be sign-extended to 64 bits when it's added to the %rip
398 * value and yield the same 64-bit result that the sign-
399 * extension of the original signed 32-bit displacement would
400 * have given.
401 */
402 newdisp = (u8 *) src + (s64) insn->displacement.value
403 - (u8 *) real;
404 if ((s64) (s32) newdisp != newdisp) {
405 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
406 return 0;
407 }
408 disp = (u8 *) dest + insn_offset_displacement(insn);
409 *(s32 *) disp = (s32) newdisp;
410 }
411 #endif
412 return insn->length;
413 }
414
415 /* Prepare reljump right after instruction to boost */
prepare_boost(kprobe_opcode_t * buf,struct kprobe * p,struct insn * insn)416 static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
417 struct insn *insn)
418 {
419 int len = insn->length;
420
421 if (can_boost(insn, p->addr) &&
422 MAX_INSN_SIZE - len >= JMP32_INSN_SIZE) {
423 /*
424 * These instructions can be executed directly if it
425 * jumps back to correct address.
426 */
427 synthesize_reljump(buf + len, p->ainsn.insn + len,
428 p->addr + insn->length);
429 len += JMP32_INSN_SIZE;
430 p->ainsn.boostable = true;
431 } else {
432 p->ainsn.boostable = false;
433 }
434
435 return len;
436 }
437
438 /* Make page to RO mode when allocate it */
alloc_insn_page(void)439 void *alloc_insn_page(void)
440 {
441 void *page;
442
443 page = module_alloc(PAGE_SIZE);
444 if (!page)
445 return NULL;
446
447 set_vm_flush_reset_perms(page);
448 /*
449 * First make the page read-only, and only then make it executable to
450 * prevent it from being W+X in between.
451 */
452 set_memory_ro((unsigned long)page, 1);
453
454 /*
455 * TODO: Once additional kernel code protection mechanisms are set, ensure
456 * that the page was not maliciously altered and it is still zeroed.
457 */
458 set_memory_x((unsigned long)page, 1);
459
460 return page;
461 }
462
463 /* Recover page to RW mode before releasing it */
free_insn_page(void * page)464 void free_insn_page(void *page)
465 {
466 module_memfree(page);
467 }
468
arch_copy_kprobe(struct kprobe * p)469 static int arch_copy_kprobe(struct kprobe *p)
470 {
471 struct insn insn;
472 kprobe_opcode_t buf[MAX_INSN_SIZE];
473 int len;
474
475 /* Copy an instruction with recovering if other optprobe modifies it.*/
476 len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn);
477 if (!len)
478 return -EINVAL;
479
480 /*
481 * __copy_instruction can modify the displacement of the instruction,
482 * but it doesn't affect boostable check.
483 */
484 len = prepare_boost(buf, p, &insn);
485
486 /* Check whether the instruction modifies Interrupt Flag or not */
487 p->ainsn.if_modifier = is_IF_modifier(buf);
488
489 /* Also, displacement change doesn't affect the first byte */
490 p->opcode = buf[0];
491
492 p->ainsn.tp_len = len;
493 perf_event_text_poke(p->ainsn.insn, NULL, 0, buf, len);
494
495 /* OK, write back the instruction(s) into ROX insn buffer */
496 text_poke(p->ainsn.insn, buf, len);
497
498 return 0;
499 }
500
arch_prepare_kprobe(struct kprobe * p)501 int arch_prepare_kprobe(struct kprobe *p)
502 {
503 int ret;
504
505 if (alternatives_text_reserved(p->addr, p->addr))
506 return -EINVAL;
507
508 if (!can_probe((unsigned long)p->addr))
509 return -EILSEQ;
510 /* insn: must be on special executable page on x86. */
511 p->ainsn.insn = get_insn_slot();
512 if (!p->ainsn.insn)
513 return -ENOMEM;
514
515 ret = arch_copy_kprobe(p);
516 if (ret) {
517 free_insn_slot(p->ainsn.insn, 0);
518 p->ainsn.insn = NULL;
519 }
520
521 return ret;
522 }
523
arch_arm_kprobe(struct kprobe * p)524 void arch_arm_kprobe(struct kprobe *p)
525 {
526 u8 int3 = INT3_INSN_OPCODE;
527
528 text_poke(p->addr, &int3, 1);
529 text_poke_sync();
530 perf_event_text_poke(p->addr, &p->opcode, 1, &int3, 1);
531 }
532
arch_disarm_kprobe(struct kprobe * p)533 void arch_disarm_kprobe(struct kprobe *p)
534 {
535 u8 int3 = INT3_INSN_OPCODE;
536
537 perf_event_text_poke(p->addr, &int3, 1, &p->opcode, 1);
538 text_poke(p->addr, &p->opcode, 1);
539 text_poke_sync();
540 }
541
arch_remove_kprobe(struct kprobe * p)542 void arch_remove_kprobe(struct kprobe *p)
543 {
544 if (p->ainsn.insn) {
545 /* Record the perf event before freeing the slot */
546 perf_event_text_poke(p->ainsn.insn, p->ainsn.insn,
547 p->ainsn.tp_len, NULL, 0);
548 free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
549 p->ainsn.insn = NULL;
550 }
551 }
552
553 static nokprobe_inline void
save_previous_kprobe(struct kprobe_ctlblk * kcb)554 save_previous_kprobe(struct kprobe_ctlblk *kcb)
555 {
556 kcb->prev_kprobe.kp = kprobe_running();
557 kcb->prev_kprobe.status = kcb->kprobe_status;
558 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
559 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
560 }
561
562 static nokprobe_inline void
restore_previous_kprobe(struct kprobe_ctlblk * kcb)563 restore_previous_kprobe(struct kprobe_ctlblk *kcb)
564 {
565 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
566 kcb->kprobe_status = kcb->prev_kprobe.status;
567 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
568 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
569 }
570
571 static nokprobe_inline void
set_current_kprobe(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)572 set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
573 struct kprobe_ctlblk *kcb)
574 {
575 __this_cpu_write(current_kprobe, p);
576 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
577 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
578 if (p->ainsn.if_modifier)
579 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
580 }
581
clear_btf(void)582 static nokprobe_inline void clear_btf(void)
583 {
584 if (test_thread_flag(TIF_BLOCKSTEP)) {
585 unsigned long debugctl = get_debugctlmsr();
586
587 debugctl &= ~DEBUGCTLMSR_BTF;
588 update_debugctlmsr(debugctl);
589 }
590 }
591
restore_btf(void)592 static nokprobe_inline void restore_btf(void)
593 {
594 if (test_thread_flag(TIF_BLOCKSTEP)) {
595 unsigned long debugctl = get_debugctlmsr();
596
597 debugctl |= DEBUGCTLMSR_BTF;
598 update_debugctlmsr(debugctl);
599 }
600 }
601
arch_prepare_kretprobe(struct kretprobe_instance * ri,struct pt_regs * regs)602 void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
603 {
604 unsigned long *sara = stack_addr(regs);
605
606 ri->ret_addr = (kprobe_opcode_t *) *sara;
607 ri->fp = sara;
608
609 /* Replace the return addr with trampoline addr */
610 *sara = (unsigned long) &kretprobe_trampoline;
611 }
612 NOKPROBE_SYMBOL(arch_prepare_kretprobe);
613
setup_singlestep(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb,int reenter)614 static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
615 struct kprobe_ctlblk *kcb, int reenter)
616 {
617 if (setup_detour_execution(p, regs, reenter))
618 return;
619
620 #if !defined(CONFIG_PREEMPTION)
621 if (p->ainsn.boostable && !p->post_handler) {
622 /* Boost up -- we can execute copied instructions directly */
623 if (!reenter)
624 reset_current_kprobe();
625 /*
626 * Reentering boosted probe doesn't reset current_kprobe,
627 * nor set current_kprobe, because it doesn't use single
628 * stepping.
629 */
630 regs->ip = (unsigned long)p->ainsn.insn;
631 return;
632 }
633 #endif
634 if (reenter) {
635 save_previous_kprobe(kcb);
636 set_current_kprobe(p, regs, kcb);
637 kcb->kprobe_status = KPROBE_REENTER;
638 } else
639 kcb->kprobe_status = KPROBE_HIT_SS;
640 /* Prepare real single stepping */
641 clear_btf();
642 regs->flags |= X86_EFLAGS_TF;
643 regs->flags &= ~X86_EFLAGS_IF;
644 /* single step inline if the instruction is an int3 */
645 if (p->opcode == INT3_INSN_OPCODE)
646 regs->ip = (unsigned long)p->addr;
647 else
648 regs->ip = (unsigned long)p->ainsn.insn;
649 }
650 NOKPROBE_SYMBOL(setup_singlestep);
651
652 /*
653 * We have reentered the kprobe_handler(), since another probe was hit while
654 * within the handler. We save the original kprobes variables and just single
655 * step on the instruction of the new probe without calling any user handlers.
656 */
reenter_kprobe(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)657 static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
658 struct kprobe_ctlblk *kcb)
659 {
660 switch (kcb->kprobe_status) {
661 case KPROBE_HIT_SSDONE:
662 case KPROBE_HIT_ACTIVE:
663 case KPROBE_HIT_SS:
664 kprobes_inc_nmissed_count(p);
665 setup_singlestep(p, regs, kcb, 1);
666 break;
667 case KPROBE_REENTER:
668 /* A probe has been hit in the codepath leading up to, or just
669 * after, single-stepping of a probed instruction. This entire
670 * codepath should strictly reside in .kprobes.text section.
671 * Raise a BUG or we'll continue in an endless reentering loop
672 * and eventually a stack overflow.
673 */
674 pr_err("Unrecoverable kprobe detected.\n");
675 dump_kprobe(p);
676 BUG();
677 default:
678 /* impossible cases */
679 WARN_ON(1);
680 return 0;
681 }
682
683 return 1;
684 }
685 NOKPROBE_SYMBOL(reenter_kprobe);
686
687 /*
688 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
689 * remain disabled throughout this function.
690 */
kprobe_int3_handler(struct pt_regs * regs)691 int kprobe_int3_handler(struct pt_regs *regs)
692 {
693 kprobe_opcode_t *addr;
694 struct kprobe *p;
695 struct kprobe_ctlblk *kcb;
696
697 if (user_mode(regs))
698 return 0;
699
700 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
701 /*
702 * We don't want to be preempted for the entire duration of kprobe
703 * processing. Since int3 and debug trap disables irqs and we clear
704 * IF while singlestepping, it must be no preemptible.
705 */
706
707 kcb = get_kprobe_ctlblk();
708 p = get_kprobe(addr);
709
710 if (p) {
711 if (kprobe_running()) {
712 if (reenter_kprobe(p, regs, kcb))
713 return 1;
714 } else {
715 set_current_kprobe(p, regs, kcb);
716 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
717
718 /*
719 * If we have no pre-handler or it returned 0, we
720 * continue with normal processing. If we have a
721 * pre-handler and it returned non-zero, that means
722 * user handler setup registers to exit to another
723 * instruction, we must skip the single stepping.
724 */
725 if (!p->pre_handler || !p->pre_handler(p, regs))
726 setup_singlestep(p, regs, kcb, 0);
727 else
728 reset_current_kprobe();
729 return 1;
730 }
731 } else if (*addr != INT3_INSN_OPCODE) {
732 /*
733 * The breakpoint instruction was removed right
734 * after we hit it. Another cpu has removed
735 * either a probepoint or a debugger breakpoint
736 * at this address. In either case, no further
737 * handling of this interrupt is appropriate.
738 * Back up over the (now missing) int3 and run
739 * the original instruction.
740 */
741 regs->ip = (unsigned long)addr;
742 return 1;
743 } /* else: not a kprobe fault; let the kernel handle it */
744
745 return 0;
746 }
747 NOKPROBE_SYMBOL(kprobe_int3_handler);
748
749 /*
750 * When a retprobed function returns, this code saves registers and
751 * calls trampoline_handler() runs, which calls the kretprobe's handler.
752 */
753 asm(
754 ".text\n"
755 ".global kretprobe_trampoline\n"
756 ".type kretprobe_trampoline, @function\n"
757 "kretprobe_trampoline:\n"
758 /* We don't bother saving the ss register */
759 #ifdef CONFIG_X86_64
760 " pushq %rsp\n"
761 " pushfq\n"
762 SAVE_REGS_STRING
763 " movq %rsp, %rdi\n"
764 " call trampoline_handler\n"
765 /* Replace saved sp with true return address. */
766 " movq %rax, 19*8(%rsp)\n"
767 RESTORE_REGS_STRING
768 " popfq\n"
769 #else
770 " pushl %esp\n"
771 " pushfl\n"
772 SAVE_REGS_STRING
773 " movl %esp, %eax\n"
774 " call trampoline_handler\n"
775 /* Replace saved sp with true return address. */
776 " movl %eax, 15*4(%esp)\n"
777 RESTORE_REGS_STRING
778 " popfl\n"
779 #endif
780 ASM_RET
781 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
782 );
783 NOKPROBE_SYMBOL(kretprobe_trampoline);
784 STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
785
786
787 /*
788 * Called from kretprobe_trampoline
789 */
trampoline_handler(struct pt_regs * regs)790 __used __visible void *trampoline_handler(struct pt_regs *regs)
791 {
792 /* fixup registers */
793 regs->cs = __KERNEL_CS;
794 #ifdef CONFIG_X86_32
795 regs->gs = 0;
796 #endif
797 regs->ip = (unsigned long)&kretprobe_trampoline;
798 regs->orig_ax = ~0UL;
799
800 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, ®s->sp);
801 }
802 NOKPROBE_SYMBOL(trampoline_handler);
803
804 /*
805 * Called after single-stepping. p->addr is the address of the
806 * instruction whose first byte has been replaced by the "int 3"
807 * instruction. To avoid the SMP problems that can occur when we
808 * temporarily put back the original opcode to single-step, we
809 * single-stepped a copy of the instruction. The address of this
810 * copy is p->ainsn.insn.
811 *
812 * This function prepares to return from the post-single-step
813 * interrupt. We have to fix up the stack as follows:
814 *
815 * 0) Except in the case of absolute or indirect jump or call instructions,
816 * the new ip is relative to the copied instruction. We need to make
817 * it relative to the original instruction.
818 *
819 * 1) If the single-stepped instruction was pushfl, then the TF and IF
820 * flags are set in the just-pushed flags, and may need to be cleared.
821 *
822 * 2) If the single-stepped instruction was a call, the return address
823 * that is atop the stack is the address following the copied instruction.
824 * We need to make it the address following the original instruction.
825 *
826 * If this is the first time we've single-stepped the instruction at
827 * this probepoint, and the instruction is boostable, boost it: add a
828 * jump instruction after the copied instruction, that jumps to the next
829 * instruction after the probepoint.
830 */
resume_execution(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)831 static void resume_execution(struct kprobe *p, struct pt_regs *regs,
832 struct kprobe_ctlblk *kcb)
833 {
834 unsigned long *tos = stack_addr(regs);
835 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
836 unsigned long orig_ip = (unsigned long)p->addr;
837 kprobe_opcode_t *insn = p->ainsn.insn;
838
839 /* Skip prefixes */
840 insn = skip_prefixes(insn);
841
842 regs->flags &= ~X86_EFLAGS_TF;
843 switch (*insn) {
844 case 0x9c: /* pushfl */
845 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
846 *tos |= kcb->kprobe_old_flags;
847 break;
848 case 0xc2: /* iret/ret/lret */
849 case 0xc3:
850 case 0xca:
851 case 0xcb:
852 case 0xcf:
853 case 0xea: /* jmp absolute -- ip is correct */
854 /* ip is already adjusted, no more changes required */
855 p->ainsn.boostable = true;
856 goto no_change;
857 case 0xe8: /* call relative - Fix return addr */
858 *tos = orig_ip + (*tos - copy_ip);
859 break;
860 #ifdef CONFIG_X86_32
861 case 0x9a: /* call absolute -- same as call absolute, indirect */
862 *tos = orig_ip + (*tos - copy_ip);
863 goto no_change;
864 #endif
865 case 0xff:
866 if ((insn[1] & 0x30) == 0x10) {
867 /*
868 * call absolute, indirect
869 * Fix return addr; ip is correct.
870 * But this is not boostable
871 */
872 *tos = orig_ip + (*tos - copy_ip);
873 goto no_change;
874 } else if (((insn[1] & 0x31) == 0x20) ||
875 ((insn[1] & 0x31) == 0x21)) {
876 /*
877 * jmp near and far, absolute indirect
878 * ip is correct. And this is boostable
879 */
880 p->ainsn.boostable = true;
881 goto no_change;
882 }
883 default:
884 break;
885 }
886
887 regs->ip += orig_ip - copy_ip;
888
889 no_change:
890 restore_btf();
891 }
892 NOKPROBE_SYMBOL(resume_execution);
893
894 /*
895 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
896 * remain disabled throughout this function.
897 */
kprobe_debug_handler(struct pt_regs * regs)898 int kprobe_debug_handler(struct pt_regs *regs)
899 {
900 struct kprobe *cur = kprobe_running();
901 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
902
903 if (!cur)
904 return 0;
905
906 resume_execution(cur, regs, kcb);
907 regs->flags |= kcb->kprobe_saved_flags;
908
909 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
910 kcb->kprobe_status = KPROBE_HIT_SSDONE;
911 cur->post_handler(cur, regs, 0);
912 }
913
914 /* Restore back the original saved kprobes variables and continue. */
915 if (kcb->kprobe_status == KPROBE_REENTER) {
916 restore_previous_kprobe(kcb);
917 goto out;
918 }
919 reset_current_kprobe();
920 out:
921 /*
922 * if somebody else is singlestepping across a probe point, flags
923 * will have TF set, in which case, continue the remaining processing
924 * of do_debug, as if this is not a probe hit.
925 */
926 if (regs->flags & X86_EFLAGS_TF)
927 return 0;
928
929 return 1;
930 }
931 NOKPROBE_SYMBOL(kprobe_debug_handler);
932
kprobe_fault_handler(struct pt_regs * regs,int trapnr)933 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
934 {
935 struct kprobe *cur = kprobe_running();
936 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
937
938 if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
939 /* This must happen on single-stepping */
940 WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
941 kcb->kprobe_status != KPROBE_REENTER);
942 /*
943 * We are here because the instruction being single
944 * stepped caused a page fault. We reset the current
945 * kprobe and the ip points back to the probe address
946 * and allow the page fault handler to continue as a
947 * normal page fault.
948 */
949 regs->ip = (unsigned long)cur->addr;
950 /*
951 * Trap flag (TF) has been set here because this fault
952 * happened where the single stepping will be done.
953 * So clear it by resetting the current kprobe:
954 */
955 regs->flags &= ~X86_EFLAGS_TF;
956 /*
957 * Since the single step (trap) has been cancelled,
958 * we need to restore BTF here.
959 */
960 restore_btf();
961
962 /*
963 * If the TF flag was set before the kprobe hit,
964 * don't touch it:
965 */
966 regs->flags |= kcb->kprobe_old_flags;
967
968 if (kcb->kprobe_status == KPROBE_REENTER)
969 restore_previous_kprobe(kcb);
970 else
971 reset_current_kprobe();
972 } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
973 kcb->kprobe_status == KPROBE_HIT_SSDONE) {
974 /*
975 * We increment the nmissed count for accounting,
976 * we can also use npre/npostfault count for accounting
977 * these specific fault cases.
978 */
979 kprobes_inc_nmissed_count(cur);
980
981 /*
982 * We come here because instructions in the pre/post
983 * handler caused the page_fault, this could happen
984 * if handler tries to access user space by
985 * copy_from_user(), get_user() etc. Let the
986 * user-specified handler try to fix it first.
987 */
988 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
989 return 1;
990 }
991
992 return 0;
993 }
994 NOKPROBE_SYMBOL(kprobe_fault_handler);
995
arch_populate_kprobe_blacklist(void)996 int __init arch_populate_kprobe_blacklist(void)
997 {
998 return kprobe_add_area_blacklist((unsigned long)__entry_text_start,
999 (unsigned long)__entry_text_end);
1000 }
1001
arch_init_kprobes(void)1002 int __init arch_init_kprobes(void)
1003 {
1004 return 0;
1005 }
1006
arch_trampoline_kprobe(struct kprobe * p)1007 int arch_trampoline_kprobe(struct kprobe *p)
1008 {
1009 return 0;
1010 }
1011