1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Kernel Probes Jump Optimization (Optprobes) 4 * 5 * Copyright (C) IBM Corporation, 2002, 2004 6 * Copyright (C) Hitachi Ltd., 2012 7 */ 8 #include <linux/kprobes.h> 9 #include <linux/perf_event.h> 10 #include <linux/ptrace.h> 11 #include <linux/string.h> 12 #include <linux/slab.h> 13 #include <linux/hardirq.h> 14 #include <linux/preempt.h> 15 #include <linux/extable.h> 16 #include <linux/kdebug.h> 17 #include <linux/kallsyms.h> 18 #include <linux/kgdb.h> 19 #include <linux/ftrace.h> 20 #include <linux/objtool.h> 21 #include <linux/pgtable.h> 22 #include <linux/static_call.h> 23 24 #include <asm/text-patching.h> 25 #include <asm/cacheflush.h> 26 #include <asm/desc.h> 27 #include <linux/uaccess.h> 28 #include <asm/alternative.h> 29 #include <asm/insn.h> 30 #include <asm/debugreg.h> 31 #include <asm/set_memory.h> 32 #include <asm/sections.h> 33 #include <asm/nospec-branch.h> 34 35 #include "common.h" 36 37 unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr) 38 { 39 struct optimized_kprobe *op; 40 struct kprobe *kp; 41 long offs; 42 int i; 43 44 for (i = 0; i < JMP32_INSN_SIZE; i++) { 45 kp = get_kprobe((void *)addr - i); 46 /* This function only handles jump-optimized kprobe */ 47 if (kp && kprobe_optimized(kp)) { 48 op = container_of(kp, struct optimized_kprobe, kp); 49 /* If op is optimized or under unoptimizing */ 50 if (list_empty(&op->list) || optprobe_queued_unopt(op)) 51 goto found; 52 } 53 } 54 55 return addr; 56 found: 57 /* 58 * If the kprobe can be optimized, original bytes which can be 59 * overwritten by jump destination address. In this case, original 60 * bytes must be recovered from op->optinsn.copied_insn buffer. 61 */ 62 if (copy_from_kernel_nofault(buf, (void *)addr, 63 MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) 64 return 0UL; 65 66 if (addr == (unsigned long)kp->addr) { 67 buf[0] = kp->opcode; 68 memcpy(buf + 1, op->optinsn.copied_insn, DISP32_SIZE); 69 } else { 70 offs = addr - (unsigned long)kp->addr - 1; 71 memcpy(buf, op->optinsn.copied_insn + offs, DISP32_SIZE - offs); 72 } 73 74 return (unsigned long)buf; 75 } 76 77 static void synthesize_clac(kprobe_opcode_t *addr) 78 { 79 /* 80 * Can't be cpu_feature_enabled() due to how objtool treats this feature bit. 81 * This isn't a fast path anyway. 82 */ 83 if (!boot_cpu_has(X86_FEATURE_SMAP)) 84 return; 85 86 /* Replace the NOP3 with CLAC */ 87 addr[0] = 0x0f; 88 addr[1] = 0x01; 89 addr[2] = 0xca; 90 } 91 92 /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */ 93 static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val) 94 { 95 #ifdef CONFIG_X86_64 96 *addr++ = 0x48; 97 *addr++ = 0xbf; 98 #else 99 *addr++ = 0xb8; 100 #endif 101 *(unsigned long *)addr = val; 102 } 103 104 asm ( 105 ".pushsection .rodata\n" 106 ".global optprobe_template_entry\n" 107 "optprobe_template_entry:\n" 108 #ifdef CONFIG_X86_64 109 " pushq $" __stringify(__KERNEL_DS) "\n" 110 /* Save the 'sp - 8', this will be fixed later. */ 111 " pushq %rsp\n" 112 " pushfq\n" 113 ".global optprobe_template_clac\n" 114 "optprobe_template_clac:\n" 115 ASM_NOP3 116 SAVE_REGS_STRING 117 " movq %rsp, %rsi\n" 118 ".global optprobe_template_val\n" 119 "optprobe_template_val:\n" 120 ASM_NOP5 121 ASM_NOP5 122 ".global optprobe_template_call\n" 123 "optprobe_template_call:\n" 124 ASM_NOP5 125 /* Copy 'regs->flags' into 'regs->ss'. */ 126 " movq 18*8(%rsp), %rdx\n" 127 " movq %rdx, 20*8(%rsp)\n" 128 RESTORE_REGS_STRING 129 /* Skip 'regs->flags' and 'regs->sp'. */ 130 " addq $16, %rsp\n" 131 /* And pop flags register from 'regs->ss'. */ 132 " popfq\n" 133 #else /* CONFIG_X86_32 */ 134 " pushl %ss\n" 135 /* Save the 'sp - 4', this will be fixed later. */ 136 " pushl %esp\n" 137 " pushfl\n" 138 ".global optprobe_template_clac\n" 139 "optprobe_template_clac:\n" 140 ASM_NOP3 141 SAVE_REGS_STRING 142 " movl %esp, %edx\n" 143 ".global optprobe_template_val\n" 144 "optprobe_template_val:\n" 145 ASM_NOP5 146 ".global optprobe_template_call\n" 147 "optprobe_template_call:\n" 148 ASM_NOP5 149 /* Copy 'regs->flags' into 'regs->ss'. */ 150 " movl 14*4(%esp), %edx\n" 151 " movl %edx, 16*4(%esp)\n" 152 RESTORE_REGS_STRING 153 /* Skip 'regs->flags' and 'regs->sp'. */ 154 " addl $8, %esp\n" 155 /* And pop flags register from 'regs->ss'. */ 156 " popfl\n" 157 #endif 158 ".global optprobe_template_end\n" 159 "optprobe_template_end:\n" 160 ".popsection\n"); 161 162 #define TMPL_CLAC_IDX \ 163 ((long)optprobe_template_clac - (long)optprobe_template_entry) 164 #define TMPL_MOVE_IDX \ 165 ((long)optprobe_template_val - (long)optprobe_template_entry) 166 #define TMPL_CALL_IDX \ 167 ((long)optprobe_template_call - (long)optprobe_template_entry) 168 #define TMPL_END_IDX \ 169 ((long)optprobe_template_end - (long)optprobe_template_entry) 170 171 /* Optimized kprobe call back function: called from optinsn */ 172 static void 173 optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs) 174 { 175 /* This is possible if op is under delayed unoptimizing */ 176 if (kprobe_disabled(&op->kp)) 177 return; 178 179 preempt_disable(); 180 if (kprobe_running()) { 181 kprobes_inc_nmissed_count(&op->kp); 182 } else { 183 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 184 /* Adjust stack pointer */ 185 regs->sp += sizeof(long); 186 /* Save skipped registers */ 187 regs->cs = __KERNEL_CS; 188 #ifdef CONFIG_X86_32 189 regs->gs = 0; 190 #endif 191 regs->ip = (unsigned long)op->kp.addr + INT3_INSN_SIZE; 192 regs->orig_ax = ~0UL; 193 194 __this_cpu_write(current_kprobe, &op->kp); 195 kcb->kprobe_status = KPROBE_HIT_ACTIVE; 196 opt_pre_handler(&op->kp, regs); 197 __this_cpu_write(current_kprobe, NULL); 198 } 199 preempt_enable(); 200 } 201 NOKPROBE_SYMBOL(optimized_callback); 202 203 static int copy_optimized_instructions(u8 *dest, u8 *src, u8 *real) 204 { 205 struct insn insn; 206 int len = 0, ret; 207 208 while (len < JMP32_INSN_SIZE) { 209 ret = __copy_instruction(dest + len, src + len, real + len, &insn); 210 if (!ret || !can_boost(&insn, src + len)) 211 return -EINVAL; 212 len += ret; 213 } 214 /* Check whether the address range is reserved */ 215 if (ftrace_text_reserved(src, src + len - 1) || 216 jump_label_text_reserved(src, src + len - 1) || 217 static_call_text_reserved(src, src + len - 1)) 218 return -EBUSY; 219 220 return len; 221 } 222 223 /* Check whether insn is indirect jump */ 224 static int insn_is_indirect_jump(struct insn *insn) 225 { 226 return ((insn->opcode.bytes[0] == 0xff && 227 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */ 228 insn->opcode.bytes[0] == 0xea); /* Segment based jump */ 229 } 230 231 /* Check whether insn jumps into specified address range */ 232 static int insn_jump_into_range(struct insn *insn, unsigned long start, int len) 233 { 234 unsigned long target = 0; 235 236 switch (insn->opcode.bytes[0]) { 237 case 0xe0: /* loopne */ 238 case 0xe1: /* loope */ 239 case 0xe2: /* loop */ 240 case 0xe3: /* jcxz */ 241 case 0xe9: /* near relative jump */ 242 case 0xeb: /* short relative jump */ 243 break; 244 case 0x0f: 245 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */ 246 break; 247 return 0; 248 default: 249 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */ 250 break; 251 return 0; 252 } 253 target = (unsigned long)insn->next_byte + insn->immediate.value; 254 255 return (start <= target && target <= start + len); 256 } 257 258 /* Decode whole function to ensure any instructions don't jump into target */ 259 static int can_optimize(unsigned long paddr) 260 { 261 unsigned long addr, size = 0, offset = 0; 262 struct insn insn; 263 kprobe_opcode_t buf[MAX_INSN_SIZE]; 264 265 /* Lookup symbol including addr */ 266 if (!kallsyms_lookup_size_offset(paddr, &size, &offset)) 267 return 0; 268 269 /* 270 * Do not optimize in the entry code due to the unstable 271 * stack handling and registers setup. 272 */ 273 if (((paddr >= (unsigned long)__entry_text_start) && 274 (paddr < (unsigned long)__entry_text_end))) 275 return 0; 276 277 /* Check there is enough space for a relative jump. */ 278 if (size - offset < JMP32_INSN_SIZE) 279 return 0; 280 281 /* Decode instructions */ 282 addr = paddr - offset; 283 while (addr < paddr - offset + size) { /* Decode until function end */ 284 unsigned long recovered_insn; 285 int ret; 286 287 if (search_exception_tables(addr)) 288 /* 289 * Since some fixup code will jumps into this function, 290 * we can't optimize kprobe in this function. 291 */ 292 return 0; 293 recovered_insn = recover_probed_instruction(buf, addr); 294 if (!recovered_insn) 295 return 0; 296 297 ret = insn_decode_kernel(&insn, (void *)recovered_insn); 298 if (ret < 0) 299 return 0; 300 #ifdef CONFIG_KGDB 301 /* 302 * If there is a dynamically installed kgdb sw breakpoint, 303 * this function should not be probed. 304 */ 305 if (insn.opcode.bytes[0] == INT3_INSN_OPCODE && 306 kgdb_has_hit_break(addr)) 307 return 0; 308 #endif 309 /* Recover address */ 310 insn.kaddr = (void *)addr; 311 insn.next_byte = (void *)(addr + insn.length); 312 /* 313 * Check any instructions don't jump into target, indirectly or 314 * directly. 315 * 316 * The indirect case is present to handle a code with jump 317 * tables. When the kernel uses retpolines, the check should in 318 * theory additionally look for jumps to indirect thunks. 319 * However, the kernel built with retpolines or IBT has jump 320 * tables disabled so the check can be skipped altogether. 321 */ 322 if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && 323 !IS_ENABLED(CONFIG_X86_KERNEL_IBT) && 324 insn_is_indirect_jump(&insn)) 325 return 0; 326 if (insn_jump_into_range(&insn, paddr + INT3_INSN_SIZE, 327 DISP32_SIZE)) 328 return 0; 329 addr += insn.length; 330 } 331 332 return 1; 333 } 334 335 /* Check optimized_kprobe can actually be optimized. */ 336 int arch_check_optimized_kprobe(struct optimized_kprobe *op) 337 { 338 int i; 339 struct kprobe *p; 340 341 for (i = 1; i < op->optinsn.size; i++) { 342 p = get_kprobe(op->kp.addr + i); 343 if (p && !kprobe_disarmed(p)) 344 return -EEXIST; 345 } 346 347 return 0; 348 } 349 350 /* Check the addr is within the optimized instructions. */ 351 int arch_within_optimized_kprobe(struct optimized_kprobe *op, 352 kprobe_opcode_t *addr) 353 { 354 return (op->kp.addr <= addr && 355 op->kp.addr + op->optinsn.size > addr); 356 } 357 358 /* Free optimized instruction slot */ 359 static 360 void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty) 361 { 362 u8 *slot = op->optinsn.insn; 363 if (slot) { 364 int len = TMPL_END_IDX + op->optinsn.size + JMP32_INSN_SIZE; 365 366 /* Record the perf event before freeing the slot */ 367 if (dirty) 368 perf_event_text_poke(slot, slot, len, NULL, 0); 369 370 free_optinsn_slot(slot, dirty); 371 op->optinsn.insn = NULL; 372 op->optinsn.size = 0; 373 } 374 } 375 376 void arch_remove_optimized_kprobe(struct optimized_kprobe *op) 377 { 378 __arch_remove_optimized_kprobe(op, 1); 379 } 380 381 /* 382 * Copy replacing target instructions 383 * Target instructions MUST be relocatable (checked inside) 384 * This is called when new aggr(opt)probe is allocated or reused. 385 */ 386 int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, 387 struct kprobe *__unused) 388 { 389 u8 *buf = NULL, *slot; 390 int ret, len; 391 long rel; 392 393 if (!can_optimize((unsigned long)op->kp.addr)) 394 return -EILSEQ; 395 396 buf = kzalloc(MAX_OPTINSN_SIZE, GFP_KERNEL); 397 if (!buf) 398 return -ENOMEM; 399 400 op->optinsn.insn = slot = get_optinsn_slot(); 401 if (!slot) { 402 ret = -ENOMEM; 403 goto out; 404 } 405 406 /* 407 * Verify if the address gap is in 2GB range, because this uses 408 * a relative jump. 409 */ 410 rel = (long)slot - (long)op->kp.addr + JMP32_INSN_SIZE; 411 if (abs(rel) > 0x7fffffff) { 412 ret = -ERANGE; 413 goto err; 414 } 415 416 /* Copy arch-dep-instance from template */ 417 memcpy(buf, optprobe_template_entry, TMPL_END_IDX); 418 419 /* Copy instructions into the out-of-line buffer */ 420 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr, 421 slot + TMPL_END_IDX); 422 if (ret < 0) 423 goto err; 424 op->optinsn.size = ret; 425 len = TMPL_END_IDX + op->optinsn.size; 426 427 synthesize_clac(buf + TMPL_CLAC_IDX); 428 429 /* Set probe information */ 430 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op); 431 432 /* Set probe function call */ 433 synthesize_relcall(buf + TMPL_CALL_IDX, 434 slot + TMPL_CALL_IDX, optimized_callback); 435 436 /* Set returning jmp instruction at the tail of out-of-line buffer */ 437 synthesize_reljump(buf + len, slot + len, 438 (u8 *)op->kp.addr + op->optinsn.size); 439 len += JMP32_INSN_SIZE; 440 441 /* 442 * Note len = TMPL_END_IDX + op->optinsn.size + JMP32_INSN_SIZE is also 443 * used in __arch_remove_optimized_kprobe(). 444 */ 445 446 /* We have to use text_poke() for instruction buffer because it is RO */ 447 perf_event_text_poke(slot, NULL, 0, buf, len); 448 text_poke(slot, buf, len); 449 450 ret = 0; 451 out: 452 kfree(buf); 453 return ret; 454 455 err: 456 __arch_remove_optimized_kprobe(op, 0); 457 goto out; 458 } 459 460 /* 461 * Replace breakpoints (INT3) with relative jumps (JMP.d32). 462 * Caller must call with locking kprobe_mutex and text_mutex. 463 * 464 * The caller will have installed a regular kprobe and after that issued 465 * syncrhonize_rcu_tasks(), this ensures that the instruction(s) that live in 466 * the 4 bytes after the INT3 are unused and can now be overwritten. 467 */ 468 void arch_optimize_kprobes(struct list_head *oplist) 469 { 470 struct optimized_kprobe *op, *tmp; 471 u8 insn_buff[JMP32_INSN_SIZE]; 472 473 list_for_each_entry_safe(op, tmp, oplist, list) { 474 s32 rel = (s32)((long)op->optinsn.insn - 475 ((long)op->kp.addr + JMP32_INSN_SIZE)); 476 477 WARN_ON(kprobe_disabled(&op->kp)); 478 479 /* Backup instructions which will be replaced by jump address */ 480 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_INSN_SIZE, 481 DISP32_SIZE); 482 483 insn_buff[0] = JMP32_INSN_OPCODE; 484 *(s32 *)(&insn_buff[1]) = rel; 485 486 smp_text_poke_single(op->kp.addr, insn_buff, JMP32_INSN_SIZE, NULL); 487 488 list_del_init(&op->list); 489 } 490 } 491 492 /* 493 * Replace a relative jump (JMP.d32) with a breakpoint (INT3). 494 * 495 * After that, we can restore the 4 bytes after the INT3 to undo what 496 * arch_optimize_kprobes() scribbled. This is safe since those bytes will be 497 * unused once the INT3 lands. 498 */ 499 void arch_unoptimize_kprobe(struct optimized_kprobe *op) 500 { 501 u8 new[JMP32_INSN_SIZE] = { INT3_INSN_OPCODE, }; 502 u8 old[JMP32_INSN_SIZE]; 503 u8 *addr = op->kp.addr; 504 505 memcpy(old, op->kp.addr, JMP32_INSN_SIZE); 506 memcpy(new + INT3_INSN_SIZE, 507 op->optinsn.copied_insn, 508 JMP32_INSN_SIZE - INT3_INSN_SIZE); 509 510 text_poke(addr, new, INT3_INSN_SIZE); 511 smp_text_poke_sync_each_cpu(); 512 text_poke(addr + INT3_INSN_SIZE, 513 new + INT3_INSN_SIZE, 514 JMP32_INSN_SIZE - INT3_INSN_SIZE); 515 smp_text_poke_sync_each_cpu(); 516 517 perf_event_text_poke(op->kp.addr, old, JMP32_INSN_SIZE, new, JMP32_INSN_SIZE); 518 } 519 520 /* 521 * Recover original instructions and breakpoints from relative jumps. 522 * Caller must call with locking kprobe_mutex. 523 */ 524 extern void arch_unoptimize_kprobes(struct list_head *oplist, 525 struct list_head *done_list) 526 { 527 struct optimized_kprobe *op, *tmp; 528 529 list_for_each_entry_safe(op, tmp, oplist, list) { 530 arch_unoptimize_kprobe(op); 531 list_move(&op->list, done_list); 532 } 533 } 534 535 int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter) 536 { 537 struct optimized_kprobe *op; 538 539 if (p->flags & KPROBE_FLAG_OPTIMIZED) { 540 /* This kprobe is really able to run optimized path. */ 541 op = container_of(p, struct optimized_kprobe, kp); 542 /* Detour through copied instructions */ 543 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX; 544 if (!reenter) 545 reset_current_kprobe(); 546 return 1; 547 } 548 return 0; 549 } 550 NOKPROBE_SYMBOL(setup_detour_execution); 551