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