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