module.c 6.5 KB

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