1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
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>
15 #ifndef ARCH_SHF_SMALL
16 #define ARCH_SHF_SMALL 0
19 /* If this is set, the section belongs in the init part of the module */
20 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
21 /* Maximum number of characters written by module_flags() */
22 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
24 #ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
25 #define data_layout core_layout
29 * Modules' sections will be aligned on page boundaries
30 * to ensure complete separation of code and data, but
31 * only when CONFIG_STRICT_MODULE_RWX=y
33 #ifdef CONFIG_STRICT_MODULE_RWX
34 # define strict_align(X) PAGE_ALIGN(X)
36 # define strict_align(X) (X)
39 extern struct mutex module_mutex;
40 extern struct list_head modules;
42 extern struct module_attribute *modinfo_attrs[];
43 extern size_t modinfo_attrs_count;
45 /* Provided by the linker */
46 extern const struct kernel_symbol __start___ksymtab[];
47 extern const struct kernel_symbol __stop___ksymtab[];
48 extern const struct kernel_symbol __start___ksymtab_gpl[];
49 extern const struct kernel_symbol __stop___ksymtab_gpl[];
50 extern const s32 __start___kcrctab[];
51 extern const s32 __start___kcrctab_gpl[];
55 /* pointer to module in temporary copy, freed at end of load_module() */
60 char *secstrings, *strtab;
61 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
62 struct _ddebug *debug;
63 unsigned int num_debug;
65 #ifdef CONFIG_KALLSYMS
66 unsigned long mod_kallsyms_init_off;
68 #ifdef CONFIG_MODULE_DECOMPRESS
70 unsigned int max_pages;
71 unsigned int used_pages;
74 unsigned int sym, str, mod, vers, info, pcpu;
83 struct find_symbol_arg {
92 const struct kernel_symbol *sym;
93 enum mod_license license;
96 int mod_verify_sig(const void *mod, struct load_info *info);
97 int try_to_force_load(struct module *mod, const char *reason);
98 bool find_symbol(struct find_symbol_arg *fsa);
99 struct module *find_module_all(const char *name, size_t len, bool even_unformed);
100 int cmp_name(const void *name, const void *sym);
101 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
102 unsigned int section);
103 char *module_flags(struct module *mod, char *buf);
104 size_t module_flags_taint(unsigned long taints, char *buf);
106 static inline void module_assert_mutex_or_preempt(void)
108 #ifdef CONFIG_LOCKDEP
109 if (unlikely(!debug_locks))
112 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
113 !lockdep_is_held(&module_mutex));
117 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
119 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
120 return (unsigned long)offset_to_ptr(&sym->value_offset);
126 #ifdef CONFIG_LIVEPATCH
127 int copy_module_elf(struct module *mod, struct load_info *info);
128 void free_module_elf(struct module *mod);
129 #else /* !CONFIG_LIVEPATCH */
130 static inline int copy_module_elf(struct module *mod, struct load_info *info)
135 static inline void free_module_elf(struct module *mod) { }
136 #endif /* CONFIG_LIVEPATCH */
138 static inline bool set_livepatch_module(struct module *mod)
140 #ifdef CONFIG_LIVEPATCH
148 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
149 struct mod_unload_taint {
150 struct list_head list;
151 char name[MODULE_NAME_LEN];
152 unsigned long taints;
156 int try_add_tainted_module(struct module *mod);
157 void print_unloaded_tainted_modules(void);
158 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
159 static inline int try_add_tainted_module(struct module *mod)
164 static inline void print_unloaded_tainted_modules(void)
167 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
169 #ifdef CONFIG_MODULE_DECOMPRESS
170 int module_decompress(struct load_info *info, const void *buf, size_t size);
171 void module_decompress_cleanup(struct load_info *info);
173 static inline int module_decompress(struct load_info *info,
174 const void *buf, size_t size)
179 static inline void module_decompress_cleanup(struct load_info *info)
184 struct mod_tree_root {
185 #ifdef CONFIG_MODULES_TREE_LOOKUP
186 struct latch_tree_root root;
188 unsigned long addr_min;
189 unsigned long addr_max;
192 extern struct mod_tree_root mod_tree;
193 extern struct mod_tree_root mod_data_tree;
195 #ifdef CONFIG_MODULES_TREE_LOOKUP
196 void mod_tree_insert(struct module *mod);
197 void mod_tree_remove_init(struct module *mod);
198 void mod_tree_remove(struct module *mod);
199 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
200 #else /* !CONFIG_MODULES_TREE_LOOKUP */
202 static inline void mod_tree_insert(struct module *mod) { }
203 static inline void mod_tree_remove_init(struct module *mod) { }
204 static inline void mod_tree_remove(struct module *mod) { }
205 static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
209 list_for_each_entry_rcu(mod, &modules, list,
210 lockdep_is_held(&module_mutex)) {
211 if (within_module(addr, mod))
217 #endif /* CONFIG_MODULES_TREE_LOOKUP */
219 void module_enable_ro(const struct module *mod, bool after_init);
220 void module_enable_nx(const struct module *mod);
221 void module_enable_x(const struct module *mod);
222 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
223 char *secstrings, struct module *mod);
224 bool module_check_misalignment(const struct module *mod);
226 #ifdef CONFIG_MODULE_SIG
227 int module_sig_check(struct load_info *info, int flags);
228 #else /* !CONFIG_MODULE_SIG */
229 static inline int module_sig_check(struct load_info *info, int flags)
233 #endif /* !CONFIG_MODULE_SIG */
235 #ifdef CONFIG_DEBUG_KMEMLEAK
236 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
237 #else /* !CONFIG_DEBUG_KMEMLEAK */
238 static inline void kmemleak_load_module(const struct module *mod,
239 const struct load_info *info) { }
240 #endif /* CONFIG_DEBUG_KMEMLEAK */
242 #ifdef CONFIG_KALLSYMS
243 void init_build_id(struct module *mod, const struct load_info *info);
244 void layout_symtab(struct module *mod, struct load_info *info);
245 void add_kallsyms(struct module *mod, const struct load_info *info);
246 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
248 static inline bool sect_empty(const Elf_Shdr *sect)
250 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
252 #else /* !CONFIG_KALLSYMS */
253 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
254 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
255 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
256 #endif /* CONFIG_KALLSYMS */
259 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
260 struct kernel_param *kparam, unsigned int num_params);
261 void mod_sysfs_teardown(struct module *mod);
262 void init_param_lock(struct module *mod);
263 #else /* !CONFIG_SYSFS */
264 static inline int mod_sysfs_setup(struct module *mod,
265 const struct load_info *info,
266 struct kernel_param *kparam,
267 unsigned int num_params)
272 static inline void mod_sysfs_teardown(struct module *mod) { }
273 static inline void init_param_lock(struct module *mod) { }
274 #endif /* CONFIG_SYSFS */
276 #ifdef CONFIG_MODVERSIONS
277 int check_version(const struct load_info *info,
278 const char *symname, struct module *mod, const s32 *crc);
279 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
280 struct kernel_symbol *ks, struct tracepoint * const *tp);
281 int check_modstruct_version(const struct load_info *info, struct module *mod);
282 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
283 #else /* !CONFIG_MODVERSIONS */
284 static inline int check_version(const struct load_info *info,
292 static inline int check_modstruct_version(const struct load_info *info,
298 static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
300 return strcmp(amagic, bmagic) == 0;
302 #endif /* CONFIG_MODVERSIONS */