1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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>
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 struct kernel_symbol {
36 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
43 const char *namespace;
47 extern struct mutex module_mutex;
48 extern struct list_head modules;
50 extern struct module_attribute *modinfo_attrs[];
51 extern size_t modinfo_attrs_count;
53 /* Provided by the linker */
54 extern const struct kernel_symbol __start___ksymtab[];
55 extern const struct kernel_symbol __stop___ksymtab[];
56 extern const struct kernel_symbol __start___ksymtab_gpl[];
57 extern const struct kernel_symbol __stop___ksymtab_gpl[];
58 extern const s32 __start___kcrctab[];
59 extern const s32 __start___kcrctab_gpl[];
63 /* pointer to module in temporary copy, freed at end of load_module() */
68 char *secstrings, *strtab;
69 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
71 #ifdef CONFIG_KALLSYMS
72 unsigned long mod_kallsyms_init_off;
74 #ifdef CONFIG_MODULE_DECOMPRESS
75 #ifdef CONFIG_MODULE_STATS
76 unsigned long compressed_len;
79 unsigned int max_pages;
80 unsigned int used_pages;
83 unsigned int sym, str, mod, vers, info, pcpu;
92 struct find_symbol_arg {
101 const struct kernel_symbol *sym;
102 enum mod_license license;
105 int mod_verify_sig(const void *mod, struct load_info *info);
106 int try_to_force_load(struct module *mod, const char *reason);
107 bool find_symbol(struct find_symbol_arg *fsa);
108 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
109 int cmp_name(const void *name, const void *sym);
110 long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
111 Elf_Shdr *sechdr, unsigned int section);
112 char *module_flags(struct module *mod, char *buf, bool show_state);
113 size_t module_flags_taint(unsigned long taints, char *buf);
115 char *module_next_tag_pair(char *string, unsigned long *secsize);
117 #define for_each_modinfo_entry(entry, info, name) \
118 for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry))
120 static inline void module_assert_mutex_or_preempt(void)
122 #ifdef CONFIG_LOCKDEP
123 if (unlikely(!debug_locks))
126 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
127 !lockdep_is_held(&module_mutex));
131 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
133 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
134 return (unsigned long)offset_to_ptr(&sym->value_offset);
140 #ifdef CONFIG_LIVEPATCH
141 int copy_module_elf(struct module *mod, struct load_info *info);
142 void free_module_elf(struct module *mod);
143 #else /* !CONFIG_LIVEPATCH */
144 static inline int copy_module_elf(struct module *mod, struct load_info *info)
149 static inline void free_module_elf(struct module *mod) { }
150 #endif /* CONFIG_LIVEPATCH */
152 static inline bool set_livepatch_module(struct module *mod)
154 #ifdef CONFIG_LIVEPATCH
163 * enum fail_dup_mod_reason - state at which a duplicate module was detected
165 * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but
166 * we've determined that another module with the same name is already loaded
167 * or being processed on our &modules list. This happens on early_mod_check()
168 * right before layout_and_allocate(). The kernel would have already
169 * vmalloc()'d space for the entire module through finit_module(). If
170 * decompression was used two vmap() spaces were used. These failures can
171 * happen when userspace has not seen the module present on the kernel and
172 * tries to load the module multiple times at same time.
173 * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation
174 * checks and the kernel determines that the module was unique and because
175 * of this allocated yet another private kernel copy of the module space in
176 * layout_and_allocate() but after this determined in add_unformed_module()
177 * that another module with the same name is already loaded or being processed.
178 * These failures should be mitigated as much as possible and are indicative
179 * of really fast races in loading modules. Without module decompression
180 * they waste twice as much vmap space. With module decompression three
181 * times the module's size vmap space is wasted.
183 enum fail_dup_mod_reason {
184 FAIL_DUP_MOD_BECOMING = 0,
188 #ifdef CONFIG_MODULE_DEBUGFS
189 extern struct dentry *mod_debugfs_root;
192 #ifdef CONFIG_MODULE_STATS
194 #define mod_stat_add_long(count, var) atomic_long_add(count, var)
195 #define mod_stat_inc(name) atomic_inc(name)
197 extern atomic_long_t total_mod_size;
198 extern atomic_long_t total_text_size;
199 extern atomic_long_t invalid_kread_bytes;
200 extern atomic_long_t invalid_decompress_bytes;
202 extern atomic_t modcount;
203 extern atomic_t failed_kreads;
204 extern atomic_t failed_decompress;
205 struct mod_fail_load {
206 struct list_head list;
207 char name[MODULE_NAME_LEN];
209 unsigned long dup_fail_mask;
212 int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
213 void mod_stat_bump_invalid(struct load_info *info, int flags);
214 void mod_stat_bump_becoming(struct load_info *info, int flags);
218 #define mod_stat_add_long(name, var)
219 #define mod_stat_inc(name)
221 static inline int try_add_failed_module(const char *name,
222 enum fail_dup_mod_reason reason)
227 static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
231 static inline void mod_stat_bump_becoming(struct load_info *info, int flags)
235 #endif /* CONFIG_MODULE_STATS */
237 #ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS
238 bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
239 void kmod_dup_request_announce(char *module_name, int ret);
241 static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
246 static inline void kmod_dup_request_announce(char *module_name, int ret)
251 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
252 struct mod_unload_taint {
253 struct list_head list;
254 char name[MODULE_NAME_LEN];
255 unsigned long taints;
259 int try_add_tainted_module(struct module *mod);
260 void print_unloaded_tainted_modules(void);
261 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
262 static inline int try_add_tainted_module(struct module *mod)
267 static inline void print_unloaded_tainted_modules(void)
270 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
272 #ifdef CONFIG_MODULE_DECOMPRESS
273 int module_decompress(struct load_info *info, const void *buf, size_t size);
274 void module_decompress_cleanup(struct load_info *info);
276 static inline int module_decompress(struct load_info *info,
277 const void *buf, size_t size)
282 static inline void module_decompress_cleanup(struct load_info *info)
287 struct mod_tree_root {
288 #ifdef CONFIG_MODULES_TREE_LOOKUP
289 struct latch_tree_root root;
291 unsigned long addr_min;
292 unsigned long addr_max;
293 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
294 unsigned long data_addr_min;
295 unsigned long data_addr_max;
299 extern struct mod_tree_root mod_tree;
301 #ifdef CONFIG_MODULES_TREE_LOOKUP
302 void mod_tree_insert(struct module *mod);
303 void mod_tree_remove_init(struct module *mod);
304 void mod_tree_remove(struct module *mod);
305 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
306 #else /* !CONFIG_MODULES_TREE_LOOKUP */
308 static inline void mod_tree_insert(struct module *mod) { }
309 static inline void mod_tree_remove_init(struct module *mod) { }
310 static inline void mod_tree_remove(struct module *mod) { }
311 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
315 list_for_each_entry_rcu(mod, &modules, list,
316 lockdep_is_held(&module_mutex)) {
317 if (within_module(addr, mod))
323 #endif /* CONFIG_MODULES_TREE_LOOKUP */
325 void module_enable_ro(const struct module *mod, bool after_init);
326 void module_enable_nx(const struct module *mod);
327 void module_enable_x(const struct module *mod);
328 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
329 char *secstrings, struct module *mod);
331 #ifdef CONFIG_MODULE_SIG
332 int module_sig_check(struct load_info *info, int flags);
333 #else /* !CONFIG_MODULE_SIG */
334 static inline int module_sig_check(struct load_info *info, int flags)
338 #endif /* !CONFIG_MODULE_SIG */
340 #ifdef CONFIG_DEBUG_KMEMLEAK
341 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
342 #else /* !CONFIG_DEBUG_KMEMLEAK */
343 static inline void kmemleak_load_module(const struct module *mod,
344 const struct load_info *info) { }
345 #endif /* CONFIG_DEBUG_KMEMLEAK */
347 #ifdef CONFIG_KALLSYMS
348 void init_build_id(struct module *mod, const struct load_info *info);
349 void layout_symtab(struct module *mod, struct load_info *info);
350 void add_kallsyms(struct module *mod, const struct load_info *info);
352 static inline bool sect_empty(const Elf_Shdr *sect)
354 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
356 #else /* !CONFIG_KALLSYMS */
357 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
358 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
359 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
360 #endif /* CONFIG_KALLSYMS */
363 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
364 struct kernel_param *kparam, unsigned int num_params);
365 void mod_sysfs_teardown(struct module *mod);
366 void init_param_lock(struct module *mod);
367 #else /* !CONFIG_SYSFS */
368 static inline int mod_sysfs_setup(struct module *mod,
369 const struct load_info *info,
370 struct kernel_param *kparam,
371 unsigned int num_params)
376 static inline void mod_sysfs_teardown(struct module *mod) { }
377 static inline void init_param_lock(struct module *mod) { }
378 #endif /* CONFIG_SYSFS */
380 #ifdef CONFIG_MODVERSIONS
381 int check_version(const struct load_info *info,
382 const char *symname, struct module *mod, const s32 *crc);
383 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
384 struct kernel_symbol *ks, struct tracepoint * const *tp);
385 int check_modstruct_version(const struct load_info *info, struct module *mod);
386 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
387 #else /* !CONFIG_MODVERSIONS */
388 static inline int check_version(const struct load_info *info,
396 static inline int check_modstruct_version(const struct load_info *info,
402 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
404 return strcmp(amagic, bmagic) == 0;
406 #endif /* CONFIG_MODVERSIONS */