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