module.c 6.6 KB

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