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