module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AArch64 loadable module support.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. *
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. */
  9. #define pr_fmt(fmt) "Modules: " fmt
  10. #include <linux/bitops.h>
  11. #include <linux/elf.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/kasan.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/moduleloader.h>
  17. #include <linux/random.h>
  18. #include <linux/scs.h>
  19. #include <asm/alternative.h>
  20. #include <asm/insn.h>
  21. #include <asm/scs.h>
  22. #include <asm/sections.h>
  23. enum aarch64_reloc_op {
  24. RELOC_OP_NONE,
  25. RELOC_OP_ABS,
  26. RELOC_OP_PREL,
  27. RELOC_OP_PAGE,
  28. };
  29. static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
  30. {
  31. switch (reloc_op) {
  32. case RELOC_OP_ABS:
  33. return val;
  34. case RELOC_OP_PREL:
  35. return val - (u64)place;
  36. case RELOC_OP_PAGE:
  37. return (val & ~0xfff) - ((u64)place & ~0xfff);
  38. case RELOC_OP_NONE:
  39. return 0;
  40. }
  41. pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
  42. return 0;
  43. }
  44. static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
  45. {
  46. s64 sval = do_reloc(op, place, val);
  47. /*
  48. * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
  49. * relative and absolute relocations as having a range of [-2^15, 2^16)
  50. * or [-2^31, 2^32), respectively. However, in order to be able to
  51. * detect overflows reliably, we have to choose whether we interpret
  52. * such quantities as signed or as unsigned, and stick with it.
  53. * The way we organize our address space requires a signed
  54. * interpretation of 32-bit relative references, so let's use that
  55. * for all R_AARCH64_PRELxx relocations. This means our upper
  56. * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
  57. */
  58. switch (len) {
  59. case 16:
  60. *(s16 *)place = sval;
  61. switch (op) {
  62. case RELOC_OP_ABS:
  63. if (sval < 0 || sval > U16_MAX)
  64. return -ERANGE;
  65. break;
  66. case RELOC_OP_PREL:
  67. if (sval < S16_MIN || sval > S16_MAX)
  68. return -ERANGE;
  69. break;
  70. default:
  71. pr_err("Invalid 16-bit data relocation (%d)\n", op);
  72. return 0;
  73. }
  74. break;
  75. case 32:
  76. *(s32 *)place = sval;
  77. switch (op) {
  78. case RELOC_OP_ABS:
  79. if (sval < 0 || sval > U32_MAX)
  80. return -ERANGE;
  81. break;
  82. case RELOC_OP_PREL:
  83. if (sval < S32_MIN || sval > S32_MAX)
  84. return -ERANGE;
  85. break;
  86. default:
  87. pr_err("Invalid 32-bit data relocation (%d)\n", op);
  88. return 0;
  89. }
  90. break;
  91. case 64:
  92. *(s64 *)place = sval;
  93. break;
  94. default:
  95. pr_err("Invalid length (%d) for data relocation\n", len);
  96. return 0;
  97. }
  98. return 0;
  99. }
  100. enum aarch64_insn_movw_imm_type {
  101. AARCH64_INSN_IMM_MOVNZ,
  102. AARCH64_INSN_IMM_MOVKZ,
  103. };
  104. static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
  105. int lsb, enum aarch64_insn_movw_imm_type imm_type)
  106. {
  107. u64 imm;
  108. s64 sval;
  109. u32 insn = le32_to_cpu(*place);
  110. sval = do_reloc(op, place, val);
  111. imm = sval >> lsb;
  112. if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
  113. /*
  114. * For signed MOVW relocations, we have to manipulate the
  115. * instruction encoding depending on whether or not the
  116. * immediate is less than zero.
  117. */
  118. insn &= ~(3 << 29);
  119. if (sval >= 0) {
  120. /* >=0: Set the instruction to MOVZ (opcode 10b). */
  121. insn |= 2 << 29;
  122. } else {
  123. /*
  124. * <0: Set the instruction to MOVN (opcode 00b).
  125. * Since we've masked the opcode already, we
  126. * don't need to do anything other than
  127. * inverting the new immediate field.
  128. */
  129. imm = ~imm;
  130. }
  131. }
  132. /* Update the instruction with the new encoding. */
  133. insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
  134. *place = cpu_to_le32(insn);
  135. if (imm > U16_MAX)
  136. return -ERANGE;
  137. return 0;
  138. }
  139. static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
  140. int lsb, int len, enum aarch64_insn_imm_type imm_type)
  141. {
  142. u64 imm, imm_mask;
  143. s64 sval;
  144. u32 insn = le32_to_cpu(*place);
  145. /* Calculate the relocation value. */
  146. sval = do_reloc(op, place, val);
  147. sval >>= lsb;
  148. /* Extract the value bits and shift them to bit 0. */
  149. imm_mask = (BIT(lsb + len) - 1) >> lsb;
  150. imm = sval & imm_mask;
  151. /* Update the instruction's immediate field. */
  152. insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
  153. *place = cpu_to_le32(insn);
  154. /*
  155. * Extract the upper value bits (including the sign bit) and
  156. * shift them to bit 0.
  157. */
  158. sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
  159. /*
  160. * Overflow has occurred if the upper bits are not all equal to
  161. * the sign bit of the value.
  162. */
  163. if ((u64)(sval + 1) >= 2)
  164. return -ERANGE;
  165. return 0;
  166. }
  167. static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
  168. __le32 *place, u64 val)
  169. {
  170. u32 insn;
  171. if (!is_forbidden_offset_for_adrp(place))
  172. return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
  173. AARCH64_INSN_IMM_ADR);
  174. /* patch ADRP to ADR if it is in range */
  175. if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
  176. AARCH64_INSN_IMM_ADR)) {
  177. insn = le32_to_cpu(*place);
  178. insn &= ~BIT(31);
  179. } else {
  180. /* out of range for ADR -> emit a veneer */
  181. val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
  182. if (!val)
  183. return -ENOEXEC;
  184. insn = aarch64_insn_gen_branch_imm((u64)place, val,
  185. AARCH64_INSN_BRANCH_NOLINK);
  186. }
  187. *place = cpu_to_le32(insn);
  188. return 0;
  189. }
  190. int apply_relocate_add(Elf64_Shdr *sechdrs,
  191. const char *strtab,
  192. unsigned int symindex,
  193. unsigned int relsec,
  194. struct module *me)
  195. {
  196. unsigned int i;
  197. int ovf;
  198. bool overflow_check;
  199. Elf64_Sym *sym;
  200. void *loc;
  201. u64 val;
  202. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  203. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  204. /* loc corresponds to P in the AArch64 ELF document. */
  205. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  206. + rel[i].r_offset;
  207. /* sym is the ELF symbol we're referring to. */
  208. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  209. + ELF64_R_SYM(rel[i].r_info);
  210. /* val corresponds to (S + A) in the AArch64 ELF document. */
  211. val = sym->st_value + rel[i].r_addend;
  212. /* Check for overflow by default. */
  213. overflow_check = true;
  214. /* Perform the static relocation. */
  215. switch (ELF64_R_TYPE(rel[i].r_info)) {
  216. /* Null relocations. */
  217. case R_ARM_NONE:
  218. case R_AARCH64_NONE:
  219. ovf = 0;
  220. break;
  221. /* Data relocations. */
  222. case R_AARCH64_ABS64:
  223. overflow_check = false;
  224. ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
  225. break;
  226. case R_AARCH64_ABS32:
  227. ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
  228. break;
  229. case R_AARCH64_ABS16:
  230. ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
  231. break;
  232. case R_AARCH64_PREL64:
  233. overflow_check = false;
  234. ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
  235. break;
  236. case R_AARCH64_PREL32:
  237. ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
  238. break;
  239. case R_AARCH64_PREL16:
  240. ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
  241. break;
  242. /* MOVW instruction relocations. */
  243. case R_AARCH64_MOVW_UABS_G0_NC:
  244. overflow_check = false;
  245. fallthrough;
  246. case R_AARCH64_MOVW_UABS_G0:
  247. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  248. AARCH64_INSN_IMM_MOVKZ);
  249. break;
  250. case R_AARCH64_MOVW_UABS_G1_NC:
  251. overflow_check = false;
  252. fallthrough;
  253. case R_AARCH64_MOVW_UABS_G1:
  254. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  255. AARCH64_INSN_IMM_MOVKZ);
  256. break;
  257. case R_AARCH64_MOVW_UABS_G2_NC:
  258. overflow_check = false;
  259. fallthrough;
  260. case R_AARCH64_MOVW_UABS_G2:
  261. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  262. AARCH64_INSN_IMM_MOVKZ);
  263. break;
  264. case R_AARCH64_MOVW_UABS_G3:
  265. /* We're using the top bits so we can't overflow. */
  266. overflow_check = false;
  267. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
  268. AARCH64_INSN_IMM_MOVKZ);
  269. break;
  270. case R_AARCH64_MOVW_SABS_G0:
  271. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  272. AARCH64_INSN_IMM_MOVNZ);
  273. break;
  274. case R_AARCH64_MOVW_SABS_G1:
  275. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  276. AARCH64_INSN_IMM_MOVNZ);
  277. break;
  278. case R_AARCH64_MOVW_SABS_G2:
  279. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  280. AARCH64_INSN_IMM_MOVNZ);
  281. break;
  282. case R_AARCH64_MOVW_PREL_G0_NC:
  283. overflow_check = false;
  284. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  285. AARCH64_INSN_IMM_MOVKZ);
  286. break;
  287. case R_AARCH64_MOVW_PREL_G0:
  288. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  289. AARCH64_INSN_IMM_MOVNZ);
  290. break;
  291. case R_AARCH64_MOVW_PREL_G1_NC:
  292. overflow_check = false;
  293. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  294. AARCH64_INSN_IMM_MOVKZ);
  295. break;
  296. case R_AARCH64_MOVW_PREL_G1:
  297. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  298. AARCH64_INSN_IMM_MOVNZ);
  299. break;
  300. case R_AARCH64_MOVW_PREL_G2_NC:
  301. overflow_check = false;
  302. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  303. AARCH64_INSN_IMM_MOVKZ);
  304. break;
  305. case R_AARCH64_MOVW_PREL_G2:
  306. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  307. AARCH64_INSN_IMM_MOVNZ);
  308. break;
  309. case R_AARCH64_MOVW_PREL_G3:
  310. /* We're using the top bits so we can't overflow. */
  311. overflow_check = false;
  312. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
  313. AARCH64_INSN_IMM_MOVNZ);
  314. break;
  315. /* Immediate instruction relocations. */
  316. case R_AARCH64_LD_PREL_LO19:
  317. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  318. AARCH64_INSN_IMM_19);
  319. break;
  320. case R_AARCH64_ADR_PREL_LO21:
  321. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
  322. AARCH64_INSN_IMM_ADR);
  323. break;
  324. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  325. overflow_check = false;
  326. fallthrough;
  327. case R_AARCH64_ADR_PREL_PG_HI21:
  328. ovf = reloc_insn_adrp(me, sechdrs, loc, val);
  329. if (ovf && ovf != -ERANGE)
  330. return ovf;
  331. break;
  332. case R_AARCH64_ADD_ABS_LO12_NC:
  333. case R_AARCH64_LDST8_ABS_LO12_NC:
  334. overflow_check = false;
  335. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
  336. AARCH64_INSN_IMM_12);
  337. break;
  338. case R_AARCH64_LDST16_ABS_LO12_NC:
  339. overflow_check = false;
  340. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
  341. AARCH64_INSN_IMM_12);
  342. break;
  343. case R_AARCH64_LDST32_ABS_LO12_NC:
  344. overflow_check = false;
  345. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
  346. AARCH64_INSN_IMM_12);
  347. break;
  348. case R_AARCH64_LDST64_ABS_LO12_NC:
  349. overflow_check = false;
  350. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
  351. AARCH64_INSN_IMM_12);
  352. break;
  353. case R_AARCH64_LDST128_ABS_LO12_NC:
  354. overflow_check = false;
  355. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
  356. AARCH64_INSN_IMM_12);
  357. break;
  358. case R_AARCH64_TSTBR14:
  359. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
  360. AARCH64_INSN_IMM_14);
  361. break;
  362. case R_AARCH64_CONDBR19:
  363. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  364. AARCH64_INSN_IMM_19);
  365. break;
  366. case R_AARCH64_JUMP26:
  367. case R_AARCH64_CALL26:
  368. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
  369. AARCH64_INSN_IMM_26);
  370. if (ovf == -ERANGE) {
  371. val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
  372. if (!val)
  373. return -ENOEXEC;
  374. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
  375. 26, AARCH64_INSN_IMM_26);
  376. }
  377. break;
  378. default:
  379. pr_err("module %s: unsupported RELA relocation: %llu\n",
  380. me->name, ELF64_R_TYPE(rel[i].r_info));
  381. return -ENOEXEC;
  382. }
  383. if (overflow_check && ovf == -ERANGE)
  384. goto overflow;
  385. }
  386. return 0;
  387. overflow:
  388. pr_err("module %s: overflow in relocation type %d val %Lx\n",
  389. me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
  390. return -ENOEXEC;
  391. }
  392. static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
  393. {
  394. *plt = get_plt_entry(addr, plt);
  395. }
  396. static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
  397. const Elf_Shdr *sechdrs,
  398. struct module *mod)
  399. {
  400. #if defined(CONFIG_DYNAMIC_FTRACE)
  401. const Elf_Shdr *s;
  402. struct plt_entry *plts;
  403. s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
  404. if (!s)
  405. return -ENOEXEC;
  406. plts = (void *)s->sh_addr;
  407. __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
  408. mod->arch.ftrace_trampolines = plts;
  409. s = find_section(hdr, sechdrs, ".init.text.ftrace_trampoline");
  410. if (!s)
  411. return -ENOEXEC;
  412. plts = (void *)s->sh_addr;
  413. __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
  414. mod->arch.init_ftrace_trampolines = plts;
  415. #endif
  416. return 0;
  417. }
  418. int module_finalize(const Elf_Ehdr *hdr,
  419. const Elf_Shdr *sechdrs,
  420. struct module *me)
  421. {
  422. const Elf_Shdr *s;
  423. s = find_section(hdr, sechdrs, ".altinstructions");
  424. if (s)
  425. apply_alternatives_module((void *)s->sh_addr, s->sh_size);
  426. if (scs_is_dynamic()) {
  427. s = find_section(hdr, sechdrs, ".init.eh_frame");
  428. if (s)
  429. __pi_scs_patch((void *)s->sh_addr, s->sh_size);
  430. }
  431. return module_init_ftrace_plt(hdr, sechdrs, me);
  432. }