patch-scs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 - Google LLC
  4. * Author: Ard Biesheuvel <ardb@google.com>
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/linkage.h>
  9. #include <linux/types.h>
  10. #include <asm/scs.h>
  11. #include "pi.h"
  12. bool dynamic_scs_is_enabled;
  13. //
  14. // This minimal DWARF CFI parser is partially based on the code in
  15. // arch/arc/kernel/unwind.c, and on the document below:
  16. // https://refspecs.linuxbase.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
  17. //
  18. #define DW_CFA_nop 0x00
  19. #define DW_CFA_set_loc 0x01
  20. #define DW_CFA_advance_loc1 0x02
  21. #define DW_CFA_advance_loc2 0x03
  22. #define DW_CFA_advance_loc4 0x04
  23. #define DW_CFA_offset_extended 0x05
  24. #define DW_CFA_restore_extended 0x06
  25. #define DW_CFA_undefined 0x07
  26. #define DW_CFA_same_value 0x08
  27. #define DW_CFA_register 0x09
  28. #define DW_CFA_remember_state 0x0a
  29. #define DW_CFA_restore_state 0x0b
  30. #define DW_CFA_def_cfa 0x0c
  31. #define DW_CFA_def_cfa_register 0x0d
  32. #define DW_CFA_def_cfa_offset 0x0e
  33. #define DW_CFA_def_cfa_expression 0x0f
  34. #define DW_CFA_expression 0x10
  35. #define DW_CFA_offset_extended_sf 0x11
  36. #define DW_CFA_def_cfa_sf 0x12
  37. #define DW_CFA_def_cfa_offset_sf 0x13
  38. #define DW_CFA_val_offset 0x14
  39. #define DW_CFA_val_offset_sf 0x15
  40. #define DW_CFA_val_expression 0x16
  41. #define DW_CFA_lo_user 0x1c
  42. #define DW_CFA_negate_ra_state 0x2d
  43. #define DW_CFA_GNU_args_size 0x2e
  44. #define DW_CFA_GNU_negative_offset_extended 0x2f
  45. #define DW_CFA_hi_user 0x3f
  46. enum {
  47. PACIASP = 0xd503233f,
  48. AUTIASP = 0xd50323bf,
  49. SCS_PUSH = 0xf800865e,
  50. SCS_POP = 0xf85f8e5e,
  51. };
  52. static void __always_inline scs_patch_loc(u64 loc)
  53. {
  54. u32 insn = le32_to_cpup((void *)loc);
  55. switch (insn) {
  56. case PACIASP:
  57. *(u32 *)loc = cpu_to_le32(SCS_PUSH);
  58. break;
  59. case AUTIASP:
  60. *(u32 *)loc = cpu_to_le32(SCS_POP);
  61. break;
  62. default:
  63. /*
  64. * While the DW_CFA_negate_ra_state directive is guaranteed to
  65. * appear right after a PACIASP/AUTIASP instruction, it may
  66. * also appear after a DW_CFA_restore_state directive that
  67. * restores a state that is only partially accurate, and is
  68. * followed by DW_CFA_negate_ra_state directive to toggle the
  69. * PAC bit again. So we permit other instructions here, and ignore
  70. * them.
  71. */
  72. return;
  73. }
  74. if (IS_ENABLED(CONFIG_ARM64_WORKAROUND_CLEAN_CACHE))
  75. asm("dc civac, %0" :: "r"(loc));
  76. else
  77. asm(ALTERNATIVE("dc cvau, %0", "nop", ARM64_HAS_CACHE_IDC)
  78. :: "r"(loc));
  79. }
  80. /*
  81. * Skip one uleb128/sleb128 encoded quantity from the opcode stream. All bytes
  82. * except the last one have bit #7 set.
  83. */
  84. static int __always_inline skip_xleb128(const u8 **opcode, int size)
  85. {
  86. u8 c;
  87. do {
  88. c = *(*opcode)++;
  89. size--;
  90. } while (c & BIT(7));
  91. return size;
  92. }
  93. struct eh_frame {
  94. /*
  95. * The size of this frame if 0 < size < U32_MAX, 0 terminates the list.
  96. */
  97. u32 size;
  98. /*
  99. * The first frame is a Common Information Entry (CIE) frame, followed
  100. * by one or more Frame Description Entry (FDE) frames. In the former
  101. * case, this field is 0, otherwise it is the negated offset relative
  102. * to the associated CIE frame.
  103. */
  104. u32 cie_id_or_pointer;
  105. union {
  106. struct { // CIE
  107. u8 version;
  108. u8 augmentation_string[];
  109. };
  110. struct { // FDE
  111. s32 initial_loc;
  112. s32 range;
  113. u8 opcodes[];
  114. };
  115. };
  116. };
  117. static int scs_handle_fde_frame(const struct eh_frame *frame,
  118. bool fde_has_augmentation_data,
  119. int code_alignment_factor,
  120. bool dry_run)
  121. {
  122. int size = frame->size - offsetof(struct eh_frame, opcodes) + 4;
  123. u64 loc = (u64)offset_to_ptr(&frame->initial_loc);
  124. const u8 *opcode = frame->opcodes;
  125. if (fde_has_augmentation_data) {
  126. int l;
  127. // assume single byte uleb128_t
  128. if (WARN_ON(*opcode & BIT(7)))
  129. return -ENOEXEC;
  130. l = *opcode++;
  131. opcode += l;
  132. size -= l + 1;
  133. }
  134. /*
  135. * Starting from 'loc', apply the CFA opcodes that advance the location
  136. * pointer, and identify the locations of the PAC instructions.
  137. */
  138. while (size-- > 0) {
  139. switch (*opcode++) {
  140. case DW_CFA_nop:
  141. case DW_CFA_remember_state:
  142. case DW_CFA_restore_state:
  143. break;
  144. case DW_CFA_advance_loc1:
  145. loc += *opcode++ * code_alignment_factor;
  146. size--;
  147. break;
  148. case DW_CFA_advance_loc2:
  149. loc += *opcode++ * code_alignment_factor;
  150. loc += (*opcode++ << 8) * code_alignment_factor;
  151. size -= 2;
  152. break;
  153. case DW_CFA_def_cfa:
  154. case DW_CFA_offset_extended:
  155. size = skip_xleb128(&opcode, size);
  156. fallthrough;
  157. case DW_CFA_def_cfa_offset:
  158. case DW_CFA_def_cfa_offset_sf:
  159. case DW_CFA_def_cfa_register:
  160. case DW_CFA_same_value:
  161. case DW_CFA_restore_extended:
  162. case 0x80 ... 0xbf:
  163. size = skip_xleb128(&opcode, size);
  164. break;
  165. case DW_CFA_negate_ra_state:
  166. if (!dry_run)
  167. scs_patch_loc(loc - 4);
  168. break;
  169. case 0x40 ... 0x7f:
  170. // advance loc
  171. loc += (opcode[-1] & 0x3f) * code_alignment_factor;
  172. break;
  173. case 0xc0 ... 0xff:
  174. break;
  175. default:
  176. return -ENOEXEC;
  177. }
  178. }
  179. return 0;
  180. }
  181. int scs_patch(const u8 eh_frame[], int size)
  182. {
  183. const u8 *p = eh_frame;
  184. while (size > 4) {
  185. const struct eh_frame *frame = (const void *)p;
  186. bool fde_has_augmentation_data = true;
  187. int code_alignment_factor = 1;
  188. int ret;
  189. if (frame->size == 0 ||
  190. frame->size == U32_MAX ||
  191. frame->size > size)
  192. break;
  193. if (frame->cie_id_or_pointer == 0) {
  194. const u8 *p = frame->augmentation_string;
  195. /* a 'z' in the augmentation string must come first */
  196. fde_has_augmentation_data = *p == 'z';
  197. /*
  198. * The code alignment factor is a uleb128 encoded field
  199. * but given that the only sensible values are 1 or 4,
  200. * there is no point in decoding the whole thing.
  201. */
  202. p += strlen(p) + 1;
  203. if (!WARN_ON(*p & BIT(7)))
  204. code_alignment_factor = *p;
  205. } else {
  206. ret = scs_handle_fde_frame(frame,
  207. fde_has_augmentation_data,
  208. code_alignment_factor,
  209. true);
  210. if (ret)
  211. return ret;
  212. scs_handle_fde_frame(frame, fde_has_augmentation_data,
  213. code_alignment_factor, false);
  214. }
  215. p += sizeof(frame->size) + frame->size;
  216. size -= sizeof(frame->size) + frame->size;
  217. }
  218. return 0;
  219. }