projects/linux·blob v6.14 · refs

423 lines · 12.4 KiB

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 ([email protected])
6 * Copyright (C) 2023 Luis Chamberlain <[email protected]>
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
35struct 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
47extern struct mutex module_mutex;
48extern struct list_head modules;
49
50extern const struct module_attribute *const modinfo_attrs[];
51extern const size_t modinfo_attrs_count;
52
53/* Provided by the linker */
54extern const struct kernel_symbol __start___ksymtab[];
55extern const struct kernel_symbol __stop___ksymtab[];
56extern const struct kernel_symbol __start___ksymtab_gpl[];
57extern const struct kernel_symbol __stop___ksymtab_gpl[];
58extern const u32 __start___kcrctab[];
59extern const u32 __start___kcrctab_gpl[];
60
61struct 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
94enum mod_license {
95 NOT_GPL_ONLY,
96 GPL_ONLY,
97};
98
99struct 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
112int mod_verify_sig(const void *mod, struct load_info *info);
113int try_to_force_load(struct module *mod, const char *reason);
114bool find_symbol(struct find_symbol_arg *fsa);
115struct module *find_module_all(const char *name, size_t len, bool even_unformed);
116int cmp_name(const void *name, const void *sym);
117long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
118 Elf_Shdr *sechdr, unsigned int section);
119char *module_flags(struct module *mod, char *buf, bool show_state);
120size_t module_flags_taint(unsigned long taints, char *buf);
121
122char *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
127static inline void module_assert_mutex_or_preempt(void)
128{
129#ifdef CONFIG_LOCKDEP
130 if (unlikely(!debug_locks))
131 return;
132
133 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
134 !lockdep_is_held(&module_mutex));
135#endif
136}
137
138static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
139{
140#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
141 return (unsigned long)offset_to_ptr(&sym->value_offset);
142#else
143 return sym->value;
144#endif
145}
146
147#ifdef CONFIG_LIVEPATCH
148int copy_module_elf(struct module *mod, struct load_info *info);
149void free_module_elf(struct module *mod);
150#else /* !CONFIG_LIVEPATCH */
151static inline int copy_module_elf(struct module *mod, struct load_info *info)
152{
153 return 0;
154}
155
156static inline void free_module_elf(struct module *mod) { }
157#endif /* CONFIG_LIVEPATCH */
158
159static inline bool set_livepatch_module(struct module *mod)
160{
161#ifdef CONFIG_LIVEPATCH
162 mod->klp = true;
163 return true;
164#else
165 return false;
166#endif
167}
168
169/**
170 * enum fail_dup_mod_reason - state at which a duplicate module was detected
171 *
172 * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but
173 * we've determined that another module with the same name is already loaded
174 * or being processed on our &modules list. This happens on early_mod_check()
175 * right before layout_and_allocate(). The kernel would have already
176 * vmalloc()'d space for the entire module through finit_module(). If
177 * decompression was used two vmap() spaces were used. These failures can
178 * happen when userspace has not seen the module present on the kernel and
179 * tries to load the module multiple times at same time.
180 * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation
181 * checks and the kernel determines that the module was unique and because
182 * of this allocated yet another private kernel copy of the module space in
183 * layout_and_allocate() but after this determined in add_unformed_module()
184 * that another module with the same name is already loaded or being processed.
185 * These failures should be mitigated as much as possible and are indicative
186 * of really fast races in loading modules. Without module decompression
187 * they waste twice as much vmap space. With module decompression three
188 * times the module's size vmap space is wasted.
189 */
190enum fail_dup_mod_reason {
191 FAIL_DUP_MOD_BECOMING = 0,
192 FAIL_DUP_MOD_LOAD,
193};
194
195#ifdef CONFIG_MODULE_DEBUGFS
196extern struct dentry *mod_debugfs_root;
197#endif
198
199#ifdef CONFIG_MODULE_STATS
200
201#define mod_stat_add_long(count, var) atomic_long_add(count, var)
202#define mod_stat_inc(name) atomic_inc(name)
203
204extern atomic_long_t total_mod_size;
205extern atomic_long_t total_text_size;
206extern atomic_long_t invalid_kread_bytes;
207extern atomic_long_t invalid_decompress_bytes;
208
209extern atomic_t modcount;
210extern atomic_t failed_kreads;
211extern atomic_t failed_decompress;
212struct mod_fail_load {
213 struct list_head list;
214 char name[MODULE_NAME_LEN];
215 atomic_long_t count;
216 unsigned long dup_fail_mask;
217};
218
219int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
220void mod_stat_bump_invalid(struct load_info *info, int flags);
221void mod_stat_bump_becoming(struct load_info *info, int flags);
222
223#else
224
225#define mod_stat_add_long(name, var)
226#define mod_stat_inc(name)
227
228static inline int try_add_failed_module(const char *name,
229 enum fail_dup_mod_reason reason)
230{
231 return 0;
232}
233
234static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
235{
236}
237
238static inline void mod_stat_bump_becoming(struct load_info *info, int flags)
239{
240}
241
242#endif /* CONFIG_MODULE_STATS */
243
244#ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS
245bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
246void kmod_dup_request_announce(char *module_name, int ret);
247#else
248static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
249{
250 return false;
251}
252
253static inline void kmod_dup_request_announce(char *module_name, int ret)
254{
255}
256#endif
257
258#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
259struct mod_unload_taint {
260 struct list_head list;
261 char name[MODULE_NAME_LEN];
262 unsigned long taints;
263 u64 count;
264};
265
266int try_add_tainted_module(struct module *mod);
267void print_unloaded_tainted_modules(void);
268#else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
269static inline int try_add_tainted_module(struct module *mod)
270{
271 return 0;
272}
273
274static inline void print_unloaded_tainted_modules(void)
275{
276}
277#endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
278
279#ifdef CONFIG_MODULE_DECOMPRESS
280int module_decompress(struct load_info *info, const void *buf, size_t size);
281void module_decompress_cleanup(struct load_info *info);
282#else
283static inline int module_decompress(struct load_info *info,
284 const void *buf, size_t size)
285{
286 return -EOPNOTSUPP;
287}
288
289static inline void module_decompress_cleanup(struct load_info *info)
290{
291}
292#endif
293
294struct mod_tree_root {
295#ifdef CONFIG_MODULES_TREE_LOOKUP
296 struct latch_tree_root root;
297#endif
298 unsigned long addr_min;
299 unsigned long addr_max;
300#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
301 unsigned long data_addr_min;
302 unsigned long data_addr_max;
303#endif
304};
305
306extern struct mod_tree_root mod_tree;
307
308#ifdef CONFIG_MODULES_TREE_LOOKUP
309void mod_tree_insert(struct module *mod);
310void mod_tree_remove_init(struct module *mod);
311void mod_tree_remove(struct module *mod);
312struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
313#else /* !CONFIG_MODULES_TREE_LOOKUP */
314
315static inline void mod_tree_insert(struct module *mod) { }
316static inline void mod_tree_remove_init(struct module *mod) { }
317static inline void mod_tree_remove(struct module *mod) { }
318static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
319{
320 struct module *mod;
321
322 list_for_each_entry_rcu(mod, &modules, list,
323 lockdep_is_held(&module_mutex)) {
324 if (within_module(addr, mod))
325 return mod;
326 }
327
328 return NULL;
329}
330#endif /* CONFIG_MODULES_TREE_LOOKUP */
331
332int module_enable_rodata_ro(const struct module *mod);
333int module_enable_rodata_ro_after_init(const struct module *mod);
334int module_enable_data_nx(const struct module *mod);
335int module_enable_text_rox(const struct module *mod);
336int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
337 char *secstrings, struct module *mod);
338
339#ifdef CONFIG_MODULE_SIG
340int module_sig_check(struct load_info *info, int flags);
341#else /* !CONFIG_MODULE_SIG */
342static inline int module_sig_check(struct load_info *info, int flags)
343{
344 return 0;
345}
346#endif /* !CONFIG_MODULE_SIG */
347
348#ifdef CONFIG_DEBUG_KMEMLEAK
349void kmemleak_load_module(const struct module *mod, const struct load_info *info);
350#else /* !CONFIG_DEBUG_KMEMLEAK */
351static inline void kmemleak_load_module(const struct module *mod,
352 const struct load_info *info) { }
353#endif /* CONFIG_DEBUG_KMEMLEAK */
354
355#ifdef CONFIG_KALLSYMS
356void init_build_id(struct module *mod, const struct load_info *info);
357void layout_symtab(struct module *mod, struct load_info *info);
358void add_kallsyms(struct module *mod, const struct load_info *info);
359
360static inline bool sect_empty(const Elf_Shdr *sect)
361{
362 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
363}
364#else /* !CONFIG_KALLSYMS */
365static inline void init_build_id(struct module *mod, const struct load_info *info) { }
366static inline void layout_symtab(struct module *mod, struct load_info *info) { }
367static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
368#endif /* CONFIG_KALLSYMS */
369
370#ifdef CONFIG_SYSFS
371int mod_sysfs_setup(struct module *mod, const struct load_info *info,
372 struct kernel_param *kparam, unsigned int num_params);
373void mod_sysfs_teardown(struct module *mod);
374void init_param_lock(struct module *mod);
375#else /* !CONFIG_SYSFS */
376static inline int mod_sysfs_setup(struct module *mod,
377 const struct load_info *info,
378 struct kernel_param *kparam,
379 unsigned int num_params)
380{
381 return 0;
382}
383
384static inline void mod_sysfs_teardown(struct module *mod) { }
385static inline void init_param_lock(struct module *mod) { }
386#endif /* CONFIG_SYSFS */
387
388#ifdef CONFIG_MODVERSIONS
389int check_version(const struct load_info *info,
390 const char *symname, struct module *mod, const u32 *crc);
391void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
392 struct kernel_symbol *ks, struct tracepoint * const *tp);
393int check_modstruct_version(const struct load_info *info, struct module *mod);
394int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
395struct modversion_info_ext {
396 size_t remaining;
397 const u32 *crc;
398 const char *name;
399};
400void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver);
401void modversion_ext_advance(struct modversion_info_ext *ver);
402#define for_each_modversion_info_ext(ver, info) \
403 for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver))
404#else /* !CONFIG_MODVERSIONS */
405static inline int check_version(const struct load_info *info,
406 const char *symname,
407 struct module *mod,
408 const u32 *crc)
409{
410 return 1;
411}
412
413static inline int check_modstruct_version(const struct load_info *info,
414 struct module *mod)
415{
416 return 1;
417}
418
419static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
420{
421 return strcmp(amagic, bmagic) == 0;
422}
423#endif /* CONFIG_MODVERSIONS */