blob: 9b0e36145474b70c3ffd703fbf48d73964ab9b37 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Paul Gortmaker9984de12011-05-23 14:51:41 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Kees Cook34e11692012-10-16 07:31:07 +103024#include <linux/file.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040025#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070026#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070027#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
29#include <linux/vmalloc.h>
30#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040031#include <linux/proc_fs.h>
Kees Cook2e72d512012-10-16 07:32:07 +103032#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/seq_file.h>
34#include <linux/syscalls.h>
35#include <linux/fcntl.h>
36#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/cpu.h>
39#include <linux/moduleparam.h>
40#include <linux/errno.h>
41#include <linux/err.h>
42#include <linux/vermagic.h>
43#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040044#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070046#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080047#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020048#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070051#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020052#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080053#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040054#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040055#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080056#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090057#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010058#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040059#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010060#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020061#include <linux/bsearch.h>
Rusty Russell2f3238a2012-10-22 18:09:41 +103062#include <uapi/linux/module.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010063#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Li Zefan7ead8b82009-08-17 16:56:28 +080065#define CREATE_TRACE_POINTS
66#include <trace/events/module.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
matthieu castet84e1c6b2010-11-16 22:35:16 +010072/*
73 * Modules' sections will be aligned on page boundaries
74 * to ensure complete separation of code and data, but
75 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
76 */
77#ifdef CONFIG_DEBUG_SET_MODULE_RONX
78# define debug_align(X) ALIGN(X, PAGE_SIZE)
79#else
80# define debug_align(X) (X)
81#endif
82
83/*
84 * Given BASE and SIZE this macro calculates the number of pages the
85 * memory regions occupies
86 */
87#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
88 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
89 PFN_DOWN((unsigned long)BASE) + 1) \
90 : (0UL))
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* If this is set, the section belongs in the init part of the module */
93#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
94
Rusty Russell75676502010-06-05 11:17:36 -060095/*
96 * Mutex protects:
97 * 1) List of modules (also safely readable with preempt_disable),
98 * 2) module_use links,
99 * 3) module_addr_min/module_addr_max.
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030100 * (delete and add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500101DEFINE_MUTEX(module_mutex);
102EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static LIST_HEAD(modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930104
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930105#ifdef CONFIG_MODULES_TREE_LOOKUP
106
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930107/*
108 * Use a latched RB-tree for __module_address(); this allows us to use
109 * RCU-sched lookups of the address from any context.
110 *
111 * Because modules have two address ranges: init and core, we need two
112 * latch_tree_nodes entries. Therefore we need the back-pointer from
113 * mod_tree_node.
114 *
115 * Because init ranges are short lived we mark them unlikely and have placed
116 * them outside the critical cacheline in struct module.
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930117 *
118 * This is conditional on PERF_EVENTS || TRACING because those can really hit
119 * __module_address() hard by doing a lot of stack unwinding; potentially from
120 * NMI context.
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930121 */
122
123static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
124{
125 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
126 struct module *mod = mtn->mod;
127
128 if (unlikely(mtn == &mod->mtn_init))
129 return (unsigned long)mod->module_init;
130
131 return (unsigned long)mod->module_core;
132}
133
134static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
135{
136 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
137 struct module *mod = mtn->mod;
138
139 if (unlikely(mtn == &mod->mtn_init))
140 return (unsigned long)mod->init_size;
141
142 return (unsigned long)mod->core_size;
143}
144
145static __always_inline bool
146mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
147{
148 return __mod_tree_val(a) < __mod_tree_val(b);
149}
150
151static __always_inline int
152mod_tree_comp(void *key, struct latch_tree_node *n)
153{
154 unsigned long val = (unsigned long)key;
155 unsigned long start, end;
156
157 start = __mod_tree_val(n);
158 if (val < start)
159 return -1;
160
161 end = start + __mod_tree_size(n);
162 if (val >= end)
163 return 1;
164
165 return 0;
166}
167
168static const struct latch_tree_ops mod_tree_ops = {
169 .less = mod_tree_less,
170 .comp = mod_tree_comp,
171};
172
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930173static struct mod_tree_root {
174 struct latch_tree_root root;
175 unsigned long addr_min;
176 unsigned long addr_max;
177} mod_tree __cacheline_aligned = {
178 .addr_min = -1UL,
179};
180
181#define module_addr_min mod_tree.addr_min
182#define module_addr_max mod_tree.addr_max
183
184static noinline void __mod_tree_insert(struct mod_tree_node *node)
185{
186 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
187}
188
189static void __mod_tree_remove(struct mod_tree_node *node)
190{
191 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
192}
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930193
194/*
195 * These modifications: insert, remove_init and remove; are serialized by the
196 * module_mutex.
197 */
198static void mod_tree_insert(struct module *mod)
199{
200 mod->mtn_core.mod = mod;
201 mod->mtn_init.mod = mod;
202
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930203 __mod_tree_insert(&mod->mtn_core);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930204 if (mod->init_size)
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930205 __mod_tree_insert(&mod->mtn_init);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930206}
207
208static void mod_tree_remove_init(struct module *mod)
209{
210 if (mod->init_size)
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930211 __mod_tree_remove(&mod->mtn_init);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930212}
213
214static void mod_tree_remove(struct module *mod)
215{
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930216 __mod_tree_remove(&mod->mtn_core);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930217 mod_tree_remove_init(mod);
218}
219
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930220static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930221{
222 struct latch_tree_node *ltn;
223
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930224 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930225 if (!ltn)
226 return NULL;
227
228 return container_of(ltn, struct mod_tree_node, node)->mod;
229}
230
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930231#else /* MODULES_TREE_LOOKUP */
232
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930233static unsigned long module_addr_min = -1UL, module_addr_max = 0;
234
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930235static void mod_tree_insert(struct module *mod) { }
236static void mod_tree_remove_init(struct module *mod) { }
237static void mod_tree_remove(struct module *mod) { }
238
239static struct module *mod_find(unsigned long addr)
240{
241 struct module *mod;
242
243 list_for_each_entry_rcu(mod, &modules, list) {
244 if (within_module(addr, mod))
245 return mod;
246 }
247
248 return NULL;
249}
250
251#endif /* MODULES_TREE_LOOKUP */
252
Peter Zijlstra4f6665462015-05-27 11:09:38 +0930253/*
254 * Bounds of module text, for speeding up __module_address.
255 * Protected by module_mutex.
256 */
257static void __mod_update_bounds(void *base, unsigned int size)
258{
259 unsigned long min = (unsigned long)base;
260 unsigned long max = min + size;
261
262 if (min < module_addr_min)
263 module_addr_min = min;
264 if (max > module_addr_max)
265 module_addr_max = max;
266}
267
268static void mod_update_bounds(struct module *mod)
269{
270 __mod_update_bounds(mod->module_core, mod->core_size);
271 if (mod->init_size)
272 __mod_update_bounds(mod->module_init, mod->init_size);
273}
274
Jason Wessel67fc4e02010-05-20 21:04:21 -0500275#ifdef CONFIG_KGDB_KDB
276struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
277#endif /* CONFIG_KGDB_KDB */
278
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930279static void module_assert_mutex(void)
280{
281 lockdep_assert_held(&module_mutex);
282}
283
284static void module_assert_mutex_or_preempt(void)
285{
286#ifdef CONFIG_LOCKDEP
287 if (unlikely(!debug_locks))
288 return;
289
290 WARN_ON(!rcu_read_lock_sched_held() &&
291 !lockdep_is_held(&module_mutex));
292#endif
293}
294
Rusty Russell106a4ee2012-09-26 10:09:40 +0100295#ifdef CONFIG_MODULE_SIG
296#ifdef CONFIG_MODULE_SIG_FORCE
297static bool sig_enforce = true;
298#else
299static bool sig_enforce = false;
300
Rusty Russell106a4ee2012-09-26 10:09:40 +0100301module_param(sig_enforce, bool_enable_only, 0644);
302#endif /* !CONFIG_MODULE_SIG_FORCE */
303#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Stephen Rothwell19e45292009-04-14 17:27:18 +1000305/* Block module loading/unloading? */
306int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800307core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000308
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500309/* Waiting for a module to finish initializing? */
310static DECLARE_WAIT_QUEUE_HEAD(module_wq);
311
Alan Sterne041c682006-03-27 01:16:30 -0800312static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ionut Alexa6da0b562014-11-10 09:31:29 +1030314int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Alan Sterne041c682006-03-27 01:16:30 -0800316 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318EXPORT_SYMBOL(register_module_notifier);
319
Ionut Alexa6da0b562014-11-10 09:31:29 +1030320int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Alan Sterne041c682006-03-27 01:16:30 -0800322 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324EXPORT_SYMBOL(unregister_module_notifier);
325
Rusty Russelleded41c2010-08-05 12:59:07 -0600326struct load_info {
327 Elf_Ehdr *hdr;
328 unsigned long len;
329 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600330 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600331 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600332 struct _ddebug *debug;
333 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100334 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600335 struct {
336 unsigned int sym, str, mod, vers, info, pcpu;
337 } index;
338};
339
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800340/* We require a truly strong try_module_get(): 0 means failure due to
341 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static inline int strong_try_module_get(struct module *mod)
343{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030344 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500346 return -EBUSY;
347 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500349 else
350 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Rusty Russell373d4d02013-01-21 17:17:39 +1030353static inline void add_taint_module(struct module *mod, unsigned flag,
354 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700355{
Rusty Russell373d4d02013-01-21 17:17:39 +1030356 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700357 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700358}
359
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200360/*
361 * A thread that wants to hold a reference to a module only while it
362 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
364void __module_put_and_exit(struct module *mod, long code)
365{
366 module_put(mod);
367 do_exit(code);
368}
369EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600372static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned int i;
375
Rusty Russell49668682010-08-05 12:59:10 -0600376 for (i = 1; i < info->hdr->e_shnum; i++) {
377 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600379 if ((shdr->sh_flags & SHF_ALLOC)
380 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384}
385
Rusty Russell5e458cc2008-10-22 10:00:13 -0500386/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600387static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500388{
389 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600390 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500391}
392
393/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600394static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500395 const char *name,
396 size_t object_size,
397 unsigned int *num)
398{
Rusty Russell49668682010-08-05 12:59:10 -0600399 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500400
401 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600402 *num = info->sechdrs[sec].sh_size / object_size;
403 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* Provided by the linker */
407extern const struct kernel_symbol __start___ksymtab[];
408extern const struct kernel_symbol __stop___ksymtab[];
409extern const struct kernel_symbol __start___ksymtab_gpl[];
410extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800411extern const struct kernel_symbol __start___ksymtab_gpl_future[];
412extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413extern const unsigned long __start___kcrctab[];
414extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800415extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500416#ifdef CONFIG_UNUSED_SYMBOLS
417extern const struct kernel_symbol __start___ksymtab_unused[];
418extern const struct kernel_symbol __stop___ksymtab_unused[];
419extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
420extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700421extern const unsigned long __start___kcrctab_unused[];
422extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425#ifndef CONFIG_MODVERSIONS
426#define symversion(base, idx) NULL
427#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800428#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#endif
430
Rusty Russelldafd0942008-07-22 19:24:25 -0500431static bool each_symbol_in_section(const struct symsearch *arr,
432 unsigned int arrsize,
433 struct module *owner,
434 bool (*fn)(const struct symsearch *syms,
435 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200436 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500437 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100438{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200439 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500440
441 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200442 if (fn(&arr[j], owner, data))
443 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500444 }
445
446 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100447}
448
Rusty Russelldafd0942008-07-22 19:24:25 -0500449/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200450bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
451 struct module *owner,
452 void *data),
453 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700454{
Rusty Russelldafd0942008-07-22 19:24:25 -0500455 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600456 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500457 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
458 NOT_GPL_ONLY, false },
459 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
460 __start___kcrctab_gpl,
461 GPL_ONLY, false },
462 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
463 __start___kcrctab_gpl_future,
464 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500465#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500466 { __start___ksymtab_unused, __stop___ksymtab_unused,
467 __start___kcrctab_unused,
468 NOT_GPL_ONLY, true },
469 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
470 __start___kcrctab_unused_gpl,
471 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500472#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500473 };
474
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930475 module_assert_mutex_or_preempt();
476
Rusty Russelldafd0942008-07-22 19:24:25 -0500477 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
478 return true;
479
Andi Kleend72b3752008-08-30 10:09:00 +0200480 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500481 struct symsearch arr[] = {
482 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
483 NOT_GPL_ONLY, false },
484 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
485 mod->gpl_crcs,
486 GPL_ONLY, false },
487 { mod->gpl_future_syms,
488 mod->gpl_future_syms + mod->num_gpl_future_syms,
489 mod->gpl_future_crcs,
490 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500491#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500492 { mod->unused_syms,
493 mod->unused_syms + mod->num_unused_syms,
494 mod->unused_crcs,
495 NOT_GPL_ONLY, true },
496 { mod->unused_gpl_syms,
497 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
498 mod->unused_gpl_crcs,
499 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500500#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500501 };
502
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030503 if (mod->state == MODULE_STATE_UNFORMED)
504 continue;
505
Rusty Russelldafd0942008-07-22 19:24:25 -0500506 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
507 return true;
508 }
509 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700510}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200511EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700512
Rusty Russelldafd0942008-07-22 19:24:25 -0500513struct find_symbol_arg {
514 /* Input */
515 const char *name;
516 bool gplok;
517 bool warn;
518
519 /* Output */
520 struct module *owner;
521 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500522 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500523};
524
Rusty Russellde4d8d52011-04-19 21:49:58 +0200525static bool check_symbol(const struct symsearch *syms,
526 struct module *owner,
527 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500528{
Rusty Russelldafd0942008-07-22 19:24:25 -0500529 struct find_symbol_arg *fsa = data;
530
Rusty Russelldafd0942008-07-22 19:24:25 -0500531 if (!fsa->gplok) {
532 if (syms->licence == GPL_ONLY)
533 return false;
534 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800535 pr_warn("Symbol %s is being used by a non-GPL module, "
536 "which will not be allowed in the future\n",
537 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500538 }
539 }
540
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500541#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500542 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800543 pr_warn("Symbol %s is marked as UNUSED, however this module is "
544 "using it.\n", fsa->name);
545 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030546 pr_warn("Please evaluate if this is the right api to use and "
547 "if it really is, submit a report to the linux kernel "
548 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800549 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500550 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500551#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500552
553 fsa->owner = owner;
554 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500555 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500556 return true;
557}
558
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200559static int cmp_name(const void *va, const void *vb)
560{
561 const char *a;
562 const struct kernel_symbol *b;
563 a = va; b = vb;
564 return strcmp(a, b->name);
565}
566
Rusty Russellde4d8d52011-04-19 21:49:58 +0200567static bool find_symbol_in_section(const struct symsearch *syms,
568 struct module *owner,
569 void *data)
570{
571 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200572 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200573
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200574 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
575 sizeof(struct kernel_symbol), cmp_name);
576
577 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
578 return true;
579
Rusty Russellde4d8d52011-04-19 21:49:58 +0200580 return false;
581}
582
Tim Abbott414fd312008-12-05 19:03:56 -0500583/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600584 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500585const struct kernel_symbol *find_symbol(const char *name,
586 struct module **owner,
587 const unsigned long **crc,
588 bool gplok,
589 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Rusty Russelldafd0942008-07-22 19:24:25 -0500591 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Rusty Russelldafd0942008-07-22 19:24:25 -0500593 fsa.name = name;
594 fsa.gplok = gplok;
595 fsa.warn = warn;
596
Rusty Russellde4d8d52011-04-19 21:49:58 +0200597 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500598 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500599 *owner = fsa.owner;
600 if (crc)
601 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500602 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500604
Jim Cromie5e124162011-12-06 12:11:31 -0700605 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500606 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
Tim Abbottc6b37802008-12-05 19:03:59 -0500608EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/* Search for module by name: must hold module_mutex. */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930611static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030612 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct module *mod;
615
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930616 module_assert_mutex();
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030619 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
620 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930621 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return mod;
623 }
624 return NULL;
625}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030626
627struct module *find_module(const char *name)
628{
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930629 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030630}
Tim Abbottc6b37802008-12-05 19:03:59 -0500631EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900634
Tejun Heo259354d2010-03-10 18:56:10 +0900635static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900636{
Tejun Heo259354d2010-03-10 18:56:10 +0900637 return mod->percpu;
638}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900639
Rusty Russell9eb76d72013-07-03 10:06:29 +0930640static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900641{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930642 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
643 unsigned long align = pcpusec->sh_addralign;
644
645 if (!pcpusec->sh_size)
646 return 0;
647
Tejun Heofbf59bc2009-02-20 16:29:08 +0900648 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800649 pr_warn("%s: per-cpu alignment %li > %li\n",
650 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900651 align = PAGE_SIZE;
652 }
653
Rusty Russell9eb76d72013-07-03 10:06:29 +0930654 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900655 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800656 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
657 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900658 return -ENOMEM;
659 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930660 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900661 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900662}
663
Tejun Heo259354d2010-03-10 18:56:10 +0900664static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900665{
Tejun Heo259354d2010-03-10 18:56:10 +0900666 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900667}
668
Rusty Russell49668682010-08-05 12:59:10 -0600669static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900670{
Rusty Russell49668682010-08-05 12:59:10 -0600671 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900672}
673
Tejun Heo259354d2010-03-10 18:56:10 +0900674static void percpu_modcopy(struct module *mod,
675 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900676{
677 int cpu;
678
679 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900680 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900681}
682
Tejun Heo10fad5e2010-03-10 18:57:54 +0900683/**
684 * is_module_percpu_address - test whether address is from module static percpu
685 * @addr: address to test
686 *
687 * Test whether @addr belongs to module static percpu area.
688 *
689 * RETURNS:
690 * %true if @addr is from module static percpu area
691 */
692bool is_module_percpu_address(unsigned long addr)
693{
694 struct module *mod;
695 unsigned int cpu;
696
697 preempt_disable();
698
699 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030700 if (mod->state == MODULE_STATE_UNFORMED)
701 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900702 if (!mod->percpu_size)
703 continue;
704 for_each_possible_cpu(cpu) {
705 void *start = per_cpu_ptr(mod->percpu, cpu);
706
707 if ((void *)addr >= start &&
708 (void *)addr < start + mod->percpu_size) {
709 preempt_enable();
710 return true;
711 }
712 }
713 }
714
715 preempt_enable();
716 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700717}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900720
Tejun Heo259354d2010-03-10 18:56:10 +0900721static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 return NULL;
724}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930725static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930727 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
728 if (info->sechdrs[info->index.pcpu].sh_size != 0)
729 return -ENOMEM;
730 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900731}
732static inline void percpu_modfree(struct module *mod)
733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Rusty Russell49668682010-08-05 12:59:10 -0600735static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 return 0;
738}
Tejun Heo259354d2010-03-10 18:56:10 +0900739static inline void percpu_modcopy(struct module *mod,
740 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 /* pcpusec should be 0, and size of that section should be 0. */
743 BUG_ON(size != 0);
744}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900745bool is_module_percpu_address(unsigned long addr)
746{
747 return false;
748}
Tejun Heo6b588c12009-02-20 16:29:07 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#endif /* CONFIG_SMP */
751
Matt Domschc988d2b2005-06-23 22:05:15 -0700752#define MODINFO_ATTR(field) \
753static void setup_modinfo_##field(struct module *mod, const char *s) \
754{ \
755 mod->field = kstrdup(s, GFP_KERNEL); \
756} \
757static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930758 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700759{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930760 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700761} \
762static int modinfo_##field##_exists(struct module *mod) \
763{ \
764 return mod->field != NULL; \
765} \
766static void free_modinfo_##field(struct module *mod) \
767{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700768 kfree(mod->field); \
769 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700770} \
771static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900772 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700773 .show = show_modinfo_##field, \
774 .setup = setup_modinfo_##field, \
775 .test = modinfo_##field##_exists, \
776 .free = free_modinfo_##field, \
777};
778
779MODINFO_ATTR(version);
780MODINFO_ATTR(srcversion);
781
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100782static char last_unloaded_module[MODULE_NAME_LEN+1];
783
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800784#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400785
786EXPORT_TRACEPOINT_SYMBOL(module_get);
787
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030788/* MODULE_REF_BASE is the base reference count by kmodule loader. */
789#define MODULE_REF_BASE 1
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600792static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030794 /*
795 * Initialize reference counter to MODULE_REF_BASE.
796 * refcnt == 0 means module is going.
797 */
798 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600799
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700800 INIT_LIST_HEAD(&mod->source_list);
801 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030804 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600805
806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/* Does a already use b? */
810static int already_uses(struct module *a, struct module *b)
811{
812 struct module_use *use;
813
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700814 list_for_each_entry(use, &b->source_list, source_list) {
815 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700816 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 1;
818 }
819 }
Jim Cromie5e124162011-12-06 12:11:31 -0700820 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700824/*
825 * Module a uses b
826 * - we add 'a' as a "source", 'b' as a "target" of module use
827 * - the module_use is added to the list of 'b' sources (so
828 * 'b' can walk the list to see who sourced them), and of 'a'
829 * targets (so 'a' can see what modules it targets).
830 */
831static int add_module_usage(struct module *a, struct module *b)
832{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700833 struct module_use *use;
834
Jim Cromie5e124162011-12-06 12:11:31 -0700835 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700836 use = kmalloc(sizeof(*use), GFP_ATOMIC);
837 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800838 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700839 return -ENOMEM;
840 }
841
842 use->source = a;
843 use->target = b;
844 list_add(&use->source_list, &b->source_list);
845 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700846 return 0;
847}
848
Rusty Russell75676502010-06-05 11:17:36 -0600849/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600850int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600852 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100853
Rusty Russell9bea7f22010-06-05 11:17:37 -0600854 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700855 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700856
Rusty Russell9bea7f22010-06-05 11:17:37 -0600857 /* If module isn't available, we fail. */
858 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500859 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700862 err = add_module_usage(a, b);
863 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600865 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600869EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/* Clear the unload stuff of the module. */
872static void module_unload_free(struct module *mod)
873{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700874 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rusty Russell75676502010-06-05 11:17:36 -0600876 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700877 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
878 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700879 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700880 module_put(i);
881 list_del(&use->source_list);
882 list_del(&use->target_list);
883 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Rusty Russell75676502010-06-05 11:17:36 -0600885 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800889static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int ret = (flags & O_TRUNC);
892 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030893 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return ret;
895}
896#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800897static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 return 0;
900}
901#endif /* CONFIG_MODULE_FORCE_UNLOAD */
902
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030903/* Try to release refcount of module, 0 means success. */
904static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030906 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030908 /* Try to decrement refcnt which we set at loading */
909 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
910 BUG_ON(ret < 0);
911 if (ret)
912 /* Someone can put this right now, recover with checking */
913 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030915 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static int try_stop_module(struct module *mod, int flags, int *forced)
919{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030920 /* If it's not unused, quit unless we're forcing. */
921 if (try_release_module_ref(mod) != 0) {
922 *forced = try_force_unload(flags);
923 if (!(*forced))
924 return -EWOULDBLOCK;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030927 /* Mark it as dying. */
928 mod->state = MODULE_STATE_GOING;
929
930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Rusty Russelld5db1392015-01-22 11:13:14 +1030933/**
934 * module_refcount - return the refcount or -1 if unloading
935 *
936 * @mod: the module we're checking
937 *
938 * Returns:
939 * -1 if the module is in the process of unloading
940 * otherwise the number of references in the kernel to the module
941 */
942int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Rusty Russelld5db1392015-01-22 11:13:14 +1030944 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946EXPORT_SYMBOL(module_refcount);
947
948/* This exists whether we can unload or not */
949static void free_module(struct module *mod);
950
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100951SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
952 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800955 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int ret, forced = 0;
957
Kees Cook3d433212009-04-02 15:49:29 -0700958 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800959 return -EPERM;
960
961 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
962 return -EFAULT;
963 name[MODULE_NAME_LEN-1] = '\0';
964
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200965 if (mutex_lock_interruptible(&module_mutex) != 0)
966 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 mod = find_module(name);
969 if (!mod) {
970 ret = -ENOENT;
971 goto out;
972 }
973
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700974 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* Other modules depend on us: get rid of them first. */
976 ret = -EWOULDBLOCK;
977 goto out;
978 }
979
980 /* Doing init or already dying? */
981 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930982 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700983 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ret = -EBUSY;
985 goto out;
986 }
987
988 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700989 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800990 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!forced) {
992 /* This module can't be removed */
993 ret = -EBUSY;
994 goto out;
995 }
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Stop the machine so refcounts can't move and disable module. */
999 ret = try_stop_module(mod, flags, &forced);
1000 if (ret != 0)
1001 goto out;
1002
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001003 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001004 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001005 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02001007 blocking_notifier_call_chain(&module_notify_list,
1008 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001009 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001010
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001011 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001012 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Rusty Russell75676502010-06-05 11:17:36 -06001014 free_module(mod);
1015 return 0;
1016out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001017 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return ret;
1019}
1020
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001021static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 struct module_use *use;
1024 int printed_something = 0;
1025
Rusty Russelld5db1392015-01-22 11:13:14 +10301026 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Ionut Alexa6da0b562014-11-10 09:31:29 +10301028 /*
1029 * Always include a trailing , so userspace can differentiate
1030 * between this and the old multi-field proc format.
1031 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001032 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001034 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (mod->init != NULL && mod->exit == NULL) {
1038 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301039 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
1042 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301043 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046void __symbol_put(const char *symbol)
1047{
1048 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Rusty Russell24da1cb2007-07-15 23:41:46 -07001050 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001051 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 BUG();
1053 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001054 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056EXPORT_SYMBOL(__symbol_put);
1057
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301058/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059void symbol_put_addr(void *addr)
1060{
Trent Piepho5e376612006-05-15 09:44:06 -07001061 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301062 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301064 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001065 return;
1066
Rusty Russella6e6abd2009-03-31 13:05:31 -06001067 /* module_text_address is safe here: we're supposed to have reference
1068 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301069 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001070 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001071 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073EXPORT_SYMBOL_GPL(symbol_put_addr);
1074
1075static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301076 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Rusty Russelld5db1392015-01-22 11:13:14 +10301078 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Kay Sieverscca3e702012-01-13 09:32:15 +10301081static struct module_attribute modinfo_refcnt =
1082 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Steven Rostedtd53799b2012-03-26 12:50:52 +10301084void __module_get(struct module *module)
1085{
1086 if (module) {
1087 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301088 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301089 trace_module_get(module, _RET_IP_);
1090 preempt_enable();
1091 }
1092}
1093EXPORT_SYMBOL(__module_get);
1094
1095bool try_module_get(struct module *module)
1096{
1097 bool ret = true;
1098
1099 if (module) {
1100 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301101 /* Note: here, we can fail to get a reference */
1102 if (likely(module_is_live(module) &&
1103 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301104 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301105 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301106 ret = false;
1107
1108 preempt_enable();
1109 }
1110 return ret;
1111}
1112EXPORT_SYMBOL(try_module_get);
1113
Al Virof6a57032006-10-18 01:47:25 -04001114void module_put(struct module *module)
1115{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301116 int ret;
1117
Al Virof6a57032006-10-18 01:47:25 -04001118 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001119 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301120 ret = atomic_dec_if_positive(&module->refcnt);
1121 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001122 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001123 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001124 }
1125}
1126EXPORT_SYMBOL(module_put);
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001129static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301132 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static inline void module_unload_free(struct module *mod)
1136{
1137}
1138
Rusty Russell9bea7f22010-06-05 11:17:37 -06001139int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001141 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001143EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001145static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149#endif /* CONFIG_MODULE_UNLOAD */
1150
Kevin Winchester53999bf2012-01-15 19:32:55 -04001151static size_t module_flags_taint(struct module *mod, char *buf)
1152{
1153 size_t l = 0;
1154
1155 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1156 buf[l++] = 'P';
1157 if (mod->taints & (1 << TAINT_OOT_MODULE))
1158 buf[l++] = 'O';
1159 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1160 buf[l++] = 'F';
1161 if (mod->taints & (1 << TAINT_CRAP))
1162 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301163 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301164 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001165 /*
1166 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001167 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001168 * apply to modules.
1169 */
1170 return l;
1171}
1172
Kay Sievers1f717402006-11-24 12:15:25 +01001173static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301174 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001175{
1176 const char *state = "unknown";
1177
Kay Sievers4befb022011-07-24 22:06:04 +09301178 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001179 case MODULE_STATE_LIVE:
1180 state = "live";
1181 break;
1182 case MODULE_STATE_COMING:
1183 state = "coming";
1184 break;
1185 case MODULE_STATE_GOING:
1186 state = "going";
1187 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301188 default:
1189 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001190 }
1191 return sprintf(buffer, "%s\n", state);
1192}
1193
Kay Sieverscca3e702012-01-13 09:32:15 +10301194static struct module_attribute modinfo_initstate =
1195 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001196
Kay Sievers88bfa3242011-07-24 22:06:04 +09301197static ssize_t store_uevent(struct module_attribute *mattr,
1198 struct module_kobject *mk,
1199 const char *buffer, size_t count)
1200{
1201 enum kobject_action action;
1202
1203 if (kobject_action_type(buffer, count, &action) == 0)
1204 kobject_uevent(&mk->kobj, action);
1205 return count;
1206}
1207
Kay Sieverscca3e702012-01-13 09:32:15 +10301208struct module_attribute module_uevent =
1209 __ATTR(uevent, 0200, NULL, store_uevent);
1210
1211static ssize_t show_coresize(struct module_attribute *mattr,
1212 struct module_kobject *mk, char *buffer)
1213{
1214 return sprintf(buffer, "%u\n", mk->mod->core_size);
1215}
1216
1217static struct module_attribute modinfo_coresize =
1218 __ATTR(coresize, 0444, show_coresize, NULL);
1219
1220static ssize_t show_initsize(struct module_attribute *mattr,
1221 struct module_kobject *mk, char *buffer)
1222{
1223 return sprintf(buffer, "%u\n", mk->mod->init_size);
1224}
1225
1226static struct module_attribute modinfo_initsize =
1227 __ATTR(initsize, 0444, show_initsize, NULL);
1228
1229static ssize_t show_taint(struct module_attribute *mattr,
1230 struct module_kobject *mk, char *buffer)
1231{
1232 size_t l;
1233
1234 l = module_flags_taint(mk->mod, buffer);
1235 buffer[l++] = '\n';
1236 return l;
1237}
1238
1239static struct module_attribute modinfo_taint =
1240 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa3242011-07-24 22:06:04 +09301241
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001242static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301243 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001244 &modinfo_version,
1245 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301246 &modinfo_initstate,
1247 &modinfo_coresize,
1248 &modinfo_initsize,
1249 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001250#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301251 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001252#endif
1253 NULL,
1254};
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256static const char vermagic[] = VERMAGIC_STRING;
1257
Rusty Russellc6e665c2009-03-31 13:05:33 -06001258static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001259{
1260#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001261 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001262 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301263 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001264 return 0;
1265#else
1266 return -ENOEXEC;
1267#endif
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001271/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1272static unsigned long maybe_relocated(unsigned long crc,
1273 const struct module *crc_owner)
1274{
1275#ifdef ARCH_RELOCATES_KCRCTAB
1276 if (crc_owner == NULL)
1277 return crc - (unsigned long)reloc_start;
1278#endif
1279 return crc;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282static int check_version(Elf_Shdr *sechdrs,
1283 unsigned int versindex,
1284 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301285 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001286 const unsigned long *crc,
1287 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 unsigned int i, num_versions;
1290 struct modversion_info *versions;
1291
1292 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1293 if (!crc)
1294 return 1;
1295
Rusty Russella5dd6972008-05-09 16:24:21 +10001296 /* No versions at all? modprobe --force does this. */
1297 if (versindex == 0)
1298 return try_to_force_load(mod, symname) == 0;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 versions = (void *) sechdrs[versindex].sh_addr;
1301 num_versions = sechdrs[versindex].sh_size
1302 / sizeof(struct modversion_info);
1303
1304 for (i = 0; i < num_versions; i++) {
1305 if (strcmp(versions[i].name, symname) != 0)
1306 continue;
1307
Rusty Russelld4703ae2009-12-15 16:28:32 -06001308 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001310 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001311 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001312 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001314
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001315 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001316 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001317
1318bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301319 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001320 mod->name, symname);
1321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1325 unsigned int versindex,
1326 struct module *mod)
1327{
1328 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301330 /*
1331 * Since this should be found in kernel (which can't be removed), no
1332 * locking is necessary -- use preempt_disable() to placate lockdep.
1333 */
1334 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301335 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301336 &crc, true, false)) {
1337 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301339 }
1340 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301341 return check_version(sechdrs, versindex,
1342 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001343 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Rusty Russell91e37a72008-05-09 16:25:28 +10001346/* First part is kernel version, which we ignore if module has crcs. */
1347static inline int same_magic(const char *amagic, const char *bmagic,
1348 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Rusty Russell91e37a72008-05-09 16:25:28 +10001350 if (has_crcs) {
1351 amagic += strcspn(amagic, " ");
1352 bmagic += strcspn(bmagic, " ");
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return strcmp(amagic, bmagic) == 0;
1355}
1356#else
1357static inline int check_version(Elf_Shdr *sechdrs,
1358 unsigned int versindex,
1359 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301360 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001361 const unsigned long *crc,
1362 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 return 1;
1365}
1366
1367static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1368 unsigned int versindex,
1369 struct module *mod)
1370{
1371 return 1;
1372}
1373
Rusty Russell91e37a72008-05-09 16:25:28 +10001374static inline int same_magic(const char *amagic, const char *bmagic,
1375 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return strcmp(amagic, bmagic) == 0;
1378}
1379#endif /* CONFIG_MODVERSIONS */
1380
Rusty Russell75676502010-06-05 11:17:36 -06001381/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001382static const struct kernel_symbol *resolve_symbol(struct module *mod,
1383 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001384 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001385 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001388 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001390 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301392 /*
1393 * The module_mutex should not be a heavily contended lock;
1394 * if we get the occasional sleep here, we'll go an extra iteration
1395 * in the wait_event_interruptible(), which is harmless.
1396 */
1397 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001398 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001399 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001400 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001401 if (!sym)
1402 goto unlock;
1403
Rusty Russell49668682010-08-05 12:59:10 -06001404 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1405 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001406 sym = ERR_PTR(-EINVAL);
1407 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001409
1410 err = ref_module(mod, owner);
1411 if (err) {
1412 sym = ERR_PTR(err);
1413 goto getname;
1414 }
1415
1416getname:
1417 /* We must make copy under the lock if we failed to get ref. */
1418 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1419unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001420 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001421 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Rusty Russell49668682010-08-05 12:59:10 -06001424static const struct kernel_symbol *
1425resolve_symbol_wait(struct module *mod,
1426 const struct load_info *info,
1427 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001428{
1429 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001430 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001431
1432 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001433 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1434 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001435 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001436 pr_warn("%s: gave up waiting for init of module %s.\n",
1437 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001438 }
1439 return ksym;
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
1443 * /sys/module/foo/sections stuff
1444 * J. Corbet <corbet@lwn.net>
1445 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001446#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001447
Rusty Russell8f6d0372010-08-05 12:59:09 -06001448#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001449static inline bool sect_empty(const Elf_Shdr *sect)
1450{
1451 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1452}
1453
Ionut Alexa6da0b562014-11-10 09:31:29 +10301454struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001455 struct module_attribute mattr;
1456 char *name;
1457 unsigned long address;
1458};
1459
Ionut Alexa6da0b562014-11-10 09:31:29 +10301460struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001461 struct attribute_group grp;
1462 unsigned int nsections;
1463 struct module_sect_attr attrs[0];
1464};
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301467 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct module_sect_attr *sattr =
1470 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001471 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001474static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1475{
Rusty Russella58730c2008-03-13 09:03:44 +00001476 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001477
1478 for (section = 0; section < sect_attrs->nsections; section++)
1479 kfree(sect_attrs->attrs[section].name);
1480 kfree(sect_attrs);
1481}
1482
Rusty Russell8f6d0372010-08-05 12:59:09 -06001483static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 unsigned int nloaded = 0, i, size[2];
1486 struct module_sect_attrs *sect_attrs;
1487 struct module_sect_attr *sattr;
1488 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001491 for (i = 0; i < info->hdr->e_shnum; i++)
1492 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 nloaded++;
1494 size[0] = ALIGN(sizeof(*sect_attrs)
1495 + nloaded * sizeof(sect_attrs->attrs[0]),
1496 sizeof(sect_attrs->grp.attrs[0]));
1497 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001498 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1499 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return;
1501
1502 /* Setup section attributes. */
1503 sect_attrs->grp.name = "sections";
1504 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1505
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001506 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sattr = &sect_attrs->attrs[0];
1508 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001509 for (i = 0; i < info->hdr->e_shnum; i++) {
1510 Elf_Shdr *sec = &info->sechdrs[i];
1511 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001512 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001513 sattr->address = sec->sh_addr;
1514 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001515 GFP_KERNEL);
1516 if (sattr->name == NULL)
1517 goto out;
1518 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001519 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sattr->mattr.show = module_sect_show;
1521 sattr->mattr.store = NULL;
1522 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 sattr->mattr.attr.mode = S_IRUGO;
1524 *(gattr++) = &(sattr++)->mattr.attr;
1525 }
1526 *gattr = NULL;
1527
1528 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1529 goto out;
1530
1531 mod->sect_attrs = sect_attrs;
1532 return;
1533 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001534 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537static void remove_sect_attrs(struct module *mod)
1538{
1539 if (mod->sect_attrs) {
1540 sysfs_remove_group(&mod->mkobj.kobj,
1541 &mod->sect_attrs->grp);
1542 /* We are positive that no one is using any sect attrs
1543 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001544 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mod->sect_attrs = NULL;
1546 }
1547}
1548
Roland McGrath6d760132007-10-16 23:26:40 -07001549/*
1550 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1551 */
1552
1553struct module_notes_attrs {
1554 struct kobject *dir;
1555 unsigned int notes;
1556 struct bin_attribute attrs[0];
1557};
1558
Chris Wright2c3c8be2010-05-12 18:28:57 -07001559static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001560 struct bin_attribute *bin_attr,
1561 char *buf, loff_t pos, size_t count)
1562{
1563 /*
1564 * The caller checked the pos and count against our size.
1565 */
1566 memcpy(buf, bin_attr->private + pos, count);
1567 return count;
1568}
1569
1570static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1571 unsigned int i)
1572{
1573 if (notes_attrs->dir) {
1574 while (i-- > 0)
1575 sysfs_remove_bin_file(notes_attrs->dir,
1576 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001577 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001578 }
1579 kfree(notes_attrs);
1580}
1581
Rusty Russell8f6d0372010-08-05 12:59:09 -06001582static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001583{
1584 unsigned int notes, loaded, i;
1585 struct module_notes_attrs *notes_attrs;
1586 struct bin_attribute *nattr;
1587
Ingo Molnarea6bff32009-08-28 10:44:56 +02001588 /* failed to create section attributes, so can't create notes */
1589 if (!mod->sect_attrs)
1590 return;
1591
Roland McGrath6d760132007-10-16 23:26:40 -07001592 /* Count notes sections and allocate structures. */
1593 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001594 for (i = 0; i < info->hdr->e_shnum; i++)
1595 if (!sect_empty(&info->sechdrs[i]) &&
1596 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001597 ++notes;
1598
1599 if (notes == 0)
1600 return;
1601
1602 notes_attrs = kzalloc(sizeof(*notes_attrs)
1603 + notes * sizeof(notes_attrs->attrs[0]),
1604 GFP_KERNEL);
1605 if (notes_attrs == NULL)
1606 return;
1607
1608 notes_attrs->notes = notes;
1609 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001610 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1611 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001612 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001613 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001614 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001615 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1616 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001617 nattr->size = info->sechdrs[i].sh_size;
1618 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001619 nattr->read = module_notes_read;
1620 ++nattr;
1621 }
1622 ++loaded;
1623 }
1624
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001625 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001626 if (!notes_attrs->dir)
1627 goto out;
1628
1629 for (i = 0; i < notes; ++i)
1630 if (sysfs_create_bin_file(notes_attrs->dir,
1631 &notes_attrs->attrs[i]))
1632 goto out;
1633
1634 mod->notes_attrs = notes_attrs;
1635 return;
1636
1637 out:
1638 free_notes_attrs(notes_attrs, i);
1639}
1640
1641static void remove_notes_attrs(struct module *mod)
1642{
1643 if (mod->notes_attrs)
1644 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001648
Rusty Russell8f6d0372010-08-05 12:59:09 -06001649static inline void add_sect_attrs(struct module *mod,
1650 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652}
1653
1654static inline void remove_sect_attrs(struct module *mod)
1655{
1656}
Roland McGrath6d760132007-10-16 23:26:40 -07001657
Rusty Russell8f6d0372010-08-05 12:59:09 -06001658static inline void add_notes_attrs(struct module *mod,
1659 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001660{
1661}
1662
1663static inline void remove_notes_attrs(struct module *mod)
1664{
1665}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001666#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001668static void add_usage_links(struct module *mod)
1669{
1670#ifdef CONFIG_MODULE_UNLOAD
1671 struct module_use *use;
1672 int nowarn;
1673
Rusty Russell75676502010-06-05 11:17:36 -06001674 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001675 list_for_each_entry(use, &mod->target_list, target_list) {
1676 nowarn = sysfs_create_link(use->target->holders_dir,
1677 &mod->mkobj.kobj, mod->name);
1678 }
Rusty Russell75676502010-06-05 11:17:36 -06001679 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001680#endif
1681}
1682
1683static void del_usage_links(struct module *mod)
1684{
1685#ifdef CONFIG_MODULE_UNLOAD
1686 struct module_use *use;
1687
Rusty Russell75676502010-06-05 11:17:36 -06001688 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001689 list_for_each_entry(use, &mod->target_list, target_list)
1690 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001691 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001692#endif
1693}
1694
Rusty Russell6407ebb22010-06-05 11:17:36 -06001695static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001696{
1697 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001698 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001699 int error = 0;
1700 int i;
1701
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001702 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1703 (ARRAY_SIZE(modinfo_attrs) + 1)),
1704 GFP_KERNEL);
1705 if (!mod->modinfo_attrs)
1706 return -ENOMEM;
1707
1708 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001709 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1710 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001711 (attr->test && attr->test(mod))) {
1712 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001713 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301714 error = sysfs_create_file(&mod->mkobj.kobj,
1715 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001716 ++temp_attr;
1717 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001718 }
1719 return error;
1720}
1721
Rusty Russell6407ebb22010-06-05 11:17:36 -06001722static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001723{
1724 struct module_attribute *attr;
1725 int i;
1726
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001727 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1728 /* pick a field to test for end of list */
1729 if (!attr->attr.name)
1730 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301731 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001732 if (attr->free)
1733 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001734 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001735 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Li Zhong942e4432013-09-03 16:33:57 +09301738static void mod_kobject_put(struct module *mod)
1739{
1740 DECLARE_COMPLETION_ONSTACK(c);
1741 mod->mkobj.kobj_completion = &c;
1742 kobject_put(&mod->mkobj.kobj);
1743 wait_for_completion(&c);
1744}
1745
Rusty Russell6407ebb22010-06-05 11:17:36 -06001746static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001749 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001751 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001752 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001753 err = -EINVAL;
1754 goto out;
1755 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001756
1757 kobj = kset_find_obj(module_kset, mod->name);
1758 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001759 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001760 kobject_put(kobj);
1761 err = -EINVAL;
1762 goto out;
1763 }
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001766
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001767 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1768 mod->mkobj.kobj.kset = module_kset;
1769 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1770 "%s", mod->name);
1771 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301772 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001773
Kay Sievers97c146e2007-11-29 23:46:11 +01001774 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001775out:
1776 return err;
1777}
1778
Rusty Russell6407ebb22010-06-05 11:17:36 -06001779static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001780 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001781 struct kernel_param *kparam,
1782 unsigned int num_params)
1783{
1784 int err;
1785
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001786 err = mod_sysfs_init(mod);
1787 if (err)
1788 goto out;
1789
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001790 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001791 if (!mod->holders_dir) {
1792 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001793 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001794 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 err = module_param_sysfs_setup(mod, kparam, num_params);
1797 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001798 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Matt Domschc988d2b2005-06-23 22:05:15 -07001800 err = module_add_modinfo_attrs(mod);
1801 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001802 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001803
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001804 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001805 add_sect_attrs(mod, info);
1806 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001807
Kay Sieverse17e0f52006-11-24 12:15:25 +01001808 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
1810
Kay Sieverse17e0f52006-11-24 12:15:25 +01001811out_unreg_param:
1812 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001813out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001814 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001815out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301816 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001817out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return err;
1819}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001820
1821static void mod_sysfs_fini(struct module *mod)
1822{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001823 remove_notes_attrs(mod);
1824 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301825 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001826}
1827
Rusty Russell8f6d0372010-08-05 12:59:09 -06001828#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001829
Rusty Russell8f6d0372010-08-05 12:59:09 -06001830static int mod_sysfs_setup(struct module *mod,
1831 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001832 struct kernel_param *kparam,
1833 unsigned int num_params)
1834{
1835 return 0;
1836}
1837
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001838static void mod_sysfs_fini(struct module *mod)
1839{
1840}
1841
Rusty Russell36b03602010-08-05 12:59:09 -06001842static void module_remove_modinfo_attrs(struct module *mod)
1843{
1844}
1845
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001846static void del_usage_links(struct module *mod)
1847{
1848}
1849
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001850#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Rusty Russell36b03602010-08-05 12:59:09 -06001852static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001854 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001855 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001857 kobject_put(mod->mkobj.drivers_dir);
1858 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001859 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
matthieu castet84e1c6b2010-11-16 22:35:16 +01001862#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1863/*
1864 * LKM RO/NX protection: protect module's text/ro-data
1865 * from modification and any data from execution.
1866 */
1867void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1868{
1869 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1870 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1871
1872 if (end_pfn > begin_pfn)
1873 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1874}
1875
1876static void set_section_ro_nx(void *base,
1877 unsigned long text_size,
1878 unsigned long ro_size,
1879 unsigned long total_size)
1880{
1881 /* begin and end PFNs of the current subsection */
1882 unsigned long begin_pfn;
1883 unsigned long end_pfn;
1884
1885 /*
1886 * Set RO for module text and RO-data:
1887 * - Always protect first page.
1888 * - Do not protect last partial page.
1889 */
1890 if (ro_size > 0)
1891 set_page_attributes(base, base + ro_size, set_memory_ro);
1892
1893 /*
1894 * Set NX permissions for module data:
1895 * - Do not protect first partial page.
1896 * - Always protect last page.
1897 */
1898 if (total_size > text_size) {
1899 begin_pfn = PFN_UP((unsigned long)base + text_size);
1900 end_pfn = PFN_UP((unsigned long)base + total_size);
1901 if (end_pfn > begin_pfn)
1902 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1903 }
1904}
1905
Jan Glauber01526ed2011-05-19 16:55:26 -06001906static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001907{
Jan Glauber01526ed2011-05-19 16:55:26 -06001908 set_page_attributes(mod->module_core + mod->core_text_size,
1909 mod->module_core + mod->core_size,
1910 set_memory_x);
1911 set_page_attributes(mod->module_core,
1912 mod->module_core + mod->core_ro_size,
1913 set_memory_rw);
1914}
1915
1916static void unset_module_init_ro_nx(struct module *mod)
1917{
1918 set_page_attributes(mod->module_init + mod->init_text_size,
1919 mod->module_init + mod->init_size,
1920 set_memory_x);
1921 set_page_attributes(mod->module_init,
1922 mod->module_init + mod->init_ro_size,
1923 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001924}
1925
1926/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001927void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001928{
1929 struct module *mod;
1930
1931 mutex_lock(&module_mutex);
1932 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301933 if (mod->state == MODULE_STATE_UNFORMED)
1934 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001935 if ((mod->module_core) && (mod->core_text_size)) {
1936 set_page_attributes(mod->module_core,
1937 mod->module_core + mod->core_text_size,
1938 set_memory_rw);
1939 }
1940 if ((mod->module_init) && (mod->init_text_size)) {
1941 set_page_attributes(mod->module_init,
1942 mod->module_init + mod->init_text_size,
1943 set_memory_rw);
1944 }
1945 }
1946 mutex_unlock(&module_mutex);
1947}
1948
1949/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001950void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001951{
1952 struct module *mod;
1953
1954 mutex_lock(&module_mutex);
1955 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301956 if (mod->state == MODULE_STATE_UNFORMED)
1957 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001958 if ((mod->module_core) && (mod->core_text_size)) {
1959 set_page_attributes(mod->module_core,
1960 mod->module_core + mod->core_text_size,
1961 set_memory_ro);
1962 }
1963 if ((mod->module_init) && (mod->init_text_size)) {
1964 set_page_attributes(mod->module_init,
1965 mod->module_init + mod->init_text_size,
1966 set_memory_ro);
1967 }
1968 }
1969 mutex_unlock(&module_mutex);
1970}
1971#else
1972static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
Jan Glauber01526ed2011-05-19 16:55:26 -06001973static void unset_module_core_ro_nx(struct module *mod) { }
1974static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001975#endif
1976
Rusty Russellbe1f2212015-01-20 09:07:05 +10301977void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001978{
1979 vfree(module_region);
1980}
1981
1982void __weak module_arch_cleanup(struct module *mod)
1983{
1984}
1985
Rusty Russelld453cde2015-01-20 09:07:04 +10301986void __weak module_arch_freeing_init(struct module *mod)
1987{
1988}
1989
Rusty Russell75676502010-06-05 11:17:36 -06001990/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991static void free_module(struct module *mod)
1992{
Li Zefan7ead8b82009-08-17 16:56:28 +08001993 trace_module_free(mod);
1994
Rusty Russell36b03602010-08-05 12:59:09 -06001995 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Rusty Russell944a1fa2013-04-17 13:20:03 +09301997 /* We leave it in list to prevent duplicate loads, but make sure
1998 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301999 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302000 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10302001 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09302002
Jason Baronb82bab4b2010-07-27 13:18:01 -07002003 /* Remove dynamic debug info */
2004 ddebug_remove_module(mod->name);
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* Arch-specific cleanup. */
2007 module_arch_cleanup(mod);
2008
2009 /* Module unload stuff */
2010 module_unload_free(mod);
2011
Rusty Russelle180a6b2009-03-31 13:05:29 -06002012 /* Free any allocated parameters. */
2013 destroy_params(mod->kp, mod->num_kp);
2014
Rusty Russell944a1fa2013-04-17 13:20:03 +09302015 /* Now we can delete it from the lists */
2016 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302017 /* Unlink carefully: kallsyms could be walking list. */
2018 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302019 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302020 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302021 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302022 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2023 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302024 mutex_unlock(&module_mutex);
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06002027 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10302028 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302029 module_memfree(mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002031 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002032
Peter Zijlstra35a93932015-02-26 16:23:11 +01002033 /* Free lock-classes; relies on the preceding sync_rcu(). */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002034 lockdep_free_key_range(mod->module_core, mod->core_size);
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06002037 unset_module_core_ro_nx(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302038 module_memfree(mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002039
2040#ifdef CONFIG_MPU
2041 update_protections(current->mm);
2042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
2045void *__symbol_get(const char *symbol)
2046{
2047 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002048 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Rusty Russell24da1cb2007-07-15 23:41:46 -07002050 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002051 sym = find_symbol(symbol, &owner, NULL, true, true);
2052 if (sym && strong_try_module_get(owner))
2053 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002054 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Tim Abbott414fd312008-12-05 19:03:56 -05002056 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058EXPORT_SYMBOL_GPL(__symbol_get);
2059
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002060/*
2061 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002062 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002063 *
2064 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002065 */
2066static int verify_export_symbols(struct module *mod)
2067{
Rusty Russellb2111042008-05-01 21:15:00 -05002068 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002069 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002070 const struct kernel_symbol *s;
2071 struct {
2072 const struct kernel_symbol *sym;
2073 unsigned int num;
2074 } arr[] = {
2075 { mod->syms, mod->num_syms },
2076 { mod->gpl_syms, mod->num_gpl_syms },
2077 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002078#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002079 { mod->unused_syms, mod->num_unused_syms },
2080 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002081#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002082 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002083
Rusty Russellb2111042008-05-01 21:15:00 -05002084 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2085 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002086 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002087 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002088 " (owned by %s)\n",
2089 mod->name, s->name, module_name(owner));
2090 return -ENOEXEC;
2091 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002092 }
Rusty Russellb2111042008-05-01 21:15:00 -05002093 }
2094 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002095}
2096
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002097/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002098static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Rusty Russell49668682010-08-05 12:59:10 -06002100 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2101 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002103 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002105 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Rusty Russell49668682010-08-05 12:59:10 -06002107 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2108 const char *name = info->strtab + sym[i].st_name;
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 switch (sym[i].st_shndx) {
2111 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002112 /* Ignore common symbols */
2113 if (!strncmp(name, "__gnu_lto", 9))
2114 break;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* We compiled with -fno-common. These are not
2117 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002118 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302119 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 mod->name);
2121 ret = -ENOEXEC;
2122 break;
2123
2124 case SHN_ABS:
2125 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002126 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 (long)sym[i].st_value);
2128 break;
2129
2130 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002131 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002133 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002134 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002136 }
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002139 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 break;
2141
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002142 pr_warn("%s: Unknown symbol %s (err %li)\n",
2143 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002144 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 break;
2146
2147 default:
2148 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002149 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002150 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 else
Rusty Russell49668682010-08-05 12:59:10 -06002152 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 sym[i].st_value += secbase;
2154 break;
2155 }
2156 }
2157
2158 return ret;
2159}
2160
Rusty Russell49668682010-08-05 12:59:10 -06002161static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002162{
2163 unsigned int i;
2164 int err = 0;
2165
2166 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002167 for (i = 1; i < info->hdr->e_shnum; i++) {
2168 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002169
2170 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002171 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002172 continue;
2173
2174 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002175 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002176 continue;
2177
Rusty Russell49668682010-08-05 12:59:10 -06002178 if (info->sechdrs[i].sh_type == SHT_REL)
2179 err = apply_relocate(info->sechdrs, info->strtab,
2180 info->index.sym, i, mod);
2181 else if (info->sechdrs[i].sh_type == SHT_RELA)
2182 err = apply_relocate_add(info->sechdrs, info->strtab,
2183 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002184 if (err < 0)
2185 break;
2186 }
2187 return err;
2188}
2189
Helge Deller088af9a2008-12-31 12:31:18 +01002190/* Additional bytes needed by arch in front of individual sections */
2191unsigned int __weak arch_mod_section_prepend(struct module *mod,
2192 unsigned int section)
2193{
2194 /* default implementation just returns zero */
2195 return 0;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002199static long get_offset(struct module *mod, unsigned int *size,
2200 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 long ret;
2203
Helge Deller088af9a2008-12-31 12:31:18 +01002204 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2206 *size = ret + sechdr->sh_size;
2207 return ret;
2208}
2209
2210/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2211 might -- code, read-only data, read-write data, small data. Tally
2212 sizes, and place the offsets into sh_entsize fields: high bit means it
2213 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002214static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 static unsigned long const masks[][2] = {
2217 /* NOTE: all executable code must be the first section
2218 * in this array; otherwise modify the text_size
2219 * finder in the two loops below */
2220 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2221 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2222 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2223 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2224 };
2225 unsigned int m, i;
2226
Rusty Russell49668682010-08-05 12:59:10 -06002227 for (i = 0; i < info->hdr->e_shnum; i++)
2228 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Jim Cromie5e124162011-12-06 12:11:31 -07002230 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002232 for (i = 0; i < info->hdr->e_shnum; ++i) {
2233 Elf_Shdr *s = &info->sechdrs[i];
2234 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2237 || (s->sh_flags & masks[m][1])
2238 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002239 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002241 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002242 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002244 switch (m) {
2245 case 0: /* executable */
2246 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002248 break;
2249 case 1: /* RO: text and ro-data */
2250 mod->core_size = debug_align(mod->core_size);
2251 mod->core_ro_size = mod->core_size;
2252 break;
2253 case 3: /* whole core */
2254 mod->core_size = debug_align(mod->core_size);
2255 break;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258
Jim Cromie5e124162011-12-06 12:11:31 -07002259 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002261 for (i = 0; i < info->hdr->e_shnum; ++i) {
2262 Elf_Shdr *s = &info->sechdrs[i];
2263 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2266 || (s->sh_flags & masks[m][1])
2267 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002268 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002270 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002272 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002274 switch (m) {
2275 case 0: /* executable */
2276 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002278 break;
2279 case 1: /* RO: text and ro-data */
2280 mod->init_size = debug_align(mod->init_size);
2281 mod->init_ro_size = mod->init_size;
2282 break;
2283 case 3: /* whole init */
2284 mod->init_size = debug_align(mod->init_size);
2285 break;
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290static void set_license(struct module *mod, const char *license)
2291{
2292 if (!license)
2293 license = "unspecified";
2294
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002295 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002296 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002297 pr_warn("%s: module license '%s' taints kernel.\n",
2298 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302299 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2300 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302}
2303
2304/* Parse tag=value strings from .modinfo section */
2305static char *next_string(char *string, unsigned long *secsize)
2306{
2307 /* Skip non-zero chars */
2308 while (string[0]) {
2309 string++;
2310 if ((*secsize)-- <= 1)
2311 return NULL;
2312 }
2313
2314 /* Skip any zero padding. */
2315 while (!string[0]) {
2316 string++;
2317 if ((*secsize)-- <= 1)
2318 return NULL;
2319 }
2320 return string;
2321}
2322
Rusty Russell49668682010-08-05 12:59:10 -06002323static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 char *p;
2326 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002327 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2328 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Rusty Russell49668682010-08-05 12:59:10 -06002330 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2332 return p + taglen + 1;
2333 }
2334 return NULL;
2335}
2336
Rusty Russell49668682010-08-05 12:59:10 -06002337static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002338{
2339 struct module_attribute *attr;
2340 int i;
2341
2342 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2343 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002344 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002345 }
2346}
Matt Domschc988d2b2005-06-23 22:05:15 -07002347
Rusty Russella263f772009-09-25 00:32:58 -06002348static void free_modinfo(struct module *mod)
2349{
2350 struct module_attribute *attr;
2351 int i;
2352
2353 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2354 if (attr->free)
2355 attr->free(mod);
2356 }
2357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002360
2361/* lookup symbol in given range of kernel_symbols */
2362static const struct kernel_symbol *lookup_symbol(const char *name,
2363 const struct kernel_symbol *start,
2364 const struct kernel_symbol *stop)
2365{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002366 return bsearch(name, start, stop - start,
2367 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002368}
2369
Tim Abbottca4787b2009-01-05 08:40:10 -06002370static int is_exported(const char *name, unsigned long value,
2371 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Tim Abbottca4787b2009-01-05 08:40:10 -06002373 const struct kernel_symbol *ks;
2374 if (!mod)
2375 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002376 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002377 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2378 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
2381/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002382static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Rusty Russelleded41c2010-08-05 12:59:07 -06002384 const Elf_Shdr *sechdrs = info->sechdrs;
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2387 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2388 return 'v';
2389 else
2390 return 'w';
2391 }
2392 if (sym->st_shndx == SHN_UNDEF)
2393 return 'U';
2394 if (sym->st_shndx == SHN_ABS)
2395 return 'a';
2396 if (sym->st_shndx >= SHN_LORESERVE)
2397 return '?';
2398 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2399 return 't';
2400 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2401 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2402 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2403 return 'r';
2404 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2405 return 'g';
2406 else
2407 return 'd';
2408 }
2409 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2410 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2411 return 's';
2412 else
2413 return 'b';
2414 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002415 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2416 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return '?';
2420}
2421
Jan Beulich4a496222009-07-06 14:50:42 +01002422static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Ionut Alexa6da0b562014-11-10 09:31:29 +10302423 unsigned int shnum)
Jan Beulich4a496222009-07-06 14:50:42 +01002424{
2425 const Elf_Shdr *sec;
2426
2427 if (src->st_shndx == SHN_UNDEF
2428 || src->st_shndx >= shnum
2429 || !src->st_name)
2430 return false;
2431
2432 sec = sechdrs + src->st_shndx;
2433 if (!(sec->sh_flags & SHF_ALLOC)
2434#ifndef CONFIG_KALLSYMS_ALL
2435 || !(sec->sh_flags & SHF_EXECINSTR)
2436#endif
2437 || (sec->sh_entsize & INIT_OFFSET_MASK))
2438 return false;
2439
2440 return true;
2441}
2442
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302443/*
2444 * We only allocate and copy the strings needed by the parts of symtab
2445 * we keep. This is simple, but has the effect of making multiple
2446 * copies of duplicates. We could be more sophisticated, see
2447 * linux-kernel thread starting with
2448 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2449 */
Rusty Russell49668682010-08-05 12:59:10 -06002450static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002451{
Rusty Russell49668682010-08-05 12:59:10 -06002452 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2453 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002454 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302455 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002456
2457 /* Put symbol section at end of init part of module. */
2458 symsect->sh_flags |= SHF_ALLOC;
2459 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002460 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002461 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002462
Rusty Russell49668682010-08-05 12:59:10 -06002463 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002464 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002465
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302466 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302467 for (ndst = i = 0; i < nsrc; i++) {
2468 if (i == 0 ||
2469 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2470 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302471 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002472 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302473 }
Jan Beulich4a496222009-07-06 14:50:42 +01002474
2475 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002476 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302477 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2478 mod->core_size += strtab_size;
Laura Abbott168e47f2015-02-25 14:14:57 -08002479 mod->core_size = debug_align(mod->core_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002480
Jan Beulich554bdfe2009-07-06 14:51:44 +01002481 /* Put string table section at end of init part of module. */
2482 strsect->sh_flags |= SHF_ALLOC;
2483 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002484 info->index.str) | INIT_OFFSET_MASK;
Laura Abbott168e47f2015-02-25 14:14:57 -08002485 mod->init_size = debug_align(mod->init_size);
Jim Cromie5e124162011-12-06 12:11:31 -07002486 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002487}
2488
Rusty Russell811d66a2010-08-05 12:59:12 -06002489static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Jan Beulich4a496222009-07-06 14:50:42 +01002491 unsigned int i, ndst;
2492 const Elf_Sym *src;
2493 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002494 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002495 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Rusty Russelleded41c2010-08-05 12:59:07 -06002497 mod->symtab = (void *)symsec->sh_addr;
2498 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002499 /* Make sure we get permanent strtab: don't use info->strtab. */
2500 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Set types up while we still have access to sections. */
2503 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002504 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002505
Rusty Russelld9131882010-08-05 12:59:08 -06002506 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302507 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002508 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302509 for (ndst = i = 0; i < mod->num_symtab; i++) {
2510 if (i == 0 ||
2511 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2512 dst[ndst] = src[i];
2513 dst[ndst++].st_name = s - mod->core_strtab;
2514 s += strlcpy(s, &mod->strtab[src[i].st_name],
2515 KSYM_NAME_LEN) + 1;
2516 }
Jan Beulich4a496222009-07-06 14:50:42 +01002517 }
2518 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520#else
Rusty Russell49668682010-08-05 12:59:10 -06002521static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002522{
2523}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002524
Michał Mirosławabbce902010-09-20 01:58:08 +02002525static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527}
2528#endif /* CONFIG_KALLSYMS */
2529
Jason Barone9d376f2009-02-05 11:51:38 -05002530static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002531{
Rusty Russell811d66a2010-08-05 12:59:12 -06002532 if (!debug)
2533 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002534#ifdef CONFIG_DYNAMIC_DEBUG
2535 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002536 pr_err("dynamic debug error adding module: %s\n",
2537 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002538#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002539}
Jason Baron346e15b2008-08-12 16:46:19 -04002540
Yehuda Sadehff49d742010-07-03 13:07:35 +10002541static void dynamic_debug_remove(struct _ddebug *debug)
2542{
2543 if (debug)
2544 ddebug_remove_module(debug->modname);
2545}
2546
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002547void * __weak module_alloc(unsigned long size)
2548{
Rusty Russell82fab442012-12-11 09:38:33 +10302549 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002550}
2551
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002552#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002553static void kmemleak_load_module(const struct module *mod,
2554 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002555{
2556 unsigned int i;
2557
2558 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002559 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002560
Rusty Russell49668682010-08-05 12:59:10 -06002561 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002562 /* Scan all writable sections that's not executable */
2563 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2564 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2565 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002566 continue;
2567
Rusty Russell49668682010-08-05 12:59:10 -06002568 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2569 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002570 }
2571}
2572#else
Rusty Russell49668682010-08-05 12:59:10 -06002573static inline void kmemleak_load_module(const struct module *mod,
2574 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002575{
2576}
2577#endif
2578
Rusty Russell106a4ee2012-09-26 10:09:40 +01002579#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302580static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002581{
2582 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302583 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2584 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002585
Kees Cook34e11692012-10-16 07:31:07 +10302586 if (info->len > markerlen &&
2587 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002588 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302589 info->len -= markerlen;
2590 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002591 }
2592
2593 if (!err) {
2594 info->sig_ok = true;
2595 return 0;
2596 }
2597
2598 /* Not having a signature is only an error if we're strict. */
2599 if (err == -ENOKEY && !sig_enforce)
2600 err = 0;
2601
2602 return err;
2603}
2604#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302605static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002606{
2607 return 0;
2608}
2609#endif /* !CONFIG_MODULE_SIG */
2610
Kees Cook34e11692012-10-16 07:31:07 +10302611/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2612static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002613{
Kees Cook34e11692012-10-16 07:31:07 +10302614 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002615 return -ENOEXEC;
2616
Kees Cook34e11692012-10-16 07:31:07 +10302617 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2618 || info->hdr->e_type != ET_REL
2619 || !elf_check_arch(info->hdr)
2620 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2621 return -ENOEXEC;
2622
2623 if (info->hdr->e_shoff >= info->len
2624 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2625 info->len - info->hdr->e_shoff))
2626 return -ENOEXEC;
2627
2628 return 0;
2629}
2630
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002631#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2632
2633static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2634{
2635 do {
2636 unsigned long n = min(len, COPY_CHUNK_SIZE);
2637
2638 if (copy_from_user(dst, usrc, n) != 0)
2639 return -EFAULT;
2640 cond_resched();
2641 dst += n;
2642 usrc += n;
2643 len -= n;
2644 } while (len);
2645 return 0;
2646}
2647
Kees Cook34e11692012-10-16 07:31:07 +10302648/* Sets info->hdr and info->len. */
2649static int copy_module_from_user(const void __user *umod, unsigned long len,
2650 struct load_info *info)
2651{
Kees Cook2e72d512012-10-16 07:32:07 +10302652 int err;
2653
Kees Cook34e11692012-10-16 07:31:07 +10302654 info->len = len;
2655 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002656 return -ENOEXEC;
2657
Kees Cook2e72d512012-10-16 07:32:07 +10302658 err = security_kernel_module_from_file(NULL);
2659 if (err)
2660 return err;
2661
Rusty Russell40dd2562010-08-05 12:59:03 -06002662 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302663 info->hdr = __vmalloc(info->len,
2664 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302665 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002666 return -ENOMEM;
2667
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002668 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302669 vfree(info->hdr);
2670 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002671 }
2672
Rusty Russell40dd2562010-08-05 12:59:03 -06002673 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302674}
Rusty Russell40dd2562010-08-05 12:59:03 -06002675
Kees Cook34e11692012-10-16 07:31:07 +10302676/* Sets info->hdr and info->len. */
2677static int copy_module_from_fd(int fd, struct load_info *info)
2678{
Al Viroa2e05782013-08-30 12:41:41 -04002679 struct fd f = fdget(fd);
Kees Cook34e11692012-10-16 07:31:07 +10302680 int err;
2681 struct kstat stat;
2682 loff_t pos;
2683 ssize_t bytes = 0;
2684
Al Viroa2e05782013-08-30 12:41:41 -04002685 if (!f.file)
Kees Cook34e11692012-10-16 07:31:07 +10302686 return -ENOEXEC;
2687
Al Viroa2e05782013-08-30 12:41:41 -04002688 err = security_kernel_module_from_file(f.file);
Kees Cook2e72d512012-10-16 07:32:07 +10302689 if (err)
2690 goto out;
2691
Al Viroa2e05782013-08-30 12:41:41 -04002692 err = vfs_getattr(&f.file->f_path, &stat);
Kees Cook34e11692012-10-16 07:31:07 +10302693 if (err)
2694 goto out;
2695
2696 if (stat.size > INT_MAX) {
2697 err = -EFBIG;
2698 goto out;
2699 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302700
2701 /* Don't hand 0 to vmalloc, it whines. */
2702 if (stat.size == 0) {
2703 err = -EINVAL;
2704 goto out;
2705 }
2706
Kees Cook34e11692012-10-16 07:31:07 +10302707 info->hdr = vmalloc(stat.size);
2708 if (!info->hdr) {
2709 err = -ENOMEM;
2710 goto out;
2711 }
2712
2713 pos = 0;
2714 while (pos < stat.size) {
Al Viroa2e05782013-08-30 12:41:41 -04002715 bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
Kees Cook34e11692012-10-16 07:31:07 +10302716 stat.size - pos);
2717 if (bytes < 0) {
2718 vfree(info->hdr);
2719 err = bytes;
2720 goto out;
2721 }
2722 if (bytes == 0)
2723 break;
2724 pos += bytes;
2725 }
2726 info->len = pos;
2727
2728out:
Al Viroa2e05782013-08-30 12:41:41 -04002729 fdput(f);
Rusty Russell40dd2562010-08-05 12:59:03 -06002730 return err;
2731}
2732
Rusty Russelld9131882010-08-05 12:59:08 -06002733static void free_copy(struct load_info *info)
2734{
Rusty Russelld9131882010-08-05 12:59:08 -06002735 vfree(info->hdr);
2736}
2737
Rusty Russell2f3238a2012-10-22 18:09:41 +10302738static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002739{
2740 unsigned int i;
2741
2742 /* This should always be true, but let's be sure. */
2743 info->sechdrs[0].sh_addr = 0;
2744
2745 for (i = 1; i < info->hdr->e_shnum; i++) {
2746 Elf_Shdr *shdr = &info->sechdrs[i];
2747 if (shdr->sh_type != SHT_NOBITS
2748 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002749 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002750 return -ENOEXEC;
2751 }
2752
2753 /* Mark all sections sh_addr with their address in the
2754 temporary image. */
2755 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2756
2757#ifndef CONFIG_MODULE_UNLOAD
2758 /* Don't load .exit sections */
2759 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2760 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2761#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002762 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002763
2764 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302765 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2766 info->index.vers = 0; /* Pretend no __versions section! */
2767 else
2768 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002769 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002770 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2771 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002772 return 0;
2773}
2774
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002775/*
2776 * Set up our basic convenience variables (pointers to section headers,
2777 * search for module section index etc), and do some basic section
2778 * verification.
2779 *
2780 * Return the temporary module pointer (we'll replace it with the final
2781 * one when we move the module sections around).
2782 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302783static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002784{
2785 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002786 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002787 struct module *mod;
2788
2789 /* Set up the convenience variables */
2790 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002791 info->secstrings = (void *)info->hdr
2792 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002793
Rusty Russell2f3238a2012-10-22 18:09:41 +10302794 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002795 if (err)
2796 return ERR_PTR(err);
2797
2798 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002799 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002800 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2801 info->index.sym = i;
2802 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002803 info->strtab = (char *)info->hdr
2804 + info->sechdrs[info->index.str].sh_offset;
2805 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002806 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002807 }
2808
Rusty Russell49668682010-08-05 12:59:10 -06002809 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002810 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002811 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002812 return ERR_PTR(-ENOEXEC);
2813 }
2814 /* This is temporary: point mod into copy of data. */
2815 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2816
2817 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002818 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002819 return ERR_PTR(-ENOEXEC);
2820 }
2821
Rusty Russell49668682010-08-05 12:59:10 -06002822 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002823
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002824 /* Check module struct version now, before we try to use module. */
2825 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2826 return ERR_PTR(-ENOEXEC);
2827
2828 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002829}
2830
Rusty Russell2f3238a2012-10-22 18:09:41 +10302831static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002832{
Rusty Russell49668682010-08-05 12:59:10 -06002833 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002834 int err;
2835
Rusty Russell2f3238a2012-10-22 18:09:41 +10302836 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2837 modmagic = NULL;
2838
Rusty Russell40dd2562010-08-05 12:59:03 -06002839 /* This is allowed: modprobe --force will invalidate it. */
2840 if (!modmagic) {
2841 err = try_to_force_load(mod, "bad vermagic");
2842 if (err)
2843 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002844 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002845 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002846 mod->name, modmagic, vermagic);
2847 return -ENOEXEC;
2848 }
2849
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002850 if (!get_modinfo(info, "intree"))
Rusty Russell373d4d02013-01-21 17:17:39 +10302851 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002852
Rusty Russell49668682010-08-05 12:59:10 -06002853 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302854 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002855 pr_warn("%s: module is from the staging directory, the quality "
2856 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002857 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002858
2859 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002860 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002861
Rusty Russell40dd2562010-08-05 12:59:03 -06002862 return 0;
2863}
2864
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302865static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002866{
Rusty Russell49668682010-08-05 12:59:10 -06002867 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002868 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002869 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002870 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002871 mod->crcs = section_addr(info, "__kcrctab");
2872 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002873 sizeof(*mod->gpl_syms),
2874 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002875 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2876 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002877 "__ksymtab_gpl_future",
2878 sizeof(*mod->gpl_future_syms),
2879 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002880 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002881
2882#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002883 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002884 sizeof(*mod->unused_syms),
2885 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002886 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2887 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002888 sizeof(*mod->unused_gpl_syms),
2889 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002890 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002891#endif
2892#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002893 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002894 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302895 if (!mod->ctors)
2896 mod->ctors = section_objs(info, ".init_array",
2897 sizeof(*mod->ctors), &mod->num_ctors);
2898 else if (find_sec(info, ".init_array")) {
2899 /*
2900 * This shouldn't happen with same compiler and binutils
2901 * building all parts of the module.
2902 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10302903 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302904 mod->name);
2905 return -EINVAL;
2906 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002907#endif
2908
2909#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002910 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2911 sizeof(*mod->tracepoints_ptrs),
2912 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002913#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002914#ifdef HAVE_JUMP_LABEL
2915 mod->jump_entries = section_objs(info, "__jump_table",
2916 sizeof(*mod->jump_entries),
2917 &mod->num_jump_entries);
2918#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002919#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002920 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002921 sizeof(*mod->trace_events),
2922 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002923 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
2924 sizeof(*mod->trace_enums),
2925 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002926#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002927#ifdef CONFIG_TRACING
2928 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2929 sizeof(*mod->trace_bprintk_fmt_start),
2930 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002931#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002932#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2933 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002934 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002935 sizeof(*mod->ftrace_callsites),
2936 &mod->num_ftrace_callsites);
2937#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002938
Rusty Russell811d66a2010-08-05 12:59:12 -06002939 mod->extable = section_objs(info, "__ex_table",
2940 sizeof(*mod->extable), &mod->num_exentries);
2941
Rusty Russell49668682010-08-05 12:59:10 -06002942 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002943 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002944
2945 info->debug = section_objs(info, "__verbose",
2946 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302947
2948 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002949}
2950
Rusty Russell49668682010-08-05 12:59:10 -06002951static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002952{
2953 int i;
2954 void *ptr;
2955
2956 /* Do the allocs. */
Peter Zijlstra4f6665462015-05-27 11:09:38 +09302957 ptr = module_alloc(mod->core_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002958 /*
2959 * The pointer to this block is stored in the module structure
2960 * which is inside the block. Just mark it as not being a
2961 * leak.
2962 */
2963 kmemleak_not_leak(ptr);
2964 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002965 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002966
2967 memset(ptr, 0, mod->core_size);
2968 mod->module_core = ptr;
2969
Rusty Russell82fab442012-12-11 09:38:33 +10302970 if (mod->init_size) {
Peter Zijlstra4f6665462015-05-27 11:09:38 +09302971 ptr = module_alloc(mod->init_size);
Rusty Russell82fab442012-12-11 09:38:33 +10302972 /*
2973 * The pointer to this block is stored in the module structure
2974 * which is inside the block. This block doesn't need to be
2975 * scanned as it contains data and code that will be freed
2976 * after the module is initialized.
2977 */
2978 kmemleak_ignore(ptr);
2979 if (!ptr) {
Rusty Russellbe1f2212015-01-20 09:07:05 +10302980 module_memfree(mod->module_core);
Rusty Russell82fab442012-12-11 09:38:33 +10302981 return -ENOMEM;
2982 }
2983 memset(ptr, 0, mod->init_size);
2984 mod->module_init = ptr;
2985 } else
2986 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002987
2988 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002989 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002990 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002991 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002992 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002993
Rusty Russell49668682010-08-05 12:59:10 -06002994 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002995 continue;
2996
Rusty Russell49668682010-08-05 12:59:10 -06002997 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002998 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002999 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003000 else
Rusty Russell49668682010-08-05 12:59:10 -06003001 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003002
Rusty Russell49668682010-08-05 12:59:10 -06003003 if (shdr->sh_type != SHT_NOBITS)
3004 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003005 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003006 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003007 pr_debug("\t0x%lx %s\n",
3008 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003009 }
Rusty Russelld9131882010-08-05 12:59:08 -06003010
3011 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003012}
3013
Rusty Russell49668682010-08-05 12:59:10 -06003014static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003015{
3016 /*
3017 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3018 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3019 * using GPL-only symbols it needs.
3020 */
3021 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303022 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003023
3024 /* driverloader was caught wrongly pretending to be under GPL */
3025 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303026 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3027 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003028
Matthew Garrettc99af372012-06-22 13:49:31 -04003029 /* lve claims to be GPL but upstream won't provide source */
3030 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303031 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3032 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003033
Rusty Russell22e268e2010-08-05 12:59:05 -06003034#ifdef CONFIG_MODVERSIONS
3035 if ((mod->num_syms && !mod->crcs)
3036 || (mod->num_gpl_syms && !mod->gpl_crcs)
3037 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3038#ifdef CONFIG_UNUSED_SYMBOLS
3039 || (mod->num_unused_syms && !mod->unused_crcs)
3040 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3041#endif
3042 ) {
3043 return try_to_force_load(mod,
3044 "no versions for exported symbols");
3045 }
3046#endif
3047 return 0;
3048}
3049
3050static void flush_module_icache(const struct module *mod)
3051{
3052 mm_segment_t old_fs;
3053
3054 /* flush the icache in correct context */
3055 old_fs = get_fs();
3056 set_fs(KERNEL_DS);
3057
3058 /*
3059 * Flush the instruction cache, since we've played with text.
3060 * Do it before processing of module parameters, so the module
3061 * can provide parameter accessor functions of its own.
3062 */
3063 if (mod->module_init)
3064 flush_icache_range((unsigned long)mod->module_init,
3065 (unsigned long)mod->module_init
3066 + mod->init_size);
3067 flush_icache_range((unsigned long)mod->module_core,
3068 (unsigned long)mod->module_core + mod->core_size);
3069
3070 set_fs(old_fs);
3071}
3072
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003073int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3074 Elf_Shdr *sechdrs,
3075 char *secstrings,
3076 struct module *mod)
3077{
3078 return 0;
3079}
3080
Rusty Russell2f3238a2012-10-22 18:09:41 +10303081static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003082{
3083 /* Module within temporary copy. */
3084 struct module *mod;
3085 int err;
3086
Rusty Russell2f3238a2012-10-22 18:09:41 +10303087 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003088 if (IS_ERR(mod))
3089 return mod;
3090
Rusty Russell2f3238a2012-10-22 18:09:41 +10303091 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003092 if (err)
3093 return ERR_PTR(err);
3094
3095 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003096 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3097 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003098 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303099 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003100
Rusty Russell8d8022e2013-07-03 10:06:28 +09303101 /* We will do a special allocation for per-cpu sections later. */
3102 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003103
3104 /* Determine total sizes, and put offsets in sh_entsize. For now
3105 this is done generically; there doesn't appear to be any
3106 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003107 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003108 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003109
3110 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003111 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003112 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303113 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003114
3115 /* Module has been copied to its final place now: return it. */
3116 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003117 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003118 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003119}
3120
3121/* mod is no longer valid after this! */
3122static void module_deallocate(struct module *mod, struct load_info *info)
3123{
Rusty Russelld9131882010-08-05 12:59:08 -06003124 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303125 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10303126 module_memfree(mod->module_init);
3127 module_memfree(mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06003128}
3129
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003130int __weak module_finalize(const Elf_Ehdr *hdr,
3131 const Elf_Shdr *sechdrs,
3132 struct module *me)
3133{
3134 return 0;
3135}
3136
Rusty Russell811d66a2010-08-05 12:59:12 -06003137static int post_relocation(struct module *mod, const struct load_info *info)
3138{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003139 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003140 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3141
3142 /* Copy relocated percpu area over. */
3143 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3144 info->sechdrs[info->index.pcpu].sh_size);
3145
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003146 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003147 add_kallsyms(mod, info);
3148
3149 /* Arch-specific module finalizing. */
3150 return module_finalize(info->hdr, info->sechdrs, mod);
3151}
3152
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303153/* Is this module of this name done loading? No locks held. */
3154static bool finished_loading(const char *name)
3155{
3156 struct module *mod;
3157 bool ret;
3158
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303159 /*
3160 * The module_mutex should not be a heavily contended lock;
3161 * if we get the occasional sleep here, we'll go an extra iteration
3162 * in the wait_event_interruptible(), which is harmless.
3163 */
3164 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303165 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303166 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303167 ret = !mod || mod->state == MODULE_STATE_LIVE
3168 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303169 mutex_unlock(&module_mutex);
3170
3171 return ret;
3172}
3173
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003174/* Call module constructors. */
3175static void do_mod_ctors(struct module *mod)
3176{
3177#ifdef CONFIG_CONSTRUCTORS
3178 unsigned long i;
3179
3180 for (i = 0; i < mod->num_ctors; i++)
3181 mod->ctors[i]();
3182#endif
3183}
3184
Rusty Russellc7496372015-01-20 09:07:05 +10303185/* For freeing module_init on success, in case kallsyms traversing */
3186struct mod_initfree {
3187 struct rcu_head rcu;
3188 void *module_init;
3189};
3190
3191static void do_free_init(struct rcu_head *head)
3192{
3193 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3194 module_memfree(m->module_init);
3195 kfree(m);
3196}
3197
Jan Kiszkabe02a182015-02-17 13:46:50 -08003198/*
3199 * This is where the real work happens.
3200 *
3201 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3202 * helper command 'lx-symbols'.
3203 */
3204static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303207 struct mod_initfree *freeinit;
3208
3209 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3210 if (!freeinit) {
3211 ret = -ENOMEM;
3212 goto fail;
3213 }
3214 freeinit->module_init = mod->module_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Tejun Heo774a1222013-01-15 18:52:51 -08003216 /*
3217 * We want to find out whether @mod uses async during init. Clear
3218 * PF_USED_ASYNC. async_schedule*() will set it.
3219 */
3220 current->flags &= ~PF_USED_ASYNC;
3221
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003222 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 /* Start the module */
3224 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003225 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303227 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003229 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003230 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3231 "follow 0/-E convention\n"
3232 "%s: loading module anyway...\n",
3233 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003234 dump_stack();
3235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Rusty Russell6f139092012-09-28 14:31:03 +09303237 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003239 blocking_notifier_call_chain(&module_notify_list,
3240 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003241
Tejun Heo774a1222013-01-15 18:52:51 -08003242 /*
3243 * We need to finish all async code before the module init sequence
3244 * is done. This has potential to deadlock. For example, a newly
3245 * detected block device can trigger request_module() of the
3246 * default iosched from async probing task. Once userland helper
3247 * reaches here, async_synchronize_full() will wait on the async
3248 * task waiting on request_module() and deadlock.
3249 *
3250 * This deadlock is avoided by perfomring async_synchronize_full()
3251 * iff module init queued any async jobs. This isn't a full
3252 * solution as it will deadlock the same if module loading from
3253 * async jobs nests more than once; however, due to the various
3254 * constraints, this hack seems to be the best option for now.
3255 * Please refer to the following thread for details.
3256 *
3257 * http://thread.gmane.org/gmane.linux.kernel/1420814
3258 */
3259 if (current->flags & PF_USED_ASYNC)
3260 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003261
Rusty Russell6c5db222008-03-10 11:43:52 -07003262 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 /* Drop initial reference. */
3264 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003265 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003266#ifdef CONFIG_KALLSYMS
3267 mod->num_symtab = mod->core_num_syms;
3268 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003269 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003270#endif
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303271 mod_tree_remove_init(mod);
Jan Glauber01526ed2011-05-19 16:55:26 -06003272 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303273 module_arch_freeing_init(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 mod->module_init = NULL;
3275 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003276 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 mod->init_text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303278 /*
3279 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303280 * walking this with preempt disabled. In all the failure paths, we
3281 * call synchronize_sched(), but we don't want to slow down the success
3282 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303283 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303284 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003285 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303286 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303289
3290fail_free_freeinit:
3291 kfree(freeinit);
3292fail:
3293 /* Try to protect us from buggy refcounters. */
3294 mod->state = MODULE_STATE_GOING;
3295 synchronize_sched();
3296 module_put(mod);
3297 blocking_notifier_call_chain(&module_notify_list,
3298 MODULE_STATE_GOING, mod);
3299 free_module(mod);
3300 wake_up_all(&module_wq);
3301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Kees Cook34e11692012-10-16 07:31:07 +10303304static int may_init_module(void)
3305{
3306 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3307 return -EPERM;
3308
3309 return 0;
3310}
3311
Rusty Russella3535c72013-01-21 17:18:59 +10303312/*
3313 * We try to place it in the list now to make sure it's unique before
3314 * we dedicate too many resources. In particular, temporary percpu
3315 * memory exhaustion.
3316 */
3317static int add_unformed_module(struct module *mod)
3318{
3319 int err;
3320 struct module *old;
3321
3322 mod->state = MODULE_STATE_UNFORMED;
3323
3324again:
3325 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303326 old = find_module_all(mod->name, strlen(mod->name), true);
3327 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303328 if (old->state == MODULE_STATE_COMING
3329 || old->state == MODULE_STATE_UNFORMED) {
3330 /* Wait in case it fails to load. */
3331 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303332 err = wait_event_interruptible(module_wq,
3333 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303334 if (err)
3335 goto out_unlocked;
3336 goto again;
3337 }
3338 err = -EEXIST;
3339 goto out;
3340 }
Peter Zijlstra4f6665462015-05-27 11:09:38 +09303341 mod_update_bounds(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303342 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303343 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303344 err = 0;
3345
3346out:
3347 mutex_unlock(&module_mutex);
3348out_unlocked:
3349 return err;
3350}
3351
3352static int complete_formation(struct module *mod, struct load_info *info)
3353{
3354 int err;
3355
3356 mutex_lock(&module_mutex);
3357
3358 /* Find duplicate symbols (must be called under lock). */
3359 err = verify_export_symbols(mod);
3360 if (err < 0)
3361 goto out;
3362
3363 /* This relies on module_mutex for list integrity. */
3364 module_bug_finalize(info->hdr, info->sechdrs, mod);
3365
Rusty Russell49822232014-05-14 10:54:19 +09303366 /* Set RO and NX regions for core */
3367 set_section_ro_nx(mod->module_core,
3368 mod->core_text_size,
3369 mod->core_ro_size,
3370 mod->core_size);
3371
3372 /* Set RO and NX regions for init */
3373 set_section_ro_nx(mod->module_init,
3374 mod->init_text_size,
3375 mod->init_ro_size,
3376 mod->init_size);
3377
Rusty Russella3535c72013-01-21 17:18:59 +10303378 /* Mark state as coming so strong_try_module_get() ignores us,
3379 * but kallsyms etc. can see us. */
3380 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303381 mutex_unlock(&module_mutex);
3382
3383 blocking_notifier_call_chain(&module_notify_list,
3384 MODULE_STATE_COMING, mod);
3385 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303386
3387out:
3388 mutex_unlock(&module_mutex);
3389 return err;
3390}
3391
Rusty Russell54041d82013-07-02 15:35:12 +09303392static int unknown_module_param_cb(char *param, char *val, const char *modname)
3393{
Ionut Alexa6da0b562014-11-10 09:31:29 +10303394 /* Check for magic 'dyndbg' arg */
Rusty Russell54041d82013-07-02 15:35:12 +09303395 int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003396 if (ret != 0)
3397 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303398 return 0;
3399}
3400
Kees Cook34e11692012-10-16 07:31:07 +10303401/* Allocate and load the module: note that size of section 0 is always
3402 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303403static int load_module(struct load_info *info, const char __user *uargs,
3404 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303405{
Rusty Russella3535c72013-01-21 17:18:59 +10303406 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303407 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303408 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303409
3410 err = module_sig_check(info);
3411 if (err)
3412 goto free_copy;
3413
3414 err = elf_header_check(info);
3415 if (err)
3416 goto free_copy;
3417
3418 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303419 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303420 if (IS_ERR(mod)) {
3421 err = PTR_ERR(mod);
3422 goto free_copy;
3423 }
3424
Rusty Russella3535c72013-01-21 17:18:59 +10303425 /* Reserve our place in the list. */
3426 err = add_unformed_module(mod);
3427 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303428 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303429
Kees Cook34e11692012-10-16 07:31:07 +10303430#ifdef CONFIG_MODULE_SIG
3431 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303432 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003433 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303434 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003435 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303436 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303437 }
Kees Cook34e11692012-10-16 07:31:07 +10303438#endif
3439
Rusty Russell8d8022e2013-07-03 10:06:28 +09303440 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303441 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303442 if (err)
3443 goto unlink_mod;
3444
Kees Cook34e11692012-10-16 07:31:07 +10303445 /* Now module is in final location, initialize linked lists, etc. */
3446 err = module_unload_init(mod);
3447 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303448 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303449
3450 /* Now we've got everything in the final locations, we can
3451 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303452 err = find_module_sections(mod, info);
3453 if (err)
3454 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303455
3456 err = check_module_license_and_versions(mod);
3457 if (err)
3458 goto free_unload;
3459
3460 /* Set up MODINFO_ATTR fields */
3461 setup_modinfo(mod, info);
3462
3463 /* Fix up syms, so that st_value is a pointer to location. */
3464 err = simplify_symbols(mod, info);
3465 if (err < 0)
3466 goto free_modinfo;
3467
3468 err = apply_relocations(mod, info);
3469 if (err < 0)
3470 goto free_modinfo;
3471
3472 err = post_relocation(mod, info);
3473 if (err < 0)
3474 goto free_modinfo;
3475
3476 flush_module_icache(mod);
3477
3478 /* Now copy in args */
3479 mod->args = strndup_user(uargs, ~0UL >> 1);
3480 if (IS_ERR(mod->args)) {
3481 err = PTR_ERR(mod->args);
3482 goto free_arch_cleanup;
3483 }
3484
Kees Cook34e11692012-10-16 07:31:07 +10303485 dynamic_debug_setup(info->debug, info->num_debug);
3486
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003487 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3488 ftrace_module_init(mod);
3489
Rusty Russella3535c72013-01-21 17:18:59 +10303490 /* Finally it's fully formed, ready to start executing. */
3491 err = complete_formation(mod, info);
3492 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303493 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303494
Kees Cook34e11692012-10-16 07:31:07 +10303495 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303496 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3497 -32768, 32767, unknown_module_param_cb);
3498 if (IS_ERR(after_dashes)) {
3499 err = PTR_ERR(after_dashes);
Rusty Russell1fb93412013-01-12 13:27:34 +10303500 goto bug_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303501 } else if (after_dashes) {
3502 pr_warn("%s: parameters '%s' after `--' ignored\n",
3503 mod->name, after_dashes);
3504 }
Kees Cook34e11692012-10-16 07:31:07 +10303505
3506 /* Link in to syfs. */
3507 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3508 if (err < 0)
Rusty Russell1fb93412013-01-12 13:27:34 +10303509 goto bug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303510
3511 /* Get rid of temporary copy. */
3512 free_copy(info);
3513
3514 /* Done! */
3515 trace_module_load(mod);
3516
3517 return do_init_module(mod);
3518
Rusty Russell1fb93412013-01-12 13:27:34 +10303519 bug_cleanup:
3520 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303521 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303522 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003523 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303524
3525 /* we can't deallocate the module until we clear memory protection */
3526 unset_module_init_ro_nx(mod);
3527 unset_module_core_ro_nx(mod);
3528
Rusty Russella3535c72013-01-21 17:18:59 +10303529 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303530 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303531 synchronize_sched();
3532 kfree(mod->args);
3533 free_arch_cleanup:
3534 module_arch_cleanup(mod);
3535 free_modinfo:
3536 free_modinfo(mod);
3537 free_unload:
3538 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303539 unlink_mod:
3540 mutex_lock(&module_mutex);
3541 /* Unlink carefully: kallsyms could be walking list. */
3542 list_del_rcu(&mod->list);
3543 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303544 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3545 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303546 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303547 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003548 /* Free lock-classes; relies on the preceding sync_rcu() */
3549 lockdep_free_key_range(mod->module_core, mod->core_size);
3550
Kees Cook34e11692012-10-16 07:31:07 +10303551 module_deallocate(mod, info);
3552 free_copy:
3553 free_copy(info);
3554 return err;
3555}
3556
3557SYSCALL_DEFINE3(init_module, void __user *, umod,
3558 unsigned long, len, const char __user *, uargs)
3559{
3560 int err;
3561 struct load_info info = { };
3562
3563 err = may_init_module();
3564 if (err)
3565 return err;
3566
3567 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3568 umod, len, uargs);
3569
3570 err = copy_module_from_user(umod, len, &info);
3571 if (err)
3572 return err;
3573
Rusty Russell2f3238a2012-10-22 18:09:41 +10303574 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303575}
3576
Rusty Russell2f3238a2012-10-22 18:09:41 +10303577SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303578{
3579 int err;
3580 struct load_info info = { };
3581
3582 err = may_init_module();
3583 if (err)
3584 return err;
3585
Rusty Russell2f3238a2012-10-22 18:09:41 +10303586 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3587
3588 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3589 |MODULE_INIT_IGNORE_VERMAGIC))
3590 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303591
3592 err = copy_module_from_fd(fd, &info);
3593 if (err)
3594 return err;
3595
Rusty Russell2f3238a2012-10-22 18:09:41 +10303596 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303597}
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599static inline int within(unsigned long addr, void *start, unsigned long size)
3600{
3601 return ((void *)addr >= start && (void *)addr < start + size);
3602}
3603
3604#ifdef CONFIG_KALLSYMS
3605/*
3606 * This ignores the intensely annoying "mapping symbols" found
3607 * in ARM ELF files: $a, $t and $d.
3608 */
3609static inline int is_arm_mapping_symbol(const char *str)
3610{
Russell King2e3a10a2014-07-27 07:29:01 +09303611 if (str[0] == '.' && str[1] == 'L')
3612 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003613 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 && (str[2] == '\0' || str[2] == '.');
3615}
3616
3617static const char *get_ksymbol(struct module *mod,
3618 unsigned long addr,
3619 unsigned long *size,
3620 unsigned long *offset)
3621{
3622 unsigned int i, best = 0;
3623 unsigned long nextval;
3624
3625 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003626 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003628 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3630
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003631 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003632 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 for (i = 1; i < mod->num_symtab; i++) {
3634 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3635 continue;
3636
3637 /* We ignore unnamed symbols: they're uninformative
3638 * and inserted at a whim. */
3639 if (mod->symtab[i].st_value <= addr
3640 && mod->symtab[i].st_value > mod->symtab[best].st_value
3641 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3642 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3643 best = i;
3644 if (mod->symtab[i].st_value > addr
3645 && mod->symtab[i].st_value < nextval
3646 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3647 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3648 nextval = mod->symtab[i].st_value;
3649 }
3650
3651 if (!best)
3652 return NULL;
3653
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003654 if (size)
3655 *size = nextval - mod->symtab[best].st_value;
3656 if (offset)
3657 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return mod->strtab + mod->symtab[best].st_name;
3659}
3660
Rusty Russell6dd06c92008-01-29 17:13:22 -05003661/* For kallsyms to ask for address resolution. NULL means not found. Careful
3662 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003663const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003664 unsigned long *size,
3665 unsigned long *offset,
3666 char **modname,
3667 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003669 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303670 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Rusty Russellcb2a5202008-01-14 00:55:03 -08003672 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303673 mod = __module_address(addr);
3674 if (mod) {
3675 if (modname)
3676 *modname = mod->name;
3677 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003679 /* Make a copy in here where it's safe */
3680 if (ret) {
3681 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3682 ret = namebuf;
3683 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003684 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303685
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003689int lookup_module_symbol_name(unsigned long addr, char *symname)
3690{
3691 struct module *mod;
3692
Rusty Russellcb2a5202008-01-14 00:55:03 -08003693 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003694 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303695 if (mod->state == MODULE_STATE_UNFORMED)
3696 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303697 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003698 const char *sym;
3699
3700 sym = get_ksymbol(mod, addr, NULL, NULL);
3701 if (!sym)
3702 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003703 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003704 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003705 return 0;
3706 }
3707 }
3708out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003709 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003710 return -ERANGE;
3711}
3712
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003713int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3714 unsigned long *offset, char *modname, char *name)
3715{
3716 struct module *mod;
3717
Rusty Russellcb2a5202008-01-14 00:55:03 -08003718 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003719 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303720 if (mod->state == MODULE_STATE_UNFORMED)
3721 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303722 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003723 const char *sym;
3724
3725 sym = get_ksymbol(mod, addr, size, offset);
3726 if (!sym)
3727 goto out;
3728 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003729 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003730 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003731 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003732 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003733 return 0;
3734 }
3735 }
3736out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003737 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003738 return -ERANGE;
3739}
3740
Alexey Dobriyanea078902007-05-08 00:28:39 -07003741int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3742 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
3744 struct module *mod;
3745
Rusty Russellcb2a5202008-01-14 00:55:03 -08003746 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003747 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303748 if (mod->state == MODULE_STATE_UNFORMED)
3749 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (symnum < mod->num_symtab) {
3751 *value = mod->symtab[symnum].st_value;
3752 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003753 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003754 KSYM_NAME_LEN);
3755 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003756 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003757 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
3760 symnum -= mod->num_symtab;
3761 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003762 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003763 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
3766static unsigned long mod_find_symname(struct module *mod, const char *name)
3767{
3768 unsigned int i;
3769
3770 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003771 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3772 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 return mod->symtab[i].st_value;
3774 return 0;
3775}
3776
3777/* Look for this name: can be of form module:name. */
3778unsigned long module_kallsyms_lookup_name(const char *name)
3779{
3780 struct module *mod;
3781 char *colon;
3782 unsigned long ret = 0;
3783
3784 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003785 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303787 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303790 list_for_each_entry_rcu(mod, &modules, list) {
3791 if (mod->state == MODULE_STATE_UNFORMED)
3792 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 if ((ret = mod_find_symname(mod, name)) != 0)
3794 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003797 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return ret;
3799}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003800
3801int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3802 struct module *, unsigned long),
3803 void *data)
3804{
3805 struct module *mod;
3806 unsigned int i;
3807 int ret;
3808
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303809 module_assert_mutex();
3810
Anders Kaseorg75a66612008-12-05 19:03:58 -05003811 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303812 if (mod->state == MODULE_STATE_UNFORMED)
3813 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003814 for (i = 0; i < mod->num_symtab; i++) {
3815 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3816 mod, mod->symtab[i].st_value);
3817 if (ret != 0)
3818 return ret;
3819 }
3820 }
3821 return 0;
3822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823#endif /* CONFIG_KALLSYMS */
3824
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003825static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003826{
3827 int bx = 0;
3828
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303829 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003830 if (mod->taints ||
3831 mod->state == MODULE_STATE_GOING ||
3832 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003833 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303834 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003835 /* Show a - for module-is-being-unloaded */
3836 if (mod->state == MODULE_STATE_GOING)
3837 buf[bx++] = '-';
3838 /* Show a + for module-is-being-loaded */
3839 if (mod->state == MODULE_STATE_COMING)
3840 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003841 buf[bx++] = ')';
3842 }
3843 buf[bx] = '\0';
3844
3845 return buf;
3846}
3847
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003848#ifdef CONFIG_PROC_FS
3849/* Called by the /proc file system to return a list of modules. */
3850static void *m_start(struct seq_file *m, loff_t *pos)
3851{
3852 mutex_lock(&module_mutex);
3853 return seq_list_start(&modules, *pos);
3854}
3855
3856static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3857{
3858 return seq_list_next(p, &modules, pos);
3859}
3860
3861static void m_stop(struct seq_file *m, void *p)
3862{
3863 mutex_unlock(&module_mutex);
3864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866static int m_show(struct seq_file *m, void *p)
3867{
3868 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003869 char buf[8];
3870
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303871 /* We always ignore unformed modules. */
3872 if (mod->state == MODULE_STATE_UNFORMED)
3873 return 0;
3874
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003875 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 mod->name, mod->init_size + mod->core_size);
3877 print_unload_info(m, mod);
3878
3879 /* Informative for users. */
3880 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10303881 mod->state == MODULE_STATE_GOING ? "Unloading" :
3882 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 "Live");
3884 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003885 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003887 /* Taints info */
3888 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003889 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003890
Ionut Alexa6da0b562014-11-10 09:31:29 +10303891 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return 0;
3893}
3894
3895/* Format: modulename size refcount deps address
3896
3897 Where refcount is a number or -, and deps is a comma-separated list
3898 of depends or -.
3899*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003900static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 .start = m_start,
3902 .next = m_next,
3903 .stop = m_stop,
3904 .show = m_show
3905};
3906
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003907static int modules_open(struct inode *inode, struct file *file)
3908{
3909 return seq_open(file, &modules_op);
3910}
3911
3912static const struct file_operations proc_modules_operations = {
3913 .open = modules_open,
3914 .read = seq_read,
3915 .llseek = seq_lseek,
3916 .release = seq_release,
3917};
3918
3919static int __init proc_modules_init(void)
3920{
3921 proc_create("modules", 0, NULL, &proc_modules_operations);
3922 return 0;
3923}
3924module_init(proc_modules_init);
3925#endif
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927/* Given an address, look for it in the module exception tables. */
3928const struct exception_table_entry *search_module_extables(unsigned long addr)
3929{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 const struct exception_table_entry *e = NULL;
3931 struct module *mod;
3932
Rusty Russell24da1cb2007-07-15 23:41:46 -07003933 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003934 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303935 if (mod->state == MODULE_STATE_UNFORMED)
3936 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (mod->num_exentries == 0)
3938 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 e = search_extable(mod->extable,
3941 mod->extable + mod->num_exentries - 1,
3942 addr);
3943 if (e)
3944 break;
3945 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003946 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003949 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return e;
3951}
3952
Ingo Molnar4d435f92006-07-03 00:24:24 -07003953/*
Rusty Russelle6104992009-03-31 13:05:31 -06003954 * is_module_address - is this address inside a module?
3955 * @addr: the address to check.
3956 *
3957 * See is_module_text_address() if you simply want to see if the address
3958 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003959 */
Rusty Russelle6104992009-03-31 13:05:31 -06003960bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003961{
Rusty Russelle6104992009-03-31 13:05:31 -06003962 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003963
Rusty Russell24da1cb2007-07-15 23:41:46 -07003964 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003965 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003966 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003967
Rusty Russelle6104992009-03-31 13:05:31 -06003968 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003969}
3970
Rusty Russelle6104992009-03-31 13:05:31 -06003971/*
3972 * __module_address - get the module which contains an address.
3973 * @addr: the address.
3974 *
3975 * Must be called with preempt disabled or module mutex held so that
3976 * module doesn't get freed during this.
3977 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003978struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
3980 struct module *mod;
3981
Rusty Russell3a642e92008-07-22 19:24:28 -05003982 if (addr < module_addr_min || addr > module_addr_max)
3983 return NULL;
3984
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303985 module_assert_mutex_or_preempt();
3986
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09303987 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303988 if (mod) {
3989 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303990 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303991 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303992 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303993 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
Tim Abbottc6b37802008-12-05 19:03:59 -05003995EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Rusty Russelle6104992009-03-31 13:05:31 -06003997/*
3998 * is_module_text_address - is this address inside module code?
3999 * @addr: the address to check.
4000 *
4001 * See is_module_address() if you simply want to see if the address is
4002 * anywhere in a module. See kernel_text_address() for testing if an
4003 * address corresponds to kernel or module code.
4004 */
4005bool is_module_text_address(unsigned long addr)
4006{
4007 bool ret;
4008
4009 preempt_disable();
4010 ret = __module_text_address(addr) != NULL;
4011 preempt_enable();
4012
4013 return ret;
4014}
4015
4016/*
4017 * __module_text_address - get the module whose code contains an address.
4018 * @addr: the address.
4019 *
4020 * Must be called with preempt disabled or module mutex held so that
4021 * module doesn't get freed during this.
4022 */
4023struct module *__module_text_address(unsigned long addr)
4024{
4025 struct module *mod = __module_address(addr);
4026 if (mod) {
4027 /* Make sure it's within the text section. */
4028 if (!within(addr, mod->module_init, mod->init_text_size)
4029 && !within(addr, mod->module_core, mod->core_text_size))
4030 mod = NULL;
4031 }
4032 return mod;
4033}
Tim Abbottc6b37802008-12-05 19:03:59 -05004034EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036/* Don't grab lock, we're oopsing. */
4037void print_modules(void)
4038{
4039 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07004040 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Linus Torvaldsb2311252009-06-16 11:07:14 -07004042 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004043 /* Most callers should already have preempt disabled, but make sure */
4044 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304045 list_for_each_entry_rcu(mod, &modules, list) {
4046 if (mod->state == MODULE_STATE_UNFORMED)
4047 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304048 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304049 }
Andi Kleend72b3752008-08-30 10:09:00 +02004050 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004051 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304052 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4053 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004057/* Generate the signature for all relevant module structures here.
4058 * If these change, we don't want to try to parse the module. */
4059void module_layout(struct module *mod,
4060 struct modversion_info *ver,
4061 struct kernel_param *kp,
4062 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004063 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004064{
4065}
4066EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067#endif