module-plts.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/elf.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sort.h>
  12. static bool in_init(const struct module *mod, void *loc)
  13. {
  14. return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
  15. }
  16. u64 module_emit_plt_entry(struct module *mod, void *loc, const Elf64_Rela *rela,
  17. Elf64_Sym *sym)
  18. {
  19. struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
  20. &mod->arch.init;
  21. struct plt_entry *plt = (struct plt_entry *)pltsec->plt->sh_addr;
  22. int i = pltsec->plt_num_entries;
  23. u64 val = sym->st_value + rela->r_addend;
  24. plt[i] = get_plt_entry(val);
  25. /*
  26. * Check if the entry we just created is a duplicate. Given that the
  27. * relocations are sorted, this will be the last entry we allocated.
  28. * (if one exists).
  29. */
  30. if (i > 0 && plt_entries_equal(plt + i, plt + i - 1))
  31. return (u64)&plt[i - 1];
  32. pltsec->plt_num_entries++;
  33. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  34. return 0;
  35. return (u64)&plt[i];
  36. }
  37. #ifdef CONFIG_ARM64_ERRATUM_843419
  38. u64 module_emit_veneer_for_adrp(struct module *mod, void *loc, u64 val)
  39. {
  40. struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
  41. &mod->arch.init;
  42. struct plt_entry *plt = (struct plt_entry *)pltsec->plt->sh_addr;
  43. int i = pltsec->plt_num_entries++;
  44. u32 mov0, mov1, mov2, br;
  45. int rd;
  46. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  47. return 0;
  48. /* get the destination register of the ADRP instruction */
  49. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD,
  50. le32_to_cpup((__le32 *)loc));
  51. /* generate the veneer instructions */
  52. mov0 = aarch64_insn_gen_movewide(rd, (u16)~val, 0,
  53. AARCH64_INSN_VARIANT_64BIT,
  54. AARCH64_INSN_MOVEWIDE_INVERSE);
  55. mov1 = aarch64_insn_gen_movewide(rd, (u16)(val >> 16), 16,
  56. AARCH64_INSN_VARIANT_64BIT,
  57. AARCH64_INSN_MOVEWIDE_KEEP);
  58. mov2 = aarch64_insn_gen_movewide(rd, (u16)(val >> 32), 32,
  59. AARCH64_INSN_VARIANT_64BIT,
  60. AARCH64_INSN_MOVEWIDE_KEEP);
  61. br = aarch64_insn_gen_branch_imm((u64)&plt[i].br, (u64)loc + 4,
  62. AARCH64_INSN_BRANCH_NOLINK);
  63. plt[i] = (struct plt_entry){
  64. cpu_to_le32(mov0),
  65. cpu_to_le32(mov1),
  66. cpu_to_le32(mov2),
  67. cpu_to_le32(br)
  68. };
  69. return (u64)&plt[i];
  70. }
  71. #endif
  72. #define cmp_3way(a,b) ((a) < (b) ? -1 : (a) > (b))
  73. static int cmp_rela(const void *a, const void *b)
  74. {
  75. const Elf64_Rela *x = a, *y = b;
  76. int i;
  77. /* sort by type, symbol index and addend */
  78. i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
  79. if (i == 0)
  80. i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
  81. if (i == 0)
  82. i = cmp_3way(x->r_addend, y->r_addend);
  83. return i;
  84. }
  85. static bool duplicate_rel(const Elf64_Rela *rela, int num)
  86. {
  87. /*
  88. * Entries are sorted by type, symbol index and addend. That means
  89. * that, if a duplicate entry exists, it must be in the preceding
  90. * slot.
  91. */
  92. return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
  93. }
  94. static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
  95. Elf64_Word dstidx, Elf_Shdr *dstsec)
  96. {
  97. unsigned int ret = 0;
  98. Elf64_Sym *s;
  99. int i;
  100. for (i = 0; i < num; i++) {
  101. u64 min_align;
  102. switch (ELF64_R_TYPE(rela[i].r_info)) {
  103. case R_AARCH64_JUMP26:
  104. case R_AARCH64_CALL26:
  105. if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
  106. break;
  107. /*
  108. * We only have to consider branch targets that resolve
  109. * to symbols that are defined in a different section.
  110. * This is not simply a heuristic, it is a fundamental
  111. * limitation, since there is no guaranteed way to emit
  112. * PLT entries sufficiently close to the branch if the
  113. * section size exceeds the range of a branch
  114. * instruction. So ignore relocations against defined
  115. * symbols if they live in the same section as the
  116. * relocation target.
  117. */
  118. s = syms + ELF64_R_SYM(rela[i].r_info);
  119. if (s->st_shndx == dstidx)
  120. break;
  121. /*
  122. * Jump relocations with non-zero addends against
  123. * undefined symbols are supported by the ELF spec, but
  124. * do not occur in practice (e.g., 'jump n bytes past
  125. * the entry point of undefined function symbol f').
  126. * So we need to support them, but there is no need to
  127. * take them into consideration when trying to optimize
  128. * this code. So let's only check for duplicates when
  129. * the addend is zero: this allows us to record the PLT
  130. * entry address in the symbol table itself, rather than
  131. * having to search the list for duplicates each time we
  132. * emit one.
  133. */
  134. if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
  135. ret++;
  136. break;
  137. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  138. case R_AARCH64_ADR_PREL_PG_HI21:
  139. if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) ||
  140. !cpus_have_const_cap(ARM64_WORKAROUND_843419))
  141. break;
  142. /*
  143. * Determine the minimal safe alignment for this ADRP
  144. * instruction: the section alignment at which it is
  145. * guaranteed not to appear at a vulnerable offset.
  146. *
  147. * This comes down to finding the least significant zero
  148. * bit in bits [11:3] of the section offset, and
  149. * increasing the section's alignment so that the
  150. * resulting address of this instruction is guaranteed
  151. * to equal the offset in that particular bit (as well
  152. * as all less signficant bits). This ensures that the
  153. * address modulo 4 KB != 0xfff8 or 0xfffc (which would
  154. * have all ones in bits [11:3])
  155. */
  156. min_align = 2ULL << ffz(rela[i].r_offset | 0x7);
  157. /*
  158. * Allocate veneer space for each ADRP that may appear
  159. * at a vulnerable offset nonetheless. At relocation
  160. * time, some of these will remain unused since some
  161. * ADRP instructions can be patched to ADR instructions
  162. * instead.
  163. */
  164. if (min_align > SZ_4K)
  165. ret++;
  166. else
  167. dstsec->sh_addralign = max(dstsec->sh_addralign,
  168. min_align);
  169. break;
  170. }
  171. }
  172. return ret;
  173. }
  174. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  175. char *secstrings, struct module *mod)
  176. {
  177. unsigned long core_plts = 0;
  178. unsigned long init_plts = 0;
  179. Elf64_Sym *syms = NULL;
  180. Elf_Shdr *tramp = NULL;
  181. int i;
  182. /*
  183. * Find the empty .plt section so we can expand it to store the PLT
  184. * entries. Record the symtab address as well.
  185. */
  186. for (i = 0; i < ehdr->e_shnum; i++) {
  187. if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
  188. mod->arch.core.plt = sechdrs + i;
  189. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
  190. mod->arch.init.plt = sechdrs + i;
  191. else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
  192. !strcmp(secstrings + sechdrs[i].sh_name,
  193. ".text.ftrace_trampoline"))
  194. tramp = sechdrs + i;
  195. else if (sechdrs[i].sh_type == SHT_SYMTAB)
  196. syms = (Elf64_Sym *)sechdrs[i].sh_addr;
  197. }
  198. if (!mod->arch.core.plt || !mod->arch.init.plt) {
  199. pr_err("%s: module PLT section(s) missing\n", mod->name);
  200. return -ENOEXEC;
  201. }
  202. if (!syms) {
  203. pr_err("%s: module symtab section missing\n", mod->name);
  204. return -ENOEXEC;
  205. }
  206. for (i = 0; i < ehdr->e_shnum; i++) {
  207. Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
  208. int numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
  209. Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
  210. if (sechdrs[i].sh_type != SHT_RELA)
  211. continue;
  212. /* ignore relocations that operate on non-exec sections */
  213. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  214. continue;
  215. /* sort by type, symbol index and addend */
  216. sort(rels, numrels, sizeof(Elf64_Rela), cmp_rela, NULL);
  217. if (strncmp(secstrings + dstsec->sh_name, ".init", 5) != 0)
  218. core_plts += count_plts(syms, rels, numrels,
  219. sechdrs[i].sh_info, dstsec);
  220. else
  221. init_plts += count_plts(syms, rels, numrels,
  222. sechdrs[i].sh_info, dstsec);
  223. }
  224. mod->arch.core.plt->sh_type = SHT_NOBITS;
  225. mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  226. mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
  227. mod->arch.core.plt->sh_size = (core_plts + 1) * sizeof(struct plt_entry);
  228. mod->arch.core.plt_num_entries = 0;
  229. mod->arch.core.plt_max_entries = core_plts;
  230. mod->arch.init.plt->sh_type = SHT_NOBITS;
  231. mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  232. mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
  233. mod->arch.init.plt->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
  234. mod->arch.init.plt_num_entries = 0;
  235. mod->arch.init.plt_max_entries = init_plts;
  236. if (tramp) {
  237. tramp->sh_type = SHT_NOBITS;
  238. tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  239. tramp->sh_addralign = __alignof__(struct plt_entry);
  240. tramp->sh_size = sizeof(struct plt_entry);
  241. }
  242. return 0;
  243. }