xref: /linux-6.15/kernel/module/main.c (revision febaa65c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2002 Richard Henderson
4  * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
5  * Copyright (C) 2023 Luis Chamberlain <[email protected]>
6  */
7 
8 #define INCLUDE_VERMAGIC
9 
10 #include <linux/export.h>
11 #include <linux/extable.h>
12 #include <linux/moduleloader.h>
13 #include <linux/module_signature.h>
14 #include <linux/trace_events.h>
15 #include <linux/init.h>
16 #include <linux/kallsyms.h>
17 #include <linux/buildid.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/kernel_read_file.h>
21 #include <linux/kstrtox.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/elf.h>
25 #include <linux/seq_file.h>
26 #include <linux/syscalls.h>
27 #include <linux/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/capability.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/sched.h>
37 #include <linux/device.h>
38 #include <linux/string.h>
39 #include <linux/mutex.h>
40 #include <linux/rculist.h>
41 #include <linux/uaccess.h>
42 #include <asm/cacheflush.h>
43 #include <linux/set_memory.h>
44 #include <asm/mmu_context.h>
45 #include <linux/license.h>
46 #include <asm/sections.h>
47 #include <linux/tracepoint.h>
48 #include <linux/ftrace.h>
49 #include <linux/livepatch.h>
50 #include <linux/async.h>
51 #include <linux/percpu.h>
52 #include <linux/kmemleak.h>
53 #include <linux/jump_label.h>
54 #include <linux/pfn.h>
55 #include <linux/bsearch.h>
56 #include <linux/dynamic_debug.h>
57 #include <linux/audit.h>
58 #include <linux/cfi.h>
59 #include <linux/codetag.h>
60 #include <linux/debugfs.h>
61 #include <linux/execmem.h>
62 #include <uapi/linux/module.h>
63 #include "internal.h"
64 
65 #define CREATE_TRACE_POINTS
66 #include <trace/events/module.h>
67 
68 /*
69  * Mutex protects:
70  * 1) List of modules (also safely readable within RCU read section),
71  * 2) module_use links,
72  * 3) mod_tree.addr_min/mod_tree.addr_max.
73  * (delete and add uses RCU list operations).
74  */
75 DEFINE_MUTEX(module_mutex);
76 LIST_HEAD(modules);
77 
78 /* Work queue for freeing init sections in success case */
79 static void do_free_init(struct work_struct *w);
80 static DECLARE_WORK(init_free_wq, do_free_init);
81 static LLIST_HEAD(init_free_list);
82 
83 struct mod_tree_root mod_tree __cacheline_aligned = {
84 	.addr_min = -1UL,
85 };
86 
87 struct symsearch {
88 	const struct kernel_symbol *start, *stop;
89 	const u32 *crcs;
90 	enum mod_license license;
91 };
92 
93 /*
94  * Bounds of module memory, for speeding up __module_address.
95  * Protected by module_mutex.
96  */
97 static void __mod_update_bounds(enum mod_mem_type type __maybe_unused, void *base,
98 				unsigned int size, struct mod_tree_root *tree)
99 {
100 	unsigned long min = (unsigned long)base;
101 	unsigned long max = min + size;
102 
103 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
104 	if (mod_mem_type_is_core_data(type)) {
105 		if (min < tree->data_addr_min)
106 			tree->data_addr_min = min;
107 		if (max > tree->data_addr_max)
108 			tree->data_addr_max = max;
109 		return;
110 	}
111 #endif
112 	if (min < tree->addr_min)
113 		tree->addr_min = min;
114 	if (max > tree->addr_max)
115 		tree->addr_max = max;
116 }
117 
118 static void mod_update_bounds(struct module *mod)
119 {
120 	for_each_mod_mem_type(type) {
121 		struct module_memory *mod_mem = &mod->mem[type];
122 
123 		if (mod_mem->size)
124 			__mod_update_bounds(type, mod_mem->base, mod_mem->size, &mod_tree);
125 	}
126 }
127 
128 /* Block module loading/unloading? */
129 int modules_disabled;
130 core_param(nomodule, modules_disabled, bint, 0);
131 
132 /* Waiting for a module to finish initializing? */
133 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
134 
135 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
136 
137 int register_module_notifier(struct notifier_block *nb)
138 {
139 	return blocking_notifier_chain_register(&module_notify_list, nb);
140 }
141 EXPORT_SYMBOL(register_module_notifier);
142 
143 int unregister_module_notifier(struct notifier_block *nb)
144 {
145 	return blocking_notifier_chain_unregister(&module_notify_list, nb);
146 }
147 EXPORT_SYMBOL(unregister_module_notifier);
148 
149 /*
150  * We require a truly strong try_module_get(): 0 means success.
151  * Otherwise an error is returned due to ongoing or failed
152  * initialization etc.
153  */
154 static inline int strong_try_module_get(struct module *mod)
155 {
156 	BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
157 	if (mod && mod->state == MODULE_STATE_COMING)
158 		return -EBUSY;
159 	if (try_module_get(mod))
160 		return 0;
161 	else
162 		return -ENOENT;
163 }
164 
165 static inline void add_taint_module(struct module *mod, unsigned flag,
166 				    enum lockdep_ok lockdep_ok)
167 {
168 	add_taint(flag, lockdep_ok);
169 	set_bit(flag, &mod->taints);
170 }
171 
172 /*
173  * A thread that wants to hold a reference to a module only while it
174  * is running can call this to safely exit.
175  */
176 void __noreturn __module_put_and_kthread_exit(struct module *mod, long code)
177 {
178 	module_put(mod);
179 	kthread_exit(code);
180 }
181 EXPORT_SYMBOL(__module_put_and_kthread_exit);
182 
183 /* Find a module section: 0 means not found. */
184 static unsigned int find_sec(const struct load_info *info, const char *name)
185 {
186 	unsigned int i;
187 
188 	for (i = 1; i < info->hdr->e_shnum; i++) {
189 		Elf_Shdr *shdr = &info->sechdrs[i];
190 		/* Alloc bit cleared means "ignore it." */
191 		if ((shdr->sh_flags & SHF_ALLOC)
192 		    && strcmp(info->secstrings + shdr->sh_name, name) == 0)
193 			return i;
194 	}
195 	return 0;
196 }
197 
198 /**
199  * find_any_unique_sec() - Find a unique section index by name
200  * @info: Load info for the module to scan
201  * @name: Name of the section we're looking for
202  *
203  * Locates a unique section by name. Ignores SHF_ALLOC.
204  *
205  * Return: Section index if found uniquely, zero if absent, negative count
206  *         of total instances if multiple were found.
207  */
208 static int find_any_unique_sec(const struct load_info *info, const char *name)
209 {
210 	unsigned int idx;
211 	unsigned int count = 0;
212 	int i;
213 
214 	for (i = 1; i < info->hdr->e_shnum; i++) {
215 		if (strcmp(info->secstrings + info->sechdrs[i].sh_name,
216 			   name) == 0) {
217 			count++;
218 			idx = i;
219 		}
220 	}
221 	if (count == 1) {
222 		return idx;
223 	} else if (count == 0) {
224 		return 0;
225 	} else {
226 		return -count;
227 	}
228 }
229 
230 /* Find a module section, or NULL. */
231 static void *section_addr(const struct load_info *info, const char *name)
232 {
233 	/* Section 0 has sh_addr 0. */
234 	return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
235 }
236 
237 /* Find a module section, or NULL.  Fill in number of "objects" in section. */
238 static void *section_objs(const struct load_info *info,
239 			  const char *name,
240 			  size_t object_size,
241 			  unsigned int *num)
242 {
243 	unsigned int sec = find_sec(info, name);
244 
245 	/* Section 0 has sh_addr 0 and sh_size 0. */
246 	*num = info->sechdrs[sec].sh_size / object_size;
247 	return (void *)info->sechdrs[sec].sh_addr;
248 }
249 
250 /* Find a module section: 0 means not found. Ignores SHF_ALLOC flag. */
251 static unsigned int find_any_sec(const struct load_info *info, const char *name)
252 {
253 	unsigned int i;
254 
255 	for (i = 1; i < info->hdr->e_shnum; i++) {
256 		Elf_Shdr *shdr = &info->sechdrs[i];
257 		if (strcmp(info->secstrings + shdr->sh_name, name) == 0)
258 			return i;
259 	}
260 	return 0;
261 }
262 
263 /*
264  * Find a module section, or NULL. Fill in number of "objects" in section.
265  * Ignores SHF_ALLOC flag.
266  */
267 static __maybe_unused void *any_section_objs(const struct load_info *info,
268 					     const char *name,
269 					     size_t object_size,
270 					     unsigned int *num)
271 {
272 	unsigned int sec = find_any_sec(info, name);
273 
274 	/* Section 0 has sh_addr 0 and sh_size 0. */
275 	*num = info->sechdrs[sec].sh_size / object_size;
276 	return (void *)info->sechdrs[sec].sh_addr;
277 }
278 
279 #ifndef CONFIG_MODVERSIONS
280 #define symversion(base, idx) NULL
281 #else
282 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
283 #endif
284 
285 static const char *kernel_symbol_name(const struct kernel_symbol *sym)
286 {
287 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
288 	return offset_to_ptr(&sym->name_offset);
289 #else
290 	return sym->name;
291 #endif
292 }
293 
294 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
295 {
296 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
297 	if (!sym->namespace_offset)
298 		return NULL;
299 	return offset_to_ptr(&sym->namespace_offset);
300 #else
301 	return sym->namespace;
302 #endif
303 }
304 
305 int cmp_name(const void *name, const void *sym)
306 {
307 	return strcmp(name, kernel_symbol_name(sym));
308 }
309 
310 static bool find_exported_symbol_in_section(const struct symsearch *syms,
311 					    struct module *owner,
312 					    struct find_symbol_arg *fsa)
313 {
314 	struct kernel_symbol *sym;
315 
316 	if (!fsa->gplok && syms->license == GPL_ONLY)
317 		return false;
318 
319 	sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
320 			sizeof(struct kernel_symbol), cmp_name);
321 	if (!sym)
322 		return false;
323 
324 	fsa->owner = owner;
325 	fsa->crc = symversion(syms->crcs, sym - syms->start);
326 	fsa->sym = sym;
327 	fsa->license = syms->license;
328 
329 	return true;
330 }
331 
332 /*
333  * Find an exported symbol and return it, along with, (optional) crc and
334  * (optional) module which owns it.  Needs preempt disabled or module_mutex.
335  */
336 bool find_symbol(struct find_symbol_arg *fsa)
337 {
338 	static const struct symsearch arr[] = {
339 		{ __start___ksymtab, __stop___ksymtab, __start___kcrctab,
340 		  NOT_GPL_ONLY },
341 		{ __start___ksymtab_gpl, __stop___ksymtab_gpl,
342 		  __start___kcrctab_gpl,
343 		  GPL_ONLY },
344 	};
345 	struct module *mod;
346 	unsigned int i;
347 
348 	module_assert_mutex_or_preempt();
349 
350 	for (i = 0; i < ARRAY_SIZE(arr); i++)
351 		if (find_exported_symbol_in_section(&arr[i], NULL, fsa))
352 			return true;
353 
354 	list_for_each_entry_rcu(mod, &modules, list,
355 				lockdep_is_held(&module_mutex)) {
356 		struct symsearch arr[] = {
357 			{ mod->syms, mod->syms + mod->num_syms, mod->crcs,
358 			  NOT_GPL_ONLY },
359 			{ mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
360 			  mod->gpl_crcs,
361 			  GPL_ONLY },
362 		};
363 
364 		if (mod->state == MODULE_STATE_UNFORMED)
365 			continue;
366 
367 		for (i = 0; i < ARRAY_SIZE(arr); i++)
368 			if (find_exported_symbol_in_section(&arr[i], mod, fsa))
369 				return true;
370 	}
371 
372 	pr_debug("Failed to find symbol %s\n", fsa->name);
373 	return false;
374 }
375 
376 /*
377  * Search for module by name: must hold module_mutex (or RCU for read-only
378  * access).
379  */
380 struct module *find_module_all(const char *name, size_t len,
381 			       bool even_unformed)
382 {
383 	struct module *mod;
384 
385 	list_for_each_entry_rcu(mod, &modules, list,
386 				lockdep_is_held(&module_mutex)) {
387 		if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
388 			continue;
389 		if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
390 			return mod;
391 	}
392 	return NULL;
393 }
394 
395 struct module *find_module(const char *name)
396 {
397 	return find_module_all(name, strlen(name), false);
398 }
399 
400 #ifdef CONFIG_SMP
401 
402 static inline void __percpu *mod_percpu(struct module *mod)
403 {
404 	return mod->percpu;
405 }
406 
407 static int percpu_modalloc(struct module *mod, struct load_info *info)
408 {
409 	Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
410 	unsigned long align = pcpusec->sh_addralign;
411 
412 	if (!pcpusec->sh_size)
413 		return 0;
414 
415 	if (align > PAGE_SIZE) {
416 		pr_warn("%s: per-cpu alignment %li > %li\n",
417 			mod->name, align, PAGE_SIZE);
418 		align = PAGE_SIZE;
419 	}
420 
421 	mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
422 	if (!mod->percpu) {
423 		pr_warn("%s: Could not allocate %lu bytes percpu data\n",
424 			mod->name, (unsigned long)pcpusec->sh_size);
425 		return -ENOMEM;
426 	}
427 	mod->percpu_size = pcpusec->sh_size;
428 	return 0;
429 }
430 
431 static void percpu_modfree(struct module *mod)
432 {
433 	free_percpu(mod->percpu);
434 }
435 
436 static unsigned int find_pcpusec(struct load_info *info)
437 {
438 	return find_sec(info, ".data..percpu");
439 }
440 
441 static void percpu_modcopy(struct module *mod,
442 			   const void *from, unsigned long size)
443 {
444 	int cpu;
445 
446 	for_each_possible_cpu(cpu)
447 		memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
448 }
449 
450 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
451 {
452 	struct module *mod;
453 	unsigned int cpu;
454 
455 	preempt_disable();
456 
457 	list_for_each_entry_rcu(mod, &modules, list) {
458 		if (mod->state == MODULE_STATE_UNFORMED)
459 			continue;
460 		if (!mod->percpu_size)
461 			continue;
462 		for_each_possible_cpu(cpu) {
463 			void *start = per_cpu_ptr(mod->percpu, cpu);
464 			void *va = (void *)addr;
465 
466 			if (va >= start && va < start + mod->percpu_size) {
467 				if (can_addr) {
468 					*can_addr = (unsigned long) (va - start);
469 					*can_addr += (unsigned long)
470 						per_cpu_ptr(mod->percpu,
471 							    get_boot_cpu_id());
472 				}
473 				preempt_enable();
474 				return true;
475 			}
476 		}
477 	}
478 
479 	preempt_enable();
480 	return false;
481 }
482 
483 /**
484  * is_module_percpu_address() - test whether address is from module static percpu
485  * @addr: address to test
486  *
487  * Test whether @addr belongs to module static percpu area.
488  *
489  * Return: %true if @addr is from module static percpu area
490  */
491 bool is_module_percpu_address(unsigned long addr)
492 {
493 	return __is_module_percpu_address(addr, NULL);
494 }
495 
496 #else /* ... !CONFIG_SMP */
497 
498 static inline void __percpu *mod_percpu(struct module *mod)
499 {
500 	return NULL;
501 }
502 static int percpu_modalloc(struct module *mod, struct load_info *info)
503 {
504 	/* UP modules shouldn't have this section: ENOMEM isn't quite right */
505 	if (info->sechdrs[info->index.pcpu].sh_size != 0)
506 		return -ENOMEM;
507 	return 0;
508 }
509 static inline void percpu_modfree(struct module *mod)
510 {
511 }
512 static unsigned int find_pcpusec(struct load_info *info)
513 {
514 	return 0;
515 }
516 static inline void percpu_modcopy(struct module *mod,
517 				  const void *from, unsigned long size)
518 {
519 	/* pcpusec should be 0, and size of that section should be 0. */
520 	BUG_ON(size != 0);
521 }
522 bool is_module_percpu_address(unsigned long addr)
523 {
524 	return false;
525 }
526 
527 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
528 {
529 	return false;
530 }
531 
532 #endif /* CONFIG_SMP */
533 
534 #define MODINFO_ATTR(field)	\
535 static void setup_modinfo_##field(struct module *mod, const char *s)  \
536 {                                                                     \
537 	mod->field = kstrdup(s, GFP_KERNEL);                          \
538 }                                                                     \
539 static ssize_t show_modinfo_##field(const struct module_attribute *mattr, \
540 			struct module_kobject *mk, char *buffer)      \
541 {                                                                     \
542 	return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field);  \
543 }                                                                     \
544 static int modinfo_##field##_exists(struct module *mod)               \
545 {                                                                     \
546 	return mod->field != NULL;                                    \
547 }                                                                     \
548 static void free_modinfo_##field(struct module *mod)                  \
549 {                                                                     \
550 	kfree(mod->field);                                            \
551 	mod->field = NULL;                                            \
552 }                                                                     \
553 static const struct module_attribute modinfo_##field = {              \
554 	.attr = { .name = __stringify(field), .mode = 0444 },         \
555 	.show = show_modinfo_##field,                                 \
556 	.setup = setup_modinfo_##field,                               \
557 	.test = modinfo_##field##_exists,                             \
558 	.free = free_modinfo_##field,                                 \
559 };
560 
561 MODINFO_ATTR(version);
562 MODINFO_ATTR(srcversion);
563 
564 static struct {
565 	char name[MODULE_NAME_LEN + 1];
566 	char taints[MODULE_FLAGS_BUF_SIZE];
567 } last_unloaded_module;
568 
569 #ifdef CONFIG_MODULE_UNLOAD
570 
571 EXPORT_TRACEPOINT_SYMBOL(module_get);
572 
573 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
574 #define MODULE_REF_BASE	1
575 
576 /* Init the unload section of the module. */
577 static int module_unload_init(struct module *mod)
578 {
579 	/*
580 	 * Initialize reference counter to MODULE_REF_BASE.
581 	 * refcnt == 0 means module is going.
582 	 */
583 	atomic_set(&mod->refcnt, MODULE_REF_BASE);
584 
585 	INIT_LIST_HEAD(&mod->source_list);
586 	INIT_LIST_HEAD(&mod->target_list);
587 
588 	/* Hold reference count during initialization. */
589 	atomic_inc(&mod->refcnt);
590 
591 	return 0;
592 }
593 
594 /* Does a already use b? */
595 static int already_uses(struct module *a, struct module *b)
596 {
597 	struct module_use *use;
598 
599 	list_for_each_entry(use, &b->source_list, source_list) {
600 		if (use->source == a)
601 			return 1;
602 	}
603 	pr_debug("%s does not use %s!\n", a->name, b->name);
604 	return 0;
605 }
606 
607 /*
608  * Module a uses b
609  *  - we add 'a' as a "source", 'b' as a "target" of module use
610  *  - the module_use is added to the list of 'b' sources (so
611  *    'b' can walk the list to see who sourced them), and of 'a'
612  *    targets (so 'a' can see what modules it targets).
613  */
614 static int add_module_usage(struct module *a, struct module *b)
615 {
616 	struct module_use *use;
617 
618 	pr_debug("Allocating new usage for %s.\n", a->name);
619 	use = kmalloc(sizeof(*use), GFP_ATOMIC);
620 	if (!use)
621 		return -ENOMEM;
622 
623 	use->source = a;
624 	use->target = b;
625 	list_add(&use->source_list, &b->source_list);
626 	list_add(&use->target_list, &a->target_list);
627 	return 0;
628 }
629 
630 /* Module a uses b: caller needs module_mutex() */
631 static int ref_module(struct module *a, struct module *b)
632 {
633 	int err;
634 
635 	if (b == NULL || already_uses(a, b))
636 		return 0;
637 
638 	/* If module isn't available, we fail. */
639 	err = strong_try_module_get(b);
640 	if (err)
641 		return err;
642 
643 	err = add_module_usage(a, b);
644 	if (err) {
645 		module_put(b);
646 		return err;
647 	}
648 	return 0;
649 }
650 
651 /* Clear the unload stuff of the module. */
652 static void module_unload_free(struct module *mod)
653 {
654 	struct module_use *use, *tmp;
655 
656 	mutex_lock(&module_mutex);
657 	list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
658 		struct module *i = use->target;
659 		pr_debug("%s unusing %s\n", mod->name, i->name);
660 		module_put(i);
661 		list_del(&use->source_list);
662 		list_del(&use->target_list);
663 		kfree(use);
664 	}
665 	mutex_unlock(&module_mutex);
666 }
667 
668 #ifdef CONFIG_MODULE_FORCE_UNLOAD
669 static inline int try_force_unload(unsigned int flags)
670 {
671 	int ret = (flags & O_TRUNC);
672 	if (ret)
673 		add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
674 	return ret;
675 }
676 #else
677 static inline int try_force_unload(unsigned int flags)
678 {
679 	return 0;
680 }
681 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
682 
683 /* Try to release refcount of module, 0 means success. */
684 static int try_release_module_ref(struct module *mod)
685 {
686 	int ret;
687 
688 	/* Try to decrement refcnt which we set at loading */
689 	ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
690 	BUG_ON(ret < 0);
691 	if (ret)
692 		/* Someone can put this right now, recover with checking */
693 		ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
694 
695 	return ret;
696 }
697 
698 static int try_stop_module(struct module *mod, int flags, int *forced)
699 {
700 	/* If it's not unused, quit unless we're forcing. */
701 	if (try_release_module_ref(mod) != 0) {
702 		*forced = try_force_unload(flags);
703 		if (!(*forced))
704 			return -EWOULDBLOCK;
705 	}
706 
707 	/* Mark it as dying. */
708 	mod->state = MODULE_STATE_GOING;
709 
710 	return 0;
711 }
712 
713 /**
714  * module_refcount() - return the refcount or -1 if unloading
715  * @mod:	the module we're checking
716  *
717  * Return:
718  *	-1 if the module is in the process of unloading
719  *	otherwise the number of references in the kernel to the module
720  */
721 int module_refcount(struct module *mod)
722 {
723 	return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
724 }
725 EXPORT_SYMBOL(module_refcount);
726 
727 /* This exists whether we can unload or not */
728 static void free_module(struct module *mod);
729 
730 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
731 		unsigned int, flags)
732 {
733 	struct module *mod;
734 	char name[MODULE_NAME_LEN];
735 	char buf[MODULE_FLAGS_BUF_SIZE];
736 	int ret, forced = 0;
737 
738 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
739 		return -EPERM;
740 
741 	if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
742 		return -EFAULT;
743 	name[MODULE_NAME_LEN-1] = '\0';
744 
745 	audit_log_kern_module(name);
746 
747 	if (mutex_lock_interruptible(&module_mutex) != 0)
748 		return -EINTR;
749 
750 	mod = find_module(name);
751 	if (!mod) {
752 		ret = -ENOENT;
753 		goto out;
754 	}
755 
756 	if (!list_empty(&mod->source_list)) {
757 		/* Other modules depend on us: get rid of them first. */
758 		ret = -EWOULDBLOCK;
759 		goto out;
760 	}
761 
762 	/* Doing init or already dying? */
763 	if (mod->state != MODULE_STATE_LIVE) {
764 		/* FIXME: if (force), slam module count damn the torpedoes */
765 		pr_debug("%s already dying\n", mod->name);
766 		ret = -EBUSY;
767 		goto out;
768 	}
769 
770 	/* If it has an init func, it must have an exit func to unload */
771 	if (mod->init && !mod->exit) {
772 		forced = try_force_unload(flags);
773 		if (!forced) {
774 			/* This module can't be removed */
775 			ret = -EBUSY;
776 			goto out;
777 		}
778 	}
779 
780 	ret = try_stop_module(mod, flags, &forced);
781 	if (ret != 0)
782 		goto out;
783 
784 	mutex_unlock(&module_mutex);
785 	/* Final destruction now no one is using it. */
786 	if (mod->exit != NULL)
787 		mod->exit();
788 	blocking_notifier_call_chain(&module_notify_list,
789 				     MODULE_STATE_GOING, mod);
790 	klp_module_going(mod);
791 	ftrace_release_mod(mod);
792 
793 	async_synchronize_full();
794 
795 	/* Store the name and taints of the last unloaded module for diagnostic purposes */
796 	strscpy(last_unloaded_module.name, mod->name, sizeof(last_unloaded_module.name));
797 	strscpy(last_unloaded_module.taints, module_flags(mod, buf, false), sizeof(last_unloaded_module.taints));
798 
799 	free_module(mod);
800 	/* someone could wait for the module in add_unformed_module() */
801 	wake_up_all(&module_wq);
802 	return 0;
803 out:
804 	mutex_unlock(&module_mutex);
805 	return ret;
806 }
807 
808 void __symbol_put(const char *symbol)
809 {
810 	struct find_symbol_arg fsa = {
811 		.name	= symbol,
812 		.gplok	= true,
813 	};
814 
815 	preempt_disable();
816 	BUG_ON(!find_symbol(&fsa));
817 	module_put(fsa.owner);
818 	preempt_enable();
819 }
820 EXPORT_SYMBOL(__symbol_put);
821 
822 /* Note this assumes addr is a function, which it currently always is. */
823 void symbol_put_addr(void *addr)
824 {
825 	struct module *modaddr;
826 	unsigned long a = (unsigned long)dereference_function_descriptor(addr);
827 
828 	if (core_kernel_text(a))
829 		return;
830 
831 	/*
832 	 * Even though we hold a reference on the module; we still need to
833 	 * disable preemption in order to safely traverse the data structure.
834 	 */
835 	preempt_disable();
836 	modaddr = __module_text_address(a);
837 	BUG_ON(!modaddr);
838 	module_put(modaddr);
839 	preempt_enable();
840 }
841 EXPORT_SYMBOL_GPL(symbol_put_addr);
842 
843 static ssize_t show_refcnt(const struct module_attribute *mattr,
844 			   struct module_kobject *mk, char *buffer)
845 {
846 	return sprintf(buffer, "%i\n", module_refcount(mk->mod));
847 }
848 
849 static const struct module_attribute modinfo_refcnt =
850 	__ATTR(refcnt, 0444, show_refcnt, NULL);
851 
852 void __module_get(struct module *module)
853 {
854 	if (module) {
855 		atomic_inc(&module->refcnt);
856 		trace_module_get(module, _RET_IP_);
857 	}
858 }
859 EXPORT_SYMBOL(__module_get);
860 
861 bool try_module_get(struct module *module)
862 {
863 	bool ret = true;
864 
865 	if (module) {
866 		/* Note: here, we can fail to get a reference */
867 		if (likely(module_is_live(module) &&
868 			   atomic_inc_not_zero(&module->refcnt) != 0))
869 			trace_module_get(module, _RET_IP_);
870 		else
871 			ret = false;
872 	}
873 	return ret;
874 }
875 EXPORT_SYMBOL(try_module_get);
876 
877 void module_put(struct module *module)
878 {
879 	int ret;
880 
881 	if (module) {
882 		ret = atomic_dec_if_positive(&module->refcnt);
883 		WARN_ON(ret < 0);	/* Failed to put refcount */
884 		trace_module_put(module, _RET_IP_);
885 	}
886 }
887 EXPORT_SYMBOL(module_put);
888 
889 #else /* !CONFIG_MODULE_UNLOAD */
890 static inline void module_unload_free(struct module *mod)
891 {
892 }
893 
894 static int ref_module(struct module *a, struct module *b)
895 {
896 	return strong_try_module_get(b);
897 }
898 
899 static inline int module_unload_init(struct module *mod)
900 {
901 	return 0;
902 }
903 #endif /* CONFIG_MODULE_UNLOAD */
904 
905 size_t module_flags_taint(unsigned long taints, char *buf)
906 {
907 	size_t l = 0;
908 	int i;
909 
910 	for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
911 		if (taint_flags[i].module && test_bit(i, &taints))
912 			buf[l++] = taint_flags[i].c_true;
913 	}
914 
915 	return l;
916 }
917 
918 static ssize_t show_initstate(const struct module_attribute *mattr,
919 			      struct module_kobject *mk, char *buffer)
920 {
921 	const char *state = "unknown";
922 
923 	switch (mk->mod->state) {
924 	case MODULE_STATE_LIVE:
925 		state = "live";
926 		break;
927 	case MODULE_STATE_COMING:
928 		state = "coming";
929 		break;
930 	case MODULE_STATE_GOING:
931 		state = "going";
932 		break;
933 	default:
934 		BUG();
935 	}
936 	return sprintf(buffer, "%s\n", state);
937 }
938 
939 static const struct module_attribute modinfo_initstate =
940 	__ATTR(initstate, 0444, show_initstate, NULL);
941 
942 static ssize_t store_uevent(const struct module_attribute *mattr,
943 			    struct module_kobject *mk,
944 			    const char *buffer, size_t count)
945 {
946 	int rc;
947 
948 	rc = kobject_synth_uevent(&mk->kobj, buffer, count);
949 	return rc ? rc : count;
950 }
951 
952 const struct module_attribute module_uevent =
953 	__ATTR(uevent, 0200, NULL, store_uevent);
954 
955 static ssize_t show_coresize(const struct module_attribute *mattr,
956 			     struct module_kobject *mk, char *buffer)
957 {
958 	unsigned int size = mk->mod->mem[MOD_TEXT].size;
959 
960 	if (!IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)) {
961 		for_class_mod_mem_type(type, core_data)
962 			size += mk->mod->mem[type].size;
963 	}
964 	return sprintf(buffer, "%u\n", size);
965 }
966 
967 static const struct module_attribute modinfo_coresize =
968 	__ATTR(coresize, 0444, show_coresize, NULL);
969 
970 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
971 static ssize_t show_datasize(const struct module_attribute *mattr,
972 			     struct module_kobject *mk, char *buffer)
973 {
974 	unsigned int size = 0;
975 
976 	for_class_mod_mem_type(type, core_data)
977 		size += mk->mod->mem[type].size;
978 	return sprintf(buffer, "%u\n", size);
979 }
980 
981 static const struct module_attribute modinfo_datasize =
982 	__ATTR(datasize, 0444, show_datasize, NULL);
983 #endif
984 
985 static ssize_t show_initsize(const struct module_attribute *mattr,
986 			     struct module_kobject *mk, char *buffer)
987 {
988 	unsigned int size = 0;
989 
990 	for_class_mod_mem_type(type, init)
991 		size += mk->mod->mem[type].size;
992 	return sprintf(buffer, "%u\n", size);
993 }
994 
995 static const struct module_attribute modinfo_initsize =
996 	__ATTR(initsize, 0444, show_initsize, NULL);
997 
998 static ssize_t show_taint(const struct module_attribute *mattr,
999 			  struct module_kobject *mk, char *buffer)
1000 {
1001 	size_t l;
1002 
1003 	l = module_flags_taint(mk->mod->taints, buffer);
1004 	buffer[l++] = '\n';
1005 	return l;
1006 }
1007 
1008 static const struct module_attribute modinfo_taint =
1009 	__ATTR(taint, 0444, show_taint, NULL);
1010 
1011 const struct module_attribute *const modinfo_attrs[] = {
1012 	&module_uevent,
1013 	&modinfo_version,
1014 	&modinfo_srcversion,
1015 	&modinfo_initstate,
1016 	&modinfo_coresize,
1017 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
1018 	&modinfo_datasize,
1019 #endif
1020 	&modinfo_initsize,
1021 	&modinfo_taint,
1022 #ifdef CONFIG_MODULE_UNLOAD
1023 	&modinfo_refcnt,
1024 #endif
1025 	NULL,
1026 };
1027 
1028 const size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
1029 
1030 static const char vermagic[] = VERMAGIC_STRING;
1031 
1032 int try_to_force_load(struct module *mod, const char *reason)
1033 {
1034 #ifdef CONFIG_MODULE_FORCE_LOAD
1035 	if (!test_taint(TAINT_FORCED_MODULE))
1036 		pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1037 	add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1038 	return 0;
1039 #else
1040 	return -ENOEXEC;
1041 #endif
1042 }
1043 
1044 /* Parse tag=value strings from .modinfo section */
1045 char *module_next_tag_pair(char *string, unsigned long *secsize)
1046 {
1047 	/* Skip non-zero chars */
1048 	while (string[0]) {
1049 		string++;
1050 		if ((*secsize)-- <= 1)
1051 			return NULL;
1052 	}
1053 
1054 	/* Skip any zero padding. */
1055 	while (!string[0]) {
1056 		string++;
1057 		if ((*secsize)-- <= 1)
1058 			return NULL;
1059 	}
1060 	return string;
1061 }
1062 
1063 static char *get_next_modinfo(const struct load_info *info, const char *tag,
1064 			      char *prev)
1065 {
1066 	char *p;
1067 	unsigned int taglen = strlen(tag);
1068 	Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1069 	unsigned long size = infosec->sh_size;
1070 
1071 	/*
1072 	 * get_modinfo() calls made before rewrite_section_headers()
1073 	 * must use sh_offset, as sh_addr isn't set!
1074 	 */
1075 	char *modinfo = (char *)info->hdr + infosec->sh_offset;
1076 
1077 	if (prev) {
1078 		size -= prev - modinfo;
1079 		modinfo = module_next_tag_pair(prev, &size);
1080 	}
1081 
1082 	for (p = modinfo; p; p = module_next_tag_pair(p, &size)) {
1083 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1084 			return p + taglen + 1;
1085 	}
1086 	return NULL;
1087 }
1088 
1089 static char *get_modinfo(const struct load_info *info, const char *tag)
1090 {
1091 	return get_next_modinfo(info, tag, NULL);
1092 }
1093 
1094 static int verify_namespace_is_imported(const struct load_info *info,
1095 					const struct kernel_symbol *sym,
1096 					struct module *mod)
1097 {
1098 	const char *namespace;
1099 	char *imported_namespace;
1100 
1101 	namespace = kernel_symbol_namespace(sym);
1102 	if (namespace && namespace[0]) {
1103 		for_each_modinfo_entry(imported_namespace, info, "import_ns") {
1104 			if (strcmp(namespace, imported_namespace) == 0)
1105 				return 0;
1106 		}
1107 #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1108 		pr_warn(
1109 #else
1110 		pr_err(
1111 #endif
1112 			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
1113 			mod->name, kernel_symbol_name(sym), namespace);
1114 #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1115 		return -EINVAL;
1116 #endif
1117 	}
1118 	return 0;
1119 }
1120 
1121 static bool inherit_taint(struct module *mod, struct module *owner, const char *name)
1122 {
1123 	if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
1124 		return true;
1125 
1126 	if (mod->using_gplonly_symbols) {
1127 		pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n",
1128 			mod->name, name, owner->name);
1129 		return false;
1130 	}
1131 
1132 	if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1133 		pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n",
1134 			mod->name, name, owner->name);
1135 		set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
1136 	}
1137 	return true;
1138 }
1139 
1140 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
1141 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1142 						  const struct load_info *info,
1143 						  const char *name,
1144 						  char ownername[])
1145 {
1146 	struct find_symbol_arg fsa = {
1147 		.name	= name,
1148 		.gplok	= !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)),
1149 		.warn	= true,
1150 	};
1151 	int err;
1152 
1153 	/*
1154 	 * The module_mutex should not be a heavily contended lock;
1155 	 * if we get the occasional sleep here, we'll go an extra iteration
1156 	 * in the wait_event_interruptible(), which is harmless.
1157 	 */
1158 	sched_annotate_sleep();
1159 	mutex_lock(&module_mutex);
1160 	if (!find_symbol(&fsa))
1161 		goto unlock;
1162 
1163 	if (fsa.license == GPL_ONLY)
1164 		mod->using_gplonly_symbols = true;
1165 
1166 	if (!inherit_taint(mod, fsa.owner, name)) {
1167 		fsa.sym = NULL;
1168 		goto getname;
1169 	}
1170 
1171 	if (!check_version(info, name, mod, fsa.crc)) {
1172 		fsa.sym = ERR_PTR(-EINVAL);
1173 		goto getname;
1174 	}
1175 
1176 	err = verify_namespace_is_imported(info, fsa.sym, mod);
1177 	if (err) {
1178 		fsa.sym = ERR_PTR(err);
1179 		goto getname;
1180 	}
1181 
1182 	err = ref_module(mod, fsa.owner);
1183 	if (err) {
1184 		fsa.sym = ERR_PTR(err);
1185 		goto getname;
1186 	}
1187 
1188 getname:
1189 	/* We must make copy under the lock if we failed to get ref. */
1190 	strncpy(ownername, module_name(fsa.owner), MODULE_NAME_LEN);
1191 unlock:
1192 	mutex_unlock(&module_mutex);
1193 	return fsa.sym;
1194 }
1195 
1196 static const struct kernel_symbol *
1197 resolve_symbol_wait(struct module *mod,
1198 		    const struct load_info *info,
1199 		    const char *name)
1200 {
1201 	const struct kernel_symbol *ksym;
1202 	char owner[MODULE_NAME_LEN];
1203 
1204 	if (wait_event_interruptible_timeout(module_wq,
1205 			!IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1206 			|| PTR_ERR(ksym) != -EBUSY,
1207 					     30 * HZ) <= 0) {
1208 		pr_warn("%s: gave up waiting for init of module %s.\n",
1209 			mod->name, owner);
1210 	}
1211 	return ksym;
1212 }
1213 
1214 void __weak module_arch_cleanup(struct module *mod)
1215 {
1216 }
1217 
1218 void __weak module_arch_freeing_init(struct module *mod)
1219 {
1220 }
1221 
1222 void *__module_writable_address(struct module *mod, void *loc)
1223 {
1224 	for_class_mod_mem_type(type, text) {
1225 		struct module_memory *mem = &mod->mem[type];
1226 
1227 		if (loc >= mem->base && loc < mem->base + mem->size)
1228 			return loc + (mem->rw_copy - mem->base);
1229 	}
1230 
1231 	return loc;
1232 }
1233 
1234 static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
1235 {
1236 	unsigned int size = PAGE_ALIGN(mod->mem[type].size);
1237 	enum execmem_type execmem_type;
1238 	void *ptr;
1239 
1240 	mod->mem[type].size = size;
1241 
1242 	if (mod_mem_type_is_data(type))
1243 		execmem_type = EXECMEM_MODULE_DATA;
1244 	else
1245 		execmem_type = EXECMEM_MODULE_TEXT;
1246 
1247 	ptr = execmem_alloc(execmem_type, size);
1248 	if (!ptr)
1249 		return -ENOMEM;
1250 
1251 	mod->mem[type].base = ptr;
1252 
1253 	if (execmem_is_rox(execmem_type)) {
1254 		ptr = vzalloc(size);
1255 
1256 		if (!ptr) {
1257 			execmem_free(mod->mem[type].base);
1258 			return -ENOMEM;
1259 		}
1260 
1261 		mod->mem[type].rw_copy = ptr;
1262 		mod->mem[type].is_rox = true;
1263 	} else {
1264 		mod->mem[type].rw_copy = mod->mem[type].base;
1265 		memset(mod->mem[type].base, 0, size);
1266 	}
1267 
1268 	/*
1269 	 * The pointer to these blocks of memory are stored on the module
1270 	 * structure and we keep that around so long as the module is
1271 	 * around. We only free that memory when we unload the module.
1272 	 * Just mark them as not being a leak then. The .init* ELF
1273 	 * sections *do* get freed after boot so we *could* treat them
1274 	 * slightly differently with kmemleak_ignore() and only grey
1275 	 * them out as they work as typical memory allocations which
1276 	 * *do* eventually get freed, but let's just keep things simple
1277 	 * and avoid *any* false positives.
1278 	 */
1279 	kmemleak_not_leak(ptr);
1280 
1281 	return 0;
1282 }
1283 
1284 static void module_memory_free(struct module *mod, enum mod_mem_type type)
1285 {
1286 	struct module_memory *mem = &mod->mem[type];
1287 
1288 	if (mem->is_rox)
1289 		vfree(mem->rw_copy);
1290 
1291 	execmem_free(mem->base);
1292 }
1293 
1294 static void free_mod_mem(struct module *mod)
1295 {
1296 	for_each_mod_mem_type(type) {
1297 		struct module_memory *mod_mem = &mod->mem[type];
1298 
1299 		if (type == MOD_DATA)
1300 			continue;
1301 
1302 		/* Free lock-classes; relies on the preceding sync_rcu(). */
1303 		lockdep_free_key_range(mod_mem->base, mod_mem->size);
1304 		if (mod_mem->size)
1305 			module_memory_free(mod, type);
1306 	}
1307 
1308 	/* MOD_DATA hosts mod, so free it at last */
1309 	lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size);
1310 	module_memory_free(mod, MOD_DATA);
1311 }
1312 
1313 /* Free a module, remove from lists, etc. */
1314 static void free_module(struct module *mod)
1315 {
1316 	trace_module_free(mod);
1317 
1318 	codetag_unload_module(mod);
1319 
1320 	mod_sysfs_teardown(mod);
1321 
1322 	/*
1323 	 * We leave it in list to prevent duplicate loads, but make sure
1324 	 * that noone uses it while it's being deconstructed.
1325 	 */
1326 	mutex_lock(&module_mutex);
1327 	mod->state = MODULE_STATE_UNFORMED;
1328 	mutex_unlock(&module_mutex);
1329 
1330 	/* Arch-specific cleanup. */
1331 	module_arch_cleanup(mod);
1332 
1333 	/* Module unload stuff */
1334 	module_unload_free(mod);
1335 
1336 	/* Free any allocated parameters. */
1337 	destroy_params(mod->kp, mod->num_kp);
1338 
1339 	if (is_livepatch_module(mod))
1340 		free_module_elf(mod);
1341 
1342 	/* Now we can delete it from the lists */
1343 	mutex_lock(&module_mutex);
1344 	/* Unlink carefully: kallsyms could be walking list. */
1345 	list_del_rcu(&mod->list);
1346 	mod_tree_remove(mod);
1347 	/* Remove this module from bug list, this uses list_del_rcu */
1348 	module_bug_cleanup(mod);
1349 	/* Wait for RCU synchronizing before releasing mod->list and buglist. */
1350 	synchronize_rcu();
1351 	if (try_add_tainted_module(mod))
1352 		pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n",
1353 		       mod->name);
1354 	mutex_unlock(&module_mutex);
1355 
1356 	/* This may be empty, but that's OK */
1357 	module_arch_freeing_init(mod);
1358 	kfree(mod->args);
1359 	percpu_modfree(mod);
1360 
1361 	free_mod_mem(mod);
1362 }
1363 
1364 void *__symbol_get(const char *symbol)
1365 {
1366 	struct find_symbol_arg fsa = {
1367 		.name	= symbol,
1368 		.gplok	= true,
1369 		.warn	= true,
1370 	};
1371 
1372 	preempt_disable();
1373 	if (!find_symbol(&fsa))
1374 		goto fail;
1375 	if (fsa.license != GPL_ONLY) {
1376 		pr_warn("failing symbol_get of non-GPLONLY symbol %s.\n",
1377 			symbol);
1378 		goto fail;
1379 	}
1380 	if (strong_try_module_get(fsa.owner))
1381 		goto fail;
1382 	preempt_enable();
1383 	return (void *)kernel_symbol_value(fsa.sym);
1384 fail:
1385 	preempt_enable();
1386 	return NULL;
1387 }
1388 EXPORT_SYMBOL_GPL(__symbol_get);
1389 
1390 /*
1391  * Ensure that an exported symbol [global namespace] does not already exist
1392  * in the kernel or in some other module's exported symbol table.
1393  *
1394  * You must hold the module_mutex.
1395  */
1396 static int verify_exported_symbols(struct module *mod)
1397 {
1398 	unsigned int i;
1399 	const struct kernel_symbol *s;
1400 	struct {
1401 		const struct kernel_symbol *sym;
1402 		unsigned int num;
1403 	} arr[] = {
1404 		{ mod->syms, mod->num_syms },
1405 		{ mod->gpl_syms, mod->num_gpl_syms },
1406 	};
1407 
1408 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
1409 		for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1410 			struct find_symbol_arg fsa = {
1411 				.name	= kernel_symbol_name(s),
1412 				.gplok	= true,
1413 			};
1414 			if (find_symbol(&fsa)) {
1415 				pr_err("%s: exports duplicate symbol %s"
1416 				       " (owned by %s)\n",
1417 				       mod->name, kernel_symbol_name(s),
1418 				       module_name(fsa.owner));
1419 				return -ENOEXEC;
1420 			}
1421 		}
1422 	}
1423 	return 0;
1424 }
1425 
1426 static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
1427 {
1428 	/*
1429 	 * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
1430 	 * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
1431 	 * i386 has a similar problem but may not deserve a fix.
1432 	 *
1433 	 * If we ever have to ignore many symbols, consider refactoring the code to
1434 	 * only warn if referenced by a relocation.
1435 	 */
1436 	if (emachine == EM_386 || emachine == EM_X86_64)
1437 		return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
1438 	return false;
1439 }
1440 
1441 /* Change all symbols so that st_value encodes the pointer directly. */
1442 static int simplify_symbols(struct module *mod, const struct load_info *info)
1443 {
1444 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1445 	Elf_Sym *sym = (void *)symsec->sh_addr;
1446 	unsigned long secbase;
1447 	unsigned int i;
1448 	int ret = 0;
1449 	const struct kernel_symbol *ksym;
1450 
1451 	for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1452 		const char *name = info->strtab + sym[i].st_name;
1453 
1454 		switch (sym[i].st_shndx) {
1455 		case SHN_COMMON:
1456 			/* Ignore common symbols */
1457 			if (!strncmp(name, "__gnu_lto", 9))
1458 				break;
1459 
1460 			/*
1461 			 * We compiled with -fno-common.  These are not
1462 			 * supposed to happen.
1463 			 */
1464 			pr_debug("Common symbol: %s\n", name);
1465 			pr_warn("%s: please compile with -fno-common\n",
1466 			       mod->name);
1467 			ret = -ENOEXEC;
1468 			break;
1469 
1470 		case SHN_ABS:
1471 			/* Don't need to do anything */
1472 			pr_debug("Absolute symbol: 0x%08lx %s\n",
1473 				 (long)sym[i].st_value, name);
1474 			break;
1475 
1476 		case SHN_LIVEPATCH:
1477 			/* Livepatch symbols are resolved by livepatch */
1478 			break;
1479 
1480 		case SHN_UNDEF:
1481 			ksym = resolve_symbol_wait(mod, info, name);
1482 			/* Ok if resolved.  */
1483 			if (ksym && !IS_ERR(ksym)) {
1484 				sym[i].st_value = kernel_symbol_value(ksym);
1485 				break;
1486 			}
1487 
1488 			/* Ok if weak or ignored.  */
1489 			if (!ksym &&
1490 			    (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
1491 			     ignore_undef_symbol(info->hdr->e_machine, name)))
1492 				break;
1493 
1494 			ret = PTR_ERR(ksym) ?: -ENOENT;
1495 			pr_warn("%s: Unknown symbol %s (err %d)\n",
1496 				mod->name, name, ret);
1497 			break;
1498 
1499 		default:
1500 			/* Divert to percpu allocation if a percpu var. */
1501 			if (sym[i].st_shndx == info->index.pcpu)
1502 				secbase = (unsigned long)mod_percpu(mod);
1503 			else
1504 				secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1505 			sym[i].st_value += secbase;
1506 			break;
1507 		}
1508 	}
1509 
1510 	return ret;
1511 }
1512 
1513 static int apply_relocations(struct module *mod, const struct load_info *info)
1514 {
1515 	unsigned int i;
1516 	int err = 0;
1517 
1518 	/* Now do relocations. */
1519 	for (i = 1; i < info->hdr->e_shnum; i++) {
1520 		unsigned int infosec = info->sechdrs[i].sh_info;
1521 
1522 		/* Not a valid relocation section? */
1523 		if (infosec >= info->hdr->e_shnum)
1524 			continue;
1525 
1526 		/* Don't bother with non-allocated sections */
1527 		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
1528 			continue;
1529 
1530 		if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
1531 			err = klp_apply_section_relocs(mod, info->sechdrs,
1532 						       info->secstrings,
1533 						       info->strtab,
1534 						       info->index.sym, i,
1535 						       NULL);
1536 		else if (info->sechdrs[i].sh_type == SHT_REL)
1537 			err = apply_relocate(info->sechdrs, info->strtab,
1538 					     info->index.sym, i, mod);
1539 		else if (info->sechdrs[i].sh_type == SHT_RELA)
1540 			err = apply_relocate_add(info->sechdrs, info->strtab,
1541 						 info->index.sym, i, mod);
1542 		if (err < 0)
1543 			break;
1544 	}
1545 	return err;
1546 }
1547 
1548 /* Additional bytes needed by arch in front of individual sections */
1549 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1550 					     unsigned int section)
1551 {
1552 	/* default implementation just returns zero */
1553 	return 0;
1554 }
1555 
1556 long module_get_offset_and_type(struct module *mod, enum mod_mem_type type,
1557 				Elf_Shdr *sechdr, unsigned int section)
1558 {
1559 	long offset;
1560 	long mask = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK) << SH_ENTSIZE_TYPE_SHIFT;
1561 
1562 	mod->mem[type].size += arch_mod_section_prepend(mod, section);
1563 	offset = ALIGN(mod->mem[type].size, sechdr->sh_addralign ?: 1);
1564 	mod->mem[type].size = offset + sechdr->sh_size;
1565 
1566 	WARN_ON_ONCE(offset & mask);
1567 	return offset | mask;
1568 }
1569 
1570 bool module_init_layout_section(const char *sname)
1571 {
1572 #ifndef CONFIG_MODULE_UNLOAD
1573 	if (module_exit_section(sname))
1574 		return true;
1575 #endif
1576 	return module_init_section(sname);
1577 }
1578 
1579 static void __layout_sections(struct module *mod, struct load_info *info, bool is_init)
1580 {
1581 	unsigned int m, i;
1582 
1583 	static const unsigned long masks[][2] = {
1584 		/*
1585 		 * NOTE: all executable code must be the first section
1586 		 * in this array; otherwise modify the text_size
1587 		 * finder in the two loops below
1588 		 */
1589 		{ SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1590 		{ SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1591 		{ SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
1592 		{ SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1593 		{ ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1594 	};
1595 	static const int core_m_to_mem_type[] = {
1596 		MOD_TEXT,
1597 		MOD_RODATA,
1598 		MOD_RO_AFTER_INIT,
1599 		MOD_DATA,
1600 		MOD_DATA,
1601 	};
1602 	static const int init_m_to_mem_type[] = {
1603 		MOD_INIT_TEXT,
1604 		MOD_INIT_RODATA,
1605 		MOD_INVALID,
1606 		MOD_INIT_DATA,
1607 		MOD_INIT_DATA,
1608 	};
1609 
1610 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1611 		enum mod_mem_type type = is_init ? init_m_to_mem_type[m] : core_m_to_mem_type[m];
1612 
1613 		for (i = 0; i < info->hdr->e_shnum; ++i) {
1614 			Elf_Shdr *s = &info->sechdrs[i];
1615 			const char *sname = info->secstrings + s->sh_name;
1616 
1617 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
1618 			    || (s->sh_flags & masks[m][1])
1619 			    || s->sh_entsize != ~0UL
1620 			    || is_init != module_init_layout_section(sname))
1621 				continue;
1622 
1623 			if (WARN_ON_ONCE(type == MOD_INVALID))
1624 				continue;
1625 
1626 			/*
1627 			 * Do not allocate codetag memory as we load it into
1628 			 * preallocated contiguous memory.
1629 			 */
1630 			if (codetag_needs_module_section(mod, sname, s->sh_size)) {
1631 				/*
1632 				 * s->sh_entsize won't be used but populate the
1633 				 * type field to avoid confusion.
1634 				 */
1635 				s->sh_entsize = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK)
1636 						<< SH_ENTSIZE_TYPE_SHIFT;
1637 				continue;
1638 			}
1639 
1640 			s->sh_entsize = module_get_offset_and_type(mod, type, s, i);
1641 			pr_debug("\t%s\n", sname);
1642 		}
1643 	}
1644 }
1645 
1646 /*
1647  * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1648  * might -- code, read-only data, read-write data, small data.  Tally
1649  * sizes, and place the offsets into sh_entsize fields: high bit means it
1650  * belongs in init.
1651  */
1652 static void layout_sections(struct module *mod, struct load_info *info)
1653 {
1654 	unsigned int i;
1655 
1656 	for (i = 0; i < info->hdr->e_shnum; i++)
1657 		info->sechdrs[i].sh_entsize = ~0UL;
1658 
1659 	pr_debug("Core section allocation order for %s:\n", mod->name);
1660 	__layout_sections(mod, info, false);
1661 
1662 	pr_debug("Init section allocation order for %s:\n", mod->name);
1663 	__layout_sections(mod, info, true);
1664 }
1665 
1666 static void module_license_taint_check(struct module *mod, const char *license)
1667 {
1668 	if (!license)
1669 		license = "unspecified";
1670 
1671 	if (!license_is_gpl_compatible(license)) {
1672 		if (!test_taint(TAINT_PROPRIETARY_MODULE))
1673 			pr_warn("%s: module license '%s' taints kernel.\n",
1674 				mod->name, license);
1675 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
1676 				 LOCKDEP_NOW_UNRELIABLE);
1677 	}
1678 }
1679 
1680 static void setup_modinfo(struct module *mod, struct load_info *info)
1681 {
1682 	const struct module_attribute *attr;
1683 	int i;
1684 
1685 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
1686 		if (attr->setup)
1687 			attr->setup(mod, get_modinfo(info, attr->attr.name));
1688 	}
1689 }
1690 
1691 static void free_modinfo(struct module *mod)
1692 {
1693 	const struct module_attribute *attr;
1694 	int i;
1695 
1696 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
1697 		if (attr->free)
1698 			attr->free(mod);
1699 	}
1700 }
1701 
1702 bool __weak module_init_section(const char *name)
1703 {
1704 	return strstarts(name, ".init");
1705 }
1706 
1707 bool __weak module_exit_section(const char *name)
1708 {
1709 	return strstarts(name, ".exit");
1710 }
1711 
1712 static int validate_section_offset(const struct load_info *info, Elf_Shdr *shdr)
1713 {
1714 #if defined(CONFIG_64BIT)
1715 	unsigned long long secend;
1716 #else
1717 	unsigned long secend;
1718 #endif
1719 
1720 	/*
1721 	 * Check for both overflow and offset/size being
1722 	 * too large.
1723 	 */
1724 	secend = shdr->sh_offset + shdr->sh_size;
1725 	if (secend < shdr->sh_offset || secend > info->len)
1726 		return -ENOEXEC;
1727 
1728 	return 0;
1729 }
1730 
1731 /**
1732  * elf_validity_ehdr() - Checks an ELF header for module validity
1733  * @info: Load info containing the ELF header to check
1734  *
1735  * Checks whether an ELF header could belong to a valid module. Checks:
1736  *
1737  * * ELF header is within the data the user provided
1738  * * ELF magic is present
1739  * * It is relocatable (not final linked, not core file, etc.)
1740  * * The header's machine type matches what the architecture expects.
1741  * * Optional arch-specific hook for other properties
1742  *   - module_elf_check_arch() is currently only used by PPC to check
1743  *   ELF ABI version, but may be used by others in the future.
1744  *
1745  * Return: %0 if valid, %-ENOEXEC on failure.
1746  */
1747 static int elf_validity_ehdr(const struct load_info *info)
1748 {
1749 	if (info->len < sizeof(*(info->hdr))) {
1750 		pr_err("Invalid ELF header len %lu\n", info->len);
1751 		return -ENOEXEC;
1752 	}
1753 	if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0) {
1754 		pr_err("Invalid ELF header magic: != %s\n", ELFMAG);
1755 		return -ENOEXEC;
1756 	}
1757 	if (info->hdr->e_type != ET_REL) {
1758 		pr_err("Invalid ELF header type: %u != %u\n",
1759 		       info->hdr->e_type, ET_REL);
1760 		return -ENOEXEC;
1761 	}
1762 	if (!elf_check_arch(info->hdr)) {
1763 		pr_err("Invalid architecture in ELF header: %u\n",
1764 		       info->hdr->e_machine);
1765 		return -ENOEXEC;
1766 	}
1767 	if (!module_elf_check_arch(info->hdr)) {
1768 		pr_err("Invalid module architecture in ELF header: %u\n",
1769 		       info->hdr->e_machine);
1770 		return -ENOEXEC;
1771 	}
1772 	return 0;
1773 }
1774 
1775 /**
1776  * elf_validity_cache_sechdrs() - Cache section headers if valid
1777  * @info: Load info to compute section headers from
1778  *
1779  * Checks:
1780  *
1781  * * ELF header is valid (see elf_validity_ehdr())
1782  * * Section headers are the size we expect
1783  * * Section array fits in the user provided data
1784  * * Section index 0 is NULL
1785  * * Section contents are inbounds
1786  *
1787  * Then updates @info with a &load_info->sechdrs pointer if valid.
1788  *
1789  * Return: %0 if valid, negative error code if validation failed.
1790  */
1791 static int elf_validity_cache_sechdrs(struct load_info *info)
1792 {
1793 	Elf_Shdr *sechdrs;
1794 	Elf_Shdr *shdr;
1795 	int i;
1796 	int err;
1797 
1798 	err = elf_validity_ehdr(info);
1799 	if (err < 0)
1800 		return err;
1801 
1802 	if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) {
1803 		pr_err("Invalid ELF section header size\n");
1804 		return -ENOEXEC;
1805 	}
1806 
1807 	/*
1808 	 * e_shnum is 16 bits, and sizeof(Elf_Shdr) is
1809 	 * known and small. So e_shnum * sizeof(Elf_Shdr)
1810 	 * will not overflow unsigned long on any platform.
1811 	 */
1812 	if (info->hdr->e_shoff >= info->len
1813 	    || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
1814 		info->len - info->hdr->e_shoff)) {
1815 		pr_err("Invalid ELF section header overflow\n");
1816 		return -ENOEXEC;
1817 	}
1818 
1819 	sechdrs = (void *)info->hdr + info->hdr->e_shoff;
1820 
1821 	/*
1822 	 * The code assumes that section 0 has a length of zero and
1823 	 * an addr of zero, so check for it.
1824 	 */
1825 	if (sechdrs[0].sh_type != SHT_NULL
1826 	    || sechdrs[0].sh_size != 0
1827 	    || sechdrs[0].sh_addr != 0) {
1828 		pr_err("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n",
1829 		       sechdrs[0].sh_type);
1830 		return -ENOEXEC;
1831 	}
1832 
1833 	/* Validate contents are inbounds */
1834 	for (i = 1; i < info->hdr->e_shnum; i++) {
1835 		shdr = &sechdrs[i];
1836 		switch (shdr->sh_type) {
1837 		case SHT_NULL:
1838 		case SHT_NOBITS:
1839 			/* No contents, offset/size don't mean anything */
1840 			continue;
1841 		default:
1842 			err = validate_section_offset(info, shdr);
1843 			if (err < 0) {
1844 				pr_err("Invalid ELF section in module (section %u type %u)\n",
1845 				       i, shdr->sh_type);
1846 				return err;
1847 			}
1848 		}
1849 	}
1850 
1851 	info->sechdrs = sechdrs;
1852 
1853 	return 0;
1854 }
1855 
1856 /**
1857  * elf_validity_cache_secstrings() - Caches section names if valid
1858  * @info: Load info to cache section names from. Must have valid sechdrs.
1859  *
1860  * Specifically checks:
1861  *
1862  * * Section name table index is inbounds of section headers
1863  * * Section name table is not empty
1864  * * Section name table is NUL terminated
1865  * * All section name offsets are inbounds of the section
1866  *
1867  * Then updates @info with a &load_info->secstrings pointer if valid.
1868  *
1869  * Return: %0 if valid, negative error code if validation failed.
1870  */
1871 static int elf_validity_cache_secstrings(struct load_info *info)
1872 {
1873 	Elf_Shdr *strhdr, *shdr;
1874 	char *secstrings;
1875 	int i;
1876 
1877 	/*
1878 	 * Verify if the section name table index is valid.
1879 	 */
1880 	if (info->hdr->e_shstrndx == SHN_UNDEF
1881 	    || info->hdr->e_shstrndx >= info->hdr->e_shnum) {
1882 		pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n",
1883 		       info->hdr->e_shstrndx, info->hdr->e_shstrndx,
1884 		       info->hdr->e_shnum);
1885 		return -ENOEXEC;
1886 	}
1887 
1888 	strhdr = &info->sechdrs[info->hdr->e_shstrndx];
1889 
1890 	/*
1891 	 * The section name table must be NUL-terminated, as required
1892 	 * by the spec. This makes strcmp and pr_* calls that access
1893 	 * strings in the section safe.
1894 	 */
1895 	secstrings = (void *)info->hdr + strhdr->sh_offset;
1896 	if (strhdr->sh_size == 0) {
1897 		pr_err("empty section name table\n");
1898 		return -ENOEXEC;
1899 	}
1900 	if (secstrings[strhdr->sh_size - 1] != '\0') {
1901 		pr_err("ELF Spec violation: section name table isn't null terminated\n");
1902 		return -ENOEXEC;
1903 	}
1904 
1905 	for (i = 0; i < info->hdr->e_shnum; i++) {
1906 		shdr = &info->sechdrs[i];
1907 		/* SHT_NULL means sh_name has an undefined value */
1908 		if (shdr->sh_type == SHT_NULL)
1909 			continue;
1910 		if (shdr->sh_name >= strhdr->sh_size) {
1911 			pr_err("Invalid ELF section name in module (section %u type %u)\n",
1912 			       i, shdr->sh_type);
1913 			return -ENOEXEC;
1914 		}
1915 	}
1916 
1917 	info->secstrings = secstrings;
1918 	return 0;
1919 }
1920 
1921 /**
1922  * elf_validity_cache_index_info() - Validate and cache modinfo section
1923  * @info: Load info to populate the modinfo index on.
1924  *        Must have &load_info->sechdrs and &load_info->secstrings populated
1925  *
1926  * Checks that if there is a .modinfo section, it is unique.
1927  * Then, it caches its index in &load_info->index.info.
1928  * Finally, it tries to populate the name to improve error messages.
1929  *
1930  * Return: %0 if valid, %-ENOEXEC if multiple modinfo sections were found.
1931  */
1932 static int elf_validity_cache_index_info(struct load_info *info)
1933 {
1934 	int info_idx;
1935 
1936 	info_idx = find_any_unique_sec(info, ".modinfo");
1937 
1938 	if (info_idx == 0)
1939 		/* Early return, no .modinfo */
1940 		return 0;
1941 
1942 	if (info_idx < 0) {
1943 		pr_err("Only one .modinfo section must exist.\n");
1944 		return -ENOEXEC;
1945 	}
1946 
1947 	info->index.info = info_idx;
1948 	/* Try to find a name early so we can log errors with a module name */
1949 	info->name = get_modinfo(info, "name");
1950 
1951 	return 0;
1952 }
1953 
1954 /**
1955  * elf_validity_cache_index_mod() - Validates and caches this_module section
1956  * @info: Load info to cache this_module on.
1957  *        Must have &load_info->sechdrs and &load_info->secstrings populated
1958  *
1959  * The ".gnu.linkonce.this_module" ELF section is special. It is what modpost
1960  * uses to refer to __this_module and let's use rely on THIS_MODULE to point
1961  * to &__this_module properly. The kernel's modpost declares it on each
1962  * modules's *.mod.c file. If the struct module of the kernel changes a full
1963  * kernel rebuild is required.
1964  *
1965  * We have a few expectations for this special section, this function
1966  * validates all this for us:
1967  *
1968  * * The section has contents
1969  * * The section is unique
1970  * * We expect the kernel to always have to allocate it: SHF_ALLOC
1971  * * The section size must match the kernel's run time's struct module
1972  *   size
1973  *
1974  * If all checks pass, the index will be cached in &load_info->index.mod
1975  *
1976  * Return: %0 on validation success, %-ENOEXEC on failure
1977  */
1978 static int elf_validity_cache_index_mod(struct load_info *info)
1979 {
1980 	Elf_Shdr *shdr;
1981 	int mod_idx;
1982 
1983 	mod_idx = find_any_unique_sec(info, ".gnu.linkonce.this_module");
1984 	if (mod_idx <= 0) {
1985 		pr_err("module %s: Exactly one .gnu.linkonce.this_module section must exist.\n",
1986 		       info->name ?: "(missing .modinfo section or name field)");
1987 		return -ENOEXEC;
1988 	}
1989 
1990 	shdr = &info->sechdrs[mod_idx];
1991 
1992 	if (shdr->sh_type == SHT_NOBITS) {
1993 		pr_err("module %s: .gnu.linkonce.this_module section must have a size set\n",
1994 		       info->name ?: "(missing .modinfo section or name field)");
1995 		return -ENOEXEC;
1996 	}
1997 
1998 	if (!(shdr->sh_flags & SHF_ALLOC)) {
1999 		pr_err("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n",
2000 		       info->name ?: "(missing .modinfo section or name field)");
2001 		return -ENOEXEC;
2002 	}
2003 
2004 	if (shdr->sh_size != sizeof(struct module)) {
2005 		pr_err("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n",
2006 		       info->name ?: "(missing .modinfo section or name field)");
2007 		return -ENOEXEC;
2008 	}
2009 
2010 	info->index.mod = mod_idx;
2011 
2012 	return 0;
2013 }
2014 
2015 /**
2016  * elf_validity_cache_index_sym() - Validate and cache symtab index
2017  * @info: Load info to cache symtab index in.
2018  *        Must have &load_info->sechdrs and &load_info->secstrings populated.
2019  *
2020  * Checks that there is exactly one symbol table, then caches its index in
2021  * &load_info->index.sym.
2022  *
2023  * Return: %0 if valid, %-ENOEXEC on failure.
2024  */
2025 static int elf_validity_cache_index_sym(struct load_info *info)
2026 {
2027 	unsigned int sym_idx;
2028 	unsigned int num_sym_secs = 0;
2029 	int i;
2030 
2031 	for (i = 1; i < info->hdr->e_shnum; i++) {
2032 		if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2033 			num_sym_secs++;
2034 			sym_idx = i;
2035 		}
2036 	}
2037 
2038 	if (num_sym_secs != 1) {
2039 		pr_warn("%s: module has no symbols (stripped?)\n",
2040 			info->name ?: "(missing .modinfo section or name field)");
2041 		return -ENOEXEC;
2042 	}
2043 
2044 	info->index.sym = sym_idx;
2045 
2046 	return 0;
2047 }
2048 
2049 /**
2050  * elf_validity_cache_index_str() - Validate and cache strtab index
2051  * @info: Load info to cache strtab index in.
2052  *        Must have &load_info->sechdrs and &load_info->secstrings populated.
2053  *        Must have &load_info->index.sym populated.
2054  *
2055  * Looks at the symbol table's associated string table, makes sure it is
2056  * in-bounds, and caches it.
2057  *
2058  * Return: %0 if valid, %-ENOEXEC on failure.
2059  */
2060 static int elf_validity_cache_index_str(struct load_info *info)
2061 {
2062 	unsigned int str_idx = info->sechdrs[info->index.sym].sh_link;
2063 
2064 	if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) {
2065 		pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n",
2066 		       str_idx, str_idx, info->hdr->e_shnum);
2067 		return -ENOEXEC;
2068 	}
2069 
2070 	info->index.str = str_idx;
2071 	return 0;
2072 }
2073 
2074 /**
2075  * elf_validity_cache_index_versions() - Validate and cache version indices
2076  * @info:  Load info to cache version indices in.
2077  *         Must have &load_info->sechdrs and &load_info->secstrings populated.
2078  * @flags: Load flags, relevant to suppress version loading, see
2079  *         uapi/linux/module.h
2080  *
2081  * If we're ignoring modversions based on @flags, zero all version indices
2082  * and return validity. Othewrise check:
2083  *
2084  * * If "__version_ext_crcs" is present, "__version_ext_names" is present
2085  * * There is a name present for every crc
2086  *
2087  * Then populate:
2088  *
2089  * * &load_info->index.vers
2090  * * &load_info->index.vers_ext_crc
2091  * * &load_info->index.vers_ext_names
2092  *
2093  * if present.
2094  *
2095  * Return: %0 if valid, %-ENOEXEC on failure.
2096  */
2097 static int elf_validity_cache_index_versions(struct load_info *info, int flags)
2098 {
2099 	unsigned int vers_ext_crc;
2100 	unsigned int vers_ext_name;
2101 	size_t crc_count;
2102 	size_t remaining_len;
2103 	size_t name_size;
2104 	char *name;
2105 
2106 	/* If modversions were suppressed, pretend we didn't find any */
2107 	if (flags & MODULE_INIT_IGNORE_MODVERSIONS) {
2108 		info->index.vers = 0;
2109 		info->index.vers_ext_crc = 0;
2110 		info->index.vers_ext_name = 0;
2111 		return 0;
2112 	}
2113 
2114 	vers_ext_crc = find_sec(info, "__version_ext_crcs");
2115 	vers_ext_name = find_sec(info, "__version_ext_names");
2116 
2117 	/* If we have one field, we must have the other */
2118 	if (!!vers_ext_crc != !!vers_ext_name) {
2119 		pr_err("extended version crc+name presence does not match");
2120 		return -ENOEXEC;
2121 	}
2122 
2123 	/*
2124 	 * If we have extended version information, we should have the same
2125 	 * number of entries in every section.
2126 	 */
2127 	if (vers_ext_crc) {
2128 		crc_count = info->sechdrs[vers_ext_crc].sh_size / sizeof(u32);
2129 		name = (void *)info->hdr +
2130 			info->sechdrs[vers_ext_name].sh_offset;
2131 		remaining_len = info->sechdrs[vers_ext_name].sh_size;
2132 
2133 		while (crc_count--) {
2134 			name_size = strnlen(name, remaining_len) + 1;
2135 			if (name_size > remaining_len) {
2136 				pr_err("more extended version crcs than names");
2137 				return -ENOEXEC;
2138 			}
2139 			remaining_len -= name_size;
2140 			name += name_size;
2141 		}
2142 	}
2143 
2144 	info->index.vers = find_sec(info, "__versions");
2145 	info->index.vers_ext_crc = vers_ext_crc;
2146 	info->index.vers_ext_name = vers_ext_name;
2147 	return 0;
2148 }
2149 
2150 /**
2151  * elf_validity_cache_index() - Resolve, validate, cache section indices
2152  * @info:  Load info to read from and update.
2153  *         &load_info->sechdrs and &load_info->secstrings must be populated.
2154  * @flags: Load flags, relevant to suppress version loading, see
2155  *         uapi/linux/module.h
2156  *
2157  * Populates &load_info->index, validating as it goes.
2158  * See child functions for per-field validation:
2159  *
2160  * * elf_validity_cache_index_info()
2161  * * elf_validity_cache_index_mod()
2162  * * elf_validity_cache_index_sym()
2163  * * elf_validity_cache_index_str()
2164  * * elf_validity_cache_index_versions()
2165  *
2166  * If CONFIG_SMP is enabled, load the percpu section by name with no
2167  * validation.
2168  *
2169  * Return: 0 on success, negative error code if an index failed validation.
2170  */
2171 static int elf_validity_cache_index(struct load_info *info, int flags)
2172 {
2173 	int err;
2174 
2175 	err = elf_validity_cache_index_info(info);
2176 	if (err < 0)
2177 		return err;
2178 	err = elf_validity_cache_index_mod(info);
2179 	if (err < 0)
2180 		return err;
2181 	err = elf_validity_cache_index_sym(info);
2182 	if (err < 0)
2183 		return err;
2184 	err = elf_validity_cache_index_str(info);
2185 	if (err < 0)
2186 		return err;
2187 	err = elf_validity_cache_index_versions(info, flags);
2188 	if (err < 0)
2189 		return err;
2190 
2191 	info->index.pcpu = find_pcpusec(info);
2192 
2193 	return 0;
2194 }
2195 
2196 /**
2197  * elf_validity_cache_strtab() - Validate and cache symbol string table
2198  * @info: Load info to read from and update.
2199  *        Must have &load_info->sechdrs and &load_info->secstrings populated.
2200  *        Must have &load_info->index populated.
2201  *
2202  * Checks:
2203  *
2204  * * The string table is not empty.
2205  * * The string table starts and ends with NUL (required by ELF spec).
2206  * * Every &Elf_Sym->st_name offset in the symbol table is inbounds of the
2207  *   string table.
2208  *
2209  * And caches the pointer as &load_info->strtab in @info.
2210  *
2211  * Return: 0 on success, negative error code if a check failed.
2212  */
2213 static int elf_validity_cache_strtab(struct load_info *info)
2214 {
2215 	Elf_Shdr *str_shdr = &info->sechdrs[info->index.str];
2216 	Elf_Shdr *sym_shdr = &info->sechdrs[info->index.sym];
2217 	char *strtab = (char *)info->hdr + str_shdr->sh_offset;
2218 	Elf_Sym *syms = (void *)info->hdr + sym_shdr->sh_offset;
2219 	int i;
2220 
2221 	if (str_shdr->sh_size == 0) {
2222 		pr_err("empty symbol string table\n");
2223 		return -ENOEXEC;
2224 	}
2225 	if (strtab[0] != '\0') {
2226 		pr_err("symbol string table missing leading NUL\n");
2227 		return -ENOEXEC;
2228 	}
2229 	if (strtab[str_shdr->sh_size - 1] != '\0') {
2230 		pr_err("symbol string table isn't NUL terminated\n");
2231 		return -ENOEXEC;
2232 	}
2233 
2234 	/*
2235 	 * Now that we know strtab is correctly structured, check symbol
2236 	 * starts are inbounds before they're used later.
2237 	 */
2238 	for (i = 0; i < sym_shdr->sh_size / sizeof(*syms); i++) {
2239 		if (syms[i].st_name >= str_shdr->sh_size) {
2240 			pr_err("symbol name out of bounds in string table");
2241 			return -ENOEXEC;
2242 		}
2243 	}
2244 
2245 	info->strtab = strtab;
2246 	return 0;
2247 }
2248 
2249 /*
2250  * Check userspace passed ELF module against our expectations, and cache
2251  * useful variables for further processing as we go.
2252  *
2253  * This does basic validity checks against section offsets and sizes, the
2254  * section name string table, and the indices used for it (sh_name).
2255  *
2256  * As a last step, since we're already checking the ELF sections we cache
2257  * useful variables which will be used later for our convenience:
2258  *
2259  * 	o pointers to section headers
2260  * 	o cache the modinfo symbol section
2261  * 	o cache the string symbol section
2262  * 	o cache the module section
2263  *
2264  * As a last step we set info->mod to the temporary copy of the module in
2265  * info->hdr. The final one will be allocated in move_module(). Any
2266  * modifications we make to our copy of the module will be carried over
2267  * to the final minted module.
2268  */
2269 static int elf_validity_cache_copy(struct load_info *info, int flags)
2270 {
2271 	int err;
2272 
2273 	err = elf_validity_cache_sechdrs(info);
2274 	if (err < 0)
2275 		return err;
2276 	err = elf_validity_cache_secstrings(info);
2277 	if (err < 0)
2278 		return err;
2279 	err = elf_validity_cache_index(info, flags);
2280 	if (err < 0)
2281 		return err;
2282 	err = elf_validity_cache_strtab(info);
2283 	if (err < 0)
2284 		return err;
2285 
2286 	/* This is temporary: point mod into copy of data. */
2287 	info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
2288 
2289 	/*
2290 	 * If we didn't load the .modinfo 'name' field earlier, fall back to
2291 	 * on-disk struct mod 'name' field.
2292 	 */
2293 	if (!info->name)
2294 		info->name = info->mod->name;
2295 
2296 	return 0;
2297 }
2298 
2299 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2300 
2301 static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2302 {
2303 	do {
2304 		unsigned long n = min(len, COPY_CHUNK_SIZE);
2305 
2306 		if (copy_from_user(dst, usrc, n) != 0)
2307 			return -EFAULT;
2308 		cond_resched();
2309 		dst += n;
2310 		usrc += n;
2311 		len -= n;
2312 	} while (len);
2313 	return 0;
2314 }
2315 
2316 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2317 {
2318 	if (!get_modinfo(info, "livepatch"))
2319 		/* Nothing more to do */
2320 		return 0;
2321 
2322 	if (set_livepatch_module(mod))
2323 		return 0;
2324 
2325 	pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2326 	       mod->name);
2327 	return -ENOEXEC;
2328 }
2329 
2330 static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2331 {
2332 	if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2333 		return;
2334 
2335 	pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2336 		mod->name);
2337 }
2338 
2339 /* Sets info->hdr and info->len. */
2340 static int copy_module_from_user(const void __user *umod, unsigned long len,
2341 				  struct load_info *info)
2342 {
2343 	int err;
2344 
2345 	info->len = len;
2346 	if (info->len < sizeof(*(info->hdr)))
2347 		return -ENOEXEC;
2348 
2349 	err = security_kernel_load_data(LOADING_MODULE, true);
2350 	if (err)
2351 		return err;
2352 
2353 	/* Suck in entire file: we'll want most of it. */
2354 	info->hdr = __vmalloc(info->len, GFP_KERNEL | __GFP_NOWARN);
2355 	if (!info->hdr)
2356 		return -ENOMEM;
2357 
2358 	if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
2359 		err = -EFAULT;
2360 		goto out;
2361 	}
2362 
2363 	err = security_kernel_post_load_data((char *)info->hdr, info->len,
2364 					     LOADING_MODULE, "init_module");
2365 out:
2366 	if (err)
2367 		vfree(info->hdr);
2368 
2369 	return err;
2370 }
2371 
2372 static void free_copy(struct load_info *info, int flags)
2373 {
2374 	if (flags & MODULE_INIT_COMPRESSED_FILE)
2375 		module_decompress_cleanup(info);
2376 	else
2377 		vfree(info->hdr);
2378 }
2379 
2380 static int rewrite_section_headers(struct load_info *info, int flags)
2381 {
2382 	unsigned int i;
2383 
2384 	/* This should always be true, but let's be sure. */
2385 	info->sechdrs[0].sh_addr = 0;
2386 
2387 	for (i = 1; i < info->hdr->e_shnum; i++) {
2388 		Elf_Shdr *shdr = &info->sechdrs[i];
2389 
2390 		/*
2391 		 * Mark all sections sh_addr with their address in the
2392 		 * temporary image.
2393 		 */
2394 		shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2395 
2396 	}
2397 
2398 	/* Track but don't keep modinfo and version sections. */
2399 	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2400 	info->sechdrs[info->index.vers_ext_crc].sh_flags &=
2401 		~(unsigned long)SHF_ALLOC;
2402 	info->sechdrs[info->index.vers_ext_name].sh_flags &=
2403 		~(unsigned long)SHF_ALLOC;
2404 	info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2405 
2406 	return 0;
2407 }
2408 
2409 static const char *const module_license_offenders[] = {
2410 	/* driverloader was caught wrongly pretending to be under GPL */
2411 	"driverloader",
2412 
2413 	/* lve claims to be GPL but upstream won't provide source */
2414 	"lve",
2415 };
2416 
2417 /*
2418  * These calls taint the kernel depending certain module circumstances */
2419 static void module_augment_kernel_taints(struct module *mod, struct load_info *info)
2420 {
2421 	int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
2422 	size_t i;
2423 
2424 	if (!get_modinfo(info, "intree")) {
2425 		if (!test_taint(TAINT_OOT_MODULE))
2426 			pr_warn("%s: loading out-of-tree module taints kernel.\n",
2427 				mod->name);
2428 		add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
2429 	}
2430 
2431 	check_modinfo_retpoline(mod, info);
2432 
2433 	if (get_modinfo(info, "staging")) {
2434 		add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
2435 		pr_warn("%s: module is from the staging directory, the quality "
2436 			"is unknown, you have been warned.\n", mod->name);
2437 	}
2438 
2439 	if (is_livepatch_module(mod)) {
2440 		add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
2441 		pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2442 				mod->name);
2443 	}
2444 
2445 	module_license_taint_check(mod, get_modinfo(info, "license"));
2446 
2447 	if (get_modinfo(info, "test")) {
2448 		if (!test_taint(TAINT_TEST))
2449 			pr_warn("%s: loading test module taints kernel.\n",
2450 				mod->name);
2451 		add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK);
2452 	}
2453 #ifdef CONFIG_MODULE_SIG
2454 	mod->sig_ok = info->sig_ok;
2455 	if (!mod->sig_ok) {
2456 		pr_notice_once("%s: module verification failed: signature "
2457 			       "and/or required key missing - tainting "
2458 			       "kernel\n", mod->name);
2459 		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
2460 	}
2461 #endif
2462 
2463 	/*
2464 	 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2465 	 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2466 	 * using GPL-only symbols it needs.
2467 	 */
2468 	if (strcmp(mod->name, "ndiswrapper") == 0)
2469 		add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
2470 
2471 	for (i = 0; i < ARRAY_SIZE(module_license_offenders); ++i) {
2472 		if (strcmp(mod->name, module_license_offenders[i]) == 0)
2473 			add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2474 					 LOCKDEP_NOW_UNRELIABLE);
2475 	}
2476 
2477 	if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
2478 		pr_warn("%s: module license taints kernel.\n", mod->name);
2479 
2480 }
2481 
2482 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
2483 {
2484 	const char *modmagic = get_modinfo(info, "vermagic");
2485 	int err;
2486 
2487 	if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2488 		modmagic = NULL;
2489 
2490 	/* This is allowed: modprobe --force will invalidate it. */
2491 	if (!modmagic) {
2492 		err = try_to_force_load(mod, "bad vermagic");
2493 		if (err)
2494 			return err;
2495 	} else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2496 		pr_err("%s: version magic '%s' should be '%s'\n",
2497 		       info->name, modmagic, vermagic);
2498 		return -ENOEXEC;
2499 	}
2500 
2501 	err = check_modinfo_livepatch(mod, info);
2502 	if (err)
2503 		return err;
2504 
2505 	return 0;
2506 }
2507 
2508 static int find_module_sections(struct module *mod, struct load_info *info)
2509 {
2510 	mod->kp = section_objs(info, "__param",
2511 			       sizeof(*mod->kp), &mod->num_kp);
2512 	mod->syms = section_objs(info, "__ksymtab",
2513 				 sizeof(*mod->syms), &mod->num_syms);
2514 	mod->crcs = section_addr(info, "__kcrctab");
2515 	mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2516 				     sizeof(*mod->gpl_syms),
2517 				     &mod->num_gpl_syms);
2518 	mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2519 
2520 #ifdef CONFIG_CONSTRUCTORS
2521 	mod->ctors = section_objs(info, ".ctors",
2522 				  sizeof(*mod->ctors), &mod->num_ctors);
2523 	if (!mod->ctors)
2524 		mod->ctors = section_objs(info, ".init_array",
2525 				sizeof(*mod->ctors), &mod->num_ctors);
2526 	else if (find_sec(info, ".init_array")) {
2527 		/*
2528 		 * This shouldn't happen with same compiler and binutils
2529 		 * building all parts of the module.
2530 		 */
2531 		pr_warn("%s: has both .ctors and .init_array.\n",
2532 		       mod->name);
2533 		return -EINVAL;
2534 	}
2535 #endif
2536 
2537 	mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1,
2538 						&mod->noinstr_text_size);
2539 
2540 #ifdef CONFIG_TRACEPOINTS
2541 	mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2542 					     sizeof(*mod->tracepoints_ptrs),
2543 					     &mod->num_tracepoints);
2544 #endif
2545 #ifdef CONFIG_TREE_SRCU
2546 	mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
2547 					     sizeof(*mod->srcu_struct_ptrs),
2548 					     &mod->num_srcu_structs);
2549 #endif
2550 #ifdef CONFIG_BPF_EVENTS
2551 	mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
2552 					   sizeof(*mod->bpf_raw_events),
2553 					   &mod->num_bpf_raw_events);
2554 #endif
2555 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
2556 	mod->btf_data = any_section_objs(info, ".BTF", 1, &mod->btf_data_size);
2557 	mod->btf_base_data = any_section_objs(info, ".BTF.base", 1,
2558 					      &mod->btf_base_data_size);
2559 #endif
2560 #ifdef CONFIG_JUMP_LABEL
2561 	mod->jump_entries = section_objs(info, "__jump_table",
2562 					sizeof(*mod->jump_entries),
2563 					&mod->num_jump_entries);
2564 #endif
2565 #ifdef CONFIG_EVENT_TRACING
2566 	mod->trace_events = section_objs(info, "_ftrace_events",
2567 					 sizeof(*mod->trace_events),
2568 					 &mod->num_trace_events);
2569 	mod->trace_evals = section_objs(info, "_ftrace_eval_map",
2570 					sizeof(*mod->trace_evals),
2571 					&mod->num_trace_evals);
2572 #endif
2573 #ifdef CONFIG_TRACING
2574 	mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2575 					 sizeof(*mod->trace_bprintk_fmt_start),
2576 					 &mod->num_trace_bprintk_fmt);
2577 #endif
2578 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2579 	/* sechdrs[0].sh_size is always zero */
2580 	mod->ftrace_callsites = section_objs(info, FTRACE_CALLSITE_SECTION,
2581 					     sizeof(*mod->ftrace_callsites),
2582 					     &mod->num_ftrace_callsites);
2583 #endif
2584 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
2585 	mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
2586 					    sizeof(*mod->ei_funcs),
2587 					    &mod->num_ei_funcs);
2588 #endif
2589 #ifdef CONFIG_KPROBES
2590 	mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
2591 						&mod->kprobes_text_size);
2592 	mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
2593 						sizeof(unsigned long),
2594 						&mod->num_kprobe_blacklist);
2595 #endif
2596 #ifdef CONFIG_PRINTK_INDEX
2597 	mod->printk_index_start = section_objs(info, ".printk_index",
2598 					       sizeof(*mod->printk_index_start),
2599 					       &mod->printk_index_size);
2600 #endif
2601 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
2602 	mod->static_call_sites = section_objs(info, ".static_call_sites",
2603 					      sizeof(*mod->static_call_sites),
2604 					      &mod->num_static_call_sites);
2605 #endif
2606 #if IS_ENABLED(CONFIG_KUNIT)
2607 	mod->kunit_suites = section_objs(info, ".kunit_test_suites",
2608 					      sizeof(*mod->kunit_suites),
2609 					      &mod->num_kunit_suites);
2610 	mod->kunit_init_suites = section_objs(info, ".kunit_init_test_suites",
2611 					      sizeof(*mod->kunit_init_suites),
2612 					      &mod->num_kunit_init_suites);
2613 #endif
2614 
2615 	mod->extable = section_objs(info, "__ex_table",
2616 				    sizeof(*mod->extable), &mod->num_exentries);
2617 
2618 	if (section_addr(info, "__obsparm"))
2619 		pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
2620 
2621 #ifdef CONFIG_DYNAMIC_DEBUG_CORE
2622 	mod->dyndbg_info.descs = section_objs(info, "__dyndbg",
2623 					      sizeof(*mod->dyndbg_info.descs),
2624 					      &mod->dyndbg_info.num_descs);
2625 	mod->dyndbg_info.classes = section_objs(info, "__dyndbg_classes",
2626 						sizeof(*mod->dyndbg_info.classes),
2627 						&mod->dyndbg_info.num_classes);
2628 #endif
2629 
2630 	return 0;
2631 }
2632 
2633 static int move_module(struct module *mod, struct load_info *info)
2634 {
2635 	int i;
2636 	enum mod_mem_type t = 0;
2637 	int ret = -ENOMEM;
2638 	bool codetag_section_found = false;
2639 
2640 	for_each_mod_mem_type(type) {
2641 		if (!mod->mem[type].size) {
2642 			mod->mem[type].base = NULL;
2643 			mod->mem[type].rw_copy = NULL;
2644 			continue;
2645 		}
2646 
2647 		ret = module_memory_alloc(mod, type);
2648 		if (ret) {
2649 			t = type;
2650 			goto out_err;
2651 		}
2652 	}
2653 
2654 	/* Transfer each section which specifies SHF_ALLOC */
2655 	pr_debug("Final section addresses for %s:\n", mod->name);
2656 	for (i = 0; i < info->hdr->e_shnum; i++) {
2657 		void *dest;
2658 		Elf_Shdr *shdr = &info->sechdrs[i];
2659 		const char *sname;
2660 		unsigned long addr;
2661 
2662 		if (!(shdr->sh_flags & SHF_ALLOC))
2663 			continue;
2664 
2665 		sname = info->secstrings + shdr->sh_name;
2666 		/*
2667 		 * Load codetag sections separately as they might still be used
2668 		 * after module unload.
2669 		 */
2670 		if (codetag_needs_module_section(mod, sname, shdr->sh_size)) {
2671 			dest = codetag_alloc_module_section(mod, sname, shdr->sh_size,
2672 					arch_mod_section_prepend(mod, i), shdr->sh_addralign);
2673 			if (WARN_ON(!dest)) {
2674 				ret = -EINVAL;
2675 				goto out_err;
2676 			}
2677 			if (IS_ERR(dest)) {
2678 				ret = PTR_ERR(dest);
2679 				goto out_err;
2680 			}
2681 			addr = (unsigned long)dest;
2682 			codetag_section_found = true;
2683 		} else {
2684 			enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT;
2685 			unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK;
2686 
2687 			addr = (unsigned long)mod->mem[type].base + offset;
2688 			dest = mod->mem[type].rw_copy + offset;
2689 		}
2690 
2691 		if (shdr->sh_type != SHT_NOBITS) {
2692 			/*
2693 			 * Our ELF checker already validated this, but let's
2694 			 * be pedantic and make the goal clearer. We actually
2695 			 * end up copying over all modifications made to the
2696 			 * userspace copy of the entire struct module.
2697 			 */
2698 			if (i == info->index.mod &&
2699 			   (WARN_ON_ONCE(shdr->sh_size != sizeof(struct module)))) {
2700 				ret = -ENOEXEC;
2701 				goto out_err;
2702 			}
2703 			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2704 		}
2705 		/*
2706 		 * Update the userspace copy's ELF section address to point to
2707 		 * our newly allocated memory as a pure convenience so that
2708 		 * users of info can keep taking advantage and using the newly
2709 		 * minted official memory area.
2710 		 */
2711 		shdr->sh_addr = addr;
2712 		pr_debug("\t0x%lx 0x%.8lx %s\n", (long)shdr->sh_addr,
2713 			 (long)shdr->sh_size, info->secstrings + shdr->sh_name);
2714 	}
2715 
2716 	return 0;
2717 out_err:
2718 	for (t--; t >= 0; t--)
2719 		module_memory_free(mod, t);
2720 	if (codetag_section_found)
2721 		codetag_free_module_sections(mod);
2722 
2723 	return ret;
2724 }
2725 
2726 static int check_export_symbol_versions(struct module *mod)
2727 {
2728 #ifdef CONFIG_MODVERSIONS
2729 	if ((mod->num_syms && !mod->crcs) ||
2730 	    (mod->num_gpl_syms && !mod->gpl_crcs)) {
2731 		return try_to_force_load(mod,
2732 					 "no versions for exported symbols");
2733 	}
2734 #endif
2735 	return 0;
2736 }
2737 
2738 static void flush_module_icache(const struct module *mod)
2739 {
2740 	/*
2741 	 * Flush the instruction cache, since we've played with text.
2742 	 * Do it before processing of module parameters, so the module
2743 	 * can provide parameter accessor functions of its own.
2744 	 */
2745 	for_each_mod_mem_type(type) {
2746 		const struct module_memory *mod_mem = &mod->mem[type];
2747 
2748 		if (mod_mem->size) {
2749 			flush_icache_range((unsigned long)mod_mem->base,
2750 					   (unsigned long)mod_mem->base + mod_mem->size);
2751 		}
2752 	}
2753 }
2754 
2755 bool __weak module_elf_check_arch(Elf_Ehdr *hdr)
2756 {
2757 	return true;
2758 }
2759 
2760 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2761 				     Elf_Shdr *sechdrs,
2762 				     char *secstrings,
2763 				     struct module *mod)
2764 {
2765 	return 0;
2766 }
2767 
2768 /* module_blacklist is a comma-separated list of module names */
2769 static char *module_blacklist;
2770 static bool blacklisted(const char *module_name)
2771 {
2772 	const char *p;
2773 	size_t len;
2774 
2775 	if (!module_blacklist)
2776 		return false;
2777 
2778 	for (p = module_blacklist; *p; p += len) {
2779 		len = strcspn(p, ",");
2780 		if (strlen(module_name) == len && !memcmp(module_name, p, len))
2781 			return true;
2782 		if (p[len] == ',')
2783 			len++;
2784 	}
2785 	return false;
2786 }
2787 core_param(module_blacklist, module_blacklist, charp, 0400);
2788 
2789 static struct module *layout_and_allocate(struct load_info *info, int flags)
2790 {
2791 	struct module *mod;
2792 	unsigned int ndx;
2793 	int err;
2794 
2795 	/* Allow arches to frob section contents and sizes.  */
2796 	err = module_frob_arch_sections(info->hdr, info->sechdrs,
2797 					info->secstrings, info->mod);
2798 	if (err < 0)
2799 		return ERR_PTR(err);
2800 
2801 	err = module_enforce_rwx_sections(info->hdr, info->sechdrs,
2802 					  info->secstrings, info->mod);
2803 	if (err < 0)
2804 		return ERR_PTR(err);
2805 
2806 	/* We will do a special allocation for per-cpu sections later. */
2807 	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
2808 
2809 	/*
2810 	 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
2811 	 * layout_sections() can put it in the right place.
2812 	 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
2813 	 */
2814 	ndx = find_sec(info, ".data..ro_after_init");
2815 	if (ndx)
2816 		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
2817 	/*
2818 	 * Mark the __jump_table section as ro_after_init as well: these data
2819 	 * structures are never modified, with the exception of entries that
2820 	 * refer to code in the __init section, which are annotated as such
2821 	 * at module load time.
2822 	 */
2823 	ndx = find_sec(info, "__jump_table");
2824 	if (ndx)
2825 		info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
2826 
2827 	/*
2828 	 * Determine total sizes, and put offsets in sh_entsize.  For now
2829 	 * this is done generically; there doesn't appear to be any
2830 	 * special cases for the architectures.
2831 	 */
2832 	layout_sections(info->mod, info);
2833 	layout_symtab(info->mod, info);
2834 
2835 	/* Allocate and move to the final place */
2836 	err = move_module(info->mod, info);
2837 	if (err)
2838 		return ERR_PTR(err);
2839 
2840 	/* Module has been copied to its final place now: return it. */
2841 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2842 	kmemleak_load_module(mod, info);
2843 	codetag_module_replaced(info->mod, mod);
2844 
2845 	return mod;
2846 }
2847 
2848 /* mod is no longer valid after this! */
2849 static void module_deallocate(struct module *mod, struct load_info *info)
2850 {
2851 	percpu_modfree(mod);
2852 	module_arch_freeing_init(mod);
2853 
2854 	free_mod_mem(mod);
2855 }
2856 
2857 int __weak module_finalize(const Elf_Ehdr *hdr,
2858 			   const Elf_Shdr *sechdrs,
2859 			   struct module *me)
2860 {
2861 	return 0;
2862 }
2863 
2864 int __weak module_post_finalize(const Elf_Ehdr *hdr,
2865 				const Elf_Shdr *sechdrs,
2866 				struct module *me)
2867 {
2868 	return 0;
2869 }
2870 
2871 static int post_relocation(struct module *mod, const struct load_info *info)
2872 {
2873 	int ret;
2874 
2875 	/* Sort exception table now relocations are done. */
2876 	sort_extable(mod->extable, mod->extable + mod->num_exentries);
2877 
2878 	/* Copy relocated percpu area over. */
2879 	percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2880 		       info->sechdrs[info->index.pcpu].sh_size);
2881 
2882 	/* Setup kallsyms-specific fields. */
2883 	add_kallsyms(mod, info);
2884 
2885 	/* Arch-specific module finalizing. */
2886 	ret = module_finalize(info->hdr, info->sechdrs, mod);
2887 	if (ret)
2888 		return ret;
2889 
2890 	for_each_mod_mem_type(type) {
2891 		struct module_memory *mem = &mod->mem[type];
2892 
2893 		if (mem->is_rox) {
2894 			if (!execmem_update_copy(mem->base, mem->rw_copy,
2895 						 mem->size))
2896 				return -ENOMEM;
2897 
2898 			vfree(mem->rw_copy);
2899 			mem->rw_copy = NULL;
2900 		}
2901 	}
2902 
2903 	return module_post_finalize(info->hdr, info->sechdrs, mod);
2904 }
2905 
2906 /* Call module constructors. */
2907 static void do_mod_ctors(struct module *mod)
2908 {
2909 #ifdef CONFIG_CONSTRUCTORS
2910 	unsigned long i;
2911 
2912 	for (i = 0; i < mod->num_ctors; i++)
2913 		mod->ctors[i]();
2914 #endif
2915 }
2916 
2917 /* For freeing module_init on success, in case kallsyms traversing */
2918 struct mod_initfree {
2919 	struct llist_node node;
2920 	void *init_text;
2921 	void *init_data;
2922 	void *init_rodata;
2923 };
2924 
2925 static void do_free_init(struct work_struct *w)
2926 {
2927 	struct llist_node *pos, *n, *list;
2928 	struct mod_initfree *initfree;
2929 
2930 	list = llist_del_all(&init_free_list);
2931 
2932 	synchronize_rcu();
2933 
2934 	llist_for_each_safe(pos, n, list) {
2935 		initfree = container_of(pos, struct mod_initfree, node);
2936 		execmem_free(initfree->init_text);
2937 		execmem_free(initfree->init_data);
2938 		execmem_free(initfree->init_rodata);
2939 		kfree(initfree);
2940 	}
2941 }
2942 
2943 void flush_module_init_free_work(void)
2944 {
2945 	flush_work(&init_free_wq);
2946 }
2947 
2948 #undef MODULE_PARAM_PREFIX
2949 #define MODULE_PARAM_PREFIX "module."
2950 /* Default value for module->async_probe_requested */
2951 static bool async_probe;
2952 module_param(async_probe, bool, 0644);
2953 
2954 /*
2955  * This is where the real work happens.
2956  *
2957  * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
2958  * helper command 'lx-symbols'.
2959  */
2960 static noinline int do_init_module(struct module *mod)
2961 {
2962 	int ret = 0;
2963 	struct mod_initfree *freeinit;
2964 #if defined(CONFIG_MODULE_STATS)
2965 	unsigned int text_size = 0, total_size = 0;
2966 
2967 	for_each_mod_mem_type(type) {
2968 		const struct module_memory *mod_mem = &mod->mem[type];
2969 		if (mod_mem->size) {
2970 			total_size += mod_mem->size;
2971 			if (type == MOD_TEXT || type == MOD_INIT_TEXT)
2972 				text_size += mod_mem->size;
2973 		}
2974 	}
2975 #endif
2976 
2977 	freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
2978 	if (!freeinit) {
2979 		ret = -ENOMEM;
2980 		goto fail;
2981 	}
2982 	freeinit->init_text = mod->mem[MOD_INIT_TEXT].base;
2983 	freeinit->init_data = mod->mem[MOD_INIT_DATA].base;
2984 	freeinit->init_rodata = mod->mem[MOD_INIT_RODATA].base;
2985 
2986 	do_mod_ctors(mod);
2987 	/* Start the module */
2988 	if (mod->init != NULL)
2989 		ret = do_one_initcall(mod->init);
2990 	if (ret < 0) {
2991 		goto fail_free_freeinit;
2992 	}
2993 	if (ret > 0) {
2994 		pr_warn("%s: '%s'->init suspiciously returned %d, it should "
2995 			"follow 0/-E convention\n"
2996 			"%s: loading module anyway...\n",
2997 			__func__, mod->name, ret, __func__);
2998 		dump_stack();
2999 	}
3000 
3001 	/* Now it's a first class citizen! */
3002 	mod->state = MODULE_STATE_LIVE;
3003 	blocking_notifier_call_chain(&module_notify_list,
3004 				     MODULE_STATE_LIVE, mod);
3005 
3006 	/* Delay uevent until module has finished its init routine */
3007 	kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
3008 
3009 	/*
3010 	 * We need to finish all async code before the module init sequence
3011 	 * is done. This has potential to deadlock if synchronous module
3012 	 * loading is requested from async (which is not allowed!).
3013 	 *
3014 	 * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous
3015 	 * request_module() from async workers") for more details.
3016 	 */
3017 	if (!mod->async_probe_requested)
3018 		async_synchronize_full();
3019 
3020 	ftrace_free_mem(mod, mod->mem[MOD_INIT_TEXT].base,
3021 			mod->mem[MOD_INIT_TEXT].base + mod->mem[MOD_INIT_TEXT].size);
3022 	mutex_lock(&module_mutex);
3023 	/* Drop initial reference. */
3024 	module_put(mod);
3025 	trim_init_extable(mod);
3026 #ifdef CONFIG_KALLSYMS
3027 	/* Switch to core kallsyms now init is done: kallsyms may be walking! */
3028 	rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3029 #endif
3030 	ret = module_enable_rodata_ro_after_init(mod);
3031 	if (ret)
3032 		pr_warn("%s: module_enable_rodata_ro_after_init() returned %d, "
3033 			"ro_after_init data might still be writable\n",
3034 			mod->name, ret);
3035 
3036 	mod_tree_remove_init(mod);
3037 	module_arch_freeing_init(mod);
3038 	for_class_mod_mem_type(type, init) {
3039 		mod->mem[type].base = NULL;
3040 		mod->mem[type].size = 0;
3041 	}
3042 
3043 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
3044 	/* .BTF is not SHF_ALLOC and will get removed, so sanitize pointers */
3045 	mod->btf_data = NULL;
3046 	mod->btf_base_data = NULL;
3047 #endif
3048 	/*
3049 	 * We want to free module_init, but be aware that kallsyms may be
3050 	 * walking this within an RCU read section. In all the failure paths, we
3051 	 * call synchronize_rcu(), but we don't want to slow down the success
3052 	 * path. execmem_free() cannot be called in an interrupt, so do the
3053 	 * work and call synchronize_rcu() in a work queue.
3054 	 *
3055 	 * Note that execmem_alloc() on most architectures creates W+X page
3056 	 * mappings which won't be cleaned up until do_free_init() runs.  Any
3057 	 * code such as mark_rodata_ro() which depends on those mappings to
3058 	 * be cleaned up needs to sync with the queued work by invoking
3059 	 * flush_module_init_free_work().
3060 	 */
3061 	if (llist_add(&freeinit->node, &init_free_list))
3062 		schedule_work(&init_free_wq);
3063 
3064 	mutex_unlock(&module_mutex);
3065 	wake_up_all(&module_wq);
3066 
3067 	mod_stat_add_long(text_size, &total_text_size);
3068 	mod_stat_add_long(total_size, &total_mod_size);
3069 
3070 	mod_stat_inc(&modcount);
3071 
3072 	return 0;
3073 
3074 fail_free_freeinit:
3075 	kfree(freeinit);
3076 fail:
3077 	/* Try to protect us from buggy refcounters. */
3078 	mod->state = MODULE_STATE_GOING;
3079 	synchronize_rcu();
3080 	module_put(mod);
3081 	blocking_notifier_call_chain(&module_notify_list,
3082 				     MODULE_STATE_GOING, mod);
3083 	klp_module_going(mod);
3084 	ftrace_release_mod(mod);
3085 	free_module(mod);
3086 	wake_up_all(&module_wq);
3087 
3088 	return ret;
3089 }
3090 
3091 static int may_init_module(void)
3092 {
3093 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
3094 		return -EPERM;
3095 
3096 	return 0;
3097 }
3098 
3099 /* Is this module of this name done loading?  No locks held. */
3100 static bool finished_loading(const char *name)
3101 {
3102 	struct module *mod;
3103 	bool ret;
3104 
3105 	/*
3106 	 * The module_mutex should not be a heavily contended lock;
3107 	 * if we get the occasional sleep here, we'll go an extra iteration
3108 	 * in the wait_event_interruptible(), which is harmless.
3109 	 */
3110 	sched_annotate_sleep();
3111 	mutex_lock(&module_mutex);
3112 	mod = find_module_all(name, strlen(name), true);
3113 	ret = !mod || mod->state == MODULE_STATE_LIVE
3114 		|| mod->state == MODULE_STATE_GOING;
3115 	mutex_unlock(&module_mutex);
3116 
3117 	return ret;
3118 }
3119 
3120 /* Must be called with module_mutex held */
3121 static int module_patient_check_exists(const char *name,
3122 				       enum fail_dup_mod_reason reason)
3123 {
3124 	struct module *old;
3125 	int err = 0;
3126 
3127 	old = find_module_all(name, strlen(name), true);
3128 	if (old == NULL)
3129 		return 0;
3130 
3131 	if (old->state == MODULE_STATE_COMING ||
3132 	    old->state == MODULE_STATE_UNFORMED) {
3133 		/* Wait in case it fails to load. */
3134 		mutex_unlock(&module_mutex);
3135 		err = wait_event_interruptible(module_wq,
3136 				       finished_loading(name));
3137 		mutex_lock(&module_mutex);
3138 		if (err)
3139 			return err;
3140 
3141 		/* The module might have gone in the meantime. */
3142 		old = find_module_all(name, strlen(name), true);
3143 	}
3144 
3145 	if (try_add_failed_module(name, reason))
3146 		pr_warn("Could not add fail-tracking for module: %s\n", name);
3147 
3148 	/*
3149 	 * We are here only when the same module was being loaded. Do
3150 	 * not try to load it again right now. It prevents long delays
3151 	 * caused by serialized module load failures. It might happen
3152 	 * when more devices of the same type trigger load of
3153 	 * a particular module.
3154 	 */
3155 	if (old && old->state == MODULE_STATE_LIVE)
3156 		return -EEXIST;
3157 	return -EBUSY;
3158 }
3159 
3160 /*
3161  * We try to place it in the list now to make sure it's unique before
3162  * we dedicate too many resources.  In particular, temporary percpu
3163  * memory exhaustion.
3164  */
3165 static int add_unformed_module(struct module *mod)
3166 {
3167 	int err;
3168 
3169 	mod->state = MODULE_STATE_UNFORMED;
3170 
3171 	mutex_lock(&module_mutex);
3172 	err = module_patient_check_exists(mod->name, FAIL_DUP_MOD_LOAD);
3173 	if (err)
3174 		goto out;
3175 
3176 	mod_update_bounds(mod);
3177 	list_add_rcu(&mod->list, &modules);
3178 	mod_tree_insert(mod);
3179 	err = 0;
3180 
3181 out:
3182 	mutex_unlock(&module_mutex);
3183 	return err;
3184 }
3185 
3186 static int complete_formation(struct module *mod, struct load_info *info)
3187 {
3188 	int err;
3189 
3190 	mutex_lock(&module_mutex);
3191 
3192 	/* Find duplicate symbols (must be called under lock). */
3193 	err = verify_exported_symbols(mod);
3194 	if (err < 0)
3195 		goto out;
3196 
3197 	/* These rely on module_mutex for list integrity. */
3198 	module_bug_finalize(info->hdr, info->sechdrs, mod);
3199 	module_cfi_finalize(info->hdr, info->sechdrs, mod);
3200 
3201 	err = module_enable_rodata_ro(mod);
3202 	if (err)
3203 		goto out_strict_rwx;
3204 	err = module_enable_data_nx(mod);
3205 	if (err)
3206 		goto out_strict_rwx;
3207 	err = module_enable_text_rox(mod);
3208 	if (err)
3209 		goto out_strict_rwx;
3210 
3211 	/*
3212 	 * Mark state as coming so strong_try_module_get() ignores us,
3213 	 * but kallsyms etc. can see us.
3214 	 */
3215 	mod->state = MODULE_STATE_COMING;
3216 	mutex_unlock(&module_mutex);
3217 
3218 	return 0;
3219 
3220 out_strict_rwx:
3221 	module_bug_cleanup(mod);
3222 out:
3223 	mutex_unlock(&module_mutex);
3224 	return err;
3225 }
3226 
3227 static int prepare_coming_module(struct module *mod)
3228 {
3229 	int err;
3230 
3231 	ftrace_module_enable(mod);
3232 	err = klp_module_coming(mod);
3233 	if (err)
3234 		return err;
3235 
3236 	err = blocking_notifier_call_chain_robust(&module_notify_list,
3237 			MODULE_STATE_COMING, MODULE_STATE_GOING, mod);
3238 	err = notifier_to_errno(err);
3239 	if (err)
3240 		klp_module_going(mod);
3241 
3242 	return err;
3243 }
3244 
3245 static int unknown_module_param_cb(char *param, char *val, const char *modname,
3246 				   void *arg)
3247 {
3248 	struct module *mod = arg;
3249 	int ret;
3250 
3251 	if (strcmp(param, "async_probe") == 0) {
3252 		if (kstrtobool(val, &mod->async_probe_requested))
3253 			mod->async_probe_requested = true;
3254 		return 0;
3255 	}
3256 
3257 	/* Check for magic 'dyndbg' arg */
3258 	ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3259 	if (ret != 0)
3260 		pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3261 	return 0;
3262 }
3263 
3264 /* Module within temporary copy, this doesn't do any allocation  */
3265 static int early_mod_check(struct load_info *info, int flags)
3266 {
3267 	int err;
3268 
3269 	/*
3270 	 * Now that we know we have the correct module name, check
3271 	 * if it's blacklisted.
3272 	 */
3273 	if (blacklisted(info->name)) {
3274 		pr_err("Module %s is blacklisted\n", info->name);
3275 		return -EPERM;
3276 	}
3277 
3278 	err = rewrite_section_headers(info, flags);
3279 	if (err)
3280 		return err;
3281 
3282 	/* Check module struct version now, before we try to use module. */
3283 	if (!check_modstruct_version(info, info->mod))
3284 		return -ENOEXEC;
3285 
3286 	err = check_modinfo(info->mod, info, flags);
3287 	if (err)
3288 		return err;
3289 
3290 	mutex_lock(&module_mutex);
3291 	err = module_patient_check_exists(info->mod->name, FAIL_DUP_MOD_BECOMING);
3292 	mutex_unlock(&module_mutex);
3293 
3294 	return err;
3295 }
3296 
3297 /*
3298  * Allocate and load the module: note that size of section 0 is always
3299  * zero, and we rely on this for optional sections.
3300  */
3301 static int load_module(struct load_info *info, const char __user *uargs,
3302 		       int flags)
3303 {
3304 	struct module *mod;
3305 	bool module_allocated = false;
3306 	long err = 0;
3307 	char *after_dashes;
3308 
3309 	/*
3310 	 * Do the signature check (if any) first. All that
3311 	 * the signature check needs is info->len, it does
3312 	 * not need any of the section info. That can be
3313 	 * set up later. This will minimize the chances
3314 	 * of a corrupt module causing problems before
3315 	 * we even get to the signature check.
3316 	 *
3317 	 * The check will also adjust info->len by stripping
3318 	 * off the sig length at the end of the module, making
3319 	 * checks against info->len more correct.
3320 	 */
3321 	err = module_sig_check(info, flags);
3322 	if (err)
3323 		goto free_copy;
3324 
3325 	/*
3326 	 * Do basic sanity checks against the ELF header and
3327 	 * sections. Cache useful sections and set the
3328 	 * info->mod to the userspace passed struct module.
3329 	 */
3330 	err = elf_validity_cache_copy(info, flags);
3331 	if (err)
3332 		goto free_copy;
3333 
3334 	err = early_mod_check(info, flags);
3335 	if (err)
3336 		goto free_copy;
3337 
3338 	/* Figure out module layout, and allocate all the memory. */
3339 	mod = layout_and_allocate(info, flags);
3340 	if (IS_ERR(mod)) {
3341 		err = PTR_ERR(mod);
3342 		goto free_copy;
3343 	}
3344 
3345 	module_allocated = true;
3346 
3347 	audit_log_kern_module(mod->name);
3348 
3349 	/* Reserve our place in the list. */
3350 	err = add_unformed_module(mod);
3351 	if (err)
3352 		goto free_module;
3353 
3354 	/*
3355 	 * We are tainting your kernel if your module gets into
3356 	 * the modules linked list somehow.
3357 	 */
3358 	module_augment_kernel_taints(mod, info);
3359 
3360 	/* To avoid stressing percpu allocator, do this once we're unique. */
3361 	err = percpu_modalloc(mod, info);
3362 	if (err)
3363 		goto unlink_mod;
3364 
3365 	/* Now module is in final location, initialize linked lists, etc. */
3366 	err = module_unload_init(mod);
3367 	if (err)
3368 		goto unlink_mod;
3369 
3370 	init_param_lock(mod);
3371 
3372 	/*
3373 	 * Now we've got everything in the final locations, we can
3374 	 * find optional sections.
3375 	 */
3376 	err = find_module_sections(mod, info);
3377 	if (err)
3378 		goto free_unload;
3379 
3380 	err = check_export_symbol_versions(mod);
3381 	if (err)
3382 		goto free_unload;
3383 
3384 	/* Set up MODINFO_ATTR fields */
3385 	setup_modinfo(mod, info);
3386 
3387 	/* Fix up syms, so that st_value is a pointer to location. */
3388 	err = simplify_symbols(mod, info);
3389 	if (err < 0)
3390 		goto free_modinfo;
3391 
3392 	err = apply_relocations(mod, info);
3393 	if (err < 0)
3394 		goto free_modinfo;
3395 
3396 	err = post_relocation(mod, info);
3397 	if (err < 0)
3398 		goto free_modinfo;
3399 
3400 	flush_module_icache(mod);
3401 
3402 	/* Now copy in args */
3403 	mod->args = strndup_user(uargs, ~0UL >> 1);
3404 	if (IS_ERR(mod->args)) {
3405 		err = PTR_ERR(mod->args);
3406 		goto free_arch_cleanup;
3407 	}
3408 
3409 	init_build_id(mod, info);
3410 
3411 	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3412 	ftrace_module_init(mod);
3413 
3414 	/* Finally it's fully formed, ready to start executing. */
3415 	err = complete_formation(mod, info);
3416 	if (err)
3417 		goto ddebug_cleanup;
3418 
3419 	err = prepare_coming_module(mod);
3420 	if (err)
3421 		goto bug_cleanup;
3422 
3423 	mod->async_probe_requested = async_probe;
3424 
3425 	/* Module is ready to execute: parsing args may do that. */
3426 	after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3427 				  -32768, 32767, mod,
3428 				  unknown_module_param_cb);
3429 	if (IS_ERR(after_dashes)) {
3430 		err = PTR_ERR(after_dashes);
3431 		goto coming_cleanup;
3432 	} else if (after_dashes) {
3433 		pr_warn("%s: parameters '%s' after `--' ignored\n",
3434 		       mod->name, after_dashes);
3435 	}
3436 
3437 	/* Link in to sysfs. */
3438 	err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3439 	if (err < 0)
3440 		goto coming_cleanup;
3441 
3442 	if (is_livepatch_module(mod)) {
3443 		err = copy_module_elf(mod, info);
3444 		if (err < 0)
3445 			goto sysfs_cleanup;
3446 	}
3447 
3448 	/* Get rid of temporary copy. */
3449 	free_copy(info, flags);
3450 
3451 	codetag_load_module(mod);
3452 
3453 	/* Done! */
3454 	trace_module_load(mod);
3455 
3456 	return do_init_module(mod);
3457 
3458  sysfs_cleanup:
3459 	mod_sysfs_teardown(mod);
3460  coming_cleanup:
3461 	mod->state = MODULE_STATE_GOING;
3462 	destroy_params(mod->kp, mod->num_kp);
3463 	blocking_notifier_call_chain(&module_notify_list,
3464 				     MODULE_STATE_GOING, mod);
3465 	klp_module_going(mod);
3466  bug_cleanup:
3467 	mod->state = MODULE_STATE_GOING;
3468 	/* module_bug_cleanup needs module_mutex protection */
3469 	mutex_lock(&module_mutex);
3470 	module_bug_cleanup(mod);
3471 	mutex_unlock(&module_mutex);
3472 
3473  ddebug_cleanup:
3474 	ftrace_release_mod(mod);
3475 	synchronize_rcu();
3476 	kfree(mod->args);
3477  free_arch_cleanup:
3478 	module_arch_cleanup(mod);
3479  free_modinfo:
3480 	free_modinfo(mod);
3481  free_unload:
3482 	module_unload_free(mod);
3483  unlink_mod:
3484 	mutex_lock(&module_mutex);
3485 	/* Unlink carefully: kallsyms could be walking list. */
3486 	list_del_rcu(&mod->list);
3487 	mod_tree_remove(mod);
3488 	wake_up_all(&module_wq);
3489 	/* Wait for RCU-sched synchronizing before releasing mod->list. */
3490 	synchronize_rcu();
3491 	mutex_unlock(&module_mutex);
3492  free_module:
3493 	mod_stat_bump_invalid(info, flags);
3494 	/* Free lock-classes; relies on the preceding sync_rcu() */
3495 	for_class_mod_mem_type(type, core_data) {
3496 		lockdep_free_key_range(mod->mem[type].base,
3497 				       mod->mem[type].size);
3498 	}
3499 
3500 	module_deallocate(mod, info);
3501  free_copy:
3502 	/*
3503 	 * The info->len is always set. We distinguish between
3504 	 * failures once the proper module was allocated and
3505 	 * before that.
3506 	 */
3507 	if (!module_allocated)
3508 		mod_stat_bump_becoming(info, flags);
3509 	free_copy(info, flags);
3510 	return err;
3511 }
3512 
3513 SYSCALL_DEFINE3(init_module, void __user *, umod,
3514 		unsigned long, len, const char __user *, uargs)
3515 {
3516 	int err;
3517 	struct load_info info = { };
3518 
3519 	err = may_init_module();
3520 	if (err)
3521 		return err;
3522 
3523 	pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3524 	       umod, len, uargs);
3525 
3526 	err = copy_module_from_user(umod, len, &info);
3527 	if (err) {
3528 		mod_stat_inc(&failed_kreads);
3529 		mod_stat_add_long(len, &invalid_kread_bytes);
3530 		return err;
3531 	}
3532 
3533 	return load_module(&info, uargs, 0);
3534 }
3535 
3536 struct idempotent {
3537 	const void *cookie;
3538 	struct hlist_node entry;
3539 	struct completion complete;
3540 	int ret;
3541 };
3542 
3543 #define IDEM_HASH_BITS 8
3544 static struct hlist_head idem_hash[1 << IDEM_HASH_BITS];
3545 static DEFINE_SPINLOCK(idem_lock);
3546 
3547 static bool idempotent(struct idempotent *u, const void *cookie)
3548 {
3549 	int hash = hash_ptr(cookie, IDEM_HASH_BITS);
3550 	struct hlist_head *head = idem_hash + hash;
3551 	struct idempotent *existing;
3552 	bool first;
3553 
3554 	u->ret = -EINTR;
3555 	u->cookie = cookie;
3556 	init_completion(&u->complete);
3557 
3558 	spin_lock(&idem_lock);
3559 	first = true;
3560 	hlist_for_each_entry(existing, head, entry) {
3561 		if (existing->cookie != cookie)
3562 			continue;
3563 		first = false;
3564 		break;
3565 	}
3566 	hlist_add_head(&u->entry, idem_hash + hash);
3567 	spin_unlock(&idem_lock);
3568 
3569 	return !first;
3570 }
3571 
3572 /*
3573  * We were the first one with 'cookie' on the list, and we ended
3574  * up completing the operation. We now need to walk the list,
3575  * remove everybody - which includes ourselves - fill in the return
3576  * value, and then complete the operation.
3577  */
3578 static int idempotent_complete(struct idempotent *u, int ret)
3579 {
3580 	const void *cookie = u->cookie;
3581 	int hash = hash_ptr(cookie, IDEM_HASH_BITS);
3582 	struct hlist_head *head = idem_hash + hash;
3583 	struct hlist_node *next;
3584 	struct idempotent *pos;
3585 
3586 	spin_lock(&idem_lock);
3587 	hlist_for_each_entry_safe(pos, next, head, entry) {
3588 		if (pos->cookie != cookie)
3589 			continue;
3590 		hlist_del_init(&pos->entry);
3591 		pos->ret = ret;
3592 		complete(&pos->complete);
3593 	}
3594 	spin_unlock(&idem_lock);
3595 	return ret;
3596 }
3597 
3598 /*
3599  * Wait for the idempotent worker.
3600  *
3601  * If we get interrupted, we need to remove ourselves from the
3602  * the idempotent list, and the completion may still come in.
3603  *
3604  * The 'idem_lock' protects against the race, and 'idem.ret' was
3605  * initialized to -EINTR and is thus always the right return
3606  * value even if the idempotent work then completes between
3607  * the wait_for_completion and the cleanup.
3608  */
3609 static int idempotent_wait_for_completion(struct idempotent *u)
3610 {
3611 	if (wait_for_completion_interruptible(&u->complete)) {
3612 		spin_lock(&idem_lock);
3613 		if (!hlist_unhashed(&u->entry))
3614 			hlist_del(&u->entry);
3615 		spin_unlock(&idem_lock);
3616 	}
3617 	return u->ret;
3618 }
3619 
3620 static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
3621 {
3622 	struct load_info info = { };
3623 	void *buf = NULL;
3624 	int len;
3625 
3626 	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
3627 	if (len < 0) {
3628 		mod_stat_inc(&failed_kreads);
3629 		return len;
3630 	}
3631 
3632 	if (flags & MODULE_INIT_COMPRESSED_FILE) {
3633 		int err = module_decompress(&info, buf, len);
3634 		vfree(buf); /* compressed data is no longer needed */
3635 		if (err) {
3636 			mod_stat_inc(&failed_decompress);
3637 			mod_stat_add_long(len, &invalid_decompress_bytes);
3638 			return err;
3639 		}
3640 	} else {
3641 		info.hdr = buf;
3642 		info.len = len;
3643 	}
3644 
3645 	return load_module(&info, uargs, flags);
3646 }
3647 
3648 static int idempotent_init_module(struct file *f, const char __user * uargs, int flags)
3649 {
3650 	struct idempotent idem;
3651 
3652 	if (!(f->f_mode & FMODE_READ))
3653 		return -EBADF;
3654 
3655 	/* Are we the winners of the race and get to do this? */
3656 	if (!idempotent(&idem, file_inode(f))) {
3657 		int ret = init_module_from_file(f, uargs, flags);
3658 		return idempotent_complete(&idem, ret);
3659 	}
3660 
3661 	/*
3662 	 * Somebody else won the race and is loading the module.
3663 	 */
3664 	return idempotent_wait_for_completion(&idem);
3665 }
3666 
3667 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
3668 {
3669 	int err = may_init_module();
3670 	if (err)
3671 		return err;
3672 
3673 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3674 
3675 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3676 		      |MODULE_INIT_IGNORE_VERMAGIC
3677 		      |MODULE_INIT_COMPRESSED_FILE))
3678 		return -EINVAL;
3679 
3680 	CLASS(fd, f)(fd);
3681 	if (fd_empty(f))
3682 		return -EBADF;
3683 	return idempotent_init_module(fd_file(f), uargs, flags);
3684 }
3685 
3686 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
3687 char *module_flags(struct module *mod, char *buf, bool show_state)
3688 {
3689 	int bx = 0;
3690 
3691 	BUG_ON(mod->state == MODULE_STATE_UNFORMED);
3692 	if (!mod->taints && !show_state)
3693 		goto out;
3694 	if (mod->taints ||
3695 	    mod->state == MODULE_STATE_GOING ||
3696 	    mod->state == MODULE_STATE_COMING) {
3697 		buf[bx++] = '(';
3698 		bx += module_flags_taint(mod->taints, buf + bx);
3699 		/* Show a - for module-is-being-unloaded */
3700 		if (mod->state == MODULE_STATE_GOING && show_state)
3701 			buf[bx++] = '-';
3702 		/* Show a + for module-is-being-loaded */
3703 		if (mod->state == MODULE_STATE_COMING && show_state)
3704 			buf[bx++] = '+';
3705 		buf[bx++] = ')';
3706 	}
3707 out:
3708 	buf[bx] = '\0';
3709 
3710 	return buf;
3711 }
3712 
3713 /* Given an address, look for it in the module exception tables. */
3714 const struct exception_table_entry *search_module_extables(unsigned long addr)
3715 {
3716 	const struct exception_table_entry *e = NULL;
3717 	struct module *mod;
3718 
3719 	preempt_disable();
3720 	mod = __module_address(addr);
3721 	if (!mod)
3722 		goto out;
3723 
3724 	if (!mod->num_exentries)
3725 		goto out;
3726 
3727 	e = search_extable(mod->extable,
3728 			   mod->num_exentries,
3729 			   addr);
3730 out:
3731 	preempt_enable();
3732 
3733 	/*
3734 	 * Now, if we found one, we are running inside it now, hence
3735 	 * we cannot unload the module, hence no refcnt needed.
3736 	 */
3737 	return e;
3738 }
3739 
3740 /**
3741  * is_module_address() - is this address inside a module?
3742  * @addr: the address to check.
3743  *
3744  * See is_module_text_address() if you simply want to see if the address
3745  * is code (not data).
3746  */
3747 bool is_module_address(unsigned long addr)
3748 {
3749 	bool ret;
3750 
3751 	preempt_disable();
3752 	ret = __module_address(addr) != NULL;
3753 	preempt_enable();
3754 
3755 	return ret;
3756 }
3757 
3758 /**
3759  * __module_address() - get the module which contains an address.
3760  * @addr: the address.
3761  *
3762  * Must be called with preempt disabled or module mutex held so that
3763  * module doesn't get freed during this.
3764  */
3765 struct module *__module_address(unsigned long addr)
3766 {
3767 	struct module *mod;
3768 
3769 	if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max)
3770 		goto lookup;
3771 
3772 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
3773 	if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max)
3774 		goto lookup;
3775 #endif
3776 
3777 	return NULL;
3778 
3779 lookup:
3780 	module_assert_mutex_or_preempt();
3781 
3782 	mod = mod_find(addr, &mod_tree);
3783 	if (mod) {
3784 		BUG_ON(!within_module(addr, mod));
3785 		if (mod->state == MODULE_STATE_UNFORMED)
3786 			mod = NULL;
3787 	}
3788 	return mod;
3789 }
3790 
3791 /**
3792  * is_module_text_address() - is this address inside module code?
3793  * @addr: the address to check.
3794  *
3795  * See is_module_address() if you simply want to see if the address is
3796  * anywhere in a module.  See kernel_text_address() for testing if an
3797  * address corresponds to kernel or module code.
3798  */
3799 bool is_module_text_address(unsigned long addr)
3800 {
3801 	bool ret;
3802 
3803 	preempt_disable();
3804 	ret = __module_text_address(addr) != NULL;
3805 	preempt_enable();
3806 
3807 	return ret;
3808 }
3809 
3810 /**
3811  * __module_text_address() - get the module whose code contains an address.
3812  * @addr: the address.
3813  *
3814  * Must be called with preempt disabled or module mutex held so that
3815  * module doesn't get freed during this.
3816  */
3817 struct module *__module_text_address(unsigned long addr)
3818 {
3819 	struct module *mod = __module_address(addr);
3820 	if (mod) {
3821 		/* Make sure it's within the text section. */
3822 		if (!within_module_mem_type(addr, mod, MOD_TEXT) &&
3823 		    !within_module_mem_type(addr, mod, MOD_INIT_TEXT))
3824 			mod = NULL;
3825 	}
3826 	return mod;
3827 }
3828 
3829 /* Don't grab lock, we're oopsing. */
3830 void print_modules(void)
3831 {
3832 	struct module *mod;
3833 	char buf[MODULE_FLAGS_BUF_SIZE];
3834 
3835 	printk(KERN_DEFAULT "Modules linked in:");
3836 	/* Most callers should already have preempt disabled, but make sure */
3837 	guard(rcu)();
3838 	list_for_each_entry_rcu(mod, &modules, list) {
3839 		if (mod->state == MODULE_STATE_UNFORMED)
3840 			continue;
3841 		pr_cont(" %s%s", mod->name, module_flags(mod, buf, true));
3842 	}
3843 
3844 	print_unloaded_tainted_modules();
3845 	if (last_unloaded_module.name[0])
3846 		pr_cont(" [last unloaded: %s%s]", last_unloaded_module.name,
3847 			last_unloaded_module.taints);
3848 	pr_cont("\n");
3849 }
3850 
3851 #ifdef CONFIG_MODULE_DEBUGFS
3852 struct dentry *mod_debugfs_root;
3853 
3854 static int module_debugfs_init(void)
3855 {
3856 	mod_debugfs_root = debugfs_create_dir("modules", NULL);
3857 	return 0;
3858 }
3859 module_init(module_debugfs_init);
3860 #endif
3861