internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Module internals
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org>
  7. */
  8. #include <linux/elf.h>
  9. #include <linux/compiler.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/rculist.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/mm.h>
  15. #ifndef ARCH_SHF_SMALL
  16. #define ARCH_SHF_SMALL 0
  17. #endif
  18. /*
  19. * Use highest 4 bits of sh_entsize to store the mod_mem_type of this
  20. * section. This leaves 28 bits for offset on 32-bit systems, which is
  21. * about 256 MiB (WARN_ON_ONCE if we exceed that).
  22. */
  23. #define SH_ENTSIZE_TYPE_BITS 4
  24. #define SH_ENTSIZE_TYPE_SHIFT (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)
  25. #define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1)
  26. #define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1)
  27. /* Maximum number of characters written by module_flags() */
  28. #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
  29. struct kernel_symbol {
  30. #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  31. int value_offset;
  32. int name_offset;
  33. int namespace_offset;
  34. #else
  35. unsigned long value;
  36. const char *name;
  37. const char *namespace;
  38. #endif
  39. };
  40. extern struct mutex module_mutex;
  41. extern struct list_head modules;
  42. extern struct module_attribute *modinfo_attrs[];
  43. extern size_t modinfo_attrs_count;
  44. /* Provided by the linker */
  45. extern const struct kernel_symbol __start___ksymtab[];
  46. extern const struct kernel_symbol __stop___ksymtab[];
  47. extern const struct kernel_symbol __start___ksymtab_gpl[];
  48. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  49. extern const s32 __start___kcrctab[];
  50. extern const s32 __start___kcrctab_gpl[];
  51. struct load_info {
  52. const char *name;
  53. /* pointer to module in temporary copy, freed at end of load_module() */
  54. struct module *mod;
  55. Elf_Ehdr *hdr;
  56. unsigned long len;
  57. Elf_Shdr *sechdrs;
  58. char *secstrings, *strtab;
  59. unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
  60. bool sig_ok;
  61. #ifdef CONFIG_KALLSYMS
  62. unsigned long mod_kallsyms_init_off;
  63. #endif
  64. #ifdef CONFIG_MODULE_DECOMPRESS
  65. #ifdef CONFIG_MODULE_STATS
  66. unsigned long compressed_len;
  67. #endif
  68. struct page **pages;
  69. unsigned int max_pages;
  70. unsigned int used_pages;
  71. #endif
  72. struct {
  73. unsigned int sym, str, mod, vers, info, pcpu;
  74. } index;
  75. };
  76. enum mod_license {
  77. NOT_GPL_ONLY,
  78. GPL_ONLY,
  79. };
  80. struct find_symbol_arg {
  81. /* Input */
  82. const char *name;
  83. bool gplok;
  84. bool warn;
  85. /* Output */
  86. struct module *owner;
  87. const s32 *crc;
  88. const struct kernel_symbol *sym;
  89. enum mod_license license;
  90. };
  91. int mod_verify_sig(const void *mod, struct load_info *info);
  92. int try_to_force_load(struct module *mod, const char *reason);
  93. bool find_symbol(struct find_symbol_arg *fsa);
  94. struct module *find_module_all(const char *name, size_t len, bool even_unformed);
  95. int cmp_name(const void *name, const void *sym);
  96. long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
  97. Elf_Shdr *sechdr, unsigned int section);
  98. char *module_flags(struct module *mod, char *buf, bool show_state);
  99. size_t module_flags_taint(unsigned long taints, char *buf);
  100. char *module_next_tag_pair(char *string, unsigned long *secsize);
  101. #define for_each_modinfo_entry(entry, info, name) \
  102. for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry))
  103. static inline void module_assert_mutex_or_preempt(void)
  104. {
  105. #ifdef CONFIG_LOCKDEP
  106. if (unlikely(!debug_locks))
  107. return;
  108. WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
  109. !lockdep_is_held(&module_mutex));
  110. #endif
  111. }
  112. static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
  113. {
  114. #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  115. return (unsigned long)offset_to_ptr(&sym->value_offset);
  116. #else
  117. return sym->value;
  118. #endif
  119. }
  120. #ifdef CONFIG_LIVEPATCH
  121. int copy_module_elf(struct module *mod, struct load_info *info);
  122. void free_module_elf(struct module *mod);
  123. #else /* !CONFIG_LIVEPATCH */
  124. static inline int copy_module_elf(struct module *mod, struct load_info *info)
  125. {
  126. return 0;
  127. }
  128. static inline void free_module_elf(struct module *mod) { }
  129. #endif /* CONFIG_LIVEPATCH */
  130. static inline bool set_livepatch_module(struct module *mod)
  131. {
  132. #ifdef CONFIG_LIVEPATCH
  133. mod->klp = true;
  134. return true;
  135. #else
  136. return false;
  137. #endif
  138. }
  139. /**
  140. * enum fail_dup_mod_reason - state at which a duplicate module was detected
  141. *
  142. * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but
  143. * we've determined that another module with the same name is already loaded
  144. * or being processed on our &modules list. This happens on early_mod_check()
  145. * right before layout_and_allocate(). The kernel would have already
  146. * vmalloc()'d space for the entire module through finit_module(). If
  147. * decompression was used two vmap() spaces were used. These failures can
  148. * happen when userspace has not seen the module present on the kernel and
  149. * tries to load the module multiple times at same time.
  150. * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation
  151. * checks and the kernel determines that the module was unique and because
  152. * of this allocated yet another private kernel copy of the module space in
  153. * layout_and_allocate() but after this determined in add_unformed_module()
  154. * that another module with the same name is already loaded or being processed.
  155. * These failures should be mitigated as much as possible and are indicative
  156. * of really fast races in loading modules. Without module decompression
  157. * they waste twice as much vmap space. With module decompression three
  158. * times the module's size vmap space is wasted.
  159. */
  160. enum fail_dup_mod_reason {
  161. FAIL_DUP_MOD_BECOMING = 0,
  162. FAIL_DUP_MOD_LOAD,
  163. };
  164. #ifdef CONFIG_MODULE_DEBUGFS
  165. extern struct dentry *mod_debugfs_root;
  166. #endif
  167. #ifdef CONFIG_MODULE_STATS
  168. #define mod_stat_add_long(count, var) atomic_long_add(count, var)
  169. #define mod_stat_inc(name) atomic_inc(name)
  170. extern atomic_long_t total_mod_size;
  171. extern atomic_long_t total_text_size;
  172. extern atomic_long_t invalid_kread_bytes;
  173. extern atomic_long_t invalid_decompress_bytes;
  174. extern atomic_t modcount;
  175. extern atomic_t failed_kreads;
  176. extern atomic_t failed_decompress;
  177. struct mod_fail_load {
  178. struct list_head list;
  179. char name[MODULE_NAME_LEN];
  180. atomic_long_t count;
  181. unsigned long dup_fail_mask;
  182. };
  183. int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
  184. void mod_stat_bump_invalid(struct load_info *info, int flags);
  185. void mod_stat_bump_becoming(struct load_info *info, int flags);
  186. #else
  187. #define mod_stat_add_long(name, var)
  188. #define mod_stat_inc(name)
  189. static inline int try_add_failed_module(const char *name,
  190. enum fail_dup_mod_reason reason)
  191. {
  192. return 0;
  193. }
  194. static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
  195. {
  196. }
  197. static inline void mod_stat_bump_becoming(struct load_info *info, int flags)
  198. {
  199. }
  200. #endif /* CONFIG_MODULE_STATS */
  201. #ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS
  202. bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
  203. void kmod_dup_request_announce(char *module_name, int ret);
  204. #else
  205. static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
  206. {
  207. return false;
  208. }
  209. static inline void kmod_dup_request_announce(char *module_name, int ret)
  210. {
  211. }
  212. #endif
  213. #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
  214. struct mod_unload_taint {
  215. struct list_head list;
  216. char name[MODULE_NAME_LEN];
  217. unsigned long taints;
  218. u64 count;
  219. };
  220. int try_add_tainted_module(struct module *mod);
  221. void print_unloaded_tainted_modules(void);
  222. #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
  223. static inline int try_add_tainted_module(struct module *mod)
  224. {
  225. return 0;
  226. }
  227. static inline void print_unloaded_tainted_modules(void)
  228. {
  229. }
  230. #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
  231. #ifdef CONFIG_MODULE_DECOMPRESS
  232. int module_decompress(struct load_info *info, const void *buf, size_t size);
  233. void module_decompress_cleanup(struct load_info *info);
  234. #else
  235. static inline int module_decompress(struct load_info *info,
  236. const void *buf, size_t size)
  237. {
  238. return -EOPNOTSUPP;
  239. }
  240. static inline void module_decompress_cleanup(struct load_info *info)
  241. {
  242. }
  243. #endif
  244. struct mod_tree_root {
  245. #ifdef CONFIG_MODULES_TREE_LOOKUP
  246. struct latch_tree_root root;
  247. #endif
  248. unsigned long addr_min;
  249. unsigned long addr_max;
  250. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  251. unsigned long data_addr_min;
  252. unsigned long data_addr_max;
  253. #endif
  254. };
  255. extern struct mod_tree_root mod_tree;
  256. #ifdef CONFIG_MODULES_TREE_LOOKUP
  257. void mod_tree_insert(struct module *mod);
  258. void mod_tree_remove_init(struct module *mod);
  259. void mod_tree_remove(struct module *mod);
  260. struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
  261. #else /* !CONFIG_MODULES_TREE_LOOKUP */
  262. static inline void mod_tree_insert(struct module *mod) { }
  263. static inline void mod_tree_remove_init(struct module *mod) { }
  264. static inline void mod_tree_remove(struct module *mod) { }
  265. static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
  266. {
  267. struct module *mod;
  268. list_for_each_entry_rcu(mod, &modules, list,
  269. lockdep_is_held(&module_mutex)) {
  270. if (within_module(addr, mod))
  271. return mod;
  272. }
  273. return NULL;
  274. }
  275. #endif /* CONFIG_MODULES_TREE_LOOKUP */
  276. int module_enable_rodata_ro(const struct module *mod, bool after_init);
  277. int module_enable_data_nx(const struct module *mod);
  278. int module_enable_text_rox(const struct module *mod);
  279. int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  280. char *secstrings, struct module *mod);
  281. #ifdef CONFIG_MODULE_SIG
  282. int module_sig_check(struct load_info *info, int flags);
  283. #else /* !CONFIG_MODULE_SIG */
  284. static inline int module_sig_check(struct load_info *info, int flags)
  285. {
  286. return 0;
  287. }
  288. #endif /* !CONFIG_MODULE_SIG */
  289. #ifdef CONFIG_DEBUG_KMEMLEAK
  290. void kmemleak_load_module(const struct module *mod, const struct load_info *info);
  291. #else /* !CONFIG_DEBUG_KMEMLEAK */
  292. static inline void kmemleak_load_module(const struct module *mod,
  293. const struct load_info *info) { }
  294. #endif /* CONFIG_DEBUG_KMEMLEAK */
  295. #ifdef CONFIG_KALLSYMS
  296. void init_build_id(struct module *mod, const struct load_info *info);
  297. void layout_symtab(struct module *mod, struct load_info *info);
  298. void add_kallsyms(struct module *mod, const struct load_info *info);
  299. static inline bool sect_empty(const Elf_Shdr *sect)
  300. {
  301. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  302. }
  303. #else /* !CONFIG_KALLSYMS */
  304. static inline void init_build_id(struct module *mod, const struct load_info *info) { }
  305. static inline void layout_symtab(struct module *mod, struct load_info *info) { }
  306. static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
  307. #endif /* CONFIG_KALLSYMS */
  308. #ifdef CONFIG_SYSFS
  309. int mod_sysfs_setup(struct module *mod, const struct load_info *info,
  310. struct kernel_param *kparam, unsigned int num_params);
  311. void mod_sysfs_teardown(struct module *mod);
  312. void init_param_lock(struct module *mod);
  313. #else /* !CONFIG_SYSFS */
  314. static inline int mod_sysfs_setup(struct module *mod,
  315. const struct load_info *info,
  316. struct kernel_param *kparam,
  317. unsigned int num_params)
  318. {
  319. return 0;
  320. }
  321. static inline void mod_sysfs_teardown(struct module *mod) { }
  322. static inline void init_param_lock(struct module *mod) { }
  323. #endif /* CONFIG_SYSFS */
  324. #ifdef CONFIG_MODVERSIONS
  325. int check_version(const struct load_info *info,
  326. const char *symname, struct module *mod, const s32 *crc);
  327. void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
  328. struct kernel_symbol *ks, struct tracepoint * const *tp);
  329. int check_modstruct_version(const struct load_info *info, struct module *mod);
  330. int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
  331. #else /* !CONFIG_MODVERSIONS */
  332. static inline int check_version(const struct load_info *info,
  333. const char *symname,
  334. struct module *mod,
  335. const s32 *crc)
  336. {
  337. return 1;
  338. }
  339. static inline int check_modstruct_version(const struct load_info *info,
  340. struct module *mod)
  341. {
  342. return 1;
  343. }
  344. static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
  345. {
  346. return strcmp(amagic, bmagic) == 0;
  347. }
  348. #endif /* CONFIG_MODVERSIONS */