module.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/page.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/setup.h>
  31. #if 0
  32. #define DEBUGP(fmt, ...) \
  33. printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  34. #else
  35. #define DEBUGP(fmt, ...) \
  36. do { \
  37. if (0) \
  38. printk(KERN_DEBUG fmt, ##__VA_ARGS__); \
  39. } while (0)
  40. #endif
  41. #ifdef CONFIG_RANDOMIZE_BASE
  42. static unsigned long module_load_offset;
  43. /* Mutex protects the module_load_offset. */
  44. static DEFINE_MUTEX(module_kaslr_mutex);
  45. static unsigned long int get_module_load_offset(void)
  46. {
  47. if (kaslr_enabled()) {
  48. mutex_lock(&module_kaslr_mutex);
  49. /*
  50. * Calculate the module_load_offset the first time this
  51. * code is called. Once calculated it stays the same until
  52. * reboot.
  53. */
  54. if (module_load_offset == 0)
  55. module_load_offset =
  56. (get_random_int() % 1024 + 1) * PAGE_SIZE;
  57. mutex_unlock(&module_kaslr_mutex);
  58. }
  59. return module_load_offset;
  60. }
  61. #else
  62. static unsigned long int get_module_load_offset(void)
  63. {
  64. return 0;
  65. }
  66. #endif
  67. void *module_alloc(unsigned long size)
  68. {
  69. void *p;
  70. if (PAGE_ALIGN(size) > MODULES_LEN)
  71. return NULL;
  72. p = __vmalloc_node_range(size, MODULE_ALIGN,
  73. MODULES_VADDR + get_module_load_offset(),
  74. MODULES_END, GFP_KERNEL | __GFP_HIGHMEM,
  75. PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  76. __builtin_return_address(0));
  77. if (p && (kasan_module_alloc(p, size) < 0)) {
  78. vfree(p);
  79. return NULL;
  80. }
  81. return p;
  82. }
  83. #ifdef CONFIG_X86_32
  84. int apply_relocate(Elf32_Shdr *sechdrs,
  85. const char *strtab,
  86. unsigned int symindex,
  87. unsigned int relsec,
  88. struct module *me)
  89. {
  90. unsigned int i;
  91. Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
  92. Elf32_Sym *sym;
  93. uint32_t *location;
  94. DEBUGP("Applying relocate section %u to %u\n",
  95. relsec, sechdrs[relsec].sh_info);
  96. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  97. /* This is where to make the change */
  98. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  99. + rel[i].r_offset;
  100. /* This is the symbol it is referring to. Note that all
  101. undefined symbols have been resolved. */
  102. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  103. + ELF32_R_SYM(rel[i].r_info);
  104. switch (ELF32_R_TYPE(rel[i].r_info)) {
  105. case R_386_32:
  106. /* We add the value into the location given */
  107. *location += sym->st_value;
  108. break;
  109. case R_386_PC32:
  110. /* Add the value, subtract its position */
  111. *location += sym->st_value - (uint32_t)location;
  112. break;
  113. default:
  114. pr_err("%s: Unknown relocation: %u\n",
  115. me->name, ELF32_R_TYPE(rel[i].r_info));
  116. return -ENOEXEC;
  117. }
  118. }
  119. return 0;
  120. }
  121. #else /*X86_64*/
  122. int apply_relocate_add(Elf64_Shdr *sechdrs,
  123. const char *strtab,
  124. unsigned int symindex,
  125. unsigned int relsec,
  126. struct module *me)
  127. {
  128. unsigned int i;
  129. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  130. Elf64_Sym *sym;
  131. void *loc;
  132. u64 val;
  133. DEBUGP("Applying relocate section %u to %u\n",
  134. relsec, sechdrs[relsec].sh_info);
  135. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  136. /* This is where to make the change */
  137. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  138. + rel[i].r_offset;
  139. /* This is the symbol it is referring to. Note that all
  140. undefined symbols have been resolved. */
  141. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  142. + ELF64_R_SYM(rel[i].r_info);
  143. DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
  144. (int)ELF64_R_TYPE(rel[i].r_info),
  145. sym->st_value, rel[i].r_addend, (u64)loc);
  146. val = sym->st_value + rel[i].r_addend;
  147. switch (ELF64_R_TYPE(rel[i].r_info)) {
  148. case R_X86_64_NONE:
  149. break;
  150. case R_X86_64_64:
  151. if (*(u64 *)loc != 0)
  152. goto invalid_relocation;
  153. *(u64 *)loc = val;
  154. break;
  155. case R_X86_64_32:
  156. if (*(u32 *)loc != 0)
  157. goto invalid_relocation;
  158. *(u32 *)loc = val;
  159. if (val != *(u32 *)loc)
  160. goto overflow;
  161. break;
  162. case R_X86_64_32S:
  163. if (*(s32 *)loc != 0)
  164. goto invalid_relocation;
  165. *(s32 *)loc = val;
  166. if ((s64)val != *(s32 *)loc)
  167. goto overflow;
  168. break;
  169. case R_X86_64_PC32:
  170. case R_X86_64_PLT32:
  171. if (*(u32 *)loc != 0)
  172. goto invalid_relocation;
  173. val -= (u64)loc;
  174. *(u32 *)loc = val;
  175. #if 0
  176. if ((s64)val != *(s32 *)loc)
  177. goto overflow;
  178. #endif
  179. break;
  180. default:
  181. pr_err("%s: Unknown rela relocation: %llu\n",
  182. me->name, ELF64_R_TYPE(rel[i].r_info));
  183. return -ENOEXEC;
  184. }
  185. }
  186. return 0;
  187. invalid_relocation:
  188. pr_err("x86/modules: Skipping invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n",
  189. (int)ELF64_R_TYPE(rel[i].r_info), loc, val);
  190. return -ENOEXEC;
  191. overflow:
  192. pr_err("overflow in relocation type %d val %Lx\n",
  193. (int)ELF64_R_TYPE(rel[i].r_info), val);
  194. pr_err("`%s' likely not compiled with -mcmodel=kernel\n",
  195. me->name);
  196. return -ENOEXEC;
  197. }
  198. #endif
  199. int module_finalize(const Elf_Ehdr *hdr,
  200. const Elf_Shdr *sechdrs,
  201. struct module *me)
  202. {
  203. const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
  204. *para = NULL;
  205. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  206. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  207. if (!strcmp(".text", secstrings + s->sh_name))
  208. text = s;
  209. if (!strcmp(".altinstructions", secstrings + s->sh_name))
  210. alt = s;
  211. if (!strcmp(".smp_locks", secstrings + s->sh_name))
  212. locks = s;
  213. if (!strcmp(".parainstructions", secstrings + s->sh_name))
  214. para = s;
  215. }
  216. if (alt) {
  217. /* patch .altinstructions */
  218. void *aseg = (void *)alt->sh_addr;
  219. apply_alternatives(aseg, aseg + alt->sh_size);
  220. }
  221. if (locks && text) {
  222. void *lseg = (void *)locks->sh_addr;
  223. void *tseg = (void *)text->sh_addr;
  224. alternatives_smp_module_add(me, me->name,
  225. lseg, lseg + locks->sh_size,
  226. tseg, tseg + text->sh_size);
  227. }
  228. if (para) {
  229. void *pseg = (void *)para->sh_addr;
  230. apply_paravirt(pseg, pseg + para->sh_size);
  231. }
  232. /* make jump label nops */
  233. jump_label_apply_nops(me);
  234. return 0;
  235. }
  236. void module_arch_cleanup(struct module *mod)
  237. {
  238. alternatives_smp_module_del(mod);
  239. }