module.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Kernel module help for x86.
  2. Copyright (C) 2001 Rusty Russell.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/moduleloader.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kasan.h>
  23. #include <linux/bug.h>
  24. #include <linux/mm.h>
  25. #include <linux/gfp.h>
  26. #include <linux/jump_label.h>
  27. #include <linux/random.h>
  28. #include <asm/text-patching.h>
  29. #include <asm/page.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/setup.h>
  32. #include <asm/unwind.h>
  33. #if 0
  34. #define DEBUGP(fmt, ...) \
  35. printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  36. #else
  37. #define DEBUGP(fmt, ...) \
  38. do { \
  39. if (0) \
  40. printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
  41. } while (0)
  42. #endif
  43. #ifdef CONFIG_RANDOMIZE_BASE
  44. static unsigned long module_load_offset;
  45. /* Mutex protects the module_load_offset. */
  46. static DEFINE_MUTEX(module_kaslr_mutex);
  47. static unsigned long int get_module_load_offset(void)
  48. {
  49. if (kaslr_enabled()) {
  50. mutex_lock(&module_kaslr_mutex);
  51. /*
  52. * Calculate the module_load_offset the first time this
  53. * code is called. Once calculated it stays the same until
  54. * reboot.
  55. */
  56. if (module_load_offset == 0)
  57. module_load_offset =
  58. (get_random_int() % 1024 + 1) * PAGE_SIZE;
  59. mutex_unlock(&module_kaslr_mutex);
  60. }
  61. return module_load_offset;
  62. }
  63. #else
  64. static unsigned long int get_module_load_offset(void)
  65. {
  66. return 0;
  67. }
  68. #endif
  69. void *module_alloc(unsigned long size)
  70. {
  71. void *p;
  72. if (PAGE_ALIGN(size) > MODULES_LEN)
  73. return NULL;
  74. p = __vmalloc_node_range(size, MODULE_ALIGN,
  75. MODULES_VADDR + get_module_load_offset(),
  76. MODULES_END, GFP_KERNEL,
  77. PAGE_KERNEL, 0, NUMA_NO_NODE,
  78. __builtin_return_address(0));
  79. if (p && (kasan_module_alloc(p, size) < 0)) {
  80. vfree(p);
  81. return NULL;
  82. }
  83. return p;
  84. }
  85. #ifdef CONFIG_X86_32
  86. int apply_relocate(Elf32_Shdr *sechdrs,
  87. const char *strtab,
  88. unsigned int symindex,
  89. unsigned int relsec,
  90. struct module *me)
  91. {
  92. unsigned int i;
  93. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  94. Elf32_Sym *sym;
  95. uint32_t *location;
  96. DEBUGP("Applying relocate section %u to %u\n",
  97. relsec, sechdrs[relsec].sh_info);
  98. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  99. /* This is where to make the change */
  100. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  101. + rel[i].r_offset;
  102. /* This is the symbol it is referring to. Note that all
  103. undefined symbols have been resolved. */
  104. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  105. + ELF32_R_SYM(rel[i].r_info);
  106. switch (ELF32_R_TYPE(rel[i].r_info)) {
  107. case R_386_32:
  108. /* We add the value into the location given */
  109. *location += sym->st_value;
  110. break;
  111. case R_386_PC32:
  112. case R_386_PLT32:
  113. /* Add the value, subtract its position */
  114. *location += sym->st_value - (uint32_t)location;
  115. break;
  116. default:
  117. pr_err("%s: Unknown relocation: %u\n",
  118. me->name, ELF32_R_TYPE(rel[i].r_info));
  119. return -ENOEXEC;
  120. }
  121. }
  122. return 0;
  123. }
  124. #else /*X86_64*/
  125. int apply_relocate_add(Elf64_Shdr *sechdrs,
  126. const char *strtab,
  127. unsigned int symindex,
  128. unsigned int relsec,
  129. struct module *me)
  130. {
  131. unsigned int i;
  132. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  133. Elf64_Sym *sym;
  134. void *loc;
  135. u64 val;
  136. DEBUGP("Applying relocate section %u to %u\n",
  137. relsec, sechdrs[relsec].sh_info);
  138. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  139. /* This is where to make the change */
  140. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  141. + rel[i].r_offset;
  142. /* This is the symbol it is referring to. Note that all
  143. undefined symbols have been resolved. */
  144. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  145. + ELF64_R_SYM(rel[i].r_info);
  146. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  147. (int)ELF64_R_TYPE(rel[i].r_info),
  148. sym->st_value, rel[i].r_addend, (u64)loc);
  149. val = sym->st_value + rel[i].r_addend;
  150. switch (ELF64_R_TYPE(rel[i].r_info)) {
  151. case R_X86_64_NONE:
  152. break;
  153. case R_X86_64_64:
  154. if (*(u64 *)loc != 0)
  155. goto invalid_relocation;
  156. *(u64 *)loc = val;
  157. break;
  158. case R_X86_64_32:
  159. if (*(u32 *)loc != 0)
  160. goto invalid_relocation;
  161. *(u32 *)loc = val;
  162. if (val != *(u32 *)loc)
  163. goto overflow;
  164. break;
  165. case R_X86_64_32S:
  166. if (*(s32 *)loc != 0)
  167. goto invalid_relocation;
  168. *(s32 *)loc = val;
  169. if ((s64)val != *(s32 *)loc)
  170. goto overflow;
  171. break;
  172. case R_X86_64_PC32:
  173. case R_X86_64_PLT32:
  174. if (*(u32 *)loc != 0)
  175. goto invalid_relocation;
  176. val -= (u64)loc;
  177. *(u32 *)loc = val;
  178. #if 0
  179. if ((s64)val != *(s32 *)loc)
  180. goto overflow;
  181. #endif
  182. break;
  183. default:
  184. pr_err("%s: Unknown rela relocation: %llu\n",
  185. me->name, ELF64_R_TYPE(rel[i].r_info));
  186. return -ENOEXEC;
  187. }
  188. }
  189. return 0;
  190. invalid_relocation:
  191. pr_err("x86/modules: Skipping invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n",
  192. (int)ELF64_R_TYPE(rel[i].r_info), loc, val);
  193. return -ENOEXEC;
  194. overflow:
  195. pr_err("overflow in relocation type %d val %Lx\n",
  196. (int)ELF64_R_TYPE(rel[i].r_info), val);
  197. pr_err("`%s' likely not compiled with -mcmodel=kernel\n",
  198. me->name);
  199. return -ENOEXEC;
  200. }
  201. #endif
  202. int module_finalize(const Elf_Ehdr *hdr,
  203. const Elf_Shdr *sechdrs,
  204. struct module *me)
  205. {
  206. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  207. *para = NULL, *orc = NULL, *orc_ip = NULL;
  208. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  209. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  210. if (!strcmp(".text", secstrings + s->sh_name))
  211. text = s;
  212. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  213. alt = s;
  214. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  215. locks = s;
  216. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  217. para = s;
  218. if (!strcmp(".orc_unwind", secstrings + s->sh_name))
  219. orc = s;
  220. if (!strcmp(".orc_unwind_ip", secstrings + s->sh_name))
  221. orc_ip = s;
  222. }
  223. if (alt) {
  224. /* patch .altinstructions */
  225. void *aseg = (void *)alt->sh_addr;
  226. apply_alternatives(aseg, aseg + alt->sh_size);
  227. }
  228. if (locks && text) {
  229. void *lseg = (void *)locks->sh_addr;
  230. void *tseg = (void *)text->sh_addr;
  231. alternatives_smp_module_add(me, me->name,
  232. lseg, lseg + locks->sh_size,
  233. tseg, tseg + text->sh_size);
  234. }
  235. if (para) {
  236. void *pseg = (void *)para->sh_addr;
  237. apply_paravirt(pseg, pseg + para->sh_size);
  238. }
  239. /* make jump label nops */
  240. jump_label_apply_nops(me);
  241. if (orc && orc_ip)
  242. unwind_module_init(me, (void *)orc_ip->sh_addr, orc_ip->sh_size,
  243. (void *)orc->sh_addr, orc->sh_size);
  244. return 0;
  245. }
  246. void module_arch_cleanup(struct module *mod)
  247. {
  248. alternatives_smp_module_del(mod);
  249. }