1 #include <linux/config.h>
2 #include <linux/mm.h>
3 #include <linux/module.h>
4 #include <asm/module.h>
5 #include <asm/uaccess.h>
6 #include <linux/vmalloc.h>
7 #include <linux/smp_lock.h>
8 #include <asm/pgalloc.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/kmod.h>
12
13 /*
14 * Originally by Anonymous (as far as I know...)
15 * Linux version by Bas Laarhoven <bas@vimec.nl>
16 * 0.99.14 version by Jon Tombs <jon@gtex02.us.es>,
17 * Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C)
18 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
19 * Add MOD_INITIALIZING Keith Owens <kaos@ocs.com.au> Nov 1999
20 * Add kallsyms support, Keith Owens <kaos@ocs.com.au> Apr 2000
21 * Add asm/module support, IA64 has special requirements. Keith Owens <kaos@ocs.com.au> Sep 2000
22 * Fix assorted bugs in module verification. Keith Owens <kaos@ocs.com.au> Sep 2000
23 * Fix sys_init_module race, Andrew Morton <andrewm@uow.edu.au> Oct 2000
24 * http://www.uwsg.iu.edu/hypermail/linux/kernel/0008.3/0379.html
25 * Replace xxx_module_symbol with inter_module_xxx. Keith Owens <kaos@ocs.com.au> Oct 2000
26 *
27 * This source is covered by the GNU GPL, the same as all kernel sources.
28 */
29
30 #if defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS)
31
32 extern struct module_symbol __start___ksymtab[];
33 extern struct module_symbol __stop___ksymtab[];
34
35 extern const struct exception_table_entry __start___ex_table[];
36 extern const struct exception_table_entry __stop___ex_table[];
37
38 extern const char __start___kallsyms[] __attribute__ ((weak));
39 extern const char __stop___kallsyms[] __attribute__ ((weak));
40
41 static struct module kernel_module =
42 {
43 size_of_struct: sizeof(struct module),
44 name: "",
45 uc: {ATOMIC_INIT(1)},
46 flags: MOD_RUNNING,
47 syms: __start___ksymtab,
48 ex_table_start: __start___ex_table,
49 ex_table_end: __stop___ex_table,
50 kallsyms_start: __start___kallsyms,
51 kallsyms_end: __stop___kallsyms,
52 };
53
54 struct module *module_list = &kernel_module;
55
56 #endif /* defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS) */
57
58 /* inter_module functions are always available, even when the kernel is
59 * compiled without modules. Consumers of inter_module_xxx routines
60 * will always work, even when both are built into the kernel, this
61 * approach removes lots of #ifdefs in mainline code.
62 */
63
64 static struct list_head ime_list = LIST_HEAD_INIT(ime_list);
65 static spinlock_t ime_lock = SPIN_LOCK_UNLOCKED;
66 static int kmalloc_failed;
67
68 /**
69 * inter_module_register - register a new set of inter module data.
70 * @im_name: an arbitrary string to identify the data, must be unique
71 * @owner: module that is registering the data, always use THIS_MODULE
72 * @userdata: pointer to arbitrary userdata to be registered
73 *
74 * Description: Check that the im_name has not already been registered,
75 * complain if it has. For new data, add it to the inter_module_entry
76 * list.
77 */
78 void inter_module_register(const char *im_name, struct module *owner, const void *userdata)
79 {
80 struct list_head *tmp;
81 struct inter_module_entry *ime, *ime_new;
82
83 if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) {
84 /* Overloaded kernel, not fatal */
85 printk(KERN_ERR
86 "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
87 im_name);
88 kmalloc_failed = 1;
89 return;
90 }
91 memset(ime_new, 0, sizeof(*ime_new));
92 ime_new->im_name = im_name;
93 ime_new->owner = owner;
94 ime_new->userdata = userdata;
95
96 spin_lock(&ime_lock);
97 list_for_each(tmp, &ime_list) {
98 ime = list_entry(tmp, struct inter_module_entry, list);
99 if (strcmp(ime->im_name, im_name) == 0) {
100 spin_unlock(&ime_lock);
101 kfree(ime_new);
102 /* Program logic error, fatal */
103 printk(KERN_ERR "inter_module_register: duplicate im_name '%s'", im_name);
104 BUG();
105 }
106 }
107 list_add(&(ime_new->list), &ime_list);
108 spin_unlock(&ime_lock);
109 }
110
111 /**
112 * inter_module_unregister - unregister a set of inter module data.
113 * @im_name: an arbitrary string to identify the data, must be unique
114 *
115 * Description: Check that the im_name has been registered, complain if
116 * it has not. For existing data, remove it from the
117 * inter_module_entry list.
118 */
119 void inter_module_unregister(const char *im_name)
120 {
121 struct list_head *tmp;
122 struct inter_module_entry *ime;
123
124 spin_lock(&ime_lock);
125 list_for_each(tmp, &ime_list) {
126 ime = list_entry(tmp, struct inter_module_entry, list);
127 if (strcmp(ime->im_name, im_name) == 0) {
128 list_del(&(ime->list));
129 spin_unlock(&ime_lock);
130 kfree(ime);
131 return;
132 }
133 }
134 spin_unlock(&ime_lock);
135 if (kmalloc_failed) {
136 printk(KERN_ERR
137 "inter_module_unregister: no entry for '%s', "
138 "probably caused by previous kmalloc failure\n",
139 im_name);
140 return;
141 }
142 else {
143 /* Program logic error, fatal */
144 printk(KERN_ERR "inter_module_unregister: no entry for '%s'", im_name);
145 BUG();
146 }
147 }
148
149 /**
150 * inter_module_get - return arbitrary userdata from another module.
151 * @im_name: an arbitrary string to identify the data, must be unique
152 *
153 * Description: If the im_name has not been registered, return NULL.
154 * Try to increment the use count on the owning module, if that fails
155 * then return NULL. Otherwise return the userdata.
156 */
157 const void *inter_module_get(const char *im_name)
158 {
159 struct list_head *tmp;
160 struct inter_module_entry *ime;
161 const void *result = NULL;
162
163 spin_lock(&ime_lock);
164 list_for_each(tmp, &ime_list) {
165 ime = list_entry(tmp, struct inter_module_entry, list);
166 if (strcmp(ime->im_name, im_name) == 0) {
167 if (try_inc_mod_count(ime->owner))
168 result = ime->userdata;
169 break;
170 }
171 }
172 spin_unlock(&ime_lock);
173 return(result);
174 }
175
176 /**
177 * inter_module_get_request - im get with automatic request_module.
178 * @im_name: an arbitrary string to identify the data, must be unique
179 * @modname: module that is expected to register im_name
180 *
181 * Description: If inter_module_get fails, do request_module then retry.
182 */
183 const void *inter_module_get_request(const char *im_name, const char *modname)
184 {
185 const void *result = inter_module_get(im_name);
186 if (!result) {
187 request_module(modname);
188 result = inter_module_get(im_name);
189 }
190 return(result);
191 }
192
193 /**
194 * inter_module_put - release use of data from another module.
195 * @im_name: an arbitrary string to identify the data, must be unique
196 *
197 * Description: If the im_name has not been registered, complain,
198 * otherwise decrement the use count on the owning module.
199 */
200 void inter_module_put(const char *im_name)
201 {
202 struct list_head *tmp;
203 struct inter_module_entry *ime;
204
205 spin_lock(&ime_lock);
206 list_for_each(tmp, &ime_list) {
207 ime = list_entry(tmp, struct inter_module_entry, list);
208 if (strcmp(ime->im_name, im_name) == 0) {
209 if (ime->owner)
210 __MOD_DEC_USE_COUNT(ime->owner);
211 spin_unlock(&ime_lock);
212 return;
213 }
214 }
215 spin_unlock(&ime_lock);
216 printk(KERN_ERR "inter_module_put: no entry for '%s'", im_name);
217 BUG();
218 }
219
220
221 #if defined(CONFIG_MODULES) /* The rest of the source */
222
223 static long get_mod_name(const char *user_name, char **buf);
224 static void put_mod_name(char *buf);
225 struct module *find_module(const char *name);
226 void free_module(struct module *, int tag_freed);
227
228
229 /*
230 * Called at boot time
231 */
232
233 void __init init_modules(void)
234 {
235 kernel_module.nsyms = __stop___ksymtab - __start___ksymtab;
236
237 #ifdef __alpha__
238 __asm__("stq $29,%0" : "=m"(kernel_module.gp));
239 #endif
240 }
241
242 /*
243 * Copy the name of a module from user space.
244 */
245
246 static inline long
247 get_mod_name(const char *user_name, char **buf)
248 {
249 unsigned long page;
250 long retval;
251
252 page = __get_free_page(GFP_KERNEL);
253 if (!page)
254 return -ENOMEM;
255
256 retval = strncpy_from_user((char *)page, user_name, PAGE_SIZE);
257 if (retval > 0) {
258 if (retval < PAGE_SIZE) {
259 *buf = (char *)page;
260 return retval;
261 }
262 retval = -ENAMETOOLONG;
263 } else if (!retval)
264 retval = -EINVAL;
265
266 free_page(page);
267 return retval;
268 }
269
270 static inline void
271 put_mod_name(char *buf)
272 {
273 free_page((unsigned long)buf);
274 }
275
276 /*
277 * Allocate space for a module.
278 */
279
280 asmlinkage unsigned long
281 sys_create_module(const char *name_user, size_t size)
282 {
283 char *name;
284 long namelen, error;
285 struct module *mod;
286
287 if (!capable(CAP_SYS_MODULE))
288 return -EPERM;
289 lock_kernel();
290 if ((namelen = get_mod_name(name_user, &name)) < 0) {
291 error = namelen;
292 goto err0;
293 }
294 if (size < sizeof(struct module)+namelen) {
295 error = -EINVAL;
296 goto err1;
297 }
298 if (find_module(name) != NULL) {
299 error = -EEXIST;
300 goto err1;
301 }
302 if ((mod = (struct module *)module_map(size)) == NULL) {
303 error = -ENOMEM;
304 goto err1;
305 }
306
307 memset(mod, 0, sizeof(*mod));
308 mod->size_of_struct = sizeof(*mod);
309 mod->next = module_list;
310 mod->name = (char *)(mod + 1);
311 mod->size = size;
312 memcpy((char*)(mod+1), name, namelen+1);
313
314 put_mod_name(name);
315
316 module_list = mod; /* link it in */
317
318 error = (long) mod;
319 goto err0;
320 err1:
321 put_mod_name(name);
322 err0:
323 unlock_kernel();
324 return error;
325 }
326
327 /*
328 * Initialize a module.
329 */
330
331 asmlinkage long
332 sys_init_module(const char *name_user, struct module *mod_user)
333 {
334 struct module mod_tmp, *mod;
335 char *name, *n_name, *name_tmp = NULL;
336 long namelen, n_namelen, i, error;
337 unsigned long mod_user_size;
338 struct module_ref *dep;
339
340 if (!capable(CAP_SYS_MODULE))
341 return -EPERM;
342 lock_kernel();
343 if ((namelen = get_mod_name(name_user, &name)) < 0) {
344 error = namelen;
345 goto err0;
346 }
347 if ((mod = find_module(name)) == NULL) {
348 error = -ENOENT;
349 goto err1;
350 }
351
352 /* Check module header size. We allow a bit of slop over the
353 size we are familiar with to cope with a version of insmod
354 for a newer kernel. But don't over do it. */
355 if ((error = get_user(mod_user_size, &mod_user->size_of_struct)) != 0)
356 goto err1;
357 if (mod_user_size < (unsigned long)&((struct module *)0L)->persist_start
358 || mod_user_size > sizeof(struct module) + 16*sizeof(void*)) {
359 printk(KERN_ERR "init_module: Invalid module header size.\n"
360 KERN_ERR "A new version of the modutils is likely "
361 "needed.\n");
362 error = -EINVAL;
363 goto err1;
364 }
365
366 /* Hold the current contents while we play with the user's idea
367 of righteousness. */
368 mod_tmp = *mod;
369 name_tmp = kmalloc(strlen(mod->name) + 1, GFP_KERNEL); /* Where's kstrdup()? */
370 if (name_tmp == NULL) {
371 error = -ENOMEM;
372 goto err1;
373 }
374 strcpy(name_tmp, mod->name);
375
376 error = copy_from_user(mod, mod_user, mod_user_size);
377 if (error) {
378 error = -EFAULT;
379 goto err2;
380 }
381
382 /* Sanity check the size of the module. */
383 error = -EINVAL;
384
385 if (mod->size > mod_tmp.size) {
386 printk(KERN_ERR "init_module: Size of initialized module "
387 "exceeds size of created module.\n");
388 goto err2;
389 }
390
391 /* Make sure all interesting pointers are sane. */
392
393 if (!mod_bound(mod->name, namelen, mod)) {
394 printk(KERN_ERR "init_module: mod->name out of bounds.\n");
395 goto err2;
396 }
397 if (mod->nsyms && !mod_bound(mod->syms, mod->nsyms, mod)) {
398 printk(KERN_ERR "init_module: mod->syms out of bounds.\n");
399 goto err2;
400 }
401 if (mod->ndeps && !mod_bound(mod->deps, mod->ndeps, mod)) {
402 printk(KERN_ERR "init_module: mod->deps out of bounds.\n");
403 goto err2;
404 }
405 if (mod->init && !mod_bound(mod->init, 0, mod)) {
406 printk(KERN_ERR "init_module: mod->init out of bounds.\n");
407 goto err2;
408 }
409 if (mod->cleanup && !mod_bound(mod->cleanup, 0, mod)) {
410 printk(KERN_ERR "init_module: mod->cleanup out of bounds.\n");
411 goto err2;
412 }
413 if (mod->ex_table_start > mod->ex_table_end
414 || (mod->ex_table_start &&
415 !((unsigned long)mod->ex_table_start >= ((unsigned long)mod + mod->size_of_struct)
416 && ((unsigned long)mod->ex_table_end
417 < (unsigned long)mod + mod->size)))
418 || (((unsigned long)mod->ex_table_start
419 - (unsigned long)mod->ex_table_end)
420 % sizeof(struct exception_table_entry))) {
421 printk(KERN_ERR "init_module: mod->ex_table_* invalid.\n");
422 goto err2;
423 }
424 if (mod->flags & ~MOD_AUTOCLEAN) {
425 printk(KERN_ERR "init_module: mod->flags invalid.\n");
426 goto err2;
427 }
428 #ifdef __alpha__
429 if (!mod_bound(mod->gp - 0x8000, 0, mod)) {
430 printk(KERN_ERR "init_module: mod->gp out of bounds.\n");
431 goto err2;
432 }
433 #endif
434 if (mod_member_present(mod, can_unload)
435 && mod->can_unload && !mod_bound(mod->can_unload, 0, mod)) {
436 printk(KERN_ERR "init_module: mod->can_unload out of bounds.\n");
437 goto err2;
438 }
439 if (mod_member_present(mod, kallsyms_end)) {
440 if (mod->kallsyms_end &&
441 (!mod_bound(mod->kallsyms_start, 0, mod) ||
442 !mod_bound(mod->kallsyms_end, 0, mod))) {
443 printk(KERN_ERR "init_module: mod->kallsyms out of bounds.\n");
444 goto err2;
445 }
446 if (mod->kallsyms_start > mod->kallsyms_end) {
447 printk(KERN_ERR "init_module: mod->kallsyms invalid.\n");
448 goto err2;
449 }
450 }
451 if (mod_member_present(mod, archdata_end)) {
452 if (mod->archdata_end &&
453 (!mod_bound(mod->archdata_start, 0, mod) ||
454 !mod_bound(mod->archdata_end, 0, mod))) {
455 printk(KERN_ERR "init_module: mod->archdata out of bounds.\n");
456 goto err2;
457 }
458 if (mod->archdata_start > mod->archdata_end) {
459 printk(KERN_ERR "init_module: mod->archdata invalid.\n");
460 goto err2;
461 }
462 }
463 if (mod_member_present(mod, kernel_data) && mod->kernel_data) {
464 printk(KERN_ERR "init_module: mod->kernel_data must be zero.\n");
465 goto err2;
466 }
467
468 /* Check that the user isn't doing something silly with the name. */
469
470 if ((n_namelen = get_mod_name(mod->name - (unsigned long)mod
471 + (unsigned long)mod_user,
472 &n_name)) < 0) {
473 printk(KERN_ERR "init_module: get_mod_name failure.\n");
474 error = n_namelen;
475 goto err2;
476 }
477 if (namelen != n_namelen || strcmp(n_name, mod_tmp.name) != 0) {
478 printk(KERN_ERR "init_module: changed module name to "
479 "`%s' from `%s'\n",
480 n_name, mod_tmp.name);
481 goto err3;
482 }
483
484 /* Ok, that's about all the sanity we can stomach; copy the rest. */
485
486 if (copy_from_user((char *)mod+mod_user_size,
487 (char *)mod_user+mod_user_size,
488 mod->size-mod_user_size)) {
489 error = -EFAULT;
490 goto err3;
491 }
492
493 if (module_arch_init(mod))
494 goto err3;
495
496 /* On some machines it is necessary to do something here
497 to make the I and D caches consistent. */
498 flush_icache_range((unsigned long)mod, (unsigned long)mod + mod->size);
499
500 mod->next = mod_tmp.next;
501 mod->refs = NULL;
502
503 /* Sanity check the module's dependents */
504 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
505 struct module *o, *d = dep->dep;
506
507 /* Make sure the indicated dependencies are really modules. */
508 if (d == mod) {
509 printk(KERN_ERR "init_module: self-referential "
510 "dependency in mod->deps.\n");
511 goto err3;
512 }
513
514 /* Scan the current modules for this dependency */
515 for (o = module_list; o != &kernel_module && o != d; o = o->next)
516 ;
517
518 if (o != d) {
519 printk(KERN_ERR "init_module: found dependency that is "
520 "(no longer?) a module.\n");
521 goto err3;
522 }
523 }
524
525 /* Update module references. */
526 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
527 struct module *d = dep->dep;
528
529 dep->ref = mod;
530 dep->next_ref = d->refs;
531 d->refs = dep;
532 /* Being referenced by a dependent module counts as a
533 use as far as kmod is concerned. */
534 d->flags |= MOD_USED_ONCE;
535 }
536
537 /* Free our temporary memory. */
538 put_mod_name(n_name);
539 put_mod_name(name);
540
541 /* Initialize the module. */
542 mod->flags |= MOD_INITIALIZING;
543 atomic_set(&mod->uc.usecount,1);
544 if (mod->init && (error = mod->init()) != 0) {
545 atomic_set(&mod->uc.usecount,0);
546 mod->flags &= ~MOD_INITIALIZING;
547 if (error > 0) /* Buggy module */
548 error = -EBUSY;
549 goto err0;
550 }
551 atomic_dec(&mod->uc.usecount);
552
553 /* And set it running. */
554 mod->flags = (mod->flags | MOD_RUNNING) & ~MOD_INITIALIZING;
555 error = 0;
556 goto err0;
557
558 err3:
559 put_mod_name(n_name);
560 err2:
561 *mod = mod_tmp;
562 strcpy((char *)mod->name, name_tmp); /* We know there is room for this */
563 err1:
564 put_mod_name(name);
565 err0:
566 unlock_kernel();
567 kfree(name_tmp);
568 return error;
569 }
570
571 static spinlock_t unload_lock = SPIN_LOCK_UNLOCKED;
572 int try_inc_mod_count(struct module *mod)
573 {
574 int res = 1;
575 if (mod) {
576 spin_lock(&unload_lock);
577 if (mod->flags & MOD_DELETED)
578 res = 0;
579 else
580 __MOD_INC_USE_COUNT(mod);
581 spin_unlock(&unload_lock);
582 }
583 return res;
584 }
585
586 asmlinkage long
587 sys_delete_module(const char *name_user)
588 {
589 struct module *mod, *next;
590 char *name;
591 long error;
592 int something_changed;
593
594 if (!capable(CAP_SYS_MODULE))
595 return -EPERM;
596
597 lock_kernel();
598 if (name_user) {
599 if ((error = get_mod_name(name_user, &name)) < 0)
600 goto out;
601 if (error == 0) {
602 error = -EINVAL;
603 put_mod_name(name);
604 goto out;
605 }
606 error = -ENOENT;
607 if ((mod = find_module(name)) == NULL) {
608 put_mod_name(name);
609 goto out;
610 }
611 put_mod_name(name);
612 error = -EBUSY;
613 if (mod->refs != NULL)
614 goto out;
615
616 spin_lock(&unload_lock);
617 if (!__MOD_IN_USE(mod)) {
618 mod->flags |= MOD_DELETED;
619 spin_unlock(&unload_lock);
620 free_module(mod, 0);
621 error = 0;
622 } else {
623 spin_unlock(&unload_lock);
624 }
625 goto out;
626 }
627
628 /* Do automatic reaping */
629 restart:
630 something_changed = 0;
631 for (mod = module_list; mod != &kernel_module; mod = next) {
632 next = mod->next;
633 spin_lock(&unload_lock);
634 if (mod->refs == NULL
635 && (mod->flags & MOD_AUTOCLEAN)
636 && (mod->flags & MOD_RUNNING)
637 && !(mod->flags & MOD_DELETED)
638 && (mod->flags & MOD_USED_ONCE)
639 && !__MOD_IN_USE(mod)) {
640 if ((mod->flags & MOD_VISITED)
641 && !(mod->flags & MOD_JUST_FREED)) {
642 spin_unlock(&unload_lock);
643 mod->flags &= ~MOD_VISITED;
644 } else {
645 mod->flags |= MOD_DELETED;
646 spin_unlock(&unload_lock);
647 free_module(mod, 1);
648 something_changed = 1;
649 }
650 } else {
651 spin_unlock(&unload_lock);
652 }
653 }
654 if (something_changed)
655 goto restart;
656 for (mod = module_list; mod != &kernel_module; mod = mod->next)
657 mod->flags &= ~MOD_JUST_FREED;
658 error = 0;
659 out:
660 unlock_kernel();
661 return error;
662 }
663
664 /* Query various bits about modules. */
665
666 static int
667 qm_modules(char *buf, size_t bufsize, size_t *ret)
668 {
669 struct module *mod;
670 size_t nmod, space, len;
671
672 nmod = space = 0;
673
674 for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
675 len = strlen(mod->name)+1;
676 if (len > bufsize)
677 goto calc_space_needed;
678 if (copy_to_user(buf, mod->name, len))
679 return -EFAULT;
680 buf += len;
681 bufsize -= len;
682 space += len;
683 }
684
685 if (put_user(nmod, ret))
686 return -EFAULT;
687 else
688 return 0;
689
690 calc_space_needed:
691 space += len;
692 while ((mod = mod->next) != &kernel_module)
693 space += strlen(mod->name)+1;
694
695 if (put_user(space, ret))
696 return -EFAULT;
697 else
698 return -ENOSPC;
699 }
700
701 static int
702 qm_deps(struct module *mod, char *buf, size_t bufsize, size_t *ret)
703 {
704 size_t i, space, len;
705
706 if (mod == &kernel_module)
707 return -EINVAL;
708 if (!MOD_CAN_QUERY(mod))
709 if (put_user(0, ret))
710 return -EFAULT;
711 else
712 return 0;
713
714 space = 0;
715 for (i = 0; i < mod->ndeps; ++i) {
716 const char *dep_name = mod->deps[i].dep->name;
717
718 len = strlen(dep_name)+1;
719 if (len > bufsize)
720 goto calc_space_needed;
721 if (copy_to_user(buf, dep_name, len))
722 return -EFAULT;
723 buf += len;
724 bufsize -= len;
725 space += len;
726 }
727
728 if (put_user(i, ret))
729 return -EFAULT;
730 else
731 return 0;
732
733 calc_space_needed:
734 space += len;
735 while (++i < mod->ndeps)
736 space += strlen(mod->deps[i].dep->name)+1;
737
738 if (put_user(space, ret))
739 return -EFAULT;
740 else
741 return -ENOSPC;
742 }
743
744 static int
745 qm_refs(struct module *mod, char *buf, size_t bufsize, size_t *ret)
746 {
747 size_t nrefs, space, len;
748 struct module_ref *ref;
749
750 if (mod == &kernel_module)
751 return -EINVAL;
752 if (!MOD_CAN_QUERY(mod))
753 if (put_user(0, ret))
754 return -EFAULT;
755 else
756 return 0;
757
758 space = 0;
759 for (nrefs = 0, ref = mod->refs; ref ; ++nrefs, ref = ref->next_ref) {
760 const char *ref_name = ref->ref->name;
761
762 len = strlen(ref_name)+1;
763 if (len > bufsize)
764 goto calc_space_needed;
765 if (copy_to_user(buf, ref_name, len))
766 return -EFAULT;
767 buf += len;
768 bufsize -= len;
769 space += len;
770 }
771
772 if (put_user(nrefs, ret))
773 return -EFAULT;
774 else
775 return 0;
776
777 calc_space_needed:
778 space += len;
779 while ((ref = ref->next_ref) != NULL)
780 space += strlen(ref->ref->name)+1;
781
782 if (put_user(space, ret))
783 return -EFAULT;
784 else
785 return -ENOSPC;
786 }
787
788 static int
789 qm_symbols(struct module *mod, char *buf, size_t bufsize, size_t *ret)
790 {
791 size_t i, space, len;
792 struct module_symbol *s;
793 char *strings;
794 unsigned long *vals;
795
796 if (!MOD_CAN_QUERY(mod))
797 if (put_user(0, ret))
798 return -EFAULT;
799 else
800 return 0;
801
802 space = mod->nsyms * 2*sizeof(void *);
803
804 i = len = 0;
805 s = mod->syms;
806
807 if (space > bufsize)
808 goto calc_space_needed;
809
810 if (!access_ok(VERIFY_WRITE, buf, space))
811 return -EFAULT;
812
813 bufsize -= space;
814 vals = (unsigned long *)buf;
815 strings = buf+space;
816
817 for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
818 len = strlen(s->name)+1;
819 if (len > bufsize)
820 goto calc_space_needed;
821
822 if (copy_to_user(strings, s->name, len)
823 || __put_user(s->value, vals+0)
824 || __put_user(space, vals+1))
825 return -EFAULT;
826
827 strings += len;
828 bufsize -= len;
829 space += len;
830 }
831
832 if (put_user(i, ret))
833 return -EFAULT;
834 else
835 return 0;
836
837 calc_space_needed:
838 for (; i < mod->nsyms; ++i, ++s)
839 space += strlen(s->name)+1;
840
841 if (put_user(space, ret))
842 return -EFAULT;
843 else
844 return -ENOSPC;
845 }
846
847 static int
848 qm_info(struct module *mod, char *buf, size_t bufsize, size_t *ret)
849 {
850 int error = 0;
851
852 if (mod == &kernel_module)
853 return -EINVAL;
854
855 if (sizeof(struct module_info) <= bufsize) {
856 struct module_info info;
857 info.addr = (unsigned long)mod;
858 info.size = mod->size;
859 info.flags = mod->flags;
860 info.usecount = (mod_member_present(mod, can_unload)
861 && mod->can_unload ? -1 : atomic_read(&mod->uc.usecount));
862
863 if (copy_to_user(buf, &info, sizeof(struct module_info)))
864 return -EFAULT;
865 } else
866 error = -ENOSPC;
867
868 if (put_user(sizeof(struct module_info), ret))
869 return -EFAULT;
870
871 return error;
872 }
873
874 asmlinkage long
875 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
876 size_t *ret)
877 {
878 struct module *mod;
879 int err;
880
881 lock_kernel();
882 if (name_user == NULL)
883 mod = &kernel_module;
884 else {
885 long namelen;
886 char *name;
887
888 if ((namelen = get_mod_name(name_user, &name)) < 0) {
889 err = namelen;
890 goto out;
891 }
892 err = -ENOENT;
893 if (namelen == 0)
894 mod = &kernel_module;
895 else if ((mod = find_module(name)) == NULL) {
896 put_mod_name(name);
897 goto out;
898 }
899 put_mod_name(name);
900 }
901
902 switch (which)
903 {
904 case 0:
905 err = 0;
906 break;
907 case QM_MODULES:
908 err = qm_modules(buf, bufsize, ret);
909 break;
910 case QM_DEPS:
911 err = qm_deps(mod, buf, bufsize, ret);
912 break;
913 case QM_REFS:
914 err = qm_refs(mod, buf, bufsize, ret);
915 break;
916 case QM_SYMBOLS:
917 err = qm_symbols(mod, buf, bufsize, ret);
918 break;
919 case QM_INFO:
920 err = qm_info(mod, buf, bufsize, ret);
921 break;
922 default:
923 err = -EINVAL;
924 break;
925 }
926 out:
927 unlock_kernel();
928 return err;
929 }
930
931 /*
932 * Copy the kernel symbol table to user space. If the argument is
933 * NULL, just return the size of the table.
934 *
935 * This call is obsolete. New programs should use query_module+QM_SYMBOLS
936 * which does not arbitrarily limit the length of symbols.
937 */
938
939 asmlinkage long
940 sys_get_kernel_syms(struct kernel_sym *table)
941 {
942 struct module *mod;
943 int i;
944 struct kernel_sym ksym;
945
946 lock_kernel();
947 for (mod = module_list, i = 0; mod; mod = mod->next) {
948 /* include the count for the module name! */
949 i += mod->nsyms + 1;
950 }
951
952 if (table == NULL)
953 goto out;
954
955 /* So that we don't give the user our stack content */
956 memset (&ksym, 0, sizeof (ksym));
957
958 for (mod = module_list, i = 0; mod; mod = mod->next) {
959 struct module_symbol *msym;
960 unsigned int j;
961
962 if (!MOD_CAN_QUERY(mod))
963 continue;
964
965 /* magic: write module info as a pseudo symbol */
966 ksym.value = (unsigned long)mod;
967 ksym.name[0] = '#';
968 strncpy(ksym.name+1, mod->name, sizeof(ksym.name)-1);
969 ksym.name[sizeof(ksym.name)-1] = '\0';
970
971 if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
972 goto out;
973 ++i, ++table;
974
975 if (mod->nsyms == 0)
976 continue;
977
978 for (j = 0, msym = mod->syms; j < mod->nsyms; ++j, ++msym) {
979 ksym.value = msym->value;
980 strncpy(ksym.name, msym->name, sizeof(ksym.name));
981 ksym.name[sizeof(ksym.name)-1] = '\0';
982
983 if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
984 goto out;
985 ++i, ++table;
986 }
987 }
988 out:
989 unlock_kernel();
990 return i;
991 }
992
993 /*
994 * Look for a module by name, ignoring modules marked for deletion.
995 */
996
997 struct module *
998 find_module(const char *name)
999 {
1000 struct module *mod;
1001
1002 for (mod = module_list; mod ; mod = mod->next) {
1003 if (mod->flags & MOD_DELETED)
1004 continue;
1005 if (!strcmp(mod->name, name))
1006 break;
1007 }
1008
1009 return mod;
1010 }
1011
1012 /*
1013 * Free the given module.
1014 */
1015
1016 void
1017 free_module(struct module *mod, int tag_freed)
1018 {
1019 struct module_ref *dep;
1020 unsigned i;
1021
1022 /* Let the module clean up. */
1023
1024 if (mod->flags & MOD_RUNNING)
1025 {
1026 if(mod->cleanup)
1027 mod->cleanup();
1028 mod->flags &= ~MOD_RUNNING;
1029 }
1030
1031 /* Remove the module from the dependency lists. */
1032
1033 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
1034 struct module_ref **pp;
1035 for (pp = &dep->dep->refs; *pp != dep; pp = &(*pp)->next_ref)
1036 continue;
1037 *pp = dep->next_ref;
1038 if (tag_freed && dep->dep->refs == NULL)
1039 dep->dep->flags |= MOD_JUST_FREED;
1040 }
1041
1042 /* And from the main module list. */
1043
1044 if (mod == module_list) {
1045 module_list = mod->next;
1046 } else {
1047 struct module *p;
1048 for (p = module_list; p->next != mod; p = p->next)
1049 continue;
1050 p->next = mod->next;
1051 }
1052
1053 /* And free the memory. */
1054
1055 module_unmap(mod);
1056 }
1057
1058 /*
1059 * Called by the /proc file system to return a current list of modules.
1060 */
1061
1062 int get_module_list(char *p)
1063 {
1064 size_t left = PAGE_SIZE;
1065 struct module *mod;
1066 char tmpstr[64];
1067 struct module_ref *ref;
1068
1069 for (mod = module_list; mod != &kernel_module; mod = mod->next) {
1070 long len;
1071 const char *q;
1072
1073 #define safe_copy_str(str, len) \
1074 do { \
1075 if (left < len) \
1076 goto fini; \
1077 memcpy(p, str, len); p += len, left -= len; \
1078 } while (0)
1079 #define safe_copy_cstr(str) safe_copy_str(str, sizeof(str)-1)
1080
1081 len = strlen(mod->name);
1082 safe_copy_str(mod->name, len);
1083
1084 if ((len = 20 - len) > 0) {
1085 if (left < len)
1086 goto fini;
1087 memset(p, ' ', len);
1088 p += len;
1089 left -= len;
1090 }
1091
1092 len = sprintf(tmpstr, "%8lu", mod->size);
1093 safe_copy_str(tmpstr, len);
1094
1095 if (mod->flags & MOD_RUNNING) {
1096 len = sprintf(tmpstr, "%4ld",
1097 (mod_member_present(mod, can_unload)
1098 && mod->can_unload
1099 ? -1L : (long)atomic_read(&mod->uc.usecount)));
1100 safe_copy_str(tmpstr, len);
1101 }
1102
1103 if (mod->flags & MOD_DELETED)
1104 safe_copy_cstr(" (deleted)");
1105 else if (mod->flags & MOD_RUNNING) {
1106 if (mod->flags & MOD_AUTOCLEAN)
1107 safe_copy_cstr(" (autoclean)");
1108 if (!(mod->flags & MOD_USED_ONCE))
1109 safe_copy_cstr(" (unused)");
1110 }
1111 else if (mod->flags & MOD_INITIALIZING)
1112 safe_copy_cstr(" (initializing)");
1113 else
1114 safe_copy_cstr(" (uninitialized)");
1115
1116 if ((ref = mod->refs) != NULL) {
1117 safe_copy_cstr(" [");
1118 while (1) {
1119 q = ref->ref->name;
1120 len = strlen(q);
1121 safe_copy_str(q, len);
1122
1123 if ((ref = ref->next_ref) != NULL)
1124 safe_copy_cstr(" ");
1125 else
1126 break;
1127 }
1128 safe_copy_cstr("]");
1129 }
1130 safe_copy_cstr("\n");
1131
1132 #undef safe_copy_str
1133 #undef safe_copy_cstr
1134 }
1135
1136 fini:
1137 return PAGE_SIZE - left;
1138 }
1139
1140 /*
1141 * Called by the /proc file system to return a current list of ksyms.
1142 */
1143
1144 int
1145 get_ksyms_list(char *buf, char **start, off_t offset, int length)
1146 {
1147 struct module *mod;
1148 char *p = buf;
1149 int len = 0; /* code from net/ipv4/proc.c */
1150 off_t pos = 0;
1151 off_t begin = 0;
1152
1153 for (mod = module_list; mod; mod = mod->next) {
1154 unsigned i;
1155 struct module_symbol *sym;
1156
1157 if (!MOD_CAN_QUERY(mod))
1158 continue;
1159
1160 for (i = mod->nsyms, sym = mod->syms; i > 0; --i, ++sym) {
1161 p = buf + len;
1162 if (*mod->name) {
1163 len += sprintf(p, "%0*lx %s\t[%s]\n",
1164 (int)(2*sizeof(void*)),
1165 sym->value, sym->name,
1166 mod->name);
1167 } else {
1168 len += sprintf(p, "%0*lx %s\n",
1169 (int)(2*sizeof(void*)),
1170 sym->value, sym->name);
1171 }
1172 pos = begin + len;
1173 if (pos < offset) {
1174 len = 0;
1175 begin = pos;
1176 }
1177 pos = begin + len;
1178 if (pos > offset+length)
1179 goto leave_the_loop;
1180 }
1181 }
1182 leave_the_loop:
1183 *start = buf + (offset - begin);
1184 len -= (offset - begin);
1185 if (len > length)
1186 len = length;
1187 return len;
1188 }
1189
1190 #else /* CONFIG_MODULES */
1191
1192 /* Dummy syscalls for people who don't want modules */
1193
1194 asmlinkage unsigned long
1195 sys_create_module(const char *name_user, size_t size)
1196 {
1197 return -ENOSYS;
1198 }
1199
1200 asmlinkage long
1201 sys_init_module(const char *name_user, struct module *mod_user)
1202 {
1203 return -ENOSYS;
1204 }
1205
1206 asmlinkage long
1207 sys_delete_module(const char *name_user)
1208 {
1209 return -ENOSYS;
1210 }
1211
1212 asmlinkage long
1213 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
1214 size_t *ret)
1215 {
1216 /* Let the program know about the new interface. Not that
1217 it'll do them much good. */
1218 if (which == 0)
1219 return 0;
1220
1221 return -ENOSYS;
1222 }
1223
1224 asmlinkage long
1225 sys_get_kernel_syms(struct kernel_sym *table)
1226 {
1227 return -ENOSYS;
1228 }
1229
1230 int try_inc_mod_count(struct module *mod)
1231 {
1232 return 1;
1233 }
1234
1235 #endif /* CONFIG_MODULES */
1236
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.