| 1 | /* |
|---|
| 2 | Copyright (C) 2002 Richard Henderson |
|---|
| 3 | Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. |
|---|
| 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 | */ |
|---|
| 19 | #include <linux/export.h> |
|---|
| 20 | #include <linux/moduleloader.h> |
|---|
| 21 | #include <linux/ftrace_event.h> |
|---|
| 22 | #include <linux/init.h> |
|---|
| 23 | #include <linux/kallsyms.h> |
|---|
| 24 | #include <linux/fs.h> |
|---|
| 25 | #include <linux/sysfs.h> |
|---|
| 26 | #include <linux/kernel.h> |
|---|
| 27 | #include <linux/slab.h> |
|---|
| 28 | #include <linux/vmalloc.h> |
|---|
| 29 | #include <linux/elf.h> |
|---|
| 30 | #include <linux/proc_fs.h> |
|---|
| 31 | #include <linux/seq_file.h> |
|---|
| 32 | #include <linux/syscalls.h> |
|---|
| 33 | #include <linux/fcntl.h> |
|---|
| 34 | #include <linux/rcupdate.h> |
|---|
| 35 | #include <linux/capability.h> |
|---|
| 36 | #include <linux/cpu.h> |
|---|
| 37 | #include <linux/moduleparam.h> |
|---|
| 38 | #include <linux/errno.h> |
|---|
| 39 | #include <linux/err.h> |
|---|
| 40 | #include <linux/vermagic.h> |
|---|
| 41 | #include <linux/notifier.h> |
|---|
| 42 | #include <linux/sched.h> |
|---|
| 43 | #include <linux/stop_machine.h> |
|---|
| 44 | #include <linux/device.h> |
|---|
| 45 | #include <linux/string.h> |
|---|
| 46 | #include <linux/mutex.h> |
|---|
| 47 | #include <linux/rculist.h> |
|---|
| 48 | #include <asm/uaccess.h> |
|---|
| 49 | #include <asm/cacheflush.h> |
|---|
| 50 | #include <asm/mmu_context.h> |
|---|
| 51 | #include <linux/license.h> |
|---|
| 52 | #include <asm/sections.h> |
|---|
| 53 | #include <linux/tracepoint.h> |
|---|
| 54 | #include <linux/ftrace.h> |
|---|
| 55 | #include <linux/async.h> |
|---|
| 56 | #include <linux/percpu.h> |
|---|
| 57 | #include <linux/kmemleak.h> |
|---|
| 58 | #include <linux/jump_label.h> |
|---|
| 59 | #include <linux/pfn.h> |
|---|
| 60 | #include <linux/bsearch.h> |
|---|
| 61 | |
|---|
| 62 | #define CREATE_TRACE_POINTS |
|---|
| 63 | #include <trace/events/module.h> |
|---|
| 64 | |
|---|
| 65 | #if 0 |
|---|
| 66 | #define DEBUGP printk |
|---|
| 67 | #else |
|---|
| 68 | #define DEBUGP(fmt , a...) |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | #ifndef ARCH_SHF_SMALL |
|---|
| 72 | #define ARCH_SHF_SMALL 0 |
|---|
| 73 | #endif |
|---|
| 74 | |
|---|
| 75 | /* |
|---|
| 76 | * Modules' sections will be aligned on page boundaries |
|---|
| 77 | * to ensure complete separation of code and data, but |
|---|
| 78 | * only when CONFIG_DEBUG_SET_MODULE_RONX=y |
|---|
| 79 | */ |
|---|
| 80 | #ifdef CONFIG_DEBUG_SET_MODULE_RONX |
|---|
| 81 | # define debug_align(X) ALIGN(X, PAGE_SIZE) |
|---|
| 82 | #else |
|---|
| 83 | # define debug_align(X) (X) |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | * Given BASE and SIZE this macro calculates the number of pages the |
|---|
| 88 | * memory regions occupies |
|---|
| 89 | */ |
|---|
| 90 | #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \ |
|---|
| 91 | (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \ |
|---|
| 92 | PFN_DOWN((unsigned long)BASE) + 1) \ |
|---|
| 93 | : (0UL)) |
|---|
| 94 | |
|---|
| 95 | /* If this is set, the section belongs in the init part of the module */ |
|---|
| 96 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) |
|---|
| 97 | |
|---|
| 98 | /* |
|---|
| 99 | * Mutex protects: |
|---|
| 100 | * 1) List of modules (also safely readable with preempt_disable), |
|---|
| 101 | * 2) module_use links, |
|---|
| 102 | * 3) module_addr_min/module_addr_max. |
|---|
| 103 | * (delete uses stop_machine/add uses RCU list operations). */ |
|---|
| 104 | DEFINE_MUTEX(module_mutex); |
|---|
| 105 | EXPORT_SYMBOL_GPL(module_mutex); |
|---|
| 106 | static LIST_HEAD(modules); |
|---|
| 107 | #ifdef CONFIG_KGDB_KDB |
|---|
| 108 | struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */ |
|---|
| 109 | #endif /* CONFIG_KGDB_KDB */ |
|---|
| 110 | #ifdef CONFIG_CRASHLOG |
|---|
| 111 | struct list_head *crashlog_modules = &modules; |
|---|
| 112 | #endif |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /* Block module loading/unloading? */ |
|---|
| 116 | int modules_disabled = 0; |
|---|
| 117 | |
|---|
| 118 | /* Waiting for a module to finish initializing? */ |
|---|
| 119 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); |
|---|
| 120 | |
|---|
| 121 | static BLOCKING_NOTIFIER_HEAD(module_notify_list); |
|---|
| 122 | |
|---|
| 123 | /* Bounds of module allocation, for speeding __module_address. |
|---|
| 124 | * Protected by module_mutex. */ |
|---|
| 125 | static unsigned long module_addr_min = -1UL, module_addr_max = 0; |
|---|
| 126 | |
|---|
| 127 | int register_module_notifier(struct notifier_block * nb) |
|---|
| 128 | { |
|---|
| 129 | return blocking_notifier_chain_register(&module_notify_list, nb); |
|---|
| 130 | } |
|---|
| 131 | EXPORT_SYMBOL(register_module_notifier); |
|---|
| 132 | |
|---|
| 133 | int unregister_module_notifier(struct notifier_block * nb) |
|---|
| 134 | { |
|---|
| 135 | return blocking_notifier_chain_unregister(&module_notify_list, nb); |
|---|
| 136 | } |
|---|
| 137 | EXPORT_SYMBOL(unregister_module_notifier); |
|---|
| 138 | |
|---|
| 139 | struct load_info { |
|---|
| 140 | Elf_Ehdr *hdr; |
|---|
| 141 | unsigned long len; |
|---|
| 142 | Elf_Shdr *sechdrs; |
|---|
| 143 | char *secstrings, *strtab; |
|---|
| 144 | unsigned long *strmap; |
|---|
| 145 | unsigned long symoffs, stroffs; |
|---|
| 146 | struct _ddebug *debug; |
|---|
| 147 | unsigned int num_debug; |
|---|
| 148 | struct { |
|---|
| 149 | unsigned int sym, str, mod, vers, info, pcpu; |
|---|
| 150 | } index; |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | /* We require a truly strong try_module_get(): 0 means failure due to |
|---|
| 154 | ongoing or failed initialization etc. */ |
|---|
| 155 | static inline int strong_try_module_get(struct module *mod) |
|---|
| 156 | { |
|---|
| 157 | if (mod && mod->state == MODULE_STATE_COMING) |
|---|
| 158 | return -EBUSY; |
|---|
| 159 | if (try_module_get(mod)) |
|---|
| 160 | return 0; |
|---|
| 161 | else |
|---|
| 162 | return -ENOENT; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | static inline void add_taint_module(struct module *mod, unsigned flag) |
|---|
| 166 | { |
|---|
| 167 | add_taint(flag); |
|---|
| 168 | mod->taints |= (1U << flag); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /* |
|---|
| 172 | * A thread that wants to hold a reference to a module only while it |
|---|
| 173 | * is running can call this to safely exit. nfsd and lockd use this. |
|---|
| 174 | */ |
|---|
| 175 | void __module_put_and_exit(struct module *mod, long code) |
|---|
| 176 | { |
|---|
| 177 | module_put(mod); |
|---|
| 178 | do_exit(code); |
|---|
| 179 | } |
|---|
| 180 | EXPORT_SYMBOL(__module_put_and_exit); |
|---|
| 181 | |
|---|
| 182 | /* Find a module section: 0 means not found. */ |
|---|
| 183 | static unsigned int find_sec(const struct load_info *info, const char *name) |
|---|
| 184 | { |
|---|
| 185 | unsigned int i; |
|---|
| 186 | |
|---|
| 187 | for (i = 1; i < info->hdr->e_shnum; i++) { |
|---|
| 188 | Elf_Shdr *shdr = &info->sechdrs[i]; |
|---|
| 189 | /* Alloc bit cleared means "ignore it." */ |
|---|
| 190 | if ((shdr->sh_flags & SHF_ALLOC) |
|---|
| 191 | && strcmp(info->secstrings + shdr->sh_name, name) == 0) |
|---|
| 192 | return i; |
|---|
| 193 | } |
|---|
| 194 | return 0; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /* Find a module section, or NULL. */ |
|---|
| 198 | static void *section_addr(const struct load_info *info, const char *name) |
|---|
| 199 | { |
|---|
| 200 | /* Section 0 has sh_addr 0. */ |
|---|
| 201 | return (void *)info->sechdrs[find_sec(info, name)].sh_addr; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | /* Find a module section, or NULL. Fill in number of "objects" in section. */ |
|---|
| 205 | static void *section_objs(const struct load_info *info, |
|---|
| 206 | const char *name, |
|---|
| 207 | size_t object_size, |
|---|
| 208 | unsigned int *num) |
|---|
| 209 | { |
|---|
| 210 | unsigned int sec = find_sec(info, name); |
|---|
| 211 | |
|---|
| 212 | /* Section 0 has sh_addr 0 and sh_size 0. */ |
|---|
| 213 | *num = info->sechdrs[sec].sh_size / object_size; |
|---|
| 214 | return (void *)info->sechdrs[sec].sh_addr; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /* Provided by the linker */ |
|---|
| 218 | extern const struct kernel_symbol __start___ksymtab[]; |
|---|
| 219 | extern const struct kernel_symbol __stop___ksymtab[]; |
|---|
| 220 | extern const struct kernel_symbol __start___ksymtab_gpl[]; |
|---|
| 221 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; |
|---|
| 222 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; |
|---|
| 223 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; |
|---|
| 224 | extern const unsigned long __start___kcrctab[]; |
|---|
| 225 | extern const unsigned long __start___kcrctab_gpl[]; |
|---|
| 226 | extern const unsigned long __start___kcrctab_gpl_future[]; |
|---|
| 227 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 228 | extern const struct kernel_symbol __start___ksymtab_unused[]; |
|---|
| 229 | extern const struct kernel_symbol __stop___ksymtab_unused[]; |
|---|
| 230 | extern const struct kernel_symbol __start___ksymtab_unused_gpl[]; |
|---|
| 231 | extern const struct kernel_symbol __stop___ksymtab_unused_gpl[]; |
|---|
| 232 | extern const unsigned long __start___kcrctab_unused[]; |
|---|
| 233 | extern const unsigned long __start___kcrctab_unused_gpl[]; |
|---|
| 234 | #endif |
|---|
| 235 | |
|---|
| 236 | #ifndef CONFIG_MODVERSIONS |
|---|
| 237 | #define symversion(base, idx) NULL |
|---|
| 238 | #else |
|---|
| 239 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) |
|---|
| 240 | #endif |
|---|
| 241 | |
|---|
| 242 | static bool each_symbol_in_section(const struct symsearch *arr, |
|---|
| 243 | unsigned int arrsize, |
|---|
| 244 | struct module *owner, |
|---|
| 245 | bool (*fn)(const struct symsearch *syms, |
|---|
| 246 | struct module *owner, |
|---|
| 247 | void *data), |
|---|
| 248 | void *data) |
|---|
| 249 | { |
|---|
| 250 | unsigned int j; |
|---|
| 251 | |
|---|
| 252 | for (j = 0; j < arrsize; j++) { |
|---|
| 253 | if (fn(&arr[j], owner, data)) |
|---|
| 254 | return true; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /* Returns true as soon as fn returns true, otherwise false. */ |
|---|
| 261 | bool each_symbol_section(bool (*fn)(const struct symsearch *arr, |
|---|
| 262 | struct module *owner, |
|---|
| 263 | void *data), |
|---|
| 264 | void *data) |
|---|
| 265 | { |
|---|
| 266 | struct module *mod; |
|---|
| 267 | static const struct symsearch arr[] = { |
|---|
| 268 | { __start___ksymtab, __stop___ksymtab, __start___kcrctab, |
|---|
| 269 | NOT_GPL_ONLY, false }, |
|---|
| 270 | { __start___ksymtab_gpl, __stop___ksymtab_gpl, |
|---|
| 271 | __start___kcrctab_gpl, |
|---|
| 272 | GPL_ONLY, false }, |
|---|
| 273 | { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future, |
|---|
| 274 | __start___kcrctab_gpl_future, |
|---|
| 275 | WILL_BE_GPL_ONLY, false }, |
|---|
| 276 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 277 | { __start___ksymtab_unused, __stop___ksymtab_unused, |
|---|
| 278 | __start___kcrctab_unused, |
|---|
| 279 | NOT_GPL_ONLY, true }, |
|---|
| 280 | { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl, |
|---|
| 281 | __start___kcrctab_unused_gpl, |
|---|
| 282 | GPL_ONLY, true }, |
|---|
| 283 | #endif |
|---|
| 284 | }; |
|---|
| 285 | |
|---|
| 286 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data)) |
|---|
| 287 | return true; |
|---|
| 288 | |
|---|
| 289 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 290 | struct symsearch arr[] = { |
|---|
| 291 | { mod->syms, mod->syms + mod->num_syms, mod->crcs, |
|---|
| 292 | NOT_GPL_ONLY, false }, |
|---|
| 293 | { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms, |
|---|
| 294 | mod->gpl_crcs, |
|---|
| 295 | GPL_ONLY, false }, |
|---|
| 296 | { mod->gpl_future_syms, |
|---|
| 297 | mod->gpl_future_syms + mod->num_gpl_future_syms, |
|---|
| 298 | mod->gpl_future_crcs, |
|---|
| 299 | WILL_BE_GPL_ONLY, false }, |
|---|
| 300 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 301 | { mod->unused_syms, |
|---|
| 302 | mod->unused_syms + mod->num_unused_syms, |
|---|
| 303 | mod->unused_crcs, |
|---|
| 304 | NOT_GPL_ONLY, true }, |
|---|
| 305 | { mod->unused_gpl_syms, |
|---|
| 306 | mod->unused_gpl_syms + mod->num_unused_gpl_syms, |
|---|
| 307 | mod->unused_gpl_crcs, |
|---|
| 308 | GPL_ONLY, true }, |
|---|
| 309 | #endif |
|---|
| 310 | }; |
|---|
| 311 | |
|---|
| 312 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) |
|---|
| 313 | return true; |
|---|
| 314 | } |
|---|
| 315 | return false; |
|---|
| 316 | } |
|---|
| 317 | EXPORT_SYMBOL_GPL(each_symbol_section); |
|---|
| 318 | |
|---|
| 319 | struct find_symbol_arg { |
|---|
| 320 | /* Input */ |
|---|
| 321 | const char *name; |
|---|
| 322 | bool gplok; |
|---|
| 323 | bool warn; |
|---|
| 324 | |
|---|
| 325 | /* Output */ |
|---|
| 326 | struct module *owner; |
|---|
| 327 | const unsigned long *crc; |
|---|
| 328 | const struct kernel_symbol *sym; |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | static bool check_symbol(const struct symsearch *syms, |
|---|
| 332 | struct module *owner, |
|---|
| 333 | unsigned int symnum, void *data) |
|---|
| 334 | { |
|---|
| 335 | struct find_symbol_arg *fsa = data; |
|---|
| 336 | |
|---|
| 337 | if (!fsa->gplok) { |
|---|
| 338 | if (syms->licence == GPL_ONLY) |
|---|
| 339 | return false; |
|---|
| 340 | if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) { |
|---|
| 341 | printk(KERN_WARNING "Symbol %s is being used " |
|---|
| 342 | "by a non-GPL module, which will not " |
|---|
| 343 | "be allowed in the future\n", fsa->name); |
|---|
| 344 | printk(KERN_WARNING "Please see the file " |
|---|
| 345 | "Documentation/feature-removal-schedule.txt " |
|---|
| 346 | "in the kernel source tree for more details.\n"); |
|---|
| 347 | } |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 351 | if (syms->unused && fsa->warn) { |
|---|
| 352 | printk(KERN_WARNING "Symbol %s is marked as UNUSED, " |
|---|
| 353 | "however this module is using it.\n", fsa->name); |
|---|
| 354 | printk(KERN_WARNING |
|---|
| 355 | "This symbol will go away in the future.\n"); |
|---|
| 356 | printk(KERN_WARNING |
|---|
| 357 | "Please evalute if this is the right api to use and if " |
|---|
| 358 | "it really is, submit a report the linux kernel " |
|---|
| 359 | "mailinglist together with submitting your code for " |
|---|
| 360 | "inclusion.\n"); |
|---|
| 361 | } |
|---|
| 362 | #endif |
|---|
| 363 | |
|---|
| 364 | fsa->owner = owner; |
|---|
| 365 | fsa->crc = symversion(syms->crcs, symnum); |
|---|
| 366 | fsa->sym = &syms->start[symnum]; |
|---|
| 367 | return true; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | static int cmp_name(const void *va, const void *vb) |
|---|
| 371 | { |
|---|
| 372 | const char *a; |
|---|
| 373 | const struct kernel_symbol *b; |
|---|
| 374 | a = va; b = vb; |
|---|
| 375 | return strcmp(a, b->name); |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | static bool find_symbol_in_section(const struct symsearch *syms, |
|---|
| 379 | struct module *owner, |
|---|
| 380 | void *data) |
|---|
| 381 | { |
|---|
| 382 | struct find_symbol_arg *fsa = data; |
|---|
| 383 | struct kernel_symbol *sym; |
|---|
| 384 | |
|---|
| 385 | sym = bsearch(fsa->name, syms->start, syms->stop - syms->start, |
|---|
| 386 | sizeof(struct kernel_symbol), cmp_name); |
|---|
| 387 | |
|---|
| 388 | if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data)) |
|---|
| 389 | return true; |
|---|
| 390 | |
|---|
| 391 | return false; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | /* Find a symbol and return it, along with, (optional) crc and |
|---|
| 395 | * (optional) module which owns it. Needs preempt disabled or module_mutex. */ |
|---|
| 396 | const struct kernel_symbol *find_symbol(const char *name, |
|---|
| 397 | struct module **owner, |
|---|
| 398 | const unsigned long **crc, |
|---|
| 399 | bool gplok, |
|---|
| 400 | bool warn) |
|---|
| 401 | { |
|---|
| 402 | struct find_symbol_arg fsa; |
|---|
| 403 | |
|---|
| 404 | fsa.name = name; |
|---|
| 405 | fsa.gplok = gplok; |
|---|
| 406 | fsa.warn = warn; |
|---|
| 407 | |
|---|
| 408 | if (each_symbol_section(find_symbol_in_section, &fsa)) { |
|---|
| 409 | if (owner) |
|---|
| 410 | *owner = fsa.owner; |
|---|
| 411 | if (crc) |
|---|
| 412 | *crc = fsa.crc; |
|---|
| 413 | return fsa.sym; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | DEBUGP("Failed to find symbol %s\n", name); |
|---|
| 417 | return NULL; |
|---|
| 418 | } |
|---|
| 419 | EXPORT_SYMBOL_GPL(find_symbol); |
|---|
| 420 | |
|---|
| 421 | /* Search for module by name: must hold module_mutex. */ |
|---|
| 422 | struct module *find_module(const char *name) |
|---|
| 423 | { |
|---|
| 424 | struct module *mod; |
|---|
| 425 | |
|---|
| 426 | list_for_each_entry(mod, &modules, list) { |
|---|
| 427 | if (strcmp(mod->name, name) == 0) |
|---|
| 428 | return mod; |
|---|
| 429 | } |
|---|
| 430 | return NULL; |
|---|
| 431 | } |
|---|
| 432 | EXPORT_SYMBOL_GPL(find_module); |
|---|
| 433 | |
|---|
| 434 | #ifdef CONFIG_SMP |
|---|
| 435 | |
|---|
| 436 | static inline void __percpu *mod_percpu(struct module *mod) |
|---|
| 437 | { |
|---|
| 438 | return mod->percpu; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | static int percpu_modalloc(struct module *mod, |
|---|
| 442 | unsigned long size, unsigned long align) |
|---|
| 443 | { |
|---|
| 444 | if (align > PAGE_SIZE) { |
|---|
| 445 | printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", |
|---|
| 446 | mod->name, align, PAGE_SIZE); |
|---|
| 447 | align = PAGE_SIZE; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | mod->percpu = __alloc_reserved_percpu(size, align); |
|---|
| 451 | if (!mod->percpu) { |
|---|
| 452 | printk(KERN_WARNING |
|---|
| 453 | "%s: Could not allocate %lu bytes percpu data\n", |
|---|
| 454 | mod->name, size); |
|---|
| 455 | return -ENOMEM; |
|---|
| 456 | } |
|---|
| 457 | mod->percpu_size = size; |
|---|
| 458 | return 0; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | static void percpu_modfree(struct module *mod) |
|---|
| 462 | { |
|---|
| 463 | free_percpu(mod->percpu); |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | static unsigned int find_pcpusec(struct load_info *info) |
|---|
| 467 | { |
|---|
| 468 | return find_sec(info, ".data..percpu"); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | static void percpu_modcopy(struct module *mod, |
|---|
| 472 | const void *from, unsigned long size) |
|---|
| 473 | { |
|---|
| 474 | int cpu; |
|---|
| 475 | |
|---|
| 476 | for_each_possible_cpu(cpu) |
|---|
| 477 | memcpy(per_cpu_ptr(mod->percpu, cpu), from, size); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | /** |
|---|
| 481 | * is_module_percpu_address - test whether address is from module static percpu |
|---|
| 482 | * @addr: address to test |
|---|
| 483 | * |
|---|
| 484 | * Test whether @addr belongs to module static percpu area. |
|---|
| 485 | * |
|---|
| 486 | * RETURNS: |
|---|
| 487 | * %true if @addr is from module static percpu area |
|---|
| 488 | */ |
|---|
| 489 | bool is_module_percpu_address(unsigned long addr) |
|---|
| 490 | { |
|---|
| 491 | struct module *mod; |
|---|
| 492 | unsigned int cpu; |
|---|
| 493 | |
|---|
| 494 | preempt_disable(); |
|---|
| 495 | |
|---|
| 496 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 497 | if (!mod->percpu_size) |
|---|
| 498 | continue; |
|---|
| 499 | for_each_possible_cpu(cpu) { |
|---|
| 500 | void *start = per_cpu_ptr(mod->percpu, cpu); |
|---|
| 501 | |
|---|
| 502 | if ((void *)addr >= start && |
|---|
| 503 | (void *)addr < start + mod->percpu_size) { |
|---|
| 504 | preempt_enable(); |
|---|
| 505 | return true; |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | preempt_enable(); |
|---|
| 511 | return false; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | #else /* ... !CONFIG_SMP */ |
|---|
| 515 | |
|---|
| 516 | static inline void __percpu *mod_percpu(struct module *mod) |
|---|
| 517 | { |
|---|
| 518 | return NULL; |
|---|
| 519 | } |
|---|
| 520 | static inline int percpu_modalloc(struct module *mod, |
|---|
| 521 | unsigned long size, unsigned long align) |
|---|
| 522 | { |
|---|
| 523 | return -ENOMEM; |
|---|
| 524 | } |
|---|
| 525 | static inline void percpu_modfree(struct module *mod) |
|---|
| 526 | { |
|---|
| 527 | } |
|---|
| 528 | static unsigned int find_pcpusec(struct load_info *info) |
|---|
| 529 | { |
|---|
| 530 | return 0; |
|---|
| 531 | } |
|---|
| 532 | static inline void percpu_modcopy(struct module *mod, |
|---|
| 533 | const void *from, unsigned long size) |
|---|
| 534 | { |
|---|
| 535 | /* pcpusec should be 0, and size of that section should be 0. */ |
|---|
| 536 | BUG_ON(size != 0); |
|---|
| 537 | } |
|---|
| 538 | bool is_module_percpu_address(unsigned long addr) |
|---|
| 539 | { |
|---|
| 540 | return false; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | #endif /* CONFIG_SMP */ |
|---|
| 544 | |
|---|
| 545 | #define MODINFO_ATTR(field) \ |
|---|
| 546 | static void setup_modinfo_##field(struct module *mod, const char *s) \ |
|---|
| 547 | { \ |
|---|
| 548 | mod->field = kstrdup(s, GFP_KERNEL); \ |
|---|
| 549 | } \ |
|---|
| 550 | static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ |
|---|
| 551 | struct module_kobject *mk, char *buffer) \ |
|---|
| 552 | { \ |
|---|
| 553 | return sprintf(buffer, "%s\n", mk->mod->field); \ |
|---|
| 554 | } \ |
|---|
| 555 | static int modinfo_##field##_exists(struct module *mod) \ |
|---|
| 556 | { \ |
|---|
| 557 | return mod->field != NULL; \ |
|---|
| 558 | } \ |
|---|
| 559 | static void free_modinfo_##field(struct module *mod) \ |
|---|
| 560 | { \ |
|---|
| 561 | kfree(mod->field); \ |
|---|
| 562 | mod->field = NULL; \ |
|---|
| 563 | } \ |
|---|
| 564 | static struct module_attribute modinfo_##field = { \ |
|---|
| 565 | .attr = { .name = __stringify(field), .mode = 0444 }, \ |
|---|
| 566 | .show = show_modinfo_##field, \ |
|---|
| 567 | .setup = setup_modinfo_##field, \ |
|---|
| 568 | .test = modinfo_##field##_exists, \ |
|---|
| 569 | .free = free_modinfo_##field, \ |
|---|
| 570 | }; |
|---|
| 571 | |
|---|
| 572 | MODINFO_ATTR(version); |
|---|
| 573 | MODINFO_ATTR(srcversion); |
|---|
| 574 | |
|---|
| 575 | static char last_unloaded_module[MODULE_NAME_LEN+1]; |
|---|
| 576 | |
|---|
| 577 | #ifdef CONFIG_MODULE_UNLOAD |
|---|
| 578 | |
|---|
| 579 | EXPORT_TRACEPOINT_SYMBOL(module_get); |
|---|
| 580 | |
|---|
| 581 | /* Init the unload section of the module. */ |
|---|
| 582 | static int module_unload_init(struct module *mod) |
|---|
| 583 | { |
|---|
| 584 | mod->refptr = alloc_percpu(struct module_ref); |
|---|
| 585 | if (!mod->refptr) |
|---|
| 586 | return -ENOMEM; |
|---|
| 587 | |
|---|
| 588 | INIT_LIST_HEAD(&mod->source_list); |
|---|
| 589 | INIT_LIST_HEAD(&mod->target_list); |
|---|
| 590 | |
|---|
| 591 | /* Hold reference count during initialization. */ |
|---|
| 592 | __this_cpu_write(mod->refptr->incs, 1); |
|---|
| 593 | /* Backwards compatibility macros put refcount during init. */ |
|---|
| 594 | mod->waiter = current; |
|---|
| 595 | |
|---|
| 596 | return 0; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | /* Does a already use b? */ |
|---|
| 600 | static int already_uses(struct module *a, struct module *b) |
|---|
| 601 | { |
|---|
| 602 | struct module_use *use; |
|---|
| 603 | |
|---|
| 604 | list_for_each_entry(use, &b->source_list, source_list) { |
|---|
| 605 | if (use->source == a) { |
|---|
| 606 | DEBUGP("%s uses %s!\n", a->name, b->name); |
|---|
| 607 | return 1; |
|---|
| 608 | } |
|---|
| 609 | } |
|---|
| 610 | DEBUGP("%s does not use %s!\n", a->name, b->name); |
|---|
| 611 | return 0; |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | /* |
|---|
| 615 | * Module a uses b |
|---|
| 616 | * - we add 'a' as a "source", 'b' as a "target" of module use |
|---|
| 617 | * - the module_use is added to the list of 'b' sources (so |
|---|
| 618 | * 'b' can walk the list to see who sourced them), and of 'a' |
|---|
| 619 | * targets (so 'a' can see what modules it targets). |
|---|
| 620 | */ |
|---|
| 621 | static int add_module_usage(struct module *a, struct module *b) |
|---|
| 622 | { |
|---|
| 623 | struct module_use *use; |
|---|
| 624 | |
|---|
| 625 | DEBUGP("Allocating new usage for %s.\n", a->name); |
|---|
| 626 | use = kmalloc(sizeof(*use), GFP_ATOMIC); |
|---|
| 627 | if (!use) { |
|---|
| 628 | printk(KERN_WARNING "%s: out of memory loading\n", a->name); |
|---|
| 629 | return -ENOMEM; |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | use->source = a; |
|---|
| 633 | use->target = b; |
|---|
| 634 | list_add(&use->source_list, &b->source_list); |
|---|
| 635 | list_add(&use->target_list, &a->target_list); |
|---|
| 636 | return 0; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | /* Module a uses b: caller needs module_mutex() */ |
|---|
| 640 | int ref_module(struct module *a, struct module *b) |
|---|
| 641 | { |
|---|
| 642 | int err; |
|---|
| 643 | |
|---|
| 644 | if (b == NULL || already_uses(a, b)) |
|---|
| 645 | return 0; |
|---|
| 646 | |
|---|
| 647 | /* If module isn't available, we fail. */ |
|---|
| 648 | err = strong_try_module_get(b); |
|---|
| 649 | if (err) |
|---|
| 650 | return err; |
|---|
| 651 | |
|---|
| 652 | err = add_module_usage(a, b); |
|---|
| 653 | if (err) { |
|---|
| 654 | module_put(b); |
|---|
| 655 | return err; |
|---|
| 656 | } |
|---|
| 657 | return 0; |
|---|
| 658 | } |
|---|
| 659 | EXPORT_SYMBOL_GPL(ref_module); |
|---|
| 660 | |
|---|
| 661 | /* Clear the unload stuff of the module. */ |
|---|
| 662 | static void module_unload_free(struct module *mod) |
|---|
| 663 | { |
|---|
| 664 | struct module_use *use, *tmp; |
|---|
| 665 | |
|---|
| 666 | mutex_lock(&module_mutex); |
|---|
| 667 | list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) { |
|---|
| 668 | struct module *i = use->target; |
|---|
| 669 | DEBUGP("%s unusing %s\n", mod->name, i->name); |
|---|
| 670 | module_put(i); |
|---|
| 671 | list_del(&use->source_list); |
|---|
| 672 | list_del(&use->target_list); |
|---|
| 673 | kfree(use); |
|---|
| 674 | } |
|---|
| 675 | mutex_unlock(&module_mutex); |
|---|
| 676 | |
|---|
| 677 | free_percpu(mod->refptr); |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | #ifdef CONFIG_MODULE_FORCE_UNLOAD |
|---|
| 681 | static inline int try_force_unload(unsigned int flags) |
|---|
| 682 | { |
|---|
| 683 | int ret = (flags & O_TRUNC); |
|---|
| 684 | if (ret) |
|---|
| 685 | add_taint(TAINT_FORCED_RMMOD); |
|---|
| 686 | return ret; |
|---|
| 687 | } |
|---|
| 688 | #else |
|---|
| 689 | static inline int try_force_unload(unsigned int flags) |
|---|
| 690 | { |
|---|
| 691 | return 0; |
|---|
| 692 | } |
|---|
| 693 | #endif /* CONFIG_MODULE_FORCE_UNLOAD */ |
|---|
| 694 | |
|---|
| 695 | struct stopref |
|---|
| 696 | { |
|---|
| 697 | struct module *mod; |
|---|
| 698 | int flags; |
|---|
| 699 | int *forced; |
|---|
| 700 | }; |
|---|
| 701 | |
|---|
| 702 | /* Whole machine is stopped with interrupts off when this runs. */ |
|---|
| 703 | static int __try_stop_module(void *_sref) |
|---|
| 704 | { |
|---|
| 705 | struct stopref *sref = _sref; |
|---|
| 706 | |
|---|
| 707 | /* If it's not unused, quit unless we're forcing. */ |
|---|
| 708 | if (module_refcount(sref->mod) != 0) { |
|---|
| 709 | if (!(*sref->forced = try_force_unload(sref->flags))) |
|---|
| 710 | return -EWOULDBLOCK; |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | /* Mark it as dying. */ |
|---|
| 714 | sref->mod->state = MODULE_STATE_GOING; |
|---|
| 715 | return 0; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | static int try_stop_module(struct module *mod, int flags, int *forced) |
|---|
| 719 | { |
|---|
| 720 | if (flags & O_NONBLOCK) { |
|---|
| 721 | struct stopref sref = { mod, flags, forced }; |
|---|
| 722 | |
|---|
| 723 | return stop_machine(__try_stop_module, &sref, NULL); |
|---|
| 724 | } else { |
|---|
| 725 | /* We don't need to stop the machine for this. */ |
|---|
| 726 | mod->state = MODULE_STATE_GOING; |
|---|
| 727 | synchronize_sched(); |
|---|
| 728 | return 0; |
|---|
| 729 | } |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | unsigned int module_refcount(struct module *mod) |
|---|
| 733 | { |
|---|
| 734 | unsigned int incs = 0, decs = 0; |
|---|
| 735 | int cpu; |
|---|
| 736 | |
|---|
| 737 | for_each_possible_cpu(cpu) |
|---|
| 738 | decs += per_cpu_ptr(mod->refptr, cpu)->decs; |
|---|
| 739 | /* |
|---|
| 740 | * ensure the incs are added up after the decs. |
|---|
| 741 | * module_put ensures incs are visible before decs with smp_wmb. |
|---|
| 742 | * |
|---|
| 743 | * This 2-count scheme avoids the situation where the refcount |
|---|
| 744 | * for CPU0 is read, then CPU0 increments the module refcount, |
|---|
| 745 | * then CPU1 drops that refcount, then the refcount for CPU1 is |
|---|
| 746 | * read. We would record a decrement but not its corresponding |
|---|
| 747 | * increment so we would see a low count (disaster). |
|---|
| 748 | * |
|---|
| 749 | * Rare situation? But module_refcount can be preempted, and we |
|---|
| 750 | * might be tallying up 4096+ CPUs. So it is not impossible. |
|---|
| 751 | */ |
|---|
| 752 | smp_rmb(); |
|---|
| 753 | for_each_possible_cpu(cpu) |
|---|
| 754 | incs += per_cpu_ptr(mod->refptr, cpu)->incs; |
|---|
| 755 | return incs - decs; |
|---|
| 756 | } |
|---|
| 757 | EXPORT_SYMBOL(module_refcount); |
|---|
| 758 | |
|---|
| 759 | /* This exists whether we can unload or not */ |
|---|
| 760 | static void free_module(struct module *mod); |
|---|
| 761 | |
|---|
| 762 | static void wait_for_zero_refcount(struct module *mod) |
|---|
| 763 | { |
|---|
| 764 | /* Since we might sleep for some time, release the mutex first */ |
|---|
| 765 | mutex_unlock(&module_mutex); |
|---|
| 766 | for (;;) { |
|---|
| 767 | DEBUGP("Looking at refcount...\n"); |
|---|
| 768 | set_current_state(TASK_UNINTERRUPTIBLE); |
|---|
| 769 | if (module_refcount(mod) == 0) |
|---|
| 770 | break; |
|---|
| 771 | schedule(); |
|---|
| 772 | } |
|---|
| 773 | current->state = TASK_RUNNING; |
|---|
| 774 | mutex_lock(&module_mutex); |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | SYSCALL_DEFINE2(delete_module, const char __user *, name_user, |
|---|
| 778 | unsigned int, flags) |
|---|
| 779 | { |
|---|
| 780 | struct module *mod; |
|---|
| 781 | char name[MODULE_NAME_LEN]; |
|---|
| 782 | int ret, forced = 0; |
|---|
| 783 | |
|---|
| 784 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
|---|
| 785 | return -EPERM; |
|---|
| 786 | |
|---|
| 787 | if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) |
|---|
| 788 | return -EFAULT; |
|---|
| 789 | name[MODULE_NAME_LEN-1] = '\0'; |
|---|
| 790 | |
|---|
| 791 | if (mutex_lock_interruptible(&module_mutex) != 0) |
|---|
| 792 | return -EINTR; |
|---|
| 793 | |
|---|
| 794 | mod = find_module(name); |
|---|
| 795 | if (!mod) { |
|---|
| 796 | ret = -ENOENT; |
|---|
| 797 | goto out; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | if (!list_empty(&mod->source_list)) { |
|---|
| 801 | /* Other modules depend on us: get rid of them first. */ |
|---|
| 802 | ret = -EWOULDBLOCK; |
|---|
| 803 | goto out; |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | /* Doing init or already dying? */ |
|---|
| 807 | if (mod->state != MODULE_STATE_LIVE) { |
|---|
| 808 | /* FIXME: if (force), slam module count and wake up |
|---|
| 809 | waiter --RR */ |
|---|
| 810 | DEBUGP("%s already dying\n", mod->name); |
|---|
| 811 | ret = -EBUSY; |
|---|
| 812 | goto out; |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | /* If it has an init func, it must have an exit func to unload */ |
|---|
| 816 | if (mod->init && !mod->exit) { |
|---|
| 817 | forced = try_force_unload(flags); |
|---|
| 818 | if (!forced) { |
|---|
| 819 | /* This module can't be removed */ |
|---|
| 820 | ret = -EBUSY; |
|---|
| 821 | goto out; |
|---|
| 822 | } |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | /* Set this up before setting mod->state */ |
|---|
| 826 | mod->waiter = current; |
|---|
| 827 | |
|---|
| 828 | /* Stop the machine so refcounts can't move and disable module. */ |
|---|
| 829 | ret = try_stop_module(mod, flags, &forced); |
|---|
| 830 | if (ret != 0) |
|---|
| 831 | goto out; |
|---|
| 832 | |
|---|
| 833 | /* Never wait if forced. */ |
|---|
| 834 | if (!forced && module_refcount(mod) != 0) |
|---|
| 835 | wait_for_zero_refcount(mod); |
|---|
| 836 | |
|---|
| 837 | mutex_unlock(&module_mutex); |
|---|
| 838 | /* Final destruction now no one is using it. */ |
|---|
| 839 | if (mod->exit != NULL) |
|---|
| 840 | mod->exit(); |
|---|
| 841 | blocking_notifier_call_chain(&module_notify_list, |
|---|
| 842 | MODULE_STATE_GOING, mod); |
|---|
| 843 | async_synchronize_full(); |
|---|
| 844 | |
|---|
| 845 | /* Store the name of the last unloaded module for diagnostic purposes */ |
|---|
| 846 | strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); |
|---|
| 847 | |
|---|
| 848 | free_module(mod); |
|---|
| 849 | return 0; |
|---|
| 850 | out: |
|---|
| 851 | mutex_unlock(&module_mutex); |
|---|
| 852 | return ret; |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
|---|
| 856 | { |
|---|
| 857 | struct module_use *use; |
|---|
| 858 | int printed_something = 0; |
|---|
| 859 | |
|---|
| 860 | seq_printf(m, " %u ", module_refcount(mod)); |
|---|
| 861 | |
|---|
| 862 | /* Always include a trailing , so userspace can differentiate |
|---|
| 863 | between this and the old multi-field proc format. */ |
|---|
| 864 | list_for_each_entry(use, &mod->source_list, source_list) { |
|---|
| 865 | printed_something = 1; |
|---|
| 866 | seq_printf(m, "%s,", use->source->name); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | if (mod->init != NULL && mod->exit == NULL) { |
|---|
| 870 | printed_something = 1; |
|---|
| 871 | seq_printf(m, "[permanent],"); |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | if (!printed_something) |
|---|
| 875 | seq_printf(m, "-"); |
|---|
| 876 | } |
|---|
| 877 | |
|---|
| 878 | void __symbol_put(const char *symbol) |
|---|
| 879 | { |
|---|
| 880 | struct module *owner; |
|---|
| 881 | |
|---|
| 882 | preempt_disable(); |
|---|
| 883 | if (!find_symbol(symbol, &owner, NULL, true, false)) |
|---|
| 884 | BUG(); |
|---|
| 885 | module_put(owner); |
|---|
| 886 | preempt_enable(); |
|---|
| 887 | } |
|---|
| 888 | EXPORT_SYMBOL(__symbol_put); |
|---|
| 889 | |
|---|
| 890 | /* Note this assumes addr is a function, which it currently always is. */ |
|---|
| 891 | void symbol_put_addr(void *addr) |
|---|
| 892 | { |
|---|
| 893 | struct module *modaddr; |
|---|
| 894 | unsigned long a = (unsigned long)dereference_function_descriptor(addr); |
|---|
| 895 | |
|---|
| 896 | if (core_kernel_text(a)) |
|---|
| 897 | return; |
|---|
| 898 | |
|---|
| 899 | /* module_text_address is safe here: we're supposed to have reference |
|---|
| 900 | * to module from symbol_get, so it can't go away. */ |
|---|
| 901 | modaddr = __module_text_address(a); |
|---|
| 902 | BUG_ON(!modaddr); |
|---|
| 903 | module_put(modaddr); |
|---|
| 904 | } |
|---|
| 905 | EXPORT_SYMBOL_GPL(symbol_put_addr); |
|---|
| 906 | |
|---|
| 907 | static ssize_t show_refcnt(struct module_attribute *mattr, |
|---|
| 908 | struct module_kobject *mk, char *buffer) |
|---|
| 909 | { |
|---|
| 910 | return sprintf(buffer, "%u\n", module_refcount(mk->mod)); |
|---|
| 911 | } |
|---|
| 912 | |
|---|
| 913 | static struct module_attribute refcnt = { |
|---|
| 914 | .attr = { .name = "refcnt", .mode = 0444 }, |
|---|
| 915 | .show = show_refcnt, |
|---|
| 916 | }; |
|---|
| 917 | |
|---|
| 918 | void module_put(struct module *module) |
|---|
| 919 | { |
|---|
| 920 | if (module) { |
|---|
| 921 | preempt_disable(); |
|---|
| 922 | smp_wmb(); /* see comment in module_refcount */ |
|---|
| 923 | __this_cpu_inc(module->refptr->decs); |
|---|
| 924 | |
|---|
| 925 | trace_module_put(module, _RET_IP_); |
|---|
| 926 | /* Maybe they're waiting for us to drop reference? */ |
|---|
| 927 | if (unlikely(!module_is_live(module))) |
|---|
| 928 | wake_up_process(module->waiter); |
|---|
| 929 | preempt_enable(); |
|---|
| 930 | } |
|---|
| 931 | } |
|---|
| 932 | EXPORT_SYMBOL(module_put); |
|---|
| 933 | |
|---|
| 934 | #else /* !CONFIG_MODULE_UNLOAD */ |
|---|
| 935 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
|---|
| 936 | { |
|---|
| 937 | /* We don't know the usage count, or what modules are using. */ |
|---|
| 938 | seq_printf(m, " - -"); |
|---|
| 939 | } |
|---|
| 940 | |
|---|
| 941 | static inline void module_unload_free(struct module *mod) |
|---|
| 942 | { |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| 945 | int ref_module(struct module *a, struct module *b) |
|---|
| 946 | { |
|---|
| 947 | return strong_try_module_get(b); |
|---|
| 948 | } |
|---|
| 949 | EXPORT_SYMBOL_GPL(ref_module); |
|---|
| 950 | |
|---|
| 951 | static inline int module_unload_init(struct module *mod) |
|---|
| 952 | { |
|---|
| 953 | return 0; |
|---|
| 954 | } |
|---|
| 955 | #endif /* CONFIG_MODULE_UNLOAD */ |
|---|
| 956 | |
|---|
| 957 | static ssize_t show_initstate(struct module_attribute *mattr, |
|---|
| 958 | struct module_kobject *mk, char *buffer) |
|---|
| 959 | { |
|---|
| 960 | const char *state = "unknown"; |
|---|
| 961 | |
|---|
| 962 | switch (mk->mod->state) { |
|---|
| 963 | case MODULE_STATE_LIVE: |
|---|
| 964 | state = "live"; |
|---|
| 965 | break; |
|---|
| 966 | case MODULE_STATE_COMING: |
|---|
| 967 | state = "coming"; |
|---|
| 968 | break; |
|---|
| 969 | case MODULE_STATE_GOING: |
|---|
| 970 | state = "going"; |
|---|
| 971 | break; |
|---|
| 972 | } |
|---|
| 973 | return sprintf(buffer, "%s\n", state); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | static struct module_attribute initstate = { |
|---|
| 977 | .attr = { .name = "initstate", .mode = 0444 }, |
|---|
| 978 | .show = show_initstate, |
|---|
| 979 | }; |
|---|
| 980 | |
|---|
| 981 | static ssize_t store_uevent(struct module_attribute *mattr, |
|---|
| 982 | struct module_kobject *mk, |
|---|
| 983 | const char *buffer, size_t count) |
|---|
| 984 | { |
|---|
| 985 | enum kobject_action action; |
|---|
| 986 | |
|---|
| 987 | if (kobject_action_type(buffer, count, &action) == 0) |
|---|
| 988 | kobject_uevent(&mk->kobj, action); |
|---|
| 989 | return count; |
|---|
| 990 | } |
|---|
| 991 | |
|---|
| 992 | struct module_attribute module_uevent = { |
|---|
| 993 | .attr = { .name = "uevent", .mode = 0200 }, |
|---|
| 994 | .store = store_uevent, |
|---|
| 995 | }; |
|---|
| 996 | |
|---|
| 997 | static struct module_attribute *modinfo_attrs[] = { |
|---|
| 998 | &modinfo_version, |
|---|
| 999 | &modinfo_srcversion, |
|---|
| 1000 | &initstate, |
|---|
| 1001 | &module_uevent, |
|---|
| 1002 | #ifdef CONFIG_MODULE_UNLOAD |
|---|
| 1003 | &refcnt, |
|---|
| 1004 | #endif |
|---|
| 1005 | NULL, |
|---|
| 1006 | }; |
|---|
| 1007 | |
|---|
| 1008 | static const char vermagic[] = VERMAGIC_STRING; |
|---|
| 1009 | |
|---|
| 1010 | static int try_to_force_load(struct module *mod, const char *reason) |
|---|
| 1011 | { |
|---|
| 1012 | #ifdef CONFIG_MODULE_FORCE_LOAD |
|---|
| 1013 | if (!test_taint(TAINT_FORCED_MODULE)) |
|---|
| 1014 | printk(KERN_WARNING "%s: %s: kernel tainted.\n", |
|---|
| 1015 | mod->name, reason); |
|---|
| 1016 | add_taint_module(mod, TAINT_FORCED_MODULE); |
|---|
| 1017 | return 0; |
|---|
| 1018 | #else |
|---|
| 1019 | return -ENOEXEC; |
|---|
| 1020 | #endif |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | #ifdef CONFIG_MODVERSIONS |
|---|
| 1024 | /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */ |
|---|
| 1025 | static unsigned long maybe_relocated(unsigned long crc, |
|---|
| 1026 | const struct module *crc_owner) |
|---|
| 1027 | { |
|---|
| 1028 | #ifdef ARCH_RELOCATES_KCRCTAB |
|---|
| 1029 | if (crc_owner == NULL) |
|---|
| 1030 | return crc - (unsigned long)reloc_start; |
|---|
| 1031 | #endif |
|---|
| 1032 | return crc; |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | static int check_version(Elf_Shdr *sechdrs, |
|---|
| 1036 | unsigned int versindex, |
|---|
| 1037 | const char *symname, |
|---|
| 1038 | struct module *mod, |
|---|
| 1039 | const unsigned long *crc, |
|---|
| 1040 | const struct module *crc_owner) |
|---|
| 1041 | { |
|---|
| 1042 | unsigned int i, num_versions; |
|---|
| 1043 | struct modversion_info *versions; |
|---|
| 1044 | |
|---|
| 1045 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ |
|---|
| 1046 | if (!crc) |
|---|
| 1047 | return 1; |
|---|
| 1048 | |
|---|
| 1049 | /* No versions at all? modprobe --force does this. */ |
|---|
| 1050 | if (versindex == 0) |
|---|
| 1051 | return try_to_force_load(mod, symname) == 0; |
|---|
| 1052 | |
|---|
| 1053 | versions = (void *) sechdrs[versindex].sh_addr; |
|---|
| 1054 | num_versions = sechdrs[versindex].sh_size |
|---|
| 1055 | / sizeof(struct modversion_info); |
|---|
| 1056 | |
|---|
| 1057 | for (i = 0; i < num_versions; i++) { |
|---|
| 1058 | if (strcmp(versions[i].name, symname) != 0) |
|---|
| 1059 | continue; |
|---|
| 1060 | |
|---|
| 1061 | if (versions[i].crc == maybe_relocated(*crc, crc_owner)) |
|---|
| 1062 | return 1; |
|---|
| 1063 | DEBUGP("Found checksum %lX vs module %lX\n", |
|---|
| 1064 | maybe_relocated(*crc, crc_owner), versions[i].crc); |
|---|
| 1065 | goto bad_version; |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | printk(KERN_WARNING "%s: no symbol version for %s\n", |
|---|
| 1069 | mod->name, symname); |
|---|
| 1070 | return 0; |
|---|
| 1071 | |
|---|
| 1072 | bad_version: |
|---|
| 1073 | printk("%s: disagrees about version of symbol %s\n", |
|---|
| 1074 | mod->name, symname); |
|---|
| 1075 | return 0; |
|---|
| 1076 | } |
|---|
| 1077 | |
|---|
| 1078 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
|---|
| 1079 | unsigned int versindex, |
|---|
| 1080 | struct module *mod) |
|---|
| 1081 | { |
|---|
| 1082 | const unsigned long *crc; |
|---|
| 1083 | |
|---|
| 1084 | /* Since this should be found in kernel (which can't be removed), |
|---|
| 1085 | * no locking is necessary. */ |
|---|
| 1086 | if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, |
|---|
| 1087 | &crc, true, false)) |
|---|
| 1088 | BUG(); |
|---|
| 1089 | return check_version(sechdrs, versindex, "module_layout", mod, crc, |
|---|
| 1090 | NULL); |
|---|
| 1091 | } |
|---|
| 1092 | |
|---|
| 1093 | /* First part is kernel version, which we ignore if module has crcs. */ |
|---|
| 1094 | static inline int same_magic(const char *amagic, const char *bmagic, |
|---|
| 1095 | bool has_crcs) |
|---|
| 1096 | { |
|---|
| 1097 | if (has_crcs) { |
|---|
| 1098 | amagic += strcspn(amagic, " "); |
|---|
| 1099 | bmagic += strcspn(bmagic, " "); |
|---|
| 1100 | } |
|---|
| 1101 | return strcmp(amagic, bmagic) == 0; |
|---|
| 1102 | } |
|---|
| 1103 | #else |
|---|
| 1104 | static inline int check_version(Elf_Shdr *sechdrs, |
|---|
| 1105 | unsigned int versindex, |
|---|
| 1106 | const char *symname, |
|---|
| 1107 | struct module *mod, |
|---|
| 1108 | const unsigned long *crc, |
|---|
| 1109 | const struct module *crc_owner) |
|---|
| 1110 | { |
|---|
| 1111 | return 1; |
|---|
| 1112 | } |
|---|
| 1113 | |
|---|
| 1114 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
|---|
| 1115 | unsigned int versindex, |
|---|
| 1116 | struct module *mod) |
|---|
| 1117 | { |
|---|
| 1118 | return 1; |
|---|
| 1119 | } |
|---|
| 1120 | |
|---|
| 1121 | static inline int same_magic(const char *amagic, const char *bmagic, |
|---|
| 1122 | bool has_crcs) |
|---|
| 1123 | { |
|---|
| 1124 | return strcmp(amagic, bmagic) == 0; |
|---|
| 1125 | } |
|---|
| 1126 | #endif /* CONFIG_MODVERSIONS */ |
|---|
| 1127 | |
|---|
| 1128 | /* Resolve a symbol for this module. I.e. if we find one, record usage. */ |
|---|
| 1129 | static const struct kernel_symbol *resolve_symbol(struct module *mod, |
|---|
| 1130 | const struct load_info *info, |
|---|
| 1131 | const char *name, |
|---|
| 1132 | char ownername[]) |
|---|
| 1133 | { |
|---|
| 1134 | struct module *owner; |
|---|
| 1135 | const struct kernel_symbol *sym; |
|---|
| 1136 | const unsigned long *crc; |
|---|
| 1137 | int err; |
|---|
| 1138 | |
|---|
| 1139 | mutex_lock(&module_mutex); |
|---|
| 1140 | sym = find_symbol(name, &owner, &crc, |
|---|
| 1141 | !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); |
|---|
| 1142 | if (!sym) |
|---|
| 1143 | goto unlock; |
|---|
| 1144 | |
|---|
| 1145 | if (!check_version(info->sechdrs, info->index.vers, name, mod, crc, |
|---|
| 1146 | owner)) { |
|---|
| 1147 | sym = ERR_PTR(-EINVAL); |
|---|
| 1148 | goto getname; |
|---|
| 1149 | } |
|---|
| 1150 | |
|---|
| 1151 | err = ref_module(mod, owner); |
|---|
| 1152 | if (err) { |
|---|
| 1153 | sym = ERR_PTR(err); |
|---|
| 1154 | goto getname; |
|---|
| 1155 | } |
|---|
| 1156 | |
|---|
| 1157 | getname: |
|---|
| 1158 | /* We must make copy under the lock if we failed to get ref. */ |
|---|
| 1159 | strncpy(ownername, module_name(owner), MODULE_NAME_LEN); |
|---|
| 1160 | unlock: |
|---|
| 1161 | mutex_unlock(&module_mutex); |
|---|
| 1162 | return sym; |
|---|
| 1163 | } |
|---|
| 1164 | |
|---|
| 1165 | static const struct kernel_symbol * |
|---|
| 1166 | resolve_symbol_wait(struct module *mod, |
|---|
| 1167 | const struct load_info *info, |
|---|
| 1168 | const char *name) |
|---|
| 1169 | { |
|---|
| 1170 | const struct kernel_symbol *ksym; |
|---|
| 1171 | char owner[MODULE_NAME_LEN]; |
|---|
| 1172 | |
|---|
| 1173 | if (wait_event_interruptible_timeout(module_wq, |
|---|
| 1174 | !IS_ERR(ksym = resolve_symbol(mod, info, name, owner)) |
|---|
| 1175 | || PTR_ERR(ksym) != -EBUSY, |
|---|
| 1176 | 30 * HZ) <= 0) { |
|---|
| 1177 | printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n", |
|---|
| 1178 | mod->name, owner); |
|---|
| 1179 | } |
|---|
| 1180 | return ksym; |
|---|
| 1181 | } |
|---|
| 1182 | |
|---|
| 1183 | /* |
|---|
| 1184 | * /sys/module/foo/sections stuff |
|---|
| 1185 | * J. Corbet <corbet@lwn.net> |
|---|
| 1186 | */ |
|---|
| 1187 | #ifdef CONFIG_SYSFS |
|---|
| 1188 | |
|---|
| 1189 | #ifdef CONFIG_KALLSYMS |
|---|
| 1190 | static inline bool sect_empty(const Elf_Shdr *sect) |
|---|
| 1191 | { |
|---|
| 1192 | return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0; |
|---|
| 1193 | } |
|---|
| 1194 | |
|---|
| 1195 | struct module_sect_attr |
|---|
| 1196 | { |
|---|
| 1197 | struct module_attribute mattr; |
|---|
| 1198 | char *name; |
|---|
| 1199 | unsigned long address; |
|---|
| 1200 | }; |
|---|
| 1201 | |
|---|
| 1202 | struct module_sect_attrs |
|---|
| 1203 | { |
|---|
| 1204 | struct attribute_group grp; |
|---|
| 1205 | unsigned int nsections; |
|---|
| 1206 | struct module_sect_attr attrs[0]; |
|---|
| 1207 | }; |
|---|
| 1208 | |
|---|
| 1209 | static ssize_t module_sect_show(struct module_attribute *mattr, |
|---|
| 1210 | struct module_kobject *mk, char *buf) |
|---|
| 1211 | { |
|---|
| 1212 | struct module_sect_attr *sattr = |
|---|
| 1213 | container_of(mattr, struct module_sect_attr, mattr); |
|---|
| 1214 | return sprintf(buf, "0x%pK\n", (void *)sattr->address); |
|---|
| 1215 | } |
|---|
| 1216 | |
|---|
| 1217 | static void free_sect_attrs(struct module_sect_attrs *sect_attrs) |
|---|
| 1218 | { |
|---|
| 1219 | unsigned int section; |
|---|
| 1220 | |
|---|
| 1221 | for (section = 0; section < sect_attrs->nsections; section++) |
|---|
| 1222 | kfree(sect_attrs->attrs[section].name); |
|---|
| 1223 | kfree(sect_attrs); |
|---|
| 1224 | } |
|---|
| 1225 | |
|---|
| 1226 | static void add_sect_attrs(struct module *mod, const struct load_info *info) |
|---|
| 1227 | { |
|---|
| 1228 | unsigned int nloaded = 0, i, size[2]; |
|---|
| 1229 | struct module_sect_attrs *sect_attrs; |
|---|
| 1230 | struct module_sect_attr *sattr; |
|---|
| 1231 | struct attribute **gattr; |
|---|
| 1232 | |
|---|
| 1233 | /* Count loaded sections and allocate structures */ |
|---|
| 1234 | for (i = 0; i < info->hdr->e_shnum; i++) |
|---|
| 1235 | if (!sect_empty(&info->sechdrs[i])) |
|---|
| 1236 | nloaded++; |
|---|
| 1237 | size[0] = ALIGN(sizeof(*sect_attrs) |
|---|
| 1238 | + nloaded * sizeof(sect_attrs->attrs[0]), |
|---|
| 1239 | sizeof(sect_attrs->grp.attrs[0])); |
|---|
| 1240 | size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); |
|---|
| 1241 | sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); |
|---|
| 1242 | if (sect_attrs == NULL) |
|---|
| 1243 | return; |
|---|
| 1244 | |
|---|
| 1245 | /* Setup section attributes. */ |
|---|
| 1246 | sect_attrs->grp.name = "sections"; |
|---|
| 1247 | sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; |
|---|
| 1248 | |
|---|
| 1249 | sect_attrs->nsections = 0; |
|---|
| 1250 | sattr = §_attrs->attrs[0]; |
|---|
| 1251 | gattr = §_attrs->grp.attrs[0]; |
|---|
| 1252 | for (i = 0; i < info->hdr->e_shnum; i++) { |
|---|
| 1253 | Elf_Shdr *sec = &info->sechdrs[i]; |
|---|
| 1254 | if (sect_empty(sec)) |
|---|
| 1255 | continue; |
|---|
| 1256 | sattr->address = sec->sh_addr; |
|---|
| 1257 | sattr->name = kstrdup(info->secstrings + sec->sh_name, |
|---|
| 1258 | GFP_KERNEL); |
|---|
| 1259 | if (sattr->name == NULL) |
|---|
| 1260 | goto out; |
|---|
| 1261 | sect_attrs->nsections++; |
|---|
| 1262 | sysfs_attr_init(&sattr->mattr.attr); |
|---|
| 1263 | sattr->mattr.show = module_sect_show; |
|---|
| 1264 | sattr->mattr.store = NULL; |
|---|
| 1265 | sattr->mattr.attr.name = sattr->name; |
|---|
| 1266 | sattr->mattr.attr.mode = S_IRUGO; |
|---|
| 1267 | *(gattr++) = &(sattr++)->mattr.attr; |
|---|
| 1268 | } |
|---|
| 1269 | *gattr = NULL; |
|---|
| 1270 | |
|---|
| 1271 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) |
|---|
| 1272 | goto out; |
|---|
| 1273 | |
|---|
| 1274 | mod->sect_attrs = sect_attrs; |
|---|
| 1275 | return; |
|---|
| 1276 | out: |
|---|
| 1277 | free_sect_attrs(sect_attrs); |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | static void remove_sect_attrs(struct module *mod) |
|---|
| 1281 | { |
|---|
| 1282 | if (mod->sect_attrs) { |
|---|
| 1283 | sysfs_remove_group(&mod->mkobj.kobj, |
|---|
| 1284 | &mod->sect_attrs->grp); |
|---|
| 1285 | /* We are positive that no one is using any sect attrs |
|---|
| 1286 | * at this point. Deallocate immediately. */ |
|---|
| 1287 | free_sect_attrs(mod->sect_attrs); |
|---|
| 1288 | mod->sect_attrs = NULL; |
|---|
| 1289 | } |
|---|
| 1290 | } |
|---|
| 1291 | |
|---|
| 1292 | /* |
|---|
| 1293 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. |
|---|
| 1294 | */ |
|---|
| 1295 | |
|---|
| 1296 | struct module_notes_attrs { |
|---|
| 1297 | struct kobject *dir; |
|---|
| 1298 | unsigned int notes; |
|---|
| 1299 | struct bin_attribute attrs[0]; |
|---|
| 1300 | }; |
|---|
| 1301 | |
|---|
| 1302 | static ssize_t module_notes_read(struct file *filp, struct kobject *kobj, |
|---|
| 1303 | struct bin_attribute *bin_attr, |
|---|
| 1304 | char *buf, loff_t pos, size_t count) |
|---|
| 1305 | { |
|---|
| 1306 | /* |
|---|
| 1307 | * The caller checked the pos and count against our size. |
|---|
| 1308 | */ |
|---|
| 1309 | memcpy(buf, bin_attr->private + pos, count); |
|---|
| 1310 | return count; |
|---|
| 1311 | } |
|---|
| 1312 | |
|---|
| 1313 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs, |
|---|
| 1314 | unsigned int i) |
|---|
| 1315 | { |
|---|
| 1316 | if (notes_attrs->dir) { |
|---|
| 1317 | while (i-- > 0) |
|---|
| 1318 | sysfs_remove_bin_file(notes_attrs->dir, |
|---|
| 1319 | ¬es_attrs->attrs[i]); |
|---|
| 1320 | kobject_put(notes_attrs->dir); |
|---|
| 1321 | } |
|---|
| 1322 | kfree(notes_attrs); |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | static void add_notes_attrs(struct module *mod, const struct load_info *info) |
|---|
| 1326 | { |
|---|
| 1327 | unsigned int notes, loaded, i; |
|---|
| 1328 | struct module_notes_attrs *notes_attrs; |
|---|
| 1329 | struct bin_attribute *nattr; |
|---|
| 1330 | |
|---|
| 1331 | /* failed to create section attributes, so can't create notes */ |
|---|
| 1332 | if (!mod->sect_attrs) |
|---|
| 1333 | return; |
|---|
| 1334 | |
|---|
| 1335 | /* Count notes sections and allocate structures. */ |
|---|
| 1336 | notes = 0; |
|---|
| 1337 | for (i = 0; i < info->hdr->e_shnum; i++) |
|---|
| 1338 | if (!sect_empty(&info->sechdrs[i]) && |
|---|
| 1339 | (info->sechdrs[i].sh_type == SHT_NOTE)) |
|---|
| 1340 | ++notes; |
|---|
| 1341 | |
|---|
| 1342 | if (notes == 0) |
|---|
| 1343 | return; |
|---|
| 1344 | |
|---|
| 1345 | notes_attrs = kzalloc(sizeof(*notes_attrs) |
|---|
| 1346 | + notes * sizeof(notes_attrs->attrs[0]), |
|---|
| 1347 | GFP_KERNEL); |
|---|
| 1348 | if (notes_attrs == NULL) |
|---|
| 1349 | return; |
|---|
| 1350 | |
|---|
| 1351 | notes_attrs->notes = notes; |
|---|
| 1352 | nattr = ¬es_attrs->attrs[0]; |
|---|
| 1353 | for (loaded = i = 0; i < info->hdr->e_shnum; ++i) { |
|---|
| 1354 | if (sect_empty(&info->sechdrs[i])) |
|---|
| 1355 | continue; |
|---|
| 1356 | if (info->sechdrs[i].sh_type == SHT_NOTE) { |
|---|
| 1357 | sysfs_bin_attr_init(nattr); |
|---|
| 1358 | nattr->attr.name = mod->sect_attrs->attrs[loaded].name; |
|---|
| 1359 | nattr->attr.mode = S_IRUGO; |
|---|
| 1360 | nattr->size = info->sechdrs[i].sh_size; |
|---|
| 1361 | nattr->private = (void *) info->sechdrs[i].sh_addr; |
|---|
| 1362 | nattr->read = module_notes_read; |
|---|
| 1363 | ++nattr; |
|---|
| 1364 | } |
|---|
| 1365 | ++loaded; |
|---|
| 1366 | } |
|---|
| 1367 | |
|---|
| 1368 | notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj); |
|---|
| 1369 | if (!notes_attrs->dir) |
|---|
| 1370 | goto out; |
|---|
| 1371 | |
|---|
| 1372 | for (i = 0; i < notes; ++i) |
|---|
| 1373 | if (sysfs_create_bin_file(notes_attrs->dir, |
|---|
| 1374 | ¬es_attrs->attrs[i])) |
|---|
| 1375 | goto out; |
|---|
| 1376 | |
|---|
| 1377 | mod->notes_attrs = notes_attrs; |
|---|
| 1378 | return; |
|---|
| 1379 | |
|---|
| 1380 | out: |
|---|
| 1381 | free_notes_attrs(notes_attrs, i); |
|---|
| 1382 | } |
|---|
| 1383 | |
|---|
| 1384 | static void remove_notes_attrs(struct module *mod) |
|---|
| 1385 | { |
|---|
| 1386 | if (mod->notes_attrs) |
|---|
| 1387 | free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | #else |
|---|
| 1391 | |
|---|
| 1392 | static inline void add_sect_attrs(struct module *mod, |
|---|
| 1393 | const struct load_info *info) |
|---|
| 1394 | { |
|---|
| 1395 | } |
|---|
| 1396 | |
|---|
| 1397 | static inline void remove_sect_attrs(struct module *mod) |
|---|
| 1398 | { |
|---|
| 1399 | } |
|---|
| 1400 | |
|---|
| 1401 | static inline void add_notes_attrs(struct module *mod, |
|---|
| 1402 | const struct load_info *info) |
|---|
| 1403 | { |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | static inline void remove_notes_attrs(struct module *mod) |
|---|
| 1407 | { |
|---|
| 1408 | } |
|---|
| 1409 | #endif /* CONFIG_KALLSYMS */ |
|---|
| 1410 | |
|---|
| 1411 | static void add_usage_links(struct module *mod) |
|---|
| 1412 | { |
|---|
| 1413 | #ifdef CONFIG_MODULE_UNLOAD |
|---|
| 1414 | struct module_use *use; |
|---|
| 1415 | int nowarn; |
|---|
| 1416 | |
|---|
| 1417 | mutex_lock(&module_mutex); |
|---|
| 1418 | list_for_each_entry(use, &mod->target_list, target_list) { |
|---|
| 1419 | nowarn = sysfs_create_link(use->target->holders_dir, |
|---|
| 1420 | &mod->mkobj.kobj, mod->name); |
|---|
| 1421 | } |
|---|
| 1422 | mutex_unlock(&module_mutex); |
|---|
| 1423 | #endif |
|---|
| 1424 | } |
|---|
| 1425 | |
|---|
| 1426 | static void del_usage_links(struct module *mod) |
|---|
| 1427 | { |
|---|
| 1428 | #ifdef CONFIG_MODULE_UNLOAD |
|---|
| 1429 | struct module_use *use; |
|---|
| 1430 | |
|---|
| 1431 | mutex_lock(&module_mutex); |
|---|
| 1432 | list_for_each_entry(use, &mod->target_list, target_list) |
|---|
| 1433 | sysfs_remove_link(use->target->holders_dir, mod->name); |
|---|
| 1434 | mutex_unlock(&module_mutex); |
|---|
| 1435 | #endif |
|---|
| 1436 | } |
|---|
| 1437 | |
|---|
| 1438 | static int module_add_modinfo_attrs(struct module *mod) |
|---|
| 1439 | { |
|---|
| 1440 | struct module_attribute *attr; |
|---|
| 1441 | struct module_attribute *temp_attr; |
|---|
| 1442 | int error = 0; |
|---|
| 1443 | int i; |
|---|
| 1444 | |
|---|
| 1445 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * |
|---|
| 1446 | (ARRAY_SIZE(modinfo_attrs) + 1)), |
|---|
| 1447 | GFP_KERNEL); |
|---|
| 1448 | if (!mod->modinfo_attrs) |
|---|
| 1449 | return -ENOMEM; |
|---|
| 1450 | |
|---|
| 1451 | temp_attr = mod->modinfo_attrs; |
|---|
| 1452 | for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { |
|---|
| 1453 | if (!attr->test || |
|---|
| 1454 | (attr->test && attr->test(mod))) { |
|---|
| 1455 | memcpy(temp_attr, attr, sizeof(*temp_attr)); |
|---|
| 1456 | sysfs_attr_init(&temp_attr->attr); |
|---|
| 1457 | error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); |
|---|
| 1458 | ++temp_attr; |
|---|
| 1459 | } |
|---|
| 1460 | } |
|---|
| 1461 | return error; |
|---|
| 1462 | } |
|---|
| 1463 | |
|---|
| 1464 | static void module_remove_modinfo_attrs(struct module *mod) |
|---|
| 1465 | { |
|---|
| 1466 | struct module_attribute *attr; |
|---|
| 1467 | int i; |
|---|
| 1468 | |
|---|
| 1469 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { |
|---|
| 1470 | /* pick a field to test for end of list */ |
|---|
| 1471 | if (!attr->attr.name) |
|---|
| 1472 | break; |
|---|
| 1473 | sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); |
|---|
| 1474 | if (attr->free) |
|---|
| 1475 | attr->free(mod); |
|---|
| 1476 | } |
|---|
| 1477 | kfree(mod->modinfo_attrs); |
|---|
| 1478 | } |
|---|
| 1479 | |
|---|
| 1480 | static int mod_sysfs_init(struct module *mod) |
|---|
| 1481 | { |
|---|
| 1482 | int err; |
|---|
| 1483 | struct kobject *kobj; |
|---|
| 1484 | |
|---|
| 1485 | if (!module_sysfs_initialized) { |
|---|
| 1486 | printk(KERN_ERR "%s: module sysfs not initialized\n", |
|---|
| 1487 | mod->name); |
|---|
| 1488 | err = -EINVAL; |
|---|
| 1489 | goto out; |
|---|
| 1490 | } |
|---|
| 1491 | |
|---|
| 1492 | kobj = kset_find_obj(module_kset, mod->name); |
|---|
| 1493 | if (kobj) { |
|---|
| 1494 | printk(KERN_ERR "%s: module is already loaded\n", mod->name); |
|---|
| 1495 | kobject_put(kobj); |
|---|
| 1496 | err = -EINVAL; |
|---|
| 1497 | goto out; |
|---|
| 1498 | } |
|---|
| 1499 | |
|---|
| 1500 | mod->mkobj.mod = mod; |
|---|
| 1501 | |
|---|
| 1502 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); |
|---|
| 1503 | mod->mkobj.kobj.kset = module_kset; |
|---|
| 1504 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, |
|---|
| 1505 | "%s", mod->name); |
|---|
| 1506 | if (err) |
|---|
| 1507 | kobject_put(&mod->mkobj.kobj); |
|---|
| 1508 | |
|---|
| 1509 | /* delay uevent until full sysfs population */ |
|---|
| 1510 | out: |
|---|
| 1511 | return err; |
|---|
| 1512 | } |
|---|
| 1513 | |
|---|
| 1514 | static int mod_sysfs_setup(struct module *mod, |
|---|
| 1515 | const struct load_info *info, |
|---|
| 1516 | struct kernel_param *kparam, |
|---|
| 1517 | unsigned int num_params) |
|---|
| 1518 | { |
|---|
| 1519 | int err; |
|---|
| 1520 | |
|---|
| 1521 | err = mod_sysfs_init(mod); |
|---|
| 1522 | if (err) |
|---|
| 1523 | goto out; |
|---|
| 1524 | |
|---|
| 1525 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); |
|---|
| 1526 | if (!mod->holders_dir) { |
|---|
| 1527 | err = -ENOMEM; |
|---|
| 1528 | goto out_unreg; |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | err = module_param_sysfs_setup(mod, kparam, num_params); |
|---|
| 1532 | if (err) |
|---|
| 1533 | goto out_unreg_holders; |
|---|
| 1534 | |
|---|
| 1535 | err = module_add_modinfo_attrs(mod); |
|---|
| 1536 | if (err) |
|---|
| 1537 | goto out_unreg_param; |
|---|
| 1538 | |
|---|
| 1539 | add_usage_links(mod); |
|---|
| 1540 | add_sect_attrs(mod, info); |
|---|
| 1541 | add_notes_attrs(mod, info); |
|---|
| 1542 | |
|---|
| 1543 | kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); |
|---|
| 1544 | return 0; |
|---|
| 1545 | |
|---|
| 1546 | out_unreg_param: |
|---|
| 1547 | module_param_sysfs_remove(mod); |
|---|
| 1548 | out_unreg_holders: |
|---|
| 1549 | kobject_put(mod->holders_dir); |
|---|
| 1550 | out_unreg: |
|---|
| 1551 | kobject_put(&mod->mkobj.kobj); |
|---|
| 1552 | out: |
|---|
| 1553 | return err; |
|---|
| 1554 | } |
|---|
| 1555 | |
|---|
| 1556 | static void mod_sysfs_fini(struct module *mod) |
|---|
| 1557 | { |
|---|
| 1558 | remove_notes_attrs(mod); |
|---|
| 1559 | remove_sect_attrs(mod); |
|---|
| 1560 | kobject_put(&mod->mkobj.kobj); |
|---|
| 1561 | } |
|---|
| 1562 | |
|---|
| 1563 | #else /* !CONFIG_SYSFS */ |
|---|
| 1564 | |
|---|
| 1565 | static int mod_sysfs_setup(struct module *mod, |
|---|
| 1566 | const struct load_info *info, |
|---|
| 1567 | struct kernel_param *kparam, |
|---|
| 1568 | unsigned int num_params) |
|---|
| 1569 | { |
|---|
| 1570 | return 0; |
|---|
| 1571 | } |
|---|
| 1572 | |
|---|
| 1573 | static void mod_sysfs_fini(struct module *mod) |
|---|
| 1574 | { |
|---|
| 1575 | } |
|---|
| 1576 | |
|---|
| 1577 | static void module_remove_modinfo_attrs(struct module *mod) |
|---|
| 1578 | { |
|---|
| 1579 | } |
|---|
| 1580 | |
|---|
| 1581 | static void del_usage_links(struct module *mod) |
|---|
| 1582 | { |
|---|
| 1583 | } |
|---|
| 1584 | |
|---|
| 1585 | #endif /* CONFIG_SYSFS */ |
|---|
| 1586 | |
|---|
| 1587 | static void mod_sysfs_teardown(struct module *mod) |
|---|
| 1588 | { |
|---|
| 1589 | del_usage_links(mod); |
|---|
| 1590 | module_remove_modinfo_attrs(mod); |
|---|
| 1591 | module_param_sysfs_remove(mod); |
|---|
| 1592 | kobject_put(mod->mkobj.drivers_dir); |
|---|
| 1593 | kobject_put(mod->holders_dir); |
|---|
| 1594 | mod_sysfs_fini(mod); |
|---|
| 1595 | } |
|---|
| 1596 | |
|---|
| 1597 | /* |
|---|
| 1598 | * unlink the module with the whole machine is stopped with interrupts off |
|---|
| 1599 | * - this defends against kallsyms not taking locks |
|---|
| 1600 | */ |
|---|
| 1601 | static int __unlink_module(void *_mod) |
|---|
| 1602 | { |
|---|
| 1603 | struct module *mod = _mod; |
|---|
| 1604 | list_del(&mod->list); |
|---|
| 1605 | module_bug_cleanup(mod); |
|---|
| 1606 | return 0; |
|---|
| 1607 | } |
|---|
| 1608 | |
|---|
| 1609 | #ifdef CONFIG_DEBUG_SET_MODULE_RONX |
|---|
| 1610 | /* |
|---|
| 1611 | * LKM RO/NX protection: protect module's text/ro-data |
|---|
| 1612 | * from modification and any data from execution. |
|---|
| 1613 | */ |
|---|
| 1614 | void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages)) |
|---|
| 1615 | { |
|---|
| 1616 | unsigned long begin_pfn = PFN_DOWN((unsigned long)start); |
|---|
| 1617 | unsigned long end_pfn = PFN_DOWN((unsigned long)end); |
|---|
| 1618 | |
|---|
| 1619 | if (end_pfn > begin_pfn) |
|---|
| 1620 | set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn); |
|---|
| 1621 | } |
|---|
| 1622 | |
|---|
| 1623 | static void set_section_ro_nx(void *base, |
|---|
| 1624 | unsigned long text_size, |
|---|
| 1625 | unsigned long ro_size, |
|---|
| 1626 | unsigned long total_size) |
|---|
| 1627 | { |
|---|
| 1628 | /* begin and end PFNs of the current subsection */ |
|---|
| 1629 | unsigned long begin_pfn; |
|---|
| 1630 | unsigned long end_pfn; |
|---|
| 1631 | |
|---|
| 1632 | /* |
|---|
| 1633 | * Set RO for module text and RO-data: |
|---|
| 1634 | * - Always protect first page. |
|---|
| 1635 | * - Do not protect last partial page. |
|---|
| 1636 | */ |
|---|
| 1637 | if (ro_size > 0) |
|---|
| 1638 | set_page_attributes(base, base + ro_size, set_memory_ro); |
|---|
| 1639 | |
|---|
| 1640 | /* |
|---|
| 1641 | * Set NX permissions for module data: |
|---|
| 1642 | * - Do not protect first partial page. |
|---|
| 1643 | * - Always protect last page. |
|---|
| 1644 | */ |
|---|
| 1645 | if (total_size > text_size) { |
|---|
| 1646 | begin_pfn = PFN_UP((unsigned long)base + text_size); |
|---|
| 1647 | end_pfn = PFN_UP((unsigned long)base + total_size); |
|---|
| 1648 | if (end_pfn > begin_pfn) |
|---|
| 1649 | set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn); |
|---|
| 1650 | } |
|---|
| 1651 | } |
|---|
| 1652 | |
|---|
| 1653 | static void unset_module_core_ro_nx(struct module *mod) |
|---|
| 1654 | { |
|---|
| 1655 | set_page_attributes(mod->module_core + mod->core_text_size, |
|---|
| 1656 | mod->module_core + mod->core_size, |
|---|
| 1657 | set_memory_x); |
|---|
| 1658 | set_page_attributes(mod->module_core, |
|---|
| 1659 | mod->module_core + mod->core_ro_size, |
|---|
| 1660 | set_memory_rw); |
|---|
| 1661 | } |
|---|
| 1662 | |
|---|
| 1663 | static void unset_module_init_ro_nx(struct module *mod) |
|---|
| 1664 | { |
|---|
| 1665 | set_page_attributes(mod->module_init + mod->init_text_size, |
|---|
| 1666 | mod->module_init + mod->init_size, |
|---|
| 1667 | set_memory_x); |
|---|
| 1668 | set_page_attributes(mod->module_init, |
|---|
| 1669 | mod->module_init + mod->init_ro_size, |
|---|
| 1670 | set_memory_rw); |
|---|
| 1671 | } |
|---|
| 1672 | |
|---|
| 1673 | /* Iterate through all modules and set each module's text as RW */ |
|---|
| 1674 | void set_all_modules_text_rw(void) |
|---|
| 1675 | { |
|---|
| 1676 | struct module *mod; |
|---|
| 1677 | |
|---|
| 1678 | mutex_lock(&module_mutex); |
|---|
| 1679 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 1680 | if ((mod->module_core) && (mod->core_text_size)) { |
|---|
| 1681 | set_page_attributes(mod->module_core, |
|---|
| 1682 | mod->module_core + mod->core_text_size, |
|---|
| 1683 | set_memory_rw); |
|---|
| 1684 | } |
|---|
| 1685 | if ((mod->module_init) && (mod->init_text_size)) { |
|---|
| 1686 | set_page_attributes(mod->module_init, |
|---|
| 1687 | mod->module_init + mod->init_text_size, |
|---|
| 1688 | set_memory_rw); |
|---|
| 1689 | } |
|---|
| 1690 | } |
|---|
| 1691 | mutex_unlock(&module_mutex); |
|---|
| 1692 | } |
|---|
| 1693 | |
|---|
| 1694 | /* Iterate through all modules and set each module's text as RO */ |
|---|
| 1695 | void set_all_modules_text_ro(void) |
|---|
| 1696 | { |
|---|
| 1697 | struct module *mod; |
|---|
| 1698 | |
|---|
| 1699 | mutex_lock(&module_mutex); |
|---|
| 1700 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 1701 | if ((mod->module_core) && (mod->core_text_size)) { |
|---|
| 1702 | set_page_attributes(mod->module_core, |
|---|
| 1703 | mod->module_core + mod->core_text_size, |
|---|
| 1704 | set_memory_ro); |
|---|
| 1705 | } |
|---|
| 1706 | if ((mod->module_init) && (mod->init_text_size)) { |
|---|
| 1707 | set_page_attributes(mod->module_init, |
|---|
| 1708 | mod->module_init + mod->init_text_size, |
|---|
| 1709 | set_memory_ro); |
|---|
| 1710 | } |
|---|
| 1711 | } |
|---|
| 1712 | mutex_unlock(&module_mutex); |
|---|
| 1713 | } |
|---|
| 1714 | #else |
|---|
| 1715 | static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { } |
|---|
| 1716 | static void unset_module_core_ro_nx(struct module *mod) { } |
|---|
| 1717 | static void unset_module_init_ro_nx(struct module *mod) { } |
|---|
| 1718 | #endif |
|---|
| 1719 | |
|---|
| 1720 | void __weak module_free(struct module *mod, void *module_region) |
|---|
| 1721 | { |
|---|
| 1722 | vfree(module_region); |
|---|
| 1723 | } |
|---|
| 1724 | |
|---|
| 1725 | void __weak module_arch_cleanup(struct module *mod) |
|---|
| 1726 | { |
|---|
| 1727 | } |
|---|
| 1728 | |
|---|
| 1729 | /* Free a module, remove from lists, etc. */ |
|---|
| 1730 | static void free_module(struct module *mod) |
|---|
| 1731 | { |
|---|
| 1732 | trace_module_free(mod); |
|---|
| 1733 | |
|---|
| 1734 | /* Delete from various lists */ |
|---|
| 1735 | mutex_lock(&module_mutex); |
|---|
| 1736 | stop_machine(__unlink_module, mod, NULL); |
|---|
| 1737 | mutex_unlock(&module_mutex); |
|---|
| 1738 | mod_sysfs_teardown(mod); |
|---|
| 1739 | |
|---|
| 1740 | /* Remove dynamic debug info */ |
|---|
| 1741 | ddebug_remove_module(mod->name); |
|---|
| 1742 | |
|---|
| 1743 | /* Arch-specific cleanup. */ |
|---|
| 1744 | module_arch_cleanup(mod); |
|---|
| 1745 | |
|---|
| 1746 | /* Module unload stuff */ |
|---|
| 1747 | module_unload_free(mod); |
|---|
| 1748 | |
|---|
| 1749 | /* Free any allocated parameters. */ |
|---|
| 1750 | destroy_params(mod->kp, mod->num_kp); |
|---|
| 1751 | |
|---|
| 1752 | /* This may be NULL, but that's OK */ |
|---|
| 1753 | unset_module_init_ro_nx(mod); |
|---|
| 1754 | module_free(mod, mod->module_init); |
|---|
| 1755 | kfree(mod->args); |
|---|
| 1756 | percpu_modfree(mod); |
|---|
| 1757 | |
|---|
| 1758 | /* Free lock-classes: */ |
|---|
| 1759 | lockdep_free_key_range(mod->module_core, mod->core_size); |
|---|
| 1760 | |
|---|
| 1761 | /* Finally, free the core (containing the module structure) */ |
|---|
| 1762 | unset_module_core_ro_nx(mod); |
|---|
| 1763 | module_free(mod, mod->module_core); |
|---|
| 1764 | |
|---|
| 1765 | #ifdef CONFIG_MPU |
|---|
| 1766 | update_protections(current->mm); |
|---|
| 1767 | #endif |
|---|
| 1768 | } |
|---|
| 1769 | |
|---|
| 1770 | void *__symbol_get(const char *symbol) |
|---|
| 1771 | { |
|---|
| 1772 | struct module *owner; |
|---|
| 1773 | const struct kernel_symbol *sym; |
|---|
| 1774 | |
|---|
| 1775 | preempt_disable(); |
|---|
| 1776 | sym = find_symbol(symbol, &owner, NULL, true, true); |
|---|
| 1777 | if (sym && strong_try_module_get(owner)) |
|---|
| 1778 | sym = NULL; |
|---|
| 1779 | preempt_enable(); |
|---|
| 1780 | |
|---|
| 1781 | return sym ? (void *)sym->value : NULL; |
|---|
| 1782 | } |
|---|
| 1783 | EXPORT_SYMBOL_GPL(__symbol_get); |
|---|
| 1784 | |
|---|
| 1785 | /* |
|---|
| 1786 | * Ensure that an exported symbol [global namespace] does not already exist |
|---|
| 1787 | * in the kernel or in some other module's exported symbol table. |
|---|
| 1788 | * |
|---|
| 1789 | * You must hold the module_mutex. |
|---|
| 1790 | */ |
|---|
| 1791 | static int verify_export_symbols(struct module *mod) |
|---|
| 1792 | { |
|---|
| 1793 | unsigned int i; |
|---|
| 1794 | struct module *owner; |
|---|
| 1795 | const struct kernel_symbol *s; |
|---|
| 1796 | struct { |
|---|
| 1797 | const struct kernel_symbol *sym; |
|---|
| 1798 | unsigned int num; |
|---|
| 1799 | } arr[] = { |
|---|
| 1800 | { mod->syms, mod->num_syms }, |
|---|
| 1801 | { mod->gpl_syms, mod->num_gpl_syms }, |
|---|
| 1802 | { mod->gpl_future_syms, mod->num_gpl_future_syms }, |
|---|
| 1803 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 1804 | { mod->unused_syms, mod->num_unused_syms }, |
|---|
| 1805 | { mod->unused_gpl_syms, mod->num_unused_gpl_syms }, |
|---|
| 1806 | #endif |
|---|
| 1807 | }; |
|---|
| 1808 | |
|---|
| 1809 | for (i = 0; i < ARRAY_SIZE(arr); i++) { |
|---|
| 1810 | for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { |
|---|
| 1811 | if (find_symbol(s->name, &owner, NULL, true, false)) { |
|---|
| 1812 | printk(KERN_ERR |
|---|
| 1813 | "%s: exports duplicate symbol %s" |
|---|
| 1814 | " (owned by %s)\n", |
|---|
| 1815 | mod->name, s->name, module_name(owner)); |
|---|
| 1816 | return -ENOEXEC; |
|---|
| 1817 | } |
|---|
| 1818 | } |
|---|
| 1819 | } |
|---|
| 1820 | return 0; |
|---|
| 1821 | } |
|---|
| 1822 | |
|---|
| 1823 | /* Change all symbols so that st_value encodes the pointer directly. */ |
|---|
| 1824 | static int simplify_symbols(struct module *mod, const struct load_info *info) |
|---|
| 1825 | { |
|---|
| 1826 | Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; |
|---|
| 1827 | Elf_Sym *sym = (void *)symsec->sh_addr; |
|---|
| 1828 | unsigned long secbase; |
|---|
| 1829 | unsigned int i; |
|---|
| 1830 | int ret = 0; |
|---|
| 1831 | const struct kernel_symbol *ksym; |
|---|
| 1832 | |
|---|
| 1833 | for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) { |
|---|
| 1834 | const char *name = info->strtab + sym[i].st_name; |
|---|
| 1835 | |
|---|
| 1836 | switch (sym[i].st_shndx) { |
|---|
| 1837 | case SHN_COMMON: |
|---|
| 1838 | /* We compiled with -fno-common. These are not |
|---|
| 1839 | supposed to happen. */ |
|---|
| 1840 | DEBUGP("Common symbol: %s\n", name); |
|---|
| 1841 | printk("%s: please compile with -fno-common\n", |
|---|
| 1842 | mod->name); |
|---|
| 1843 | ret = -ENOEXEC; |
|---|
| 1844 | break; |
|---|
| 1845 | |
|---|
| 1846 | case SHN_ABS: |
|---|
| 1847 | /* Don't need to do anything */ |
|---|
| 1848 | DEBUGP("Absolute symbol: 0x%08lx\n", |
|---|
| 1849 | (long)sym[i].st_value); |
|---|
| 1850 | break; |
|---|
| 1851 | |
|---|
| 1852 | case SHN_UNDEF: |
|---|
| 1853 | ksym = resolve_symbol_wait(mod, info, name); |
|---|
| 1854 | /* Ok if resolved. */ |
|---|
| 1855 | if (ksym && !IS_ERR(ksym)) { |
|---|
| 1856 | sym[i].st_value = ksym->value; |
|---|
| 1857 | break; |
|---|
| 1858 | } |
|---|
| 1859 | |
|---|
| 1860 | /* Ok if weak. */ |
|---|
| 1861 | if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK) |
|---|
| 1862 | break; |
|---|
| 1863 | |
|---|
| 1864 | printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n", |
|---|
| 1865 | mod->name, name, PTR_ERR(ksym)); |
|---|
| 1866 | ret = PTR_ERR(ksym) ?: -ENOENT; |
|---|
| 1867 | break; |
|---|
| 1868 | |
|---|
| 1869 | default: |
|---|
| 1870 | /* Divert to percpu allocation if a percpu var. */ |
|---|
| 1871 | if (sym[i].st_shndx == info->index.pcpu) |
|---|
| 1872 | secbase = (unsigned long)mod_percpu(mod); |
|---|
| 1873 | else |
|---|
| 1874 | secbase = info->sechdrs[sym[i].st_shndx].sh_addr; |
|---|
| 1875 | sym[i].st_value += secbase; |
|---|
| 1876 | break; |
|---|
| 1877 | } |
|---|
| 1878 | } |
|---|
| 1879 | |
|---|
| 1880 | return ret; |
|---|
| 1881 | } |
|---|
| 1882 | |
|---|
| 1883 | int __weak apply_relocate(Elf_Shdr *sechdrs, |
|---|
| 1884 | const char *strtab, |
|---|
| 1885 | unsigned int symindex, |
|---|
| 1886 | unsigned int relsec, |
|---|
| 1887 | struct module *me) |
|---|
| 1888 | { |
|---|
| 1889 | pr_err("module %s: REL relocation unsupported\n", me->name); |
|---|
| 1890 | return -ENOEXEC; |
|---|
| 1891 | } |
|---|
| 1892 | |
|---|
| 1893 | int __weak apply_relocate_add(Elf_Shdr *sechdrs, |
|---|
| 1894 | const char *strtab, |
|---|
| 1895 | unsigned int symindex, |
|---|
| 1896 | unsigned int relsec, |
|---|
| 1897 | struct module *me) |
|---|
| 1898 | { |
|---|
| 1899 | pr_err("module %s: RELA relocation unsupported\n", me->name); |
|---|
| 1900 | return -ENOEXEC; |
|---|
| 1901 | } |
|---|
| 1902 | |
|---|
| 1903 | static int apply_relocations(struct module *mod, const struct load_info *info) |
|---|
| 1904 | { |
|---|
| 1905 | unsigned int i; |
|---|
| 1906 | int err = 0; |
|---|
| 1907 | |
|---|
| 1908 | /* Now do relocations. */ |
|---|
| 1909 | for (i = 1; i < info->hdr->e_shnum; i++) { |
|---|
| 1910 | unsigned int infosec = info->sechdrs[i].sh_info; |
|---|
| 1911 | |
|---|
| 1912 | /* Not a valid relocation section? */ |
|---|
| 1913 | if (infosec >= info->hdr->e_shnum) |
|---|
| 1914 | continue; |
|---|
| 1915 | |
|---|
| 1916 | /* Don't bother with non-allocated sections */ |
|---|
| 1917 | if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC)) |
|---|
| 1918 | continue; |
|---|
| 1919 | |
|---|
| 1920 | if (info->sechdrs[i].sh_type == SHT_REL) |
|---|
| 1921 | err = apply_relocate(info->sechdrs, info->strtab, |
|---|
| 1922 | info->index.sym, i, mod); |
|---|
| 1923 | else if (info->sechdrs[i].sh_type == SHT_RELA) |
|---|
| 1924 | err = apply_relocate_add(info->sechdrs, info->strtab, |
|---|
| 1925 | info->index.sym, i, mod); |
|---|
| 1926 | if (err < 0) |
|---|
| 1927 | break; |
|---|
| 1928 | } |
|---|
| 1929 | return err; |
|---|
| 1930 | } |
|---|
| 1931 | |
|---|
| 1932 | /* Additional bytes needed by arch in front of individual sections */ |
|---|
| 1933 | unsigned int __weak arch_mod_section_prepend(struct module *mod, |
|---|
| 1934 | unsigned int section) |
|---|
| 1935 | { |
|---|
| 1936 | /* default implementation just returns zero */ |
|---|
| 1937 | return 0; |
|---|
| 1938 | } |
|---|
| 1939 | |
|---|
| 1940 | /* Update size with this section: return offset. */ |
|---|
| 1941 | static long get_offset(struct module *mod, unsigned int *size, |
|---|
| 1942 | Elf_Shdr *sechdr, unsigned int section) |
|---|
| 1943 | { |
|---|
| 1944 | long ret; |
|---|
| 1945 | |
|---|
| 1946 | *size += arch_mod_section_prepend(mod, section); |
|---|
| 1947 | ret = ALIGN(*size, sechdr->sh_addralign ?: 1); |
|---|
| 1948 | *size = ret + sechdr->sh_size; |
|---|
| 1949 | return ret; |
|---|
| 1950 | } |
|---|
| 1951 | |
|---|
| 1952 | /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld |
|---|
| 1953 | might -- code, read-only data, read-write data, small data. Tally |
|---|
| 1954 | sizes, and place the offsets into sh_entsize fields: high bit means it |
|---|
| 1955 | belongs in init. */ |
|---|
| 1956 | static void layout_sections(struct module *mod, struct load_info *info) |
|---|
| 1957 | { |
|---|
| 1958 | static unsigned long const masks[][2] = { |
|---|
| 1959 | /* NOTE: all executable code must be the first section |
|---|
| 1960 | * in this array; otherwise modify the text_size |
|---|
| 1961 | * finder in the two loops below */ |
|---|
| 1962 | { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, |
|---|
| 1963 | { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, |
|---|
| 1964 | { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, |
|---|
| 1965 | { ARCH_SHF_SMALL | SHF_ALLOC, 0 } |
|---|
| 1966 | }; |
|---|
| 1967 | unsigned int m, i; |
|---|
| 1968 | |
|---|
| 1969 | for (i = 0; i < info->hdr->e_shnum; i++) |
|---|
| 1970 | info->sechdrs[i].sh_entsize = ~0UL; |
|---|
| 1971 | |
|---|
| 1972 | DEBUGP("Core section allocation order:\n"); |
|---|
| 1973 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
|---|
| 1974 | for (i = 0; i < info->hdr->e_shnum; ++i) { |
|---|
| 1975 | Elf_Shdr *s = &info->sechdrs[i]; |
|---|
| 1976 | const char *sname = info->secstrings + s->sh_name; |
|---|
| 1977 | |
|---|
| 1978 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
|---|
| 1979 | || (s->sh_flags & masks[m][1]) |
|---|
| 1980 | || s->sh_entsize != ~0UL |
|---|
| 1981 | || strstarts(sname, ".init")) |
|---|
| 1982 | continue; |
|---|
| 1983 | s->sh_entsize = get_offset(mod, &mod->core_size, s, i); |
|---|
| 1984 | DEBUGP("\t%s\n", name); |
|---|
| 1985 | } |
|---|
| 1986 | switch (m) { |
|---|
| 1987 | case 0: /* executable */ |
|---|
| 1988 | mod->core_size = debug_align(mod->core_size); |
|---|
| 1989 | mod->core_text_size = mod->core_size; |
|---|
| 1990 | break; |
|---|
| 1991 | case 1: /* RO: text and ro-data */ |
|---|
| 1992 | mod->core_size = debug_align(mod->core_size); |
|---|
| 1993 | mod->core_ro_size = mod->core_size; |
|---|
| 1994 | break; |
|---|
| 1995 | case 3: /* whole core */ |
|---|
| 1996 | mod->core_size = debug_align(mod->core_size); |
|---|
| 1997 | break; |
|---|
| 1998 | } |
|---|
| 1999 | } |
|---|
| 2000 | |
|---|
| 2001 | DEBUGP("Init section allocation order:\n"); |
|---|
| 2002 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
|---|
| 2003 | for (i = 0; i < info->hdr->e_shnum; ++i) { |
|---|
| 2004 | Elf_Shdr *s = &info->sechdrs[i]; |
|---|
| 2005 | const char *sname = info->secstrings + s->sh_name; |
|---|
| 2006 | |
|---|
| 2007 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
|---|
| 2008 | || (s->sh_flags & masks[m][1]) |
|---|
| 2009 | || s->sh_entsize != ~0UL |
|---|
| 2010 | || !strstarts(sname, ".init")) |
|---|
| 2011 | continue; |
|---|
| 2012 | s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) |
|---|
| 2013 | | INIT_OFFSET_MASK); |
|---|
| 2014 | DEBUGP("\t%s\n", sname); |
|---|
| 2015 | } |
|---|
| 2016 | switch (m) { |
|---|
| 2017 | case 0: /* executable */ |
|---|
| 2018 | mod->init_size = debug_align(mod->init_size); |
|---|
| 2019 | mod->init_text_size = mod->init_size; |
|---|
| 2020 | break; |
|---|
| 2021 | case 1: /* RO: text and ro-data */ |
|---|
| 2022 | mod->init_size = debug_align(mod->init_size); |
|---|
| 2023 | mod->init_ro_size = mod->init_size; |
|---|
| 2024 | break; |
|---|
| 2025 | case 3: /* whole init */ |
|---|
| 2026 | mod->init_size = debug_align(mod->init_size); |
|---|
| 2027 | break; |
|---|
| 2028 | } |
|---|
| 2029 | } |
|---|
| 2030 | } |
|---|
| 2031 | |
|---|
| 2032 | static void set_license(struct module *mod, const char *license) |
|---|
| 2033 | { |
|---|
| 2034 | if (!license) |
|---|
| 2035 | license = "unspecified"; |
|---|
| 2036 | |
|---|
| 2037 | if (!license_is_gpl_compatible(license)) { |
|---|
| 2038 | if (!test_taint(TAINT_PROPRIETARY_MODULE)) |
|---|
| 2039 | printk(KERN_WARNING "%s: module license '%s' taints " |
|---|
| 2040 | "kernel.\n", mod->name, license); |
|---|
| 2041 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); |
|---|
| 2042 | } |
|---|
| 2043 | } |
|---|
| 2044 | |
|---|
| 2045 | /* Parse tag=value strings from .modinfo section */ |
|---|
| 2046 | static char *next_string(char *string, unsigned long *secsize) |
|---|
| 2047 | { |
|---|
| 2048 | /* Skip non-zero chars */ |
|---|
| 2049 | while (string[0]) { |
|---|
| 2050 | string++; |
|---|
| 2051 | if ((*secsize)-- <= 1) |
|---|
| 2052 | return NULL; |
|---|
| 2053 | } |
|---|
| 2054 | |
|---|
| 2055 | /* Skip any zero padding. */ |
|---|
| 2056 | while (!string[0]) { |
|---|
| 2057 | string++; |
|---|
| 2058 | if ((*secsize)-- <= 1) |
|---|
| 2059 | return NULL; |
|---|
| 2060 | } |
|---|
| 2061 | return string; |
|---|
| 2062 | } |
|---|
| 2063 | |
|---|
| 2064 | static char *get_modinfo(struct load_info *info, const char *tag) |
|---|
| 2065 | { |
|---|
| 2066 | char *p; |
|---|
| 2067 | unsigned int taglen = strlen(tag); |
|---|
| 2068 | Elf_Shdr *infosec = &info->sechdrs[info->index.info]; |
|---|
| 2069 | unsigned long size = infosec->sh_size; |
|---|
| 2070 | |
|---|
| 2071 | for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) { |
|---|
| 2072 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
|---|
| 2073 | return p + taglen + 1; |
|---|
| 2074 | } |
|---|
| 2075 | return NULL; |
|---|
| 2076 | } |
|---|
| 2077 | |
|---|
| 2078 | static void setup_modinfo(struct module *mod, struct load_info *info) |
|---|
| 2079 | { |
|---|
| 2080 | struct module_attribute *attr; |
|---|
| 2081 | int i; |
|---|
| 2082 | |
|---|
| 2083 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
|---|
| 2084 | if (attr->setup) |
|---|
| 2085 | attr->setup(mod, get_modinfo(info, attr->attr.name)); |
|---|
| 2086 | } |
|---|
| 2087 | } |
|---|
| 2088 | |
|---|
| 2089 | static void free_modinfo(struct module *mod) |
|---|
| 2090 | { |
|---|
| 2091 | struct module_attribute *attr; |
|---|
| 2092 | int i; |
|---|
| 2093 | |
|---|
| 2094 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
|---|
| 2095 | if (attr->free) |
|---|
| 2096 | attr->free(mod); |
|---|
| 2097 | } |
|---|
| 2098 | } |
|---|
| 2099 | |
|---|
| 2100 | #ifdef CONFIG_KALLSYMS |
|---|
| 2101 | |
|---|
| 2102 | /* lookup symbol in given range of kernel_symbols */ |
|---|
| 2103 | static const struct kernel_symbol *lookup_symbol(const char *name, |
|---|
| 2104 | const struct kernel_symbol *start, |
|---|
| 2105 | const struct kernel_symbol *stop) |
|---|
| 2106 | { |
|---|
| 2107 | return bsearch(name, start, stop - start, |
|---|
| 2108 | sizeof(struct kernel_symbol), cmp_name); |
|---|
| 2109 | } |
|---|
| 2110 | |
|---|
| 2111 | static int is_exported(const char *name, unsigned long value, |
|---|
| 2112 | const struct module *mod) |
|---|
| 2113 | { |
|---|
| 2114 | const struct kernel_symbol *ks; |
|---|
| 2115 | if (!mod) |
|---|
| 2116 | ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); |
|---|
| 2117 | else |
|---|
| 2118 | ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); |
|---|
| 2119 | return ks != NULL && ks->value == value; |
|---|
| 2120 | } |
|---|
| 2121 | |
|---|
| 2122 | /* As per nm */ |
|---|
| 2123 | static char elf_type(const Elf_Sym *sym, const struct load_info *info) |
|---|
| 2124 | { |
|---|
| 2125 | const Elf_Shdr *sechdrs = info->sechdrs; |
|---|
| 2126 | |
|---|
| 2127 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { |
|---|
| 2128 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) |
|---|
| 2129 | return 'v'; |
|---|
| 2130 | else |
|---|
| 2131 | return 'w'; |
|---|
| 2132 | } |
|---|
| 2133 | if (sym->st_shndx == SHN_UNDEF) |
|---|
| 2134 | return 'U'; |
|---|
| 2135 | if (sym->st_shndx == SHN_ABS) |
|---|
| 2136 | return 'a'; |
|---|
| 2137 | if (sym->st_shndx >= SHN_LORESERVE) |
|---|
| 2138 | return '?'; |
|---|
| 2139 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) |
|---|
| 2140 | return 't'; |
|---|
| 2141 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC |
|---|
| 2142 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { |
|---|
| 2143 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) |
|---|
| 2144 | return 'r'; |
|---|
| 2145 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
|---|
| 2146 | return 'g'; |
|---|
| 2147 | else |
|---|
| 2148 | return 'd'; |
|---|
| 2149 | } |
|---|
| 2150 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { |
|---|
| 2151 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
|---|
| 2152 | return 's'; |
|---|
| 2153 | else |
|---|
| 2154 | return 'b'; |
|---|
| 2155 | } |
|---|
| 2156 | if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, |
|---|
| 2157 | ".debug")) { |
|---|
| 2158 | return 'n'; |
|---|
| 2159 | } |
|---|
| 2160 | return '?'; |
|---|
| 2161 | } |
|---|
| 2162 | |
|---|
| 2163 | static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, |
|---|
| 2164 | unsigned int shnum) |
|---|
| 2165 | { |
|---|
| 2166 | const Elf_Shdr *sec; |
|---|
| 2167 | |
|---|
| 2168 | if (src->st_shndx == SHN_UNDEF |
|---|
| 2169 | || src->st_shndx >= shnum |
|---|
| 2170 | || !src->st_name) |
|---|
| 2171 | return false; |
|---|
| 2172 | |
|---|
| 2173 | sec = sechdrs + src->st_shndx; |
|---|
| 2174 | if (!(sec->sh_flags & SHF_ALLOC) |
|---|
| 2175 | #ifndef CONFIG_KALLSYMS_ALL |
|---|
| 2176 | || !(sec->sh_flags & SHF_EXECINSTR) |
|---|
| 2177 | #endif |
|---|
| 2178 | || (sec->sh_entsize & INIT_OFFSET_MASK)) |
|---|
| 2179 | return false; |
|---|
| 2180 | |
|---|
| 2181 | return true; |
|---|
| 2182 | } |
|---|
| 2183 | |
|---|
| 2184 | static void layout_symtab(struct module *mod, struct load_info *info) |
|---|
| 2185 | { |
|---|
| 2186 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; |
|---|
| 2187 | Elf_Shdr *strsect = info->sechdrs + info->index.str; |
|---|
| 2188 | const Elf_Sym *src; |
|---|
| 2189 | unsigned int i, nsrc, ndst; |
|---|
| 2190 | |
|---|
| 2191 | /* Put symbol section at end of init part of module. */ |
|---|
| 2192 | symsect->sh_flags |= SHF_ALLOC; |
|---|
| 2193 | symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect, |
|---|
| 2194 | info->index.sym) | INIT_OFFSET_MASK; |
|---|
| 2195 | DEBUGP("\t%s\n", info->secstrings + symsect->sh_name); |
|---|
| 2196 | |
|---|
| 2197 | src = (void *)info->hdr + symsect->sh_offset; |
|---|
| 2198 | nsrc = symsect->sh_size / sizeof(*src); |
|---|
| 2199 | for (ndst = i = 1; i < nsrc; ++i, ++src) |
|---|
| 2200 | if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) { |
|---|
| 2201 | unsigned int j = src->st_name; |
|---|
| 2202 | |
|---|
| 2203 | while (!__test_and_set_bit(j, info->strmap) |
|---|
| 2204 | && info->strtab[j]) |
|---|
| 2205 | ++j; |
|---|
| 2206 | ++ndst; |
|---|
| 2207 | } |
|---|
| 2208 | |
|---|
| 2209 | /* Append room for core symbols at end of core part. */ |
|---|
| 2210 | info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); |
|---|
| 2211 | mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym); |
|---|
| 2212 | |
|---|
| 2213 | /* Put string table section at end of init part of module. */ |
|---|
| 2214 | strsect->sh_flags |= SHF_ALLOC; |
|---|
| 2215 | strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect, |
|---|
| 2216 | info->index.str) | INIT_OFFSET_MASK; |
|---|
| 2217 | DEBUGP("\t%s\n", info->secstrings + strsect->sh_name); |
|---|
| 2218 | |
|---|
| 2219 | /* Append room for core symbols' strings at end of core part. */ |
|---|
| 2220 | info->stroffs = mod->core_size; |
|---|
| 2221 | __set_bit(0, info->strmap); |
|---|
| 2222 | mod->core_size += bitmap_weight(info->strmap, strsect->sh_size); |
|---|
| 2223 | } |
|---|
| 2224 | |
|---|
| 2225 | static void add_kallsyms(struct module *mod, const struct load_info *info) |
|---|
| 2226 | { |
|---|
| 2227 | unsigned int i, ndst; |
|---|
| 2228 | const Elf_Sym *src; |
|---|
| 2229 | Elf_Sym *dst; |
|---|
| 2230 | char *s; |
|---|
| 2231 | Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; |
|---|
| 2232 | |
|---|
| 2233 | mod->symtab = (void *)symsec->sh_addr; |
|---|
| 2234 | mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym); |
|---|
| 2235 | /* Make sure we get permanent strtab: don't use info->strtab. */ |
|---|
| 2236 | mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr; |
|---|
| 2237 | |
|---|
| 2238 | /* Set types up while we still have access to sections. */ |
|---|
| 2239 | for (i = 0; i < mod->num_symtab; i++) |
|---|
| 2240 | mod->symtab[i].st_info = elf_type(&mod->symtab[i], info); |
|---|
| 2241 | |
|---|
| 2242 | mod->core_symtab = dst = mod->module_core + info->symoffs; |
|---|
| 2243 | src = mod->symtab; |
|---|
| 2244 | *dst = *src; |
|---|
| 2245 | for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) { |
|---|
| 2246 | if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) |
|---|
| 2247 | continue; |
|---|
| 2248 | dst[ndst] = *src; |
|---|
| 2249 | dst[ndst].st_name = bitmap_weight(info->strmap, |
|---|
| 2250 | dst[ndst].st_name); |
|---|
| 2251 | ++ndst; |
|---|
| 2252 | } |
|---|
| 2253 | mod->core_num_syms = ndst; |
|---|
| 2254 | |
|---|
| 2255 | mod->core_strtab = s = mod->module_core + info->stroffs; |
|---|
| 2256 | for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i) |
|---|
| 2257 | if (test_bit(i, info->strmap)) |
|---|
| 2258 | *++s = mod->strtab[i]; |
|---|
| 2259 | } |
|---|
| 2260 | #else |
|---|
| 2261 | static inline void layout_symtab(struct module *mod, struct load_info *info) |
|---|
| 2262 | { |
|---|
| 2263 | } |
|---|
| 2264 | |
|---|
| 2265 | static void add_kallsyms(struct module *mod, const struct load_info *info) |
|---|
| 2266 | { |
|---|
| 2267 | } |
|---|
| 2268 | #endif /* CONFIG_KALLSYMS */ |
|---|
| 2269 | |
|---|
| 2270 | static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num) |
|---|
| 2271 | { |
|---|
| 2272 | if (!debug) |
|---|
| 2273 | return; |
|---|
| 2274 | #ifdef CONFIG_DYNAMIC_DEBUG |
|---|
| 2275 | if (ddebug_add_module(debug, num, debug->modname)) |
|---|
| 2276 | printk(KERN_ERR "dynamic debug error adding module: %s\n", |
|---|
| 2277 | debug->modname); |
|---|
| 2278 | #endif |
|---|
| 2279 | } |
|---|
| 2280 | |
|---|
| 2281 | static void dynamic_debug_remove(struct _ddebug *debug) |
|---|
| 2282 | { |
|---|
| 2283 | if (debug) |
|---|
| 2284 | ddebug_remove_module(debug->modname); |
|---|
| 2285 | } |
|---|
| 2286 | |
|---|
| 2287 | void * __weak module_alloc(unsigned long size) |
|---|
| 2288 | { |
|---|
| 2289 | return size == 0 ? NULL : vmalloc_exec(size); |
|---|
| 2290 | } |
|---|
| 2291 | |
|---|
| 2292 | static void *module_alloc_update_bounds(unsigned long size) |
|---|
| 2293 | { |
|---|
| 2294 | void *ret = module_alloc(size); |
|---|
| 2295 | |
|---|
| 2296 | if (ret) { |
|---|
| 2297 | mutex_lock(&module_mutex); |
|---|
| 2298 | /* Update module bounds. */ |
|---|
| 2299 | if ((unsigned long)ret < module_addr_min) |
|---|
| 2300 | module_addr_min = (unsigned long)ret; |
|---|
| 2301 | if ((unsigned long)ret + size > module_addr_max) |
|---|
| 2302 | module_addr_max = (unsigned long)ret + size; |
|---|
| 2303 | mutex_unlock(&module_mutex); |
|---|
| 2304 | } |
|---|
| 2305 | return ret; |
|---|
| 2306 | } |
|---|
| 2307 | |
|---|
| 2308 | #ifdef CONFIG_DEBUG_KMEMLEAK |
|---|
| 2309 | static void kmemleak_load_module(const struct module *mod, |
|---|
| 2310 | const struct load_info *info) |
|---|
| 2311 | { |
|---|
| 2312 | unsigned int i; |
|---|
| 2313 | |
|---|
| 2314 | /* only scan the sections containing data */ |
|---|
| 2315 | kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); |
|---|
| 2316 | |
|---|
| 2317 | for (i = 1; i < info->hdr->e_shnum; i++) { |
|---|
| 2318 | const char *name = info->secstrings + info->sechdrs[i].sh_name; |
|---|
| 2319 | if (!(info->sechdrs[i].sh_flags & SHF_ALLOC)) |
|---|
| 2320 | continue; |
|---|
| 2321 | if (!strstarts(name, ".data") && !strstarts(name, ".bss")) |
|---|
| 2322 | continue; |
|---|
| 2323 | |
|---|
| 2324 | kmemleak_scan_area((void *)info->sechdrs[i].sh_addr, |
|---|
| 2325 | info->sechdrs[i].sh_size, GFP_KERNEL); |
|---|
| 2326 | } |
|---|
| 2327 | } |
|---|
| 2328 | #else |
|---|
| 2329 | static inline void kmemleak_load_module(const struct module *mod, |
|---|
| 2330 | const struct load_info *info) |
|---|
| 2331 | { |
|---|
| 2332 | } |
|---|
| 2333 | #endif |
|---|
| 2334 | |
|---|
| 2335 | /* Sets info->hdr and info->len. */ |
|---|
| 2336 | static int copy_and_check(struct load_info *info, |
|---|
| 2337 | const void __user *umod, unsigned long len, |
|---|
| 2338 | const char __user *uargs) |
|---|
| 2339 | { |
|---|
| 2340 | int err; |
|---|
| 2341 | Elf_Ehdr *hdr; |
|---|
| 2342 | |
|---|
| 2343 | if (len < sizeof(*hdr)) |
|---|
| 2344 | return -ENOEXEC; |
|---|
| 2345 | |
|---|
| 2346 | /* Suck in entire file: we'll want most of it. */ |
|---|
| 2347 | if ((hdr = vmalloc(len)) == NULL) |
|---|
| 2348 | return -ENOMEM; |
|---|
| 2349 | |
|---|
| 2350 | if (copy_from_user(hdr, umod, len) != 0) { |
|---|
| 2351 | err = -EFAULT; |
|---|
| 2352 | goto free_hdr; |
|---|
| 2353 | } |
|---|
| 2354 | |
|---|
| 2355 | /* Sanity checks against insmoding binaries or wrong arch, |
|---|
| 2356 | weird elf version */ |
|---|
| 2357 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 |
|---|
| 2358 | || hdr->e_type != ET_REL |
|---|
| 2359 | || !elf_check_arch(hdr) |
|---|
| 2360 | || hdr->e_shentsize != sizeof(Elf_Shdr)) { |
|---|
| 2361 | err = -ENOEXEC; |
|---|
| 2362 | goto free_hdr; |
|---|
| 2363 | } |
|---|
| 2364 | |
|---|
| 2365 | if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) { |
|---|
| 2366 | err = -ENOEXEC; |
|---|
| 2367 | goto free_hdr; |
|---|
| 2368 | } |
|---|
| 2369 | |
|---|
| 2370 | info->hdr = hdr; |
|---|
| 2371 | info->len = len; |
|---|
| 2372 | return 0; |
|---|
| 2373 | |
|---|
| 2374 | free_hdr: |
|---|
| 2375 | vfree(hdr); |
|---|
| 2376 | return err; |
|---|
| 2377 | } |
|---|
| 2378 | |
|---|
| 2379 | static void free_copy(struct load_info *info) |
|---|
| 2380 | { |
|---|
| 2381 | vfree(info->hdr); |
|---|
| 2382 | } |
|---|
| 2383 | |
|---|
| 2384 | static int rewrite_section_headers(struct load_info *info) |
|---|
| 2385 | { |
|---|
| 2386 | unsigned int i; |
|---|
| 2387 | |
|---|
| 2388 | /* This should always be true, but let's be sure. */ |
|---|
| 2389 | info->sechdrs[0].sh_addr = 0; |
|---|
| 2390 | |
|---|
| 2391 | for (i = 1; i < info->hdr->e_shnum; i++) { |
|---|
| 2392 | Elf_Shdr *shdr = &info->sechdrs[i]; |
|---|
| 2393 | if (shdr->sh_type != SHT_NOBITS |
|---|
| 2394 | && info->len < shdr->sh_offset + shdr->sh_size) { |
|---|
| 2395 | printk(KERN_ERR "Module len %lu truncated\n", |
|---|
| 2396 | info->len); |
|---|
| 2397 | return -ENOEXEC; |
|---|
| 2398 | } |
|---|
| 2399 | |
|---|
| 2400 | /* Mark all sections sh_addr with their address in the |
|---|
| 2401 | temporary image. */ |
|---|
| 2402 | shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset; |
|---|
| 2403 | |
|---|
| 2404 | #ifndef CONFIG_MODULE_UNLOAD |
|---|
| 2405 | /* Don't load .exit sections */ |
|---|
| 2406 | if (strstarts(info->secstrings+shdr->sh_name, ".exit")) |
|---|
| 2407 | shdr->sh_flags &= ~(unsigned long)SHF_ALLOC; |
|---|
| 2408 | #endif |
|---|
| 2409 | } |
|---|
| 2410 | |
|---|
| 2411 | /* Track but don't keep modinfo and version sections. */ |
|---|
| 2412 | info->index.vers = find_sec(info, "__versions"); |
|---|
| 2413 | info->index.info = find_sec(info, ".modinfo"); |
|---|
| 2414 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; |
|---|
| 2415 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; |
|---|
| 2416 | return 0; |
|---|
| 2417 | } |
|---|
| 2418 | |
|---|
| 2419 | /* |
|---|
| 2420 | * Set up our basic convenience variables (pointers to section headers, |
|---|
| 2421 | * search for module section index etc), and do some basic section |
|---|
| 2422 | * verification. |
|---|
| 2423 | * |
|---|
| 2424 | * Return the temporary module pointer (we'll replace it with the final |
|---|
| 2425 | * one when we move the module sections around). |
|---|
| 2426 | */ |
|---|
| 2427 | static struct module *setup_load_info(struct load_info *info) |
|---|
| 2428 | { |
|---|
| 2429 | unsigned int i; |
|---|
| 2430 | int err; |
|---|
| 2431 | struct module *mod; |
|---|
| 2432 | |
|---|
| 2433 | /* Set up the convenience variables */ |
|---|
| 2434 | info->sechdrs = (void *)info->hdr + info->hdr->e_shoff; |
|---|
| 2435 | info->secstrings = (void *)info->hdr |
|---|
| 2436 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; |
|---|
| 2437 | |
|---|
| 2438 | err = rewrite_section_headers(info); |
|---|
| 2439 | if (err) |
|---|
| 2440 | return ERR_PTR(err); |
|---|
| 2441 | |
|---|
| 2442 | /* Find internal symbols and strings. */ |
|---|
| 2443 | for (i = 1; i < info->hdr->e_shnum; i++) { |
|---|
| 2444 | if (info->sechdrs[i].sh_type == SHT_SYMTAB) { |
|---|
| 2445 | info->index.sym = i; |
|---|
| 2446 | info->index.str = info->sechdrs[i].sh_link; |
|---|
| 2447 | info->strtab = (char *)info->hdr |
|---|
| 2448 | + info->sechdrs[info->index.str].sh_offset; |
|---|
| 2449 | break; |
|---|
| 2450 | } |
|---|
| 2451 | } |
|---|
| 2452 | |
|---|
| 2453 | info->index.mod = find_sec(info, ".gnu.linkonce.this_module"); |
|---|
| 2454 | if (!info->index.mod) { |
|---|
| 2455 | printk(KERN_WARNING "No module found in object\n"); |
|---|
| 2456 | return ERR_PTR(-ENOEXEC); |
|---|
| 2457 | } |
|---|
| 2458 | /* This is temporary: point mod into copy of data. */ |
|---|
| 2459 | mod = (void *)info->sechdrs[info->index.mod].sh_addr; |
|---|
| 2460 | |
|---|
| 2461 | if (info->index.sym == 0) { |
|---|
| 2462 | printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", |
|---|
| 2463 | mod->name); |
|---|
| 2464 | return ERR_PTR(-ENOEXEC); |
|---|
| 2465 | } |
|---|
| 2466 | |
|---|
| 2467 | info->index.pcpu = find_pcpusec(info); |
|---|
| 2468 | |
|---|
| 2469 | /* Check module struct version now, before we try to use module. */ |
|---|
| 2470 | if (!check_modstruct_version(info->sechdrs, info->index.vers, mod)) |
|---|
| 2471 | return ERR_PTR(-ENOEXEC); |
|---|
| 2472 | |
|---|
| 2473 | return mod; |
|---|
| 2474 | } |
|---|
| 2475 | |
|---|
| 2476 | static int check_modinfo(struct module *mod, struct load_info *info) |
|---|
| 2477 | { |
|---|
| 2478 | const char *modmagic = get_modinfo(info, "vermagic"); |
|---|
| 2479 | int err; |
|---|
| 2480 | |
|---|
| 2481 | /* This is allowed: modprobe --force will invalidate it. */ |
|---|
| 2482 | if (!modmagic) { |
|---|
| 2483 | err = try_to_force_load(mod, "bad vermagic"); |
|---|
| 2484 | if (err) |
|---|
| 2485 | return err; |
|---|
| 2486 | } else if (!same_magic(modmagic, vermagic, info->index.vers)) { |
|---|
| 2487 | printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", |
|---|
| 2488 | mod->name, modmagic, vermagic); |
|---|
| 2489 | return -ENOEXEC; |
|---|
| 2490 | } |
|---|
| 2491 | |
|---|
| 2492 | if (!get_modinfo(info, "intree")) |
|---|
| 2493 | add_taint_module(mod, TAINT_OOT_MODULE); |
|---|
| 2494 | |
|---|
| 2495 | if (get_modinfo(info, "staging")) { |
|---|
| 2496 | add_taint_module(mod, TAINT_CRAP); |
|---|
| 2497 | printk(KERN_WARNING "%s: module is from the staging directory," |
|---|
| 2498 | " the quality is unknown, you have been warned.\n", |
|---|
| 2499 | mod->name); |
|---|
| 2500 | } |
|---|
| 2501 | |
|---|
| 2502 | /* Set up license info based on the info section */ |
|---|
| 2503 | set_license(mod, get_modinfo(info, "license")); |
|---|
| 2504 | |
|---|
| 2505 | return 0; |
|---|
| 2506 | } |
|---|
| 2507 | |
|---|
| 2508 | static void find_module_sections(struct module *mod, struct load_info *info) |
|---|
| 2509 | { |
|---|
| 2510 | mod->kp = section_objs(info, "__param", |
|---|
| 2511 | sizeof(*mod->kp), &mod->num_kp); |
|---|
| 2512 | mod->syms = section_objs(info, "__ksymtab", |
|---|
| 2513 | sizeof(*mod->syms), &mod->num_syms); |
|---|
| 2514 | mod->crcs = section_addr(info, "__kcrctab"); |
|---|
| 2515 | mod->gpl_syms = section_objs(info, "__ksymtab_gpl", |
|---|
| 2516 | sizeof(*mod->gpl_syms), |
|---|
| 2517 | &mod->num_gpl_syms); |
|---|
| 2518 | mod->gpl_crcs = section_addr(info, "__kcrctab_gpl"); |
|---|
| 2519 | mod->gpl_future_syms = section_objs(info, |
|---|
| 2520 | "__ksymtab_gpl_future", |
|---|
| 2521 | sizeof(*mod->gpl_future_syms), |
|---|
| 2522 | &mod->num_gpl_future_syms); |
|---|
| 2523 | mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future"); |
|---|
| 2524 | |
|---|
| 2525 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 2526 | mod->unused_syms = section_objs(info, "__ksymtab_unused", |
|---|
| 2527 | sizeof(*mod->unused_syms), |
|---|
| 2528 | &mod->num_unused_syms); |
|---|
| 2529 | mod->unused_crcs = section_addr(info, "__kcrctab_unused"); |
|---|
| 2530 | mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl", |
|---|
| 2531 | sizeof(*mod->unused_gpl_syms), |
|---|
| 2532 | &mod->num_unused_gpl_syms); |
|---|
| 2533 | mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl"); |
|---|
| 2534 | #endif |
|---|
| 2535 | #ifdef CONFIG_CONSTRUCTORS |
|---|
| 2536 | mod->ctors = section_objs(info, ".ctors", |
|---|
| 2537 | sizeof(*mod->ctors), &mod->num_ctors); |
|---|
| 2538 | #endif |
|---|
| 2539 | |
|---|
| 2540 | #ifdef CONFIG_TRACEPOINTS |
|---|
| 2541 | mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs", |
|---|
| 2542 | sizeof(*mod->tracepoints_ptrs), |
|---|
| 2543 | &mod->num_tracepoints); |
|---|
| 2544 | #endif |
|---|
| 2545 | #ifdef HAVE_JUMP_LABEL |
|---|
| 2546 | mod->jump_entries = section_objs(info, "__jump_table", |
|---|
| 2547 | sizeof(*mod->jump_entries), |
|---|
| 2548 | &mod->num_jump_entries); |
|---|
| 2549 | #endif |
|---|
| 2550 | #ifdef CONFIG_EVENT_TRACING |
|---|
| 2551 | mod->trace_events = section_objs(info, "_ftrace_events", |
|---|
| 2552 | sizeof(*mod->trace_events), |
|---|
| 2553 | &mod->num_trace_events); |
|---|
| 2554 | /* |
|---|
| 2555 | * This section contains pointers to allocated objects in the trace |
|---|
| 2556 | * code and not scanning it leads to false positives. |
|---|
| 2557 | */ |
|---|
| 2558 | kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) * |
|---|
| 2559 | mod->num_trace_events, GFP_KERNEL); |
|---|
| 2560 | #endif |
|---|
| 2561 | #ifdef CONFIG_TRACING |
|---|
| 2562 | mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt", |
|---|
| 2563 | sizeof(*mod->trace_bprintk_fmt_start), |
|---|
| 2564 | &mod->num_trace_bprintk_fmt); |
|---|
| 2565 | /* |
|---|
| 2566 | * This section contains pointers to allocated objects in the trace |
|---|
| 2567 | * code and not scanning it leads to false positives. |
|---|
| 2568 | */ |
|---|
| 2569 | kmemleak_scan_area(mod->trace_bprintk_fmt_start, |
|---|
| 2570 | sizeof(*mod->trace_bprintk_fmt_start) * |
|---|
| 2571 | mod->num_trace_bprintk_fmt, GFP_KERNEL); |
|---|
| 2572 | #endif |
|---|
| 2573 | #ifdef CONFIG_FTRACE_MCOUNT_RECORD |
|---|
| 2574 | /* sechdrs[0].sh_size is always zero */ |
|---|
| 2575 | mod->ftrace_callsites = section_objs(info, "__mcount_loc", |
|---|
| 2576 | sizeof(*mod->ftrace_callsites), |
|---|
| 2577 | &mod->num_ftrace_callsites); |
|---|
| 2578 | #endif |
|---|
| 2579 | |
|---|
| 2580 | mod->extable = section_objs(info, "__ex_table", |
|---|
| 2581 | sizeof(*mod->extable), &mod->num_exentries); |
|---|
| 2582 | |
|---|
| 2583 | if (section_addr(info, "__obsparm")) |
|---|
| 2584 | printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", |
|---|
| 2585 | mod->name); |
|---|
| 2586 | |
|---|
| 2587 | info->debug = section_objs(info, "__verbose", |
|---|
| 2588 | sizeof(*info->debug), &info->num_debug); |
|---|
| 2589 | } |
|---|
| 2590 | |
|---|
| 2591 | static int move_module(struct module *mod, struct load_info *info) |
|---|
| 2592 | { |
|---|
| 2593 | int i; |
|---|
| 2594 | void *ptr; |
|---|
| 2595 | |
|---|
| 2596 | /* Do the allocs. */ |
|---|
| 2597 | ptr = module_alloc_update_bounds(mod->core_size); |
|---|
| 2598 | /* |
|---|
| 2599 | * The pointer to this block is stored in the module structure |
|---|
| 2600 | * which is inside the block. Just mark it as not being a |
|---|
| 2601 | * leak. |
|---|
| 2602 | */ |
|---|
| 2603 | kmemleak_not_leak(ptr); |
|---|
| 2604 | if (!ptr) |
|---|
| 2605 | return -ENOMEM; |
|---|
| 2606 | |
|---|
| 2607 | memset(ptr, 0, mod->core_size); |
|---|
| 2608 | mod->module_core = ptr; |
|---|
| 2609 | |
|---|
| 2610 | ptr = module_alloc_update_bounds(mod->init_size); |
|---|
| 2611 | /* |
|---|
| 2612 | * The pointer to this block is stored in the module structure |
|---|
| 2613 | * which is inside the block. This block doesn't need to be |
|---|
| 2614 | * scanned as it contains data and code that will be freed |
|---|
| 2615 | * after the module is initialized. |
|---|
| 2616 | */ |
|---|
| 2617 | kmemleak_ignore(ptr); |
|---|
| 2618 | if (!ptr && mod->init_size) { |
|---|
| 2619 | module_free(mod, mod->module_core); |
|---|
| 2620 | return -ENOMEM; |
|---|
| 2621 | } |
|---|
| 2622 | memset(ptr, 0, mod->init_size); |
|---|
| 2623 | mod->module_init = ptr; |
|---|
| 2624 | |
|---|
| 2625 | /* Transfer each section which specifies SHF_ALLOC */ |
|---|
| 2626 | DEBUGP("final section addresses:\n"); |
|---|
| 2627 | for (i = 0; i < info->hdr->e_shnum; i++) { |
|---|
| 2628 | void *dest; |
|---|
| 2629 | Elf_Shdr *shdr = &info->sechdrs[i]; |
|---|
| 2630 | |
|---|
| 2631 | if (!(shdr->sh_flags & SHF_ALLOC)) |
|---|
| 2632 | continue; |
|---|
| 2633 | |
|---|
| 2634 | if (shdr->sh_entsize & INIT_OFFSET_MASK) |
|---|
| 2635 | dest = mod->module_init |
|---|
| 2636 | + (shdr->sh_entsize & ~INIT_OFFSET_MASK); |
|---|
| 2637 | else |
|---|
| 2638 | dest = mod->module_core + shdr->sh_entsize; |
|---|
| 2639 | |
|---|
| 2640 | if (shdr->sh_type != SHT_NOBITS) |
|---|
| 2641 | memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); |
|---|
| 2642 | /* Update sh_addr to point to copy in image. */ |
|---|
| 2643 | shdr->sh_addr = (unsigned long)dest; |
|---|
| 2644 | DEBUGP("\t0x%lx %s\n", |
|---|
| 2645 | shdr->sh_addr, info->secstrings + shdr->sh_name); |
|---|
| 2646 | } |
|---|
| 2647 | |
|---|
| 2648 | return 0; |
|---|
| 2649 | } |
|---|
| 2650 | |
|---|
| 2651 | static int check_module_license_and_versions(struct module *mod) |
|---|
| 2652 | { |
|---|
| 2653 | /* |
|---|
| 2654 | * ndiswrapper is under GPL by itself, but loads proprietary modules. |
|---|
| 2655 | * Don't use add_taint_module(), as it would prevent ndiswrapper from |
|---|
| 2656 | * using GPL-only symbols it needs. |
|---|
| 2657 | */ |
|---|
| 2658 | if (strcmp(mod->name, "ndiswrapper") == 0) |
|---|
| 2659 | add_taint(TAINT_PROPRIETARY_MODULE); |
|---|
| 2660 | |
|---|
| 2661 | /* driverloader was caught wrongly pretending to be under GPL */ |
|---|
| 2662 | if (strcmp(mod->name, "driverloader") == 0) |
|---|
| 2663 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); |
|---|
| 2664 | |
|---|
| 2665 | #ifdef CONFIG_MODVERSIONS |
|---|
| 2666 | if ((mod->num_syms && !mod->crcs) |
|---|
| 2667 | || (mod->num_gpl_syms && !mod->gpl_crcs) |
|---|
| 2668 | || (mod->num_gpl_future_syms && !mod->gpl_future_crcs) |
|---|
| 2669 | #ifdef CONFIG_UNUSED_SYMBOLS |
|---|
| 2670 | || (mod->num_unused_syms && !mod->unused_crcs) |
|---|
| 2671 | || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) |
|---|
| 2672 | #endif |
|---|
| 2673 | ) { |
|---|
| 2674 | return try_to_force_load(mod, |
|---|
| 2675 | "no versions for exported symbols"); |
|---|
| 2676 | } |
|---|
| 2677 | #endif |
|---|
| 2678 | return 0; |
|---|
| 2679 | } |
|---|
| 2680 | |
|---|
| 2681 | static void flush_module_icache(const struct module *mod) |
|---|
| 2682 | { |
|---|
| 2683 | mm_segment_t old_fs; |
|---|
| 2684 | |
|---|
| 2685 | /* flush the icache in correct context */ |
|---|
| 2686 | old_fs = get_fs(); |
|---|
| 2687 | set_fs(KERNEL_DS); |
|---|
| 2688 | |
|---|
| 2689 | /* |
|---|
| 2690 | * Flush the instruction cache, since we've played with text. |
|---|
| 2691 | * Do it before processing of module parameters, so the module |
|---|
| 2692 | * can provide parameter accessor functions of its own. |
|---|
| 2693 | */ |
|---|
| 2694 | if (mod->module_init) |
|---|
| 2695 | flush_icache_range((unsigned long)mod->module_init, |
|---|
| 2696 | (unsigned long)mod->module_init |
|---|
| 2697 | + mod->init_size); |
|---|
| 2698 | flush_icache_range((unsigned long)mod->module_core, |
|---|
| 2699 | (unsigned long)mod->module_core + mod->core_size); |
|---|
| 2700 | |
|---|
| 2701 | set_fs(old_fs); |
|---|
| 2702 | } |
|---|
| 2703 | |
|---|
| 2704 | int __weak module_frob_arch_sections(Elf_Ehdr *hdr, |
|---|
| 2705 | Elf_Shdr *sechdrs, |
|---|
| 2706 | char *secstrings, |
|---|
| 2707 | struct module *mod) |
|---|
| 2708 | { |
|---|
| 2709 | return 0; |
|---|
| 2710 | } |
|---|
| 2711 | |
|---|
| 2712 | static struct module *layout_and_allocate(struct load_info *info) |
|---|
| 2713 | { |
|---|
| 2714 | /* Module within temporary copy. */ |
|---|
| 2715 | struct module *mod; |
|---|
| 2716 | Elf_Shdr *pcpusec; |
|---|
| 2717 | int err; |
|---|
| 2718 | |
|---|
| 2719 | mod = setup_load_info(info); |
|---|
| 2720 | if (IS_ERR(mod)) |
|---|
| 2721 | return mod; |
|---|
| 2722 | |
|---|
| 2723 | err = check_modinfo(mod, info); |
|---|
| 2724 | if (err) |
|---|
| 2725 | return ERR_PTR(err); |
|---|
| 2726 | |
|---|
| 2727 | /* Allow arches to frob section contents and sizes. */ |
|---|
| 2728 | err = module_frob_arch_sections(info->hdr, info->sechdrs, |
|---|
| 2729 | info->secstrings, mod); |
|---|
| 2730 | if (err < 0) |
|---|
| 2731 | goto out; |
|---|
| 2732 | |
|---|
| 2733 | pcpusec = &info->sechdrs[info->index.pcpu]; |
|---|
| 2734 | if (pcpusec->sh_size) { |
|---|
| 2735 | /* We have a special allocation for this section. */ |
|---|
| 2736 | err = percpu_modalloc(mod, |
|---|
| 2737 | pcpusec->sh_size, pcpusec->sh_addralign); |
|---|
| 2738 | if (err) |
|---|
| 2739 | goto out; |
|---|
| 2740 | pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC; |
|---|
| 2741 | } |
|---|
| 2742 | |
|---|
| 2743 | /* Determine total sizes, and put offsets in sh_entsize. For now |
|---|
| 2744 | this is done generically; there doesn't appear to be any |
|---|
| 2745 | special cases for the architectures. */ |
|---|
| 2746 | layout_sections(mod, info); |
|---|
| 2747 | |
|---|
| 2748 | info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size) |
|---|
| 2749 | * sizeof(long), GFP_KERNEL); |
|---|
| 2750 | if (!info->strmap) { |
|---|
| 2751 | err = -ENOMEM; |
|---|
| 2752 | goto free_percpu; |
|---|
| 2753 | } |
|---|
| 2754 | layout_symtab(mod, info); |
|---|
| 2755 | |
|---|
| 2756 | /* Allocate and move to the final place */ |
|---|
| 2757 | err = move_module(mod, info); |
|---|
| 2758 | if (err) |
|---|
| 2759 | goto free_strmap; |
|---|
| 2760 | |
|---|
| 2761 | /* Module has been copied to its final place now: return it. */ |
|---|
| 2762 | mod = (void *)info->sechdrs[info->index.mod].sh_addr; |
|---|
| 2763 | kmemleak_load_module(mod, info); |
|---|
| 2764 | return mod; |
|---|
| 2765 | |
|---|
| 2766 | free_strmap: |
|---|
| 2767 | kfree(info->strmap); |
|---|
| 2768 | free_percpu: |
|---|
| 2769 | percpu_modfree(mod); |
|---|
| 2770 | out: |
|---|
| 2771 | return ERR_PTR(err); |
|---|
| 2772 | } |
|---|
| 2773 | |
|---|
| 2774 | /* mod is no longer valid after this! */ |
|---|
| 2775 | static void module_deallocate(struct module *mod, struct load_info *info) |
|---|
| 2776 | { |
|---|
| 2777 | kfree(info->strmap); |
|---|
| 2778 | percpu_modfree(mod); |
|---|
| 2779 | module_free(mod, mod->module_init); |
|---|
| 2780 | module_free(mod, mod->module_core); |
|---|
| 2781 | } |
|---|
| 2782 | |
|---|
| 2783 | int __weak module_finalize(const Elf_Ehdr *hdr, |
|---|
| 2784 | const Elf_Shdr *sechdrs, |
|---|
| 2785 | struct module *me) |
|---|
| 2786 | { |
|---|
| 2787 | return 0; |
|---|
| 2788 | } |
|---|
| 2789 | |
|---|
| 2790 | static int post_relocation(struct module *mod, const struct load_info *info) |
|---|
| 2791 | { |
|---|
| 2792 | /* Sort exception table now relocations are done. */ |
|---|
| 2793 | sort_extable(mod->extable, mod->extable + mod->num_exentries); |
|---|
| 2794 | |
|---|
| 2795 | /* Copy relocated percpu area over. */ |
|---|
| 2796 | percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr, |
|---|
| 2797 | info->sechdrs[info->index.pcpu].sh_size); |
|---|
| 2798 | |
|---|
| 2799 | /* Setup kallsyms-specific fields. */ |
|---|
| 2800 | add_kallsyms(mod, info); |
|---|
| 2801 | |
|---|
| 2802 | /* Arch-specific module finalizing. */ |
|---|
| 2803 | return module_finalize(info->hdr, info->sechdrs, mod); |
|---|
| 2804 | } |
|---|
| 2805 | |
|---|
| 2806 | /* Allocate and load the module: note that size of section 0 is always |
|---|
| 2807 | zero, and we rely on this for optional sections. */ |
|---|
| 2808 | static struct module *load_module(void __user *umod, |
|---|
| 2809 | unsigned long len, |
|---|
| 2810 | const char __user *uargs) |
|---|
| 2811 | { |
|---|
| 2812 | struct load_info info = { NULL, }; |
|---|
| 2813 | struct module *mod; |
|---|
| 2814 | long err; |
|---|
| 2815 | |
|---|
| 2816 | DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", |
|---|
| 2817 | umod, len, uargs); |
|---|
| 2818 | |
|---|
| 2819 | /* Copy in the blobs from userspace, check they are vaguely sane. */ |
|---|
| 2820 | err = copy_and_check(&info, umod, len, uargs); |
|---|
| 2821 | if (err) |
|---|
| 2822 | return ERR_PTR(err); |
|---|
| 2823 | |
|---|
| 2824 | /* Figure out module layout, and allocate all the memory. */ |
|---|
| 2825 | mod = layout_and_allocate(&info); |
|---|
| 2826 | if (IS_ERR(mod)) { |
|---|
| 2827 | err = PTR_ERR(mod); |
|---|
| 2828 | goto free_copy; |
|---|
| 2829 | } |
|---|
| 2830 | |
|---|
| 2831 | /* Now module is in final location, initialize linked lists, etc. */ |
|---|
| 2832 | err = module_unload_init(mod); |
|---|
| 2833 | if (err) |
|---|
| 2834 | goto free_module; |
|---|
| 2835 | |
|---|
| 2836 | /* Now we've got everything in the final locations, we can |
|---|
| 2837 | * find optional sections. */ |
|---|
| 2838 | find_module_sections(mod, &info); |
|---|
| 2839 | |
|---|
| 2840 | err = check_module_license_and_versions(mod); |
|---|
| 2841 | if (err) |
|---|
| 2842 | goto free_unload; |
|---|
| 2843 | |
|---|
| 2844 | /* Set up MODINFO_ATTR fields */ |
|---|
| 2845 | setup_modinfo(mod, &info); |
|---|
| 2846 | |
|---|
| 2847 | /* Fix up syms, so that st_value is a pointer to location. */ |
|---|
| 2848 | err = simplify_symbols(mod, &info); |
|---|
| 2849 | if (err < 0) |
|---|
| 2850 | goto free_modinfo; |
|---|
| 2851 | |
|---|
| 2852 | err = apply_relocations(mod, &info); |
|---|
| 2853 | if (err < 0) |
|---|
| 2854 | goto free_modinfo; |
|---|
| 2855 | |
|---|
| 2856 | err = post_relocation(mod, &info); |
|---|
| 2857 | if (err < 0) |
|---|
| 2858 | goto free_modinfo; |
|---|
| 2859 | |
|---|
| 2860 | flush_module_icache(mod); |
|---|
| 2861 | |
|---|
| 2862 | /* Now copy in args */ |
|---|
| 2863 | mod->args = strndup_user(uargs, ~0UL >> 1); |
|---|
| 2864 | if (IS_ERR(mod->args)) { |
|---|
| 2865 | err = PTR_ERR(mod->args); |
|---|
| 2866 | goto free_arch_cleanup; |
|---|
| 2867 | } |
|---|
| 2868 | |
|---|
| 2869 | /* Mark state as coming so strong_try_module_get() ignores us. */ |
|---|
| 2870 | mod->state = MODULE_STATE_COMING; |
|---|
| 2871 | |
|---|
| 2872 | /* Now sew it into the lists so we can get lockdep and oops |
|---|
| 2873 | * info during argument parsing. No one should access us, since |
|---|
| 2874 | * strong_try_module_get() will fail. |
|---|
| 2875 | * lockdep/oops can run asynchronous, so use the RCU list insertion |
|---|
| 2876 | * function to insert in a way safe to concurrent readers. |
|---|
| 2877 | * The mutex protects against concurrent writers. |
|---|
| 2878 | */ |
|---|
| 2879 | mutex_lock(&module_mutex); |
|---|
| 2880 | if (find_module(mod->name)) { |
|---|
| 2881 | err = -EEXIST; |
|---|
| 2882 | goto unlock; |
|---|
| 2883 | } |
|---|
| 2884 | |
|---|
| 2885 | /* This has to be done once we're sure module name is unique. */ |
|---|
| 2886 | dynamic_debug_setup(info.debug, info.num_debug); |
|---|
| 2887 | |
|---|
| 2888 | /* Find duplicate symbols */ |
|---|
| 2889 | err = verify_export_symbols(mod); |
|---|
| 2890 | if (err < 0) |
|---|
| 2891 | goto ddebug; |
|---|
| 2892 | |
|---|
| 2893 | module_bug_finalize(info.hdr, info.sechdrs, mod); |
|---|
| 2894 | list_add_rcu(&mod->list, &modules); |
|---|
| 2895 | mutex_unlock(&module_mutex); |
|---|
| 2896 | |
|---|
| 2897 | /* Module is ready to execute: parsing args may do that. */ |
|---|
| 2898 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); |
|---|
| 2899 | if (err < 0) |
|---|
| 2900 | goto unlink; |
|---|
| 2901 | |
|---|
| 2902 | /* Link in to syfs. */ |
|---|
| 2903 | err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); |
|---|
| 2904 | if (err < 0) |
|---|
| 2905 | goto unlink; |
|---|
| 2906 | |
|---|
| 2907 | /* Get rid of temporary copy and strmap. */ |
|---|
| 2908 | kfree(info.strmap); |
|---|
| 2909 | free_copy(&info); |
|---|
| 2910 | |
|---|
| 2911 | /* Done! */ |
|---|
| 2912 | trace_module_load(mod); |
|---|
| 2913 | return mod; |
|---|
| 2914 | |
|---|
| 2915 | unlink: |
|---|
| 2916 | mutex_lock(&module_mutex); |
|---|
| 2917 | /* Unlink carefully: kallsyms could be walking list. */ |
|---|
| 2918 | list_del_rcu(&mod->list); |
|---|
| 2919 | module_bug_cleanup(mod); |
|---|
| 2920 | |
|---|
| 2921 | ddebug: |
|---|
| 2922 | dynamic_debug_remove(info.debug); |
|---|
| 2923 | unlock: |
|---|
| 2924 | mutex_unlock(&module_mutex); |
|---|
| 2925 | synchronize_sched(); |
|---|
| 2926 | kfree(mod->args); |
|---|
| 2927 | free_arch_cleanup: |
|---|
| 2928 | module_arch_cleanup(mod); |
|---|
| 2929 | free_modinfo: |
|---|
| 2930 | free_modinfo(mod); |
|---|
| 2931 | free_unload: |
|---|
| 2932 | module_unload_free(mod); |
|---|
| 2933 | free_module: |
|---|
| 2934 | module_deallocate(mod, &info); |
|---|
| 2935 | free_copy: |
|---|
| 2936 | free_copy(&info); |
|---|
| 2937 | return ERR_PTR(err); |
|---|
| 2938 | } |
|---|
| 2939 | |
|---|
| 2940 | /* Call module constructors. */ |
|---|
| 2941 | static void do_mod_ctors(struct module *mod) |
|---|
| 2942 | { |
|---|
| 2943 | #ifdef CONFIG_CONSTRUCTORS |
|---|
| 2944 | unsigned long i; |
|---|
| 2945 | |
|---|
| 2946 | for (i = 0; i < mod->num_ctors; i++) |
|---|
| 2947 | mod->ctors[i](); |
|---|
| 2948 | #endif |
|---|
| 2949 | } |
|---|
| 2950 | |
|---|
| 2951 | /* This is where the real work happens */ |
|---|
| 2952 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
|---|
| 2953 | unsigned long, len, const char __user *, uargs) |
|---|
| 2954 | { |
|---|
| 2955 | struct module *mod; |
|---|
| 2956 | int ret = 0; |
|---|
| 2957 | |
|---|
| 2958 | /* Must have permission */ |
|---|
| 2959 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
|---|
| 2960 | return -EPERM; |
|---|
| 2961 | |
|---|
| 2962 | /* Do all the hard work */ |
|---|
| 2963 | mod = load_module(umod, len, uargs); |
|---|
| 2964 | if (IS_ERR(mod)) |
|---|
| 2965 | return PTR_ERR(mod); |
|---|
| 2966 | |
|---|
| 2967 | blocking_notifier_call_chain(&module_notify_list, |
|---|
| 2968 | MODULE_STATE_COMING, mod); |
|---|
| 2969 | |
|---|
| 2970 | /* Set RO and NX regions for core */ |
|---|
| 2971 | set_section_ro_nx(mod->module_core, |
|---|
| 2972 | mod->core_text_size, |
|---|
| 2973 | mod->core_ro_size, |
|---|
| 2974 | mod->core_size); |
|---|
| 2975 | |
|---|
| 2976 | /* Set RO and NX regions for init */ |
|---|
| 2977 | set_section_ro_nx(mod->module_init, |
|---|
| 2978 | mod->init_text_size, |
|---|
| 2979 | mod->init_ro_size, |
|---|
| 2980 | mod->init_size); |
|---|
| 2981 | |
|---|
| 2982 | do_mod_ctors(mod); |
|---|
| 2983 | /* Start the module */ |
|---|
| 2984 | if (mod->init != NULL) |
|---|
| 2985 | ret = do_one_initcall(mod->init); |
|---|
| 2986 | if (ret < 0) { |
|---|
| 2987 | /* Init routine failed: abort. Try to protect us from |
|---|
| 2988 | buggy refcounters. */ |
|---|
| 2989 | mod->state = MODULE_STATE_GOING; |
|---|
| 2990 | synchronize_sched(); |
|---|
| 2991 | module_put(mod); |
|---|
| 2992 | blocking_notifier_call_chain(&module_notify_list, |
|---|
| 2993 | MODULE_STATE_GOING, mod); |
|---|
| 2994 | free_module(mod); |
|---|
| 2995 | wake_up(&module_wq); |
|---|
| 2996 | return ret; |
|---|
| 2997 | } |
|---|
| 2998 | if (ret > 0) { |
|---|
| 2999 | printk(KERN_WARNING |
|---|
| 3000 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" |
|---|
| 3001 | "%s: loading module anyway...\n", |
|---|
| 3002 | __func__, mod->name, ret, |
|---|
| 3003 | __func__); |
|---|
| 3004 | dump_stack(); |
|---|
| 3005 | } |
|---|
| 3006 | |
|---|
| 3007 | /* Now it's a first class citizen! Wake up anyone waiting for it. */ |
|---|
| 3008 | mod->state = MODULE_STATE_LIVE; |
|---|
| 3009 | wake_up(&module_wq); |
|---|
| 3010 | blocking_notifier_call_chain(&module_notify_list, |
|---|
| 3011 | MODULE_STATE_LIVE, mod); |
|---|
| 3012 | |
|---|
| 3013 | /* We need to finish all async code before the module init sequence is done */ |
|---|
| 3014 | async_synchronize_full(); |
|---|
| 3015 | |
|---|
| 3016 | mutex_lock(&module_mutex); |
|---|
| 3017 | /* Drop initial reference. */ |
|---|
| 3018 | module_put(mod); |
|---|
| 3019 | trim_init_extable(mod); |
|---|
| 3020 | #ifdef CONFIG_KALLSYMS |
|---|
| 3021 | mod->num_symtab = mod->core_num_syms; |
|---|
| 3022 | mod->symtab = mod->core_symtab; |
|---|
| 3023 | mod->strtab = mod->core_strtab; |
|---|
| 3024 | #endif |
|---|
| 3025 | unset_module_init_ro_nx(mod); |
|---|
| 3026 | module_free(mod, mod->module_init); |
|---|
| 3027 | mod->module_init = NULL; |
|---|
| 3028 | mod->init_size = 0; |
|---|
| 3029 | mod->init_ro_size = 0; |
|---|
| 3030 | mod->init_text_size = 0; |
|---|
| 3031 | mutex_unlock(&module_mutex); |
|---|
| 3032 | |
|---|
| 3033 | return 0; |
|---|
| 3034 | } |
|---|
| 3035 | |
|---|
| 3036 | static inline int within(unsigned long addr, void *start, unsigned long size) |
|---|
| 3037 | { |
|---|
| 3038 | return ((void *)addr >= start && (void *)addr < start + size); |
|---|
| 3039 | } |
|---|
| 3040 | |
|---|
| 3041 | #ifdef CONFIG_KALLSYMS |
|---|
| 3042 | /* |
|---|
| 3043 | * This ignores the intensely annoying "mapping symbols" found |
|---|
| 3044 | * in ARM ELF files: $a, $t and $d. |
|---|
| 3045 | */ |
|---|
| 3046 | static inline int is_arm_mapping_symbol(const char *str) |
|---|
| 3047 | { |
|---|
| 3048 | return str[0] == '$' && strchr("atd", str[1]) |
|---|
| 3049 | && (str[2] == '\0' || str[2] == '.'); |
|---|
| 3050 | } |
|---|
| 3051 | |
|---|
| 3052 | static const char *get_ksymbol(struct module *mod, |
|---|
| 3053 | unsigned long addr, |
|---|
| 3054 | unsigned long *size, |
|---|
| 3055 | unsigned long *offset) |
|---|
| 3056 | { |
|---|
| 3057 | unsigned int i, best = 0; |
|---|
| 3058 | unsigned long nextval; |
|---|
| 3059 | |
|---|
| 3060 | /* At worse, next value is at end of module */ |
|---|
| 3061 | if (within_module_init(addr, mod)) |
|---|
| 3062 | nextval = (unsigned long)mod->module_init+mod->init_text_size; |
|---|
| 3063 | else |
|---|
| 3064 | nextval = (unsigned long)mod->module_core+mod->core_text_size; |
|---|
| 3065 | |
|---|
| 3066 | /* Scan for closest preceding symbol, and next symbol. (ELF |
|---|
| 3067 | starts real symbols at 1). */ |
|---|
| 3068 | for (i = 1; i < mod->num_symtab; i++) { |
|---|
| 3069 | if (mod->symtab[i].st_shndx == SHN_UNDEF) |
|---|
| 3070 | continue; |
|---|
| 3071 | |
|---|
| 3072 | /* We ignore unnamed symbols: they're uninformative |
|---|
| 3073 | * and inserted at a whim. */ |
|---|
| 3074 | if (mod->symtab[i].st_value <= addr |
|---|
| 3075 | && mod->symtab[i].st_value > mod->symtab[best].st_value |
|---|
| 3076 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
|---|
| 3077 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
|---|
| 3078 | best = i; |
|---|
| 3079 | if (mod->symtab[i].st_value > addr |
|---|
| 3080 | && mod->symtab[i].st_value < nextval |
|---|
| 3081 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
|---|
| 3082 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
|---|
| 3083 | nextval = mod->symtab[i].st_value; |
|---|
| 3084 | } |
|---|
| 3085 | |
|---|
| 3086 | if (!best) |
|---|
| 3087 | return NULL; |
|---|
| 3088 | |
|---|
| 3089 | if (size) |
|---|
| 3090 | *size = nextval - mod->symtab[best].st_value; |
|---|
| 3091 | if (offset) |
|---|
| 3092 | *offset = addr - mod->symtab[best].st_value; |
|---|
| 3093 | return mod->strtab + mod->symtab[best].st_name; |
|---|
| 3094 | } |
|---|
| 3095 | |
|---|
| 3096 | /* For kallsyms to ask for address resolution. NULL means not found. Careful |
|---|
| 3097 | * not to lock to avoid deadlock on oopses, simply disable preemption. */ |
|---|
| 3098 | const char *module_address_lookup(unsigned long addr, |
|---|
| 3099 | unsigned long *size, |
|---|
| 3100 | unsigned long *offset, |
|---|
| 3101 | char **modname, |
|---|
| 3102 | char *namebuf) |
|---|
| 3103 | { |
|---|
| 3104 | struct module *mod; |
|---|
| 3105 | const char *ret = NULL; |
|---|
| 3106 | |
|---|
| 3107 | preempt_disable(); |
|---|
| 3108 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 3109 | if (within_module_init(addr, mod) || |
|---|
| 3110 | within_module_core(addr, mod)) { |
|---|
| 3111 | if (modname) |
|---|
| 3112 | *modname = mod->name; |
|---|
| 3113 | ret = get_ksymbol(mod, addr, size, offset); |
|---|
| 3114 | break; |
|---|
| 3115 | } |
|---|
| 3116 | } |
|---|
| 3117 | /* Make a copy in here where it's safe */ |
|---|
| 3118 | if (ret) { |
|---|
| 3119 | strncpy(namebuf, ret, KSYM_NAME_LEN - 1); |
|---|
| 3120 | ret = namebuf; |
|---|
| 3121 | } |
|---|
| 3122 | preempt_enable(); |
|---|
| 3123 | return ret; |
|---|
| 3124 | } |
|---|
| 3125 | |
|---|
| 3126 | int lookup_module_symbol_name(unsigned long addr, char *symname) |
|---|
| 3127 | { |
|---|
| 3128 | struct module *mod; |
|---|
| 3129 | |
|---|
| 3130 | preempt_disable(); |
|---|
| 3131 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 3132 | if (within_module_init(addr, mod) || |
|---|
| 3133 | within_module_core(addr, mod)) { |
|---|
| 3134 | const char *sym; |
|---|
| 3135 | |
|---|
| 3136 | sym = get_ksymbol(mod, addr, NULL, NULL); |
|---|
| 3137 | if (!sym) |
|---|
| 3138 | goto out; |
|---|
| 3139 | strlcpy(symname, sym, KSYM_NAME_LEN); |
|---|
| 3140 | preempt_enable(); |
|---|
| 3141 | return 0; |
|---|
| 3142 | } |
|---|
| 3143 | } |
|---|
| 3144 | out: |
|---|
| 3145 | preempt_enable(); |
|---|
| 3146 | return -ERANGE; |
|---|
| 3147 | } |
|---|
| 3148 | |
|---|
| 3149 | int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, |
|---|
| 3150 | unsigned long *offset, char *modname, char *name) |
|---|
| 3151 | { |
|---|
| 3152 | struct module *mod; |
|---|
| 3153 | |
|---|
| 3154 | preempt_disable(); |
|---|
| 3155 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 3156 | if (within_module_init(addr, mod) || |
|---|
| 3157 | within_module_core(addr, mod)) { |
|---|
| 3158 | const char *sym; |
|---|
| 3159 | |
|---|
| 3160 | sym = get_ksymbol(mod, addr, size, offset); |
|---|
| 3161 | if (!sym) |
|---|
| 3162 | goto out; |
|---|
| 3163 | if (modname) |
|---|
| 3164 | strlcpy(modname, mod->name, MODULE_NAME_LEN); |
|---|
| 3165 | if (name) |
|---|
| 3166 | strlcpy(name, sym, KSYM_NAME_LEN); |
|---|
| 3167 | preempt_enable(); |
|---|
| 3168 | return 0; |
|---|
| 3169 | } |
|---|
| 3170 | } |
|---|
| 3171 | out: |
|---|
| 3172 | preempt_enable(); |
|---|
| 3173 | return -ERANGE; |
|---|
| 3174 | } |
|---|
| 3175 | |
|---|
| 3176 | int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
|---|
| 3177 | char *name, char *module_name, int *exported) |
|---|
| 3178 | { |
|---|
| 3179 | struct module *mod; |
|---|
| 3180 | |
|---|
| 3181 | preempt_disable(); |
|---|
| 3182 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 3183 | if (symnum < mod->num_symtab) { |
|---|
| 3184 | *value = mod->symtab[symnum].st_value; |
|---|
| 3185 | *type = mod->symtab[symnum].st_info; |
|---|
| 3186 | strlcpy(name, mod->strtab + mod->symtab[symnum].st_name, |
|---|
| 3187 | KSYM_NAME_LEN); |
|---|
| 3188 | strlcpy(module_name, mod->name, MODULE_NAME_LEN); |
|---|
| 3189 | *exported = is_exported(name, *value, mod); |
|---|
| 3190 | preempt_enable(); |
|---|
| 3191 | return 0; |
|---|
| 3192 | } |
|---|
| 3193 | symnum -= mod->num_symtab; |
|---|
| 3194 | } |
|---|
| 3195 | preempt_enable(); |
|---|
| 3196 | return -ERANGE; |
|---|
| 3197 | } |
|---|
| 3198 | |
|---|
| 3199 | static unsigned long mod_find_symname(struct module *mod, const char *name) |
|---|
| 3200 | { |
|---|
| 3201 | unsigned int i; |
|---|
| 3202 | |
|---|
| 3203 | for (i = 0; i < mod->num_symtab; i++) |
|---|
| 3204 | if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && |
|---|
| 3205 | mod->symtab[i].st_info != 'U') |
|---|
| 3206 | return mod->symtab[i].st_value; |
|---|
| 3207 | return 0; |
|---|
| 3208 | } |
|---|
| 3209 | |
|---|
| 3210 | /* Look for this name: can be of form module:name. */ |
|---|
| 3211 | unsigned long module_kallsyms_lookup_name(const char *name) |
|---|
| 3212 | { |
|---|
| 3213 | struct module *mod; |
|---|
| 3214 | char *colon; |
|---|
| 3215 | unsigned long ret = 0; |
|---|
| 3216 | |
|---|
| 3217 | /* Don't lock: we're in enough trouble already. */ |
|---|
| 3218 | preempt_disable(); |
|---|
| 3219 | if ((colon = strchr(name, ':')) != NULL) { |
|---|
| 3220 | *colon = '\0'; |
|---|
| 3221 | if ((mod = find_module(name)) != NULL) |
|---|
| 3222 | ret = mod_find_symname(mod, colon+1); |
|---|
| 3223 | *colon = ':'; |
|---|
| 3224 | } else { |
|---|
| 3225 | list_for_each_entry_rcu(mod, &modules, list) |
|---|
| 3226 | if ((ret = mod_find_symname(mod, name)) != 0) |
|---|
| 3227 | break; |
|---|
| 3228 | } |
|---|
| 3229 | preempt_enable(); |
|---|
| 3230 | return ret; |
|---|
| 3231 | } |
|---|
| 3232 | |
|---|
| 3233 | int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, |
|---|
| 3234 | struct module *, unsigned long), |
|---|
| 3235 | void *data) |
|---|
| 3236 | { |
|---|
| 3237 | struct module *mod; |
|---|
| 3238 | unsigned int i; |
|---|
| 3239 | int ret; |
|---|
| 3240 | |
|---|
| 3241 | list_for_each_entry(mod, &modules, list) { |
|---|
| 3242 | for (i = 0; i < mod->num_symtab; i++) { |
|---|
| 3243 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, |
|---|
| 3244 | mod, mod->symtab[i].st_value); |
|---|
| 3245 | if (ret != 0) |
|---|
| 3246 | return ret; |
|---|
| 3247 | } |
|---|
| 3248 | } |
|---|
| 3249 | return 0; |
|---|
| 3250 | } |
|---|
| 3251 | #endif /* CONFIG_KALLSYMS */ |
|---|
| 3252 | |
|---|
| 3253 | static char *module_flags(struct module *mod, char *buf) |
|---|
| 3254 | { |
|---|
| 3255 | int bx = 0; |
|---|
| 3256 | |
|---|
| 3257 | if (mod->taints || |
|---|
| 3258 | mod->state == MODULE_STATE_GOING || |
|---|
| 3259 | mod->state == MODULE_STATE_COMING) { |
|---|
| 3260 | buf[bx++] = '('; |
|---|
| 3261 | if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE)) |
|---|
| 3262 | buf[bx++] = 'P'; |
|---|
| 3263 | else if (mod->taints & (1 << TAINT_OOT_MODULE)) |
|---|
| 3264 | buf[bx++] = 'O'; |
|---|
| 3265 | if (mod->taints & (1 << TAINT_FORCED_MODULE)) |
|---|
| 3266 | buf[bx++] = 'F'; |
|---|
| 3267 | if (mod->taints & (1 << TAINT_CRAP)) |
|---|
| 3268 | buf[bx++] = 'C'; |
|---|
| 3269 | /* |
|---|
| 3270 | * TAINT_FORCED_RMMOD: could be added. |
|---|
| 3271 | * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't |
|---|
| 3272 | * apply to modules. |
|---|
| 3273 | */ |
|---|
| 3274 | |
|---|
| 3275 | /* Show a - for module-is-being-unloaded */ |
|---|
| 3276 | if (mod->state == MODULE_STATE_GOING) |
|---|
| 3277 | buf[bx++] = '-'; |
|---|
| 3278 | /* Show a + for module-is-being-loaded */ |
|---|
| 3279 | if (mod->state == MODULE_STATE_COMING) |
|---|
| 3280 | buf[bx++] = '+'; |
|---|
| 3281 | buf[bx++] = ')'; |
|---|
| 3282 | } |
|---|
| 3283 | buf[bx] = '\0'; |
|---|
| 3284 | |
|---|
| 3285 | return buf; |
|---|
| 3286 | } |
|---|
| 3287 | |
|---|
| 3288 | #ifdef CONFIG_PROC_FS |
|---|
| 3289 | /* Called by the /proc file system to return a list of modules. */ |
|---|
| 3290 | static void *m_start(struct seq_file *m, loff_t *pos) |
|---|
| 3291 | { |
|---|
| 3292 | mutex_lock(&module_mutex); |
|---|
| 3293 | return seq_list_start(&modules, *pos); |
|---|
| 3294 | } |
|---|
| 3295 | |
|---|
| 3296 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) |
|---|
| 3297 | { |
|---|
| 3298 | return seq_list_next(p, &modules, pos); |
|---|
| 3299 | } |
|---|
| 3300 | |
|---|
| 3301 | static void m_stop(struct seq_file *m, void *p) |
|---|
| 3302 | { |
|---|
| 3303 | mutex_unlock(&module_mutex); |
|---|
| 3304 | } |
|---|
| 3305 | |
|---|
| 3306 | static int m_show(struct seq_file *m, void *p) |
|---|
| 3307 | { |
|---|
| 3308 | struct module *mod = list_entry(p, struct module, list); |
|---|
| 3309 | char buf[8]; |
|---|
| 3310 | |
|---|
| 3311 | seq_printf(m, "%s %u", |
|---|
| 3312 | mod->name, mod->init_size + mod->core_size); |
|---|
| 3313 | print_unload_info(m, mod); |
|---|
| 3314 | |
|---|
| 3315 | /* Informative for users. */ |
|---|
| 3316 | seq_printf(m, " %s", |
|---|
| 3317 | mod->state == MODULE_STATE_GOING ? "Unloading": |
|---|
| 3318 | mod->state == MODULE_STATE_COMING ? "Loading": |
|---|
| 3319 | "Live"); |
|---|
| 3320 | /* Used by oprofile and other similar tools. */ |
|---|
| 3321 | seq_printf(m, " 0x%pK", mod->module_core); |
|---|
| 3322 | |
|---|
| 3323 | /* Taints info */ |
|---|
| 3324 | if (mod->taints) |
|---|
| 3325 | seq_printf(m, " %s", module_flags(mod, buf)); |
|---|
| 3326 | |
|---|
| 3327 | seq_printf(m, "\n"); |
|---|
| 3328 | return 0; |
|---|
| 3329 | } |
|---|
| 3330 | |
|---|
| 3331 | /* Format: modulename size refcount deps address |
|---|
| 3332 | |
|---|
| 3333 | Where refcount is a number or -, and deps is a comma-separated list |
|---|
| 3334 | of depends or -. |
|---|
| 3335 | */ |
|---|
| 3336 | static const struct seq_operations modules_op = { |
|---|
| 3337 | .start = m_start, |
|---|
| 3338 | .next = m_next, |
|---|
| 3339 | .stop = m_stop, |
|---|
| 3340 | .show = m_show |
|---|
| 3341 | }; |
|---|
| 3342 | |
|---|
| 3343 | static int modules_open(struct inode *inode, struct file *file) |
|---|
| 3344 | { |
|---|
| 3345 | return seq_open(file, &modules_op); |
|---|
| 3346 | } |
|---|
| 3347 | |
|---|
| 3348 | static const struct file_operations proc_modules_operations = { |
|---|
| 3349 | .open = modules_open, |
|---|
| 3350 | .read = seq_read, |
|---|
| 3351 | .llseek = seq_lseek, |
|---|
| 3352 | .release = seq_release, |
|---|
| 3353 | }; |
|---|
| 3354 | |
|---|
| 3355 | static int __init proc_modules_init(void) |
|---|
| 3356 | { |
|---|
| 3357 | proc_create("modules", 0, NULL, &proc_modules_operations); |
|---|
| 3358 | return 0; |
|---|
| 3359 | } |
|---|
| 3360 | module_init(proc_modules_init); |
|---|
| 3361 | #endif |
|---|
| 3362 | |
|---|
| 3363 | /* Given an address, look for it in the module exception tables. */ |
|---|
| 3364 | const struct exception_table_entry *search_module_extables(unsigned long addr) |
|---|
| 3365 | { |
|---|
| 3366 | const struct exception_table_entry *e = NULL; |
|---|
| 3367 | struct module *mod; |
|---|
| 3368 | |
|---|
| 3369 | preempt_disable(); |
|---|
| 3370 | list_for_each_entry_rcu(mod, &modules, list) { |
|---|
| 3371 | if (mod->num_exentries == 0) |
|---|
| 3372 | continue; |
|---|
| 3373 | |
|---|
| 3374 | e = search_extable(mod->extable, |
|---|
| 3375 | mod->extable + mod->num_exentries - 1, |
|---|
| 3376 | addr); |
|---|
| 3377 | if (e) |
|---|
| 3378 | break; |
|---|
| 3379 | } |
|---|
| 3380 | preempt_enable(); |
|---|
| 3381 | |
|---|
| 3382 | /* Now, if we found one, we are running inside it now, hence |
|---|
| 3383 | we cannot unload the module, hence no refcnt needed. */ |
|---|
| 3384 | return e; |
|---|
| 3385 | } |
|---|
| 3386 | |
|---|
| 3387 | /* |
|---|
| 3388 | * is_module_address - is this address inside a module? |
|---|
| 3389 | * @addr: the address to check. |
|---|
| 3390 | * |
|---|
| 3391 | * See is_module_text_address() if you simply want to see if the address |
|---|
| 3392 | * is code (not data). |
|---|
| 3393 | */ |
|---|
| 3394 | bool is_module_address(unsigned long addr) |
|---|
| 3395 | { |
|---|
| 3396 | bool ret; |
|---|
| 3397 | |
|---|
| 3398 | preempt_disable(); |
|---|
| 3399 | ret = __module_address(addr) != NULL; |
|---|
| 3400 | preempt_enable(); |
|---|
| 3401 | |
|---|
| 3402 | return ret; |
|---|
| 3403 | } |
|---|
| 3404 | |
|---|
| 3405 | /* |
|---|
| 3406 | * __module_address - get the module which contains an address. |
|---|
| 3407 | * @addr: the address. |
|---|
| 3408 | * |
|---|
| 3409 | * Must be called with preempt disabled or module mutex held so that |
|---|
| 3410 | * module doesn't get freed during this. |
|---|
| 3411 | */ |
|---|
| 3412 | struct module *__module_address(unsigned long addr) |
|---|
| 3413 | { |
|---|
| 3414 | struct module *mod; |
|---|
| 3415 | |
|---|
| 3416 | if (addr < module_addr_min || addr > module_addr_max) |
|---|
| 3417 | return NULL; |
|---|
| 3418 | |
|---|
| 3419 | list_for_each_entry_rcu(mod, &modules, list) |
|---|
| 3420 | if (within_module_core(addr, mod) |
|---|
| 3421 | || within_module_init(addr, mod)) |
|---|
| 3422 | return mod; |
|---|
| 3423 | return NULL; |
|---|
| 3424 | } |
|---|
| 3425 | EXPORT_SYMBOL_GPL(__module_address); |
|---|
| 3426 | |
|---|
| 3427 | /* |
|---|
| 3428 | * is_module_text_address - is this address inside module code? |
|---|
| 3429 | * @addr: the address to check. |
|---|
| 3430 | * |
|---|
| 3431 | * See is_module_address() if you simply want to see if the address is |
|---|
| 3432 | * anywhere in a module. See kernel_text_address() for testing if an |
|---|
| 3433 | * address corresponds to kernel or module code. |
|---|
| 3434 | */ |
|---|
| 3435 | bool is_module_text_address(unsigned long addr) |
|---|
| 3436 | { |
|---|
| 3437 | bool ret; |
|---|
| 3438 | |
|---|
| 3439 | preempt_disable(); |
|---|
| 3440 | ret = __module_text_address(addr) != NULL; |
|---|
| 3441 | preempt_enable(); |
|---|
| 3442 | |
|---|
| 3443 | return ret; |
|---|
| 3444 | } |
|---|
| 3445 | |
|---|
| 3446 | /* |
|---|
| 3447 | * __module_text_address - get the module whose code contains an address. |
|---|
| 3448 | * @addr: the address. |
|---|
| 3449 | * |
|---|
| 3450 | * Must be called with preempt disabled or module mutex held so that |
|---|
| 3451 | * module doesn't get freed during this. |
|---|
| 3452 | */ |
|---|
| 3453 | struct module *__module_text_address(unsigned long addr) |
|---|
| 3454 | { |
|---|
| 3455 | struct module *mod = __module_address(addr); |
|---|
| 3456 | if (mod) { |
|---|
| 3457 | /* Make sure it's within the text section. */ |
|---|
| 3458 | if (!within(addr, mod->module_init, mod->init_text_size) |
|---|
| 3459 | && !within(addr, mod->module_core, mod->core_text_size)) |
|---|
| 3460 | mod = NULL; |
|---|
| 3461 | } |
|---|
| 3462 | return mod; |
|---|
| 3463 | } |
|---|
| 3464 | EXPORT_SYMBOL_GPL(__module_text_address); |
|---|
| 3465 | |
|---|
| 3466 | /* Don't grab lock, we're oopsing. */ |
|---|
| 3467 | void print_modules(void) |
|---|
| 3468 | { |
|---|
| 3469 | struct module *mod; |
|---|
| 3470 | char buf[8]; |
|---|
| 3471 | |
|---|
| 3472 | printk(KERN_DEFAULT "Modules linked in:"); |
|---|
| 3473 | /* Most callers should already have preempt disabled, but make sure */ |
|---|
| 3474 | preempt_disable(); |
|---|
| 3475 | list_for_each_entry_rcu(mod, &modules, list) |
|---|
| 3476 | printk(" %s%s", mod->name, module_flags(mod, buf)); |
|---|
| 3477 | preempt_enable(); |
|---|
| 3478 | if (last_unloaded_module[0]) |
|---|
| 3479 | printk(" [last unloaded: %s]", last_unloaded_module); |
|---|
| 3480 | printk("\n"); |
|---|
| 3481 | } |
|---|
| 3482 | |
|---|
| 3483 | #ifdef CONFIG_MODVERSIONS |
|---|
| 3484 | /* Generate the signature for all relevant module structures here. |
|---|
| 3485 | * If these change, we don't want to try to parse the module. */ |
|---|
| 3486 | void module_layout(struct module *mod, |
|---|
| 3487 | struct modversion_info *ver, |
|---|
| 3488 | struct kernel_param *kp, |
|---|
| 3489 | struct kernel_symbol *ks, |
|---|
| 3490 | struct tracepoint * const *tp) |
|---|
| 3491 | { |
|---|
| 3492 | } |
|---|
| 3493 | EXPORT_SYMBOL(module_layout); |
|---|
| 3494 | #endif |
|---|