1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2002 Richard Henderson 4 * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. 5 */ 6 7 #define INCLUDE_VERMAGIC 8 9 #include <linux/export.h> 10 #include <linux/extable.h> 11 #include <linux/moduleloader.h> 12 #include <linux/module_signature.h> 13 #include <linux/trace_events.h> 14 #include <linux/init.h> 15 #include <linux/kallsyms.h> 16 #include <linux/buildid.h> 17 #include <linux/fs.h> 18 #include <linux/kernel.h> 19 #include <linux/kernel_read_file.h> 20 #include <linux/kstrtox.h> 21 #include <linux/slab.h> 22 #include <linux/vmalloc.h> 23 #include <linux/elf.h> 24 #include <linux/seq_file.h> 25 #include <linux/syscalls.h> 26 #include <linux/fcntl.h> 27 #include <linux/rcupdate.h> 28 #include <linux/capability.h> 29 #include <linux/cpu.h> 30 #include <linux/moduleparam.h> 31 #include <linux/errno.h> 32 #include <linux/err.h> 33 #include <linux/vermagic.h> 34 #include <linux/notifier.h> 35 #include <linux/sched.h> 36 #include <linux/device.h> 37 #include <linux/string.h> 38 #include <linux/mutex.h> 39 #include <linux/rculist.h> 40 #include <linux/uaccess.h> 41 #include <asm/cacheflush.h> 42 #include <linux/set_memory.h> 43 #include <asm/mmu_context.h> 44 #include <linux/license.h> 45 #include <asm/sections.h> 46 #include <linux/tracepoint.h> 47 #include <linux/ftrace.h> 48 #include <linux/livepatch.h> 49 #include <linux/async.h> 50 #include <linux/percpu.h> 51 #include <linux/kmemleak.h> 52 #include <linux/jump_label.h> 53 #include <linux/pfn.h> 54 #include <linux/bsearch.h> 55 #include <linux/dynamic_debug.h> 56 #include <linux/audit.h> 57 #include <linux/cfi.h> 58 #include <uapi/linux/module.h> 59 #include "internal.h" 60 61 #define CREATE_TRACE_POINTS 62 #include <trace/events/module.h> 63 64 /* 65 * Mutex protects: 66 * 1) List of modules (also safely readable with preempt_disable), 67 * 2) module_use links, 68 * 3) mod_tree.addr_min/mod_tree.addr_max. 69 * (delete and add uses RCU list operations). 70 */ 71 DEFINE_MUTEX(module_mutex); 72 LIST_HEAD(modules); 73 74 /* Work queue for freeing init sections in success case */ 75 static void do_free_init(struct work_struct *w); 76 static DECLARE_WORK(init_free_wq, do_free_init); 77 static LLIST_HEAD(init_free_list); 78 79 struct mod_tree_root mod_tree __cacheline_aligned = { 80 .addr_min = -1UL, 81 }; 82 83 struct symsearch { 84 const struct kernel_symbol *start, *stop; 85 const s32 *crcs; 86 enum mod_license license; 87 }; 88 89 /* 90 * Bounds of module memory, for speeding up __module_address. 91 * Protected by module_mutex. 92 */ 93 static void __mod_update_bounds(enum mod_mem_type type __maybe_unused, void *base, 94 unsigned int size, struct mod_tree_root *tree) 95 { 96 unsigned long min = (unsigned long)base; 97 unsigned long max = min + size; 98 99 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 100 if (mod_mem_type_is_core_data(type)) { 101 if (min < tree->data_addr_min) 102 tree->data_addr_min = min; 103 if (max > tree->data_addr_max) 104 tree->data_addr_max = max; 105 return; 106 } 107 #endif 108 if (min < tree->addr_min) 109 tree->addr_min = min; 110 if (max > tree->addr_max) 111 tree->addr_max = max; 112 } 113 114 static void mod_update_bounds(struct module *mod) 115 { 116 for_each_mod_mem_type(type) { 117 struct module_memory *mod_mem = &mod->mem[type]; 118 119 if (mod_mem->size) 120 __mod_update_bounds(type, mod_mem->base, mod_mem->size, &mod_tree); 121 } 122 } 123 124 /* Block module loading/unloading? */ 125 int modules_disabled; 126 core_param(nomodule, modules_disabled, bint, 0); 127 128 /* Waiting for a module to finish initializing? */ 129 static DECLARE_WAIT_QUEUE_HEAD(module_wq); 130 131 static BLOCKING_NOTIFIER_HEAD(module_notify_list); 132 133 int register_module_notifier(struct notifier_block *nb) 134 { 135 return blocking_notifier_chain_register(&module_notify_list, nb); 136 } 137 EXPORT_SYMBOL(register_module_notifier); 138 139 int unregister_module_notifier(struct notifier_block *nb) 140 { 141 return blocking_notifier_chain_unregister(&module_notify_list, nb); 142 } 143 EXPORT_SYMBOL(unregister_module_notifier); 144 145 /* 146 * We require a truly strong try_module_get(): 0 means success. 147 * Otherwise an error is returned due to ongoing or failed 148 * initialization etc. 149 */ 150 static inline int strong_try_module_get(struct module *mod) 151 { 152 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED); 153 if (mod && mod->state == MODULE_STATE_COMING) 154 return -EBUSY; 155 if (try_module_get(mod)) 156 return 0; 157 else 158 return -ENOENT; 159 } 160 161 static inline void add_taint_module(struct module *mod, unsigned flag, 162 enum lockdep_ok lockdep_ok) 163 { 164 add_taint(flag, lockdep_ok); 165 set_bit(flag, &mod->taints); 166 } 167 168 /* 169 * A thread that wants to hold a reference to a module only while it 170 * is running can call this to safely exit. 171 */ 172 void __noreturn __module_put_and_kthread_exit(struct module *mod, long code) 173 { 174 module_put(mod); 175 kthread_exit(code); 176 } 177 EXPORT_SYMBOL(__module_put_and_kthread_exit); 178 179 /* Find a module section: 0 means not found. */ 180 static unsigned int find_sec(const struct load_info *info, const char *name) 181 { 182 unsigned int i; 183 184 for (i = 1; i < info->hdr->e_shnum; i++) { 185 Elf_Shdr *shdr = &info->sechdrs[i]; 186 /* Alloc bit cleared means "ignore it." */ 187 if ((shdr->sh_flags & SHF_ALLOC) 188 && strcmp(info->secstrings + shdr->sh_name, name) == 0) 189 return i; 190 } 191 return 0; 192 } 193 194 /* Find a module section, or NULL. */ 195 static void *section_addr(const struct load_info *info, const char *name) 196 { 197 /* Section 0 has sh_addr 0. */ 198 return (void *)info->sechdrs[find_sec(info, name)].sh_addr; 199 } 200 201 /* Find a module section, or NULL. Fill in number of "objects" in section. */ 202 static void *section_objs(const struct load_info *info, 203 const char *name, 204 size_t object_size, 205 unsigned int *num) 206 { 207 unsigned int sec = find_sec(info, name); 208 209 /* Section 0 has sh_addr 0 and sh_size 0. */ 210 *num = info->sechdrs[sec].sh_size / object_size; 211 return (void *)info->sechdrs[sec].sh_addr; 212 } 213 214 /* Find a module section: 0 means not found. Ignores SHF_ALLOC flag. */ 215 static unsigned int find_any_sec(const struct load_info *info, const char *name) 216 { 217 unsigned int i; 218 219 for (i = 1; i < info->hdr->e_shnum; i++) { 220 Elf_Shdr *shdr = &info->sechdrs[i]; 221 if (strcmp(info->secstrings + shdr->sh_name, name) == 0) 222 return i; 223 } 224 return 0; 225 } 226 227 /* 228 * Find a module section, or NULL. Fill in number of "objects" in section. 229 * Ignores SHF_ALLOC flag. 230 */ 231 static __maybe_unused void *any_section_objs(const struct load_info *info, 232 const char *name, 233 size_t object_size, 234 unsigned int *num) 235 { 236 unsigned int sec = find_any_sec(info, name); 237 238 /* Section 0 has sh_addr 0 and sh_size 0. */ 239 *num = info->sechdrs[sec].sh_size / object_size; 240 return (void *)info->sechdrs[sec].sh_addr; 241 } 242 243 #ifndef CONFIG_MODVERSIONS 244 #define symversion(base, idx) NULL 245 #else 246 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) 247 #endif 248 249 static const char *kernel_symbol_name(const struct kernel_symbol *sym) 250 { 251 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS 252 return offset_to_ptr(&sym->name_offset); 253 #else 254 return sym->name; 255 #endif 256 } 257 258 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym) 259 { 260 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS 261 if (!sym->namespace_offset) 262 return NULL; 263 return offset_to_ptr(&sym->namespace_offset); 264 #else 265 return sym->namespace; 266 #endif 267 } 268 269 int cmp_name(const void *name, const void *sym) 270 { 271 return strcmp(name, kernel_symbol_name(sym)); 272 } 273 274 static bool find_exported_symbol_in_section(const struct symsearch *syms, 275 struct module *owner, 276 struct find_symbol_arg *fsa) 277 { 278 struct kernel_symbol *sym; 279 280 if (!fsa->gplok && syms->license == GPL_ONLY) 281 return false; 282 283 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start, 284 sizeof(struct kernel_symbol), cmp_name); 285 if (!sym) 286 return false; 287 288 fsa->owner = owner; 289 fsa->crc = symversion(syms->crcs, sym - syms->start); 290 fsa->sym = sym; 291 fsa->license = syms->license; 292 293 return true; 294 } 295 296 /* 297 * Find an exported symbol and return it, along with, (optional) crc and 298 * (optional) module which owns it. Needs preempt disabled or module_mutex. 299 */ 300 bool find_symbol(struct find_symbol_arg *fsa) 301 { 302 static const struct symsearch arr[] = { 303 { __start___ksymtab, __stop___ksymtab, __start___kcrctab, 304 NOT_GPL_ONLY }, 305 { __start___ksymtab_gpl, __stop___ksymtab_gpl, 306 __start___kcrctab_gpl, 307 GPL_ONLY }, 308 }; 309 struct module *mod; 310 unsigned int i; 311 312 module_assert_mutex_or_preempt(); 313 314 for (i = 0; i < ARRAY_SIZE(arr); i++) 315 if (find_exported_symbol_in_section(&arr[i], NULL, fsa)) 316 return true; 317 318 list_for_each_entry_rcu(mod, &modules, list, 319 lockdep_is_held(&module_mutex)) { 320 struct symsearch arr[] = { 321 { mod->syms, mod->syms + mod->num_syms, mod->crcs, 322 NOT_GPL_ONLY }, 323 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms, 324 mod->gpl_crcs, 325 GPL_ONLY }, 326 }; 327 328 if (mod->state == MODULE_STATE_UNFORMED) 329 continue; 330 331 for (i = 0; i < ARRAY_SIZE(arr); i++) 332 if (find_exported_symbol_in_section(&arr[i], mod, fsa)) 333 return true; 334 } 335 336 pr_debug("Failed to find symbol %s\n", fsa->name); 337 return false; 338 } 339 340 /* 341 * Search for module by name: must hold module_mutex (or preempt disabled 342 * for read-only access). 343 */ 344 struct module *find_module_all(const char *name, size_t len, 345 bool even_unformed) 346 { 347 struct module *mod; 348 349 module_assert_mutex_or_preempt(); 350 351 list_for_each_entry_rcu(mod, &modules, list, 352 lockdep_is_held(&module_mutex)) { 353 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) 354 continue; 355 if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) 356 return mod; 357 } 358 return NULL; 359 } 360 361 struct module *find_module(const char *name) 362 { 363 return find_module_all(name, strlen(name), false); 364 } 365 366 #ifdef CONFIG_SMP 367 368 static inline void __percpu *mod_percpu(struct module *mod) 369 { 370 return mod->percpu; 371 } 372 373 static int percpu_modalloc(struct module *mod, struct load_info *info) 374 { 375 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu]; 376 unsigned long align = pcpusec->sh_addralign; 377 378 if (!pcpusec->sh_size) 379 return 0; 380 381 if (align > PAGE_SIZE) { 382 pr_warn("%s: per-cpu alignment %li > %li\n", 383 mod->name, align, PAGE_SIZE); 384 align = PAGE_SIZE; 385 } 386 387 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align); 388 if (!mod->percpu) { 389 pr_warn("%s: Could not allocate %lu bytes percpu data\n", 390 mod->name, (unsigned long)pcpusec->sh_size); 391 return -ENOMEM; 392 } 393 mod->percpu_size = pcpusec->sh_size; 394 return 0; 395 } 396 397 static void percpu_modfree(struct module *mod) 398 { 399 free_percpu(mod->percpu); 400 } 401 402 static unsigned int find_pcpusec(struct load_info *info) 403 { 404 return find_sec(info, ".data..percpu"); 405 } 406 407 static void percpu_modcopy(struct module *mod, 408 const void *from, unsigned long size) 409 { 410 int cpu; 411 412 for_each_possible_cpu(cpu) 413 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size); 414 } 415 416 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr) 417 { 418 struct module *mod; 419 unsigned int cpu; 420 421 preempt_disable(); 422 423 list_for_each_entry_rcu(mod, &modules, list) { 424 if (mod->state == MODULE_STATE_UNFORMED) 425 continue; 426 if (!mod->percpu_size) 427 continue; 428 for_each_possible_cpu(cpu) { 429 void *start = per_cpu_ptr(mod->percpu, cpu); 430 void *va = (void *)addr; 431 432 if (va >= start && va < start + mod->percpu_size) { 433 if (can_addr) { 434 *can_addr = (unsigned long) (va - start); 435 *can_addr += (unsigned long) 436 per_cpu_ptr(mod->percpu, 437 get_boot_cpu_id()); 438 } 439 preempt_enable(); 440 return true; 441 } 442 } 443 } 444 445 preempt_enable(); 446 return false; 447 } 448 449 /** 450 * is_module_percpu_address() - test whether address is from module static percpu 451 * @addr: address to test 452 * 453 * Test whether @addr belongs to module static percpu area. 454 * 455 * Return: %true if @addr is from module static percpu area 456 */ 457 bool is_module_percpu_address(unsigned long addr) 458 { 459 return __is_module_percpu_address(addr, NULL); 460 } 461 462 #else /* ... !CONFIG_SMP */ 463 464 static inline void __percpu *mod_percpu(struct module *mod) 465 { 466 return NULL; 467 } 468 static int percpu_modalloc(struct module *mod, struct load_info *info) 469 { 470 /* UP modules shouldn't have this section: ENOMEM isn't quite right */ 471 if (info->sechdrs[info->index.pcpu].sh_size != 0) 472 return -ENOMEM; 473 return 0; 474 } 475 static inline void percpu_modfree(struct module *mod) 476 { 477 } 478 static unsigned int find_pcpusec(struct load_info *info) 479 { 480 return 0; 481 } 482 static inline void percpu_modcopy(struct module *mod, 483 const void *from, unsigned long size) 484 { 485 /* pcpusec should be 0, and size of that section should be 0. */ 486 BUG_ON(size != 0); 487 } 488 bool is_module_percpu_address(unsigned long addr) 489 { 490 return false; 491 } 492 493 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr) 494 { 495 return false; 496 } 497 498 #endif /* CONFIG_SMP */ 499 500 #define MODINFO_ATTR(field) \ 501 static void setup_modinfo_##field(struct module *mod, const char *s) \ 502 { \ 503 mod->field = kstrdup(s, GFP_KERNEL); \ 504 } \ 505 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ 506 struct module_kobject *mk, char *buffer) \ 507 { \ 508 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \ 509 } \ 510 static int modinfo_##field##_exists(struct module *mod) \ 511 { \ 512 return mod->field != NULL; \ 513 } \ 514 static void free_modinfo_##field(struct module *mod) \ 515 { \ 516 kfree(mod->field); \ 517 mod->field = NULL; \ 518 } \ 519 static struct module_attribute modinfo_##field = { \ 520 .attr = { .name = __stringify(field), .mode = 0444 }, \ 521 .show = show_modinfo_##field, \ 522 .setup = setup_modinfo_##field, \ 523 .test = modinfo_##field##_exists, \ 524 .free = free_modinfo_##field, \ 525 }; 526 527 MODINFO_ATTR(version); 528 MODINFO_ATTR(srcversion); 529 530 static struct { 531 char name[MODULE_NAME_LEN + 1]; 532 char taints[MODULE_FLAGS_BUF_SIZE]; 533 } last_unloaded_module; 534 535 #ifdef CONFIG_MODULE_UNLOAD 536 537 EXPORT_TRACEPOINT_SYMBOL(module_get); 538 539 /* MODULE_REF_BASE is the base reference count by kmodule loader. */ 540 #define MODULE_REF_BASE 1 541 542 /* Init the unload section of the module. */ 543 static int module_unload_init(struct module *mod) 544 { 545 /* 546 * Initialize reference counter to MODULE_REF_BASE. 547 * refcnt == 0 means module is going. 548 */ 549 atomic_set(&mod->refcnt, MODULE_REF_BASE); 550 551 INIT_LIST_HEAD(&mod->source_list); 552 INIT_LIST_HEAD(&mod->target_list); 553 554 /* Hold reference count during initialization. */ 555 atomic_inc(&mod->refcnt); 556 557 return 0; 558 } 559 560 /* Does a already use b? */ 561 static int already_uses(struct module *a, struct module *b) 562 { 563 struct module_use *use; 564 565 list_for_each_entry(use, &b->source_list, source_list) { 566 if (use->source == a) { 567 pr_debug("%s uses %s!\n", a->name, b->name); 568 return 1; 569 } 570 } 571 pr_debug("%s does not use %s!\n", a->name, b->name); 572 return 0; 573 } 574 575 /* 576 * Module a uses b 577 * - we add 'a' as a "source", 'b' as a "target" of module use 578 * - the module_use is added to the list of 'b' sources (so 579 * 'b' can walk the list to see who sourced them), and of 'a' 580 * targets (so 'a' can see what modules it targets). 581 */ 582 static int add_module_usage(struct module *a, struct module *b) 583 { 584 struct module_use *use; 585 586 pr_debug("Allocating new usage for %s.\n", a->name); 587 use = kmalloc(sizeof(*use), GFP_ATOMIC); 588 if (!use) 589 return -ENOMEM; 590 591 use->source = a; 592 use->target = b; 593 list_add(&use->source_list, &b->source_list); 594 list_add(&use->target_list, &a->target_list); 595 return 0; 596 } 597 598 /* Module a uses b: caller needs module_mutex() */ 599 static int ref_module(struct module *a, struct module *b) 600 { 601 int err; 602 603 if (b == NULL || already_uses(a, b)) 604 return 0; 605 606 /* If module isn't available, we fail. */ 607 err = strong_try_module_get(b); 608 if (err) 609 return err; 610 611 err = add_module_usage(a, b); 612 if (err) { 613 module_put(b); 614 return err; 615 } 616 return 0; 617 } 618 619 /* Clear the unload stuff of the module. */ 620 static void module_unload_free(struct module *mod) 621 { 622 struct module_use *use, *tmp; 623 624 mutex_lock(&module_mutex); 625 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) { 626 struct module *i = use->target; 627 pr_debug("%s unusing %s\n", mod->name, i->name); 628 module_put(i); 629 list_del(&use->source_list); 630 list_del(&use->target_list); 631 kfree(use); 632 } 633 mutex_unlock(&module_mutex); 634 } 635 636 #ifdef CONFIG_MODULE_FORCE_UNLOAD 637 static inline int try_force_unload(unsigned int flags) 638 { 639 int ret = (flags & O_TRUNC); 640 if (ret) 641 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE); 642 return ret; 643 } 644 #else 645 static inline int try_force_unload(unsigned int flags) 646 { 647 return 0; 648 } 649 #endif /* CONFIG_MODULE_FORCE_UNLOAD */ 650 651 /* Try to release refcount of module, 0 means success. */ 652 static int try_release_module_ref(struct module *mod) 653 { 654 int ret; 655 656 /* Try to decrement refcnt which we set at loading */ 657 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt); 658 BUG_ON(ret < 0); 659 if (ret) 660 /* Someone can put this right now, recover with checking */ 661 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0); 662 663 return ret; 664 } 665 666 static int try_stop_module(struct module *mod, int flags, int *forced) 667 { 668 /* If it's not unused, quit unless we're forcing. */ 669 if (try_release_module_ref(mod) != 0) { 670 *forced = try_force_unload(flags); 671 if (!(*forced)) 672 return -EWOULDBLOCK; 673 } 674 675 /* Mark it as dying. */ 676 mod->state = MODULE_STATE_GOING; 677 678 return 0; 679 } 680 681 /** 682 * module_refcount() - return the refcount or -1 if unloading 683 * @mod: the module we're checking 684 * 685 * Return: 686 * -1 if the module is in the process of unloading 687 * otherwise the number of references in the kernel to the module 688 */ 689 int module_refcount(struct module *mod) 690 { 691 return atomic_read(&mod->refcnt) - MODULE_REF_BASE; 692 } 693 EXPORT_SYMBOL(module_refcount); 694 695 /* This exists whether we can unload or not */ 696 static void free_module(struct module *mod); 697 698 SYSCALL_DEFINE2(delete_module, const char __user *, name_user, 699 unsigned int, flags) 700 { 701 struct module *mod; 702 char name[MODULE_NAME_LEN]; 703 char buf[MODULE_FLAGS_BUF_SIZE]; 704 int ret, forced = 0; 705 706 if (!capable(CAP_SYS_MODULE) || modules_disabled) 707 return -EPERM; 708 709 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) 710 return -EFAULT; 711 name[MODULE_NAME_LEN-1] = '\0'; 712 713 audit_log_kern_module(name); 714 715 if (mutex_lock_interruptible(&module_mutex) != 0) 716 return -EINTR; 717 718 mod = find_module(name); 719 if (!mod) { 720 ret = -ENOENT; 721 goto out; 722 } 723 724 if (!list_empty(&mod->source_list)) { 725 /* Other modules depend on us: get rid of them first. */ 726 ret = -EWOULDBLOCK; 727 goto out; 728 } 729 730 /* Doing init or already dying? */ 731 if (mod->state != MODULE_STATE_LIVE) { 732 /* FIXME: if (force), slam module count damn the torpedoes */ 733 pr_debug("%s already dying\n", mod->name); 734 ret = -EBUSY; 735 goto out; 736 } 737 738 /* If it has an init func, it must have an exit func to unload */ 739 if (mod->init && !mod->exit) { 740 forced = try_force_unload(flags); 741 if (!forced) { 742 /* This module can't be removed */ 743 ret = -EBUSY; 744 goto out; 745 } 746 } 747 748 ret = try_stop_module(mod, flags, &forced); 749 if (ret != 0) 750 goto out; 751 752 mutex_unlock(&module_mutex); 753 /* Final destruction now no one is using it. */ 754 if (mod->exit != NULL) 755 mod->exit(); 756 blocking_notifier_call_chain(&module_notify_list, 757 MODULE_STATE_GOING, mod); 758 klp_module_going(mod); 759 ftrace_release_mod(mod); 760 761 async_synchronize_full(); 762 763 /* Store the name and taints of the last unloaded module for diagnostic purposes */ 764 strscpy(last_unloaded_module.name, mod->name, sizeof(last_unloaded_module.name)); 765 strscpy(last_unloaded_module.taints, module_flags(mod, buf, false), sizeof(last_unloaded_module.taints)); 766 767 free_module(mod); 768 /* someone could wait for the module in add_unformed_module() */ 769 wake_up_all(&module_wq); 770 return 0; 771 out: 772 mutex_unlock(&module_mutex); 773 return ret; 774 } 775 776 void __symbol_put(const char *symbol) 777 { 778 struct find_symbol_arg fsa = { 779 .name = symbol, 780 .gplok = true, 781 }; 782 783 preempt_disable(); 784 BUG_ON(!find_symbol(&fsa)); 785 module_put(fsa.owner); 786 preempt_enable(); 787 } 788 EXPORT_SYMBOL(__symbol_put); 789 790 /* Note this assumes addr is a function, which it currently always is. */ 791 void symbol_put_addr(void *addr) 792 { 793 struct module *modaddr; 794 unsigned long a = (unsigned long)dereference_function_descriptor(addr); 795 796 if (core_kernel_text(a)) 797 return; 798 799 /* 800 * Even though we hold a reference on the module; we still need to 801 * disable preemption in order to safely traverse the data structure. 802 */ 803 preempt_disable(); 804 modaddr = __module_text_address(a); 805 BUG_ON(!modaddr); 806 module_put(modaddr); 807 preempt_enable(); 808 } 809 EXPORT_SYMBOL_GPL(symbol_put_addr); 810 811 static ssize_t show_refcnt(struct module_attribute *mattr, 812 struct module_kobject *mk, char *buffer) 813 { 814 return sprintf(buffer, "%i\n", module_refcount(mk->mod)); 815 } 816 817 static struct module_attribute modinfo_refcnt = 818 __ATTR(refcnt, 0444, show_refcnt, NULL); 819 820 void __module_get(struct module *module) 821 { 822 if (module) { 823 preempt_disable(); 824 atomic_inc(&module->refcnt); 825 trace_module_get(module, _RET_IP_); 826 preempt_enable(); 827 } 828 } 829 EXPORT_SYMBOL(__module_get); 830 831 bool try_module_get(struct module *module) 832 { 833 bool ret = true; 834 835 if (module) { 836 preempt_disable(); 837 /* Note: here, we can fail to get a reference */ 838 if (likely(module_is_live(module) && 839 atomic_inc_not_zero(&module->refcnt) != 0)) 840 trace_module_get(module, _RET_IP_); 841 else 842 ret = false; 843 844 preempt_enable(); 845 } 846 return ret; 847 } 848 EXPORT_SYMBOL(try_module_get); 849 850 void module_put(struct module *module) 851 { 852 int ret; 853 854 if (module) { 855 preempt_disable(); 856 ret = atomic_dec_if_positive(&module->refcnt); 857 WARN_ON(ret < 0); /* Failed to put refcount */ 858 trace_module_put(module, _RET_IP_); 859 preempt_enable(); 860 } 861 } 862 EXPORT_SYMBOL(module_put); 863 864 #else /* !CONFIG_MODULE_UNLOAD */ 865 static inline void module_unload_free(struct module *mod) 866 { 867 } 868 869 static int ref_module(struct module *a, struct module *b) 870 { 871 return strong_try_module_get(b); 872 } 873 874 static inline int module_unload_init(struct module *mod) 875 { 876 return 0; 877 } 878 #endif /* CONFIG_MODULE_UNLOAD */ 879 880 size_t module_flags_taint(unsigned long taints, char *buf) 881 { 882 size_t l = 0; 883 int i; 884 885 for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 886 if (taint_flags[i].module && test_bit(i, &taints)) 887 buf[l++] = taint_flags[i].c_true; 888 } 889 890 return l; 891 } 892 893 static ssize_t show_initstate(struct module_attribute *mattr, 894 struct module_kobject *mk, char *buffer) 895 { 896 const char *state = "unknown"; 897 898 switch (mk->mod->state) { 899 case MODULE_STATE_LIVE: 900 state = "live"; 901 break; 902 case MODULE_STATE_COMING: 903 state = "coming"; 904 break; 905 case MODULE_STATE_GOING: 906 state = "going"; 907 break; 908 default: 909 BUG(); 910 } 911 return sprintf(buffer, "%s\n", state); 912 } 913 914 static struct module_attribute modinfo_initstate = 915 __ATTR(initstate, 0444, show_initstate, NULL); 916 917 static ssize_t store_uevent(struct module_attribute *mattr, 918 struct module_kobject *mk, 919 const char *buffer, size_t count) 920 { 921 int rc; 922 923 rc = kobject_synth_uevent(&mk->kobj, buffer, count); 924 return rc ? rc : count; 925 } 926 927 struct module_attribute module_uevent = 928 __ATTR(uevent, 0200, NULL, store_uevent); 929 930 static ssize_t show_coresize(struct module_attribute *mattr, 931 struct module_kobject *mk, char *buffer) 932 { 933 unsigned int size = mk->mod->mem[MOD_TEXT].size; 934 935 if (!IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)) { 936 for_class_mod_mem_type(type, core_data) 937 size += mk->mod->mem[type].size; 938 } 939 return sprintf(buffer, "%u\n", size); 940 } 941 942 static struct module_attribute modinfo_coresize = 943 __ATTR(coresize, 0444, show_coresize, NULL); 944 945 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 946 static ssize_t show_datasize(struct module_attribute *mattr, 947 struct module_kobject *mk, char *buffer) 948 { 949 unsigned int size = 0; 950 951 for_class_mod_mem_type(type, core_data) 952 size += mk->mod->mem[type].size; 953 return sprintf(buffer, "%u\n", size); 954 } 955 956 static struct module_attribute modinfo_datasize = 957 __ATTR(datasize, 0444, show_datasize, NULL); 958 #endif 959 960 static ssize_t show_initsize(struct module_attribute *mattr, 961 struct module_kobject *mk, char *buffer) 962 { 963 unsigned int size = 0; 964 965 for_class_mod_mem_type(type, init) 966 size += mk->mod->mem[type].size; 967 return sprintf(buffer, "%u\n", size); 968 } 969 970 static struct module_attribute modinfo_initsize = 971 __ATTR(initsize, 0444, show_initsize, NULL); 972 973 static ssize_t show_taint(struct module_attribute *mattr, 974 struct module_kobject *mk, char *buffer) 975 { 976 size_t l; 977 978 l = module_flags_taint(mk->mod->taints, buffer); 979 buffer[l++] = '\n'; 980 return l; 981 } 982 983 static struct module_attribute modinfo_taint = 984 __ATTR(taint, 0444, show_taint, NULL); 985 986 struct module_attribute *modinfo_attrs[] = { 987 &module_uevent, 988 &modinfo_version, 989 &modinfo_srcversion, 990 &modinfo_initstate, 991 &modinfo_coresize, 992 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 993 &modinfo_datasize, 994 #endif 995 &modinfo_initsize, 996 &modinfo_taint, 997 #ifdef CONFIG_MODULE_UNLOAD 998 &modinfo_refcnt, 999 #endif 1000 NULL, 1001 }; 1002 1003 size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs); 1004 1005 static const char vermagic[] = VERMAGIC_STRING; 1006 1007 int try_to_force_load(struct module *mod, const char *reason) 1008 { 1009 #ifdef CONFIG_MODULE_FORCE_LOAD 1010 if (!test_taint(TAINT_FORCED_MODULE)) 1011 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason); 1012 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE); 1013 return 0; 1014 #else 1015 return -ENOEXEC; 1016 #endif 1017 } 1018 1019 static char *get_modinfo(const struct load_info *info, const char *tag); 1020 static char *get_next_modinfo(const struct load_info *info, const char *tag, 1021 char *prev); 1022 1023 static int verify_namespace_is_imported(const struct load_info *info, 1024 const struct kernel_symbol *sym, 1025 struct module *mod) 1026 { 1027 const char *namespace; 1028 char *imported_namespace; 1029 1030 namespace = kernel_symbol_namespace(sym); 1031 if (namespace && namespace[0]) { 1032 imported_namespace = get_modinfo(info, "import_ns"); 1033 while (imported_namespace) { 1034 if (strcmp(namespace, imported_namespace) == 0) 1035 return 0; 1036 imported_namespace = get_next_modinfo( 1037 info, "import_ns", imported_namespace); 1038 } 1039 #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS 1040 pr_warn( 1041 #else 1042 pr_err( 1043 #endif 1044 "%s: module uses symbol (%s) from namespace %s, but does not import it.\n", 1045 mod->name, kernel_symbol_name(sym), namespace); 1046 #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS 1047 return -EINVAL; 1048 #endif 1049 } 1050 return 0; 1051 } 1052 1053 static bool inherit_taint(struct module *mod, struct module *owner, const char *name) 1054 { 1055 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints)) 1056 return true; 1057 1058 if (mod->using_gplonly_symbols) { 1059 pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n", 1060 mod->name, name, owner->name); 1061 return false; 1062 } 1063 1064 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) { 1065 pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n", 1066 mod->name, name, owner->name); 1067 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints); 1068 } 1069 return true; 1070 } 1071 1072 /* Resolve a symbol for this module. I.e. if we find one, record usage. */ 1073 static const struct kernel_symbol *resolve_symbol(struct module *mod, 1074 const struct load_info *info, 1075 const char *name, 1076 char ownername[]) 1077 { 1078 struct find_symbol_arg fsa = { 1079 .name = name, 1080 .gplok = !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), 1081 .warn = true, 1082 }; 1083 int err; 1084 1085 /* 1086 * The module_mutex should not be a heavily contended lock; 1087 * if we get the occasional sleep here, we'll go an extra iteration 1088 * in the wait_event_interruptible(), which is harmless. 1089 */ 1090 sched_annotate_sleep(); 1091 mutex_lock(&module_mutex); 1092 if (!find_symbol(&fsa)) 1093 goto unlock; 1094 1095 if (fsa.license == GPL_ONLY) 1096 mod->using_gplonly_symbols = true; 1097 1098 if (!inherit_taint(mod, fsa.owner, name)) { 1099 fsa.sym = NULL; 1100 goto getname; 1101 } 1102 1103 if (!check_version(info, name, mod, fsa.crc)) { 1104 fsa.sym = ERR_PTR(-EINVAL); 1105 goto getname; 1106 } 1107 1108 err = verify_namespace_is_imported(info, fsa.sym, mod); 1109 if (err) { 1110 fsa.sym = ERR_PTR(err); 1111 goto getname; 1112 } 1113 1114 err = ref_module(mod, fsa.owner); 1115 if (err) { 1116 fsa.sym = ERR_PTR(err); 1117 goto getname; 1118 } 1119 1120 getname: 1121 /* We must make copy under the lock if we failed to get ref. */ 1122 strncpy(ownername, module_name(fsa.owner), MODULE_NAME_LEN); 1123 unlock: 1124 mutex_unlock(&module_mutex); 1125 return fsa.sym; 1126 } 1127 1128 static const struct kernel_symbol * 1129 resolve_symbol_wait(struct module *mod, 1130 const struct load_info *info, 1131 const char *name) 1132 { 1133 const struct kernel_symbol *ksym; 1134 char owner[MODULE_NAME_LEN]; 1135 1136 if (wait_event_interruptible_timeout(module_wq, 1137 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner)) 1138 || PTR_ERR(ksym) != -EBUSY, 1139 30 * HZ) <= 0) { 1140 pr_warn("%s: gave up waiting for init of module %s.\n", 1141 mod->name, owner); 1142 } 1143 return ksym; 1144 } 1145 1146 void __weak module_memfree(void *module_region) 1147 { 1148 /* 1149 * This memory may be RO, and freeing RO memory in an interrupt is not 1150 * supported by vmalloc. 1151 */ 1152 WARN_ON(in_interrupt()); 1153 vfree(module_region); 1154 } 1155 1156 void __weak module_arch_cleanup(struct module *mod) 1157 { 1158 } 1159 1160 void __weak module_arch_freeing_init(struct module *mod) 1161 { 1162 } 1163 1164 static bool mod_mem_use_vmalloc(enum mod_mem_type type) 1165 { 1166 return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && 1167 mod_mem_type_is_core_data(type); 1168 } 1169 1170 static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) 1171 { 1172 if (mod_mem_use_vmalloc(type)) 1173 return vzalloc(size); 1174 return module_alloc(size); 1175 } 1176 1177 static void module_memory_free(void *ptr, enum mod_mem_type type) 1178 { 1179 if (mod_mem_use_vmalloc(type)) 1180 vfree(ptr); 1181 else 1182 module_memfree(ptr); 1183 } 1184 1185 static void free_mod_mem(struct module *mod) 1186 { 1187 for_each_mod_mem_type(type) { 1188 struct module_memory *mod_mem = &mod->mem[type]; 1189 1190 if (type == MOD_DATA) 1191 continue; 1192 1193 /* Free lock-classes; relies on the preceding sync_rcu(). */ 1194 lockdep_free_key_range(mod_mem->base, mod_mem->size); 1195 if (mod_mem->size) 1196 module_memory_free(mod_mem->base, type); 1197 } 1198 1199 /* MOD_DATA hosts mod, so free it at last */ 1200 lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); 1201 module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); 1202 } 1203 1204 /* Free a module, remove from lists, etc. */ 1205 static void free_module(struct module *mod) 1206 { 1207 trace_module_free(mod); 1208 1209 mod_sysfs_teardown(mod); 1210 1211 /* 1212 * We leave it in list to prevent duplicate loads, but make sure 1213 * that noone uses it while it's being deconstructed. 1214 */ 1215 mutex_lock(&module_mutex); 1216 mod->state = MODULE_STATE_UNFORMED; 1217 mutex_unlock(&module_mutex); 1218 1219 /* Arch-specific cleanup. */ 1220 module_arch_cleanup(mod); 1221 1222 /* Module unload stuff */ 1223 module_unload_free(mod); 1224 1225 /* Free any allocated parameters. */ 1226 destroy_params(mod->kp, mod->num_kp); 1227 1228 if (is_livepatch_module(mod)) 1229 free_module_elf(mod); 1230 1231 /* Now we can delete it from the lists */ 1232 mutex_lock(&module_mutex); 1233 /* Unlink carefully: kallsyms could be walking list. */ 1234 list_del_rcu(&mod->list); 1235 mod_tree_remove(mod); 1236 /* Remove this module from bug list, this uses list_del_rcu */ 1237 module_bug_cleanup(mod); 1238 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */ 1239 synchronize_rcu(); 1240 if (try_add_tainted_module(mod)) 1241 pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n", 1242 mod->name); 1243 mutex_unlock(&module_mutex); 1244 1245 /* This may be empty, but that's OK */ 1246 module_arch_freeing_init(mod); 1247 kfree(mod->args); 1248 percpu_modfree(mod); 1249 1250 free_mod_mem(mod); 1251 } 1252 1253 void *__symbol_get(const char *symbol) 1254 { 1255 struct find_symbol_arg fsa = { 1256 .name = symbol, 1257 .gplok = true, 1258 .warn = true, 1259 }; 1260 1261 preempt_disable(); 1262 if (!find_symbol(&fsa) || strong_try_module_get(fsa.owner)) { 1263 preempt_enable(); 1264 return NULL; 1265 } 1266 preempt_enable(); 1267 return (void *)kernel_symbol_value(fsa.sym); 1268 } 1269 EXPORT_SYMBOL_GPL(__symbol_get); 1270 1271 /* 1272 * Ensure that an exported symbol [global namespace] does not already exist 1273 * in the kernel or in some other module's exported symbol table. 1274 * 1275 * You must hold the module_mutex. 1276 */ 1277 static int verify_exported_symbols(struct module *mod) 1278 { 1279 unsigned int i; 1280 const struct kernel_symbol *s; 1281 struct { 1282 const struct kernel_symbol *sym; 1283 unsigned int num; 1284 } arr[] = { 1285 { mod->syms, mod->num_syms }, 1286 { mod->gpl_syms, mod->num_gpl_syms }, 1287 }; 1288 1289 for (i = 0; i < ARRAY_SIZE(arr); i++) { 1290 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { 1291 struct find_symbol_arg fsa = { 1292 .name = kernel_symbol_name(s), 1293 .gplok = true, 1294 }; 1295 if (find_symbol(&fsa)) { 1296 pr_err("%s: exports duplicate symbol %s" 1297 " (owned by %s)\n", 1298 mod->name, kernel_symbol_name(s), 1299 module_name(fsa.owner)); 1300 return -ENOEXEC; 1301 } 1302 } 1303 } 1304 return 0; 1305 } 1306 1307 static bool ignore_undef_symbol(Elf_Half emachine, const char *name) 1308 { 1309 /* 1310 * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as 1311 * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64. 1312 * i386 has a similar problem but may not deserve a fix. 1313 * 1314 * If we ever have to ignore many symbols, consider refactoring the code to 1315 * only warn if referenced by a relocation. 1316 */ 1317 if (emachine == EM_386 || emachine == EM_X86_64) 1318 return !strcmp(name, "_GLOBAL_OFFSET_TABLE_"); 1319 return false; 1320 } 1321 1322 /* Change all symbols so that st_value encodes the pointer directly. */ 1323 static int simplify_symbols(struct module *mod, const struct load_info *info) 1324 { 1325 Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; 1326 Elf_Sym *sym = (void *)symsec->sh_addr; 1327 unsigned long secbase; 1328 unsigned int i; 1329 int ret = 0; 1330 const struct kernel_symbol *ksym; 1331 1332 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) { 1333 const char *name = info->strtab + sym[i].st_name; 1334 1335 switch (sym[i].st_shndx) { 1336 case SHN_COMMON: 1337 /* Ignore common symbols */ 1338 if (!strncmp(name, "__gnu_lto", 9)) 1339 break; 1340 1341 /* 1342 * We compiled with -fno-common. These are not 1343 * supposed to happen. 1344 */ 1345 pr_debug("Common symbol: %s\n", name); 1346 pr_warn("%s: please compile with -fno-common\n", 1347 mod->name); 1348 ret = -ENOEXEC; 1349 break; 1350 1351 case SHN_ABS: 1352 /* Don't need to do anything */ 1353 pr_debug("Absolute symbol: 0x%08lx\n", 1354 (long)sym[i].st_value); 1355 break; 1356 1357 case SHN_LIVEPATCH: 1358 /* Livepatch symbols are resolved by livepatch */ 1359 break; 1360 1361 case SHN_UNDEF: 1362 ksym = resolve_symbol_wait(mod, info, name); 1363 /* Ok if resolved. */ 1364 if (ksym && !IS_ERR(ksym)) { 1365 sym[i].st_value = kernel_symbol_value(ksym); 1366 break; 1367 } 1368 1369 /* Ok if weak or ignored. */ 1370 if (!ksym && 1371 (ELF_ST_BIND(sym[i].st_info) == STB_WEAK || 1372 ignore_undef_symbol(info->hdr->e_machine, name))) 1373 break; 1374 1375 ret = PTR_ERR(ksym) ?: -ENOENT; 1376 pr_warn("%s: Unknown symbol %s (err %d)\n", 1377 mod->name, name, ret); 1378 break; 1379 1380 default: 1381 /* Divert to percpu allocation if a percpu var. */ 1382 if (sym[i].st_shndx == info->index.pcpu) 1383 secbase = (unsigned long)mod_percpu(mod); 1384 else 1385 secbase = info->sechdrs[sym[i].st_shndx].sh_addr; 1386 sym[i].st_value += secbase; 1387 break; 1388 } 1389 } 1390 1391 return ret; 1392 } 1393 1394 static int apply_relocations(struct module *mod, const struct load_info *info) 1395 { 1396 unsigned int i; 1397 int err = 0; 1398 1399 /* Now do relocations. */ 1400 for (i = 1; i < info->hdr->e_shnum; i++) { 1401 unsigned int infosec = info->sechdrs[i].sh_info; 1402 1403 /* Not a valid relocation section? */ 1404 if (infosec >= info->hdr->e_shnum) 1405 continue; 1406 1407 /* Don't bother with non-allocated sections */ 1408 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC)) 1409 continue; 1410 1411 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH) 1412 err = klp_apply_section_relocs(mod, info->sechdrs, 1413 info->secstrings, 1414 info->strtab, 1415 info->index.sym, i, 1416 NULL); 1417 else if (info->sechdrs[i].sh_type == SHT_REL) 1418 err = apply_relocate(info->sechdrs, info->strtab, 1419 info->index.sym, i, mod); 1420 else if (info->sechdrs[i].sh_type == SHT_RELA) 1421 err = apply_relocate_add(info->sechdrs, info->strtab, 1422 info->index.sym, i, mod); 1423 if (err < 0) 1424 break; 1425 } 1426 return err; 1427 } 1428 1429 /* Additional bytes needed by arch in front of individual sections */ 1430 unsigned int __weak arch_mod_section_prepend(struct module *mod, 1431 unsigned int section) 1432 { 1433 /* default implementation just returns zero */ 1434 return 0; 1435 } 1436 1437 long module_get_offset_and_type(struct module *mod, enum mod_mem_type type, 1438 Elf_Shdr *sechdr, unsigned int section) 1439 { 1440 long offset; 1441 long mask = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK) << SH_ENTSIZE_TYPE_SHIFT; 1442 1443 mod->mem[type].size += arch_mod_section_prepend(mod, section); 1444 offset = ALIGN(mod->mem[type].size, sechdr->sh_addralign ?: 1); 1445 mod->mem[type].size = offset + sechdr->sh_size; 1446 1447 WARN_ON_ONCE(offset & mask); 1448 return offset | mask; 1449 } 1450 1451 static bool module_init_layout_section(const char *sname) 1452 { 1453 #ifndef CONFIG_MODULE_UNLOAD 1454 if (module_exit_section(sname)) 1455 return true; 1456 #endif 1457 return module_init_section(sname); 1458 } 1459 1460 static void __layout_sections(struct module *mod, struct load_info *info, bool is_init) 1461 { 1462 unsigned int m, i; 1463 1464 static const unsigned long masks[][2] = { 1465 /* 1466 * NOTE: all executable code must be the first section 1467 * in this array; otherwise modify the text_size 1468 * finder in the two loops below 1469 */ 1470 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, 1471 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, 1472 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL }, 1473 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, 1474 { ARCH_SHF_SMALL | SHF_ALLOC, 0 } 1475 }; 1476 static const int core_m_to_mem_type[] = { 1477 MOD_TEXT, 1478 MOD_RODATA, 1479 MOD_RO_AFTER_INIT, 1480 MOD_DATA, 1481 MOD_INVALID, /* This is needed to match the masks array */ 1482 }; 1483 static const int init_m_to_mem_type[] = { 1484 MOD_INIT_TEXT, 1485 MOD_INIT_RODATA, 1486 MOD_INVALID, 1487 MOD_INIT_DATA, 1488 MOD_INVALID, /* This is needed to match the masks array */ 1489 }; 1490 1491 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1492 enum mod_mem_type type = is_init ? init_m_to_mem_type[m] : core_m_to_mem_type[m]; 1493 1494 for (i = 0; i < info->hdr->e_shnum; ++i) { 1495 Elf_Shdr *s = &info->sechdrs[i]; 1496 const char *sname = info->secstrings + s->sh_name; 1497 1498 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1499 || (s->sh_flags & masks[m][1]) 1500 || s->sh_entsize != ~0UL 1501 || is_init != module_init_layout_section(sname)) 1502 continue; 1503 1504 if (WARN_ON_ONCE(type == MOD_INVALID)) 1505 continue; 1506 1507 s->sh_entsize = module_get_offset_and_type(mod, type, s, i); 1508 pr_debug("\t%s\n", sname); 1509 } 1510 } 1511 } 1512 1513 /* 1514 * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld 1515 * might -- code, read-only data, read-write data, small data. Tally 1516 * sizes, and place the offsets into sh_entsize fields: high bit means it 1517 * belongs in init. 1518 */ 1519 static void layout_sections(struct module *mod, struct load_info *info) 1520 { 1521 unsigned int i; 1522 1523 for (i = 0; i < info->hdr->e_shnum; i++) 1524 info->sechdrs[i].sh_entsize = ~0UL; 1525 1526 pr_debug("Core section allocation order:\n"); 1527 __layout_sections(mod, info, false); 1528 1529 pr_debug("Init section allocation order:\n"); 1530 __layout_sections(mod, info, true); 1531 } 1532 1533 static void set_license(struct module *mod, const char *license) 1534 { 1535 if (!license) 1536 license = "unspecified"; 1537 1538 if (!license_is_gpl_compatible(license)) { 1539 if (!test_taint(TAINT_PROPRIETARY_MODULE)) 1540 pr_warn("%s: module license '%s' taints kernel.\n", 1541 mod->name, license); 1542 add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 1543 LOCKDEP_NOW_UNRELIABLE); 1544 } 1545 } 1546 1547 /* Parse tag=value strings from .modinfo section */ 1548 static char *next_string(char *string, unsigned long *secsize) 1549 { 1550 /* Skip non-zero chars */ 1551 while (string[0]) { 1552 string++; 1553 if ((*secsize)-- <= 1) 1554 return NULL; 1555 } 1556 1557 /* Skip any zero padding. */ 1558 while (!string[0]) { 1559 string++; 1560 if ((*secsize)-- <= 1) 1561 return NULL; 1562 } 1563 return string; 1564 } 1565 1566 static char *get_next_modinfo(const struct load_info *info, const char *tag, 1567 char *prev) 1568 { 1569 char *p; 1570 unsigned int taglen = strlen(tag); 1571 Elf_Shdr *infosec = &info->sechdrs[info->index.info]; 1572 unsigned long size = infosec->sh_size; 1573 1574 /* 1575 * get_modinfo() calls made before rewrite_section_headers() 1576 * must use sh_offset, as sh_addr isn't set! 1577 */ 1578 char *modinfo = (char *)info->hdr + infosec->sh_offset; 1579 1580 if (prev) { 1581 size -= prev - modinfo; 1582 modinfo = next_string(prev, &size); 1583 } 1584 1585 for (p = modinfo; p; p = next_string(p, &size)) { 1586 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1587 return p + taglen + 1; 1588 } 1589 return NULL; 1590 } 1591 1592 static char *get_modinfo(const struct load_info *info, const char *tag) 1593 { 1594 return get_next_modinfo(info, tag, NULL); 1595 } 1596 1597 static void setup_modinfo(struct module *mod, struct load_info *info) 1598 { 1599 struct module_attribute *attr; 1600 int i; 1601 1602 for (i = 0; (attr = modinfo_attrs[i]); i++) { 1603 if (attr->setup) 1604 attr->setup(mod, get_modinfo(info, attr->attr.name)); 1605 } 1606 } 1607 1608 static void free_modinfo(struct module *mod) 1609 { 1610 struct module_attribute *attr; 1611 int i; 1612 1613 for (i = 0; (attr = modinfo_attrs[i]); i++) { 1614 if (attr->free) 1615 attr->free(mod); 1616 } 1617 } 1618 1619 void * __weak module_alloc(unsigned long size) 1620 { 1621 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 1622 GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, 1623 NUMA_NO_NODE, __builtin_return_address(0)); 1624 } 1625 1626 bool __weak module_init_section(const char *name) 1627 { 1628 return strstarts(name, ".init"); 1629 } 1630 1631 bool __weak module_exit_section(const char *name) 1632 { 1633 return strstarts(name, ".exit"); 1634 } 1635 1636 static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) 1637 { 1638 #if defined(CONFIG_64BIT) 1639 unsigned long long secend; 1640 #else 1641 unsigned long secend; 1642 #endif 1643 1644 /* 1645 * Check for both overflow and offset/size being 1646 * too large. 1647 */ 1648 secend = shdr->sh_offset + shdr->sh_size; 1649 if (secend < shdr->sh_offset || secend > info->len) 1650 return -ENOEXEC; 1651 1652 return 0; 1653 } 1654 1655 /* 1656 * Sanity checks against invalid binaries, wrong arch, weird elf version. 1657 * 1658 * Also do basic validity checks against section offsets and sizes, the 1659 * section name string table, and the indices used for it (sh_name). 1660 */ 1661 static int elf_validity_check(struct load_info *info) 1662 { 1663 unsigned int i; 1664 Elf_Shdr *shdr, *strhdr; 1665 int err; 1666 1667 if (info->len < sizeof(*(info->hdr))) { 1668 pr_err("Invalid ELF header len %lu\n", info->len); 1669 goto no_exec; 1670 } 1671 1672 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0) { 1673 pr_err("Invalid ELF header magic: != %s\n", ELFMAG); 1674 goto no_exec; 1675 } 1676 if (info->hdr->e_type != ET_REL) { 1677 pr_err("Invalid ELF header type: %u != %u\n", 1678 info->hdr->e_type, ET_REL); 1679 goto no_exec; 1680 } 1681 if (!elf_check_arch(info->hdr)) { 1682 pr_err("Invalid architecture in ELF header: %u\n", 1683 info->hdr->e_machine); 1684 goto no_exec; 1685 } 1686 if (!module_elf_check_arch(info->hdr)) { 1687 pr_err("Invalid module architecture in ELF header: %u\n", 1688 info->hdr->e_machine); 1689 goto no_exec; 1690 } 1691 if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) { 1692 pr_err("Invalid ELF section header size\n"); 1693 goto no_exec; 1694 } 1695 1696 /* 1697 * e_shnum is 16 bits, and sizeof(Elf_Shdr) is 1698 * known and small. So e_shnum * sizeof(Elf_Shdr) 1699 * will not overflow unsigned long on any platform. 1700 */ 1701 if (info->hdr->e_shoff >= info->len 1702 || (info->hdr->e_shnum * sizeof(Elf_Shdr) > 1703 info->len - info->hdr->e_shoff)) { 1704 pr_err("Invalid ELF section header overflow\n"); 1705 goto no_exec; 1706 } 1707 1708 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff; 1709 1710 /* 1711 * Verify if the section name table index is valid. 1712 */ 1713 if (info->hdr->e_shstrndx == SHN_UNDEF 1714 || info->hdr->e_shstrndx >= info->hdr->e_shnum) { 1715 pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n", 1716 info->hdr->e_shstrndx, info->hdr->e_shstrndx, 1717 info->hdr->e_shnum); 1718 goto no_exec; 1719 } 1720 1721 strhdr = &info->sechdrs[info->hdr->e_shstrndx]; 1722 err = validate_section_offset(info, strhdr); 1723 if (err < 0) { 1724 pr_err("Invalid ELF section hdr(type %u)\n", strhdr->sh_type); 1725 return err; 1726 } 1727 1728 /* 1729 * The section name table must be NUL-terminated, as required 1730 * by the spec. This makes strcmp and pr_* calls that access 1731 * strings in the section safe. 1732 */ 1733 info->secstrings = (void *)info->hdr + strhdr->sh_offset; 1734 if (strhdr->sh_size == 0) { 1735 pr_err("empty section name table\n"); 1736 goto no_exec; 1737 } 1738 if (info->secstrings[strhdr->sh_size - 1] != '\0') { 1739 pr_err("ELF Spec violation: section name table isn't null terminated\n"); 1740 goto no_exec; 1741 } 1742 1743 /* 1744 * The code assumes that section 0 has a length of zero and 1745 * an addr of zero, so check for it. 1746 */ 1747 if (info->sechdrs[0].sh_type != SHT_NULL 1748 || info->sechdrs[0].sh_size != 0 1749 || info->sechdrs[0].sh_addr != 0) { 1750 pr_err("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n", 1751 info->sechdrs[0].sh_type); 1752 goto no_exec; 1753 } 1754 1755 for (i = 1; i < info->hdr->e_shnum; i++) { 1756 shdr = &info->sechdrs[i]; 1757 switch (shdr->sh_type) { 1758 case SHT_NULL: 1759 case SHT_NOBITS: 1760 continue; 1761 case SHT_SYMTAB: 1762 if (shdr->sh_link == SHN_UNDEF 1763 || shdr->sh_link >= info->hdr->e_shnum) { 1764 pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", 1765 shdr->sh_link, shdr->sh_link, 1766 info->hdr->e_shnum); 1767 goto no_exec; 1768 } 1769 fallthrough; 1770 default: 1771 err = validate_section_offset(info, shdr); 1772 if (err < 0) { 1773 pr_err("Invalid ELF section in module (section %u type %u)\n", 1774 i, shdr->sh_type); 1775 return err; 1776 } 1777 1778 if (shdr->sh_flags & SHF_ALLOC) { 1779 if (shdr->sh_name >= strhdr->sh_size) { 1780 pr_err("Invalid ELF section name in module (section %u type %u)\n", 1781 i, shdr->sh_type); 1782 return -ENOEXEC; 1783 } 1784 } 1785 break; 1786 } 1787 } 1788 1789 return 0; 1790 1791 no_exec: 1792 return -ENOEXEC; 1793 } 1794 1795 #define COPY_CHUNK_SIZE (16*PAGE_SIZE) 1796 1797 static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len) 1798 { 1799 do { 1800 unsigned long n = min(len, COPY_CHUNK_SIZE); 1801 1802 if (copy_from_user(dst, usrc, n) != 0) 1803 return -EFAULT; 1804 cond_resched(); 1805 dst += n; 1806 usrc += n; 1807 len -= n; 1808 } while (len); 1809 return 0; 1810 } 1811 1812 static int check_modinfo_livepatch(struct module *mod, struct load_info *info) 1813 { 1814 if (!get_modinfo(info, "livepatch")) 1815 /* Nothing more to do */ 1816 return 0; 1817 1818 if (set_livepatch_module(mod)) { 1819 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); 1820 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", 1821 mod->name); 1822 return 0; 1823 } 1824 1825 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled", 1826 mod->name); 1827 return -ENOEXEC; 1828 } 1829 1830 static void check_modinfo_retpoline(struct module *mod, struct load_info *info) 1831 { 1832 if (retpoline_module_ok(get_modinfo(info, "retpoline"))) 1833 return; 1834 1835 pr_warn("%s: loading module not compiled with retpoline compiler.\n", 1836 mod->name); 1837 } 1838 1839 /* Sets info->hdr and info->len. */ 1840 static int copy_module_from_user(const void __user *umod, unsigned long len, 1841 struct load_info *info) 1842 { 1843 int err; 1844 1845 info->len = len; 1846 if (info->len < sizeof(*(info->hdr))) 1847 return -ENOEXEC; 1848 1849 err = security_kernel_load_data(LOADING_MODULE, true); 1850 if (err) 1851 return err; 1852 1853 /* Suck in entire file: we'll want most of it. */ 1854 info->hdr = __vmalloc(info->len, GFP_KERNEL | __GFP_NOWARN); 1855 if (!info->hdr) 1856 return -ENOMEM; 1857 1858 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) { 1859 err = -EFAULT; 1860 goto out; 1861 } 1862 1863 err = security_kernel_post_load_data((char *)info->hdr, info->len, 1864 LOADING_MODULE, "init_module"); 1865 out: 1866 if (err) 1867 vfree(info->hdr); 1868 1869 return err; 1870 } 1871 1872 static void free_copy(struct load_info *info, int flags) 1873 { 1874 if (flags & MODULE_INIT_COMPRESSED_FILE) 1875 module_decompress_cleanup(info); 1876 else 1877 vfree(info->hdr); 1878 } 1879 1880 static int rewrite_section_headers(struct load_info *info, int flags) 1881 { 1882 unsigned int i; 1883 1884 /* This should always be true, but let's be sure. */ 1885 info->sechdrs[0].sh_addr = 0; 1886 1887 for (i = 1; i < info->hdr->e_shnum; i++) { 1888 Elf_Shdr *shdr = &info->sechdrs[i]; 1889 1890 /* 1891 * Mark all sections sh_addr with their address in the 1892 * temporary image. 1893 */ 1894 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset; 1895 1896 } 1897 1898 /* Track but don't keep modinfo and version sections. */ 1899 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; 1900 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; 1901 1902 return 0; 1903 } 1904 1905 /* 1906 * Set up our basic convenience variables (pointers to section headers, 1907 * search for module section index etc), and do some basic section 1908 * verification. 1909 * 1910 * Set info->mod to the temporary copy of the module in info->hdr. The final one 1911 * will be allocated in move_module(). 1912 */ 1913 static int setup_load_info(struct load_info *info, int flags) 1914 { 1915 unsigned int i; 1916 1917 /* Try to find a name early so we can log errors with a module name */ 1918 info->index.info = find_sec(info, ".modinfo"); 1919 if (info->index.info) 1920 info->name = get_modinfo(info, "name"); 1921 1922 /* Find internal symbols and strings. */ 1923 for (i = 1; i < info->hdr->e_shnum; i++) { 1924 if (info->sechdrs[i].sh_type == SHT_SYMTAB) { 1925 info->index.sym = i; 1926 info->index.str = info->sechdrs[i].sh_link; 1927 info->strtab = (char *)info->hdr 1928 + info->sechdrs[info->index.str].sh_offset; 1929 break; 1930 } 1931 } 1932 1933 if (info->index.sym == 0) { 1934 pr_warn("%s: module has no symbols (stripped?)\n", 1935 info->name ?: "(missing .modinfo section or name field)"); 1936 return -ENOEXEC; 1937 } 1938 1939 info->index.mod = find_sec(info, ".gnu.linkonce.this_module"); 1940 if (!info->index.mod) { 1941 pr_warn("%s: No module found in object\n", 1942 info->name ?: "(missing .modinfo section or name field)"); 1943 return -ENOEXEC; 1944 } 1945 /* This is temporary: point mod into copy of data. */ 1946 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset; 1947 1948 /* 1949 * If we didn't load the .modinfo 'name' field earlier, fall back to 1950 * on-disk struct mod 'name' field. 1951 */ 1952 if (!info->name) 1953 info->name = info->mod->name; 1954 1955 if (flags & MODULE_INIT_IGNORE_MODVERSIONS) 1956 info->index.vers = 0; /* Pretend no __versions section! */ 1957 else 1958 info->index.vers = find_sec(info, "__versions"); 1959 1960 info->index.pcpu = find_pcpusec(info); 1961 1962 return 0; 1963 } 1964 1965 static int check_modinfo(struct module *mod, struct load_info *info, int flags) 1966 { 1967 const char *modmagic = get_modinfo(info, "vermagic"); 1968 int err; 1969 1970 if (flags & MODULE_INIT_IGNORE_VERMAGIC) 1971 modmagic = NULL; 1972 1973 /* This is allowed: modprobe --force will invalidate it. */ 1974 if (!modmagic) { 1975 err = try_to_force_load(mod, "bad vermagic"); 1976 if (err) 1977 return err; 1978 } else if (!same_magic(modmagic, vermagic, info->index.vers)) { 1979 pr_err("%s: version magic '%s' should be '%s'\n", 1980 info->name, modmagic, vermagic); 1981 return -ENOEXEC; 1982 } 1983 1984 if (!get_modinfo(info, "intree")) { 1985 if (!test_taint(TAINT_OOT_MODULE)) 1986 pr_warn("%s: loading out-of-tree module taints kernel.\n", 1987 mod->name); 1988 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK); 1989 } 1990 1991 check_modinfo_retpoline(mod, info); 1992 1993 if (get_modinfo(info, "staging")) { 1994 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK); 1995 pr_warn("%s: module is from the staging directory, the quality " 1996 "is unknown, you have been warned.\n", mod->name); 1997 } 1998 1999 err = check_modinfo_livepatch(mod, info); 2000 if (err) 2001 return err; 2002 2003 /* Set up license info based on the info section */ 2004 set_license(mod, get_modinfo(info, "license")); 2005 2006 if (get_modinfo(info, "test")) { 2007 if (!test_taint(TAINT_TEST)) 2008 pr_warn("%s: loading test module taints kernel.\n", 2009 mod->name); 2010 add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK); 2011 } 2012 2013 return 0; 2014 } 2015 2016 static int find_module_sections(struct module *mod, struct load_info *info) 2017 { 2018 mod->kp = section_objs(info, "__param", 2019 sizeof(*mod->kp), &mod->num_kp); 2020 mod->syms = section_objs(info, "__ksymtab", 2021 sizeof(*mod->syms), &mod->num_syms); 2022 mod->crcs = section_addr(info, "__kcrctab"); 2023 mod->gpl_syms = section_objs(info, "__ksymtab_gpl", 2024 sizeof(*mod->gpl_syms), 2025 &mod->num_gpl_syms); 2026 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl"); 2027 2028 #ifdef CONFIG_CONSTRUCTORS 2029 mod->ctors = section_objs(info, ".ctors", 2030 sizeof(*mod->ctors), &mod->num_ctors); 2031 if (!mod->ctors) 2032 mod->ctors = section_objs(info, ".init_array", 2033 sizeof(*mod->ctors), &mod->num_ctors); 2034 else if (find_sec(info, ".init_array")) { 2035 /* 2036 * This shouldn't happen with same compiler and binutils 2037 * building all parts of the module. 2038 */ 2039 pr_warn("%s: has both .ctors and .init_array.\n", 2040 mod->name); 2041 return -EINVAL; 2042 } 2043 #endif 2044 2045 mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1, 2046 &mod->noinstr_text_size); 2047 2048 #ifdef CONFIG_TRACEPOINTS 2049 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs", 2050 sizeof(*mod->tracepoints_ptrs), 2051 &mod->num_tracepoints); 2052 #endif 2053 #ifdef CONFIG_TREE_SRCU 2054 mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs", 2055 sizeof(*mod->srcu_struct_ptrs), 2056 &mod->num_srcu_structs); 2057 #endif 2058 #ifdef CONFIG_BPF_EVENTS 2059 mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map", 2060 sizeof(*mod->bpf_raw_events), 2061 &mod->num_bpf_raw_events); 2062 #endif 2063 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 2064 mod->btf_data = any_section_objs(info, ".BTF", 1, &mod->btf_data_size); 2065 #endif 2066 #ifdef CONFIG_JUMP_LABEL 2067 mod->jump_entries = section_objs(info, "__jump_table", 2068 sizeof(*mod->jump_entries), 2069 &mod->num_jump_entries); 2070 #endif 2071 #ifdef CONFIG_EVENT_TRACING 2072 mod->trace_events = section_objs(info, "_ftrace_events", 2073 sizeof(*mod->trace_events), 2074 &mod->num_trace_events); 2075 mod->trace_evals = section_objs(info, "_ftrace_eval_map", 2076 sizeof(*mod->trace_evals), 2077 &mod->num_trace_evals); 2078 #endif 2079 #ifdef CONFIG_TRACING 2080 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt", 2081 sizeof(*mod->trace_bprintk_fmt_start), 2082 &mod->num_trace_bprintk_fmt); 2083 #endif 2084 #ifdef CONFIG_FTRACE_MCOUNT_RECORD 2085 /* sechdrs[0].sh_size is always zero */ 2086 mod->ftrace_callsites = section_objs(info, FTRACE_CALLSITE_SECTION, 2087 sizeof(*mod->ftrace_callsites), 2088 &mod->num_ftrace_callsites); 2089 #endif 2090 #ifdef CONFIG_FUNCTION_ERROR_INJECTION 2091 mod->ei_funcs = section_objs(info, "_error_injection_whitelist", 2092 sizeof(*mod->ei_funcs), 2093 &mod->num_ei_funcs); 2094 #endif 2095 #ifdef CONFIG_KPROBES 2096 mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1, 2097 &mod->kprobes_text_size); 2098 mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist", 2099 sizeof(unsigned long), 2100 &mod->num_kprobe_blacklist); 2101 #endif 2102 #ifdef CONFIG_PRINTK_INDEX 2103 mod->printk_index_start = section_objs(info, ".printk_index", 2104 sizeof(*mod->printk_index_start), 2105 &mod->printk_index_size); 2106 #endif 2107 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE 2108 mod->static_call_sites = section_objs(info, ".static_call_sites", 2109 sizeof(*mod->static_call_sites), 2110 &mod->num_static_call_sites); 2111 #endif 2112 #if IS_ENABLED(CONFIG_KUNIT) 2113 mod->kunit_suites = section_objs(info, ".kunit_test_suites", 2114 sizeof(*mod->kunit_suites), 2115 &mod->num_kunit_suites); 2116 #endif 2117 2118 mod->extable = section_objs(info, "__ex_table", 2119 sizeof(*mod->extable), &mod->num_exentries); 2120 2121 if (section_addr(info, "__obsparm")) 2122 pr_warn("%s: Ignoring obsolete parameters\n", mod->name); 2123 2124 #ifdef CONFIG_DYNAMIC_DEBUG_CORE 2125 mod->dyndbg_info.descs = section_objs(info, "__dyndbg", 2126 sizeof(*mod->dyndbg_info.descs), 2127 &mod->dyndbg_info.num_descs); 2128 mod->dyndbg_info.classes = section_objs(info, "__dyndbg_classes", 2129 sizeof(*mod->dyndbg_info.classes), 2130 &mod->dyndbg_info.num_classes); 2131 #endif 2132 2133 return 0; 2134 } 2135 2136 static int move_module(struct module *mod, struct load_info *info) 2137 { 2138 int i; 2139 void *ptr; 2140 enum mod_mem_type t; 2141 2142 for_each_mod_mem_type(type) { 2143 if (!mod->mem[type].size) { 2144 mod->mem[type].base = NULL; 2145 continue; 2146 } 2147 mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size); 2148 ptr = module_memory_alloc(mod->mem[type].size, type); 2149 2150 /* 2151 * The pointer to this block is stored in the module structure 2152 * which is inside the block. Just mark it as not being a 2153 * leak. 2154 */ 2155 kmemleak_ignore(ptr); 2156 if (!ptr) { 2157 t = type; 2158 goto out_enomem; 2159 } 2160 memset(ptr, 0, mod->mem[type].size); 2161 mod->mem[type].base = ptr; 2162 } 2163 2164 /* Transfer each section which specifies SHF_ALLOC */ 2165 pr_debug("final section addresses:\n"); 2166 for (i = 0; i < info->hdr->e_shnum; i++) { 2167 void *dest; 2168 Elf_Shdr *shdr = &info->sechdrs[i]; 2169 enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; 2170 2171 if (!(shdr->sh_flags & SHF_ALLOC)) 2172 continue; 2173 2174 dest = mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK); 2175 2176 if (shdr->sh_type != SHT_NOBITS) 2177 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); 2178 /* Update sh_addr to point to copy in image. */ 2179 shdr->sh_addr = (unsigned long)dest; 2180 pr_debug("\t0x%lx %s\n", 2181 (long)shdr->sh_addr, info->secstrings + shdr->sh_name); 2182 } 2183 2184 return 0; 2185 out_enomem: 2186 for (t--; t >= 0; t--) 2187 module_memory_free(mod->mem[t].base, t); 2188 return -ENOMEM; 2189 } 2190 2191 static int check_module_license_and_versions(struct module *mod) 2192 { 2193 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE); 2194 2195 /* 2196 * ndiswrapper is under GPL by itself, but loads proprietary modules. 2197 * Don't use add_taint_module(), as it would prevent ndiswrapper from 2198 * using GPL-only symbols it needs. 2199 */ 2200 if (strcmp(mod->name, "ndiswrapper") == 0) 2201 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE); 2202 2203 /* driverloader was caught wrongly pretending to be under GPL */ 2204 if (strcmp(mod->name, "driverloader") == 0) 2205 add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 2206 LOCKDEP_NOW_UNRELIABLE); 2207 2208 /* lve claims to be GPL but upstream won't provide source */ 2209 if (strcmp(mod->name, "lve") == 0) 2210 add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 2211 LOCKDEP_NOW_UNRELIABLE); 2212 2213 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE)) 2214 pr_warn("%s: module license taints kernel.\n", mod->name); 2215 2216 #ifdef CONFIG_MODVERSIONS 2217 if ((mod->num_syms && !mod->crcs) || 2218 (mod->num_gpl_syms && !mod->gpl_crcs)) { 2219 return try_to_force_load(mod, 2220 "no versions for exported symbols"); 2221 } 2222 #endif 2223 return 0; 2224 } 2225 2226 static void flush_module_icache(const struct module *mod) 2227 { 2228 /* 2229 * Flush the instruction cache, since we've played with text. 2230 * Do it before processing of module parameters, so the module 2231 * can provide parameter accessor functions of its own. 2232 */ 2233 for_each_mod_mem_type(type) { 2234 const struct module_memory *mod_mem = &mod->mem[type]; 2235 2236 if (mod_mem->size) { 2237 flush_icache_range((unsigned long)mod_mem->base, 2238 (unsigned long)mod_mem->base + mod_mem->size); 2239 } 2240 } 2241 } 2242 2243 bool __weak module_elf_check_arch(Elf_Ehdr *hdr) 2244 { 2245 return true; 2246 } 2247 2248 int __weak module_frob_arch_sections(Elf_Ehdr *hdr, 2249 Elf_Shdr *sechdrs, 2250 char *secstrings, 2251 struct module *mod) 2252 { 2253 return 0; 2254 } 2255 2256 /* module_blacklist is a comma-separated list of module names */ 2257 static char *module_blacklist; 2258 static bool blacklisted(const char *module_name) 2259 { 2260 const char *p; 2261 size_t len; 2262 2263 if (!module_blacklist) 2264 return false; 2265 2266 for (p = module_blacklist; *p; p += len) { 2267 len = strcspn(p, ","); 2268 if (strlen(module_name) == len && !memcmp(module_name, p, len)) 2269 return true; 2270 if (p[len] == ',') 2271 len++; 2272 } 2273 return false; 2274 } 2275 core_param(module_blacklist, module_blacklist, charp, 0400); 2276 2277 static struct module *layout_and_allocate(struct load_info *info, int flags) 2278 { 2279 struct module *mod; 2280 unsigned int ndx; 2281 int err; 2282 2283 err = check_modinfo(info->mod, info, flags); 2284 if (err) 2285 return ERR_PTR(err); 2286 2287 /* Allow arches to frob section contents and sizes. */ 2288 err = module_frob_arch_sections(info->hdr, info->sechdrs, 2289 info->secstrings, info->mod); 2290 if (err < 0) 2291 return ERR_PTR(err); 2292 2293 err = module_enforce_rwx_sections(info->hdr, info->sechdrs, 2294 info->secstrings, info->mod); 2295 if (err < 0) 2296 return ERR_PTR(err); 2297 2298 /* We will do a special allocation for per-cpu sections later. */ 2299 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC; 2300 2301 /* 2302 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that 2303 * layout_sections() can put it in the right place. 2304 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set. 2305 */ 2306 ndx = find_sec(info, ".data..ro_after_init"); 2307 if (ndx) 2308 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT; 2309 /* 2310 * Mark the __jump_table section as ro_after_init as well: these data 2311 * structures are never modified, with the exception of entries that 2312 * refer to code in the __init section, which are annotated as such 2313 * at module load time. 2314 */ 2315 ndx = find_sec(info, "__jump_table"); 2316 if (ndx) 2317 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT; 2318 2319 /* 2320 * Determine total sizes, and put offsets in sh_entsize. For now 2321 * this is done generically; there doesn't appear to be any 2322 * special cases for the architectures. 2323 */ 2324 layout_sections(info->mod, info); 2325 layout_symtab(info->mod, info); 2326 2327 /* Allocate and move to the final place */ 2328 err = move_module(info->mod, info); 2329 if (err) 2330 return ERR_PTR(err); 2331 2332 /* Module has been copied to its final place now: return it. */ 2333 mod = (void *)info->sechdrs[info->index.mod].sh_addr; 2334 kmemleak_load_module(mod, info); 2335 return mod; 2336 } 2337 2338 /* mod is no longer valid after this! */ 2339 static void module_deallocate(struct module *mod, struct load_info *info) 2340 { 2341 percpu_modfree(mod); 2342 module_arch_freeing_init(mod); 2343 2344 free_mod_mem(mod); 2345 } 2346 2347 int __weak module_finalize(const Elf_Ehdr *hdr, 2348 const Elf_Shdr *sechdrs, 2349 struct module *me) 2350 { 2351 return 0; 2352 } 2353 2354 static int post_relocation(struct module *mod, const struct load_info *info) 2355 { 2356 /* Sort exception table now relocations are done. */ 2357 sort_extable(mod->extable, mod->extable + mod->num_exentries); 2358 2359 /* Copy relocated percpu area over. */ 2360 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr, 2361 info->sechdrs[info->index.pcpu].sh_size); 2362 2363 /* Setup kallsyms-specific fields. */ 2364 add_kallsyms(mod, info); 2365 2366 /* Arch-specific module finalizing. */ 2367 return module_finalize(info->hdr, info->sechdrs, mod); 2368 } 2369 2370 /* Is this module of this name done loading? No locks held. */ 2371 static bool finished_loading(const char *name) 2372 { 2373 struct module *mod; 2374 bool ret; 2375 2376 /* 2377 * The module_mutex should not be a heavily contended lock; 2378 * if we get the occasional sleep here, we'll go an extra iteration 2379 * in the wait_event_interruptible(), which is harmless. 2380 */ 2381 sched_annotate_sleep(); 2382 mutex_lock(&module_mutex); 2383 mod = find_module_all(name, strlen(name), true); 2384 ret = !mod || mod->state == MODULE_STATE_LIVE 2385 || mod->state == MODULE_STATE_GOING; 2386 mutex_unlock(&module_mutex); 2387 2388 return ret; 2389 } 2390 2391 /* Call module constructors. */ 2392 static void do_mod_ctors(struct module *mod) 2393 { 2394 #ifdef CONFIG_CONSTRUCTORS 2395 unsigned long i; 2396 2397 for (i = 0; i < mod->num_ctors; i++) 2398 mod->ctors[i](); 2399 #endif 2400 } 2401 2402 /* For freeing module_init on success, in case kallsyms traversing */ 2403 struct mod_initfree { 2404 struct llist_node node; 2405 void *init_text; 2406 void *init_data; 2407 void *init_rodata; 2408 }; 2409 2410 static void do_free_init(struct work_struct *w) 2411 { 2412 struct llist_node *pos, *n, *list; 2413 struct mod_initfree *initfree; 2414 2415 list = llist_del_all(&init_free_list); 2416 2417 synchronize_rcu(); 2418 2419 llist_for_each_safe(pos, n, list) { 2420 initfree = container_of(pos, struct mod_initfree, node); 2421 module_memfree(initfree->init_text); 2422 module_memfree(initfree->init_data); 2423 module_memfree(initfree->init_rodata); 2424 kfree(initfree); 2425 } 2426 } 2427 2428 #undef MODULE_PARAM_PREFIX 2429 #define MODULE_PARAM_PREFIX "module." 2430 /* Default value for module->async_probe_requested */ 2431 static bool async_probe; 2432 module_param(async_probe, bool, 0644); 2433 2434 /* 2435 * This is where the real work happens. 2436 * 2437 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb 2438 * helper command 'lx-symbols'. 2439 */ 2440 static noinline int do_init_module(struct module *mod) 2441 { 2442 int ret = 0; 2443 struct mod_initfree *freeinit; 2444 2445 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL); 2446 if (!freeinit) { 2447 ret = -ENOMEM; 2448 goto fail; 2449 } 2450 freeinit->init_text = mod->mem[MOD_INIT_TEXT].base; 2451 freeinit->init_data = mod->mem[MOD_INIT_DATA].base; 2452 freeinit->init_rodata = mod->mem[MOD_INIT_RODATA].base; 2453 2454 do_mod_ctors(mod); 2455 /* Start the module */ 2456 if (mod->init != NULL) 2457 ret = do_one_initcall(mod->init); 2458 if (ret < 0) { 2459 goto fail_free_freeinit; 2460 } 2461 if (ret > 0) { 2462 pr_warn("%s: '%s'->init suspiciously returned %d, it should " 2463 "follow 0/-E convention\n" 2464 "%s: loading module anyway...\n", 2465 __func__, mod->name, ret, __func__); 2466 dump_stack(); 2467 } 2468 2469 /* Now it's a first class citizen! */ 2470 mod->state = MODULE_STATE_LIVE; 2471 blocking_notifier_call_chain(&module_notify_list, 2472 MODULE_STATE_LIVE, mod); 2473 2474 /* Delay uevent until module has finished its init routine */ 2475 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 2476 2477 /* 2478 * We need to finish all async code before the module init sequence 2479 * is done. This has potential to deadlock if synchronous module 2480 * loading is requested from async (which is not allowed!). 2481 * 2482 * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous 2483 * request_module() from async workers") for more details. 2484 */ 2485 if (!mod->async_probe_requested) 2486 async_synchronize_full(); 2487 2488 ftrace_free_mem(mod, mod->mem[MOD_INIT_TEXT].base, 2489 mod->mem[MOD_INIT_TEXT].base + mod->mem[MOD_INIT_TEXT].size); 2490 mutex_lock(&module_mutex); 2491 /* Drop initial reference. */ 2492 module_put(mod); 2493 trim_init_extable(mod); 2494 #ifdef CONFIG_KALLSYMS 2495 /* Switch to core kallsyms now init is done: kallsyms may be walking! */ 2496 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); 2497 #endif 2498 module_enable_ro(mod, true); 2499 mod_tree_remove_init(mod); 2500 module_arch_freeing_init(mod); 2501 for_class_mod_mem_type(type, init) { 2502 mod->mem[type].base = NULL; 2503 mod->mem[type].size = 0; 2504 } 2505 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 2506 /* .BTF is not SHF_ALLOC and will get removed, so sanitize pointer */ 2507 mod->btf_data = NULL; 2508 #endif 2509 /* 2510 * We want to free module_init, but be aware that kallsyms may be 2511 * walking this with preempt disabled. In all the failure paths, we 2512 * call synchronize_rcu(), but we don't want to slow down the success 2513 * path. module_memfree() cannot be called in an interrupt, so do the 2514 * work and call synchronize_rcu() in a work queue. 2515 * 2516 * Note that module_alloc() on most architectures creates W+X page 2517 * mappings which won't be cleaned up until do_free_init() runs. Any 2518 * code such as mark_rodata_ro() which depends on those mappings to 2519 * be cleaned up needs to sync with the queued work - ie 2520 * rcu_barrier() 2521 */ 2522 if (llist_add(&freeinit->node, &init_free_list)) 2523 schedule_work(&init_free_wq); 2524 2525 mutex_unlock(&module_mutex); 2526 wake_up_all(&module_wq); 2527 2528 return 0; 2529 2530 fail_free_freeinit: 2531 kfree(freeinit); 2532 fail: 2533 /* Try to protect us from buggy refcounters. */ 2534 mod->state = MODULE_STATE_GOING; 2535 synchronize_rcu(); 2536 module_put(mod); 2537 blocking_notifier_call_chain(&module_notify_list, 2538 MODULE_STATE_GOING, mod); 2539 klp_module_going(mod); 2540 ftrace_release_mod(mod); 2541 free_module(mod); 2542 wake_up_all(&module_wq); 2543 return ret; 2544 } 2545 2546 static int may_init_module(void) 2547 { 2548 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2549 return -EPERM; 2550 2551 return 0; 2552 } 2553 2554 /* 2555 * We try to place it in the list now to make sure it's unique before 2556 * we dedicate too many resources. In particular, temporary percpu 2557 * memory exhaustion. 2558 */ 2559 static int add_unformed_module(struct module *mod) 2560 { 2561 int err; 2562 struct module *old; 2563 2564 mod->state = MODULE_STATE_UNFORMED; 2565 2566 mutex_lock(&module_mutex); 2567 old = find_module_all(mod->name, strlen(mod->name), true); 2568 if (old != NULL) { 2569 if (old->state == MODULE_STATE_COMING 2570 || old->state == MODULE_STATE_UNFORMED) { 2571 /* Wait in case it fails to load. */ 2572 mutex_unlock(&module_mutex); 2573 err = wait_event_interruptible(module_wq, 2574 finished_loading(mod->name)); 2575 if (err) 2576 goto out_unlocked; 2577 2578 /* The module might have gone in the meantime. */ 2579 mutex_lock(&module_mutex); 2580 old = find_module_all(mod->name, strlen(mod->name), 2581 true); 2582 } 2583 2584 /* 2585 * We are here only when the same module was being loaded. Do 2586 * not try to load it again right now. It prevents long delays 2587 * caused by serialized module load failures. It might happen 2588 * when more devices of the same type trigger load of 2589 * a particular module. 2590 */ 2591 if (old && old->state == MODULE_STATE_LIVE) 2592 err = -EEXIST; 2593 else 2594 err = -EBUSY; 2595 goto out; 2596 } 2597 mod_update_bounds(mod); 2598 list_add_rcu(&mod->list, &modules); 2599 mod_tree_insert(mod); 2600 err = 0; 2601 2602 out: 2603 mutex_unlock(&module_mutex); 2604 out_unlocked: 2605 return err; 2606 } 2607 2608 static int complete_formation(struct module *mod, struct load_info *info) 2609 { 2610 int err; 2611 2612 mutex_lock(&module_mutex); 2613 2614 /* Find duplicate symbols (must be called under lock). */ 2615 err = verify_exported_symbols(mod); 2616 if (err < 0) 2617 goto out; 2618 2619 /* These rely on module_mutex for list integrity. */ 2620 module_bug_finalize(info->hdr, info->sechdrs, mod); 2621 module_cfi_finalize(info->hdr, info->sechdrs, mod); 2622 2623 module_enable_ro(mod, false); 2624 module_enable_nx(mod); 2625 module_enable_x(mod); 2626 2627 /* 2628 * Mark state as coming so strong_try_module_get() ignores us, 2629 * but kallsyms etc. can see us. 2630 */ 2631 mod->state = MODULE_STATE_COMING; 2632 mutex_unlock(&module_mutex); 2633 2634 return 0; 2635 2636 out: 2637 mutex_unlock(&module_mutex); 2638 return err; 2639 } 2640 2641 static int prepare_coming_module(struct module *mod) 2642 { 2643 int err; 2644 2645 ftrace_module_enable(mod); 2646 err = klp_module_coming(mod); 2647 if (err) 2648 return err; 2649 2650 err = blocking_notifier_call_chain_robust(&module_notify_list, 2651 MODULE_STATE_COMING, MODULE_STATE_GOING, mod); 2652 err = notifier_to_errno(err); 2653 if (err) 2654 klp_module_going(mod); 2655 2656 return err; 2657 } 2658 2659 static int unknown_module_param_cb(char *param, char *val, const char *modname, 2660 void *arg) 2661 { 2662 struct module *mod = arg; 2663 int ret; 2664 2665 if (strcmp(param, "async_probe") == 0) { 2666 if (kstrtobool(val, &mod->async_probe_requested)) 2667 mod->async_probe_requested = true; 2668 return 0; 2669 } 2670 2671 /* Check for magic 'dyndbg' arg */ 2672 ret = ddebug_dyndbg_module_param_cb(param, val, modname); 2673 if (ret != 0) 2674 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param); 2675 return 0; 2676 } 2677 2678 /* 2679 * Allocate and load the module: note that size of section 0 is always 2680 * zero, and we rely on this for optional sections. 2681 */ 2682 static int load_module(struct load_info *info, const char __user *uargs, 2683 int flags) 2684 { 2685 struct module *mod; 2686 long err = 0; 2687 char *after_dashes; 2688 2689 /* 2690 * Do the signature check (if any) first. All that 2691 * the signature check needs is info->len, it does 2692 * not need any of the section info. That can be 2693 * set up later. This will minimize the chances 2694 * of a corrupt module causing problems before 2695 * we even get to the signature check. 2696 * 2697 * The check will also adjust info->len by stripping 2698 * off the sig length at the end of the module, making 2699 * checks against info->len more correct. 2700 */ 2701 err = module_sig_check(info, flags); 2702 if (err) 2703 goto free_copy; 2704 2705 /* 2706 * Do basic sanity checks against the ELF header and 2707 * sections. 2708 */ 2709 err = elf_validity_check(info); 2710 if (err) 2711 goto free_copy; 2712 2713 /* 2714 * Everything checks out, so set up the section info 2715 * in the info structure. 2716 */ 2717 err = setup_load_info(info, flags); 2718 if (err) 2719 goto free_copy; 2720 2721 /* 2722 * Now that we know we have the correct module name, check 2723 * if it's blacklisted. 2724 */ 2725 if (blacklisted(info->name)) { 2726 err = -EPERM; 2727 pr_err("Module %s is blacklisted\n", info->name); 2728 goto free_copy; 2729 } 2730 2731 err = rewrite_section_headers(info, flags); 2732 if (err) 2733 goto free_copy; 2734 2735 /* Check module struct version now, before we try to use module. */ 2736 if (!check_modstruct_version(info, info->mod)) { 2737 err = -ENOEXEC; 2738 goto free_copy; 2739 } 2740 2741 /* Figure out module layout, and allocate all the memory. */ 2742 mod = layout_and_allocate(info, flags); 2743 if (IS_ERR(mod)) { 2744 err = PTR_ERR(mod); 2745 goto free_copy; 2746 } 2747 2748 audit_log_kern_module(mod->name); 2749 2750 /* Reserve our place in the list. */ 2751 err = add_unformed_module(mod); 2752 if (err) 2753 goto free_module; 2754 2755 #ifdef CONFIG_MODULE_SIG 2756 mod->sig_ok = info->sig_ok; 2757 if (!mod->sig_ok) { 2758 pr_notice_once("%s: module verification failed: signature " 2759 "and/or required key missing - tainting " 2760 "kernel\n", mod->name); 2761 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK); 2762 } 2763 #endif 2764 2765 /* To avoid stressing percpu allocator, do this once we're unique. */ 2766 err = percpu_modalloc(mod, info); 2767 if (err) 2768 goto unlink_mod; 2769 2770 /* Now module is in final location, initialize linked lists, etc. */ 2771 err = module_unload_init(mod); 2772 if (err) 2773 goto unlink_mod; 2774 2775 init_param_lock(mod); 2776 2777 /* 2778 * Now we've got everything in the final locations, we can 2779 * find optional sections. 2780 */ 2781 err = find_module_sections(mod, info); 2782 if (err) 2783 goto free_unload; 2784 2785 err = check_module_license_and_versions(mod); 2786 if (err) 2787 goto free_unload; 2788 2789 /* Set up MODINFO_ATTR fields */ 2790 setup_modinfo(mod, info); 2791 2792 /* Fix up syms, so that st_value is a pointer to location. */ 2793 err = simplify_symbols(mod, info); 2794 if (err < 0) 2795 goto free_modinfo; 2796 2797 err = apply_relocations(mod, info); 2798 if (err < 0) 2799 goto free_modinfo; 2800 2801 err = post_relocation(mod, info); 2802 if (err < 0) 2803 goto free_modinfo; 2804 2805 flush_module_icache(mod); 2806 2807 /* Now copy in args */ 2808 mod->args = strndup_user(uargs, ~0UL >> 1); 2809 if (IS_ERR(mod->args)) { 2810 err = PTR_ERR(mod->args); 2811 goto free_arch_cleanup; 2812 } 2813 2814 init_build_id(mod, info); 2815 2816 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */ 2817 ftrace_module_init(mod); 2818 2819 /* Finally it's fully formed, ready to start executing. */ 2820 err = complete_formation(mod, info); 2821 if (err) 2822 goto ddebug_cleanup; 2823 2824 err = prepare_coming_module(mod); 2825 if (err) 2826 goto bug_cleanup; 2827 2828 mod->async_probe_requested = async_probe; 2829 2830 /* Module is ready to execute: parsing args may do that. */ 2831 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, 2832 -32768, 32767, mod, 2833 unknown_module_param_cb); 2834 if (IS_ERR(after_dashes)) { 2835 err = PTR_ERR(after_dashes); 2836 goto coming_cleanup; 2837 } else if (after_dashes) { 2838 pr_warn("%s: parameters '%s' after `--' ignored\n", 2839 mod->name, after_dashes); 2840 } 2841 2842 /* Link in to sysfs. */ 2843 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp); 2844 if (err < 0) 2845 goto coming_cleanup; 2846 2847 if (is_livepatch_module(mod)) { 2848 err = copy_module_elf(mod, info); 2849 if (err < 0) 2850 goto sysfs_cleanup; 2851 } 2852 2853 /* Get rid of temporary copy. */ 2854 free_copy(info, flags); 2855 2856 /* Done! */ 2857 trace_module_load(mod); 2858 2859 return do_init_module(mod); 2860 2861 sysfs_cleanup: 2862 mod_sysfs_teardown(mod); 2863 coming_cleanup: 2864 mod->state = MODULE_STATE_GOING; 2865 destroy_params(mod->kp, mod->num_kp); 2866 blocking_notifier_call_chain(&module_notify_list, 2867 MODULE_STATE_GOING, mod); 2868 klp_module_going(mod); 2869 bug_cleanup: 2870 mod->state = MODULE_STATE_GOING; 2871 /* module_bug_cleanup needs module_mutex protection */ 2872 mutex_lock(&module_mutex); 2873 module_bug_cleanup(mod); 2874 mutex_unlock(&module_mutex); 2875 2876 ddebug_cleanup: 2877 ftrace_release_mod(mod); 2878 synchronize_rcu(); 2879 kfree(mod->args); 2880 free_arch_cleanup: 2881 module_arch_cleanup(mod); 2882 free_modinfo: 2883 free_modinfo(mod); 2884 free_unload: 2885 module_unload_free(mod); 2886 unlink_mod: 2887 mutex_lock(&module_mutex); 2888 /* Unlink carefully: kallsyms could be walking list. */ 2889 list_del_rcu(&mod->list); 2890 mod_tree_remove(mod); 2891 wake_up_all(&module_wq); 2892 /* Wait for RCU-sched synchronizing before releasing mod->list. */ 2893 synchronize_rcu(); 2894 mutex_unlock(&module_mutex); 2895 free_module: 2896 /* Free lock-classes; relies on the preceding sync_rcu() */ 2897 for_class_mod_mem_type(type, core_data) { 2898 lockdep_free_key_range(mod->mem[type].base, 2899 mod->mem[type].size); 2900 } 2901 2902 module_deallocate(mod, info); 2903 free_copy: 2904 free_copy(info, flags); 2905 return err; 2906 } 2907 2908 SYSCALL_DEFINE3(init_module, void __user *, umod, 2909 unsigned long, len, const char __user *, uargs) 2910 { 2911 int err; 2912 struct load_info info = { }; 2913 2914 err = may_init_module(); 2915 if (err) 2916 return err; 2917 2918 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n", 2919 umod, len, uargs); 2920 2921 err = copy_module_from_user(umod, len, &info); 2922 if (err) 2923 return err; 2924 2925 return load_module(&info, uargs, 0); 2926 } 2927 2928 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) 2929 { 2930 struct load_info info = { }; 2931 void *buf = NULL; 2932 int len; 2933 int err; 2934 2935 err = may_init_module(); 2936 if (err) 2937 return err; 2938 2939 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags); 2940 2941 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS 2942 |MODULE_INIT_IGNORE_VERMAGIC 2943 |MODULE_INIT_COMPRESSED_FILE)) 2944 return -EINVAL; 2945 2946 len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL, 2947 READING_MODULE); 2948 if (len < 0) 2949 return len; 2950 2951 if (flags & MODULE_INIT_COMPRESSED_FILE) { 2952 err = module_decompress(&info, buf, len); 2953 vfree(buf); /* compressed data is no longer needed */ 2954 if (err) 2955 return err; 2956 } else { 2957 info.hdr = buf; 2958 info.len = len; 2959 } 2960 2961 return load_module(&info, uargs, flags); 2962 } 2963 2964 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ 2965 char *module_flags(struct module *mod, char *buf, bool show_state) 2966 { 2967 int bx = 0; 2968 2969 BUG_ON(mod->state == MODULE_STATE_UNFORMED); 2970 if (!mod->taints && !show_state) 2971 goto out; 2972 if (mod->taints || 2973 mod->state == MODULE_STATE_GOING || 2974 mod->state == MODULE_STATE_COMING) { 2975 buf[bx++] = '('; 2976 bx += module_flags_taint(mod->taints, buf + bx); 2977 /* Show a - for module-is-being-unloaded */ 2978 if (mod->state == MODULE_STATE_GOING && show_state) 2979 buf[bx++] = '-'; 2980 /* Show a + for module-is-being-loaded */ 2981 if (mod->state == MODULE_STATE_COMING && show_state) 2982 buf[bx++] = '+'; 2983 buf[bx++] = ')'; 2984 } 2985 out: 2986 buf[bx] = '\0'; 2987 2988 return buf; 2989 } 2990 2991 /* Given an address, look for it in the module exception tables. */ 2992 const struct exception_table_entry *search_module_extables(unsigned long addr) 2993 { 2994 const struct exception_table_entry *e = NULL; 2995 struct module *mod; 2996 2997 preempt_disable(); 2998 mod = __module_address(addr); 2999 if (!mod) 3000 goto out; 3001 3002 if (!mod->num_exentries) 3003 goto out; 3004 3005 e = search_extable(mod->extable, 3006 mod->num_exentries, 3007 addr); 3008 out: 3009 preempt_enable(); 3010 3011 /* 3012 * Now, if we found one, we are running inside it now, hence 3013 * we cannot unload the module, hence no refcnt needed. 3014 */ 3015 return e; 3016 } 3017 3018 /** 3019 * is_module_address() - is this address inside a module? 3020 * @addr: the address to check. 3021 * 3022 * See is_module_text_address() if you simply want to see if the address 3023 * is code (not data). 3024 */ 3025 bool is_module_address(unsigned long addr) 3026 { 3027 bool ret; 3028 3029 preempt_disable(); 3030 ret = __module_address(addr) != NULL; 3031 preempt_enable(); 3032 3033 return ret; 3034 } 3035 3036 /** 3037 * __module_address() - get the module which contains an address. 3038 * @addr: the address. 3039 * 3040 * Must be called with preempt disabled or module mutex held so that 3041 * module doesn't get freed during this. 3042 */ 3043 struct module *__module_address(unsigned long addr) 3044 { 3045 struct module *mod; 3046 3047 if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) 3048 goto lookup; 3049 3050 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 3051 if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max) 3052 goto lookup; 3053 #endif 3054 3055 return NULL; 3056 3057 lookup: 3058 module_assert_mutex_or_preempt(); 3059 3060 mod = mod_find(addr, &mod_tree); 3061 if (mod) { 3062 BUG_ON(!within_module(addr, mod)); 3063 if (mod->state == MODULE_STATE_UNFORMED) 3064 mod = NULL; 3065 } 3066 return mod; 3067 } 3068 3069 /** 3070 * is_module_text_address() - is this address inside module code? 3071 * @addr: the address to check. 3072 * 3073 * See is_module_address() if you simply want to see if the address is 3074 * anywhere in a module. See kernel_text_address() for testing if an 3075 * address corresponds to kernel or module code. 3076 */ 3077 bool is_module_text_address(unsigned long addr) 3078 { 3079 bool ret; 3080 3081 preempt_disable(); 3082 ret = __module_text_address(addr) != NULL; 3083 preempt_enable(); 3084 3085 return ret; 3086 } 3087 3088 /** 3089 * __module_text_address() - get the module whose code contains an address. 3090 * @addr: the address. 3091 * 3092 * Must be called with preempt disabled or module mutex held so that 3093 * module doesn't get freed during this. 3094 */ 3095 struct module *__module_text_address(unsigned long addr) 3096 { 3097 struct module *mod = __module_address(addr); 3098 if (mod) { 3099 /* Make sure it's within the text section. */ 3100 if (!within_module_mem_type(addr, mod, MOD_TEXT) && 3101 !within_module_mem_type(addr, mod, MOD_INIT_TEXT)) 3102 mod = NULL; 3103 } 3104 return mod; 3105 } 3106 3107 /* Don't grab lock, we're oopsing. */ 3108 void print_modules(void) 3109 { 3110 struct module *mod; 3111 char buf[MODULE_FLAGS_BUF_SIZE]; 3112 3113 printk(KERN_DEFAULT "Modules linked in:"); 3114 /* Most callers should already have preempt disabled, but make sure */ 3115 preempt_disable(); 3116 list_for_each_entry_rcu(mod, &modules, list) { 3117 if (mod->state == MODULE_STATE_UNFORMED) 3118 continue; 3119 pr_cont(" %s%s", mod->name, module_flags(mod, buf, true)); 3120 } 3121 3122 print_unloaded_tainted_modules(); 3123 preempt_enable(); 3124 if (last_unloaded_module.name[0]) 3125 pr_cont(" [last unloaded: %s%s]", last_unloaded_module.name, 3126 last_unloaded_module.taints); 3127 pr_cont("\n"); 3128 } 3129