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