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