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