cfi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Clang Control Flow Integrity (CFI) support.
  4. *
  5. * Copyright (C) 2023 Google LLC
  6. */
  7. #include <linux/cfi.h>
  8. #include <asm/insn.h>
  9. /*
  10. * Returns the target address and the expected type when regs->epc points
  11. * to a compiler-generated CFI trap.
  12. */
  13. static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,
  14. u32 *type)
  15. {
  16. unsigned long *regs_ptr = (unsigned long *)regs;
  17. int rs1_num;
  18. u32 insn;
  19. *target = *type = 0;
  20. /*
  21. * The compiler generates the following instruction sequence
  22. * for indirect call checks:
  23. *
  24. *   lw t1, -4(<reg>)
  25. * lui t2, <hi20>
  26. * addiw t2, t2, <lo12>
  27. * beq t1, t2, .Ltmp1
  28. * ebreak ; <- regs->epc
  29. * .Ltmp1:
  30. * jalr <reg>
  31. *
  32. * We can read the expected type and the target address from the
  33. * registers passed to the beq/jalr instructions.
  34. */
  35. if (get_kernel_nofault(insn, (void *)regs->epc - 4))
  36. return false;
  37. if (!riscv_insn_is_beq(insn))
  38. return false;
  39. *type = (u32)regs_ptr[RV_EXTRACT_RS1_REG(insn)];
  40. if (get_kernel_nofault(insn, (void *)regs->epc) ||
  41. get_kernel_nofault(insn, (void *)regs->epc + GET_INSN_LENGTH(insn)))
  42. return false;
  43. if (riscv_insn_is_jalr(insn))
  44. rs1_num = RV_EXTRACT_RS1_REG(insn);
  45. else if (riscv_insn_is_c_jalr(insn))
  46. rs1_num = RVC_EXTRACT_C2_RS1_REG(insn);
  47. else
  48. return false;
  49. *target = regs_ptr[rs1_num];
  50. return true;
  51. }
  52. /*
  53. * Checks if the ebreak trap is because of a CFI failure, and handles the trap
  54. * if needed. Returns a bug_trap_type value similarly to report_bug.
  55. */
  56. enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
  57. {
  58. unsigned long target;
  59. u32 type;
  60. if (!is_cfi_trap(regs->epc))
  61. return BUG_TRAP_TYPE_NONE;
  62. if (!decode_cfi_insn(regs, &target, &type))
  63. return report_cfi_failure_noaddr(regs, regs->epc);
  64. return report_cfi_failure(regs, regs->epc, &target, type);
  65. }
  66. #ifdef CONFIG_CFI_CLANG
  67. struct bpf_insn;
  68. /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
  69. extern unsigned int __bpf_prog_runX(const void *ctx,
  70. const struct bpf_insn *insn);
  71. /*
  72. * Force a reference to the external symbol so the compiler generates
  73. * __kcfi_typid.
  74. */
  75. __ADDRESSABLE(__bpf_prog_runX);
  76. /* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
  77. asm (
  78. " .pushsection .data..ro_after_init,\"aw\",@progbits \n"
  79. " .type cfi_bpf_hash,@object \n"
  80. " .globl cfi_bpf_hash \n"
  81. " .p2align 2, 0x0 \n"
  82. "cfi_bpf_hash: \n"
  83. " .word __kcfi_typeid___bpf_prog_runX \n"
  84. " .size cfi_bpf_hash, 4 \n"
  85. " .popsection \n"
  86. );
  87. /* Must match bpf_callback_t */
  88. extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
  89. __ADDRESSABLE(__bpf_callback_fn);
  90. /* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
  91. asm (
  92. " .pushsection .data..ro_after_init,\"aw\",@progbits \n"
  93. " .type cfi_bpf_subprog_hash,@object \n"
  94. " .globl cfi_bpf_subprog_hash \n"
  95. " .p2align 2, 0x0 \n"
  96. "cfi_bpf_subprog_hash: \n"
  97. " .word __kcfi_typeid___bpf_callback_fn \n"
  98. " .size cfi_bpf_subprog_hash, 4 \n"
  99. " .popsection \n"
  100. );
  101. u32 cfi_get_func_hash(void *func)
  102. {
  103. u32 hash;
  104. if (get_kernel_nofault(hash, func - cfi_get_offset()))
  105. return 0;
  106. return hash;
  107. }
  108. #endif