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