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