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 
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 
module_assert_mutex_or_preempt(void)127 static 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 
kernel_symbol_value(const struct kernel_symbol * sym)138 static 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
148 int copy_module_elf(struct module *mod, struct load_info *info);
149 void free_module_elf(struct module *mod);
150 #else /* !CONFIG_LIVEPATCH */
copy_module_elf(struct module * mod,struct load_info * info)151 static inline int copy_module_elf(struct module *mod, struct load_info *info)
152 {
153 	return 0;
154 }
155 
free_module_elf(struct module * mod)156 static inline void free_module_elf(struct module *mod) { }
157 #endif /* CONFIG_LIVEPATCH */
158 
set_livepatch_module(struct module * mod)159 static 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  */
190 enum fail_dup_mod_reason {
191 	FAIL_DUP_MOD_BECOMING = 0,
192 	FAIL_DUP_MOD_LOAD,
193 };
194 
195 #ifdef CONFIG_MODULE_DEBUGFS
196 extern 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 
204 extern atomic_long_t total_mod_size;
205 extern atomic_long_t total_text_size;
206 extern atomic_long_t invalid_kread_bytes;
207 extern atomic_long_t invalid_decompress_bytes;
208 
209 extern atomic_t modcount;
210 extern atomic_t failed_kreads;
211 extern atomic_t failed_decompress;
212 struct 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 
219 int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason);
220 void mod_stat_bump_invalid(struct load_info *info, int flags);
221 void 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 
try_add_failed_module(const char * name,enum fail_dup_mod_reason reason)228 static inline int try_add_failed_module(const char *name,
229 					enum fail_dup_mod_reason reason)
230 {
231 	return 0;
232 }
233 
mod_stat_bump_invalid(struct load_info * info,int flags)234 static inline void mod_stat_bump_invalid(struct load_info *info, int flags)
235 {
236 }
237 
mod_stat_bump_becoming(struct load_info * info,int flags)238 static 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
245 bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret);
246 void kmod_dup_request_announce(char *module_name, int ret);
247 #else
kmod_dup_request_exists_wait(char * module_name,bool wait,int * dup_ret)248 static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
249 {
250 	return false;
251 }
252 
kmod_dup_request_announce(char * module_name,int ret)253 static inline void kmod_dup_request_announce(char *module_name, int ret)
254 {
255 }
256 #endif
257 
258 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
259 struct mod_unload_taint {
260 	struct list_head list;
261 	char name[MODULE_NAME_LEN];
262 	unsigned long taints;
263 	u64 count;
264 };
265 
266 int try_add_tainted_module(struct module *mod);
267 void print_unloaded_tainted_modules(void);
268 #else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
try_add_tainted_module(struct module * mod)269 static inline int try_add_tainted_module(struct module *mod)
270 {
271 	return 0;
272 }
273 
print_unloaded_tainted_modules(void)274 static inline void print_unloaded_tainted_modules(void)
275 {
276 }
277 #endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
278 
279 #ifdef CONFIG_MODULE_DECOMPRESS
280 int module_decompress(struct load_info *info, const void *buf, size_t size);
281 void module_decompress_cleanup(struct load_info *info);
282 #else
module_decompress(struct load_info * info,const void * buf,size_t size)283 static inline int module_decompress(struct load_info *info,
284 				    const void *buf, size_t size)
285 {
286 	return -EOPNOTSUPP;
287 }
288 
module_decompress_cleanup(struct load_info * info)289 static inline void module_decompress_cleanup(struct load_info *info)
290 {
291 }
292 #endif
293 
294 struct 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 
306 extern struct mod_tree_root mod_tree;
307 
308 #ifdef CONFIG_MODULES_TREE_LOOKUP
309 void mod_tree_insert(struct module *mod);
310 void mod_tree_remove_init(struct module *mod);
311 void mod_tree_remove(struct module *mod);
312 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
313 #else /* !CONFIG_MODULES_TREE_LOOKUP */
314 
mod_tree_insert(struct module * mod)315 static inline void mod_tree_insert(struct module *mod) { }
mod_tree_remove_init(struct module * mod)316 static inline void mod_tree_remove_init(struct module *mod) { }
mod_tree_remove(struct module * mod)317 static inline void mod_tree_remove(struct module *mod) { }
mod_find(unsigned long addr,struct mod_tree_root * tree)318 static 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 
332 int module_enable_rodata_ro(const struct module *mod);
333 int module_enable_rodata_ro_after_init(const struct module *mod);
334 int module_enable_data_nx(const struct module *mod);
335 int module_enable_text_rox(const struct module *mod);
336 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
337 				char *secstrings, struct module *mod);
338 
339 #ifdef CONFIG_MODULE_SIG
340 int module_sig_check(struct load_info *info, int flags);
341 #else /* !CONFIG_MODULE_SIG */
module_sig_check(struct load_info * info,int flags)342 static 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
349 void kmemleak_load_module(const struct module *mod, const struct load_info *info);
350 #else /* !CONFIG_DEBUG_KMEMLEAK */
kmemleak_load_module(const struct module * mod,const struct load_info * info)351 static 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
356 void init_build_id(struct module *mod, const struct load_info *info);
357 void layout_symtab(struct module *mod, struct load_info *info);
358 void add_kallsyms(struct module *mod, const struct load_info *info);
359 
sect_empty(const Elf_Shdr * sect)360 static 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 */
init_build_id(struct module * mod,const struct load_info * info)365 static inline void init_build_id(struct module *mod, const struct load_info *info) { }
layout_symtab(struct module * mod,struct load_info * info)366 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
add_kallsyms(struct module * mod,const struct load_info * info)367 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
368 #endif /* CONFIG_KALLSYMS */
369 
370 #ifdef CONFIG_SYSFS
371 int mod_sysfs_setup(struct module *mod, const struct load_info *info,
372 		    struct kernel_param *kparam, unsigned int num_params);
373 void mod_sysfs_teardown(struct module *mod);
374 void init_param_lock(struct module *mod);
375 #else /* !CONFIG_SYSFS */
mod_sysfs_setup(struct module * mod,const struct load_info * info,struct kernel_param * kparam,unsigned int num_params)376 static 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 
mod_sysfs_teardown(struct module * mod)384 static inline void mod_sysfs_teardown(struct module *mod) { }
init_param_lock(struct module * mod)385 static inline void init_param_lock(struct module *mod) { }
386 #endif /* CONFIG_SYSFS */
387 
388 #ifdef CONFIG_MODVERSIONS
389 int check_version(const struct load_info *info,
390 		  const char *symname, struct module *mod, const u32 *crc);
391 void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
392 		   struct kernel_symbol *ks, struct tracepoint * const *tp);
393 int check_modstruct_version(const struct load_info *info, struct module *mod);
394 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
395 struct modversion_info_ext {
396 	size_t remaining;
397 	const u32 *crc;
398 	const char *name;
399 };
400 void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver);
401 void 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 */
check_version(const struct load_info * info,const char * symname,struct module * mod,const u32 * crc)405 static 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 
check_modstruct_version(const struct load_info * info,struct module * mod)413 static inline int check_modstruct_version(const struct load_info *info,
414 					  struct module *mod)
415 {
416 	return 1;
417 }
418 
same_magic(const char * amagic,const char * bmagic,bool has_crcs)419 static 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 */
424