module.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Kernel module help for x86.
  3. Copyright (C) 2001 Rusty Russell.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/moduleloader.h>
  7. #include <linux/elf.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/fs.h>
  10. #include <linux/string.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kasan.h>
  13. #include <linux/bug.h>
  14. #include <linux/mm.h>
  15. #include <linux/gfp.h>
  16. #include <linux/jump_label.h>
  17. #include <linux/random.h>
  18. #include <linux/memory.h>
  19. #include <asm/text-patching.h>
  20. #include <asm/page.h>
  21. #include <asm/setup.h>
  22. #include <asm/unwind.h>
  23. #if 0
  24. #define DEBUGP(fmt, ...) \
  25. printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  26. #else
  27. #define DEBUGP(fmt, ...) \
  28. do { \
  29. if (0) \
  30. printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
  31. } while (0)
  32. #endif
  33. #ifdef CONFIG_X86_32
  34. int apply_relocate(Elf32_Shdr *sechdrs,
  35. const char *strtab,
  36. unsigned int symindex,
  37. unsigned int relsec,
  38. struct module *me)
  39. {
  40. unsigned int i;
  41. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  42. Elf32_Sym *sym;
  43. uint32_t *location;
  44. DEBUGP("Applying relocate section %u to %u\n",
  45. relsec, sechdrs[relsec].sh_info);
  46. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  47. /* This is where to make the change */
  48. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  49. + rel[i].r_offset;
  50. /* This is the symbol it is referring to. Note that all
  51. undefined symbols have been resolved. */
  52. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  53. + ELF32_R_SYM(rel[i].r_info);
  54. switch (ELF32_R_TYPE(rel[i].r_info)) {
  55. case R_386_32:
  56. /* We add the value into the location given */
  57. *location += sym->st_value;
  58. break;
  59. case R_386_PC32:
  60. case R_386_PLT32:
  61. /* Add the value, subtract its position */
  62. *location += sym->st_value - (uint32_t)location;
  63. break;
  64. default:
  65. pr_err("%s: Unknown relocation: %u\n",
  66. me->name, ELF32_R_TYPE(rel[i].r_info));
  67. return -ENOEXEC;
  68. }
  69. }
  70. return 0;
  71. }
  72. #else /*X86_64*/
  73. static int __write_relocate_add(Elf64_Shdr *sechdrs,
  74. const char *strtab,
  75. unsigned int symindex,
  76. unsigned int relsec,
  77. struct module *me,
  78. void *(*write)(void *dest, const void *src, size_t len),
  79. bool apply)
  80. {
  81. unsigned int i;
  82. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  83. Elf64_Sym *sym;
  84. void *loc;
  85. u64 val;
  86. u64 zero = 0ULL;
  87. DEBUGP("%s relocate section %u to %u\n",
  88. apply ? "Applying" : "Clearing",
  89. relsec, sechdrs[relsec].sh_info);
  90. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  91. size_t size;
  92. /* This is where to make the change */
  93. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  94. + rel[i].r_offset;
  95. /* This is the symbol it is referring to. Note that all
  96. undefined symbols have been resolved. */
  97. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  98. + ELF64_R_SYM(rel[i].r_info);
  99. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  100. (int)ELF64_R_TYPE(rel[i].r_info),
  101. sym->st_value, rel[i].r_addend, (u64)loc);
  102. val = sym->st_value + rel[i].r_addend;
  103. switch (ELF64_R_TYPE(rel[i].r_info)) {
  104. case R_X86_64_NONE:
  105. continue; /* nothing to write */
  106. case R_X86_64_64:
  107. size = 8;
  108. break;
  109. case R_X86_64_32:
  110. if (val != *(u32 *)&val)
  111. goto overflow;
  112. size = 4;
  113. break;
  114. case R_X86_64_32S:
  115. if ((s64)val != *(s32 *)&val)
  116. goto overflow;
  117. size = 4;
  118. break;
  119. case R_X86_64_PC32:
  120. case R_X86_64_PLT32:
  121. val -= (u64)loc;
  122. size = 4;
  123. break;
  124. case R_X86_64_PC64:
  125. val -= (u64)loc;
  126. size = 8;
  127. break;
  128. default:
  129. pr_err("%s: Unknown rela relocation: %llu\n",
  130. me->name, ELF64_R_TYPE(rel[i].r_info));
  131. return -ENOEXEC;
  132. }
  133. if (apply) {
  134. if (memcmp(loc, &zero, size)) {
  135. pr_err("x86/modules: Invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n",
  136. (int)ELF64_R_TYPE(rel[i].r_info), loc, val);
  137. return -ENOEXEC;
  138. }
  139. write(loc, &val, size);
  140. } else {
  141. if (memcmp(loc, &val, size)) {
  142. pr_warn("x86/modules: Invalid relocation target, existing value does not match expected value for type %d, loc %p, val %Lx\n",
  143. (int)ELF64_R_TYPE(rel[i].r_info), loc, val);
  144. return -ENOEXEC;
  145. }
  146. write(loc, &zero, size);
  147. }
  148. }
  149. return 0;
  150. overflow:
  151. pr_err("overflow in relocation type %d val %Lx\n",
  152. (int)ELF64_R_TYPE(rel[i].r_info), val);
  153. pr_err("`%s' likely not compiled with -mcmodel=kernel\n",
  154. me->name);
  155. return -ENOEXEC;
  156. }
  157. static int write_relocate_add(Elf64_Shdr *sechdrs,
  158. const char *strtab,
  159. unsigned int symindex,
  160. unsigned int relsec,
  161. struct module *me,
  162. bool apply)
  163. {
  164. int ret;
  165. bool early = me->state == MODULE_STATE_UNFORMED;
  166. void *(*write)(void *, const void *, size_t) = memcpy;
  167. if (!early) {
  168. write = text_poke;
  169. mutex_lock(&text_mutex);
  170. }
  171. ret = __write_relocate_add(sechdrs, strtab, symindex, relsec, me,
  172. write, apply);
  173. if (!early) {
  174. text_poke_sync();
  175. mutex_unlock(&text_mutex);
  176. }
  177. return ret;
  178. }
  179. int apply_relocate_add(Elf64_Shdr *sechdrs,
  180. const char *strtab,
  181. unsigned int symindex,
  182. unsigned int relsec,
  183. struct module *me)
  184. {
  185. return write_relocate_add(sechdrs, strtab, symindex, relsec, me, true);
  186. }
  187. #ifdef CONFIG_LIVEPATCH
  188. void clear_relocate_add(Elf64_Shdr *sechdrs,
  189. const char *strtab,
  190. unsigned int symindex,
  191. unsigned int relsec,
  192. struct module *me)
  193. {
  194. write_relocate_add(sechdrs, strtab, symindex, relsec, me, false);
  195. }
  196. #endif
  197. #endif
  198. int module_finalize(const Elf_Ehdr *hdr,
  199. const Elf_Shdr *sechdrs,
  200. struct module *me)
  201. {
  202. const Elf_Shdr *s, *alt = NULL, *locks = NULL,
  203. *orc = NULL, *orc_ip = NULL,
  204. *retpolines = NULL, *returns = NULL, *ibt_endbr = NULL,
  205. *calls = NULL, *cfi = NULL;
  206. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  207. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  208. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  209. alt = s;
  210. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  211. locks = s;
  212. if (!strcmp(".orc_unwind", secstrings + s->sh_name))
  213. orc = s;
  214. if (!strcmp(".orc_unwind_ip", secstrings + s->sh_name))
  215. orc_ip = s;
  216. if (!strcmp(".retpoline_sites", secstrings + s->sh_name))
  217. retpolines = s;
  218. if (!strcmp(".return_sites", secstrings + s->sh_name))
  219. returns = s;
  220. if (!strcmp(".call_sites", secstrings + s->sh_name))
  221. calls = s;
  222. if (!strcmp(".cfi_sites", secstrings + s->sh_name))
  223. cfi = s;
  224. if (!strcmp(".ibt_endbr_seal", secstrings + s->sh_name))
  225. ibt_endbr = s;
  226. }
  227. if (retpolines || cfi) {
  228. void *rseg = NULL, *cseg = NULL;
  229. unsigned int rsize = 0, csize = 0;
  230. if (retpolines) {
  231. rseg = (void *)retpolines->sh_addr;
  232. rsize = retpolines->sh_size;
  233. }
  234. if (cfi) {
  235. cseg = (void *)cfi->sh_addr;
  236. csize = cfi->sh_size;
  237. }
  238. apply_fineibt(rseg, rseg + rsize, cseg, cseg + csize);
  239. }
  240. if (retpolines) {
  241. void *rseg = (void *)retpolines->sh_addr;
  242. apply_retpolines(rseg, rseg + retpolines->sh_size);
  243. }
  244. if (returns) {
  245. void *rseg = (void *)returns->sh_addr;
  246. apply_returns(rseg, rseg + returns->sh_size);
  247. }
  248. if (alt) {
  249. /* patch .altinstructions */
  250. void *aseg = (void *)alt->sh_addr;
  251. apply_alternatives(aseg, aseg + alt->sh_size);
  252. }
  253. if (calls || alt) {
  254. struct callthunk_sites cs = {};
  255. if (calls) {
  256. cs.call_start = (void *)calls->sh_addr;
  257. cs.call_end = (void *)calls->sh_addr + calls->sh_size;
  258. }
  259. if (alt) {
  260. cs.alt_start = (void *)alt->sh_addr;
  261. cs.alt_end = (void *)alt->sh_addr + alt->sh_size;
  262. }
  263. callthunks_patch_module_calls(&cs, me);
  264. }
  265. if (ibt_endbr) {
  266. void *iseg = (void *)ibt_endbr->sh_addr;
  267. apply_seal_endbr(iseg, iseg + ibt_endbr->sh_size);
  268. }
  269. if (locks) {
  270. void *lseg = (void *)locks->sh_addr;
  271. void *text = me->mem[MOD_TEXT].base;
  272. void *text_end = text + me->mem[MOD_TEXT].size;
  273. alternatives_smp_module_add(me, me->name,
  274. lseg, lseg + locks->sh_size,
  275. text, text_end);
  276. }
  277. if (orc && orc_ip)
  278. unwind_module_init(me, (void *)orc_ip->sh_addr, orc_ip->sh_size,
  279. (void *)orc->sh_addr, orc->sh_size);
  280. return 0;
  281. }
  282. void module_arch_cleanup(struct module *mod)
  283. {
  284. alternatives_smp_module_del(mod);
  285. }