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