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