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