Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | #include <linux/errno.h> #include <linux/kernel.h> #include <asm/segment.h> #include <linux/mm.h> /* defines GFP_KERNEL */ #include <linux/string.h> #include <linux/module.h> #include <linux/sched.h> #include <linux/malloc.h> /* * Originally by Anonymous (as far as I know...) * Linux version by Bas Laarhoven <bas@vimec.nl> * 0.99.14 version by Jon Tombs <jon@gtex02.us.es>, * * Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C) * This source is covered by the GNU GPL, the same as all kernel sources. * * Features: * - Supports stacked modules (removable only of there are no dependents). * - Supports table of symbols defined by the modules. * - Supports /proc/ksyms, showing value, name and owner of all * the symbols defined by all modules (in stack order). * - Added module dependencies information into /proc/modules * - Supports redefines of all symbols, for streams-like behaviour. * - Compatible with older versions of insmod. * * New addition in December 1994: (Bjorn Ekwall, idea from Jacques Gelinas) * - Externally callable function: * * "int register_symtab(struct symbol_table *)" * * This function can be called from within the kernel, * and ALSO from loadable modules. * The goal is to assist in modularizing the kernel even more, * and finally: reducing the number of entries in ksyms.c * since every subsystem should now be able to decide and * control exactly what symbols it wants to export, locally! * * On 1-Aug-95: <Matti.Aarnio@utu.fi> altered code to use same style as * do /proc/net/XXX "files". Namely allow more than 4kB * (or what the block size if) output. */ #ifdef DEBUG_MODULE #define PRINTK(a) printk a #else #define PRINTK(a) /* */ #endif static struct module kernel_module; static struct module *module_list = &kernel_module; static int freeing_modules; /* true if some modules are marked for deletion */ static struct module *find_module( const char *name); static int get_mod_name( char *user_name, char *buf); static int free_modules( void); static int module_init_flag = 0; /* Hmm... */ extern struct symbol_table symbol_table; /* in kernel/ksyms.c */ /* * Called at boot time */ void init_modules(void) { struct internal_symbol *sym; int i; for (i = 0, sym = symbol_table.symbol; sym->name; ++sym, ++i) ; symbol_table.n_symbols = i; kernel_module.symtab = &symbol_table; kernel_module.state = MOD_RUNNING; /* Hah! */ kernel_module.name = ""; } int rename_module_symbol(char *old_name, char *new_name) { struct internal_symbol *sym; int i = 0; /* keep gcc silent */ if (module_list->symtab) { sym = module_list->symtab->symbol; for (i = module_list->symtab->n_symbols; i > 0; ++sym, --i) { if (strcmp(sym->name, old_name) == 0) { /* found it! */ sym->name = new_name; /* done! */ PRINTK(("renamed %s to %s\n", old_name, new_name)); return 1; /* it worked! */ } } } printk("rename %s to %s failed!\n", old_name, new_name); return 0; /* not there... */ /* * This one will change the name of the first matching symbol! * * With this function, you can replace the name of a symbol defined * in the current module with a new name, e.g. when you want to insert * your own function instead of a previously defined function * with the same name. * * "Normal" usage: * * bogus_function(int params) * { * do something "smart"; * return real_function(params); * } * * ... * * init_module() * { * if (rename_module_symbol("_bogus_function", "_real_function")) * printk("yep!\n"); * else * printk("no way!\n"); * ... * } * * When loading this module, real_function will be resolved * to the real function address. * All later loaded modules that refer to "real_function()" will * then really call "bogus_function()" instead!!! * * This feature will give you ample opportunities to get to know * the taste of your foot when you stuff it into your mouth!!! */ } /* * Allocate space for a module. */ asmlinkage unsigned long sys_create_module(char *module_name, unsigned long size) { struct module *mp; void* addr; int error; int npages; int sspace = sizeof(struct module) + MOD_MAX_NAME; char name[MOD_MAX_NAME]; if (!suser()) return -EPERM; if (module_name == NULL || size == 0) return -EINVAL; if ((error = get_mod_name(module_name, name)) != 0) return error; if (find_module(name) != NULL) { return -EEXIST; } if ((mp = (struct module*) kmalloc(sspace, GFP_KERNEL)) == NULL) { return -ENOMEM; } strcpy((char *)(mp + 1), name); /* why not? */ npages = (size + sizeof (long) + PAGE_SIZE - 1) / PAGE_SIZE; if ((addr = vmalloc(npages * PAGE_SIZE)) == 0) { kfree_s(mp, sspace); return -ENOMEM; } mp->next = module_list; mp->ref = NULL; mp->symtab = NULL; mp->name = (char *)(mp + 1); mp->size = npages; mp->addr = addr; mp->state = MOD_UNINITIALIZED; mp->cleanup = NULL; * (long *) addr = 0; /* set use count to zero */ module_list = mp; /* link it in */ PRINTK(("module `%s' (%lu pages @ 0x%08lx) created\n", mp->name, (unsigned long) mp->size, (unsigned long) mp->addr)); return (unsigned long) addr; } /* * Initialize a module. */ asmlinkage int sys_init_module(char *module_name, char *code, unsigned codesize, struct mod_routines *routines, struct symbol_table *symtab) { struct module *mp; struct symbol_table *newtab; char name[MOD_MAX_NAME]; int error; struct mod_routines rt; if (!suser()) return -EPERM; #ifdef __i386__ /* A little bit of protection... we "know" where the user stack is... */ if (symtab && ((unsigned long)symtab > 0xb0000000)) { printk("warning: you are using an old insmod, no symbols will be inserted!\n"); symtab = NULL; } #endif /* * First reclaim any memory from dead modules that where not * freed when deleted. Should I think be done by timers when * the module was deleted - Jon. */ free_modules(); if ((error = get_mod_name(module_name, name)) != 0) return error; PRINTK(("initializing module `%s', %d (0x%x) bytes\n", name, codesize, codesize)); memcpy_fromfs(&rt, routines, sizeof rt); if ((mp = find_module(name)) == NULL) return -ENOENT; if ((codesize + sizeof (long) + PAGE_SIZE - 1) / PAGE_SIZE > mp->size) return -EINVAL; memcpy_fromfs((char *)mp->addr + sizeof (long), code, codesize); memset((char *)mp->addr + sizeof (long) + codesize, 0, mp->size * PAGE_SIZE - (codesize + sizeof (long))); PRINTK(( "module init entry = 0x%08lx, cleanup entry = 0x%08lx\n", (unsigned long) rt.init, (unsigned long) rt.cleanup)); mp->cleanup = rt.cleanup; /* update kernel symbol table */ if (symtab) { /* symtab == NULL means no new entries to handle */ struct internal_symbol *sym; struct module_ref *ref; int size; int i; int legal_start; if ((error = verify_area(VERIFY_READ, &symtab->size, sizeof(symtab->size)))) return error; size = get_user(&symtab->size); if ((newtab = (struct symbol_table*) kmalloc(size, GFP_KERNEL)) == NULL) { return -ENOMEM; } if ((error = verify_area(VERIFY_READ, symtab, size))) { kfree_s(newtab, size); return error; } memcpy_fromfs((char *)(newtab), symtab, size); /* sanity check */ legal_start = sizeof(struct symbol_table) + newtab->n_symbols * sizeof(struct internal_symbol) + newtab->n_refs * sizeof(struct module_ref); if ((newtab->n_symbols < 0) || (newtab->n_refs < 0) || (legal_start > size)) { printk("Illegal symbol table! Rejected!\n"); kfree_s(newtab, size); return -EINVAL; } /* relocate name pointers, index referred from start of table */ for (sym = &(newtab->symbol[0]), i = 0; i < newtab->n_symbols; ++sym, ++i) { if ((unsigned long)sym->name < legal_start || size <= (unsigned long)sym->name) { printk("Illegal symbol table! Rejected!\n"); kfree_s(newtab, size); return -EINVAL; } /* else */ sym->name += (long)newtab; } mp->symtab = newtab; /* Update module references. * On entry, from "insmod", ref->module points to * the referenced module! * Now it will point to the current module instead! * The ref structure becomes the first link in the linked * list of references to the referenced module. * Also, "sym" from above, points to the first ref entry!!! */ for (ref = (struct module_ref *)sym, i = 0; i < newtab->n_refs; ++ref, ++i) { /* Check for valid reference */ struct module *link = module_list; while (link && (ref->module != link)) link = link->next; if (link == (struct module *)0) { printk("Non-module reference! Rejected!\n"); return -EINVAL; } ref->next = ref->module->ref; ref->module->ref = ref; ref->module = mp; } } module_init_flag = 1; /* Hmm... */ if ((*rt.init)() != 0) { module_init_flag = 0; /* Hmm... */ return -EBUSY; } module_init_flag = 0; /* Hmm... */ mp->state = MOD_RUNNING; return 0; } asmlinkage int sys_delete_module(char *module_name) { struct module *mp; char name[MOD_MAX_NAME]; int error; if (!suser()) return -EPERM; /* else */ if (module_name != NULL) { if ((error = get_mod_name(module_name, name)) != 0) return error; if ((mp = find_module(name)) == NULL) return -ENOENT; if ((mp->ref != NULL) || (GET_USE_COUNT(mp) != 0)) return -EBUSY; if (mp->state == MOD_RUNNING) (*mp->cleanup)(); mp->state = MOD_DELETED; } free_modules(); return 0; } /* * Copy the kernel symbol table to user space. If the argument is null, * just return the size of the table. * * Note that the transient module symbols are copied _first_, * in lifo order!!! * * The symbols to "insmod" are according to the "old" format: struct kernel_sym, * which is actually quite handy for this purpose. * Note that insmod inserts a struct symbol_table later on... * (as that format is quite handy for the kernel...) * * For every module, the first (pseudo)symbol copied is the module name * and the address of the module struct. * This lets "insmod" keep track of references, and build the array of * struct module_refs in the symbol table. * The format of the module name is "#module", so that "insmod" can easily * notice when a module name comes along. Also, this will make it possible * to use old versions of "insmod", albeit with reduced functionality... * The "kernel" module has an empty name. */ asmlinkage int sys_get_kernel_syms(struct kernel_sym *table) { struct internal_symbol *from; struct kernel_sym isym; struct kernel_sym *to; struct module *mp = module_list; int i; int nmodsyms = 0; for (mp = module_list; mp; mp = mp->next) { if (mp->symtab && mp->symtab->n_symbols) { /* include the count for the module name! */ nmodsyms += mp->symtab->n_symbols + 1; } else /* include the count for the module name! */ nmodsyms += 1; /* return modules without symbols too */ } if (table != NULL) { to = table; if ((i = verify_area(VERIFY_WRITE, to, nmodsyms * sizeof(*table)))) return i; /* copy all module symbols first (always LIFO order) */ for (mp = module_list; mp; mp = mp->next) { if (mp->state == MOD_RUNNING) { /* magic: write module info as a pseudo symbol */ isym.value = (unsigned long)mp; sprintf(isym.name, "#%s", mp->name); memcpy_tofs(to, &isym, sizeof isym); ++to; if (mp->symtab != NULL) { for (i = mp->symtab->n_symbols, from = mp->symtab->symbol; i > 0; --i, ++from, ++to) { isym.value = (unsigned long)from->addr; strncpy(isym.name, from->name, sizeof isym.name); memcpy_tofs(to, &isym, sizeof isym); } } } } } return nmodsyms; } /* * Copy the name of a module from user space. */ int get_mod_name(char *user_name, char *buf) { int i; i = 0; for (i = 0 ; (buf[i] = get_user(user_name + i)) != '\0' ; ) { if (++i >= MOD_MAX_NAME) return -E2BIG; } return 0; } /* * Look for a module by name, ignoring modules marked for deletion. */ struct module * find_module( const char *name) { struct module *mp; for (mp = module_list ; mp ; mp = mp->next) { if (mp->state == MOD_DELETED) continue; if (!strcmp(mp->name, name)) break; } return mp; } static void drop_refs(struct module *mp) { struct module *step; struct module_ref *prev; struct module_ref *ref; for (step = module_list; step; step = step->next) { for (prev = ref = step->ref; ref; ref = prev->next) { if (ref->module == mp) { if (ref == step->ref) step->ref = ref->next; else prev->next = ref->next; break; /* every module only references once! */ } else prev = ref; } } } /* * Try to free modules which have been marked for deletion. Returns nonzero * if a module was actually freed. */ int free_modules( void) { struct module *mp; struct module **mpp; int did_deletion; did_deletion = 0; freeing_modules = 0; mpp = &module_list; while ((mp = *mpp) != NULL) { if (mp->state != MOD_DELETED) { mpp = &mp->next; } else { if (GET_USE_COUNT(mp) != 0) { freeing_modules = 1; mpp = &mp->next; } else { /* delete it */ *mpp = mp->next; if (mp->symtab) { if (mp->symtab->n_refs) drop_refs(mp); if (mp->symtab->size) kfree_s(mp->symtab, mp->symtab->size); } vfree(mp->addr); kfree_s(mp, sizeof(struct module) + MOD_MAX_NAME); did_deletion = 1; } } } return did_deletion; } /* * Called by the /proc file system to return a current list of modules. */ int get_module_list(char *buf) { char *p; const char *q; int i; struct module *mp; struct module_ref *ref; char size[32]; p = buf; /* Do not show the kernel pseudo module */ for (mp = module_list ; mp && mp->next; mp = mp->next) { if (p - buf > 4096 - 100) break; /* avoid overflowing buffer */ q = mp->name; i = 20; while (*q) { *p++ = *q++; i--; } sprintf(size, "%d", mp->size); i -= strlen(size); if (i <= 0) i = 1; while (--i >= 0) *p++ = ' '; q = size; while (*q) *p++ = *q++; if (mp->state == MOD_UNINITIALIZED) q = " (uninitialized)"; else if (mp->state == MOD_RUNNING) q = ""; else if (mp->state == MOD_DELETED) q = " (deleted)"; else q = " (bad state)"; while (*q) *p++ = *q++; if ((ref = mp->ref) != NULL) { *p++ = '\t'; *p++ = '['; for (; ref; ref = ref->next) { q = ref->module->name; while (*q) *p++ = *q++; if (ref->next) *p++ = ' '; } *p++ = ']'; } *p++ = '\n'; } return p - buf; } /* * Called by the /proc file system to return a current list of ksyms. */ int get_ksyms_list(char *buf, char **start, off_t offset, int length) { struct module *mp; struct internal_symbol *sym; int i; char *p = buf; int len = 0; /* code from net/ipv4/proc.c */ off_t pos = 0; off_t begin = 0; for (mp = module_list; mp; mp = mp->next) { if ((mp->state == MOD_RUNNING) && (mp->symtab != NULL) && (mp->symtab->n_symbols > 0)) { for (i = mp->symtab->n_symbols, sym = mp->symtab->symbol; i > 0; --i, ++sym) { p = buf + len; if (mp->name[0]) { len += sprintf(p, "%08lx %s\t[%s]\n", (long)sym->addr, sym->name, mp->name); } else { len += sprintf(p, "%08lx %s\n", (long)sym->addr, sym->name); } pos = begin + len; if (pos < offset) { len = 0; begin = pos; } pos = begin + len; if (pos > offset+length) goto leave_the_loop; } } } leave_the_loop: *start = buf + (offset - begin); len -= (offset - begin); if (len > length) len = length; return len; } /* * Rules: * - The new symbol table should be statically allocated, or else you _have_ * to set the "size" field of the struct to the number of bytes allocated. * * - The strings that name the symbols will not be copied, maybe the pointers * * - For a loadable module, the function should only be called in the * context of init_module * * Those are the only restrictions! (apart from not being reentrant...) * * If you want to remove a symbol table for a loadable module, * the call looks like: "register_symtab(0)". * * The look of the code is mostly dictated by the format of * the frozen struct symbol_table, due to compatibility demands. */ #define INTSIZ sizeof(struct internal_symbol) #define REFSIZ sizeof(struct module_ref) #define SYMSIZ sizeof(struct symbol_table) #define MODSIZ sizeof(struct module) static struct symbol_table nulltab; int register_symtab(struct symbol_table *intab) { struct module *mp; struct module *link; struct symbol_table *oldtab; struct symbol_table *newtab; struct module_ref *newref; int size; if (intab && (intab->n_symbols == 0)) { struct internal_symbol *sym; /* How many symbols, really? */ for (sym = intab->symbol; sym->name; ++sym) intab->n_symbols +=1; } #if 1 if (module_init_flag == 0) { /* Hmm... */ #else if (module_list == &kernel_module) { #endif /* Aha! Called from an "internal" module */ if (!intab) return 0; /* or -ESILLY_PROGRAMMER :-) */ /* create a pseudo module! */ if (!(mp = (struct module*) kmalloc(MODSIZ, GFP_KERNEL))) { /* panic time! */ printk("Out of memory for new symbol table!\n"); return -ENOMEM; } /* else OK */ memset(mp, 0, MODSIZ); mp->state = MOD_RUNNING; /* Since it is resident... */ mp->name = ""; /* This is still the "kernel" symbol table! */ mp->symtab = intab; /* link it in _after_ the resident symbol table */ mp->next = kernel_module.next; kernel_module.next = mp; return 0; } /* else ******** Called from a loadable module **********/ /* * This call should _only_ be done in the context of the * call to init_module i.e. when loading the module!! * Or else... */ mp = module_list; /* true when doing init_module! */ /* Any table there before? */ if ((oldtab = mp->symtab) == (struct symbol_table*)0) { /* No, just insert it! */ mp->symtab = intab; return 0; } /* else ****** we have to replace the module symbol table ******/ #if 0 if (oldtab->n_symbols > 0) { /* Oh dear, I have to drop the old ones... */ printk("Warning, dropping old symbols\n"); } #endif if (oldtab->n_refs == 0) { /* no problems! */ mp->symtab = intab; /* if the old table was kmalloc-ed, drop it */ if (oldtab->size > 0) kfree_s(oldtab, oldtab->size); return 0; } /* else */ /***** The module references other modules... insmod said so! *****/ /* We have to allocate a new symbol table, or we lose them! */ if (intab == (struct symbol_table*)0) intab = &nulltab; /* easier code with zeroes in place */ /* the input symbol table space does not include the string table */ /* (it does for symbol tables that insmod creates) */ if (!(newtab = (struct symbol_table*)kmalloc( size = SYMSIZ + intab->n_symbols * INTSIZ + oldtab->n_refs * REFSIZ, GFP_KERNEL))) { /* panic time! */ printk("Out of memory for new symbol table!\n"); return -ENOMEM; } /* copy up to, and including, the new symbols */ memcpy(newtab, intab, SYMSIZ + intab->n_symbols * INTSIZ); newtab->size = size; newtab->n_refs = oldtab->n_refs; /* copy references */ memcpy( ((char *)newtab) + SYMSIZ + intab->n_symbols * INTSIZ, ((char *)oldtab) + SYMSIZ + oldtab->n_symbols * INTSIZ, oldtab->n_refs * REFSIZ); /* relink references from the old table to the new one */ /* pointer to the first reference entry in newtab! Really! */ newref = (struct module_ref*) &(newtab->symbol[newtab->n_symbols]); /* check for reference links from previous modules */ for ( link = module_list; link && (link != &kernel_module); link = link->next) { if (link->ref && (link->ref->module == mp)) link->ref = newref++; } mp->symtab = newtab; /* all references (if any) have been handled */ /* if the old table was kmalloc-ed, drop it */ if (oldtab->size > 0) kfree_s(oldtab, oldtab->size); return 0; } |