aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c127
1 files changed, 106 insertions, 21 deletions
diff --git a/kernel/module.c b/kernel/module.c
index e797812a4d95..2d537186191f 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -18,6 +18,7 @@
18*/ 18*/
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/moduleloader.h> 20#include <linux/moduleloader.h>
21#include <linux/ftrace_event.h>
21#include <linux/init.h> 22#include <linux/init.h>
22#include <linux/kallsyms.h> 23#include <linux/kallsyms.h>
23#include <linux/fs.h> 24#include <linux/fs.h>
@@ -52,6 +53,7 @@
52#include <linux/ftrace.h> 53#include <linux/ftrace.h>
53#include <linux/async.h> 54#include <linux/async.h>
54#include <linux/percpu.h> 55#include <linux/percpu.h>
56#include <linux/kmemleak.h>
55 57
56#if 0 58#if 0
57#define DEBUGP printk 59#define DEBUGP printk
@@ -72,6 +74,9 @@ DEFINE_MUTEX(module_mutex);
72EXPORT_SYMBOL_GPL(module_mutex); 74EXPORT_SYMBOL_GPL(module_mutex);
73static LIST_HEAD(modules); 75static LIST_HEAD(modules);
74 76
77/* Block module loading/unloading? */
78int modules_disabled = 0;
79
75/* Waiting for a module to finish initializing? */ 80/* Waiting for a module to finish initializing? */
76static DECLARE_WAIT_QUEUE_HEAD(module_wq); 81static DECLARE_WAIT_QUEUE_HEAD(module_wq);
77 82
@@ -429,6 +434,7 @@ static void *percpu_modalloc(unsigned long size, unsigned long align,
429 unsigned long extra; 434 unsigned long extra;
430 unsigned int i; 435 unsigned int i;
431 void *ptr; 436 void *ptr;
437 int cpu;
432 438
433 if (align > PAGE_SIZE) { 439 if (align > PAGE_SIZE) {
434 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", 440 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
@@ -458,6 +464,11 @@ static void *percpu_modalloc(unsigned long size, unsigned long align,
458 if (!split_block(i, size)) 464 if (!split_block(i, size))
459 return NULL; 465 return NULL;
460 466
467 /* add the per-cpu scanning areas */
468 for_each_possible_cpu(cpu)
469 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
470 GFP_KERNEL);
471
461 /* Mark allocated */ 472 /* Mark allocated */
462 pcpu_size[i] = -pcpu_size[i]; 473 pcpu_size[i] = -pcpu_size[i];
463 return ptr; 474 return ptr;
@@ -472,6 +483,7 @@ static void percpu_modfree(void *freeme)
472{ 483{
473 unsigned int i; 484 unsigned int i;
474 void *ptr = __per_cpu_start + block_size(pcpu_size[0]); 485 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
486 int cpu;
475 487
476 /* First entry is core kernel percpu data. */ 488 /* First entry is core kernel percpu data. */
477 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { 489 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
@@ -483,6 +495,10 @@ static void percpu_modfree(void *freeme)
483 BUG(); 495 BUG();
484 496
485 free: 497 free:
498 /* remove the per-cpu scanning areas */
499 for_each_possible_cpu(cpu)
500 kmemleak_free(freeme + per_cpu_offset(cpu));
501
486 /* Merge with previous? */ 502 /* Merge with previous? */
487 if (pcpu_size[i-1] >= 0) { 503 if (pcpu_size[i-1] >= 0) {
488 pcpu_size[i-1] += pcpu_size[i]; 504 pcpu_size[i-1] += pcpu_size[i];
@@ -777,7 +793,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
777 char name[MODULE_NAME_LEN]; 793 char name[MODULE_NAME_LEN];
778 int ret, forced = 0; 794 int ret, forced = 0;
779 795
780 if (!capable(CAP_SYS_MODULE)) 796 if (!capable(CAP_SYS_MODULE) || modules_disabled)
781 return -EPERM; 797 return -EPERM;
782 798
783 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) 799 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
@@ -893,16 +909,18 @@ void __symbol_put(const char *symbol)
893} 909}
894EXPORT_SYMBOL(__symbol_put); 910EXPORT_SYMBOL(__symbol_put);
895 911
912/* Note this assumes addr is a function, which it currently always is. */
896void symbol_put_addr(void *addr) 913void symbol_put_addr(void *addr)
897{ 914{
898 struct module *modaddr; 915 struct module *modaddr;
916 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
899 917
900 if (core_kernel_text((unsigned long)addr)) 918 if (core_kernel_text(a))
901 return; 919 return;
902 920
903 /* module_text_address is safe here: we're supposed to have reference 921 /* module_text_address is safe here: we're supposed to have reference
904 * to module from symbol_get, so it can't go away. */ 922 * to module from symbol_get, so it can't go away. */
905 modaddr = __module_text_address((unsigned long)addr); 923 modaddr = __module_text_address(a);
906 BUG_ON(!modaddr); 924 BUG_ON(!modaddr);
907 module_put(modaddr); 925 module_put(modaddr);
908} 926}
@@ -1052,7 +1070,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1052{ 1070{
1053 const unsigned long *crc; 1071 const unsigned long *crc;
1054 1072
1055 if (!find_symbol("module_layout", NULL, &crc, true, false)) 1073 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1074 &crc, true, false))
1056 BUG(); 1075 BUG();
1057 return check_version(sechdrs, versindex, "module_layout", mod, crc); 1076 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1058} 1077}
@@ -1255,6 +1274,10 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1255 struct module_notes_attrs *notes_attrs; 1274 struct module_notes_attrs *notes_attrs;
1256 struct bin_attribute *nattr; 1275 struct bin_attribute *nattr;
1257 1276
1277 /* failed to create section attributes, so can't create notes */
1278 if (!mod->sect_attrs)
1279 return;
1280
1258 /* Count notes sections and allocate structures. */ 1281 /* Count notes sections and allocate structures. */
1259 notes = 0; 1282 notes = 0;
1260 for (i = 0; i < nsect; i++) 1283 for (i = 0; i < nsect; i++)
@@ -1489,9 +1512,6 @@ static void free_module(struct module *mod)
1489 /* Free any allocated parameters. */ 1512 /* Free any allocated parameters. */
1490 destroy_params(mod->kp, mod->num_kp); 1513 destroy_params(mod->kp, mod->num_kp);
1491 1514
1492 /* release any pointers to mcount in this module */
1493 ftrace_release(mod->module_core, mod->core_size);
1494
1495 /* This may be NULL, but that's OK */ 1515 /* This may be NULL, but that's OK */
1496 module_free(mod, mod->module_init); 1516 module_free(mod, mod->module_init);
1497 kfree(mod->args); 1517 kfree(mod->args);
@@ -1878,6 +1898,36 @@ static void *module_alloc_update_bounds(unsigned long size)
1878 return ret; 1898 return ret;
1879} 1899}
1880 1900
1901#ifdef CONFIG_DEBUG_KMEMLEAK
1902static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1903 Elf_Shdr *sechdrs, char *secstrings)
1904{
1905 unsigned int i;
1906
1907 /* only scan the sections containing data */
1908 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1909 (unsigned long)mod->module_core,
1910 sizeof(struct module), GFP_KERNEL);
1911
1912 for (i = 1; i < hdr->e_shnum; i++) {
1913 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1914 continue;
1915 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1916 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1917 continue;
1918
1919 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1920 (unsigned long)mod->module_core,
1921 sechdrs[i].sh_size, GFP_KERNEL);
1922 }
1923}
1924#else
1925static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1926 Elf_Shdr *sechdrs, char *secstrings)
1927{
1928}
1929#endif
1930
1881/* Allocate and load the module: note that size of section 0 is always 1931/* Allocate and load the module: note that size of section 0 is always
1882 zero, and we rely on this for optional sections. */ 1932 zero, and we rely on this for optional sections. */
1883static noinline struct module *load_module(void __user *umod, 1933static noinline struct module *load_module(void __user *umod,
@@ -1892,11 +1942,9 @@ static noinline struct module *load_module(void __user *umod,
1892 unsigned int symindex = 0; 1942 unsigned int symindex = 0;
1893 unsigned int strindex = 0; 1943 unsigned int strindex = 0;
1894 unsigned int modindex, versindex, infoindex, pcpuindex; 1944 unsigned int modindex, versindex, infoindex, pcpuindex;
1895 unsigned int num_mcount;
1896 struct module *mod; 1945 struct module *mod;
1897 long err = 0; 1946 long err = 0;
1898 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1947 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1899 unsigned long *mseg;
1900 mm_segment_t old_fs; 1948 mm_segment_t old_fs;
1901 1949
1902 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", 1950 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
@@ -2050,6 +2098,12 @@ static noinline struct module *load_module(void __user *umod,
2050 2098
2051 /* Do the allocs. */ 2099 /* Do the allocs. */
2052 ptr = module_alloc_update_bounds(mod->core_size); 2100 ptr = module_alloc_update_bounds(mod->core_size);
2101 /*
2102 * The pointer to this block is stored in the module structure
2103 * which is inside the block. Just mark it as not being a
2104 * leak.
2105 */
2106 kmemleak_not_leak(ptr);
2053 if (!ptr) { 2107 if (!ptr) {
2054 err = -ENOMEM; 2108 err = -ENOMEM;
2055 goto free_percpu; 2109 goto free_percpu;
@@ -2058,6 +2112,13 @@ static noinline struct module *load_module(void __user *umod,
2058 mod->module_core = ptr; 2112 mod->module_core = ptr;
2059 2113
2060 ptr = module_alloc_update_bounds(mod->init_size); 2114 ptr = module_alloc_update_bounds(mod->init_size);
2115 /*
2116 * The pointer to this block is stored in the module structure
2117 * which is inside the block. This block doesn't need to be
2118 * scanned as it contains data and code that will be freed
2119 * after the module is initialized.
2120 */
2121 kmemleak_ignore(ptr);
2061 if (!ptr && mod->init_size) { 2122 if (!ptr && mod->init_size) {
2062 err = -ENOMEM; 2123 err = -ENOMEM;
2063 goto free_core; 2124 goto free_core;
@@ -2088,6 +2149,7 @@ static noinline struct module *load_module(void __user *umod,
2088 } 2149 }
2089 /* Module has been moved. */ 2150 /* Module has been moved. */
2090 mod = (void *)sechdrs[modindex].sh_addr; 2151 mod = (void *)sechdrs[modindex].sh_addr;
2152 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2091 2153
2092#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2154#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2093 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t), 2155 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
@@ -2161,6 +2223,10 @@ static noinline struct module *load_module(void __user *umod,
2161 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings, 2223 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2162 "__kcrctab_unused_gpl"); 2224 "__kcrctab_unused_gpl");
2163#endif 2225#endif
2226#ifdef CONFIG_CONSTRUCTORS
2227 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2228 sizeof(*mod->ctors), &mod->num_ctors);
2229#endif
2164 2230
2165#ifdef CONFIG_MARKERS 2231#ifdef CONFIG_MARKERS
2166 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers", 2232 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
@@ -2172,7 +2238,19 @@ static noinline struct module *load_module(void __user *umod,
2172 sizeof(*mod->tracepoints), 2238 sizeof(*mod->tracepoints),
2173 &mod->num_tracepoints); 2239 &mod->num_tracepoints);
2174#endif 2240#endif
2175 2241#ifdef CONFIG_EVENT_TRACING
2242 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2243 "_ftrace_events",
2244 sizeof(*mod->trace_events),
2245 &mod->num_trace_events);
2246#endif
2247#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2248 /* sechdrs[0].sh_size is always zero */
2249 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2250 "__mcount_loc",
2251 sizeof(*mod->ftrace_callsites),
2252 &mod->num_ftrace_callsites);
2253#endif
2176#ifdef CONFIG_MODVERSIONS 2254#ifdef CONFIG_MODVERSIONS
2177 if ((mod->num_syms && !mod->crcs) 2255 if ((mod->num_syms && !mod->crcs)
2178 || (mod->num_gpl_syms && !mod->gpl_crcs) 2256 || (mod->num_gpl_syms && !mod->gpl_crcs)
@@ -2237,11 +2315,6 @@ static noinline struct module *load_module(void __user *umod,
2237 dynamic_debug_setup(debug, num_debug); 2315 dynamic_debug_setup(debug, num_debug);
2238 } 2316 }
2239 2317
2240 /* sechdrs[0].sh_size is always zero */
2241 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2242 sizeof(*mseg), &num_mcount);
2243 ftrace_init_module(mod, mseg, mseg + num_mcount);
2244
2245 err = module_finalize(hdr, sechdrs, mod); 2318 err = module_finalize(hdr, sechdrs, mod);
2246 if (err < 0) 2319 if (err < 0)
2247 goto cleanup; 2320 goto cleanup;
@@ -2302,7 +2375,6 @@ static noinline struct module *load_module(void __user *umod,
2302 cleanup: 2375 cleanup:
2303 kobject_del(&mod->mkobj.kobj); 2376 kobject_del(&mod->mkobj.kobj);
2304 kobject_put(&mod->mkobj.kobj); 2377 kobject_put(&mod->mkobj.kobj);
2305 ftrace_release(mod->module_core, mod->core_size);
2306 free_unload: 2378 free_unload:
2307 module_unload_free(mod); 2379 module_unload_free(mod);
2308#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2380#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
@@ -2328,6 +2400,17 @@ static noinline struct module *load_module(void __user *umod,
2328 goto free_hdr; 2400 goto free_hdr;
2329} 2401}
2330 2402
2403/* Call module constructors. */
2404static void do_mod_ctors(struct module *mod)
2405{
2406#ifdef CONFIG_CONSTRUCTORS
2407 unsigned long i;
2408
2409 for (i = 0; i < mod->num_ctors; i++)
2410 mod->ctors[i]();
2411#endif
2412}
2413
2331/* This is where the real work happens */ 2414/* This is where the real work happens */
2332SYSCALL_DEFINE3(init_module, void __user *, umod, 2415SYSCALL_DEFINE3(init_module, void __user *, umod,
2333 unsigned long, len, const char __user *, uargs) 2416 unsigned long, len, const char __user *, uargs)
@@ -2336,7 +2419,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2336 int ret = 0; 2419 int ret = 0;
2337 2420
2338 /* Must have permission */ 2421 /* Must have permission */
2339 if (!capable(CAP_SYS_MODULE)) 2422 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2340 return -EPERM; 2423 return -EPERM;
2341 2424
2342 /* Only one module load at a time, please */ 2425 /* Only one module load at a time, please */
@@ -2356,6 +2439,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2356 blocking_notifier_call_chain(&module_notify_list, 2439 blocking_notifier_call_chain(&module_notify_list,
2357 MODULE_STATE_COMING, mod); 2440 MODULE_STATE_COMING, mod);
2358 2441
2442 do_mod_ctors(mod);
2359 /* Start the module */ 2443 /* Start the module */
2360 if (mod->init != NULL) 2444 if (mod->init != NULL)
2361 ret = do_one_initcall(mod->init); 2445 ret = do_one_initcall(mod->init);
@@ -2374,9 +2458,9 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2374 return ret; 2458 return ret;
2375 } 2459 }
2376 if (ret > 0) { 2460 if (ret > 0) {
2377 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, " 2461 printk(KERN_WARNING
2378 "it should follow 0/-E convention\n" 2462"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2379 KERN_WARNING "%s: loading module anyway...\n", 2463"%s: loading module anyway...\n",
2380 __func__, mod->name, ret, 2464 __func__, mod->name, ret,
2381 __func__); 2465 __func__);
2382 dump_stack(); 2466 dump_stack();
@@ -2394,6 +2478,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2394 mutex_lock(&module_mutex); 2478 mutex_lock(&module_mutex);
2395 /* Drop initial reference. */ 2479 /* Drop initial reference. */
2396 module_put(mod); 2480 module_put(mod);
2481 trim_init_extable(mod);
2397 module_free(mod, mod->module_init); 2482 module_free(mod, mod->module_init);
2398 mod->module_init = NULL; 2483 mod->module_init = NULL;
2399 mod->init_size = 0; 2484 mod->init_size = 0;
@@ -2837,7 +2922,7 @@ void print_modules(void)
2837 struct module *mod; 2922 struct module *mod;
2838 char buf[8]; 2923 char buf[8];
2839 2924
2840 printk("Modules linked in:"); 2925 printk(KERN_DEFAULT "Modules linked in:");
2841 /* Most callers should already have preempt disabled, but make sure */ 2926 /* Most callers should already have preempt disabled, but make sure */
2842 preempt_disable(); 2927 preempt_disable();
2843 list_for_each_entry_rcu(mod, &modules, list) 2928 list_for_each_entry_rcu(mod, &modules, list)