module.c 6.5 KB

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