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