• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (C) 2002 Richard Henderson
3    Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 #include <linux/export.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
24 #include <linux/file.h>
25 #include <linux/fs.h>
26 #include <linux/sysfs.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/elf.h>
31 #include <linux/proc_fs.h>
32 #include <linux/security.h>
33 #include <linux/seq_file.h>
34 #include <linux/syscalls.h>
35 #include <linux/fcntl.h>
36 #include <linux/rcupdate.h>
37 #include <linux/capability.h>
38 #include <linux/cpu.h>
39 #include <linux/moduleparam.h>
40 #include <linux/errno.h>
41 #include <linux/err.h>
42 #include <linux/vermagic.h>
43 #include <linux/notifier.h>
44 #include <linux/sched.h>
45 #include <linux/stop_machine.h>
46 #include <linux/device.h>
47 #include <linux/string.h>
48 #include <linux/mutex.h>
49 #include <linux/rculist.h>
50 #include <asm/uaccess.h>
51 #include <asm/cacheflush.h>
52 #include <asm/mmu_context.h>
53 #include <linux/license.h>
54 #include <asm/sections.h>
55 #include <linux/tracepoint.h>
56 #include <linux/ftrace.h>
57 #include <linux/async.h>
58 #include <linux/percpu.h>
59 #include <linux/kmemleak.h>
60 #include <linux/jump_label.h>
61 #include <linux/pfn.h>
62 #include <linux/bsearch.h>
63 #include <uapi/linux/module.h>
64 #include "module-internal.h"
65 
66 #define CREATE_TRACE_POINTS
67 #include <trace/events/module.h>
68 
69 #ifndef ARCH_SHF_SMALL
70 #define ARCH_SHF_SMALL 0
71 #endif
72 
73 /*
74  * Modules' sections will be aligned on page boundaries
75  * to ensure complete separation of code and data, but
76  * only when CONFIG_DEBUG_SET_MODULE_RONX=y
77  */
78 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
79 # define debug_align(X) ALIGN(X, PAGE_SIZE)
80 #else
81 # define debug_align(X) (X)
82 #endif
83 
84 /*
85  * Given BASE and SIZE this macro calculates the number of pages the
86  * memory regions occupies
87  */
88 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ?		\
89 		(PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) -	\
90 			 PFN_DOWN((unsigned long)BASE) + 1)	\
91 		: (0UL))
92 
93 /* If this is set, the section belongs in the init part of the module */
94 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
95 
96 /*
97  * Mutex protects:
98  * 1) List of modules (also safely readable with preempt_disable),
99  * 2) module_use links,
100  * 3) module_addr_min/module_addr_max.
101  * (delete uses stop_machine/add uses RCU list operations). */
102 DEFINE_MUTEX(module_mutex);
103 EXPORT_SYMBOL_GPL(module_mutex);
104 static LIST_HEAD(modules);
105 #ifdef CONFIG_KGDB_KDB
106 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
107 #endif /* CONFIG_KGDB_KDB */
108 
109 #ifdef CONFIG_MODULE_SIG
110 #ifdef CONFIG_MODULE_SIG_FORCE
111 static bool sig_enforce = true;
112 #else
113 static bool sig_enforce = false;
114 
param_set_bool_enable_only(const char * val,const struct kernel_param * kp)115 static int param_set_bool_enable_only(const char *val,
116 				      const struct kernel_param *kp)
117 {
118 	int err;
119 	bool test;
120 	struct kernel_param dummy_kp = *kp;
121 
122 	dummy_kp.arg = &test;
123 
124 	err = param_set_bool(val, &dummy_kp);
125 	if (err)
126 		return err;
127 
128 	/* Don't let them unset it once it's set! */
129 	if (!test && sig_enforce)
130 		return -EROFS;
131 
132 	if (test)
133 		sig_enforce = true;
134 	return 0;
135 }
136 
137 static const struct kernel_param_ops param_ops_bool_enable_only = {
138 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
139 	.set = param_set_bool_enable_only,
140 	.get = param_get_bool,
141 };
142 #define param_check_bool_enable_only param_check_bool
143 
144 module_param(sig_enforce, bool_enable_only, 0644);
145 #endif /* !CONFIG_MODULE_SIG_FORCE */
146 #endif /* CONFIG_MODULE_SIG */
147 
148 /* Block module loading/unloading? */
149 int modules_disabled = 0;
150 core_param(nomodule, modules_disabled, bint, 0);
151 
152 /* Waiting for a module to finish initializing? */
153 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
154 
155 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
156 
157 /* Bounds of module allocation, for speeding __module_address.
158  * Protected by module_mutex. */
159 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
160 
register_module_notifier(struct notifier_block * nb)161 int register_module_notifier(struct notifier_block * nb)
162 {
163 	return blocking_notifier_chain_register(&module_notify_list, nb);
164 }
165 EXPORT_SYMBOL(register_module_notifier);
166 
unregister_module_notifier(struct notifier_block * nb)167 int unregister_module_notifier(struct notifier_block * nb)
168 {
169 	return blocking_notifier_chain_unregister(&module_notify_list, nb);
170 }
171 EXPORT_SYMBOL(unregister_module_notifier);
172 
173 struct load_info {
174 	Elf_Ehdr *hdr;
175 	unsigned long len;
176 	Elf_Shdr *sechdrs;
177 	char *secstrings, *strtab;
178 	unsigned long symoffs, stroffs;
179 	struct _ddebug *debug;
180 	unsigned int num_debug;
181 	bool sig_ok;
182 #ifdef CONFIG_KALLSYMS
183 	unsigned long mod_kallsyms_init_off;
184 #endif
185 	struct {
186 		unsigned int sym, str, mod, vers, info, pcpu;
187 	} index;
188 };
189 
190 /* We require a truly strong try_module_get(): 0 means failure due to
191    ongoing or failed initialization etc. */
strong_try_module_get(struct module * mod)192 static inline int strong_try_module_get(struct module *mod)
193 {
194 	BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
195 	if (mod && mod->state == MODULE_STATE_COMING)
196 		return -EBUSY;
197 	if (try_module_get(mod))
198 		return 0;
199 	else
200 		return -ENOENT;
201 }
202 
add_taint_module(struct module * mod,unsigned flag,enum lockdep_ok lockdep_ok)203 static inline void add_taint_module(struct module *mod, unsigned flag,
204 				    enum lockdep_ok lockdep_ok)
205 {
206 	add_taint(flag, lockdep_ok);
207 	mod->taints |= (1U << flag);
208 }
209 
210 /*
211  * A thread that wants to hold a reference to a module only while it
212  * is running can call this to safely exit.  nfsd and lockd use this.
213  */
__module_put_and_exit(struct module * mod,long code)214 void __module_put_and_exit(struct module *mod, long code)
215 {
216 	module_put(mod);
217 	do_exit(code);
218 }
219 EXPORT_SYMBOL(__module_put_and_exit);
220 
221 /* Find a module section: 0 means not found. */
find_sec(const struct load_info * info,const char * name)222 static unsigned int find_sec(const struct load_info *info, const char *name)
223 {
224 	unsigned int i;
225 
226 	for (i = 1; i < info->hdr->e_shnum; i++) {
227 		Elf_Shdr *shdr = &info->sechdrs[i];
228 		/* Alloc bit cleared means "ignore it." */
229 		if ((shdr->sh_flags & SHF_ALLOC)
230 		    && strcmp(info->secstrings + shdr->sh_name, name) == 0)
231 			return i;
232 	}
233 	return 0;
234 }
235 
236 /* Find a module section, or NULL. */
section_addr(const struct load_info * info,const char * name)237 static void *section_addr(const struct load_info *info, const char *name)
238 {
239 	/* Section 0 has sh_addr 0. */
240 	return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
241 }
242 
243 /* Find a module section, or NULL.  Fill in number of "objects" in section. */
section_objs(const struct load_info * info,const char * name,size_t object_size,unsigned int * num)244 static void *section_objs(const struct load_info *info,
245 			  const char *name,
246 			  size_t object_size,
247 			  unsigned int *num)
248 {
249 	unsigned int sec = find_sec(info, name);
250 
251 	/* Section 0 has sh_addr 0 and sh_size 0. */
252 	*num = info->sechdrs[sec].sh_size / object_size;
253 	return (void *)info->sechdrs[sec].sh_addr;
254 }
255 
256 /* Provided by the linker */
257 extern const struct kernel_symbol __start___ksymtab[];
258 extern const struct kernel_symbol __stop___ksymtab[];
259 extern const struct kernel_symbol __start___ksymtab_gpl[];
260 extern const struct kernel_symbol __stop___ksymtab_gpl[];
261 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
262 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
263 extern const unsigned long __start___kcrctab[];
264 extern const unsigned long __start___kcrctab_gpl[];
265 extern const unsigned long __start___kcrctab_gpl_future[];
266 #ifdef CONFIG_UNUSED_SYMBOLS
267 extern const struct kernel_symbol __start___ksymtab_unused[];
268 extern const struct kernel_symbol __stop___ksymtab_unused[];
269 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
270 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
271 extern const unsigned long __start___kcrctab_unused[];
272 extern const unsigned long __start___kcrctab_unused_gpl[];
273 #endif
274 
275 #ifndef CONFIG_MODVERSIONS
276 #define symversion(base, idx) NULL
277 #else
278 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
279 #endif
280 
each_symbol_in_section(const struct symsearch * arr,unsigned int arrsize,struct module * owner,bool (* fn)(const struct symsearch * syms,struct module * owner,void * data),void * data)281 static bool each_symbol_in_section(const struct symsearch *arr,
282 				   unsigned int arrsize,
283 				   struct module *owner,
284 				   bool (*fn)(const struct symsearch *syms,
285 					      struct module *owner,
286 					      void *data),
287 				   void *data)
288 {
289 	unsigned int j;
290 
291 	for (j = 0; j < arrsize; j++) {
292 		if (fn(&arr[j], owner, data))
293 			return true;
294 	}
295 
296 	return false;
297 }
298 
299 /* Returns true as soon as fn returns true, otherwise false. */
each_symbol_section(bool (* fn)(const struct symsearch * arr,struct module * owner,void * data),void * data)300 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
301 				    struct module *owner,
302 				    void *data),
303 			 void *data)
304 {
305 	struct module *mod;
306 	static const struct symsearch arr[] = {
307 		{ __start___ksymtab, __stop___ksymtab, __start___kcrctab,
308 		  NOT_GPL_ONLY, false },
309 		{ __start___ksymtab_gpl, __stop___ksymtab_gpl,
310 		  __start___kcrctab_gpl,
311 		  GPL_ONLY, false },
312 		{ __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
313 		  __start___kcrctab_gpl_future,
314 		  WILL_BE_GPL_ONLY, false },
315 #ifdef CONFIG_UNUSED_SYMBOLS
316 		{ __start___ksymtab_unused, __stop___ksymtab_unused,
317 		  __start___kcrctab_unused,
318 		  NOT_GPL_ONLY, true },
319 		{ __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
320 		  __start___kcrctab_unused_gpl,
321 		  GPL_ONLY, true },
322 #endif
323 	};
324 
325 	if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
326 		return true;
327 
328 	list_for_each_entry_rcu(mod, &modules, list) {
329 		struct symsearch arr[] = {
330 			{ mod->syms, mod->syms + mod->num_syms, mod->crcs,
331 			  NOT_GPL_ONLY, false },
332 			{ mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
333 			  mod->gpl_crcs,
334 			  GPL_ONLY, false },
335 			{ mod->gpl_future_syms,
336 			  mod->gpl_future_syms + mod->num_gpl_future_syms,
337 			  mod->gpl_future_crcs,
338 			  WILL_BE_GPL_ONLY, false },
339 #ifdef CONFIG_UNUSED_SYMBOLS
340 			{ mod->unused_syms,
341 			  mod->unused_syms + mod->num_unused_syms,
342 			  mod->unused_crcs,
343 			  NOT_GPL_ONLY, true },
344 			{ mod->unused_gpl_syms,
345 			  mod->unused_gpl_syms + mod->num_unused_gpl_syms,
346 			  mod->unused_gpl_crcs,
347 			  GPL_ONLY, true },
348 #endif
349 		};
350 
351 		if (mod->state == MODULE_STATE_UNFORMED)
352 			continue;
353 
354 		if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
355 			return true;
356 	}
357 	return false;
358 }
359 EXPORT_SYMBOL_GPL(each_symbol_section);
360 
361 struct find_symbol_arg {
362 	/* Input */
363 	const char *name;
364 	bool gplok;
365 	bool warn;
366 
367 	/* Output */
368 	struct module *owner;
369 	const unsigned long *crc;
370 	const struct kernel_symbol *sym;
371 };
372 
check_symbol(const struct symsearch * syms,struct module * owner,unsigned int symnum,void * data)373 static bool check_symbol(const struct symsearch *syms,
374 				 struct module *owner,
375 				 unsigned int symnum, void *data)
376 {
377 	struct find_symbol_arg *fsa = data;
378 
379 	if (!fsa->gplok) {
380 		if (syms->licence == GPL_ONLY)
381 			return false;
382 		if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
383 			pr_warn("Symbol %s is being used by a non-GPL module, "
384 				"which will not be allowed in the future\n",
385 				fsa->name);
386 		}
387 	}
388 
389 #ifdef CONFIG_UNUSED_SYMBOLS
390 	if (syms->unused && fsa->warn) {
391 		pr_warn("Symbol %s is marked as UNUSED, however this module is "
392 			"using it.\n", fsa->name);
393 		pr_warn("This symbol will go away in the future.\n");
394 		pr_warn("Please evalute if this is the right api to use and if "
395 			"it really is, submit a report the linux kernel "
396 			"mailinglist together with submitting your code for "
397 			"inclusion.\n");
398 	}
399 #endif
400 
401 	fsa->owner = owner;
402 	fsa->crc = symversion(syms->crcs, symnum);
403 	fsa->sym = &syms->start[symnum];
404 	return true;
405 }
406 
cmp_name(const void * va,const void * vb)407 static int cmp_name(const void *va, const void *vb)
408 {
409 	const char *a;
410 	const struct kernel_symbol *b;
411 	a = va; b = vb;
412 	return strcmp(a, b->name);
413 }
414 
find_symbol_in_section(const struct symsearch * syms,struct module * owner,void * data)415 static bool find_symbol_in_section(const struct symsearch *syms,
416 				   struct module *owner,
417 				   void *data)
418 {
419 	struct find_symbol_arg *fsa = data;
420 	struct kernel_symbol *sym;
421 
422 	sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
423 			sizeof(struct kernel_symbol), cmp_name);
424 
425 	if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
426 		return true;
427 
428 	return false;
429 }
430 
431 /* Find a symbol and return it, along with, (optional) crc and
432  * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
find_symbol(const char * name,struct module ** owner,const unsigned long ** crc,bool gplok,bool warn)433 const struct kernel_symbol *find_symbol(const char *name,
434 					struct module **owner,
435 					const unsigned long **crc,
436 					bool gplok,
437 					bool warn)
438 {
439 	struct find_symbol_arg fsa;
440 
441 	fsa.name = name;
442 	fsa.gplok = gplok;
443 	fsa.warn = warn;
444 
445 	if (each_symbol_section(find_symbol_in_section, &fsa)) {
446 		if (owner)
447 			*owner = fsa.owner;
448 		if (crc)
449 			*crc = fsa.crc;
450 		return fsa.sym;
451 	}
452 
453 	pr_debug("Failed to find symbol %s\n", name);
454 	return NULL;
455 }
456 EXPORT_SYMBOL_GPL(find_symbol);
457 
458 /* Search for module by name: must hold module_mutex. */
find_module_all(const char * name,size_t len,bool even_unformed)459 static struct module *find_module_all(const char *name, size_t len,
460 				      bool even_unformed)
461 {
462 	struct module *mod;
463 
464 	list_for_each_entry(mod, &modules, list) {
465 		if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
466 			continue;
467 		if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
468 			return mod;
469 	}
470 	return NULL;
471 }
472 
find_module(const char * name)473 struct module *find_module(const char *name)
474 {
475 	return find_module_all(name, strlen(name), false);
476 }
477 EXPORT_SYMBOL_GPL(find_module);
478 
479 #ifdef CONFIG_SMP
480 
mod_percpu(struct module * mod)481 static inline void __percpu *mod_percpu(struct module *mod)
482 {
483 	return mod->percpu;
484 }
485 
percpu_modalloc(struct module * mod,struct load_info * info)486 static int percpu_modalloc(struct module *mod, struct load_info *info)
487 {
488 	Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
489 	unsigned long align = pcpusec->sh_addralign;
490 
491 	if (!pcpusec->sh_size)
492 		return 0;
493 
494 	if (align > PAGE_SIZE) {
495 		pr_warn("%s: per-cpu alignment %li > %li\n",
496 			mod->name, align, PAGE_SIZE);
497 		align = PAGE_SIZE;
498 	}
499 
500 	mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
501 	if (!mod->percpu) {
502 		pr_warn("%s: Could not allocate %lu bytes percpu data\n",
503 			mod->name, (unsigned long)pcpusec->sh_size);
504 		return -ENOMEM;
505 	}
506 	mod->percpu_size = pcpusec->sh_size;
507 	return 0;
508 }
509 
percpu_modfree(struct module * mod)510 static void percpu_modfree(struct module *mod)
511 {
512 	free_percpu(mod->percpu);
513 }
514 
find_pcpusec(struct load_info * info)515 static unsigned int find_pcpusec(struct load_info *info)
516 {
517 	return find_sec(info, ".data..percpu");
518 }
519 
percpu_modcopy(struct module * mod,const void * from,unsigned long size)520 static void percpu_modcopy(struct module *mod,
521 			   const void *from, unsigned long size)
522 {
523 	int cpu;
524 
525 	for_each_possible_cpu(cpu)
526 		memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
527 }
528 
529 /**
530  * is_module_percpu_address - test whether address is from module static percpu
531  * @addr: address to test
532  *
533  * Test whether @addr belongs to module static percpu area.
534  *
535  * RETURNS:
536  * %true if @addr is from module static percpu area
537  */
is_module_percpu_address(unsigned long addr)538 bool is_module_percpu_address(unsigned long addr)
539 {
540 	struct module *mod;
541 	unsigned int cpu;
542 
543 	preempt_disable();
544 
545 	list_for_each_entry_rcu(mod, &modules, list) {
546 		if (mod->state == MODULE_STATE_UNFORMED)
547 			continue;
548 		if (!mod->percpu_size)
549 			continue;
550 		for_each_possible_cpu(cpu) {
551 			void *start = per_cpu_ptr(mod->percpu, cpu);
552 
553 			if ((void *)addr >= start &&
554 			    (void *)addr < start + mod->percpu_size) {
555 				preempt_enable();
556 				return true;
557 			}
558 		}
559 	}
560 
561 	preempt_enable();
562 	return false;
563 }
564 
565 #else /* ... !CONFIG_SMP */
566 
mod_percpu(struct module * mod)567 static inline void __percpu *mod_percpu(struct module *mod)
568 {
569 	return NULL;
570 }
percpu_modalloc(struct module * mod,struct load_info * info)571 static int percpu_modalloc(struct module *mod, struct load_info *info)
572 {
573 	/* UP modules shouldn't have this section: ENOMEM isn't quite right */
574 	if (info->sechdrs[info->index.pcpu].sh_size != 0)
575 		return -ENOMEM;
576 	return 0;
577 }
percpu_modfree(struct module * mod)578 static inline void percpu_modfree(struct module *mod)
579 {
580 }
find_pcpusec(struct load_info * info)581 static unsigned int find_pcpusec(struct load_info *info)
582 {
583 	return 0;
584 }
percpu_modcopy(struct module * mod,const void * from,unsigned long size)585 static inline void percpu_modcopy(struct module *mod,
586 				  const void *from, unsigned long size)
587 {
588 	/* pcpusec should be 0, and size of that section should be 0. */
589 	BUG_ON(size != 0);
590 }
is_module_percpu_address(unsigned long addr)591 bool is_module_percpu_address(unsigned long addr)
592 {
593 	return false;
594 }
595 
596 #endif /* CONFIG_SMP */
597 
598 #define MODINFO_ATTR(field)	\
599 static void setup_modinfo_##field(struct module *mod, const char *s)  \
600 {                                                                     \
601 	mod->field = kstrdup(s, GFP_KERNEL);                          \
602 }                                                                     \
603 static ssize_t show_modinfo_##field(struct module_attribute *mattr,   \
604 			struct module_kobject *mk, char *buffer)      \
605 {                                                                     \
606 	return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field);  \
607 }                                                                     \
608 static int modinfo_##field##_exists(struct module *mod)               \
609 {                                                                     \
610 	return mod->field != NULL;                                    \
611 }                                                                     \
612 static void free_modinfo_##field(struct module *mod)                  \
613 {                                                                     \
614 	kfree(mod->field);                                            \
615 	mod->field = NULL;                                            \
616 }                                                                     \
617 static struct module_attribute modinfo_##field = {                    \
618 	.attr = { .name = __stringify(field), .mode = 0444 },         \
619 	.show = show_modinfo_##field,                                 \
620 	.setup = setup_modinfo_##field,                               \
621 	.test = modinfo_##field##_exists,                             \
622 	.free = free_modinfo_##field,                                 \
623 };
624 
625 MODINFO_ATTR(version);
626 MODINFO_ATTR(srcversion);
627 
628 static char last_unloaded_module[MODULE_NAME_LEN+1];
629 
630 #ifdef CONFIG_MODULE_UNLOAD
631 
632 EXPORT_TRACEPOINT_SYMBOL(module_get);
633 
634 /* Init the unload section of the module. */
module_unload_init(struct module * mod)635 static int module_unload_init(struct module *mod)
636 {
637 	mod->refptr = alloc_percpu(struct module_ref);
638 	if (!mod->refptr)
639 		return -ENOMEM;
640 
641 	INIT_LIST_HEAD(&mod->source_list);
642 	INIT_LIST_HEAD(&mod->target_list);
643 
644 	/* Hold reference count during initialization. */
645 	raw_cpu_write(mod->refptr->incs, 1);
646 
647 	return 0;
648 }
649 
650 /* Does a already use b? */
already_uses(struct module * a,struct module * b)651 static int already_uses(struct module *a, struct module *b)
652 {
653 	struct module_use *use;
654 
655 	list_for_each_entry(use, &b->source_list, source_list) {
656 		if (use->source == a) {
657 			pr_debug("%s uses %s!\n", a->name, b->name);
658 			return 1;
659 		}
660 	}
661 	pr_debug("%s does not use %s!\n", a->name, b->name);
662 	return 0;
663 }
664 
665 /*
666  * Module a uses b
667  *  - we add 'a' as a "source", 'b' as a "target" of module use
668  *  - the module_use is added to the list of 'b' sources (so
669  *    'b' can walk the list to see who sourced them), and of 'a'
670  *    targets (so 'a' can see what modules it targets).
671  */
add_module_usage(struct module * a,struct module * b)672 static int add_module_usage(struct module *a, struct module *b)
673 {
674 	struct module_use *use;
675 
676 	pr_debug("Allocating new usage for %s.\n", a->name);
677 	use = kmalloc(sizeof(*use), GFP_ATOMIC);
678 	if (!use) {
679 		pr_warn("%s: out of memory loading\n", a->name);
680 		return -ENOMEM;
681 	}
682 
683 	use->source = a;
684 	use->target = b;
685 	list_add(&use->source_list, &b->source_list);
686 	list_add(&use->target_list, &a->target_list);
687 	return 0;
688 }
689 
690 /* Module a uses b: caller needs module_mutex() */
ref_module(struct module * a,struct module * b)691 int ref_module(struct module *a, struct module *b)
692 {
693 	int err;
694 
695 	if (b == NULL || already_uses(a, b))
696 		return 0;
697 
698 	/* If module isn't available, we fail. */
699 	err = strong_try_module_get(b);
700 	if (err)
701 		return err;
702 
703 	err = add_module_usage(a, b);
704 	if (err) {
705 		module_put(b);
706 		return err;
707 	}
708 	return 0;
709 }
710 EXPORT_SYMBOL_GPL(ref_module);
711 
712 /* Clear the unload stuff of the module. */
module_unload_free(struct module * mod)713 static void module_unload_free(struct module *mod)
714 {
715 	struct module_use *use, *tmp;
716 
717 	mutex_lock(&module_mutex);
718 	list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
719 		struct module *i = use->target;
720 		pr_debug("%s unusing %s\n", mod->name, i->name);
721 		module_put(i);
722 		list_del(&use->source_list);
723 		list_del(&use->target_list);
724 		kfree(use);
725 	}
726 	mutex_unlock(&module_mutex);
727 
728 	free_percpu(mod->refptr);
729 }
730 
731 #ifdef CONFIG_MODULE_FORCE_UNLOAD
try_force_unload(unsigned int flags)732 static inline int try_force_unload(unsigned int flags)
733 {
734 	int ret = (flags & O_TRUNC);
735 	if (ret)
736 		add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
737 	return ret;
738 }
739 #else
try_force_unload(unsigned int flags)740 static inline int try_force_unload(unsigned int flags)
741 {
742 	return 0;
743 }
744 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
745 
746 struct stopref
747 {
748 	struct module *mod;
749 	int flags;
750 	int *forced;
751 };
752 
753 /* Whole machine is stopped with interrupts off when this runs. */
__try_stop_module(void * _sref)754 static int __try_stop_module(void *_sref)
755 {
756 	struct stopref *sref = _sref;
757 
758 	/* If it's not unused, quit unless we're forcing. */
759 	if (module_refcount(sref->mod) != 0) {
760 		if (!(*sref->forced = try_force_unload(sref->flags)))
761 			return -EWOULDBLOCK;
762 	}
763 
764 	/* Mark it as dying. */
765 	sref->mod->state = MODULE_STATE_GOING;
766 	return 0;
767 }
768 
try_stop_module(struct module * mod,int flags,int * forced)769 static int try_stop_module(struct module *mod, int flags, int *forced)
770 {
771 	struct stopref sref = { mod, flags, forced };
772 
773 	return stop_machine(__try_stop_module, &sref, NULL);
774 }
775 
module_refcount(struct module * mod)776 unsigned long module_refcount(struct module *mod)
777 {
778 	unsigned long incs = 0, decs = 0;
779 	int cpu;
780 
781 	for_each_possible_cpu(cpu)
782 		decs += per_cpu_ptr(mod->refptr, cpu)->decs;
783 	/*
784 	 * ensure the incs are added up after the decs.
785 	 * module_put ensures incs are visible before decs with smp_wmb.
786 	 *
787 	 * This 2-count scheme avoids the situation where the refcount
788 	 * for CPU0 is read, then CPU0 increments the module refcount,
789 	 * then CPU1 drops that refcount, then the refcount for CPU1 is
790 	 * read. We would record a decrement but not its corresponding
791 	 * increment so we would see a low count (disaster).
792 	 *
793 	 * Rare situation? But module_refcount can be preempted, and we
794 	 * might be tallying up 4096+ CPUs. So it is not impossible.
795 	 */
796 	smp_rmb();
797 	for_each_possible_cpu(cpu)
798 		incs += per_cpu_ptr(mod->refptr, cpu)->incs;
799 	return incs - decs;
800 }
801 EXPORT_SYMBOL(module_refcount);
802 
803 /* This exists whether we can unload or not */
804 static void free_module(struct module *mod);
805 
SYSCALL_DEFINE2(delete_module,const char __user *,name_user,unsigned int,flags)806 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
807 		unsigned int, flags)
808 {
809 	struct module *mod;
810 	char name[MODULE_NAME_LEN];
811 	int ret, forced = 0;
812 
813 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
814 		return -EPERM;
815 
816 	if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
817 		return -EFAULT;
818 	name[MODULE_NAME_LEN-1] = '\0';
819 
820 	if (mutex_lock_interruptible(&module_mutex) != 0)
821 		return -EINTR;
822 
823 	mod = find_module(name);
824 	if (!mod) {
825 		ret = -ENOENT;
826 		goto out;
827 	}
828 
829 	if (!list_empty(&mod->source_list)) {
830 		/* Other modules depend on us: get rid of them first. */
831 		ret = -EWOULDBLOCK;
832 		goto out;
833 	}
834 
835 	/* Doing init or already dying? */
836 	if (mod->state != MODULE_STATE_LIVE) {
837 		/* FIXME: if (force), slam module count damn the torpedoes */
838 		pr_debug("%s already dying\n", mod->name);
839 		ret = -EBUSY;
840 		goto out;
841 	}
842 
843 	/* If it has an init func, it must have an exit func to unload */
844 	if (mod->init && !mod->exit) {
845 		forced = try_force_unload(flags);
846 		if (!forced) {
847 			/* This module can't be removed */
848 			ret = -EBUSY;
849 			goto out;
850 		}
851 	}
852 
853 	/* Stop the machine so refcounts can't move and disable module. */
854 	ret = try_stop_module(mod, flags, &forced);
855 	if (ret != 0)
856 		goto out;
857 
858 	mutex_unlock(&module_mutex);
859 	/* Final destruction now no one is using it. */
860 	if (mod->exit != NULL)
861 		mod->exit();
862 	blocking_notifier_call_chain(&module_notify_list,
863 				     MODULE_STATE_GOING, mod);
864 	async_synchronize_full();
865 
866 	/* Store the name of the last unloaded module for diagnostic purposes */
867 	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
868 
869 	free_module(mod);
870 	return 0;
871 out:
872 	mutex_unlock(&module_mutex);
873 	return ret;
874 }
875 
print_unload_info(struct seq_file * m,struct module * mod)876 static inline void print_unload_info(struct seq_file *m, struct module *mod)
877 {
878 	struct module_use *use;
879 	int printed_something = 0;
880 
881 	seq_printf(m, " %lu ", module_refcount(mod));
882 
883 	/* Always include a trailing , so userspace can differentiate
884            between this and the old multi-field proc format. */
885 	list_for_each_entry(use, &mod->source_list, source_list) {
886 		printed_something = 1;
887 		seq_printf(m, "%s,", use->source->name);
888 	}
889 
890 	if (mod->init != NULL && mod->exit == NULL) {
891 		printed_something = 1;
892 		seq_printf(m, "[permanent],");
893 	}
894 
895 	if (!printed_something)
896 		seq_printf(m, "-");
897 }
898 
__symbol_put(const char * symbol)899 void __symbol_put(const char *symbol)
900 {
901 	struct module *owner;
902 
903 	preempt_disable();
904 	if (!find_symbol(symbol, &owner, NULL, true, false))
905 		BUG();
906 	module_put(owner);
907 	preempt_enable();
908 }
909 EXPORT_SYMBOL(__symbol_put);
910 
911 /* Note this assumes addr is a function, which it currently always is. */
symbol_put_addr(void * addr)912 void symbol_put_addr(void *addr)
913 {
914 	struct module *modaddr;
915 	unsigned long a = (unsigned long)dereference_function_descriptor(addr);
916 
917 	if (core_kernel_text(a))
918 		return;
919 
920 	/*
921 	 * Even though we hold a reference on the module; we still need to
922 	 * disable preemption in order to safely traverse the data structure.
923 	 */
924 	preempt_disable();
925 	modaddr = __module_text_address(a);
926 	BUG_ON(!modaddr);
927 	module_put(modaddr);
928 	preempt_enable();
929 }
930 EXPORT_SYMBOL_GPL(symbol_put_addr);
931 
show_refcnt(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)932 static ssize_t show_refcnt(struct module_attribute *mattr,
933 			   struct module_kobject *mk, char *buffer)
934 {
935 	return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
936 }
937 
938 static struct module_attribute modinfo_refcnt =
939 	__ATTR(refcnt, 0444, show_refcnt, NULL);
940 
__module_get(struct module * module)941 void __module_get(struct module *module)
942 {
943 	if (module) {
944 		preempt_disable();
945 		__this_cpu_inc(module->refptr->incs);
946 		trace_module_get(module, _RET_IP_);
947 		preempt_enable();
948 	}
949 }
950 EXPORT_SYMBOL(__module_get);
951 
try_module_get(struct module * module)952 bool try_module_get(struct module *module)
953 {
954 	bool ret = true;
955 
956 	if (module) {
957 		preempt_disable();
958 
959 		if (likely(module_is_live(module))) {
960 			__this_cpu_inc(module->refptr->incs);
961 			trace_module_get(module, _RET_IP_);
962 		} else
963 			ret = false;
964 
965 		preempt_enable();
966 	}
967 	return ret;
968 }
969 EXPORT_SYMBOL(try_module_get);
970 
module_put(struct module * module)971 void module_put(struct module *module)
972 {
973 	if (module) {
974 		preempt_disable();
975 		smp_wmb(); /* see comment in module_refcount */
976 		__this_cpu_inc(module->refptr->decs);
977 
978 		trace_module_put(module, _RET_IP_);
979 		preempt_enable();
980 	}
981 }
982 EXPORT_SYMBOL(module_put);
983 
984 #else /* !CONFIG_MODULE_UNLOAD */
print_unload_info(struct seq_file * m,struct module * mod)985 static inline void print_unload_info(struct seq_file *m, struct module *mod)
986 {
987 	/* We don't know the usage count, or what modules are using. */
988 	seq_printf(m, " - -");
989 }
990 
module_unload_free(struct module * mod)991 static inline void module_unload_free(struct module *mod)
992 {
993 }
994 
ref_module(struct module * a,struct module * b)995 int ref_module(struct module *a, struct module *b)
996 {
997 	return strong_try_module_get(b);
998 }
999 EXPORT_SYMBOL_GPL(ref_module);
1000 
module_unload_init(struct module * mod)1001 static inline int module_unload_init(struct module *mod)
1002 {
1003 	return 0;
1004 }
1005 #endif /* CONFIG_MODULE_UNLOAD */
1006 
module_flags_taint(struct module * mod,char * buf)1007 static size_t module_flags_taint(struct module *mod, char *buf)
1008 {
1009 	size_t l = 0;
1010 
1011 	if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1012 		buf[l++] = 'P';
1013 	if (mod->taints & (1 << TAINT_OOT_MODULE))
1014 		buf[l++] = 'O';
1015 	if (mod->taints & (1 << TAINT_FORCED_MODULE))
1016 		buf[l++] = 'F';
1017 	if (mod->taints & (1 << TAINT_CRAP))
1018 		buf[l++] = 'C';
1019 	if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
1020 		buf[l++] = 'E';
1021 	/*
1022 	 * TAINT_FORCED_RMMOD: could be added.
1023 	 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1024 	 * apply to modules.
1025 	 */
1026 	return l;
1027 }
1028 
show_initstate(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1029 static ssize_t show_initstate(struct module_attribute *mattr,
1030 			      struct module_kobject *mk, char *buffer)
1031 {
1032 	const char *state = "unknown";
1033 
1034 	switch (mk->mod->state) {
1035 	case MODULE_STATE_LIVE:
1036 		state = "live";
1037 		break;
1038 	case MODULE_STATE_COMING:
1039 		state = "coming";
1040 		break;
1041 	case MODULE_STATE_GOING:
1042 		state = "going";
1043 		break;
1044 	default:
1045 		BUG();
1046 	}
1047 	return sprintf(buffer, "%s\n", state);
1048 }
1049 
1050 static struct module_attribute modinfo_initstate =
1051 	__ATTR(initstate, 0444, show_initstate, NULL);
1052 
store_uevent(struct module_attribute * mattr,struct module_kobject * mk,const char * buffer,size_t count)1053 static ssize_t store_uevent(struct module_attribute *mattr,
1054 			    struct module_kobject *mk,
1055 			    const char *buffer, size_t count)
1056 {
1057 	enum kobject_action action;
1058 
1059 	if (kobject_action_type(buffer, count, &action) == 0)
1060 		kobject_uevent(&mk->kobj, action);
1061 	return count;
1062 }
1063 
1064 struct module_attribute module_uevent =
1065 	__ATTR(uevent, 0200, NULL, store_uevent);
1066 
show_coresize(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1067 static ssize_t show_coresize(struct module_attribute *mattr,
1068 			     struct module_kobject *mk, char *buffer)
1069 {
1070 	return sprintf(buffer, "%u\n", mk->mod->core_size);
1071 }
1072 
1073 static struct module_attribute modinfo_coresize =
1074 	__ATTR(coresize, 0444, show_coresize, NULL);
1075 
show_initsize(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1076 static ssize_t show_initsize(struct module_attribute *mattr,
1077 			     struct module_kobject *mk, char *buffer)
1078 {
1079 	return sprintf(buffer, "%u\n", mk->mod->init_size);
1080 }
1081 
1082 static struct module_attribute modinfo_initsize =
1083 	__ATTR(initsize, 0444, show_initsize, NULL);
1084 
show_taint(struct module_attribute * mattr,struct module_kobject * mk,char * buffer)1085 static ssize_t show_taint(struct module_attribute *mattr,
1086 			  struct module_kobject *mk, char *buffer)
1087 {
1088 	size_t l;
1089 
1090 	l = module_flags_taint(mk->mod, buffer);
1091 	buffer[l++] = '\n';
1092 	return l;
1093 }
1094 
1095 static struct module_attribute modinfo_taint =
1096 	__ATTR(taint, 0444, show_taint, NULL);
1097 
1098 static struct module_attribute *modinfo_attrs[] = {
1099 	&module_uevent,
1100 	&modinfo_version,
1101 	&modinfo_srcversion,
1102 	&modinfo_initstate,
1103 	&modinfo_coresize,
1104 	&modinfo_initsize,
1105 	&modinfo_taint,
1106 #ifdef CONFIG_MODULE_UNLOAD
1107 	&modinfo_refcnt,
1108 #endif
1109 	NULL,
1110 };
1111 
1112 static const char vermagic[] = VERMAGIC_STRING;
1113 
try_to_force_load(struct module * mod,const char * reason)1114 static int try_to_force_load(struct module *mod, const char *reason)
1115 {
1116 #ifdef CONFIG_MODULE_FORCE_LOAD
1117 	if (!test_taint(TAINT_FORCED_MODULE))
1118 		pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1119 	add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1120 	return 0;
1121 #else
1122 	return -ENOEXEC;
1123 #endif
1124 }
1125 
1126 #ifdef CONFIG_MODVERSIONS
1127 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
maybe_relocated(unsigned long crc,const struct module * crc_owner)1128 static unsigned long maybe_relocated(unsigned long crc,
1129 				     const struct module *crc_owner)
1130 {
1131 #ifdef ARCH_RELOCATES_KCRCTAB
1132 	if (crc_owner == NULL)
1133 		return crc - (unsigned long)reloc_start;
1134 #endif
1135 	return crc;
1136 }
1137 
check_version(Elf_Shdr * sechdrs,unsigned int versindex,const char * symname,struct module * mod,const unsigned long * crc,const struct module * crc_owner)1138 static int check_version(Elf_Shdr *sechdrs,
1139 			 unsigned int versindex,
1140 			 const char *symname,
1141 			 struct module *mod,
1142 			 const unsigned long *crc,
1143 			 const struct module *crc_owner)
1144 {
1145 	unsigned int i, num_versions;
1146 	struct modversion_info *versions;
1147 
1148 	/* Exporting module didn't supply crcs?  OK, we're already tainted. */
1149 	if (!crc)
1150 		return 1;
1151 
1152 	/* No versions at all?  modprobe --force does this. */
1153 	if (versindex == 0)
1154 		return try_to_force_load(mod, symname) == 0;
1155 
1156 	versions = (void *) sechdrs[versindex].sh_addr;
1157 	num_versions = sechdrs[versindex].sh_size
1158 		/ sizeof(struct modversion_info);
1159 
1160 	for (i = 0; i < num_versions; i++) {
1161 		if (strcmp(versions[i].name, symname) != 0)
1162 			continue;
1163 
1164 		if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1165 			return 1;
1166 		pr_debug("Found checksum %lX vs module %lX\n",
1167 		       maybe_relocated(*crc, crc_owner), versions[i].crc);
1168 		goto bad_version;
1169 	}
1170 
1171 	pr_warn("%s: no symbol version for %s\n", mod->name, symname);
1172 	return 0;
1173 
1174 bad_version:
1175 	printk("%s: disagrees about version of symbol %s\n",
1176 	       mod->name, symname);
1177 	return 0;
1178 }
1179 
check_modstruct_version(Elf_Shdr * sechdrs,unsigned int versindex,struct module * mod)1180 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1181 					  unsigned int versindex,
1182 					  struct module *mod)
1183 {
1184 	const unsigned long *crc;
1185 
1186 	/* Since this should be found in kernel (which can't be removed),
1187 	 * no locking is necessary. */
1188 	if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
1189 			 &crc, true, false))
1190 		BUG();
1191 	return check_version(sechdrs, versindex,
1192 			     VMLINUX_SYMBOL_STR(module_layout), mod, crc,
1193 			     NULL);
1194 }
1195 
1196 /* First part is kernel version, which we ignore if module has crcs. */
same_magic(const char * amagic,const char * bmagic,bool has_crcs)1197 static inline int same_magic(const char *amagic, const char *bmagic,
1198 			     bool has_crcs)
1199 {
1200 	if (has_crcs) {
1201 		amagic += strcspn(amagic, " ");
1202 		bmagic += strcspn(bmagic, " ");
1203 	}
1204 	return strcmp(amagic, bmagic) == 0;
1205 }
1206 #else
check_version(Elf_Shdr * sechdrs,unsigned int versindex,const char * symname,struct module * mod,const unsigned long * crc,const struct module * crc_owner)1207 static inline int check_version(Elf_Shdr *sechdrs,
1208 				unsigned int versindex,
1209 				const char *symname,
1210 				struct module *mod,
1211 				const unsigned long *crc,
1212 				const struct module *crc_owner)
1213 {
1214 	return 1;
1215 }
1216 
check_modstruct_version(Elf_Shdr * sechdrs,unsigned int versindex,struct module * mod)1217 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1218 					  unsigned int versindex,
1219 					  struct module *mod)
1220 {
1221 	return 1;
1222 }
1223 
same_magic(const char * amagic,const char * bmagic,bool has_crcs)1224 static inline int same_magic(const char *amagic, const char *bmagic,
1225 			     bool has_crcs)
1226 {
1227 	return strcmp(amagic, bmagic) == 0;
1228 }
1229 #endif /* CONFIG_MODVERSIONS */
1230 
1231 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
resolve_symbol(struct module * mod,const struct load_info * info,const char * name,char ownername[])1232 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1233 						  const struct load_info *info,
1234 						  const char *name,
1235 						  char ownername[])
1236 {
1237 	struct module *owner;
1238 	const struct kernel_symbol *sym;
1239 	const unsigned long *crc;
1240 	int err;
1241 
1242 	mutex_lock(&module_mutex);
1243 	sym = find_symbol(name, &owner, &crc,
1244 			  !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1245 	if (!sym)
1246 		goto unlock;
1247 
1248 	if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1249 			   owner)) {
1250 		sym = ERR_PTR(-EINVAL);
1251 		goto getname;
1252 	}
1253 
1254 	err = ref_module(mod, owner);
1255 	if (err) {
1256 		sym = ERR_PTR(err);
1257 		goto getname;
1258 	}
1259 
1260 getname:
1261 	/* We must make copy under the lock if we failed to get ref. */
1262 	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1263 unlock:
1264 	mutex_unlock(&module_mutex);
1265 	return sym;
1266 }
1267 
1268 static const struct kernel_symbol *
resolve_symbol_wait(struct module * mod,const struct load_info * info,const char * name)1269 resolve_symbol_wait(struct module *mod,
1270 		    const struct load_info *info,
1271 		    const char *name)
1272 {
1273 	const struct kernel_symbol *ksym;
1274 	char owner[MODULE_NAME_LEN];
1275 
1276 	if (wait_event_interruptible_timeout(module_wq,
1277 			!IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1278 			|| PTR_ERR(ksym) != -EBUSY,
1279 					     30 * HZ) <= 0) {
1280 		pr_warn("%s: gave up waiting for init of module %s.\n",
1281 			mod->name, owner);
1282 	}
1283 	return ksym;
1284 }
1285 
1286 /*
1287  * /sys/module/foo/sections stuff
1288  * J. Corbet <corbet@lwn.net>
1289  */
1290 #ifdef CONFIG_SYSFS
1291 
1292 #ifdef CONFIG_KALLSYMS
sect_empty(const Elf_Shdr * sect)1293 static inline bool sect_empty(const Elf_Shdr *sect)
1294 {
1295 	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1296 }
1297 
1298 struct module_sect_attr
1299 {
1300 	struct module_attribute mattr;
1301 	char *name;
1302 	unsigned long address;
1303 };
1304 
1305 struct module_sect_attrs
1306 {
1307 	struct attribute_group grp;
1308 	unsigned int nsections;
1309 	struct module_sect_attr attrs[0];
1310 };
1311 
module_sect_show(struct module_attribute * mattr,struct module_kobject * mk,char * buf)1312 static ssize_t module_sect_show(struct module_attribute *mattr,
1313 				struct module_kobject *mk, char *buf)
1314 {
1315 	struct module_sect_attr *sattr =
1316 		container_of(mattr, struct module_sect_attr, mattr);
1317 	return sprintf(buf, "0x%pK\n", (void *)sattr->address);
1318 }
1319 
free_sect_attrs(struct module_sect_attrs * sect_attrs)1320 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1321 {
1322 	unsigned int section;
1323 
1324 	for (section = 0; section < sect_attrs->nsections; section++)
1325 		kfree(sect_attrs->attrs[section].name);
1326 	kfree(sect_attrs);
1327 }
1328 
add_sect_attrs(struct module * mod,const struct load_info * info)1329 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1330 {
1331 	unsigned int nloaded = 0, i, size[2];
1332 	struct module_sect_attrs *sect_attrs;
1333 	struct module_sect_attr *sattr;
1334 	struct attribute **gattr;
1335 
1336 	/* Count loaded sections and allocate structures */
1337 	for (i = 0; i < info->hdr->e_shnum; i++)
1338 		if (!sect_empty(&info->sechdrs[i]))
1339 			nloaded++;
1340 	size[0] = ALIGN(sizeof(*sect_attrs)
1341 			+ nloaded * sizeof(sect_attrs->attrs[0]),
1342 			sizeof(sect_attrs->grp.attrs[0]));
1343 	size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1344 	sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1345 	if (sect_attrs == NULL)
1346 		return;
1347 
1348 	/* Setup section attributes. */
1349 	sect_attrs->grp.name = "sections";
1350 	sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1351 
1352 	sect_attrs->nsections = 0;
1353 	sattr = &sect_attrs->attrs[0];
1354 	gattr = &sect_attrs->grp.attrs[0];
1355 	for (i = 0; i < info->hdr->e_shnum; i++) {
1356 		Elf_Shdr *sec = &info->sechdrs[i];
1357 		if (sect_empty(sec))
1358 			continue;
1359 		sattr->address = sec->sh_addr;
1360 		sattr->name = kstrdup(info->secstrings + sec->sh_name,
1361 					GFP_KERNEL);
1362 		if (sattr->name == NULL)
1363 			goto out;
1364 		sect_attrs->nsections++;
1365 		sysfs_attr_init(&sattr->mattr.attr);
1366 		sattr->mattr.show = module_sect_show;
1367 		sattr->mattr.store = NULL;
1368 		sattr->mattr.attr.name = sattr->name;
1369 		sattr->mattr.attr.mode = S_IRUGO;
1370 		*(gattr++) = &(sattr++)->mattr.attr;
1371 	}
1372 	*gattr = NULL;
1373 
1374 	if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1375 		goto out;
1376 
1377 	mod->sect_attrs = sect_attrs;
1378 	return;
1379   out:
1380 	free_sect_attrs(sect_attrs);
1381 }
1382 
remove_sect_attrs(struct module * mod)1383 static void remove_sect_attrs(struct module *mod)
1384 {
1385 	if (mod->sect_attrs) {
1386 		sysfs_remove_group(&mod->mkobj.kobj,
1387 				   &mod->sect_attrs->grp);
1388 		/* We are positive that no one is using any sect attrs
1389 		 * at this point.  Deallocate immediately. */
1390 		free_sect_attrs(mod->sect_attrs);
1391 		mod->sect_attrs = NULL;
1392 	}
1393 }
1394 
1395 /*
1396  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1397  */
1398 
1399 struct module_notes_attrs {
1400 	struct kobject *dir;
1401 	unsigned int notes;
1402 	struct bin_attribute attrs[0];
1403 };
1404 
module_notes_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)1405 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1406 				 struct bin_attribute *bin_attr,
1407 				 char *buf, loff_t pos, size_t count)
1408 {
1409 	/*
1410 	 * The caller checked the pos and count against our size.
1411 	 */
1412 	memcpy(buf, bin_attr->private + pos, count);
1413 	return count;
1414 }
1415 
free_notes_attrs(struct module_notes_attrs * notes_attrs,unsigned int i)1416 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1417 			     unsigned int i)
1418 {
1419 	if (notes_attrs->dir) {
1420 		while (i-- > 0)
1421 			sysfs_remove_bin_file(notes_attrs->dir,
1422 					      &notes_attrs->attrs[i]);
1423 		kobject_put(notes_attrs->dir);
1424 	}
1425 	kfree(notes_attrs);
1426 }
1427 
add_notes_attrs(struct module * mod,const struct load_info * info)1428 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1429 {
1430 	unsigned int notes, loaded, i;
1431 	struct module_notes_attrs *notes_attrs;
1432 	struct bin_attribute *nattr;
1433 
1434 	/* failed to create section attributes, so can't create notes */
1435 	if (!mod->sect_attrs)
1436 		return;
1437 
1438 	/* Count notes sections and allocate structures.  */
1439 	notes = 0;
1440 	for (i = 0; i < info->hdr->e_shnum; i++)
1441 		if (!sect_empty(&info->sechdrs[i]) &&
1442 		    (info->sechdrs[i].sh_type == SHT_NOTE))
1443 			++notes;
1444 
1445 	if (notes == 0)
1446 		return;
1447 
1448 	notes_attrs = kzalloc(sizeof(*notes_attrs)
1449 			      + notes * sizeof(notes_attrs->attrs[0]),
1450 			      GFP_KERNEL);
1451 	if (notes_attrs == NULL)
1452 		return;
1453 
1454 	notes_attrs->notes = notes;
1455 	nattr = &notes_attrs->attrs[0];
1456 	for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1457 		if (sect_empty(&info->sechdrs[i]))
1458 			continue;
1459 		if (info->sechdrs[i].sh_type == SHT_NOTE) {
1460 			sysfs_bin_attr_init(nattr);
1461 			nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1462 			nattr->attr.mode = S_IRUGO;
1463 			nattr->size = info->sechdrs[i].sh_size;
1464 			nattr->private = (void *) info->sechdrs[i].sh_addr;
1465 			nattr->read = module_notes_read;
1466 			++nattr;
1467 		}
1468 		++loaded;
1469 	}
1470 
1471 	notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1472 	if (!notes_attrs->dir)
1473 		goto out;
1474 
1475 	for (i = 0; i < notes; ++i)
1476 		if (sysfs_create_bin_file(notes_attrs->dir,
1477 					  &notes_attrs->attrs[i]))
1478 			goto out;
1479 
1480 	mod->notes_attrs = notes_attrs;
1481 	return;
1482 
1483   out:
1484 	free_notes_attrs(notes_attrs, i);
1485 }
1486 
remove_notes_attrs(struct module * mod)1487 static void remove_notes_attrs(struct module *mod)
1488 {
1489 	if (mod->notes_attrs)
1490 		free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1491 }
1492 
1493 #else
1494 
add_sect_attrs(struct module * mod,const struct load_info * info)1495 static inline void add_sect_attrs(struct module *mod,
1496 				  const struct load_info *info)
1497 {
1498 }
1499 
remove_sect_attrs(struct module * mod)1500 static inline void remove_sect_attrs(struct module *mod)
1501 {
1502 }
1503 
add_notes_attrs(struct module * mod,const struct load_info * info)1504 static inline void add_notes_attrs(struct module *mod,
1505 				   const struct load_info *info)
1506 {
1507 }
1508 
remove_notes_attrs(struct module * mod)1509 static inline void remove_notes_attrs(struct module *mod)
1510 {
1511 }
1512 #endif /* CONFIG_KALLSYMS */
1513 
add_usage_links(struct module * mod)1514 static void add_usage_links(struct module *mod)
1515 {
1516 #ifdef CONFIG_MODULE_UNLOAD
1517 	struct module_use *use;
1518 	int nowarn;
1519 
1520 	mutex_lock(&module_mutex);
1521 	list_for_each_entry(use, &mod->target_list, target_list) {
1522 		nowarn = sysfs_create_link(use->target->holders_dir,
1523 					   &mod->mkobj.kobj, mod->name);
1524 	}
1525 	mutex_unlock(&module_mutex);
1526 #endif
1527 }
1528 
del_usage_links(struct module * mod)1529 static void del_usage_links(struct module *mod)
1530 {
1531 #ifdef CONFIG_MODULE_UNLOAD
1532 	struct module_use *use;
1533 
1534 	mutex_lock(&module_mutex);
1535 	list_for_each_entry(use, &mod->target_list, target_list)
1536 		sysfs_remove_link(use->target->holders_dir, mod->name);
1537 	mutex_unlock(&module_mutex);
1538 #endif
1539 }
1540 
module_add_modinfo_attrs(struct module * mod)1541 static int module_add_modinfo_attrs(struct module *mod)
1542 {
1543 	struct module_attribute *attr;
1544 	struct module_attribute *temp_attr;
1545 	int error = 0;
1546 	int i;
1547 
1548 	mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1549 					(ARRAY_SIZE(modinfo_attrs) + 1)),
1550 					GFP_KERNEL);
1551 	if (!mod->modinfo_attrs)
1552 		return -ENOMEM;
1553 
1554 	temp_attr = mod->modinfo_attrs;
1555 	for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1556 		if (!attr->test ||
1557 		    (attr->test && attr->test(mod))) {
1558 			memcpy(temp_attr, attr, sizeof(*temp_attr));
1559 			sysfs_attr_init(&temp_attr->attr);
1560 			error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1561 			++temp_attr;
1562 		}
1563 	}
1564 	return error;
1565 }
1566 
module_remove_modinfo_attrs(struct module * mod)1567 static void module_remove_modinfo_attrs(struct module *mod)
1568 {
1569 	struct module_attribute *attr;
1570 	int i;
1571 
1572 	for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1573 		/* pick a field to test for end of list */
1574 		if (!attr->attr.name)
1575 			break;
1576 		sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1577 		if (attr->free)
1578 			attr->free(mod);
1579 	}
1580 	kfree(mod->modinfo_attrs);
1581 }
1582 
mod_kobject_put(struct module * mod)1583 static void mod_kobject_put(struct module *mod)
1584 {
1585 	DECLARE_COMPLETION_ONSTACK(c);
1586 	mod->mkobj.kobj_completion = &c;
1587 	kobject_put(&mod->mkobj.kobj);
1588 	wait_for_completion(&c);
1589 }
1590 
mod_sysfs_init(struct module * mod)1591 static int mod_sysfs_init(struct module *mod)
1592 {
1593 	int err;
1594 	struct kobject *kobj;
1595 
1596 	if (!module_sysfs_initialized) {
1597 		pr_err("%s: module sysfs not initialized\n", mod->name);
1598 		err = -EINVAL;
1599 		goto out;
1600 	}
1601 
1602 	kobj = kset_find_obj(module_kset, mod->name);
1603 	if (kobj) {
1604 		pr_err("%s: module is already loaded\n", mod->name);
1605 		kobject_put(kobj);
1606 		err = -EINVAL;
1607 		goto out;
1608 	}
1609 
1610 	mod->mkobj.mod = mod;
1611 
1612 	memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1613 	mod->mkobj.kobj.kset = module_kset;
1614 	err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1615 				   "%s", mod->name);
1616 	if (err)
1617 		mod_kobject_put(mod);
1618 
1619 	/* delay uevent until full sysfs population */
1620 out:
1621 	return err;
1622 }
1623 
mod_sysfs_setup(struct module * mod,const struct load_info * info,struct kernel_param * kparam,unsigned int num_params)1624 static int mod_sysfs_setup(struct module *mod,
1625 			   const struct load_info *info,
1626 			   struct kernel_param *kparam,
1627 			   unsigned int num_params)
1628 {
1629 	int err;
1630 
1631 	err = mod_sysfs_init(mod);
1632 	if (err)
1633 		goto out;
1634 
1635 	mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1636 	if (!mod->holders_dir) {
1637 		err = -ENOMEM;
1638 		goto out_unreg;
1639 	}
1640 
1641 	err = module_param_sysfs_setup(mod, kparam, num_params);
1642 	if (err)
1643 		goto out_unreg_holders;
1644 
1645 	err = module_add_modinfo_attrs(mod);
1646 	if (err)
1647 		goto out_unreg_param;
1648 
1649 	add_usage_links(mod);
1650 	add_sect_attrs(mod, info);
1651 	add_notes_attrs(mod, info);
1652 
1653 	kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1654 	return 0;
1655 
1656 out_unreg_param:
1657 	module_param_sysfs_remove(mod);
1658 out_unreg_holders:
1659 	kobject_put(mod->holders_dir);
1660 out_unreg:
1661 	mod_kobject_put(mod);
1662 out:
1663 	return err;
1664 }
1665 
mod_sysfs_fini(struct module * mod)1666 static void mod_sysfs_fini(struct module *mod)
1667 {
1668 	remove_notes_attrs(mod);
1669 	remove_sect_attrs(mod);
1670 	mod_kobject_put(mod);
1671 }
1672 
1673 #else /* !CONFIG_SYSFS */
1674 
mod_sysfs_setup(struct module * mod,const struct load_info * info,struct kernel_param * kparam,unsigned int num_params)1675 static int mod_sysfs_setup(struct module *mod,
1676 			   const struct load_info *info,
1677 			   struct kernel_param *kparam,
1678 			   unsigned int num_params)
1679 {
1680 	return 0;
1681 }
1682 
mod_sysfs_fini(struct module * mod)1683 static void mod_sysfs_fini(struct module *mod)
1684 {
1685 }
1686 
module_remove_modinfo_attrs(struct module * mod)1687 static void module_remove_modinfo_attrs(struct module *mod)
1688 {
1689 }
1690 
del_usage_links(struct module * mod)1691 static void del_usage_links(struct module *mod)
1692 {
1693 }
1694 
1695 #endif /* CONFIG_SYSFS */
1696 
mod_sysfs_teardown(struct module * mod)1697 static void mod_sysfs_teardown(struct module *mod)
1698 {
1699 	del_usage_links(mod);
1700 	module_remove_modinfo_attrs(mod);
1701 	module_param_sysfs_remove(mod);
1702 	kobject_put(mod->mkobj.drivers_dir);
1703 	kobject_put(mod->holders_dir);
1704 	mod_sysfs_fini(mod);
1705 }
1706 
1707 /*
1708  * unlink the module with the whole machine is stopped with interrupts off
1709  * - this defends against kallsyms not taking locks
1710  */
__unlink_module(void * _mod)1711 static int __unlink_module(void *_mod)
1712 {
1713 	struct module *mod = _mod;
1714 	list_del(&mod->list);
1715 	module_bug_cleanup(mod);
1716 	return 0;
1717 }
1718 
1719 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1720 /*
1721  * LKM RO/NX protection: protect module's text/ro-data
1722  * from modification and any data from execution.
1723  */
set_page_attributes(void * start,void * end,int (* set)(unsigned long start,int num_pages))1724 void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1725 {
1726 	unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1727 	unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1728 
1729 	if (end_pfn > begin_pfn)
1730 		set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1731 }
1732 
set_section_ro_nx(void * base,unsigned long text_size,unsigned long ro_size,unsigned long total_size)1733 static void set_section_ro_nx(void *base,
1734 			unsigned long text_size,
1735 			unsigned long ro_size,
1736 			unsigned long total_size)
1737 {
1738 	/* begin and end PFNs of the current subsection */
1739 	unsigned long begin_pfn;
1740 	unsigned long end_pfn;
1741 
1742 	/*
1743 	 * Set RO for module text and RO-data:
1744 	 * - Always protect first page.
1745 	 * - Do not protect last partial page.
1746 	 */
1747 	if (ro_size > 0)
1748 		set_page_attributes(base, base + ro_size, set_memory_ro);
1749 
1750 	/*
1751 	 * Set NX permissions for module data:
1752 	 * - Do not protect first partial page.
1753 	 * - Always protect last page.
1754 	 */
1755 	if (total_size > text_size) {
1756 		begin_pfn = PFN_UP((unsigned long)base + text_size);
1757 		end_pfn = PFN_UP((unsigned long)base + total_size);
1758 		if (end_pfn > begin_pfn)
1759 			set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1760 	}
1761 }
1762 
unset_module_core_ro_nx(struct module * mod)1763 static void unset_module_core_ro_nx(struct module *mod)
1764 {
1765 	set_page_attributes(mod->module_core + mod->core_text_size,
1766 		mod->module_core + mod->core_size,
1767 		set_memory_x);
1768 	set_page_attributes(mod->module_core,
1769 		mod->module_core + mod->core_ro_size,
1770 		set_memory_rw);
1771 }
1772 
unset_module_init_ro_nx(struct module * mod)1773 static void unset_module_init_ro_nx(struct module *mod)
1774 {
1775 	set_page_attributes(mod->module_init + mod->init_text_size,
1776 		mod->module_init + mod->init_size,
1777 		set_memory_x);
1778 	set_page_attributes(mod->module_init,
1779 		mod->module_init + mod->init_ro_size,
1780 		set_memory_rw);
1781 }
1782 
1783 /* Iterate through all modules and set each module's text as RW */
set_all_modules_text_rw(void)1784 void set_all_modules_text_rw(void)
1785 {
1786 	struct module *mod;
1787 
1788 	mutex_lock(&module_mutex);
1789 	list_for_each_entry_rcu(mod, &modules, list) {
1790 		if (mod->state == MODULE_STATE_UNFORMED)
1791 			continue;
1792 		if ((mod->module_core) && (mod->core_text_size)) {
1793 			set_page_attributes(mod->module_core,
1794 						mod->module_core + mod->core_text_size,
1795 						set_memory_rw);
1796 		}
1797 		if ((mod->module_init) && (mod->init_text_size)) {
1798 			set_page_attributes(mod->module_init,
1799 						mod->module_init + mod->init_text_size,
1800 						set_memory_rw);
1801 		}
1802 	}
1803 	mutex_unlock(&module_mutex);
1804 }
1805 
1806 /* Iterate through all modules and set each module's text as RO */
set_all_modules_text_ro(void)1807 void set_all_modules_text_ro(void)
1808 {
1809 	struct module *mod;
1810 
1811 	mutex_lock(&module_mutex);
1812 	list_for_each_entry_rcu(mod, &modules, list) {
1813 		if (mod->state == MODULE_STATE_UNFORMED)
1814 			continue;
1815 		if ((mod->module_core) && (mod->core_text_size)) {
1816 			set_page_attributes(mod->module_core,
1817 						mod->module_core + mod->core_text_size,
1818 						set_memory_ro);
1819 		}
1820 		if ((mod->module_init) && (mod->init_text_size)) {
1821 			set_page_attributes(mod->module_init,
1822 						mod->module_init + mod->init_text_size,
1823 						set_memory_ro);
1824 		}
1825 	}
1826 	mutex_unlock(&module_mutex);
1827 }
1828 #else
set_section_ro_nx(void * base,unsigned long text_size,unsigned long ro_size,unsigned long total_size)1829 static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
unset_module_core_ro_nx(struct module * mod)1830 static void unset_module_core_ro_nx(struct module *mod) { }
unset_module_init_ro_nx(struct module * mod)1831 static void unset_module_init_ro_nx(struct module *mod) { }
1832 #endif
1833 
module_free(struct module * mod,void * module_region)1834 void __weak module_free(struct module *mod, void *module_region)
1835 {
1836 	vfree(module_region);
1837 }
1838 
module_arch_cleanup(struct module * mod)1839 void __weak module_arch_cleanup(struct module *mod)
1840 {
1841 }
1842 
1843 /* Free a module, remove from lists, etc. */
free_module(struct module * mod)1844 static void free_module(struct module *mod)
1845 {
1846 	trace_module_free(mod);
1847 
1848 	mod_sysfs_teardown(mod);
1849 
1850 	/* We leave it in list to prevent duplicate loads, but make sure
1851 	 * that noone uses it while it's being deconstructed. */
1852 	mutex_lock(&module_mutex);
1853 	mod->state = MODULE_STATE_UNFORMED;
1854 	mutex_unlock(&module_mutex);
1855 
1856 	/* Remove dynamic debug info */
1857 	ddebug_remove_module(mod->name);
1858 
1859 	/* Arch-specific cleanup. */
1860 	module_arch_cleanup(mod);
1861 
1862 	/* Module unload stuff */
1863 	module_unload_free(mod);
1864 
1865 	/* Free any allocated parameters. */
1866 	destroy_params(mod->kp, mod->num_kp);
1867 
1868 	/* Now we can delete it from the lists */
1869 	mutex_lock(&module_mutex);
1870 	stop_machine(__unlink_module, mod, NULL);
1871 	mutex_unlock(&module_mutex);
1872 
1873 	/* This may be NULL, but that's OK */
1874 	unset_module_init_ro_nx(mod);
1875 	module_free(mod, mod->module_init);
1876 	kfree(mod->args);
1877 	percpu_modfree(mod);
1878 
1879 	/* Free lock-classes: */
1880 	lockdep_free_key_range(mod->module_core, mod->core_size);
1881 
1882 	/* Finally, free the core (containing the module structure) */
1883 	unset_module_core_ro_nx(mod);
1884 	module_free(mod, mod->module_core);
1885 
1886 #ifdef CONFIG_MPU
1887 	update_protections(current->mm);
1888 #endif
1889 }
1890 
__symbol_get(const char * symbol)1891 void *__symbol_get(const char *symbol)
1892 {
1893 	struct module *owner;
1894 	const struct kernel_symbol *sym;
1895 
1896 	preempt_disable();
1897 	sym = find_symbol(symbol, &owner, NULL, true, true);
1898 	if (sym && strong_try_module_get(owner))
1899 		sym = NULL;
1900 	preempt_enable();
1901 
1902 	return sym ? (void *)sym->value : NULL;
1903 }
1904 EXPORT_SYMBOL_GPL(__symbol_get);
1905 
1906 /*
1907  * Ensure that an exported symbol [global namespace] does not already exist
1908  * in the kernel or in some other module's exported symbol table.
1909  *
1910  * You must hold the module_mutex.
1911  */
verify_export_symbols(struct module * mod)1912 static int verify_export_symbols(struct module *mod)
1913 {
1914 	unsigned int i;
1915 	struct module *owner;
1916 	const struct kernel_symbol *s;
1917 	struct {
1918 		const struct kernel_symbol *sym;
1919 		unsigned int num;
1920 	} arr[] = {
1921 		{ mod->syms, mod->num_syms },
1922 		{ mod->gpl_syms, mod->num_gpl_syms },
1923 		{ mod->gpl_future_syms, mod->num_gpl_future_syms },
1924 #ifdef CONFIG_UNUSED_SYMBOLS
1925 		{ mod->unused_syms, mod->num_unused_syms },
1926 		{ mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1927 #endif
1928 	};
1929 
1930 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
1931 		for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1932 			if (find_symbol(s->name, &owner, NULL, true, false)) {
1933 				pr_err("%s: exports duplicate symbol %s"
1934 				       " (owned by %s)\n",
1935 				       mod->name, s->name, module_name(owner));
1936 				return -ENOEXEC;
1937 			}
1938 		}
1939 	}
1940 	return 0;
1941 }
1942 
1943 /* Change all symbols so that st_value encodes the pointer directly. */
simplify_symbols(struct module * mod,const struct load_info * info)1944 static int simplify_symbols(struct module *mod, const struct load_info *info)
1945 {
1946 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1947 	Elf_Sym *sym = (void *)symsec->sh_addr;
1948 	unsigned long secbase;
1949 	unsigned int i;
1950 	int ret = 0;
1951 	const struct kernel_symbol *ksym;
1952 
1953 	for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1954 		const char *name = info->strtab + sym[i].st_name;
1955 
1956 		switch (sym[i].st_shndx) {
1957 		case SHN_COMMON:
1958 			/* Ignore common symbols */
1959 			if (!strncmp(name, "__gnu_lto", 9))
1960 				break;
1961 
1962 			/* We compiled with -fno-common.  These are not
1963 			   supposed to happen.  */
1964 			pr_debug("Common symbol: %s\n", name);
1965 			printk("%s: please compile with -fno-common\n",
1966 			       mod->name);
1967 			ret = -ENOEXEC;
1968 			break;
1969 
1970 		case SHN_ABS:
1971 			/* Don't need to do anything */
1972 			pr_debug("Absolute symbol: 0x%08lx\n",
1973 			       (long)sym[i].st_value);
1974 			break;
1975 
1976 		case SHN_UNDEF:
1977 			ksym = resolve_symbol_wait(mod, info, name);
1978 			/* Ok if resolved.  */
1979 			if (ksym && !IS_ERR(ksym)) {
1980 				sym[i].st_value = ksym->value;
1981 				break;
1982 			}
1983 
1984 			/* Ok if weak.  */
1985 			if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1986 				break;
1987 
1988 			pr_warn("%s: Unknown symbol %s (err %li)\n",
1989 				mod->name, name, PTR_ERR(ksym));
1990 			ret = PTR_ERR(ksym) ?: -ENOENT;
1991 			break;
1992 
1993 		default:
1994 			/* Divert to percpu allocation if a percpu var. */
1995 			if (sym[i].st_shndx == info->index.pcpu)
1996 				secbase = (unsigned long)mod_percpu(mod);
1997 			else
1998 				secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1999 			sym[i].st_value += secbase;
2000 			break;
2001 		}
2002 	}
2003 
2004 	return ret;
2005 }
2006 
apply_relocations(struct module * mod,const struct load_info * info)2007 static int apply_relocations(struct module *mod, const struct load_info *info)
2008 {
2009 	unsigned int i;
2010 	int err = 0;
2011 
2012 	/* Now do relocations. */
2013 	for (i = 1; i < info->hdr->e_shnum; i++) {
2014 		unsigned int infosec = info->sechdrs[i].sh_info;
2015 
2016 		/* Not a valid relocation section? */
2017 		if (infosec >= info->hdr->e_shnum)
2018 			continue;
2019 
2020 		/* Don't bother with non-allocated sections */
2021 		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2022 			continue;
2023 
2024 		if (info->sechdrs[i].sh_type == SHT_REL)
2025 			err = apply_relocate(info->sechdrs, info->strtab,
2026 					     info->index.sym, i, mod);
2027 		else if (info->sechdrs[i].sh_type == SHT_RELA)
2028 			err = apply_relocate_add(info->sechdrs, info->strtab,
2029 						 info->index.sym, i, mod);
2030 		if (err < 0)
2031 			break;
2032 	}
2033 	return err;
2034 }
2035 
2036 /* Additional bytes needed by arch in front of individual sections */
arch_mod_section_prepend(struct module * mod,unsigned int section)2037 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2038 					     unsigned int section)
2039 {
2040 	/* default implementation just returns zero */
2041 	return 0;
2042 }
2043 
2044 /* Update size with this section: return offset. */
get_offset(struct module * mod,unsigned int * size,Elf_Shdr * sechdr,unsigned int section)2045 static long get_offset(struct module *mod, unsigned int *size,
2046 		       Elf_Shdr *sechdr, unsigned int section)
2047 {
2048 	long ret;
2049 
2050 	*size += arch_mod_section_prepend(mod, section);
2051 	ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2052 	*size = ret + sechdr->sh_size;
2053 	return ret;
2054 }
2055 
2056 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2057    might -- code, read-only data, read-write data, small data.  Tally
2058    sizes, and place the offsets into sh_entsize fields: high bit means it
2059    belongs in init. */
layout_sections(struct module * mod,struct load_info * info)2060 static void layout_sections(struct module *mod, struct load_info *info)
2061 {
2062 	static unsigned long const masks[][2] = {
2063 		/* NOTE: all executable code must be the first section
2064 		 * in this array; otherwise modify the text_size
2065 		 * finder in the two loops below */
2066 		{ SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2067 		{ SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2068 		{ SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2069 		{ ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2070 	};
2071 	unsigned int m, i;
2072 
2073 	for (i = 0; i < info->hdr->e_shnum; i++)
2074 		info->sechdrs[i].sh_entsize = ~0UL;
2075 
2076 	pr_debug("Core section allocation order:\n");
2077 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2078 		for (i = 0; i < info->hdr->e_shnum; ++i) {
2079 			Elf_Shdr *s = &info->sechdrs[i];
2080 			const char *sname = info->secstrings + s->sh_name;
2081 
2082 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
2083 			    || (s->sh_flags & masks[m][1])
2084 			    || s->sh_entsize != ~0UL
2085 			    || strstarts(sname, ".init"))
2086 				continue;
2087 			s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
2088 			pr_debug("\t%s\n", sname);
2089 		}
2090 		switch (m) {
2091 		case 0: /* executable */
2092 			mod->core_size = debug_align(mod->core_size);
2093 			mod->core_text_size = mod->core_size;
2094 			break;
2095 		case 1: /* RO: text and ro-data */
2096 			mod->core_size = debug_align(mod->core_size);
2097 			mod->core_ro_size = mod->core_size;
2098 			break;
2099 		case 3: /* whole core */
2100 			mod->core_size = debug_align(mod->core_size);
2101 			break;
2102 		}
2103 	}
2104 
2105 	pr_debug("Init section allocation order:\n");
2106 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2107 		for (i = 0; i < info->hdr->e_shnum; ++i) {
2108 			Elf_Shdr *s = &info->sechdrs[i];
2109 			const char *sname = info->secstrings + s->sh_name;
2110 
2111 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
2112 			    || (s->sh_flags & masks[m][1])
2113 			    || s->sh_entsize != ~0UL
2114 			    || !strstarts(sname, ".init"))
2115 				continue;
2116 			s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
2117 					 | INIT_OFFSET_MASK);
2118 			pr_debug("\t%s\n", sname);
2119 		}
2120 		switch (m) {
2121 		case 0: /* executable */
2122 			mod->init_size = debug_align(mod->init_size);
2123 			mod->init_text_size = mod->init_size;
2124 			break;
2125 		case 1: /* RO: text and ro-data */
2126 			mod->init_size = debug_align(mod->init_size);
2127 			mod->init_ro_size = mod->init_size;
2128 			break;
2129 		case 3: /* whole init */
2130 			mod->init_size = debug_align(mod->init_size);
2131 			break;
2132 		}
2133 	}
2134 }
2135 
set_license(struct module * mod,const char * license)2136 static void set_license(struct module *mod, const char *license)
2137 {
2138 	if (!license)
2139 		license = "unspecified";
2140 
2141 	if (!license_is_gpl_compatible(license)) {
2142 		if (!test_taint(TAINT_PROPRIETARY_MODULE))
2143 			pr_warn("%s: module license '%s' taints kernel.\n",
2144 				mod->name, license);
2145 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2146 				 LOCKDEP_NOW_UNRELIABLE);
2147 	}
2148 }
2149 
2150 /* Parse tag=value strings from .modinfo section */
next_string(char * string,unsigned long * secsize)2151 static char *next_string(char *string, unsigned long *secsize)
2152 {
2153 	/* Skip non-zero chars */
2154 	while (string[0]) {
2155 		string++;
2156 		if ((*secsize)-- <= 1)
2157 			return NULL;
2158 	}
2159 
2160 	/* Skip any zero padding. */
2161 	while (!string[0]) {
2162 		string++;
2163 		if ((*secsize)-- <= 1)
2164 			return NULL;
2165 	}
2166 	return string;
2167 }
2168 
get_modinfo(struct load_info * info,const char * tag)2169 static char *get_modinfo(struct load_info *info, const char *tag)
2170 {
2171 	char *p;
2172 	unsigned int taglen = strlen(tag);
2173 	Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2174 	unsigned long size = infosec->sh_size;
2175 
2176 	for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
2177 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2178 			return p + taglen + 1;
2179 	}
2180 	return NULL;
2181 }
2182 
setup_modinfo(struct module * mod,struct load_info * info)2183 static void setup_modinfo(struct module *mod, struct load_info *info)
2184 {
2185 	struct module_attribute *attr;
2186 	int i;
2187 
2188 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
2189 		if (attr->setup)
2190 			attr->setup(mod, get_modinfo(info, attr->attr.name));
2191 	}
2192 }
2193 
free_modinfo(struct module * mod)2194 static void free_modinfo(struct module *mod)
2195 {
2196 	struct module_attribute *attr;
2197 	int i;
2198 
2199 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
2200 		if (attr->free)
2201 			attr->free(mod);
2202 	}
2203 }
2204 
2205 #ifdef CONFIG_KALLSYMS
2206 
2207 /* lookup symbol in given range of kernel_symbols */
lookup_symbol(const char * name,const struct kernel_symbol * start,const struct kernel_symbol * stop)2208 static const struct kernel_symbol *lookup_symbol(const char *name,
2209 	const struct kernel_symbol *start,
2210 	const struct kernel_symbol *stop)
2211 {
2212 	return bsearch(name, start, stop - start,
2213 			sizeof(struct kernel_symbol), cmp_name);
2214 }
2215 
is_exported(const char * name,unsigned long value,const struct module * mod)2216 static int is_exported(const char *name, unsigned long value,
2217 		       const struct module *mod)
2218 {
2219 	const struct kernel_symbol *ks;
2220 	if (!mod)
2221 		ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2222 	else
2223 		ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2224 	return ks != NULL && ks->value == value;
2225 }
2226 
2227 /* As per nm */
elf_type(const Elf_Sym * sym,const struct load_info * info)2228 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2229 {
2230 	const Elf_Shdr *sechdrs = info->sechdrs;
2231 
2232 	if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2233 		if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2234 			return 'v';
2235 		else
2236 			return 'w';
2237 	}
2238 	if (sym->st_shndx == SHN_UNDEF)
2239 		return 'U';
2240 	if (sym->st_shndx == SHN_ABS)
2241 		return 'a';
2242 	if (sym->st_shndx >= SHN_LORESERVE)
2243 		return '?';
2244 	if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2245 		return 't';
2246 	if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2247 	    && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2248 		if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2249 			return 'r';
2250 		else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2251 			return 'g';
2252 		else
2253 			return 'd';
2254 	}
2255 	if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2256 		if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2257 			return 's';
2258 		else
2259 			return 'b';
2260 	}
2261 	if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2262 		      ".debug")) {
2263 		return 'n';
2264 	}
2265 	return '?';
2266 }
2267 
is_core_symbol(const Elf_Sym * src,const Elf_Shdr * sechdrs,unsigned int shnum)2268 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2269                            unsigned int shnum)
2270 {
2271 	const Elf_Shdr *sec;
2272 
2273 	if (src->st_shndx == SHN_UNDEF
2274 	    || src->st_shndx >= shnum
2275 	    || !src->st_name)
2276 		return false;
2277 
2278 	sec = sechdrs + src->st_shndx;
2279 	if (!(sec->sh_flags & SHF_ALLOC)
2280 #ifndef CONFIG_KALLSYMS_ALL
2281 	    || !(sec->sh_flags & SHF_EXECINSTR)
2282 #endif
2283 	    || (sec->sh_entsize & INIT_OFFSET_MASK))
2284 		return false;
2285 
2286 	return true;
2287 }
2288 
2289 /*
2290  * We only allocate and copy the strings needed by the parts of symtab
2291  * we keep.  This is simple, but has the effect of making multiple
2292  * copies of duplicates.  We could be more sophisticated, see
2293  * linux-kernel thread starting with
2294  * <73defb5e4bca04a6431392cc341112b1@localhost>.
2295  */
layout_symtab(struct module * mod,struct load_info * info)2296 static void layout_symtab(struct module *mod, struct load_info *info)
2297 {
2298 	Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2299 	Elf_Shdr *strsect = info->sechdrs + info->index.str;
2300 	const Elf_Sym *src;
2301 	unsigned int i, nsrc, ndst, strtab_size = 0;
2302 
2303 	/* Put symbol section at end of init part of module. */
2304 	symsect->sh_flags |= SHF_ALLOC;
2305 	symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
2306 					 info->index.sym) | INIT_OFFSET_MASK;
2307 	pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2308 
2309 	src = (void *)info->hdr + symsect->sh_offset;
2310 	nsrc = symsect->sh_size / sizeof(*src);
2311 
2312 	/* Compute total space required for the core symbols' strtab. */
2313 	for (ndst = i = 0; i < nsrc; i++) {
2314 		if (i == 0 ||
2315 		    is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2316 			strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2317 			ndst++;
2318 		}
2319 	}
2320 
2321 	/* Append room for core symbols at end of core part. */
2322 	info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2323 	info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2324 	mod->core_size += strtab_size;
2325 
2326 	/* Put string table section at end of init part of module. */
2327 	strsect->sh_flags |= SHF_ALLOC;
2328 	strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
2329 					 info->index.str) | INIT_OFFSET_MASK;
2330 	pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2331 
2332 	/* We'll tack temporary mod_kallsyms on the end. */
2333 	mod->init_size = ALIGN(mod->init_size,
2334 			       __alignof__(struct mod_kallsyms));
2335 	info->mod_kallsyms_init_off = mod->init_size;
2336 	mod->init_size += sizeof(struct mod_kallsyms);
2337 	mod->init_size = debug_align(mod->init_size);
2338 }
2339 
2340 /*
2341  * We use the full symtab and strtab which layout_symtab arranged to
2342  * be appended to the init section.  Later we switch to the cut-down
2343  * core-only ones.
2344  */
add_kallsyms(struct module * mod,const struct load_info * info)2345 static void add_kallsyms(struct module *mod, const struct load_info *info)
2346 {
2347 	unsigned int i, ndst;
2348 	const Elf_Sym *src;
2349 	Elf_Sym *dst;
2350 	char *s;
2351 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2352 
2353 	/* Set up to point into init section. */
2354 	mod->kallsyms = mod->module_init + info->mod_kallsyms_init_off;
2355 
2356 	mod->kallsyms->symtab = (void *)symsec->sh_addr;
2357 	mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2358 	/* Make sure we get permanent strtab: don't use info->strtab. */
2359 	mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2360 
2361 	/* Set types up while we still have access to sections. */
2362 	for (i = 0; i < mod->kallsyms->num_symtab; i++)
2363 		mod->kallsyms->symtab[i].st_info
2364 			= elf_type(&mod->kallsyms->symtab[i], info);
2365 
2366 	/* Now populate the cut down core kallsyms for after init. */
2367 	mod->core_kallsyms.symtab = dst = mod->module_core + info->symoffs;
2368 	mod->core_kallsyms.strtab = s = mod->module_core + info->stroffs;
2369 	src = mod->kallsyms->symtab;
2370 	for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2371 		if (i == 0 ||
2372 		    is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2373 			dst[ndst] = src[i];
2374 			dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2375 			s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2376 				     KSYM_NAME_LEN) + 1;
2377 		}
2378 	}
2379 	mod->core_kallsyms.num_symtab = ndst;
2380 }
2381 #else
layout_symtab(struct module * mod,struct load_info * info)2382 static inline void layout_symtab(struct module *mod, struct load_info *info)
2383 {
2384 }
2385 
add_kallsyms(struct module * mod,const struct load_info * info)2386 static void add_kallsyms(struct module *mod, const struct load_info *info)
2387 {
2388 }
2389 #endif /* CONFIG_KALLSYMS */
2390 
dynamic_debug_setup(struct _ddebug * debug,unsigned int num)2391 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2392 {
2393 	if (!debug)
2394 		return;
2395 #ifdef CONFIG_DYNAMIC_DEBUG
2396 	if (ddebug_add_module(debug, num, debug->modname))
2397 		pr_err("dynamic debug error adding module: %s\n",
2398 			debug->modname);
2399 #endif
2400 }
2401 
dynamic_debug_remove(struct _ddebug * debug)2402 static void dynamic_debug_remove(struct _ddebug *debug)
2403 {
2404 	if (debug)
2405 		ddebug_remove_module(debug->modname);
2406 }
2407 
module_alloc(unsigned long size)2408 void * __weak module_alloc(unsigned long size)
2409 {
2410 	return vmalloc_exec(size);
2411 }
2412 
module_alloc_update_bounds(unsigned long size)2413 static void *module_alloc_update_bounds(unsigned long size)
2414 {
2415 	void *ret = module_alloc(size);
2416 
2417 	if (ret) {
2418 		mutex_lock(&module_mutex);
2419 		/* Update module bounds. */
2420 		if ((unsigned long)ret < module_addr_min)
2421 			module_addr_min = (unsigned long)ret;
2422 		if ((unsigned long)ret + size > module_addr_max)
2423 			module_addr_max = (unsigned long)ret + size;
2424 		mutex_unlock(&module_mutex);
2425 	}
2426 	return ret;
2427 }
2428 
2429 #ifdef CONFIG_DEBUG_KMEMLEAK
kmemleak_load_module(const struct module * mod,const struct load_info * info)2430 static void kmemleak_load_module(const struct module *mod,
2431 				 const struct load_info *info)
2432 {
2433 	unsigned int i;
2434 
2435 	/* only scan the sections containing data */
2436 	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2437 
2438 	for (i = 1; i < info->hdr->e_shnum; i++) {
2439 		/* Scan all writable sections that's not executable */
2440 		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2441 		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2442 		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2443 			continue;
2444 
2445 		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2446 				   info->sechdrs[i].sh_size, GFP_KERNEL);
2447 	}
2448 }
2449 #else
kmemleak_load_module(const struct module * mod,const struct load_info * info)2450 static inline void kmemleak_load_module(const struct module *mod,
2451 					const struct load_info *info)
2452 {
2453 }
2454 #endif
2455 
2456 #ifdef CONFIG_MODULE_SIG
module_sig_check(struct load_info * info,int flags)2457 static int module_sig_check(struct load_info *info, int flags)
2458 {
2459 	int err = -ENOKEY;
2460 	const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2461 	const void *mod = info->hdr;
2462 
2463 	/*
2464 	 * Require flags == 0, as a module with version information
2465 	 * removed is no longer the module that was signed
2466 	 */
2467 	if (flags == 0 &&
2468 	    info->len > markerlen &&
2469 	    memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2470 		/* We truncate the module to discard the signature */
2471 		info->len -= markerlen;
2472 		err = mod_verify_sig(mod, &info->len);
2473 	}
2474 
2475 	if (!err) {
2476 		info->sig_ok = true;
2477 		return 0;
2478 	}
2479 
2480 	/* Not having a signature is only an error if we're strict. */
2481 	if (err == -ENOKEY && !sig_enforce)
2482 		err = 0;
2483 
2484 	return err;
2485 }
2486 #else /* !CONFIG_MODULE_SIG */
module_sig_check(struct load_info * info,int flags)2487 static int module_sig_check(struct load_info *info, int flags)
2488 {
2489 	return 0;
2490 }
2491 #endif /* !CONFIG_MODULE_SIG */
2492 
2493 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
elf_header_check(struct load_info * info)2494 static int elf_header_check(struct load_info *info)
2495 {
2496 	if (info->len < sizeof(*(info->hdr)))
2497 		return -ENOEXEC;
2498 
2499 	if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2500 	    || info->hdr->e_type != ET_REL
2501 	    || !elf_check_arch(info->hdr)
2502 	    || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2503 		return -ENOEXEC;
2504 
2505 	if (info->hdr->e_shoff >= info->len
2506 	    || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2507 		info->len - info->hdr->e_shoff))
2508 		return -ENOEXEC;
2509 
2510 	return 0;
2511 }
2512 
2513 /* Sets info->hdr and info->len. */
copy_module_from_user(const void __user * umod,unsigned long len,struct load_info * info)2514 static int copy_module_from_user(const void __user *umod, unsigned long len,
2515 				  struct load_info *info)
2516 {
2517 	int err;
2518 
2519 	info->len = len;
2520 	if (info->len < sizeof(*(info->hdr)))
2521 		return -ENOEXEC;
2522 
2523 	err = security_kernel_module_from_file(NULL);
2524 	if (err)
2525 		return err;
2526 
2527 	/* Suck in entire file: we'll want most of it. */
2528 	info->hdr = vmalloc(info->len);
2529 	if (!info->hdr)
2530 		return -ENOMEM;
2531 
2532 	if (copy_from_user(info->hdr, umod, info->len) != 0) {
2533 		vfree(info->hdr);
2534 		return -EFAULT;
2535 	}
2536 
2537 	return 0;
2538 }
2539 
2540 /* Sets info->hdr and info->len. */
copy_module_from_fd(int fd,struct load_info * info)2541 static int copy_module_from_fd(int fd, struct load_info *info)
2542 {
2543 	struct fd f = fdget(fd);
2544 	int err;
2545 	struct kstat stat;
2546 	loff_t pos;
2547 	ssize_t bytes = 0;
2548 
2549 	if (!f.file)
2550 		return -ENOEXEC;
2551 
2552 	err = security_kernel_module_from_file(f.file);
2553 	if (err)
2554 		goto out;
2555 
2556 	err = vfs_getattr(&f.file->f_path, &stat);
2557 	if (err)
2558 		goto out;
2559 
2560 	if (stat.size > INT_MAX) {
2561 		err = -EFBIG;
2562 		goto out;
2563 	}
2564 
2565 	/* Don't hand 0 to vmalloc, it whines. */
2566 	if (stat.size == 0) {
2567 		err = -EINVAL;
2568 		goto out;
2569 	}
2570 
2571 	info->hdr = vmalloc(stat.size);
2572 	if (!info->hdr) {
2573 		err = -ENOMEM;
2574 		goto out;
2575 	}
2576 
2577 	pos = 0;
2578 	while (pos < stat.size) {
2579 		bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
2580 				    stat.size - pos);
2581 		if (bytes < 0) {
2582 			vfree(info->hdr);
2583 			err = bytes;
2584 			goto out;
2585 		}
2586 		if (bytes == 0)
2587 			break;
2588 		pos += bytes;
2589 	}
2590 	info->len = pos;
2591 
2592 out:
2593 	fdput(f);
2594 	return err;
2595 }
2596 
free_copy(struct load_info * info)2597 static void free_copy(struct load_info *info)
2598 {
2599 	vfree(info->hdr);
2600 }
2601 
rewrite_section_headers(struct load_info * info,int flags)2602 static int rewrite_section_headers(struct load_info *info, int flags)
2603 {
2604 	unsigned int i;
2605 
2606 	/* This should always be true, but let's be sure. */
2607 	info->sechdrs[0].sh_addr = 0;
2608 
2609 	for (i = 1; i < info->hdr->e_shnum; i++) {
2610 		Elf_Shdr *shdr = &info->sechdrs[i];
2611 		if (shdr->sh_type != SHT_NOBITS
2612 		    && info->len < shdr->sh_offset + shdr->sh_size) {
2613 			pr_err("Module len %lu truncated\n", info->len);
2614 			return -ENOEXEC;
2615 		}
2616 
2617 		/* Mark all sections sh_addr with their address in the
2618 		   temporary image. */
2619 		shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2620 
2621 #ifndef CONFIG_MODULE_UNLOAD
2622 		/* Don't load .exit sections */
2623 		if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2624 			shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2625 #endif
2626 	}
2627 
2628 	/* Track but don't keep modinfo and version sections. */
2629 	if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2630 		info->index.vers = 0; /* Pretend no __versions section! */
2631 	else
2632 		info->index.vers = find_sec(info, "__versions");
2633 	info->index.info = find_sec(info, ".modinfo");
2634 	info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2635 	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2636 	return 0;
2637 }
2638 
2639 /*
2640  * Set up our basic convenience variables (pointers to section headers,
2641  * search for module section index etc), and do some basic section
2642  * verification.
2643  *
2644  * Return the temporary module pointer (we'll replace it with the final
2645  * one when we move the module sections around).
2646  */
setup_load_info(struct load_info * info,int flags)2647 static struct module *setup_load_info(struct load_info *info, int flags)
2648 {
2649 	unsigned int i;
2650 	int err;
2651 	struct module *mod;
2652 
2653 	/* Set up the convenience variables */
2654 	info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2655 	info->secstrings = (void *)info->hdr
2656 		+ info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2657 
2658 	err = rewrite_section_headers(info, flags);
2659 	if (err)
2660 		return ERR_PTR(err);
2661 
2662 	/* Find internal symbols and strings. */
2663 	for (i = 1; i < info->hdr->e_shnum; i++) {
2664 		if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2665 			info->index.sym = i;
2666 			info->index.str = info->sechdrs[i].sh_link;
2667 			info->strtab = (char *)info->hdr
2668 				+ info->sechdrs[info->index.str].sh_offset;
2669 			break;
2670 		}
2671 	}
2672 
2673 	info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2674 	if (!info->index.mod) {
2675 		pr_warn("No module found in object\n");
2676 		return ERR_PTR(-ENOEXEC);
2677 	}
2678 	/* This is temporary: point mod into copy of data. */
2679 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2680 
2681 	if (info->index.sym == 0) {
2682 		pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
2683 		return ERR_PTR(-ENOEXEC);
2684 	}
2685 
2686 	info->index.pcpu = find_pcpusec(info);
2687 
2688 	/* Check module struct version now, before we try to use module. */
2689 	if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2690 		return ERR_PTR(-ENOEXEC);
2691 
2692 	return mod;
2693 }
2694 
check_modinfo(struct module * mod,struct load_info * info,int flags)2695 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
2696 {
2697 	const char *modmagic = get_modinfo(info, "vermagic");
2698 	int err;
2699 
2700 	if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2701 		modmagic = NULL;
2702 
2703 	/* This is allowed: modprobe --force will invalidate it. */
2704 	if (!modmagic) {
2705 		err = try_to_force_load(mod, "bad vermagic");
2706 		if (err)
2707 			return err;
2708 	} else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2709 		pr_err("%s: version magic '%s' should be '%s'\n",
2710 		       mod->name, modmagic, vermagic);
2711 		return -ENOEXEC;
2712 	}
2713 
2714 	if (!get_modinfo(info, "intree"))
2715 		add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
2716 
2717 	if (get_modinfo(info, "staging")) {
2718 		add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
2719 		pr_warn("%s: module is from the staging directory, the quality "
2720 			"is unknown, you have been warned.\n", mod->name);
2721 	}
2722 
2723 	/* Set up license info based on the info section */
2724 	set_license(mod, get_modinfo(info, "license"));
2725 
2726 	return 0;
2727 }
2728 
find_module_sections(struct module * mod,struct load_info * info)2729 static int find_module_sections(struct module *mod, struct load_info *info)
2730 {
2731 	mod->kp = section_objs(info, "__param",
2732 			       sizeof(*mod->kp), &mod->num_kp);
2733 	mod->syms = section_objs(info, "__ksymtab",
2734 				 sizeof(*mod->syms), &mod->num_syms);
2735 	mod->crcs = section_addr(info, "__kcrctab");
2736 	mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2737 				     sizeof(*mod->gpl_syms),
2738 				     &mod->num_gpl_syms);
2739 	mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2740 	mod->gpl_future_syms = section_objs(info,
2741 					    "__ksymtab_gpl_future",
2742 					    sizeof(*mod->gpl_future_syms),
2743 					    &mod->num_gpl_future_syms);
2744 	mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2745 
2746 #ifdef CONFIG_UNUSED_SYMBOLS
2747 	mod->unused_syms = section_objs(info, "__ksymtab_unused",
2748 					sizeof(*mod->unused_syms),
2749 					&mod->num_unused_syms);
2750 	mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2751 	mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2752 					    sizeof(*mod->unused_gpl_syms),
2753 					    &mod->num_unused_gpl_syms);
2754 	mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2755 #endif
2756 #ifdef CONFIG_CONSTRUCTORS
2757 	mod->ctors = section_objs(info, ".ctors",
2758 				  sizeof(*mod->ctors), &mod->num_ctors);
2759 	if (!mod->ctors)
2760 		mod->ctors = section_objs(info, ".init_array",
2761 				sizeof(*mod->ctors), &mod->num_ctors);
2762 	else if (find_sec(info, ".init_array")) {
2763 		/*
2764 		 * This shouldn't happen with same compiler and binutils
2765 		 * building all parts of the module.
2766 		 */
2767 		printk(KERN_WARNING "%s: has both .ctors and .init_array.\n",
2768 		       mod->name);
2769 		return -EINVAL;
2770 	}
2771 #endif
2772 
2773 #ifdef CONFIG_TRACEPOINTS
2774 	mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2775 					     sizeof(*mod->tracepoints_ptrs),
2776 					     &mod->num_tracepoints);
2777 #endif
2778 #ifdef HAVE_JUMP_LABEL
2779 	mod->jump_entries = section_objs(info, "__jump_table",
2780 					sizeof(*mod->jump_entries),
2781 					&mod->num_jump_entries);
2782 #endif
2783 #ifdef CONFIG_EVENT_TRACING
2784 	mod->trace_events = section_objs(info, "_ftrace_events",
2785 					 sizeof(*mod->trace_events),
2786 					 &mod->num_trace_events);
2787 #endif
2788 #ifdef CONFIG_TRACING
2789 	mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2790 					 sizeof(*mod->trace_bprintk_fmt_start),
2791 					 &mod->num_trace_bprintk_fmt);
2792 #endif
2793 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2794 	/* sechdrs[0].sh_size is always zero */
2795 	mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2796 					     sizeof(*mod->ftrace_callsites),
2797 					     &mod->num_ftrace_callsites);
2798 #endif
2799 
2800 	mod->extable = section_objs(info, "__ex_table",
2801 				    sizeof(*mod->extable), &mod->num_exentries);
2802 
2803 	if (section_addr(info, "__obsparm"))
2804 		pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
2805 
2806 	info->debug = section_objs(info, "__verbose",
2807 				   sizeof(*info->debug), &info->num_debug);
2808 
2809 	return 0;
2810 }
2811 
move_module(struct module * mod,struct load_info * info)2812 static int move_module(struct module *mod, struct load_info *info)
2813 {
2814 	int i;
2815 	void *ptr;
2816 
2817 	/* Do the allocs. */
2818 	ptr = module_alloc_update_bounds(mod->core_size);
2819 	/*
2820 	 * The pointer to this block is stored in the module structure
2821 	 * which is inside the block. Just mark it as not being a
2822 	 * leak.
2823 	 */
2824 	kmemleak_not_leak(ptr);
2825 	if (!ptr)
2826 		return -ENOMEM;
2827 
2828 	memset(ptr, 0, mod->core_size);
2829 	mod->module_core = ptr;
2830 
2831 	if (mod->init_size) {
2832 		ptr = module_alloc_update_bounds(mod->init_size);
2833 		/*
2834 		 * The pointer to this block is stored in the module structure
2835 		 * which is inside the block. This block doesn't need to be
2836 		 * scanned as it contains data and code that will be freed
2837 		 * after the module is initialized.
2838 		 */
2839 		kmemleak_ignore(ptr);
2840 		if (!ptr) {
2841 			module_free(mod, mod->module_core);
2842 			return -ENOMEM;
2843 		}
2844 		memset(ptr, 0, mod->init_size);
2845 		mod->module_init = ptr;
2846 	} else
2847 		mod->module_init = NULL;
2848 
2849 	/* Transfer each section which specifies SHF_ALLOC */
2850 	pr_debug("final section addresses:\n");
2851 	for (i = 0; i < info->hdr->e_shnum; i++) {
2852 		void *dest;
2853 		Elf_Shdr *shdr = &info->sechdrs[i];
2854 
2855 		if (!(shdr->sh_flags & SHF_ALLOC))
2856 			continue;
2857 
2858 		if (shdr->sh_entsize & INIT_OFFSET_MASK)
2859 			dest = mod->module_init
2860 				+ (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2861 		else
2862 			dest = mod->module_core + shdr->sh_entsize;
2863 
2864 		if (shdr->sh_type != SHT_NOBITS)
2865 			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2866 		/* Update sh_addr to point to copy in image. */
2867 		shdr->sh_addr = (unsigned long)dest;
2868 		pr_debug("\t0x%lx %s\n",
2869 			 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
2870 	}
2871 
2872 	return 0;
2873 }
2874 
check_module_license_and_versions(struct module * mod)2875 static int check_module_license_and_versions(struct module *mod)
2876 {
2877 	/*
2878 	 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2879 	 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2880 	 * using GPL-only symbols it needs.
2881 	 */
2882 	if (strcmp(mod->name, "ndiswrapper") == 0)
2883 		add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
2884 
2885 	/* driverloader was caught wrongly pretending to be under GPL */
2886 	if (strcmp(mod->name, "driverloader") == 0)
2887 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2888 				 LOCKDEP_NOW_UNRELIABLE);
2889 
2890 	/* lve claims to be GPL but upstream won't provide source */
2891 	if (strcmp(mod->name, "lve") == 0)
2892 		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2893 				 LOCKDEP_NOW_UNRELIABLE);
2894 
2895 #ifdef CONFIG_MODVERSIONS
2896 	if ((mod->num_syms && !mod->crcs)
2897 	    || (mod->num_gpl_syms && !mod->gpl_crcs)
2898 	    || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2899 #ifdef CONFIG_UNUSED_SYMBOLS
2900 	    || (mod->num_unused_syms && !mod->unused_crcs)
2901 	    || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2902 #endif
2903 		) {
2904 		return try_to_force_load(mod,
2905 					 "no versions for exported symbols");
2906 	}
2907 #endif
2908 	return 0;
2909 }
2910 
flush_module_icache(const struct module * mod)2911 static void flush_module_icache(const struct module *mod)
2912 {
2913 	mm_segment_t old_fs;
2914 
2915 	/* flush the icache in correct context */
2916 	old_fs = get_fs();
2917 	set_fs(KERNEL_DS);
2918 
2919 	/*
2920 	 * Flush the instruction cache, since we've played with text.
2921 	 * Do it before processing of module parameters, so the module
2922 	 * can provide parameter accessor functions of its own.
2923 	 */
2924 	if (mod->module_init)
2925 		flush_icache_range((unsigned long)mod->module_init,
2926 				   (unsigned long)mod->module_init
2927 				   + mod->init_size);
2928 	flush_icache_range((unsigned long)mod->module_core,
2929 			   (unsigned long)mod->module_core + mod->core_size);
2930 
2931 	set_fs(old_fs);
2932 }
2933 
module_frob_arch_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)2934 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2935 				     Elf_Shdr *sechdrs,
2936 				     char *secstrings,
2937 				     struct module *mod)
2938 {
2939 	return 0;
2940 }
2941 
layout_and_allocate(struct load_info * info,int flags)2942 static struct module *layout_and_allocate(struct load_info *info, int flags)
2943 {
2944 	/* Module within temporary copy. */
2945 	struct module *mod;
2946 	int err;
2947 
2948 	mod = setup_load_info(info, flags);
2949 	if (IS_ERR(mod))
2950 		return mod;
2951 
2952 	err = check_modinfo(mod, info, flags);
2953 	if (err)
2954 		return ERR_PTR(err);
2955 
2956 	/* Allow arches to frob section contents and sizes.  */
2957 	err = module_frob_arch_sections(info->hdr, info->sechdrs,
2958 					info->secstrings, mod);
2959 	if (err < 0)
2960 		return ERR_PTR(err);
2961 
2962 	/* We will do a special allocation for per-cpu sections later. */
2963 	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
2964 
2965 	/* Determine total sizes, and put offsets in sh_entsize.  For now
2966 	   this is done generically; there doesn't appear to be any
2967 	   special cases for the architectures. */
2968 	layout_sections(mod, info);
2969 	layout_symtab(mod, info);
2970 
2971 	/* Allocate and move to the final place */
2972 	err = move_module(mod, info);
2973 	if (err)
2974 		return ERR_PTR(err);
2975 
2976 	/* Module has been copied to its final place now: return it. */
2977 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2978 	kmemleak_load_module(mod, info);
2979 	return mod;
2980 }
2981 
2982 /* mod is no longer valid after this! */
module_deallocate(struct module * mod,struct load_info * info)2983 static void module_deallocate(struct module *mod, struct load_info *info)
2984 {
2985 	percpu_modfree(mod);
2986 	module_free(mod, mod->module_init);
2987 	module_free(mod, mod->module_core);
2988 }
2989 
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)2990 int __weak module_finalize(const Elf_Ehdr *hdr,
2991 			   const Elf_Shdr *sechdrs,
2992 			   struct module *me)
2993 {
2994 	return 0;
2995 }
2996 
post_relocation(struct module * mod,const struct load_info * info)2997 static int post_relocation(struct module *mod, const struct load_info *info)
2998 {
2999 	/* Sort exception table now relocations are done. */
3000 	sort_extable(mod->extable, mod->extable + mod->num_exentries);
3001 
3002 	/* Copy relocated percpu area over. */
3003 	percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3004 		       info->sechdrs[info->index.pcpu].sh_size);
3005 
3006 	/* Setup kallsyms-specific fields. */
3007 	add_kallsyms(mod, info);
3008 
3009 	/* Arch-specific module finalizing. */
3010 	return module_finalize(info->hdr, info->sechdrs, mod);
3011 }
3012 
3013 /* Is this module of this name done loading?  No locks held. */
finished_loading(const char * name)3014 static bool finished_loading(const char *name)
3015 {
3016 	struct module *mod;
3017 	bool ret;
3018 
3019 	mutex_lock(&module_mutex);
3020 	mod = find_module_all(name, strlen(name), true);
3021 	ret = !mod || mod->state == MODULE_STATE_LIVE
3022 		|| mod->state == MODULE_STATE_GOING;
3023 	mutex_unlock(&module_mutex);
3024 
3025 	return ret;
3026 }
3027 
3028 /* Call module constructors. */
do_mod_ctors(struct module * mod)3029 static void do_mod_ctors(struct module *mod)
3030 {
3031 #ifdef CONFIG_CONSTRUCTORS
3032 	unsigned long i;
3033 
3034 	for (i = 0; i < mod->num_ctors; i++)
3035 		mod->ctors[i]();
3036 #endif
3037 }
3038 
3039 /* This is where the real work happens */
do_init_module(struct module * mod)3040 static int do_init_module(struct module *mod)
3041 {
3042 	int ret = 0;
3043 
3044 	/*
3045 	 * We want to find out whether @mod uses async during init.  Clear
3046 	 * PF_USED_ASYNC.  async_schedule*() will set it.
3047 	 */
3048 	current->flags &= ~PF_USED_ASYNC;
3049 
3050 	do_mod_ctors(mod);
3051 	/* Start the module */
3052 	if (mod->init != NULL)
3053 		ret = do_one_initcall(mod->init);
3054 	if (ret < 0) {
3055 		/* Init routine failed: abort.  Try to protect us from
3056                    buggy refcounters. */
3057 		mod->state = MODULE_STATE_GOING;
3058 		synchronize_sched();
3059 		module_put(mod);
3060 		blocking_notifier_call_chain(&module_notify_list,
3061 					     MODULE_STATE_GOING, mod);
3062 		free_module(mod);
3063 		wake_up_all(&module_wq);
3064 		return ret;
3065 	}
3066 	if (ret > 0) {
3067 		pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3068 			"follow 0/-E convention\n"
3069 			"%s: loading module anyway...\n",
3070 			__func__, mod->name, ret, __func__);
3071 		dump_stack();
3072 	}
3073 
3074 	/* Now it's a first class citizen! */
3075 	mod->state = MODULE_STATE_LIVE;
3076 	blocking_notifier_call_chain(&module_notify_list,
3077 				     MODULE_STATE_LIVE, mod);
3078 
3079 	/*
3080 	 * We need to finish all async code before the module init sequence
3081 	 * is done.  This has potential to deadlock.  For example, a newly
3082 	 * detected block device can trigger request_module() of the
3083 	 * default iosched from async probing task.  Once userland helper
3084 	 * reaches here, async_synchronize_full() will wait on the async
3085 	 * task waiting on request_module() and deadlock.
3086 	 *
3087 	 * This deadlock is avoided by perfomring async_synchronize_full()
3088 	 * iff module init queued any async jobs.  This isn't a full
3089 	 * solution as it will deadlock the same if module loading from
3090 	 * async jobs nests more than once; however, due to the various
3091 	 * constraints, this hack seems to be the best option for now.
3092 	 * Please refer to the following thread for details.
3093 	 *
3094 	 * http://thread.gmane.org/gmane.linux.kernel/1420814
3095 	 */
3096 	if (current->flags & PF_USED_ASYNC)
3097 		async_synchronize_full();
3098 
3099 	mutex_lock(&module_mutex);
3100 	/* Drop initial reference. */
3101 	module_put(mod);
3102 	trim_init_extable(mod);
3103 #ifdef CONFIG_KALLSYMS
3104 	/* Switch to core kallsyms now init is done: kallsyms may be walking! */
3105 	rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3106 #endif
3107 	unset_module_init_ro_nx(mod);
3108 	module_free(mod, mod->module_init);
3109 	mod->module_init = NULL;
3110 	mod->init_size = 0;
3111 	mod->init_ro_size = 0;
3112 	mod->init_text_size = 0;
3113 	mutex_unlock(&module_mutex);
3114 	wake_up_all(&module_wq);
3115 
3116 	return 0;
3117 }
3118 
may_init_module(void)3119 static int may_init_module(void)
3120 {
3121 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
3122 		return -EPERM;
3123 
3124 	return 0;
3125 }
3126 
3127 /*
3128  * We try to place it in the list now to make sure it's unique before
3129  * we dedicate too many resources.  In particular, temporary percpu
3130  * memory exhaustion.
3131  */
add_unformed_module(struct module * mod)3132 static int add_unformed_module(struct module *mod)
3133 {
3134 	int err;
3135 	struct module *old;
3136 
3137 	mod->state = MODULE_STATE_UNFORMED;
3138 
3139 again:
3140 	mutex_lock(&module_mutex);
3141 	old = find_module_all(mod->name, strlen(mod->name), true);
3142 	if (old != NULL) {
3143 		if (old->state == MODULE_STATE_COMING
3144 		    || old->state == MODULE_STATE_UNFORMED) {
3145 			/* Wait in case it fails to load. */
3146 			mutex_unlock(&module_mutex);
3147 			err = wait_event_interruptible(module_wq,
3148 					       finished_loading(mod->name));
3149 			if (err)
3150 				goto out_unlocked;
3151 			goto again;
3152 		}
3153 		err = -EEXIST;
3154 		goto out;
3155 	}
3156 	list_add_rcu(&mod->list, &modules);
3157 	err = 0;
3158 
3159 out:
3160 	mutex_unlock(&module_mutex);
3161 out_unlocked:
3162 	return err;
3163 }
3164 
complete_formation(struct module * mod,struct load_info * info)3165 static int complete_formation(struct module *mod, struct load_info *info)
3166 {
3167 	int err;
3168 
3169 	mutex_lock(&module_mutex);
3170 
3171 	/* Find duplicate symbols (must be called under lock). */
3172 	err = verify_export_symbols(mod);
3173 	if (err < 0)
3174 		goto out;
3175 
3176 	/* This relies on module_mutex for list integrity. */
3177 	module_bug_finalize(info->hdr, info->sechdrs, mod);
3178 
3179 	/* Set RO and NX regions for core */
3180 	set_section_ro_nx(mod->module_core,
3181 				mod->core_text_size,
3182 				mod->core_ro_size,
3183 				mod->core_size);
3184 
3185 	/* Set RO and NX regions for init */
3186 	set_section_ro_nx(mod->module_init,
3187 				mod->init_text_size,
3188 				mod->init_ro_size,
3189 				mod->init_size);
3190 
3191 	/* Mark state as coming so strong_try_module_get() ignores us,
3192 	 * but kallsyms etc. can see us. */
3193 	mod->state = MODULE_STATE_COMING;
3194 	mutex_unlock(&module_mutex);
3195 
3196 	blocking_notifier_call_chain(&module_notify_list,
3197 				     MODULE_STATE_COMING, mod);
3198 	return 0;
3199 
3200 out:
3201 	mutex_unlock(&module_mutex);
3202 	return err;
3203 }
3204 
unknown_module_param_cb(char * param,char * val,const char * modname)3205 static int unknown_module_param_cb(char *param, char *val, const char *modname)
3206 {
3207 	/* Check for magic 'dyndbg' arg */
3208 	int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3209 	if (ret != 0)
3210 		pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3211 	return 0;
3212 }
3213 
3214 /* Allocate and load the module: note that size of section 0 is always
3215    zero, and we rely on this for optional sections. */
load_module(struct load_info * info,const char __user * uargs,int flags)3216 static int load_module(struct load_info *info, const char __user *uargs,
3217 		       int flags)
3218 {
3219 	struct module *mod;
3220 	long err;
3221 	char *after_dashes;
3222 
3223 	err = module_sig_check(info, flags);
3224 	if (err)
3225 		goto free_copy;
3226 
3227 	err = elf_header_check(info);
3228 	if (err)
3229 		goto free_copy;
3230 
3231 	/* Figure out module layout, and allocate all the memory. */
3232 	mod = layout_and_allocate(info, flags);
3233 	if (IS_ERR(mod)) {
3234 		err = PTR_ERR(mod);
3235 		goto free_copy;
3236 	}
3237 
3238 	/* Reserve our place in the list. */
3239 	err = add_unformed_module(mod);
3240 	if (err)
3241 		goto free_module;
3242 
3243 #ifdef CONFIG_MODULE_SIG
3244 	mod->sig_ok = info->sig_ok;
3245 	if (!mod->sig_ok) {
3246 		pr_notice_once("%s: module verification failed: signature "
3247 			       "and/or  required key missing - tainting "
3248 			       "kernel\n", mod->name);
3249 		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
3250 	}
3251 #endif
3252 
3253 	/* To avoid stressing percpu allocator, do this once we're unique. */
3254 	err = percpu_modalloc(mod, info);
3255 	if (err)
3256 		goto unlink_mod;
3257 
3258 	/* Now module is in final location, initialize linked lists, etc. */
3259 	err = module_unload_init(mod);
3260 	if (err)
3261 		goto unlink_mod;
3262 
3263 	/* Now we've got everything in the final locations, we can
3264 	 * find optional sections. */
3265 	err = find_module_sections(mod, info);
3266 	if (err)
3267 		goto free_unload;
3268 
3269 	err = check_module_license_and_versions(mod);
3270 	if (err)
3271 		goto free_unload;
3272 
3273 	/* Set up MODINFO_ATTR fields */
3274 	setup_modinfo(mod, info);
3275 
3276 	/* Fix up syms, so that st_value is a pointer to location. */
3277 	err = simplify_symbols(mod, info);
3278 	if (err < 0)
3279 		goto free_modinfo;
3280 
3281 	err = apply_relocations(mod, info);
3282 	if (err < 0)
3283 		goto free_modinfo;
3284 
3285 	err = post_relocation(mod, info);
3286 	if (err < 0)
3287 		goto free_modinfo;
3288 
3289 	flush_module_icache(mod);
3290 
3291 	/* Now copy in args */
3292 	mod->args = strndup_user(uargs, ~0UL >> 1);
3293 	if (IS_ERR(mod->args)) {
3294 		err = PTR_ERR(mod->args);
3295 		goto free_arch_cleanup;
3296 	}
3297 
3298 	dynamic_debug_setup(info->debug, info->num_debug);
3299 
3300 	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3301 	ftrace_module_init(mod);
3302 
3303 	/* Finally it's fully formed, ready to start executing. */
3304 	err = complete_formation(mod, info);
3305 	if (err)
3306 		goto ddebug_cleanup;
3307 
3308 	/* Module is ready to execute: parsing args may do that. */
3309 	after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3310 				  -32768, 32767, unknown_module_param_cb);
3311 	if (IS_ERR(after_dashes)) {
3312 		err = PTR_ERR(after_dashes);
3313 		goto bug_cleanup;
3314 	} else if (after_dashes) {
3315 		pr_warn("%s: parameters '%s' after `--' ignored\n",
3316 		       mod->name, after_dashes);
3317 	}
3318 
3319 	/* Link in to syfs. */
3320 	err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3321 	if (err < 0)
3322 		goto bug_cleanup;
3323 
3324 	/* Get rid of temporary copy. */
3325 	free_copy(info);
3326 
3327 	/* Done! */
3328 	trace_module_load(mod);
3329 
3330 	return do_init_module(mod);
3331 
3332  bug_cleanup:
3333 	/* module_bug_cleanup needs module_mutex protection */
3334 	mutex_lock(&module_mutex);
3335 	module_bug_cleanup(mod);
3336 	mutex_unlock(&module_mutex);
3337 
3338 	blocking_notifier_call_chain(&module_notify_list,
3339 				     MODULE_STATE_GOING, mod);
3340 
3341 	/* we can't deallocate the module until we clear memory protection */
3342 	unset_module_init_ro_nx(mod);
3343 	unset_module_core_ro_nx(mod);
3344 
3345  ddebug_cleanup:
3346 	dynamic_debug_remove(info->debug);
3347 	synchronize_sched();
3348 	kfree(mod->args);
3349  free_arch_cleanup:
3350 	module_arch_cleanup(mod);
3351  free_modinfo:
3352 	free_modinfo(mod);
3353  free_unload:
3354 	module_unload_free(mod);
3355  unlink_mod:
3356 	mutex_lock(&module_mutex);
3357 	/* Unlink carefully: kallsyms could be walking list. */
3358 	list_del_rcu(&mod->list);
3359 	wake_up_all(&module_wq);
3360 	mutex_unlock(&module_mutex);
3361  free_module:
3362 	module_deallocate(mod, info);
3363  free_copy:
3364 	free_copy(info);
3365 	return err;
3366 }
3367 
SYSCALL_DEFINE3(init_module,void __user *,umod,unsigned long,len,const char __user *,uargs)3368 SYSCALL_DEFINE3(init_module, void __user *, umod,
3369 		unsigned long, len, const char __user *, uargs)
3370 {
3371 	int err;
3372 	struct load_info info = { };
3373 
3374 	err = may_init_module();
3375 	if (err)
3376 		return err;
3377 
3378 	pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3379 	       umod, len, uargs);
3380 
3381 	err = copy_module_from_user(umod, len, &info);
3382 	if (err)
3383 		return err;
3384 
3385 	return load_module(&info, uargs, 0);
3386 }
3387 
SYSCALL_DEFINE3(finit_module,int,fd,const char __user *,uargs,int,flags)3388 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
3389 {
3390 	int err;
3391 	struct load_info info = { };
3392 
3393 	err = may_init_module();
3394 	if (err)
3395 		return err;
3396 
3397 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3398 
3399 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3400 		      |MODULE_INIT_IGNORE_VERMAGIC))
3401 		return -EINVAL;
3402 
3403 	err = copy_module_from_fd(fd, &info);
3404 	if (err)
3405 		return err;
3406 
3407 	return load_module(&info, uargs, flags);
3408 }
3409 
within(unsigned long addr,void * start,unsigned long size)3410 static inline int within(unsigned long addr, void *start, unsigned long size)
3411 {
3412 	return ((void *)addr >= start && (void *)addr < start + size);
3413 }
3414 
3415 #ifdef CONFIG_KALLSYMS
3416 /*
3417  * This ignores the intensely annoying "mapping symbols" found
3418  * in ARM ELF files: $a, $t and $d.
3419  */
is_arm_mapping_symbol(const char * str)3420 static inline int is_arm_mapping_symbol(const char *str)
3421 {
3422 	if (str[0] == '.' && str[1] == 'L')
3423 		return true;
3424 	return str[0] == '$' && strchr("axtd", str[1])
3425 	       && (str[2] == '\0' || str[2] == '.');
3426 }
3427 
symname(struct mod_kallsyms * kallsyms,unsigned int symnum)3428 static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
3429 {
3430 	return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
3431 }
3432 
get_ksymbol(struct module * mod,unsigned long addr,unsigned long * size,unsigned long * offset)3433 static const char *get_ksymbol(struct module *mod,
3434 			       unsigned long addr,
3435 			       unsigned long *size,
3436 			       unsigned long *offset)
3437 {
3438 	unsigned int i, best = 0;
3439 	unsigned long nextval;
3440 	struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
3441 
3442 	/* At worse, next value is at end of module */
3443 	if (within_module_init(addr, mod))
3444 		nextval = (unsigned long)mod->module_init+mod->init_text_size;
3445 	else
3446 		nextval = (unsigned long)mod->module_core+mod->core_text_size;
3447 
3448 	/* Scan for closest preceding symbol, and next symbol. (ELF
3449 	   starts real symbols at 1). */
3450 	for (i = 1; i < kallsyms->num_symtab; i++) {
3451 		if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
3452 			continue;
3453 
3454 		/* We ignore unnamed symbols: they're uninformative
3455 		 * and inserted at a whim. */
3456 		if (*symname(kallsyms, i) == '\0'
3457 		    || is_arm_mapping_symbol(symname(kallsyms, i)))
3458 			continue;
3459 
3460 		if (kallsyms->symtab[i].st_value <= addr
3461 		    && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
3462 			best = i;
3463 		if (kallsyms->symtab[i].st_value > addr
3464 		    && kallsyms->symtab[i].st_value < nextval)
3465 			nextval = kallsyms->symtab[i].st_value;
3466 	}
3467 
3468 	if (!best)
3469 		return NULL;
3470 
3471 	if (size)
3472 		*size = nextval - kallsyms->symtab[best].st_value;
3473 	if (offset)
3474 		*offset = addr - kallsyms->symtab[best].st_value;
3475 	return symname(kallsyms, best);
3476 }
3477 
3478 /* For kallsyms to ask for address resolution.  NULL means not found.  Careful
3479  * not to lock to avoid deadlock on oopses, simply disable preemption. */
module_address_lookup(unsigned long addr,unsigned long * size,unsigned long * offset,char ** modname,char * namebuf)3480 const char *module_address_lookup(unsigned long addr,
3481 			    unsigned long *size,
3482 			    unsigned long *offset,
3483 			    char **modname,
3484 			    char *namebuf)
3485 {
3486 	struct module *mod;
3487 	const char *ret = NULL;
3488 
3489 	preempt_disable();
3490 	list_for_each_entry_rcu(mod, &modules, list) {
3491 		if (mod->state == MODULE_STATE_UNFORMED)
3492 			continue;
3493 		if (within_module(addr, mod)) {
3494 			if (modname)
3495 				*modname = mod->name;
3496 			ret = get_ksymbol(mod, addr, size, offset);
3497 			break;
3498 		}
3499 	}
3500 	/* Make a copy in here where it's safe */
3501 	if (ret) {
3502 		strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3503 		ret = namebuf;
3504 	}
3505 	preempt_enable();
3506 	return ret;
3507 }
3508 
lookup_module_symbol_name(unsigned long addr,char * symname)3509 int lookup_module_symbol_name(unsigned long addr, char *symname)
3510 {
3511 	struct module *mod;
3512 
3513 	preempt_disable();
3514 	list_for_each_entry_rcu(mod, &modules, list) {
3515 		if (mod->state == MODULE_STATE_UNFORMED)
3516 			continue;
3517 		if (within_module(addr, mod)) {
3518 			const char *sym;
3519 
3520 			sym = get_ksymbol(mod, addr, NULL, NULL);
3521 			if (!sym)
3522 				goto out;
3523 			strlcpy(symname, sym, KSYM_NAME_LEN);
3524 			preempt_enable();
3525 			return 0;
3526 		}
3527 	}
3528 out:
3529 	preempt_enable();
3530 	return -ERANGE;
3531 }
3532 
lookup_module_symbol_attrs(unsigned long addr,unsigned long * size,unsigned long * offset,char * modname,char * name)3533 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3534 			unsigned long *offset, char *modname, char *name)
3535 {
3536 	struct module *mod;
3537 
3538 	preempt_disable();
3539 	list_for_each_entry_rcu(mod, &modules, list) {
3540 		if (mod->state == MODULE_STATE_UNFORMED)
3541 			continue;
3542 		if (within_module(addr, mod)) {
3543 			const char *sym;
3544 
3545 			sym = get_ksymbol(mod, addr, size, offset);
3546 			if (!sym)
3547 				goto out;
3548 			if (modname)
3549 				strlcpy(modname, mod->name, MODULE_NAME_LEN);
3550 			if (name)
3551 				strlcpy(name, sym, KSYM_NAME_LEN);
3552 			preempt_enable();
3553 			return 0;
3554 		}
3555 	}
3556 out:
3557 	preempt_enable();
3558 	return -ERANGE;
3559 }
3560 
module_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name,char * module_name,int * exported)3561 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3562 			char *name, char *module_name, int *exported)
3563 {
3564 	struct module *mod;
3565 
3566 	preempt_disable();
3567 	list_for_each_entry_rcu(mod, &modules, list) {
3568 		struct mod_kallsyms *kallsyms;
3569 
3570 		if (mod->state == MODULE_STATE_UNFORMED)
3571 			continue;
3572 		kallsyms = rcu_dereference_sched(mod->kallsyms);
3573 		if (symnum < kallsyms->num_symtab) {
3574 			*value = kallsyms->symtab[symnum].st_value;
3575 			*type = kallsyms->symtab[symnum].st_info;
3576 			strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
3577 			strlcpy(module_name, mod->name, MODULE_NAME_LEN);
3578 			*exported = is_exported(name, *value, mod);
3579 			preempt_enable();
3580 			return 0;
3581 		}
3582 		symnum -= kallsyms->num_symtab;
3583 	}
3584 	preempt_enable();
3585 	return -ERANGE;
3586 }
3587 
mod_find_symname(struct module * mod,const char * name)3588 static unsigned long mod_find_symname(struct module *mod, const char *name)
3589 {
3590 	unsigned int i;
3591 	struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
3592 
3593 	for (i = 0; i < kallsyms->num_symtab; i++)
3594 		if (strcmp(name, symname(kallsyms, i)) == 0 &&
3595 		    kallsyms->symtab[i].st_info != 'U')
3596 			return kallsyms->symtab[i].st_value;
3597 	return 0;
3598 }
3599 
3600 /* Look for this name: can be of form module:name. */
module_kallsyms_lookup_name(const char * name)3601 unsigned long module_kallsyms_lookup_name(const char *name)
3602 {
3603 	struct module *mod;
3604 	char *colon;
3605 	unsigned long ret = 0;
3606 
3607 	/* Don't lock: we're in enough trouble already. */
3608 	preempt_disable();
3609 	if ((colon = strchr(name, ':')) != NULL) {
3610 		if ((mod = find_module_all(name, colon - name, false)) != NULL)
3611 			ret = mod_find_symname(mod, colon+1);
3612 	} else {
3613 		list_for_each_entry_rcu(mod, &modules, list) {
3614 			if (mod->state == MODULE_STATE_UNFORMED)
3615 				continue;
3616 			if ((ret = mod_find_symname(mod, name)) != 0)
3617 				break;
3618 		}
3619 	}
3620 	preempt_enable();
3621 	return ret;
3622 }
3623 
module_kallsyms_on_each_symbol(int (* fn)(void *,const char *,struct module *,unsigned long),void * data)3624 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3625 					     struct module *, unsigned long),
3626 				   void *data)
3627 {
3628 	struct module *mod;
3629 	unsigned int i;
3630 	int ret;
3631 
3632 	list_for_each_entry(mod, &modules, list) {
3633 		/* We hold module_mutex: no need for rcu_dereference_sched */
3634 		struct mod_kallsyms *kallsyms = mod->kallsyms;
3635 
3636 		if (mod->state == MODULE_STATE_UNFORMED)
3637 			continue;
3638 		for (i = 0; i < kallsyms->num_symtab; i++) {
3639 			ret = fn(data, symname(kallsyms, i),
3640 				 mod, kallsyms->symtab[i].st_value);
3641 			if (ret != 0)
3642 				return ret;
3643 		}
3644 	}
3645 	return 0;
3646 }
3647 #endif /* CONFIG_KALLSYMS */
3648 
module_flags(struct module * mod,char * buf)3649 static char *module_flags(struct module *mod, char *buf)
3650 {
3651 	int bx = 0;
3652 
3653 	BUG_ON(mod->state == MODULE_STATE_UNFORMED);
3654 	if (mod->taints ||
3655 	    mod->state == MODULE_STATE_GOING ||
3656 	    mod->state == MODULE_STATE_COMING) {
3657 		buf[bx++] = '(';
3658 		bx += module_flags_taint(mod, buf + bx);
3659 		/* Show a - for module-is-being-unloaded */
3660 		if (mod->state == MODULE_STATE_GOING)
3661 			buf[bx++] = '-';
3662 		/* Show a + for module-is-being-loaded */
3663 		if (mod->state == MODULE_STATE_COMING)
3664 			buf[bx++] = '+';
3665 		buf[bx++] = ')';
3666 	}
3667 	buf[bx] = '\0';
3668 
3669 	return buf;
3670 }
3671 
3672 #ifdef CONFIG_PROC_FS
3673 /* Called by the /proc file system to return a list of modules. */
m_start(struct seq_file * m,loff_t * pos)3674 static void *m_start(struct seq_file *m, loff_t *pos)
3675 {
3676 	mutex_lock(&module_mutex);
3677 	return seq_list_start(&modules, *pos);
3678 }
3679 
m_next(struct seq_file * m,void * p,loff_t * pos)3680 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3681 {
3682 	return seq_list_next(p, &modules, pos);
3683 }
3684 
m_stop(struct seq_file * m,void * p)3685 static void m_stop(struct seq_file *m, void *p)
3686 {
3687 	mutex_unlock(&module_mutex);
3688 }
3689 
m_show(struct seq_file * m,void * p)3690 static int m_show(struct seq_file *m, void *p)
3691 {
3692 	struct module *mod = list_entry(p, struct module, list);
3693 	char buf[8];
3694 
3695 	/* We always ignore unformed modules. */
3696 	if (mod->state == MODULE_STATE_UNFORMED)
3697 		return 0;
3698 
3699 	seq_printf(m, "%s %u",
3700 		   mod->name, mod->init_size + mod->core_size);
3701 	print_unload_info(m, mod);
3702 
3703 	/* Informative for users. */
3704 	seq_printf(m, " %s",
3705 		   mod->state == MODULE_STATE_GOING ? "Unloading":
3706 		   mod->state == MODULE_STATE_COMING ? "Loading":
3707 		   "Live");
3708 	/* Used by oprofile and other similar tools. */
3709 	seq_printf(m, " 0x%pK", mod->module_core);
3710 
3711 	/* Taints info */
3712 	if (mod->taints)
3713 		seq_printf(m, " %s", module_flags(mod, buf));
3714 
3715 	seq_printf(m, "\n");
3716 	return 0;
3717 }
3718 
3719 /* Format: modulename size refcount deps address
3720 
3721    Where refcount is a number or -, and deps is a comma-separated list
3722    of depends or -.
3723 */
3724 static const struct seq_operations modules_op = {
3725 	.start	= m_start,
3726 	.next	= m_next,
3727 	.stop	= m_stop,
3728 	.show	= m_show
3729 };
3730 
modules_open(struct inode * inode,struct file * file)3731 static int modules_open(struct inode *inode, struct file *file)
3732 {
3733 	return seq_open(file, &modules_op);
3734 }
3735 
3736 static const struct file_operations proc_modules_operations = {
3737 	.open		= modules_open,
3738 	.read		= seq_read,
3739 	.llseek		= seq_lseek,
3740 	.release	= seq_release,
3741 };
3742 
proc_modules_init(void)3743 static int __init proc_modules_init(void)
3744 {
3745 	proc_create("modules", 0, NULL, &proc_modules_operations);
3746 	return 0;
3747 }
3748 module_init(proc_modules_init);
3749 #endif
3750 
3751 /* Given an address, look for it in the module exception tables. */
search_module_extables(unsigned long addr)3752 const struct exception_table_entry *search_module_extables(unsigned long addr)
3753 {
3754 	const struct exception_table_entry *e = NULL;
3755 	struct module *mod;
3756 
3757 	preempt_disable();
3758 	list_for_each_entry_rcu(mod, &modules, list) {
3759 		if (mod->state == MODULE_STATE_UNFORMED)
3760 			continue;
3761 		if (mod->num_exentries == 0)
3762 			continue;
3763 
3764 		e = search_extable(mod->extable,
3765 				   mod->extable + mod->num_exentries - 1,
3766 				   addr);
3767 		if (e)
3768 			break;
3769 	}
3770 	preempt_enable();
3771 
3772 	/* Now, if we found one, we are running inside it now, hence
3773 	   we cannot unload the module, hence no refcnt needed. */
3774 	return e;
3775 }
3776 
3777 /*
3778  * is_module_address - is this address inside a module?
3779  * @addr: the address to check.
3780  *
3781  * See is_module_text_address() if you simply want to see if the address
3782  * is code (not data).
3783  */
is_module_address(unsigned long addr)3784 bool is_module_address(unsigned long addr)
3785 {
3786 	bool ret;
3787 
3788 	preempt_disable();
3789 	ret = __module_address(addr) != NULL;
3790 	preempt_enable();
3791 
3792 	return ret;
3793 }
3794 
3795 /*
3796  * __module_address - get the module which contains an address.
3797  * @addr: the address.
3798  *
3799  * Must be called with preempt disabled or module mutex held so that
3800  * module doesn't get freed during this.
3801  */
__module_address(unsigned long addr)3802 struct module *__module_address(unsigned long addr)
3803 {
3804 	struct module *mod;
3805 
3806 	if (addr < module_addr_min || addr > module_addr_max)
3807 		return NULL;
3808 
3809 	list_for_each_entry_rcu(mod, &modules, list) {
3810 		if (mod->state == MODULE_STATE_UNFORMED)
3811 			continue;
3812 		if (within_module(addr, mod))
3813 			return mod;
3814 	}
3815 	return NULL;
3816 }
3817 EXPORT_SYMBOL_GPL(__module_address);
3818 
3819 /*
3820  * is_module_text_address - is this address inside module code?
3821  * @addr: the address to check.
3822  *
3823  * See is_module_address() if you simply want to see if the address is
3824  * anywhere in a module.  See kernel_text_address() for testing if an
3825  * address corresponds to kernel or module code.
3826  */
is_module_text_address(unsigned long addr)3827 bool is_module_text_address(unsigned long addr)
3828 {
3829 	bool ret;
3830 
3831 	preempt_disable();
3832 	ret = __module_text_address(addr) != NULL;
3833 	preempt_enable();
3834 
3835 	return ret;
3836 }
3837 
3838 /*
3839  * __module_text_address - get the module whose code contains an address.
3840  * @addr: the address.
3841  *
3842  * Must be called with preempt disabled or module mutex held so that
3843  * module doesn't get freed during this.
3844  */
__module_text_address(unsigned long addr)3845 struct module *__module_text_address(unsigned long addr)
3846 {
3847 	struct module *mod = __module_address(addr);
3848 	if (mod) {
3849 		/* Make sure it's within the text section. */
3850 		if (!within(addr, mod->module_init, mod->init_text_size)
3851 		    && !within(addr, mod->module_core, mod->core_text_size))
3852 			mod = NULL;
3853 	}
3854 	return mod;
3855 }
3856 EXPORT_SYMBOL_GPL(__module_text_address);
3857 
3858 /* Don't grab lock, we're oopsing. */
print_modules(void)3859 void print_modules(void)
3860 {
3861 	struct module *mod;
3862 	char buf[8];
3863 
3864 	printk(KERN_DEFAULT "Modules linked in:");
3865 	/* Most callers should already have preempt disabled, but make sure */
3866 	preempt_disable();
3867 	list_for_each_entry_rcu(mod, &modules, list) {
3868 		if (mod->state == MODULE_STATE_UNFORMED)
3869 			continue;
3870 		pr_cont(" %s%s", mod->name, module_flags(mod, buf));
3871 	}
3872 	preempt_enable();
3873 	if (last_unloaded_module[0])
3874 		pr_cont(" [last unloaded: %s]", last_unloaded_module);
3875 	pr_cont("\n");
3876 }
3877 
3878 #ifdef CONFIG_MODVERSIONS
3879 /* Generate the signature for all relevant module structures here.
3880  * If these change, we don't want to try to parse the module. */
module_layout(struct module * mod,struct modversion_info * ver,struct kernel_param * kp,struct kernel_symbol * ks,struct tracepoint * const * tp)3881 void module_layout(struct module *mod,
3882 		   struct modversion_info *ver,
3883 		   struct kernel_param *kp,
3884 		   struct kernel_symbol *ks,
3885 		   struct tracepoint * const *tp)
3886 {
3887 }
3888 EXPORT_SYMBOL(module_layout);
3889 #endif
3890