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