module-plts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014-2017 Linaro Ltd. <[email protected]>
  4. */
  5. #include <linux/elf.h>
  6. #include <linux/ftrace.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleloader.h>
  10. #include <linux/sort.h>
  11. static struct plt_entry __get_adrp_add_pair(u64 dst, u64 pc,
  12. enum aarch64_insn_register reg)
  13. {
  14. u32 adrp, add;
  15. adrp = aarch64_insn_gen_adr(pc, dst, reg, AARCH64_INSN_ADR_TYPE_ADRP);
  16. add = aarch64_insn_gen_add_sub_imm(reg, reg, dst % SZ_4K,
  17. AARCH64_INSN_VARIANT_64BIT,
  18. AARCH64_INSN_ADSB_ADD);
  19. return (struct plt_entry){ cpu_to_le32(adrp), cpu_to_le32(add) };
  20. }
  21. struct plt_entry get_plt_entry(u64 dst, void *pc)
  22. {
  23. struct plt_entry plt;
  24. static u32 br;
  25. if (!br)
  26. br = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_16,
  27. AARCH64_INSN_BRANCH_NOLINK);
  28. plt = __get_adrp_add_pair(dst, (u64)pc, AARCH64_INSN_REG_16);
  29. plt.br = cpu_to_le32(br);
  30. return plt;
  31. }
  32. static bool plt_entries_equal(const struct plt_entry *a,
  33. const struct plt_entry *b)
  34. {
  35. u64 p, q;
  36. /*
  37. * Check whether both entries refer to the same target:
  38. * do the cheapest checks first.
  39. * If the 'add' or 'br' opcodes are different, then the target
  40. * cannot be the same.
  41. */
  42. if (a->add != b->add || a->br != b->br)
  43. return false;
  44. p = ALIGN_DOWN((u64)a, SZ_4K);
  45. q = ALIGN_DOWN((u64)b, SZ_4K);
  46. /*
  47. * If the 'adrp' opcodes are the same then we just need to check
  48. * that they refer to the same 4k region.
  49. */
  50. if (a->adrp == b->adrp && p == q)
  51. return true;
  52. return (p + aarch64_insn_adrp_get_offset(le32_to_cpu(a->adrp))) ==
  53. (q + aarch64_insn_adrp_get_offset(le32_to_cpu(b->adrp)));
  54. }
  55. static bool in_init(const struct module *mod, void *loc)
  56. {
  57. return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
  58. }
  59. u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
  60. void *loc, const Elf64_Rela *rela,
  61. Elf64_Sym *sym)
  62. {
  63. struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
  64. &mod->arch.init;
  65. struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
  66. int i = pltsec->plt_num_entries;
  67. int j = i - 1;
  68. u64 val = sym->st_value + rela->r_addend;
  69. if (is_forbidden_offset_for_adrp(&plt[i].adrp))
  70. i++;
  71. plt[i] = get_plt_entry(val, &plt[i]);
  72. /*
  73. * Check if the entry we just created is a duplicate. Given that the
  74. * relocations are sorted, this will be the last entry we allocated.
  75. * (if one exists).
  76. */
  77. if (j >= 0 && plt_entries_equal(plt + i, plt + j))
  78. return (u64)&plt[j];
  79. pltsec->plt_num_entries += i - j;
  80. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  81. return 0;
  82. return (u64)&plt[i];
  83. }
  84. #ifdef CONFIG_ARM64_ERRATUM_843419
  85. u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs,
  86. void *loc, u64 val)
  87. {
  88. struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
  89. &mod->arch.init;
  90. struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
  91. int i = pltsec->plt_num_entries++;
  92. u32 br;
  93. int rd;
  94. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  95. return 0;
  96. if (is_forbidden_offset_for_adrp(&plt[i].adrp))
  97. i = pltsec->plt_num_entries++;
  98. /* get the destination register of the ADRP instruction */
  99. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD,
  100. le32_to_cpup((__le32 *)loc));
  101. br = aarch64_insn_gen_branch_imm((u64)&plt[i].br, (u64)loc + 4,
  102. AARCH64_INSN_BRANCH_NOLINK);
  103. plt[i] = __get_adrp_add_pair(val, (u64)&plt[i], rd);
  104. plt[i].br = cpu_to_le32(br);
  105. return (u64)&plt[i];
  106. }
  107. #endif
  108. #define cmp_3way(a, b) ((a) < (b) ? -1 : (a) > (b))
  109. static int cmp_rela(const void *a, const void *b)
  110. {
  111. const Elf64_Rela *x = a, *y = b;
  112. int i;
  113. /* sort by type, symbol index and addend */
  114. i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
  115. if (i == 0)
  116. i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
  117. if (i == 0)
  118. i = cmp_3way(x->r_addend, y->r_addend);
  119. return i;
  120. }
  121. static bool duplicate_rel(const Elf64_Rela *rela, int num)
  122. {
  123. /*
  124. * Entries are sorted by type, symbol index and addend. That means
  125. * that, if a duplicate entry exists, it must be in the preceding
  126. * slot.
  127. */
  128. return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
  129. }
  130. static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
  131. Elf64_Word dstidx, Elf_Shdr *dstsec)
  132. {
  133. unsigned int ret = 0;
  134. Elf64_Sym *s;
  135. int i;
  136. for (i = 0; i < num; i++) {
  137. u64 min_align;
  138. switch (ELF64_R_TYPE(rela[i].r_info)) {
  139. case R_AARCH64_JUMP26:
  140. case R_AARCH64_CALL26:
  141. if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
  142. break;
  143. /*
  144. * We only have to consider branch targets that resolve
  145. * to symbols that are defined in a different section.
  146. * This is not simply a heuristic, it is a fundamental
  147. * limitation, since there is no guaranteed way to emit
  148. * PLT entries sufficiently close to the branch if the
  149. * section size exceeds the range of a branch
  150. * instruction. So ignore relocations against defined
  151. * symbols if they live in the same section as the
  152. * relocation target.
  153. */
  154. s = syms + ELF64_R_SYM(rela[i].r_info);
  155. if (s->st_shndx == dstidx)
  156. break;
  157. /*
  158. * Jump relocations with non-zero addends against
  159. * undefined symbols are supported by the ELF spec, but
  160. * do not occur in practice (e.g., 'jump n bytes past
  161. * the entry point of undefined function symbol f').
  162. * So we need to support them, but there is no need to
  163. * take them into consideration when trying to optimize
  164. * this code. So let's only check for duplicates when
  165. * the addend is zero: this allows us to record the PLT
  166. * entry address in the symbol table itself, rather than
  167. * having to search the list for duplicates each time we
  168. * emit one.
  169. */
  170. if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
  171. ret++;
  172. break;
  173. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  174. case R_AARCH64_ADR_PREL_PG_HI21:
  175. if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) ||
  176. !cpus_have_const_cap(ARM64_WORKAROUND_843419))
  177. break;
  178. /*
  179. * Determine the minimal safe alignment for this ADRP
  180. * instruction: the section alignment at which it is
  181. * guaranteed not to appear at a vulnerable offset.
  182. *
  183. * This comes down to finding the least significant zero
  184. * bit in bits [11:3] of the section offset, and
  185. * increasing the section's alignment so that the
  186. * resulting address of this instruction is guaranteed
  187. * to equal the offset in that particular bit (as well
  188. * as all less significant bits). This ensures that the
  189. * address modulo 4 KB != 0xfff8 or 0xfffc (which would
  190. * have all ones in bits [11:3])
  191. */
  192. min_align = 2ULL << ffz(rela[i].r_offset | 0x7);
  193. /*
  194. * Allocate veneer space for each ADRP that may appear
  195. * at a vulnerable offset nonetheless. At relocation
  196. * time, some of these will remain unused since some
  197. * ADRP instructions can be patched to ADR instructions
  198. * instead.
  199. */
  200. if (min_align > SZ_4K)
  201. ret++;
  202. else
  203. dstsec->sh_addralign = max(dstsec->sh_addralign,
  204. min_align);
  205. break;
  206. }
  207. }
  208. if (IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) &&
  209. cpus_have_const_cap(ARM64_WORKAROUND_843419))
  210. /*
  211. * Add some slack so we can skip PLT slots that may trigger
  212. * the erratum due to the placement of the ADRP instruction.
  213. */
  214. ret += DIV_ROUND_UP(ret, (SZ_4K / sizeof(struct plt_entry)));
  215. return ret;
  216. }
  217. static bool branch_rela_needs_plt(Elf64_Sym *syms, Elf64_Rela *rela,
  218. Elf64_Word dstidx)
  219. {
  220. Elf64_Sym *s = syms + ELF64_R_SYM(rela->r_info);
  221. if (s->st_shndx == dstidx)
  222. return false;
  223. return ELF64_R_TYPE(rela->r_info) == R_AARCH64_JUMP26 ||
  224. ELF64_R_TYPE(rela->r_info) == R_AARCH64_CALL26;
  225. }
  226. /* Group branch PLT relas at the front end of the array. */
  227. static int partition_branch_plt_relas(Elf64_Sym *syms, Elf64_Rela *rela,
  228. int numrels, Elf64_Word dstidx)
  229. {
  230. int i = 0, j = numrels - 1;
  231. if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
  232. return 0;
  233. while (i < j) {
  234. if (branch_rela_needs_plt(syms, &rela[i], dstidx))
  235. i++;
  236. else if (branch_rela_needs_plt(syms, &rela[j], dstidx))
  237. swap(rela[i], rela[j]);
  238. else
  239. j--;
  240. }
  241. return i;
  242. }
  243. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  244. char *secstrings, struct module *mod)
  245. {
  246. unsigned long core_plts = 0;
  247. unsigned long init_plts = 0;
  248. Elf64_Sym *syms = NULL;
  249. Elf_Shdr *pltsec, *tramp = NULL;
  250. int i;
  251. /*
  252. * Find the empty .plt section so we can expand it to store the PLT
  253. * entries. Record the symtab address as well.
  254. */
  255. for (i = 0; i < ehdr->e_shnum; i++) {
  256. if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
  257. mod->arch.core.plt_shndx = i;
  258. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
  259. mod->arch.init.plt_shndx = i;
  260. else if (!strcmp(secstrings + sechdrs[i].sh_name,
  261. ".text.ftrace_trampoline"))
  262. tramp = sechdrs + i;
  263. else if (sechdrs[i].sh_type == SHT_SYMTAB)
  264. syms = (Elf64_Sym *)sechdrs[i].sh_addr;
  265. }
  266. if (!mod->arch.core.plt_shndx || !mod->arch.init.plt_shndx) {
  267. pr_err("%s: module PLT section(s) missing\n", mod->name);
  268. return -ENOEXEC;
  269. }
  270. if (!syms) {
  271. pr_err("%s: module symtab section missing\n", mod->name);
  272. return -ENOEXEC;
  273. }
  274. for (i = 0; i < ehdr->e_shnum; i++) {
  275. Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
  276. int nents, numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
  277. Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
  278. if (sechdrs[i].sh_type != SHT_RELA)
  279. continue;
  280. /* ignore relocations that operate on non-exec sections */
  281. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  282. continue;
  283. /*
  284. * sort branch relocations requiring a PLT by type, symbol index
  285. * and addend
  286. */
  287. nents = partition_branch_plt_relas(syms, rels, numrels,
  288. sechdrs[i].sh_info);
  289. if (nents)
  290. sort(rels, nents, sizeof(Elf64_Rela), cmp_rela, NULL);
  291. if (!module_init_layout_section(secstrings + dstsec->sh_name))
  292. core_plts += count_plts(syms, rels, numrels,
  293. sechdrs[i].sh_info, dstsec);
  294. else
  295. init_plts += count_plts(syms, rels, numrels,
  296. sechdrs[i].sh_info, dstsec);
  297. }
  298. pltsec = sechdrs + mod->arch.core.plt_shndx;
  299. pltsec->sh_type = SHT_NOBITS;
  300. pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  301. pltsec->sh_addralign = L1_CACHE_BYTES;
  302. pltsec->sh_size = (core_plts + 1) * sizeof(struct plt_entry);
  303. mod->arch.core.plt_num_entries = 0;
  304. mod->arch.core.plt_max_entries = core_plts;
  305. pltsec = sechdrs + mod->arch.init.plt_shndx;
  306. pltsec->sh_type = SHT_NOBITS;
  307. pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  308. pltsec->sh_addralign = L1_CACHE_BYTES;
  309. pltsec->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
  310. mod->arch.init.plt_num_entries = 0;
  311. mod->arch.init.plt_max_entries = init_plts;
  312. if (tramp) {
  313. tramp->sh_type = SHT_NOBITS;
  314. tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  315. tramp->sh_addralign = __alignof__(struct plt_entry);
  316. tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);
  317. }
  318. return 0;
  319. }