1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
10 #include <linux/compiler.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/rculist.h>
14 #include <linux/rcupdate.h>
17 #ifndef ARCH_SHF_SMALL
18 #define ARCH_SHF_SMALL 0
22 * Use highest 4 bits of sh_entsize to store the mod_mem_type of this
23 * section. This leaves 28 bits for offset on 32-bit systems, which is
24 * about 256 MiB (WARN_ON_ONCE if we exceed that).
27 #define SH_ENTSIZE_TYPE_BITS 4
28 #define SH_ENTSIZE_TYPE_SHIFT (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)
29 #define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1)
30 #define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1)
32 /* Maximum number of characters written by module_flags() */
33 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
35 extern struct mutex module_mutex;
36 extern struct list_head modules;
38 extern struct module_attribute *modinfo_attrs[];
39 extern size_t modinfo_attrs_count;
41 /* Provided by the linker */
42 extern const struct kernel_symbol __start___ksymtab[];
43 extern const struct kernel_symbol __stop___ksymtab[];
44 extern const struct kernel_symbol __start___ksymtab_gpl[];
45 extern const struct kernel_symbol __stop___ksymtab_gpl[];
46 extern const s32 __start___kcrctab[];
47 extern const s32 __start___kcrctab_gpl[];
51 /* pointer to module in temporary copy, freed at end of load_module() */
56 char *secstrings, *strtab;
57 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
59 #ifdef CONFIG_KALLSYMS
60 unsigned long mod_kallsyms_init_off;
62 #ifdef CONFIG_MODULE_DECOMPRESS
63 #ifdef CONFIG_MODULE_STATS
64 unsigned long compressed_len;
67 unsigned int max_pages;
68 unsigned int used_pages;
71 unsigned int sym, str, mod, vers, info, pcpu;
80 struct find_symbol_arg {
89 const struct kernel_symbol *sym;
90 enum mod_license license;
93 int mod_verify_sig(const void *mod, struct load_info *info);
94 int try_to_force_load(struct module *mod, const char *reason);
95 bool find_symbol(struct find_symbol_arg *fsa);
96 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
97 int cmp_name(const void *name, const void *sym);
98 long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
99 Elf_Shdr *sechdr, unsigned int section);
100 char *module_flags(struct module *mod, char *buf, bool show_state);
101 size_t module_flags_taint(unsigned long taints, char *buf);
103 char *module_next_tag_pair(char *string, unsigned long *secsize);
105 #define for_each_modinfo_entry(entry, info, name) \
106 for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry))
108 static inline void module_assert_mutex_or_preempt(void)
110 #ifdef CONFIG_LOCKDEP
111 if (unlikely(!debug_locks))
114 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
115 !lockdep_is_held(&module_mutex));
119 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
121 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
122 return (unsigned long)offset_to_ptr(&sym->value_offset);
128 #ifdef CONFIG_LIVEPATCH
129 int copy_module_elf(struct module *mod, struct load_info *info);
130 void free_module_elf(struct module *mod);
131 #else /* !CONFIG_LIVEPATCH */
132 static inline int copy_module_elf(struct module *mod, struct load_info *info)
137 static inline void free_module_elf(struct module *mod) { }
138 #endif /* CONFIG_LIVEPATCH */
140 static inline bool set_livepatch_module(struct module *mod)
142 #ifdef CONFIG_LIVEPATCH
151 * enum fail_dup_mod_reason - state at which a duplicate module was detected
153 * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but
154 * we've determined that another module with the same name is already loaded
155 * or being processed on our &modules list. This happens on early_mod_check()
156 * right before layout_and_allocate(). The kernel would have already
157 * vmalloc()'d space for the entire module through finit_module(). If
158 * decompression was used two vmap() spaces were used. These failures can
159 * happen when userspace has not seen the module present on the kernel and
160 * tries to load the module multiple times at same time.
161 * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation
162 * checks and the kernel determines that the module was unique and because
163 * of this allocated yet another private kernel copy of the module space in
164 * layout_and_allocate() but after this determined in add_unformed_module()
165 * that another module with the same name is already loaded or being processed.
166 * These failures should be mitigated as much as possible and are indicative
167 * of really fast races in loading modules. Without module decompression
168 * they waste twice as much vmap space. With module decompression three
169 * times the module's size vmap space is wasted.
171 enum fail_dup_mod_reason {
172 FAIL_DUP_MOD_BECOMING = 0,
176 #ifdef CONFIG_MODULE_DEBUGFS
177 extern struct dentry *mod_debugfs_root;
180 #ifdef CONFIG_MODULE_STATS
182 #define mod_stat_add_long(count, var) atomic_long_add(count, var)
183 #define mod_stat_inc(name) atomic_inc(name)
185 extern atomic_long_t total_mod_size;
186 extern atomic_long_t total_text_size;
187 extern atomic_long_t invalid_kread_bytes;
188 extern atomic_long_t invalid_decompress_bytes;
190 extern atomic_t modcount;
191 extern atomic_t failed_kreads;
192 extern atomic_t failed_decompress;
193 struct mod_fail_load {
194 struct list_head list;
195 char name[MODULE_NAME_LEN];
197 unsigned long dup_fail_mask;
200 int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
201 void mod_stat_bump_invalid(struct load_info *info, int flags);
202 void mod_stat_bump_becoming(struct load_info *info, int flags);
206 #define mod_stat_add_long(name, var)
207 #define mod_stat_inc(name)
209 static inline int try_add_failed_module(const char *name,
210 enum fail_dup_mod_reason reason)
215 static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
219 static inline void mod_stat_bump_becoming(struct load_info *info, int flags)
223 #endif /* CONFIG_MODULE_STATS */
225 #ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS
226 bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
227 void kmod_dup_request_announce(char *module_name, int ret);
229 static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
234 static inline void kmod_dup_request_announce(char *module_name, int ret)
239 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
240 struct mod_unload_taint {
241 struct list_head list;
242 char name[MODULE_NAME_LEN];
243 unsigned long taints;
247 int try_add_tainted_module(struct module *mod);
248 void print_unloaded_tainted_modules(void);
249 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
250 static inline int try_add_tainted_module(struct module *mod)
255 static inline void print_unloaded_tainted_modules(void)
258 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
260 #ifdef CONFIG_MODULE_DECOMPRESS
261 int module_decompress(struct load_info *info, const void *buf, size_t size);
262 void module_decompress_cleanup(struct load_info *info);
264 static inline int module_decompress(struct load_info *info,
265 const void *buf, size_t size)
270 static inline void module_decompress_cleanup(struct load_info *info)
275 struct mod_tree_root {
276 #ifdef CONFIG_MODULES_TREE_LOOKUP
277 struct latch_tree_root root;
279 unsigned long addr_min;
280 unsigned long addr_max;
281 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
282 unsigned long data_addr_min;
283 unsigned long data_addr_max;
287 extern struct mod_tree_root mod_tree;
289 #ifdef CONFIG_MODULES_TREE_LOOKUP
290 void mod_tree_insert(struct module *mod);
291 void mod_tree_remove_init(struct module *mod);
292 void mod_tree_remove(struct module *mod);
293 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
294 #else /* !CONFIG_MODULES_TREE_LOOKUP */
296 static inline void mod_tree_insert(struct module *mod) { }
297 static inline void mod_tree_remove_init(struct module *mod) { }
298 static inline void mod_tree_remove(struct module *mod) { }
299 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
303 list_for_each_entry_rcu(mod, &modules, list,
304 lockdep_is_held(&module_mutex)) {
305 if (within_module(addr, mod))
311 #endif /* CONFIG_MODULES_TREE_LOOKUP */
313 void module_enable_ro(const struct module *mod, bool after_init);
314 void module_enable_nx(const struct module *mod);
315 void module_enable_x(const struct module *mod);
316 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
317 char *secstrings, struct module *mod);
319 #ifdef CONFIG_MODULE_SIG
320 int module_sig_check(struct load_info *info, int flags);
321 #else /* !CONFIG_MODULE_SIG */
322 static inline int module_sig_check(struct load_info *info, int flags)
326 #endif /* !CONFIG_MODULE_SIG */
328 #ifdef CONFIG_DEBUG_KMEMLEAK
329 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
330 #else /* !CONFIG_DEBUG_KMEMLEAK */
331 static inline void kmemleak_load_module(const struct module *mod,
332 const struct load_info *info) { }
333 #endif /* CONFIG_DEBUG_KMEMLEAK */
335 #ifdef CONFIG_KALLSYMS
336 void init_build_id(struct module *mod, const struct load_info *info);
337 void layout_symtab(struct module *mod, struct load_info *info);
338 void add_kallsyms(struct module *mod, const struct load_info *info);
340 static inline bool sect_empty(const Elf_Shdr *sect)
342 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
344 #else /* !CONFIG_KALLSYMS */
345 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
346 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
347 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
348 #endif /* CONFIG_KALLSYMS */
351 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
352 struct kernel_param *kparam, unsigned int num_params);
353 void mod_sysfs_teardown(struct module *mod);
354 void init_param_lock(struct module *mod);
355 #else /* !CONFIG_SYSFS */
356 static inline int mod_sysfs_setup(struct module *mod,
357 const struct load_info *info,
358 struct kernel_param *kparam,
359 unsigned int num_params)
364 static inline void mod_sysfs_teardown(struct module *mod) { }
365 static inline void init_param_lock(struct module *mod) { }
366 #endif /* CONFIG_SYSFS */
368 #ifdef CONFIG_MODVERSIONS
369 int check_version(const struct load_info *info,
370 const char *symname, struct module *mod, const s32 *crc);
371 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
372 struct kernel_symbol *ks, struct tracepoint * const *tp);
373 int check_modstruct_version(const struct load_info *info, struct module *mod);
374 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
375 #else /* !CONFIG_MODVERSIONS */
376 static inline int check_version(const struct load_info *info,
384 static inline int check_modstruct_version(const struct load_info *info,
390 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
392 return strcmp(amagic, bmagic) == 0;
394 #endif /* CONFIG_MODVERSIONS */