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