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)
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>
16 #ifndef ARCH_SHF_SMALL
17 #define ARCH_SHF_SMALL 0
20 /* If this is set, the section belongs in the init part of the module */
21 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
22 /* Maximum number of characters written by module_flags() */
23 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
25 #ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
26 #define data_layout core_layout
30 * Modules' sections will be aligned on page boundaries
31 * to ensure complete separation of code and data, but
32 * only when CONFIG_STRICT_MODULE_RWX=y
34 static inline unsigned int strict_align(unsigned int size)
36 if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
37 return PAGE_ALIGN(size);
42 extern struct mutex module_mutex;
43 extern struct list_head modules;
45 extern struct module_attribute *modinfo_attrs[];
46 extern size_t modinfo_attrs_count;
48 /* Provided by the linker */
49 extern const struct kernel_symbol __start___ksymtab[];
50 extern const struct kernel_symbol __stop___ksymtab[];
51 extern const struct kernel_symbol __start___ksymtab_gpl[];
52 extern const struct kernel_symbol __stop___ksymtab_gpl[];
53 extern const s32 __start___kcrctab[];
54 extern const s32 __start___kcrctab_gpl[];
56 #include <linux/dynamic_debug.h>
59 /* pointer to module in temporary copy, freed at end of load_module() */
64 char *secstrings, *strtab;
65 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
66 struct _ddebug_info dyndbg;
68 #ifdef CONFIG_KALLSYMS
69 unsigned long mod_kallsyms_init_off;
71 #ifdef CONFIG_MODULE_DECOMPRESS
73 unsigned int max_pages;
74 unsigned int used_pages;
77 unsigned int sym, str, mod, vers, info, pcpu;
86 struct find_symbol_arg {
95 const struct kernel_symbol *sym;
96 enum mod_license license;
99 int mod_verify_sig(const void *mod, struct load_info *info);
100 int try_to_force_load(struct module *mod, const char *reason);
101 bool find_symbol(struct find_symbol_arg *fsa);
102 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
103 int cmp_name(const void *name, const void *sym);
104 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
105 unsigned int section);
106 char *module_flags(struct module *mod, char *buf, bool show_state);
107 size_t module_flags_taint(unsigned long taints, char *buf);
109 static inline void module_assert_mutex_or_preempt(void)
111 #ifdef CONFIG_LOCKDEP
112 if (unlikely(!debug_locks))
115 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
116 !lockdep_is_held(&module_mutex));
120 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
122 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
123 return (unsigned long)offset_to_ptr(&sym->value_offset);
129 #ifdef CONFIG_LIVEPATCH
130 int copy_module_elf(struct module *mod, struct load_info *info);
131 void free_module_elf(struct module *mod);
132 #else /* !CONFIG_LIVEPATCH */
133 static inline int copy_module_elf(struct module *mod, struct load_info *info)
138 static inline void free_module_elf(struct module *mod) { }
139 #endif /* CONFIG_LIVEPATCH */
141 static inline bool set_livepatch_module(struct module *mod)
143 #ifdef CONFIG_LIVEPATCH
151 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
152 struct mod_unload_taint {
153 struct list_head list;
154 char name[MODULE_NAME_LEN];
155 unsigned long taints;
159 int try_add_tainted_module(struct module *mod);
160 void print_unloaded_tainted_modules(void);
161 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
162 static inline int try_add_tainted_module(struct module *mod)
167 static inline void print_unloaded_tainted_modules(void)
170 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
172 #ifdef CONFIG_MODULE_DECOMPRESS
173 int module_decompress(struct load_info *info, const void *buf, size_t size);
174 void module_decompress_cleanup(struct load_info *info);
176 static inline int module_decompress(struct load_info *info,
177 const void *buf, size_t size)
182 static inline void module_decompress_cleanup(struct load_info *info)
187 struct mod_tree_root {
188 #ifdef CONFIG_MODULES_TREE_LOOKUP
189 struct latch_tree_root root;
191 unsigned long addr_min;
192 unsigned long addr_max;
195 extern struct mod_tree_root mod_tree;
196 extern struct mod_tree_root mod_data_tree;
198 #ifdef CONFIG_MODULES_TREE_LOOKUP
199 void mod_tree_insert(struct module *mod);
200 void mod_tree_remove_init(struct module *mod);
201 void mod_tree_remove(struct module *mod);
202 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
203 #else /* !CONFIG_MODULES_TREE_LOOKUP */
205 static inline void mod_tree_insert(struct module *mod) { }
206 static inline void mod_tree_remove_init(struct module *mod) { }
207 static inline void mod_tree_remove(struct module *mod) { }
208 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
212 list_for_each_entry_rcu(mod, &modules, list,
213 lockdep_is_held(&module_mutex)) {
214 if (within_module(addr, mod))
220 #endif /* CONFIG_MODULES_TREE_LOOKUP */
222 void module_enable_ro(const struct module *mod, bool after_init);
223 void module_enable_nx(const struct module *mod);
224 void module_enable_x(const struct module *mod);
225 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
226 char *secstrings, struct module *mod);
227 bool module_check_misalignment(const struct module *mod);
229 #ifdef CONFIG_MODULE_SIG
230 int module_sig_check(struct load_info *info, int flags);
231 #else /* !CONFIG_MODULE_SIG */
232 static inline int module_sig_check(struct load_info *info, int flags)
236 #endif /* !CONFIG_MODULE_SIG */
238 #ifdef CONFIG_DEBUG_KMEMLEAK
239 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
240 #else /* !CONFIG_DEBUG_KMEMLEAK */
241 static inline void kmemleak_load_module(const struct module *mod,
242 const struct load_info *info) { }
243 #endif /* CONFIG_DEBUG_KMEMLEAK */
245 #ifdef CONFIG_KALLSYMS
246 void init_build_id(struct module *mod, const struct load_info *info);
247 void layout_symtab(struct module *mod, struct load_info *info);
248 void add_kallsyms(struct module *mod, const struct load_info *info);
249 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
251 static inline bool sect_empty(const Elf_Shdr *sect)
253 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
255 #else /* !CONFIG_KALLSYMS */
256 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
257 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
258 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
259 #endif /* CONFIG_KALLSYMS */
262 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
263 struct kernel_param *kparam, unsigned int num_params);
264 void mod_sysfs_teardown(struct module *mod);
265 void init_param_lock(struct module *mod);
266 #else /* !CONFIG_SYSFS */
267 static inline int mod_sysfs_setup(struct module *mod,
268 const struct load_info *info,
269 struct kernel_param *kparam,
270 unsigned int num_params)
275 static inline void mod_sysfs_teardown(struct module *mod) { }
276 static inline void init_param_lock(struct module *mod) { }
277 #endif /* CONFIG_SYSFS */
279 #ifdef CONFIG_MODVERSIONS
280 int check_version(const struct load_info *info,
281 const char *symname, struct module *mod, const s32 *crc);
282 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
283 struct kernel_symbol *ks, struct tracepoint * const *tp);
284 int check_modstruct_version(const struct load_info *info, struct module *mod);
285 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
286 #else /* !CONFIG_MODVERSIONS */
287 static inline int check_version(const struct load_info *info,
295 static inline int check_modstruct_version(const struct load_info *info,
301 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
303 return strcmp(amagic, bmagic) == 0;
305 #endif /* CONFIG_MODVERSIONS */