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