blob: 293dfaf4ce52ee03ec291cf5fa0bbd44115789e6 [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
173static struct latch_tree_root mod_tree __cacheline_aligned;
174
175/*
176 * These modifications: insert, remove_init and remove; are serialized by the
177 * module_mutex.
178 */
179static void mod_tree_insert(struct module *mod)
180{
181 mod->mtn_core.mod = mod;
182 mod->mtn_init.mod = mod;
183
184 latch_tree_insert(&mod->mtn_core.node, &mod_tree, &mod_tree_ops);
185 if (mod->init_size)
186 latch_tree_insert(&mod->mtn_init.node, &mod_tree, &mod_tree_ops);
187}
188
189static void mod_tree_remove_init(struct module *mod)
190{
191 if (mod->init_size)
192 latch_tree_erase(&mod->mtn_init.node, &mod_tree, &mod_tree_ops);
193}
194
195static void mod_tree_remove(struct module *mod)
196{
197 latch_tree_erase(&mod->mtn_core.node, &mod_tree, &mod_tree_ops);
198 mod_tree_remove_init(mod);
199}
200
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930201static struct module *mod_find(unsigned long addr)
Peter Zijlstra93c2e102015-05-27 11:09:37 +0930202{
203 struct latch_tree_node *ltn;
204
205 ltn = latch_tree_find((void *)addr, &mod_tree, &mod_tree_ops);
206 if (!ltn)
207 return NULL;
208
209 return container_of(ltn, struct mod_tree_node, node)->mod;
210}
211
Peter Zijlstra6c9692e2015-05-27 11:09:37 +0930212#else /* MODULES_TREE_LOOKUP */
213
214static void mod_tree_insert(struct module *mod) { }
215static void mod_tree_remove_init(struct module *mod) { }
216static void mod_tree_remove(struct module *mod) { }
217
218static struct module *mod_find(unsigned long addr)
219{
220 struct module *mod;
221
222 list_for_each_entry_rcu(mod, &modules, list) {
223 if (within_module(addr, mod))
224 return mod;
225 }
226
227 return NULL;
228}
229
230#endif /* MODULES_TREE_LOOKUP */
231
Jason Wessel67fc4e02010-05-20 21:04:21 -0500232#ifdef CONFIG_KGDB_KDB
233struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
234#endif /* CONFIG_KGDB_KDB */
235
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930236static void module_assert_mutex(void)
237{
238 lockdep_assert_held(&module_mutex);
239}
240
241static void module_assert_mutex_or_preempt(void)
242{
243#ifdef CONFIG_LOCKDEP
244 if (unlikely(!debug_locks))
245 return;
246
247 WARN_ON(!rcu_read_lock_sched_held() &&
248 !lockdep_is_held(&module_mutex));
249#endif
250}
251
Rusty Russell106a4ee2012-09-26 10:09:40 +0100252#ifdef CONFIG_MODULE_SIG
253#ifdef CONFIG_MODULE_SIG_FORCE
254static bool sig_enforce = true;
255#else
256static bool sig_enforce = false;
257
258static int param_set_bool_enable_only(const char *val,
259 const struct kernel_param *kp)
260{
261 int err;
262 bool test;
263 struct kernel_param dummy_kp = *kp;
264
265 dummy_kp.arg = &test;
266
267 err = param_set_bool(val, &dummy_kp);
268 if (err)
269 return err;
270
271 /* Don't let them unset it once it's set! */
272 if (!test && sig_enforce)
273 return -EROFS;
274
275 if (test)
276 sig_enforce = true;
277 return 0;
278}
279
280static const struct kernel_param_ops param_ops_bool_enable_only = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930281 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell106a4ee2012-09-26 10:09:40 +0100282 .set = param_set_bool_enable_only,
283 .get = param_get_bool,
284};
285#define param_check_bool_enable_only param_check_bool
286
287module_param(sig_enforce, bool_enable_only, 0644);
288#endif /* !CONFIG_MODULE_SIG_FORCE */
289#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Stephen Rothwell19e45292009-04-14 17:27:18 +1000291/* Block module loading/unloading? */
292int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800293core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000294
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500295/* Waiting for a module to finish initializing? */
296static DECLARE_WAIT_QUEUE_HEAD(module_wq);
297
Alan Sterne041c682006-03-27 01:16:30 -0800298static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Rusty Russell75676502010-06-05 11:17:36 -0600300/* Bounds of module allocation, for speeding __module_address.
301 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500302static unsigned long module_addr_min = -1UL, module_addr_max = 0;
303
Ionut Alexa6da0b562014-11-10 09:31:29 +1030304int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Alan Sterne041c682006-03-27 01:16:30 -0800306 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308EXPORT_SYMBOL(register_module_notifier);
309
Ionut Alexa6da0b562014-11-10 09:31:29 +1030310int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Alan Sterne041c682006-03-27 01:16:30 -0800312 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314EXPORT_SYMBOL(unregister_module_notifier);
315
Rusty Russelleded41c2010-08-05 12:59:07 -0600316struct load_info {
317 Elf_Ehdr *hdr;
318 unsigned long len;
319 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600320 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600321 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600322 struct _ddebug *debug;
323 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100324 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600325 struct {
326 unsigned int sym, str, mod, vers, info, pcpu;
327 } index;
328};
329
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800330/* We require a truly strong try_module_get(): 0 means failure due to
331 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static inline int strong_try_module_get(struct module *mod)
333{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030334 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500336 return -EBUSY;
337 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500339 else
340 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Rusty Russell373d4d02013-01-21 17:17:39 +1030343static inline void add_taint_module(struct module *mod, unsigned flag,
344 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700345{
Rusty Russell373d4d02013-01-21 17:17:39 +1030346 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700347 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700348}
349
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200350/*
351 * A thread that wants to hold a reference to a module only while it
352 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354void __module_put_and_exit(struct module *mod, long code)
355{
356 module_put(mod);
357 do_exit(code);
358}
359EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600362static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned int i;
365
Rusty Russell49668682010-08-05 12:59:10 -0600366 for (i = 1; i < info->hdr->e_shnum; i++) {
367 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600369 if ((shdr->sh_flags & SHF_ALLOC)
370 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Rusty Russell5e458cc2008-10-22 10:00:13 -0500376/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600377static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500378{
379 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600380 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500381}
382
383/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600384static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500385 const char *name,
386 size_t object_size,
387 unsigned int *num)
388{
Rusty Russell49668682010-08-05 12:59:10 -0600389 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500390
391 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600392 *num = info->sechdrs[sec].sh_size / object_size;
393 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/* Provided by the linker */
397extern const struct kernel_symbol __start___ksymtab[];
398extern const struct kernel_symbol __stop___ksymtab[];
399extern const struct kernel_symbol __start___ksymtab_gpl[];
400extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800401extern const struct kernel_symbol __start___ksymtab_gpl_future[];
402extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403extern const unsigned long __start___kcrctab[];
404extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800405extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500406#ifdef CONFIG_UNUSED_SYMBOLS
407extern const struct kernel_symbol __start___ksymtab_unused[];
408extern const struct kernel_symbol __stop___ksymtab_unused[];
409extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
410extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700411extern const unsigned long __start___kcrctab_unused[];
412extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415#ifndef CONFIG_MODVERSIONS
416#define symversion(base, idx) NULL
417#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800418#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#endif
420
Rusty Russelldafd0942008-07-22 19:24:25 -0500421static bool each_symbol_in_section(const struct symsearch *arr,
422 unsigned int arrsize,
423 struct module *owner,
424 bool (*fn)(const struct symsearch *syms,
425 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200426 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500427 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100428{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200429 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500430
431 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200432 if (fn(&arr[j], owner, data))
433 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500434 }
435
436 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100437}
438
Rusty Russelldafd0942008-07-22 19:24:25 -0500439/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200440bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
441 struct module *owner,
442 void *data),
443 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700444{
Rusty Russelldafd0942008-07-22 19:24:25 -0500445 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600446 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500447 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
448 NOT_GPL_ONLY, false },
449 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
450 __start___kcrctab_gpl,
451 GPL_ONLY, false },
452 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
453 __start___kcrctab_gpl_future,
454 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500455#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500456 { __start___ksymtab_unused, __stop___ksymtab_unused,
457 __start___kcrctab_unused,
458 NOT_GPL_ONLY, true },
459 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
460 __start___kcrctab_unused_gpl,
461 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500462#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500463 };
464
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930465 module_assert_mutex_or_preempt();
466
Rusty Russelldafd0942008-07-22 19:24:25 -0500467 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
468 return true;
469
Andi Kleend72b3752008-08-30 10:09:00 +0200470 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500471 struct symsearch arr[] = {
472 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
473 NOT_GPL_ONLY, false },
474 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
475 mod->gpl_crcs,
476 GPL_ONLY, false },
477 { mod->gpl_future_syms,
478 mod->gpl_future_syms + mod->num_gpl_future_syms,
479 mod->gpl_future_crcs,
480 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500481#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500482 { mod->unused_syms,
483 mod->unused_syms + mod->num_unused_syms,
484 mod->unused_crcs,
485 NOT_GPL_ONLY, true },
486 { mod->unused_gpl_syms,
487 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
488 mod->unused_gpl_crcs,
489 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500490#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500491 };
492
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030493 if (mod->state == MODULE_STATE_UNFORMED)
494 continue;
495
Rusty Russelldafd0942008-07-22 19:24:25 -0500496 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
497 return true;
498 }
499 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700500}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200501EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700502
Rusty Russelldafd0942008-07-22 19:24:25 -0500503struct find_symbol_arg {
504 /* Input */
505 const char *name;
506 bool gplok;
507 bool warn;
508
509 /* Output */
510 struct module *owner;
511 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500512 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500513};
514
Rusty Russellde4d8d52011-04-19 21:49:58 +0200515static bool check_symbol(const struct symsearch *syms,
516 struct module *owner,
517 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500518{
Rusty Russelldafd0942008-07-22 19:24:25 -0500519 struct find_symbol_arg *fsa = data;
520
Rusty Russelldafd0942008-07-22 19:24:25 -0500521 if (!fsa->gplok) {
522 if (syms->licence == GPL_ONLY)
523 return false;
524 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800525 pr_warn("Symbol %s is being used by a non-GPL module, "
526 "which will not be allowed in the future\n",
527 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500528 }
529 }
530
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500531#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500532 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800533 pr_warn("Symbol %s is marked as UNUSED, however this module is "
534 "using it.\n", fsa->name);
535 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030536 pr_warn("Please evaluate if this is the right api to use and "
537 "if it really is, submit a report to the linux kernel "
538 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800539 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500540 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500541#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500542
543 fsa->owner = owner;
544 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500545 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500546 return true;
547}
548
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200549static int cmp_name(const void *va, const void *vb)
550{
551 const char *a;
552 const struct kernel_symbol *b;
553 a = va; b = vb;
554 return strcmp(a, b->name);
555}
556
Rusty Russellde4d8d52011-04-19 21:49:58 +0200557static bool find_symbol_in_section(const struct symsearch *syms,
558 struct module *owner,
559 void *data)
560{
561 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200562 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200563
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200564 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
565 sizeof(struct kernel_symbol), cmp_name);
566
567 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
568 return true;
569
Rusty Russellde4d8d52011-04-19 21:49:58 +0200570 return false;
571}
572
Tim Abbott414fd312008-12-05 19:03:56 -0500573/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600574 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500575const struct kernel_symbol *find_symbol(const char *name,
576 struct module **owner,
577 const unsigned long **crc,
578 bool gplok,
579 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rusty Russelldafd0942008-07-22 19:24:25 -0500581 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Rusty Russelldafd0942008-07-22 19:24:25 -0500583 fsa.name = name;
584 fsa.gplok = gplok;
585 fsa.warn = warn;
586
Rusty Russellde4d8d52011-04-19 21:49:58 +0200587 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500588 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500589 *owner = fsa.owner;
590 if (crc)
591 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500592 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500594
Jim Cromie5e124162011-12-06 12:11:31 -0700595 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500596 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Tim Abbottc6b37802008-12-05 19:03:59 -0500598EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/* Search for module by name: must hold module_mutex. */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930601static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030602 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct module *mod;
605
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930606 module_assert_mutex();
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030609 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
610 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930611 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return mod;
613 }
614 return NULL;
615}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030616
617struct module *find_module(const char *name)
618{
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930619 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030620}
Tim Abbottc6b37802008-12-05 19:03:59 -0500621EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900624
Tejun Heo259354d2010-03-10 18:56:10 +0900625static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900626{
Tejun Heo259354d2010-03-10 18:56:10 +0900627 return mod->percpu;
628}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900629
Rusty Russell9eb76d72013-07-03 10:06:29 +0930630static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900631{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930632 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
633 unsigned long align = pcpusec->sh_addralign;
634
635 if (!pcpusec->sh_size)
636 return 0;
637
Tejun Heofbf59bc2009-02-20 16:29:08 +0900638 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800639 pr_warn("%s: per-cpu alignment %li > %li\n",
640 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900641 align = PAGE_SIZE;
642 }
643
Rusty Russell9eb76d72013-07-03 10:06:29 +0930644 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900645 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800646 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
647 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900648 return -ENOMEM;
649 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930650 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900651 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900652}
653
Tejun Heo259354d2010-03-10 18:56:10 +0900654static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900655{
Tejun Heo259354d2010-03-10 18:56:10 +0900656 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900657}
658
Rusty Russell49668682010-08-05 12:59:10 -0600659static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900660{
Rusty Russell49668682010-08-05 12:59:10 -0600661 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900662}
663
Tejun Heo259354d2010-03-10 18:56:10 +0900664static void percpu_modcopy(struct module *mod,
665 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900666{
667 int cpu;
668
669 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900670 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900671}
672
Tejun Heo10fad5e2010-03-10 18:57:54 +0900673/**
674 * is_module_percpu_address - test whether address is from module static percpu
675 * @addr: address to test
676 *
677 * Test whether @addr belongs to module static percpu area.
678 *
679 * RETURNS:
680 * %true if @addr is from module static percpu area
681 */
682bool is_module_percpu_address(unsigned long addr)
683{
684 struct module *mod;
685 unsigned int cpu;
686
687 preempt_disable();
688
689 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030690 if (mod->state == MODULE_STATE_UNFORMED)
691 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900692 if (!mod->percpu_size)
693 continue;
694 for_each_possible_cpu(cpu) {
695 void *start = per_cpu_ptr(mod->percpu, cpu);
696
697 if ((void *)addr >= start &&
698 (void *)addr < start + mod->percpu_size) {
699 preempt_enable();
700 return true;
701 }
702 }
703 }
704
705 preempt_enable();
706 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700707}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900710
Tejun Heo259354d2010-03-10 18:56:10 +0900711static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 return NULL;
714}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930715static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930717 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
718 if (info->sechdrs[info->index.pcpu].sh_size != 0)
719 return -ENOMEM;
720 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900721}
722static inline void percpu_modfree(struct module *mod)
723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
Rusty Russell49668682010-08-05 12:59:10 -0600725static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 return 0;
728}
Tejun Heo259354d2010-03-10 18:56:10 +0900729static inline void percpu_modcopy(struct module *mod,
730 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 /* pcpusec should be 0, and size of that section should be 0. */
733 BUG_ON(size != 0);
734}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900735bool is_module_percpu_address(unsigned long addr)
736{
737 return false;
738}
Tejun Heo6b588c12009-02-20 16:29:07 +0900739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#endif /* CONFIG_SMP */
741
Matt Domschc988d2b2005-06-23 22:05:15 -0700742#define MODINFO_ATTR(field) \
743static void setup_modinfo_##field(struct module *mod, const char *s) \
744{ \
745 mod->field = kstrdup(s, GFP_KERNEL); \
746} \
747static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930748 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700749{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930750 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700751} \
752static int modinfo_##field##_exists(struct module *mod) \
753{ \
754 return mod->field != NULL; \
755} \
756static void free_modinfo_##field(struct module *mod) \
757{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700758 kfree(mod->field); \
759 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700760} \
761static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900762 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700763 .show = show_modinfo_##field, \
764 .setup = setup_modinfo_##field, \
765 .test = modinfo_##field##_exists, \
766 .free = free_modinfo_##field, \
767};
768
769MODINFO_ATTR(version);
770MODINFO_ATTR(srcversion);
771
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100772static char last_unloaded_module[MODULE_NAME_LEN+1];
773
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800774#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400775
776EXPORT_TRACEPOINT_SYMBOL(module_get);
777
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030778/* MODULE_REF_BASE is the base reference count by kmodule loader. */
779#define MODULE_REF_BASE 1
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600782static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030784 /*
785 * Initialize reference counter to MODULE_REF_BASE.
786 * refcnt == 0 means module is going.
787 */
788 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600789
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700790 INIT_LIST_HEAD(&mod->source_list);
791 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030794 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600795
796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/* Does a already use b? */
800static int already_uses(struct module *a, struct module *b)
801{
802 struct module_use *use;
803
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700804 list_for_each_entry(use, &b->source_list, source_list) {
805 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700806 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 1;
808 }
809 }
Jim Cromie5e124162011-12-06 12:11:31 -0700810 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 0;
812}
813
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700814/*
815 * Module a uses b
816 * - we add 'a' as a "source", 'b' as a "target" of module use
817 * - the module_use is added to the list of 'b' sources (so
818 * 'b' can walk the list to see who sourced them), and of 'a'
819 * targets (so 'a' can see what modules it targets).
820 */
821static int add_module_usage(struct module *a, struct module *b)
822{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700823 struct module_use *use;
824
Jim Cromie5e124162011-12-06 12:11:31 -0700825 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700826 use = kmalloc(sizeof(*use), GFP_ATOMIC);
827 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800828 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700829 return -ENOMEM;
830 }
831
832 use->source = a;
833 use->target = b;
834 list_add(&use->source_list, &b->source_list);
835 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700836 return 0;
837}
838
Rusty Russell75676502010-06-05 11:17:36 -0600839/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600840int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600842 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100843
Rusty Russell9bea7f22010-06-05 11:17:37 -0600844 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700845 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700846
Rusty Russell9bea7f22010-06-05 11:17:37 -0600847 /* If module isn't available, we fail. */
848 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500849 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600850 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700852 err = add_module_usage(a, b);
853 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600855 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600859EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861/* Clear the unload stuff of the module. */
862static void module_unload_free(struct module *mod)
863{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700864 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Rusty Russell75676502010-06-05 11:17:36 -0600866 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700867 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
868 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700869 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700870 module_put(i);
871 list_del(&use->source_list);
872 list_del(&use->target_list);
873 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Rusty Russell75676502010-06-05 11:17:36 -0600875 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800879static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 int ret = (flags & O_TRUNC);
882 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030883 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return ret;
885}
886#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800887static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 return 0;
890}
891#endif /* CONFIG_MODULE_FORCE_UNLOAD */
892
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030893/* Try to release refcount of module, 0 means success. */
894static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030896 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030898 /* Try to decrement refcnt which we set at loading */
899 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
900 BUG_ON(ret < 0);
901 if (ret)
902 /* Someone can put this right now, recover with checking */
903 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static int try_stop_module(struct module *mod, int flags, int *forced)
909{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030910 /* If it's not unused, quit unless we're forcing. */
911 if (try_release_module_ref(mod) != 0) {
912 *forced = try_force_unload(flags);
913 if (!(*forced))
914 return -EWOULDBLOCK;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030917 /* Mark it as dying. */
918 mod->state = MODULE_STATE_GOING;
919
920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Rusty Russelld5db1392015-01-22 11:13:14 +1030923/**
924 * module_refcount - return the refcount or -1 if unloading
925 *
926 * @mod: the module we're checking
927 *
928 * Returns:
929 * -1 if the module is in the process of unloading
930 * otherwise the number of references in the kernel to the module
931 */
932int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Rusty Russelld5db1392015-01-22 11:13:14 +1030934 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936EXPORT_SYMBOL(module_refcount);
937
938/* This exists whether we can unload or not */
939static void free_module(struct module *mod);
940
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100941SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
942 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800945 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int ret, forced = 0;
947
Kees Cook3d433212009-04-02 15:49:29 -0700948 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800949 return -EPERM;
950
951 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
952 return -EFAULT;
953 name[MODULE_NAME_LEN-1] = '\0';
954
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200955 if (mutex_lock_interruptible(&module_mutex) != 0)
956 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 mod = find_module(name);
959 if (!mod) {
960 ret = -ENOENT;
961 goto out;
962 }
963
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700964 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Other modules depend on us: get rid of them first. */
966 ret = -EWOULDBLOCK;
967 goto out;
968 }
969
970 /* Doing init or already dying? */
971 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930972 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700973 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 ret = -EBUSY;
975 goto out;
976 }
977
978 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700979 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800980 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!forced) {
982 /* This module can't be removed */
983 ret = -EBUSY;
984 goto out;
985 }
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* Stop the machine so refcounts can't move and disable module. */
989 ret = try_stop_module(mod, flags, &forced);
990 if (ret != 0)
991 goto out;
992
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200993 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300994 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200995 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200997 blocking_notifier_call_chain(&module_notify_list,
998 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800999 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -06001000
Arjan van de Vene14af7e2008-01-25 21:08:33 +01001001 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -05001002 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Rusty Russell75676502010-06-05 11:17:36 -06001004 free_module(mod);
1005 return 0;
1006out:
Ashutosh Naik6389a382006-03-23 03:00:46 -08001007 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return ret;
1009}
1010
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001011static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct module_use *use;
1014 int printed_something = 0;
1015
Rusty Russelld5db1392015-01-22 11:13:14 +10301016 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Ionut Alexa6da0b562014-11-10 09:31:29 +10301018 /*
1019 * Always include a trailing , so userspace can differentiate
1020 * between this and the old multi-field proc format.
1021 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001022 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -07001024 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (mod->init != NULL && mod->exit == NULL) {
1028 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301029 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301033 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036void __symbol_put(const char *symbol)
1037{
1038 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Rusty Russell24da1cb2007-07-15 23:41:46 -07001040 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001041 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 BUG();
1043 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001044 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046EXPORT_SYMBOL(__symbol_put);
1047
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301048/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049void symbol_put_addr(void *addr)
1050{
Trent Piepho5e376612006-05-15 09:44:06 -07001051 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301052 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301054 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001055 return;
1056
Rusty Russella6e6abd2009-03-31 13:05:31 -06001057 /* module_text_address is safe here: we're supposed to have reference
1058 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301059 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001060 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001061 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063EXPORT_SYMBOL_GPL(symbol_put_addr);
1064
1065static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301066 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Rusty Russelld5db1392015-01-22 11:13:14 +10301068 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Kay Sieverscca3e702012-01-13 09:32:15 +10301071static struct module_attribute modinfo_refcnt =
1072 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Steven Rostedtd53799b2012-03-26 12:50:52 +10301074void __module_get(struct module *module)
1075{
1076 if (module) {
1077 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301078 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301079 trace_module_get(module, _RET_IP_);
1080 preempt_enable();
1081 }
1082}
1083EXPORT_SYMBOL(__module_get);
1084
1085bool try_module_get(struct module *module)
1086{
1087 bool ret = true;
1088
1089 if (module) {
1090 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301091 /* Note: here, we can fail to get a reference */
1092 if (likely(module_is_live(module) &&
1093 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301094 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301095 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301096 ret = false;
1097
1098 preempt_enable();
1099 }
1100 return ret;
1101}
1102EXPORT_SYMBOL(try_module_get);
1103
Al Virof6a57032006-10-18 01:47:25 -04001104void module_put(struct module *module)
1105{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301106 int ret;
1107
Al Virof6a57032006-10-18 01:47:25 -04001108 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001109 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301110 ret = atomic_dec_if_positive(&module->refcnt);
1111 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001112 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001113 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001114 }
1115}
1116EXPORT_SYMBOL(module_put);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001119static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301122 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125static inline void module_unload_free(struct module *mod)
1126{
1127}
1128
Rusty Russell9bea7f22010-06-05 11:17:37 -06001129int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001131 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001133EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001135static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139#endif /* CONFIG_MODULE_UNLOAD */
1140
Kevin Winchester53999bf2012-01-15 19:32:55 -04001141static size_t module_flags_taint(struct module *mod, char *buf)
1142{
1143 size_t l = 0;
1144
1145 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1146 buf[l++] = 'P';
1147 if (mod->taints & (1 << TAINT_OOT_MODULE))
1148 buf[l++] = 'O';
1149 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1150 buf[l++] = 'F';
1151 if (mod->taints & (1 << TAINT_CRAP))
1152 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301153 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301154 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001155 /*
1156 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001157 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001158 * apply to modules.
1159 */
1160 return l;
1161}
1162
Kay Sievers1f717402006-11-24 12:15:25 +01001163static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301164 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001165{
1166 const char *state = "unknown";
1167
Kay Sievers4befb022011-07-24 22:06:04 +09301168 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001169 case MODULE_STATE_LIVE:
1170 state = "live";
1171 break;
1172 case MODULE_STATE_COMING:
1173 state = "coming";
1174 break;
1175 case MODULE_STATE_GOING:
1176 state = "going";
1177 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301178 default:
1179 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001180 }
1181 return sprintf(buffer, "%s\n", state);
1182}
1183
Kay Sieverscca3e702012-01-13 09:32:15 +10301184static struct module_attribute modinfo_initstate =
1185 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001186
Kay Sievers88bfa322011-07-24 22:06:04 +09301187static ssize_t store_uevent(struct module_attribute *mattr,
1188 struct module_kobject *mk,
1189 const char *buffer, size_t count)
1190{
1191 enum kobject_action action;
1192
1193 if (kobject_action_type(buffer, count, &action) == 0)
1194 kobject_uevent(&mk->kobj, action);
1195 return count;
1196}
1197
Kay Sieverscca3e702012-01-13 09:32:15 +10301198struct module_attribute module_uevent =
1199 __ATTR(uevent, 0200, NULL, store_uevent);
1200
1201static ssize_t show_coresize(struct module_attribute *mattr,
1202 struct module_kobject *mk, char *buffer)
1203{
1204 return sprintf(buffer, "%u\n", mk->mod->core_size);
1205}
1206
1207static struct module_attribute modinfo_coresize =
1208 __ATTR(coresize, 0444, show_coresize, NULL);
1209
1210static ssize_t show_initsize(struct module_attribute *mattr,
1211 struct module_kobject *mk, char *buffer)
1212{
1213 return sprintf(buffer, "%u\n", mk->mod->init_size);
1214}
1215
1216static struct module_attribute modinfo_initsize =
1217 __ATTR(initsize, 0444, show_initsize, NULL);
1218
1219static ssize_t show_taint(struct module_attribute *mattr,
1220 struct module_kobject *mk, char *buffer)
1221{
1222 size_t l;
1223
1224 l = module_flags_taint(mk->mod, buffer);
1225 buffer[l++] = '\n';
1226 return l;
1227}
1228
1229static struct module_attribute modinfo_taint =
1230 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301231
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001232static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301233 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001234 &modinfo_version,
1235 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301236 &modinfo_initstate,
1237 &modinfo_coresize,
1238 &modinfo_initsize,
1239 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001240#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301241 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001242#endif
1243 NULL,
1244};
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static const char vermagic[] = VERMAGIC_STRING;
1247
Rusty Russellc6e665c2009-03-31 13:05:33 -06001248static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001249{
1250#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001251 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001252 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301253 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001254 return 0;
1255#else
1256 return -ENOEXEC;
1257#endif
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001261/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1262static unsigned long maybe_relocated(unsigned long crc,
1263 const struct module *crc_owner)
1264{
1265#ifdef ARCH_RELOCATES_KCRCTAB
1266 if (crc_owner == NULL)
1267 return crc - (unsigned long)reloc_start;
1268#endif
1269 return crc;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static int check_version(Elf_Shdr *sechdrs,
1273 unsigned int versindex,
1274 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301275 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001276 const unsigned long *crc,
1277 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 unsigned int i, num_versions;
1280 struct modversion_info *versions;
1281
1282 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1283 if (!crc)
1284 return 1;
1285
Rusty Russella5dd6972008-05-09 16:24:21 +10001286 /* No versions at all? modprobe --force does this. */
1287 if (versindex == 0)
1288 return try_to_force_load(mod, symname) == 0;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 versions = (void *) sechdrs[versindex].sh_addr;
1291 num_versions = sechdrs[versindex].sh_size
1292 / sizeof(struct modversion_info);
1293
1294 for (i = 0; i < num_versions; i++) {
1295 if (strcmp(versions[i].name, symname) != 0)
1296 continue;
1297
Rusty Russelld4703ae2009-12-15 16:28:32 -06001298 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001300 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001301 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001302 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001304
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001305 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001306 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001307
1308bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301309 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001310 mod->name, symname);
1311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1315 unsigned int versindex,
1316 struct module *mod)
1317{
1318 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301320 /*
1321 * Since this should be found in kernel (which can't be removed), no
1322 * locking is necessary -- use preempt_disable() to placate lockdep.
1323 */
1324 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301325 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301326 &crc, true, false)) {
1327 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301329 }
1330 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301331 return check_version(sechdrs, versindex,
1332 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001333 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Rusty Russell91e37a72008-05-09 16:25:28 +10001336/* First part is kernel version, which we ignore if module has crcs. */
1337static inline int same_magic(const char *amagic, const char *bmagic,
1338 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Rusty Russell91e37a72008-05-09 16:25:28 +10001340 if (has_crcs) {
1341 amagic += strcspn(amagic, " ");
1342 bmagic += strcspn(bmagic, " ");
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return strcmp(amagic, bmagic) == 0;
1345}
1346#else
1347static inline int check_version(Elf_Shdr *sechdrs,
1348 unsigned int versindex,
1349 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301350 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001351 const unsigned long *crc,
1352 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 return 1;
1355}
1356
1357static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1358 unsigned int versindex,
1359 struct module *mod)
1360{
1361 return 1;
1362}
1363
Rusty Russell91e37a72008-05-09 16:25:28 +10001364static inline int same_magic(const char *amagic, const char *bmagic,
1365 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 return strcmp(amagic, bmagic) == 0;
1368}
1369#endif /* CONFIG_MODVERSIONS */
1370
Rusty Russell75676502010-06-05 11:17:36 -06001371/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001372static const struct kernel_symbol *resolve_symbol(struct module *mod,
1373 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001374 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001375 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001378 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001380 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301382 /*
1383 * The module_mutex should not be a heavily contended lock;
1384 * if we get the occasional sleep here, we'll go an extra iteration
1385 * in the wait_event_interruptible(), which is harmless.
1386 */
1387 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001388 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001389 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001390 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001391 if (!sym)
1392 goto unlock;
1393
Rusty Russell49668682010-08-05 12:59:10 -06001394 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1395 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001396 sym = ERR_PTR(-EINVAL);
1397 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001399
1400 err = ref_module(mod, owner);
1401 if (err) {
1402 sym = ERR_PTR(err);
1403 goto getname;
1404 }
1405
1406getname:
1407 /* We must make copy under the lock if we failed to get ref. */
1408 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1409unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001410 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001411 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Rusty Russell49668682010-08-05 12:59:10 -06001414static const struct kernel_symbol *
1415resolve_symbol_wait(struct module *mod,
1416 const struct load_info *info,
1417 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001418{
1419 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001420 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001421
1422 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001423 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1424 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001425 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001426 pr_warn("%s: gave up waiting for init of module %s.\n",
1427 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001428 }
1429 return ksym;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/*
1433 * /sys/module/foo/sections stuff
1434 * J. Corbet <corbet@lwn.net>
1435 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001436#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001437
Rusty Russell8f6d0372010-08-05 12:59:09 -06001438#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001439static inline bool sect_empty(const Elf_Shdr *sect)
1440{
1441 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1442}
1443
Ionut Alexa6da0b562014-11-10 09:31:29 +10301444struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001445 struct module_attribute mattr;
1446 char *name;
1447 unsigned long address;
1448};
1449
Ionut Alexa6da0b562014-11-10 09:31:29 +10301450struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001451 struct attribute_group grp;
1452 unsigned int nsections;
1453 struct module_sect_attr attrs[0];
1454};
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301457 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct module_sect_attr *sattr =
1460 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001461 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001464static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1465{
Rusty Russella58730c2008-03-13 09:03:44 +00001466 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001467
1468 for (section = 0; section < sect_attrs->nsections; section++)
1469 kfree(sect_attrs->attrs[section].name);
1470 kfree(sect_attrs);
1471}
1472
Rusty Russell8f6d0372010-08-05 12:59:09 -06001473static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 unsigned int nloaded = 0, i, size[2];
1476 struct module_sect_attrs *sect_attrs;
1477 struct module_sect_attr *sattr;
1478 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001481 for (i = 0; i < info->hdr->e_shnum; i++)
1482 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 nloaded++;
1484 size[0] = ALIGN(sizeof(*sect_attrs)
1485 + nloaded * sizeof(sect_attrs->attrs[0]),
1486 sizeof(sect_attrs->grp.attrs[0]));
1487 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001488 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1489 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return;
1491
1492 /* Setup section attributes. */
1493 sect_attrs->grp.name = "sections";
1494 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1495
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001496 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sattr = &sect_attrs->attrs[0];
1498 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001499 for (i = 0; i < info->hdr->e_shnum; i++) {
1500 Elf_Shdr *sec = &info->sechdrs[i];
1501 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001502 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001503 sattr->address = sec->sh_addr;
1504 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001505 GFP_KERNEL);
1506 if (sattr->name == NULL)
1507 goto out;
1508 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001509 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 sattr->mattr.show = module_sect_show;
1511 sattr->mattr.store = NULL;
1512 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sattr->mattr.attr.mode = S_IRUGO;
1514 *(gattr++) = &(sattr++)->mattr.attr;
1515 }
1516 *gattr = NULL;
1517
1518 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1519 goto out;
1520
1521 mod->sect_attrs = sect_attrs;
1522 return;
1523 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001524 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527static void remove_sect_attrs(struct module *mod)
1528{
1529 if (mod->sect_attrs) {
1530 sysfs_remove_group(&mod->mkobj.kobj,
1531 &mod->sect_attrs->grp);
1532 /* We are positive that no one is using any sect attrs
1533 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001534 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 mod->sect_attrs = NULL;
1536 }
1537}
1538
Roland McGrath6d760132007-10-16 23:26:40 -07001539/*
1540 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1541 */
1542
1543struct module_notes_attrs {
1544 struct kobject *dir;
1545 unsigned int notes;
1546 struct bin_attribute attrs[0];
1547};
1548
Chris Wright2c3c8be2010-05-12 18:28:57 -07001549static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001550 struct bin_attribute *bin_attr,
1551 char *buf, loff_t pos, size_t count)
1552{
1553 /*
1554 * The caller checked the pos and count against our size.
1555 */
1556 memcpy(buf, bin_attr->private + pos, count);
1557 return count;
1558}
1559
1560static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1561 unsigned int i)
1562{
1563 if (notes_attrs->dir) {
1564 while (i-- > 0)
1565 sysfs_remove_bin_file(notes_attrs->dir,
1566 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001567 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001568 }
1569 kfree(notes_attrs);
1570}
1571
Rusty Russell8f6d0372010-08-05 12:59:09 -06001572static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001573{
1574 unsigned int notes, loaded, i;
1575 struct module_notes_attrs *notes_attrs;
1576 struct bin_attribute *nattr;
1577
Ingo Molnarea6bff32009-08-28 10:44:56 +02001578 /* failed to create section attributes, so can't create notes */
1579 if (!mod->sect_attrs)
1580 return;
1581
Roland McGrath6d760132007-10-16 23:26:40 -07001582 /* Count notes sections and allocate structures. */
1583 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001584 for (i = 0; i < info->hdr->e_shnum; i++)
1585 if (!sect_empty(&info->sechdrs[i]) &&
1586 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001587 ++notes;
1588
1589 if (notes == 0)
1590 return;
1591
1592 notes_attrs = kzalloc(sizeof(*notes_attrs)
1593 + notes * sizeof(notes_attrs->attrs[0]),
1594 GFP_KERNEL);
1595 if (notes_attrs == NULL)
1596 return;
1597
1598 notes_attrs->notes = notes;
1599 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001600 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1601 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001602 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001603 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001604 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001605 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1606 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001607 nattr->size = info->sechdrs[i].sh_size;
1608 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001609 nattr->read = module_notes_read;
1610 ++nattr;
1611 }
1612 ++loaded;
1613 }
1614
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001615 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001616 if (!notes_attrs->dir)
1617 goto out;
1618
1619 for (i = 0; i < notes; ++i)
1620 if (sysfs_create_bin_file(notes_attrs->dir,
1621 &notes_attrs->attrs[i]))
1622 goto out;
1623
1624 mod->notes_attrs = notes_attrs;
1625 return;
1626
1627 out:
1628 free_notes_attrs(notes_attrs, i);
1629}
1630
1631static void remove_notes_attrs(struct module *mod)
1632{
1633 if (mod->notes_attrs)
1634 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001638
Rusty Russell8f6d0372010-08-05 12:59:09 -06001639static inline void add_sect_attrs(struct module *mod,
1640 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642}
1643
1644static inline void remove_sect_attrs(struct module *mod)
1645{
1646}
Roland McGrath6d760132007-10-16 23:26:40 -07001647
Rusty Russell8f6d0372010-08-05 12:59:09 -06001648static inline void add_notes_attrs(struct module *mod,
1649 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001650{
1651}
1652
1653static inline void remove_notes_attrs(struct module *mod)
1654{
1655}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001656#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001658static void add_usage_links(struct module *mod)
1659{
1660#ifdef CONFIG_MODULE_UNLOAD
1661 struct module_use *use;
1662 int nowarn;
1663
Rusty Russell75676502010-06-05 11:17:36 -06001664 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001665 list_for_each_entry(use, &mod->target_list, target_list) {
1666 nowarn = sysfs_create_link(use->target->holders_dir,
1667 &mod->mkobj.kobj, mod->name);
1668 }
Rusty Russell75676502010-06-05 11:17:36 -06001669 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001670#endif
1671}
1672
1673static void del_usage_links(struct module *mod)
1674{
1675#ifdef CONFIG_MODULE_UNLOAD
1676 struct module_use *use;
1677
Rusty Russell75676502010-06-05 11:17:36 -06001678 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001679 list_for_each_entry(use, &mod->target_list, target_list)
1680 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001681 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001682#endif
1683}
1684
Rusty Russell6407ebb22010-06-05 11:17:36 -06001685static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001686{
1687 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001688 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001689 int error = 0;
1690 int i;
1691
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001692 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1693 (ARRAY_SIZE(modinfo_attrs) + 1)),
1694 GFP_KERNEL);
1695 if (!mod->modinfo_attrs)
1696 return -ENOMEM;
1697
1698 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001699 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1700 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001701 (attr->test && attr->test(mod))) {
1702 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001703 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301704 error = sysfs_create_file(&mod->mkobj.kobj,
1705 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001706 ++temp_attr;
1707 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001708 }
1709 return error;
1710}
1711
Rusty Russell6407ebb22010-06-05 11:17:36 -06001712static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001713{
1714 struct module_attribute *attr;
1715 int i;
1716
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001717 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1718 /* pick a field to test for end of list */
1719 if (!attr->attr.name)
1720 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301721 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001722 if (attr->free)
1723 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001724 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001725 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Li Zhong942e4432013-09-03 16:33:57 +09301728static void mod_kobject_put(struct module *mod)
1729{
1730 DECLARE_COMPLETION_ONSTACK(c);
1731 mod->mkobj.kobj_completion = &c;
1732 kobject_put(&mod->mkobj.kobj);
1733 wait_for_completion(&c);
1734}
1735
Rusty Russell6407ebb22010-06-05 11:17:36 -06001736static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001739 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001741 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001742 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001743 err = -EINVAL;
1744 goto out;
1745 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001746
1747 kobj = kset_find_obj(module_kset, mod->name);
1748 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001749 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001750 kobject_put(kobj);
1751 err = -EINVAL;
1752 goto out;
1753 }
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001756
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001757 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1758 mod->mkobj.kobj.kset = module_kset;
1759 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1760 "%s", mod->name);
1761 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301762 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001763
Kay Sievers97c146e2007-11-29 23:46:11 +01001764 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001765out:
1766 return err;
1767}
1768
Rusty Russell6407ebb22010-06-05 11:17:36 -06001769static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001770 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001771 struct kernel_param *kparam,
1772 unsigned int num_params)
1773{
1774 int err;
1775
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001776 err = mod_sysfs_init(mod);
1777 if (err)
1778 goto out;
1779
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001780 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001781 if (!mod->holders_dir) {
1782 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001783 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001784 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 err = module_param_sysfs_setup(mod, kparam, num_params);
1787 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001788 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Matt Domschc988d2b2005-06-23 22:05:15 -07001790 err = module_add_modinfo_attrs(mod);
1791 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001792 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001793
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001794 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001795 add_sect_attrs(mod, info);
1796 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001797
Kay Sieverse17e0f52006-11-24 12:15:25 +01001798 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return 0;
1800
Kay Sieverse17e0f52006-11-24 12:15:25 +01001801out_unreg_param:
1802 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001803out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001804 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001805out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301806 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001807out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001810
1811static void mod_sysfs_fini(struct module *mod)
1812{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001813 remove_notes_attrs(mod);
1814 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301815 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001816}
1817
Rusty Russell8f6d0372010-08-05 12:59:09 -06001818#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001819
Rusty Russell8f6d0372010-08-05 12:59:09 -06001820static int mod_sysfs_setup(struct module *mod,
1821 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001822 struct kernel_param *kparam,
1823 unsigned int num_params)
1824{
1825 return 0;
1826}
1827
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001828static void mod_sysfs_fini(struct module *mod)
1829{
1830}
1831
Rusty Russell36b03602010-08-05 12:59:09 -06001832static void module_remove_modinfo_attrs(struct module *mod)
1833{
1834}
1835
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001836static void del_usage_links(struct module *mod)
1837{
1838}
1839
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001840#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Rusty Russell36b03602010-08-05 12:59:09 -06001842static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001844 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001845 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001847 kobject_put(mod->mkobj.drivers_dir);
1848 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001849 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
matthieu castet84e1c6b2010-11-16 22:35:16 +01001852#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1853/*
1854 * LKM RO/NX protection: protect module's text/ro-data
1855 * from modification and any data from execution.
1856 */
1857void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1858{
1859 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1860 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1861
1862 if (end_pfn > begin_pfn)
1863 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1864}
1865
1866static void set_section_ro_nx(void *base,
1867 unsigned long text_size,
1868 unsigned long ro_size,
1869 unsigned long total_size)
1870{
1871 /* begin and end PFNs of the current subsection */
1872 unsigned long begin_pfn;
1873 unsigned long end_pfn;
1874
1875 /*
1876 * Set RO for module text and RO-data:
1877 * - Always protect first page.
1878 * - Do not protect last partial page.
1879 */
1880 if (ro_size > 0)
1881 set_page_attributes(base, base + ro_size, set_memory_ro);
1882
1883 /*
1884 * Set NX permissions for module data:
1885 * - Do not protect first partial page.
1886 * - Always protect last page.
1887 */
1888 if (total_size > text_size) {
1889 begin_pfn = PFN_UP((unsigned long)base + text_size);
1890 end_pfn = PFN_UP((unsigned long)base + total_size);
1891 if (end_pfn > begin_pfn)
1892 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1893 }
1894}
1895
Jan Glauber01526ed2011-05-19 16:55:26 -06001896static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001897{
Jan Glauber01526ed2011-05-19 16:55:26 -06001898 set_page_attributes(mod->module_core + mod->core_text_size,
1899 mod->module_core + mod->core_size,
1900 set_memory_x);
1901 set_page_attributes(mod->module_core,
1902 mod->module_core + mod->core_ro_size,
1903 set_memory_rw);
1904}
1905
1906static void unset_module_init_ro_nx(struct module *mod)
1907{
1908 set_page_attributes(mod->module_init + mod->init_text_size,
1909 mod->module_init + mod->init_size,
1910 set_memory_x);
1911 set_page_attributes(mod->module_init,
1912 mod->module_init + mod->init_ro_size,
1913 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001914}
1915
1916/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001917void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001918{
1919 struct module *mod;
1920
1921 mutex_lock(&module_mutex);
1922 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301923 if (mod->state == MODULE_STATE_UNFORMED)
1924 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001925 if ((mod->module_core) && (mod->core_text_size)) {
1926 set_page_attributes(mod->module_core,
1927 mod->module_core + mod->core_text_size,
1928 set_memory_rw);
1929 }
1930 if ((mod->module_init) && (mod->init_text_size)) {
1931 set_page_attributes(mod->module_init,
1932 mod->module_init + mod->init_text_size,
1933 set_memory_rw);
1934 }
1935 }
1936 mutex_unlock(&module_mutex);
1937}
1938
1939/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001940void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001941{
1942 struct module *mod;
1943
1944 mutex_lock(&module_mutex);
1945 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301946 if (mod->state == MODULE_STATE_UNFORMED)
1947 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001948 if ((mod->module_core) && (mod->core_text_size)) {
1949 set_page_attributes(mod->module_core,
1950 mod->module_core + mod->core_text_size,
1951 set_memory_ro);
1952 }
1953 if ((mod->module_init) && (mod->init_text_size)) {
1954 set_page_attributes(mod->module_init,
1955 mod->module_init + mod->init_text_size,
1956 set_memory_ro);
1957 }
1958 }
1959 mutex_unlock(&module_mutex);
1960}
1961#else
1962static 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 -06001963static void unset_module_core_ro_nx(struct module *mod) { }
1964static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001965#endif
1966
Rusty Russellbe1f2212015-01-20 09:07:05 +10301967void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001968{
1969 vfree(module_region);
1970}
1971
1972void __weak module_arch_cleanup(struct module *mod)
1973{
1974}
1975
Rusty Russelld453cde2015-01-20 09:07:04 +10301976void __weak module_arch_freeing_init(struct module *mod)
1977{
1978}
1979
Rusty Russell75676502010-06-05 11:17:36 -06001980/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static void free_module(struct module *mod)
1982{
Li Zefan7ead8b82009-08-17 16:56:28 +08001983 trace_module_free(mod);
1984
Rusty Russell36b03602010-08-05 12:59:09 -06001985 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Rusty Russell944a1fa2013-04-17 13:20:03 +09301987 /* We leave it in list to prevent duplicate loads, but make sure
1988 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301989 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301990 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301991 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301992
Jason Baronb82bab4b2010-07-27 13:18:01 -07001993 /* Remove dynamic debug info */
1994 ddebug_remove_module(mod->name);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* Arch-specific cleanup. */
1997 module_arch_cleanup(mod);
1998
1999 /* Module unload stuff */
2000 module_unload_free(mod);
2001
Rusty Russelle180a6b2009-03-31 13:05:29 -06002002 /* Free any allocated parameters. */
2003 destroy_params(mod->kp, mod->num_kp);
2004
Rusty Russell944a1fa2013-04-17 13:20:03 +09302005 /* Now we can delete it from the lists */
2006 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302007 /* Unlink carefully: kallsyms could be walking list. */
2008 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09302009 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10302010 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10302011 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09302012 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2013 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09302014 mutex_unlock(&module_mutex);
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06002017 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10302018 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302019 module_memfree(mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09002021 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002022
Peter Zijlstra35a93932015-02-26 16:23:11 +01002023 /* Free lock-classes; relies on the preceding sync_rcu(). */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002024 lockdep_free_key_range(mod->module_core, mod->core_size);
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06002027 unset_module_core_ro_nx(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302028 module_memfree(mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002029
2030#ifdef CONFIG_MPU
2031 update_protections(current->mm);
2032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
2035void *__symbol_get(const char *symbol)
2036{
2037 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002038 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Rusty Russell24da1cb2007-07-15 23:41:46 -07002040 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002041 sym = find_symbol(symbol, &owner, NULL, true, true);
2042 if (sym && strong_try_module_get(owner))
2043 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002044 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Tim Abbott414fd312008-12-05 19:03:56 -05002046 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048EXPORT_SYMBOL_GPL(__symbol_get);
2049
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002050/*
2051 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002052 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002053 *
2054 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002055 */
2056static int verify_export_symbols(struct module *mod)
2057{
Rusty Russellb2111042008-05-01 21:15:00 -05002058 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002059 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002060 const struct kernel_symbol *s;
2061 struct {
2062 const struct kernel_symbol *sym;
2063 unsigned int num;
2064 } arr[] = {
2065 { mod->syms, mod->num_syms },
2066 { mod->gpl_syms, mod->num_gpl_syms },
2067 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002068#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002069 { mod->unused_syms, mod->num_unused_syms },
2070 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002071#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002072 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002073
Rusty Russellb2111042008-05-01 21:15:00 -05002074 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2075 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002076 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002077 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002078 " (owned by %s)\n",
2079 mod->name, s->name, module_name(owner));
2080 return -ENOEXEC;
2081 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002082 }
Rusty Russellb2111042008-05-01 21:15:00 -05002083 }
2084 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002085}
2086
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002087/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002088static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Rusty Russell49668682010-08-05 12:59:10 -06002090 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2091 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002093 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002095 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Rusty Russell49668682010-08-05 12:59:10 -06002097 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2098 const char *name = info->strtab + sym[i].st_name;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 switch (sym[i].st_shndx) {
2101 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002102 /* Ignore common symbols */
2103 if (!strncmp(name, "__gnu_lto", 9))
2104 break;
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* We compiled with -fno-common. These are not
2107 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002108 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302109 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 mod->name);
2111 ret = -ENOEXEC;
2112 break;
2113
2114 case SHN_ABS:
2115 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002116 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 (long)sym[i].st_value);
2118 break;
2119
2120 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002121 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002123 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002124 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002126 }
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002129 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 break;
2131
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002132 pr_warn("%s: Unknown symbol %s (err %li)\n",
2133 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002134 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 break;
2136
2137 default:
2138 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002139 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002140 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 else
Rusty Russell49668682010-08-05 12:59:10 -06002142 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 sym[i].st_value += secbase;
2144 break;
2145 }
2146 }
2147
2148 return ret;
2149}
2150
Rusty Russell49668682010-08-05 12:59:10 -06002151static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002152{
2153 unsigned int i;
2154 int err = 0;
2155
2156 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002157 for (i = 1; i < info->hdr->e_shnum; i++) {
2158 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002159
2160 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002161 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002162 continue;
2163
2164 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002165 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002166 continue;
2167
Rusty Russell49668682010-08-05 12:59:10 -06002168 if (info->sechdrs[i].sh_type == SHT_REL)
2169 err = apply_relocate(info->sechdrs, info->strtab,
2170 info->index.sym, i, mod);
2171 else if (info->sechdrs[i].sh_type == SHT_RELA)
2172 err = apply_relocate_add(info->sechdrs, info->strtab,
2173 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002174 if (err < 0)
2175 break;
2176 }
2177 return err;
2178}
2179
Helge Deller088af9a2008-12-31 12:31:18 +01002180/* Additional bytes needed by arch in front of individual sections */
2181unsigned int __weak arch_mod_section_prepend(struct module *mod,
2182 unsigned int section)
2183{
2184 /* default implementation just returns zero */
2185 return 0;
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002189static long get_offset(struct module *mod, unsigned int *size,
2190 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 long ret;
2193
Helge Deller088af9a2008-12-31 12:31:18 +01002194 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2196 *size = ret + sechdr->sh_size;
2197 return ret;
2198}
2199
2200/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2201 might -- code, read-only data, read-write data, small data. Tally
2202 sizes, and place the offsets into sh_entsize fields: high bit means it
2203 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002204static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 static unsigned long const masks[][2] = {
2207 /* NOTE: all executable code must be the first section
2208 * in this array; otherwise modify the text_size
2209 * finder in the two loops below */
2210 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2211 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2212 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2213 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2214 };
2215 unsigned int m, i;
2216
Rusty Russell49668682010-08-05 12:59:10 -06002217 for (i = 0; i < info->hdr->e_shnum; i++)
2218 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Jim Cromie5e124162011-12-06 12:11:31 -07002220 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002222 for (i = 0; i < info->hdr->e_shnum; ++i) {
2223 Elf_Shdr *s = &info->sechdrs[i];
2224 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2227 || (s->sh_flags & masks[m][1])
2228 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002229 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002231 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002232 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002234 switch (m) {
2235 case 0: /* executable */
2236 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002238 break;
2239 case 1: /* RO: text and ro-data */
2240 mod->core_size = debug_align(mod->core_size);
2241 mod->core_ro_size = mod->core_size;
2242 break;
2243 case 3: /* whole core */
2244 mod->core_size = debug_align(mod->core_size);
2245 break;
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Jim Cromie5e124162011-12-06 12:11:31 -07002249 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002251 for (i = 0; i < info->hdr->e_shnum; ++i) {
2252 Elf_Shdr *s = &info->sechdrs[i];
2253 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2256 || (s->sh_flags & masks[m][1])
2257 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002258 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002260 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002262 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002264 switch (m) {
2265 case 0: /* executable */
2266 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002268 break;
2269 case 1: /* RO: text and ro-data */
2270 mod->init_size = debug_align(mod->init_size);
2271 mod->init_ro_size = mod->init_size;
2272 break;
2273 case 3: /* whole init */
2274 mod->init_size = debug_align(mod->init_size);
2275 break;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280static void set_license(struct module *mod, const char *license)
2281{
2282 if (!license)
2283 license = "unspecified";
2284
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002285 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002286 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002287 pr_warn("%s: module license '%s' taints kernel.\n",
2288 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302289 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2290 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292}
2293
2294/* Parse tag=value strings from .modinfo section */
2295static char *next_string(char *string, unsigned long *secsize)
2296{
2297 /* Skip non-zero chars */
2298 while (string[0]) {
2299 string++;
2300 if ((*secsize)-- <= 1)
2301 return NULL;
2302 }
2303
2304 /* Skip any zero padding. */
2305 while (!string[0]) {
2306 string++;
2307 if ((*secsize)-- <= 1)
2308 return NULL;
2309 }
2310 return string;
2311}
2312
Rusty Russell49668682010-08-05 12:59:10 -06002313static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 char *p;
2316 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002317 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2318 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Rusty Russell49668682010-08-05 12:59:10 -06002320 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2322 return p + taglen + 1;
2323 }
2324 return NULL;
2325}
2326
Rusty Russell49668682010-08-05 12:59:10 -06002327static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002328{
2329 struct module_attribute *attr;
2330 int i;
2331
2332 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2333 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002334 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002335 }
2336}
Matt Domschc988d2b2005-06-23 22:05:15 -07002337
Rusty Russella263f772009-09-25 00:32:58 -06002338static void free_modinfo(struct module *mod)
2339{
2340 struct module_attribute *attr;
2341 int i;
2342
2343 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2344 if (attr->free)
2345 attr->free(mod);
2346 }
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002350
2351/* lookup symbol in given range of kernel_symbols */
2352static const struct kernel_symbol *lookup_symbol(const char *name,
2353 const struct kernel_symbol *start,
2354 const struct kernel_symbol *stop)
2355{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002356 return bsearch(name, start, stop - start,
2357 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002358}
2359
Tim Abbottca4787b2009-01-05 08:40:10 -06002360static int is_exported(const char *name, unsigned long value,
2361 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Tim Abbottca4787b2009-01-05 08:40:10 -06002363 const struct kernel_symbol *ks;
2364 if (!mod)
2365 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002366 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002367 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2368 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002372static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Rusty Russelleded41c2010-08-05 12:59:07 -06002374 const Elf_Shdr *sechdrs = info->sechdrs;
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2377 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2378 return 'v';
2379 else
2380 return 'w';
2381 }
2382 if (sym->st_shndx == SHN_UNDEF)
2383 return 'U';
2384 if (sym->st_shndx == SHN_ABS)
2385 return 'a';
2386 if (sym->st_shndx >= SHN_LORESERVE)
2387 return '?';
2388 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2389 return 't';
2390 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2391 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2392 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2393 return 'r';
2394 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2395 return 'g';
2396 else
2397 return 'd';
2398 }
2399 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2400 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2401 return 's';
2402 else
2403 return 'b';
2404 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002405 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2406 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return '?';
2410}
2411
Jan Beulich4a496222009-07-06 14:50:42 +01002412static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Ionut Alexa6da0b562014-11-10 09:31:29 +10302413 unsigned int shnum)
Jan Beulich4a496222009-07-06 14:50:42 +01002414{
2415 const Elf_Shdr *sec;
2416
2417 if (src->st_shndx == SHN_UNDEF
2418 || src->st_shndx >= shnum
2419 || !src->st_name)
2420 return false;
2421
2422 sec = sechdrs + src->st_shndx;
2423 if (!(sec->sh_flags & SHF_ALLOC)
2424#ifndef CONFIG_KALLSYMS_ALL
2425 || !(sec->sh_flags & SHF_EXECINSTR)
2426#endif
2427 || (sec->sh_entsize & INIT_OFFSET_MASK))
2428 return false;
2429
2430 return true;
2431}
2432
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302433/*
2434 * We only allocate and copy the strings needed by the parts of symtab
2435 * we keep. This is simple, but has the effect of making multiple
2436 * copies of duplicates. We could be more sophisticated, see
2437 * linux-kernel thread starting with
2438 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2439 */
Rusty Russell49668682010-08-05 12:59:10 -06002440static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002441{
Rusty Russell49668682010-08-05 12:59:10 -06002442 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2443 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002444 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302445 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002446
2447 /* Put symbol section at end of init part of module. */
2448 symsect->sh_flags |= SHF_ALLOC;
2449 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002450 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002451 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002452
Rusty Russell49668682010-08-05 12:59:10 -06002453 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002454 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002455
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302456 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302457 for (ndst = i = 0; i < nsrc; i++) {
2458 if (i == 0 ||
2459 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2460 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302461 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002462 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302463 }
Jan Beulich4a496222009-07-06 14:50:42 +01002464
2465 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002466 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302467 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2468 mod->core_size += strtab_size;
Laura Abbott168e47f2015-02-25 14:14:57 -08002469 mod->core_size = debug_align(mod->core_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002470
Jan Beulich554bdfe2009-07-06 14:51:44 +01002471 /* Put string table section at end of init part of module. */
2472 strsect->sh_flags |= SHF_ALLOC;
2473 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002474 info->index.str) | INIT_OFFSET_MASK;
Laura Abbott168e47f2015-02-25 14:14:57 -08002475 mod->init_size = debug_align(mod->init_size);
Jim Cromie5e124162011-12-06 12:11:31 -07002476 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002477}
2478
Rusty Russell811d66a2010-08-05 12:59:12 -06002479static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Jan Beulich4a496222009-07-06 14:50:42 +01002481 unsigned int i, ndst;
2482 const Elf_Sym *src;
2483 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002484 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002485 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Rusty Russelleded41c2010-08-05 12:59:07 -06002487 mod->symtab = (void *)symsec->sh_addr;
2488 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002489 /* Make sure we get permanent strtab: don't use info->strtab. */
2490 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 /* Set types up while we still have access to sections. */
2493 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002494 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002495
Rusty Russelld9131882010-08-05 12:59:08 -06002496 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302497 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002498 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302499 for (ndst = i = 0; i < mod->num_symtab; i++) {
2500 if (i == 0 ||
2501 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2502 dst[ndst] = src[i];
2503 dst[ndst++].st_name = s - mod->core_strtab;
2504 s += strlcpy(s, &mod->strtab[src[i].st_name],
2505 KSYM_NAME_LEN) + 1;
2506 }
Jan Beulich4a496222009-07-06 14:50:42 +01002507 }
2508 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510#else
Rusty Russell49668682010-08-05 12:59:10 -06002511static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002512{
2513}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002514
Michał Mirosławabbce902010-09-20 01:58:08 +02002515static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517}
2518#endif /* CONFIG_KALLSYMS */
2519
Jason Barone9d376f2009-02-05 11:51:38 -05002520static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002521{
Rusty Russell811d66a2010-08-05 12:59:12 -06002522 if (!debug)
2523 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002524#ifdef CONFIG_DYNAMIC_DEBUG
2525 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002526 pr_err("dynamic debug error adding module: %s\n",
2527 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002528#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002529}
Jason Baron346e15b2008-08-12 16:46:19 -04002530
Yehuda Sadehff49d742010-07-03 13:07:35 +10002531static void dynamic_debug_remove(struct _ddebug *debug)
2532{
2533 if (debug)
2534 ddebug_remove_module(debug->modname);
2535}
2536
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002537void * __weak module_alloc(unsigned long size)
2538{
Rusty Russell82fab442012-12-11 09:38:33 +10302539 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002540}
2541
Rusty Russell3a642e92008-07-22 19:24:28 -05002542static void *module_alloc_update_bounds(unsigned long size)
2543{
2544 void *ret = module_alloc(size);
2545
2546 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002547 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002548 /* Update module bounds. */
2549 if ((unsigned long)ret < module_addr_min)
2550 module_addr_min = (unsigned long)ret;
2551 if ((unsigned long)ret + size > module_addr_max)
2552 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002553 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002554 }
2555 return ret;
2556}
2557
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002558#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002559static void kmemleak_load_module(const struct module *mod,
2560 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002561{
2562 unsigned int i;
2563
2564 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002565 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002566
Rusty Russell49668682010-08-05 12:59:10 -06002567 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002568 /* Scan all writable sections that's not executable */
2569 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2570 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2571 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002572 continue;
2573
Rusty Russell49668682010-08-05 12:59:10 -06002574 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2575 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002576 }
2577}
2578#else
Rusty Russell49668682010-08-05 12:59:10 -06002579static inline void kmemleak_load_module(const struct module *mod,
2580 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002581{
2582}
2583#endif
2584
Rusty Russell106a4ee2012-09-26 10:09:40 +01002585#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302586static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002587{
2588 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302589 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2590 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002591
Kees Cook34e11692012-10-16 07:31:07 +10302592 if (info->len > markerlen &&
2593 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002594 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302595 info->len -= markerlen;
2596 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002597 }
2598
2599 if (!err) {
2600 info->sig_ok = true;
2601 return 0;
2602 }
2603
2604 /* Not having a signature is only an error if we're strict. */
2605 if (err == -ENOKEY && !sig_enforce)
2606 err = 0;
2607
2608 return err;
2609}
2610#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302611static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002612{
2613 return 0;
2614}
2615#endif /* !CONFIG_MODULE_SIG */
2616
Kees Cook34e11692012-10-16 07:31:07 +10302617/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2618static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002619{
Kees Cook34e11692012-10-16 07:31:07 +10302620 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002621 return -ENOEXEC;
2622
Kees Cook34e11692012-10-16 07:31:07 +10302623 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2624 || info->hdr->e_type != ET_REL
2625 || !elf_check_arch(info->hdr)
2626 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2627 return -ENOEXEC;
2628
2629 if (info->hdr->e_shoff >= info->len
2630 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2631 info->len - info->hdr->e_shoff))
2632 return -ENOEXEC;
2633
2634 return 0;
2635}
2636
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002637#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2638
2639static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2640{
2641 do {
2642 unsigned long n = min(len, COPY_CHUNK_SIZE);
2643
2644 if (copy_from_user(dst, usrc, n) != 0)
2645 return -EFAULT;
2646 cond_resched();
2647 dst += n;
2648 usrc += n;
2649 len -= n;
2650 } while (len);
2651 return 0;
2652}
2653
Kees Cook34e11692012-10-16 07:31:07 +10302654/* Sets info->hdr and info->len. */
2655static int copy_module_from_user(const void __user *umod, unsigned long len,
2656 struct load_info *info)
2657{
Kees Cook2e72d512012-10-16 07:32:07 +10302658 int err;
2659
Kees Cook34e11692012-10-16 07:31:07 +10302660 info->len = len;
2661 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002662 return -ENOEXEC;
2663
Kees Cook2e72d512012-10-16 07:32:07 +10302664 err = security_kernel_module_from_file(NULL);
2665 if (err)
2666 return err;
2667
Rusty Russell40dd2562010-08-05 12:59:03 -06002668 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302669 info->hdr = __vmalloc(info->len,
2670 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302671 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002672 return -ENOMEM;
2673
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002674 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302675 vfree(info->hdr);
2676 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002677 }
2678
Rusty Russell40dd2562010-08-05 12:59:03 -06002679 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302680}
Rusty Russell40dd2562010-08-05 12:59:03 -06002681
Kees Cook34e11692012-10-16 07:31:07 +10302682/* Sets info->hdr and info->len. */
2683static int copy_module_from_fd(int fd, struct load_info *info)
2684{
Al Viroa2e05782013-08-30 12:41:41 -04002685 struct fd f = fdget(fd);
Kees Cook34e11692012-10-16 07:31:07 +10302686 int err;
2687 struct kstat stat;
2688 loff_t pos;
2689 ssize_t bytes = 0;
2690
Al Viroa2e05782013-08-30 12:41:41 -04002691 if (!f.file)
Kees Cook34e11692012-10-16 07:31:07 +10302692 return -ENOEXEC;
2693
Al Viroa2e05782013-08-30 12:41:41 -04002694 err = security_kernel_module_from_file(f.file);
Kees Cook2e72d512012-10-16 07:32:07 +10302695 if (err)
2696 goto out;
2697
Al Viroa2e05782013-08-30 12:41:41 -04002698 err = vfs_getattr(&f.file->f_path, &stat);
Kees Cook34e11692012-10-16 07:31:07 +10302699 if (err)
2700 goto out;
2701
2702 if (stat.size > INT_MAX) {
2703 err = -EFBIG;
2704 goto out;
2705 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302706
2707 /* Don't hand 0 to vmalloc, it whines. */
2708 if (stat.size == 0) {
2709 err = -EINVAL;
2710 goto out;
2711 }
2712
Kees Cook34e11692012-10-16 07:31:07 +10302713 info->hdr = vmalloc(stat.size);
2714 if (!info->hdr) {
2715 err = -ENOMEM;
2716 goto out;
2717 }
2718
2719 pos = 0;
2720 while (pos < stat.size) {
Al Viroa2e05782013-08-30 12:41:41 -04002721 bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
Kees Cook34e11692012-10-16 07:31:07 +10302722 stat.size - pos);
2723 if (bytes < 0) {
2724 vfree(info->hdr);
2725 err = bytes;
2726 goto out;
2727 }
2728 if (bytes == 0)
2729 break;
2730 pos += bytes;
2731 }
2732 info->len = pos;
2733
2734out:
Al Viroa2e05782013-08-30 12:41:41 -04002735 fdput(f);
Rusty Russell40dd2562010-08-05 12:59:03 -06002736 return err;
2737}
2738
Rusty Russelld9131882010-08-05 12:59:08 -06002739static void free_copy(struct load_info *info)
2740{
Rusty Russelld9131882010-08-05 12:59:08 -06002741 vfree(info->hdr);
2742}
2743
Rusty Russell2f3238a2012-10-22 18:09:41 +10302744static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002745{
2746 unsigned int i;
2747
2748 /* This should always be true, but let's be sure. */
2749 info->sechdrs[0].sh_addr = 0;
2750
2751 for (i = 1; i < info->hdr->e_shnum; i++) {
2752 Elf_Shdr *shdr = &info->sechdrs[i];
2753 if (shdr->sh_type != SHT_NOBITS
2754 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002755 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002756 return -ENOEXEC;
2757 }
2758
2759 /* Mark all sections sh_addr with their address in the
2760 temporary image. */
2761 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2762
2763#ifndef CONFIG_MODULE_UNLOAD
2764 /* Don't load .exit sections */
2765 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2766 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2767#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002768 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002769
2770 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302771 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2772 info->index.vers = 0; /* Pretend no __versions section! */
2773 else
2774 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002775 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002776 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2777 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002778 return 0;
2779}
2780
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002781/*
2782 * Set up our basic convenience variables (pointers to section headers,
2783 * search for module section index etc), and do some basic section
2784 * verification.
2785 *
2786 * Return the temporary module pointer (we'll replace it with the final
2787 * one when we move the module sections around).
2788 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302789static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002790{
2791 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002792 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002793 struct module *mod;
2794
2795 /* Set up the convenience variables */
2796 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002797 info->secstrings = (void *)info->hdr
2798 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002799
Rusty Russell2f3238a2012-10-22 18:09:41 +10302800 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002801 if (err)
2802 return ERR_PTR(err);
2803
2804 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002805 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002806 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2807 info->index.sym = i;
2808 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002809 info->strtab = (char *)info->hdr
2810 + info->sechdrs[info->index.str].sh_offset;
2811 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002812 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002813 }
2814
Rusty Russell49668682010-08-05 12:59:10 -06002815 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002816 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002817 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002818 return ERR_PTR(-ENOEXEC);
2819 }
2820 /* This is temporary: point mod into copy of data. */
2821 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2822
2823 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002824 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002825 return ERR_PTR(-ENOEXEC);
2826 }
2827
Rusty Russell49668682010-08-05 12:59:10 -06002828 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002829
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002830 /* Check module struct version now, before we try to use module. */
2831 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2832 return ERR_PTR(-ENOEXEC);
2833
2834 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002835}
2836
Rusty Russell2f3238a2012-10-22 18:09:41 +10302837static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002838{
Rusty Russell49668682010-08-05 12:59:10 -06002839 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002840 int err;
2841
Rusty Russell2f3238a2012-10-22 18:09:41 +10302842 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2843 modmagic = NULL;
2844
Rusty Russell40dd2562010-08-05 12:59:03 -06002845 /* This is allowed: modprobe --force will invalidate it. */
2846 if (!modmagic) {
2847 err = try_to_force_load(mod, "bad vermagic");
2848 if (err)
2849 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002850 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002851 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002852 mod->name, modmagic, vermagic);
2853 return -ENOEXEC;
2854 }
2855
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002856 if (!get_modinfo(info, "intree"))
Rusty Russell373d4d02013-01-21 17:17:39 +10302857 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002858
Rusty Russell49668682010-08-05 12:59:10 -06002859 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302860 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002861 pr_warn("%s: module is from the staging directory, the quality "
2862 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002863 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002864
2865 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002866 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002867
Rusty Russell40dd2562010-08-05 12:59:03 -06002868 return 0;
2869}
2870
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302871static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002872{
Rusty Russell49668682010-08-05 12:59:10 -06002873 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002874 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002875 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002876 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002877 mod->crcs = section_addr(info, "__kcrctab");
2878 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002879 sizeof(*mod->gpl_syms),
2880 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002881 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2882 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002883 "__ksymtab_gpl_future",
2884 sizeof(*mod->gpl_future_syms),
2885 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002886 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002887
2888#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002889 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002890 sizeof(*mod->unused_syms),
2891 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002892 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2893 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002894 sizeof(*mod->unused_gpl_syms),
2895 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002896 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002897#endif
2898#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002899 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002900 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302901 if (!mod->ctors)
2902 mod->ctors = section_objs(info, ".init_array",
2903 sizeof(*mod->ctors), &mod->num_ctors);
2904 else if (find_sec(info, ".init_array")) {
2905 /*
2906 * This shouldn't happen with same compiler and binutils
2907 * building all parts of the module.
2908 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10302909 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302910 mod->name);
2911 return -EINVAL;
2912 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002913#endif
2914
2915#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002916 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2917 sizeof(*mod->tracepoints_ptrs),
2918 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002919#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002920#ifdef HAVE_JUMP_LABEL
2921 mod->jump_entries = section_objs(info, "__jump_table",
2922 sizeof(*mod->jump_entries),
2923 &mod->num_jump_entries);
2924#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002925#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002926 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002927 sizeof(*mod->trace_events),
2928 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002929 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
2930 sizeof(*mod->trace_enums),
2931 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002932#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002933#ifdef CONFIG_TRACING
2934 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2935 sizeof(*mod->trace_bprintk_fmt_start),
2936 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002937#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002938#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2939 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002940 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002941 sizeof(*mod->ftrace_callsites),
2942 &mod->num_ftrace_callsites);
2943#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002944
Rusty Russell811d66a2010-08-05 12:59:12 -06002945 mod->extable = section_objs(info, "__ex_table",
2946 sizeof(*mod->extable), &mod->num_exentries);
2947
Rusty Russell49668682010-08-05 12:59:10 -06002948 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002949 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002950
2951 info->debug = section_objs(info, "__verbose",
2952 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302953
2954 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002955}
2956
Rusty Russell49668682010-08-05 12:59:10 -06002957static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002958{
2959 int i;
2960 void *ptr;
2961
2962 /* Do the allocs. */
2963 ptr = module_alloc_update_bounds(mod->core_size);
2964 /*
2965 * The pointer to this block is stored in the module structure
2966 * which is inside the block. Just mark it as not being a
2967 * leak.
2968 */
2969 kmemleak_not_leak(ptr);
2970 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002971 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002972
2973 memset(ptr, 0, mod->core_size);
2974 mod->module_core = ptr;
2975
Rusty Russell82fab442012-12-11 09:38:33 +10302976 if (mod->init_size) {
2977 ptr = module_alloc_update_bounds(mod->init_size);
2978 /*
2979 * The pointer to this block is stored in the module structure
2980 * which is inside the block. This block doesn't need to be
2981 * scanned as it contains data and code that will be freed
2982 * after the module is initialized.
2983 */
2984 kmemleak_ignore(ptr);
2985 if (!ptr) {
Rusty Russellbe1f2212015-01-20 09:07:05 +10302986 module_memfree(mod->module_core);
Rusty Russell82fab442012-12-11 09:38:33 +10302987 return -ENOMEM;
2988 }
2989 memset(ptr, 0, mod->init_size);
2990 mod->module_init = ptr;
2991 } else
2992 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002993
2994 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002995 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002996 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002997 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002998 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002999
Rusty Russell49668682010-08-05 12:59:10 -06003000 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003001 continue;
3002
Rusty Russell49668682010-08-05 12:59:10 -06003003 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003004 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06003005 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003006 else
Rusty Russell49668682010-08-05 12:59:10 -06003007 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003008
Rusty Russell49668682010-08-05 12:59:10 -06003009 if (shdr->sh_type != SHT_NOBITS)
3010 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003011 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06003012 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07003013 pr_debug("\t0x%lx %s\n",
3014 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003015 }
Rusty Russelld9131882010-08-05 12:59:08 -06003016
3017 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06003018}
3019
Rusty Russell49668682010-08-05 12:59:10 -06003020static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06003021{
3022 /*
3023 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3024 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3025 * using GPL-only symbols it needs.
3026 */
3027 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303028 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003029
3030 /* driverloader was caught wrongly pretending to be under GPL */
3031 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303032 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3033 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003034
Matthew Garrettc99af372012-06-22 13:49:31 -04003035 /* lve claims to be GPL but upstream won't provide source */
3036 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303037 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3038 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003039
Rusty Russell22e268e2010-08-05 12:59:05 -06003040#ifdef CONFIG_MODVERSIONS
3041 if ((mod->num_syms && !mod->crcs)
3042 || (mod->num_gpl_syms && !mod->gpl_crcs)
3043 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3044#ifdef CONFIG_UNUSED_SYMBOLS
3045 || (mod->num_unused_syms && !mod->unused_crcs)
3046 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3047#endif
3048 ) {
3049 return try_to_force_load(mod,
3050 "no versions for exported symbols");
3051 }
3052#endif
3053 return 0;
3054}
3055
3056static void flush_module_icache(const struct module *mod)
3057{
3058 mm_segment_t old_fs;
3059
3060 /* flush the icache in correct context */
3061 old_fs = get_fs();
3062 set_fs(KERNEL_DS);
3063
3064 /*
3065 * Flush the instruction cache, since we've played with text.
3066 * Do it before processing of module parameters, so the module
3067 * can provide parameter accessor functions of its own.
3068 */
3069 if (mod->module_init)
3070 flush_icache_range((unsigned long)mod->module_init,
3071 (unsigned long)mod->module_init
3072 + mod->init_size);
3073 flush_icache_range((unsigned long)mod->module_core,
3074 (unsigned long)mod->module_core + mod->core_size);
3075
3076 set_fs(old_fs);
3077}
3078
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003079int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3080 Elf_Shdr *sechdrs,
3081 char *secstrings,
3082 struct module *mod)
3083{
3084 return 0;
3085}
3086
Rusty Russell2f3238a2012-10-22 18:09:41 +10303087static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003088{
3089 /* Module within temporary copy. */
3090 struct module *mod;
3091 int err;
3092
Rusty Russell2f3238a2012-10-22 18:09:41 +10303093 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003094 if (IS_ERR(mod))
3095 return mod;
3096
Rusty Russell2f3238a2012-10-22 18:09:41 +10303097 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003098 if (err)
3099 return ERR_PTR(err);
3100
3101 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003102 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3103 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003104 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303105 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003106
Rusty Russell8d8022e2013-07-03 10:06:28 +09303107 /* We will do a special allocation for per-cpu sections later. */
3108 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003109
3110 /* Determine total sizes, and put offsets in sh_entsize. For now
3111 this is done generically; there doesn't appear to be any
3112 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003113 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003114 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003115
3116 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003117 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003118 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303119 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003120
3121 /* Module has been copied to its final place now: return it. */
3122 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003123 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003124 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003125}
3126
3127/* mod is no longer valid after this! */
3128static void module_deallocate(struct module *mod, struct load_info *info)
3129{
Rusty Russelld9131882010-08-05 12:59:08 -06003130 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303131 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10303132 module_memfree(mod->module_init);
3133 module_memfree(mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06003134}
3135
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003136int __weak module_finalize(const Elf_Ehdr *hdr,
3137 const Elf_Shdr *sechdrs,
3138 struct module *me)
3139{
3140 return 0;
3141}
3142
Rusty Russell811d66a2010-08-05 12:59:12 -06003143static int post_relocation(struct module *mod, const struct load_info *info)
3144{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003145 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003146 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3147
3148 /* Copy relocated percpu area over. */
3149 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3150 info->sechdrs[info->index.pcpu].sh_size);
3151
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003152 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003153 add_kallsyms(mod, info);
3154
3155 /* Arch-specific module finalizing. */
3156 return module_finalize(info->hdr, info->sechdrs, mod);
3157}
3158
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303159/* Is this module of this name done loading? No locks held. */
3160static bool finished_loading(const char *name)
3161{
3162 struct module *mod;
3163 bool ret;
3164
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303165 /*
3166 * The module_mutex should not be a heavily contended lock;
3167 * if we get the occasional sleep here, we'll go an extra iteration
3168 * in the wait_event_interruptible(), which is harmless.
3169 */
3170 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303171 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303172 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303173 ret = !mod || mod->state == MODULE_STATE_LIVE
3174 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303175 mutex_unlock(&module_mutex);
3176
3177 return ret;
3178}
3179
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003180/* Call module constructors. */
3181static void do_mod_ctors(struct module *mod)
3182{
3183#ifdef CONFIG_CONSTRUCTORS
3184 unsigned long i;
3185
3186 for (i = 0; i < mod->num_ctors; i++)
3187 mod->ctors[i]();
3188#endif
3189}
3190
Rusty Russellc7496372015-01-20 09:07:05 +10303191/* For freeing module_init on success, in case kallsyms traversing */
3192struct mod_initfree {
3193 struct rcu_head rcu;
3194 void *module_init;
3195};
3196
3197static void do_free_init(struct rcu_head *head)
3198{
3199 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3200 module_memfree(m->module_init);
3201 kfree(m);
3202}
3203
Jan Kiszkabe02a182015-02-17 13:46:50 -08003204/*
3205 * This is where the real work happens.
3206 *
3207 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3208 * helper command 'lx-symbols'.
3209 */
3210static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303213 struct mod_initfree *freeinit;
3214
3215 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3216 if (!freeinit) {
3217 ret = -ENOMEM;
3218 goto fail;
3219 }
3220 freeinit->module_init = mod->module_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Tejun Heo774a1222013-01-15 18:52:51 -08003222 /*
3223 * We want to find out whether @mod uses async during init. Clear
3224 * PF_USED_ASYNC. async_schedule*() will set it.
3225 */
3226 current->flags &= ~PF_USED_ASYNC;
3227
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003228 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 /* Start the module */
3230 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003231 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303233 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003235 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003236 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3237 "follow 0/-E convention\n"
3238 "%s: loading module anyway...\n",
3239 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003240 dump_stack();
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Rusty Russell6f139092012-09-28 14:31:03 +09303243 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003245 blocking_notifier_call_chain(&module_notify_list,
3246 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003247
Tejun Heo774a1222013-01-15 18:52:51 -08003248 /*
3249 * We need to finish all async code before the module init sequence
3250 * is done. This has potential to deadlock. For example, a newly
3251 * detected block device can trigger request_module() of the
3252 * default iosched from async probing task. Once userland helper
3253 * reaches here, async_synchronize_full() will wait on the async
3254 * task waiting on request_module() and deadlock.
3255 *
3256 * This deadlock is avoided by perfomring async_synchronize_full()
3257 * iff module init queued any async jobs. This isn't a full
3258 * solution as it will deadlock the same if module loading from
3259 * async jobs nests more than once; however, due to the various
3260 * constraints, this hack seems to be the best option for now.
3261 * Please refer to the following thread for details.
3262 *
3263 * http://thread.gmane.org/gmane.linux.kernel/1420814
3264 */
3265 if (current->flags & PF_USED_ASYNC)
3266 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003267
Rusty Russell6c5db222008-03-10 11:43:52 -07003268 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /* Drop initial reference. */
3270 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003271 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003272#ifdef CONFIG_KALLSYMS
3273 mod->num_symtab = mod->core_num_syms;
3274 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003275 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003276#endif
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303277 mod_tree_remove_init(mod);
Jan Glauber01526ed2011-05-19 16:55:26 -06003278 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303279 module_arch_freeing_init(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 mod->module_init = NULL;
3281 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003282 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 mod->init_text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303284 /*
3285 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303286 * walking this with preempt disabled. In all the failure paths, we
3287 * call synchronize_sched(), but we don't want to slow down the success
3288 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303289 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303290 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003291 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303292 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303295
3296fail_free_freeinit:
3297 kfree(freeinit);
3298fail:
3299 /* Try to protect us from buggy refcounters. */
3300 mod->state = MODULE_STATE_GOING;
3301 synchronize_sched();
3302 module_put(mod);
3303 blocking_notifier_call_chain(&module_notify_list,
3304 MODULE_STATE_GOING, mod);
3305 free_module(mod);
3306 wake_up_all(&module_wq);
3307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Kees Cook34e11692012-10-16 07:31:07 +10303310static int may_init_module(void)
3311{
3312 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3313 return -EPERM;
3314
3315 return 0;
3316}
3317
Rusty Russella3535c72013-01-21 17:18:59 +10303318/*
3319 * We try to place it in the list now to make sure it's unique before
3320 * we dedicate too many resources. In particular, temporary percpu
3321 * memory exhaustion.
3322 */
3323static int add_unformed_module(struct module *mod)
3324{
3325 int err;
3326 struct module *old;
3327
3328 mod->state = MODULE_STATE_UNFORMED;
3329
3330again:
3331 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303332 old = find_module_all(mod->name, strlen(mod->name), true);
3333 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303334 if (old->state == MODULE_STATE_COMING
3335 || old->state == MODULE_STATE_UNFORMED) {
3336 /* Wait in case it fails to load. */
3337 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303338 err = wait_event_interruptible(module_wq,
3339 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303340 if (err)
3341 goto out_unlocked;
3342 goto again;
3343 }
3344 err = -EEXIST;
3345 goto out;
3346 }
3347 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303348 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303349 err = 0;
3350
3351out:
3352 mutex_unlock(&module_mutex);
3353out_unlocked:
3354 return err;
3355}
3356
3357static int complete_formation(struct module *mod, struct load_info *info)
3358{
3359 int err;
3360
3361 mutex_lock(&module_mutex);
3362
3363 /* Find duplicate symbols (must be called under lock). */
3364 err = verify_export_symbols(mod);
3365 if (err < 0)
3366 goto out;
3367
3368 /* This relies on module_mutex for list integrity. */
3369 module_bug_finalize(info->hdr, info->sechdrs, mod);
3370
Rusty Russell49822232014-05-14 10:54:19 +09303371 /* Set RO and NX regions for core */
3372 set_section_ro_nx(mod->module_core,
3373 mod->core_text_size,
3374 mod->core_ro_size,
3375 mod->core_size);
3376
3377 /* Set RO and NX regions for init */
3378 set_section_ro_nx(mod->module_init,
3379 mod->init_text_size,
3380 mod->init_ro_size,
3381 mod->init_size);
3382
Rusty Russella3535c72013-01-21 17:18:59 +10303383 /* Mark state as coming so strong_try_module_get() ignores us,
3384 * but kallsyms etc. can see us. */
3385 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303386 mutex_unlock(&module_mutex);
3387
3388 blocking_notifier_call_chain(&module_notify_list,
3389 MODULE_STATE_COMING, mod);
3390 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303391
3392out:
3393 mutex_unlock(&module_mutex);
3394 return err;
3395}
3396
Rusty Russell54041d82013-07-02 15:35:12 +09303397static int unknown_module_param_cb(char *param, char *val, const char *modname)
3398{
Ionut Alexa6da0b562014-11-10 09:31:29 +10303399 /* Check for magic 'dyndbg' arg */
Rusty Russell54041d82013-07-02 15:35:12 +09303400 int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003401 if (ret != 0)
3402 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303403 return 0;
3404}
3405
Kees Cook34e11692012-10-16 07:31:07 +10303406/* Allocate and load the module: note that size of section 0 is always
3407 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303408static int load_module(struct load_info *info, const char __user *uargs,
3409 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303410{
Rusty Russella3535c72013-01-21 17:18:59 +10303411 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303412 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303413 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303414
3415 err = module_sig_check(info);
3416 if (err)
3417 goto free_copy;
3418
3419 err = elf_header_check(info);
3420 if (err)
3421 goto free_copy;
3422
3423 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303424 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303425 if (IS_ERR(mod)) {
3426 err = PTR_ERR(mod);
3427 goto free_copy;
3428 }
3429
Rusty Russella3535c72013-01-21 17:18:59 +10303430 /* Reserve our place in the list. */
3431 err = add_unformed_module(mod);
3432 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303433 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303434
Kees Cook34e11692012-10-16 07:31:07 +10303435#ifdef CONFIG_MODULE_SIG
3436 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303437 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003438 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303439 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003440 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303441 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303442 }
Kees Cook34e11692012-10-16 07:31:07 +10303443#endif
3444
Rusty Russell8d8022e2013-07-03 10:06:28 +09303445 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303446 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303447 if (err)
3448 goto unlink_mod;
3449
Kees Cook34e11692012-10-16 07:31:07 +10303450 /* Now module is in final location, initialize linked lists, etc. */
3451 err = module_unload_init(mod);
3452 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303453 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303454
3455 /* Now we've got everything in the final locations, we can
3456 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303457 err = find_module_sections(mod, info);
3458 if (err)
3459 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303460
3461 err = check_module_license_and_versions(mod);
3462 if (err)
3463 goto free_unload;
3464
3465 /* Set up MODINFO_ATTR fields */
3466 setup_modinfo(mod, info);
3467
3468 /* Fix up syms, so that st_value is a pointer to location. */
3469 err = simplify_symbols(mod, info);
3470 if (err < 0)
3471 goto free_modinfo;
3472
3473 err = apply_relocations(mod, info);
3474 if (err < 0)
3475 goto free_modinfo;
3476
3477 err = post_relocation(mod, info);
3478 if (err < 0)
3479 goto free_modinfo;
3480
3481 flush_module_icache(mod);
3482
3483 /* Now copy in args */
3484 mod->args = strndup_user(uargs, ~0UL >> 1);
3485 if (IS_ERR(mod->args)) {
3486 err = PTR_ERR(mod->args);
3487 goto free_arch_cleanup;
3488 }
3489
Kees Cook34e11692012-10-16 07:31:07 +10303490 dynamic_debug_setup(info->debug, info->num_debug);
3491
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003492 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3493 ftrace_module_init(mod);
3494
Rusty Russella3535c72013-01-21 17:18:59 +10303495 /* Finally it's fully formed, ready to start executing. */
3496 err = complete_formation(mod, info);
3497 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303498 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303499
Kees Cook34e11692012-10-16 07:31:07 +10303500 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303501 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3502 -32768, 32767, unknown_module_param_cb);
3503 if (IS_ERR(after_dashes)) {
3504 err = PTR_ERR(after_dashes);
Rusty Russell1fb93412013-01-12 13:27:34 +10303505 goto bug_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303506 } else if (after_dashes) {
3507 pr_warn("%s: parameters '%s' after `--' ignored\n",
3508 mod->name, after_dashes);
3509 }
Kees Cook34e11692012-10-16 07:31:07 +10303510
3511 /* Link in to syfs. */
3512 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3513 if (err < 0)
Rusty Russell1fb93412013-01-12 13:27:34 +10303514 goto bug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303515
3516 /* Get rid of temporary copy. */
3517 free_copy(info);
3518
3519 /* Done! */
3520 trace_module_load(mod);
3521
3522 return do_init_module(mod);
3523
Rusty Russell1fb93412013-01-12 13:27:34 +10303524 bug_cleanup:
3525 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303526 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303527 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003528 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303529
3530 /* we can't deallocate the module until we clear memory protection */
3531 unset_module_init_ro_nx(mod);
3532 unset_module_core_ro_nx(mod);
3533
Rusty Russella3535c72013-01-21 17:18:59 +10303534 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303535 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303536 synchronize_sched();
3537 kfree(mod->args);
3538 free_arch_cleanup:
3539 module_arch_cleanup(mod);
3540 free_modinfo:
3541 free_modinfo(mod);
3542 free_unload:
3543 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303544 unlink_mod:
3545 mutex_lock(&module_mutex);
3546 /* Unlink carefully: kallsyms could be walking list. */
3547 list_del_rcu(&mod->list);
3548 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303549 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3550 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303551 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303552 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003553 /* Free lock-classes; relies on the preceding sync_rcu() */
3554 lockdep_free_key_range(mod->module_core, mod->core_size);
3555
Kees Cook34e11692012-10-16 07:31:07 +10303556 module_deallocate(mod, info);
3557 free_copy:
3558 free_copy(info);
3559 return err;
3560}
3561
3562SYSCALL_DEFINE3(init_module, void __user *, umod,
3563 unsigned long, len, const char __user *, uargs)
3564{
3565 int err;
3566 struct load_info info = { };
3567
3568 err = may_init_module();
3569 if (err)
3570 return err;
3571
3572 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3573 umod, len, uargs);
3574
3575 err = copy_module_from_user(umod, len, &info);
3576 if (err)
3577 return err;
3578
Rusty Russell2f3238a2012-10-22 18:09:41 +10303579 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303580}
3581
Rusty Russell2f3238a2012-10-22 18:09:41 +10303582SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303583{
3584 int err;
3585 struct load_info info = { };
3586
3587 err = may_init_module();
3588 if (err)
3589 return err;
3590
Rusty Russell2f3238a2012-10-22 18:09:41 +10303591 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3592
3593 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3594 |MODULE_INIT_IGNORE_VERMAGIC))
3595 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303596
3597 err = copy_module_from_fd(fd, &info);
3598 if (err)
3599 return err;
3600
Rusty Russell2f3238a2012-10-22 18:09:41 +10303601 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303602}
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604static inline int within(unsigned long addr, void *start, unsigned long size)
3605{
3606 return ((void *)addr >= start && (void *)addr < start + size);
3607}
3608
3609#ifdef CONFIG_KALLSYMS
3610/*
3611 * This ignores the intensely annoying "mapping symbols" found
3612 * in ARM ELF files: $a, $t and $d.
3613 */
3614static inline int is_arm_mapping_symbol(const char *str)
3615{
Russell King2e3a10a2014-07-27 07:29:01 +09303616 if (str[0] == '.' && str[1] == 'L')
3617 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003618 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 && (str[2] == '\0' || str[2] == '.');
3620}
3621
3622static const char *get_ksymbol(struct module *mod,
3623 unsigned long addr,
3624 unsigned long *size,
3625 unsigned long *offset)
3626{
3627 unsigned int i, best = 0;
3628 unsigned long nextval;
3629
3630 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003631 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003633 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3635
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003636 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003637 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 for (i = 1; i < mod->num_symtab; i++) {
3639 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3640 continue;
3641
3642 /* We ignore unnamed symbols: they're uninformative
3643 * and inserted at a whim. */
3644 if (mod->symtab[i].st_value <= addr
3645 && mod->symtab[i].st_value > mod->symtab[best].st_value
3646 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3647 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3648 best = i;
3649 if (mod->symtab[i].st_value > addr
3650 && mod->symtab[i].st_value < nextval
3651 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3652 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3653 nextval = mod->symtab[i].st_value;
3654 }
3655
3656 if (!best)
3657 return NULL;
3658
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003659 if (size)
3660 *size = nextval - mod->symtab[best].st_value;
3661 if (offset)
3662 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return mod->strtab + mod->symtab[best].st_name;
3664}
3665
Rusty Russell6dd06c92008-01-29 17:13:22 -05003666/* For kallsyms to ask for address resolution. NULL means not found. Careful
3667 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003668const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003669 unsigned long *size,
3670 unsigned long *offset,
3671 char **modname,
3672 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Rusty Russellcb2a5202008-01-14 00:55:03 -08003674 const char *ret = NULL;
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303675 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Rusty Russellcb2a5202008-01-14 00:55:03 -08003677 preempt_disable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303678 mod = __module_address(addr);
3679 if (mod) {
3680 if (modname)
3681 *modname = mod->name;
3682 ret = get_ksymbol(mod, addr, size, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003684 /* Make a copy in here where it's safe */
3685 if (ret) {
3686 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3687 ret = namebuf;
3688 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003689 preempt_enable();
Peter Zijlstrab7df4d12015-05-27 11:09:37 +09303690
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003691 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003694int lookup_module_symbol_name(unsigned long addr, char *symname)
3695{
3696 struct module *mod;
3697
Rusty Russellcb2a5202008-01-14 00:55:03 -08003698 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003699 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303700 if (mod->state == MODULE_STATE_UNFORMED)
3701 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303702 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003703 const char *sym;
3704
3705 sym = get_ksymbol(mod, addr, NULL, NULL);
3706 if (!sym)
3707 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003708 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003709 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003710 return 0;
3711 }
3712 }
3713out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003714 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003715 return -ERANGE;
3716}
3717
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003718int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3719 unsigned long *offset, char *modname, char *name)
3720{
3721 struct module *mod;
3722
Rusty Russellcb2a5202008-01-14 00:55:03 -08003723 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003724 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303725 if (mod->state == MODULE_STATE_UNFORMED)
3726 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303727 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003728 const char *sym;
3729
3730 sym = get_ksymbol(mod, addr, size, offset);
3731 if (!sym)
3732 goto out;
3733 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003734 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003735 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003736 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003737 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003738 return 0;
3739 }
3740 }
3741out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003742 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003743 return -ERANGE;
3744}
3745
Alexey Dobriyanea078902007-05-08 00:28:39 -07003746int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3747 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 struct module *mod;
3750
Rusty Russellcb2a5202008-01-14 00:55:03 -08003751 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003752 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303753 if (mod->state == MODULE_STATE_UNFORMED)
3754 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (symnum < mod->num_symtab) {
3756 *value = mod->symtab[symnum].st_value;
3757 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003758 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003759 KSYM_NAME_LEN);
3760 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003761 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003762 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765 symnum -= mod->num_symtab;
3766 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003767 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003768 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
3771static unsigned long mod_find_symname(struct module *mod, const char *name)
3772{
3773 unsigned int i;
3774
3775 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003776 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3777 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 return mod->symtab[i].st_value;
3779 return 0;
3780}
3781
3782/* Look for this name: can be of form module:name. */
3783unsigned long module_kallsyms_lookup_name(const char *name)
3784{
3785 struct module *mod;
3786 char *colon;
3787 unsigned long ret = 0;
3788
3789 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003790 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303792 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303795 list_for_each_entry_rcu(mod, &modules, list) {
3796 if (mod->state == MODULE_STATE_UNFORMED)
3797 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 if ((ret = mod_find_symname(mod, name)) != 0)
3799 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003802 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return ret;
3804}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003805
3806int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3807 struct module *, unsigned long),
3808 void *data)
3809{
3810 struct module *mod;
3811 unsigned int i;
3812 int ret;
3813
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303814 module_assert_mutex();
3815
Anders Kaseorg75a66612008-12-05 19:03:58 -05003816 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303817 if (mod->state == MODULE_STATE_UNFORMED)
3818 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003819 for (i = 0; i < mod->num_symtab; i++) {
3820 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3821 mod, mod->symtab[i].st_value);
3822 if (ret != 0)
3823 return ret;
3824 }
3825 }
3826 return 0;
3827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828#endif /* CONFIG_KALLSYMS */
3829
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003830static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003831{
3832 int bx = 0;
3833
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303834 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003835 if (mod->taints ||
3836 mod->state == MODULE_STATE_GOING ||
3837 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003838 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303839 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003840 /* Show a - for module-is-being-unloaded */
3841 if (mod->state == MODULE_STATE_GOING)
3842 buf[bx++] = '-';
3843 /* Show a + for module-is-being-loaded */
3844 if (mod->state == MODULE_STATE_COMING)
3845 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003846 buf[bx++] = ')';
3847 }
3848 buf[bx] = '\0';
3849
3850 return buf;
3851}
3852
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003853#ifdef CONFIG_PROC_FS
3854/* Called by the /proc file system to return a list of modules. */
3855static void *m_start(struct seq_file *m, loff_t *pos)
3856{
3857 mutex_lock(&module_mutex);
3858 return seq_list_start(&modules, *pos);
3859}
3860
3861static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3862{
3863 return seq_list_next(p, &modules, pos);
3864}
3865
3866static void m_stop(struct seq_file *m, void *p)
3867{
3868 mutex_unlock(&module_mutex);
3869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871static int m_show(struct seq_file *m, void *p)
3872{
3873 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003874 char buf[8];
3875
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303876 /* We always ignore unformed modules. */
3877 if (mod->state == MODULE_STATE_UNFORMED)
3878 return 0;
3879
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003880 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 mod->name, mod->init_size + mod->core_size);
3882 print_unload_info(m, mod);
3883
3884 /* Informative for users. */
3885 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10303886 mod->state == MODULE_STATE_GOING ? "Unloading" :
3887 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 "Live");
3889 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003890 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003892 /* Taints info */
3893 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003894 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003895
Ionut Alexa6da0b562014-11-10 09:31:29 +10303896 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 return 0;
3898}
3899
3900/* Format: modulename size refcount deps address
3901
3902 Where refcount is a number or -, and deps is a comma-separated list
3903 of depends or -.
3904*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003905static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 .start = m_start,
3907 .next = m_next,
3908 .stop = m_stop,
3909 .show = m_show
3910};
3911
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003912static int modules_open(struct inode *inode, struct file *file)
3913{
3914 return seq_open(file, &modules_op);
3915}
3916
3917static const struct file_operations proc_modules_operations = {
3918 .open = modules_open,
3919 .read = seq_read,
3920 .llseek = seq_lseek,
3921 .release = seq_release,
3922};
3923
3924static int __init proc_modules_init(void)
3925{
3926 proc_create("modules", 0, NULL, &proc_modules_operations);
3927 return 0;
3928}
3929module_init(proc_modules_init);
3930#endif
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932/* Given an address, look for it in the module exception tables. */
3933const struct exception_table_entry *search_module_extables(unsigned long addr)
3934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 const struct exception_table_entry *e = NULL;
3936 struct module *mod;
3937
Rusty Russell24da1cb2007-07-15 23:41:46 -07003938 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003939 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303940 if (mod->state == MODULE_STATE_UNFORMED)
3941 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (mod->num_exentries == 0)
3943 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 e = search_extable(mod->extable,
3946 mod->extable + mod->num_exentries - 1,
3947 addr);
3948 if (e)
3949 break;
3950 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003951 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003954 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return e;
3956}
3957
Ingo Molnar4d435f92006-07-03 00:24:24 -07003958/*
Rusty Russelle6104992009-03-31 13:05:31 -06003959 * is_module_address - is this address inside a module?
3960 * @addr: the address to check.
3961 *
3962 * See is_module_text_address() if you simply want to see if the address
3963 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003964 */
Rusty Russelle6104992009-03-31 13:05:31 -06003965bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003966{
Rusty Russelle6104992009-03-31 13:05:31 -06003967 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003968
Rusty Russell24da1cb2007-07-15 23:41:46 -07003969 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003970 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003971 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003972
Rusty Russelle6104992009-03-31 13:05:31 -06003973 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003974}
3975
Rusty Russelle6104992009-03-31 13:05:31 -06003976/*
3977 * __module_address - get the module which contains an address.
3978 * @addr: the address.
3979 *
3980 * Must be called with preempt disabled or module mutex held so that
3981 * module doesn't get freed during this.
3982 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003983struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 struct module *mod;
3986
Rusty Russell3a642e92008-07-22 19:24:28 -05003987 if (addr < module_addr_min || addr > module_addr_max)
3988 return NULL;
3989
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303990 module_assert_mutex_or_preempt();
3991
Peter Zijlstra6c9692e2015-05-27 11:09:37 +09303992 mod = mod_find(addr);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303993 if (mod) {
3994 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303995 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303996 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303997 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303998 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
Tim Abbottc6b37802008-12-05 19:03:59 -05004000EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Rusty Russelle6104992009-03-31 13:05:31 -06004002/*
4003 * is_module_text_address - is this address inside module code?
4004 * @addr: the address to check.
4005 *
4006 * See is_module_address() if you simply want to see if the address is
4007 * anywhere in a module. See kernel_text_address() for testing if an
4008 * address corresponds to kernel or module code.
4009 */
4010bool is_module_text_address(unsigned long addr)
4011{
4012 bool ret;
4013
4014 preempt_disable();
4015 ret = __module_text_address(addr) != NULL;
4016 preempt_enable();
4017
4018 return ret;
4019}
4020
4021/*
4022 * __module_text_address - get the module whose code contains an address.
4023 * @addr: the address.
4024 *
4025 * Must be called with preempt disabled or module mutex held so that
4026 * module doesn't get freed during this.
4027 */
4028struct module *__module_text_address(unsigned long addr)
4029{
4030 struct module *mod = __module_address(addr);
4031 if (mod) {
4032 /* Make sure it's within the text section. */
4033 if (!within(addr, mod->module_init, mod->init_text_size)
4034 && !within(addr, mod->module_core, mod->core_text_size))
4035 mod = NULL;
4036 }
4037 return mod;
4038}
Tim Abbottc6b37802008-12-05 19:03:59 -05004039EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041/* Don't grab lock, we're oopsing. */
4042void print_modules(void)
4043{
4044 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07004045 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Linus Torvaldsb2311252009-06-16 11:07:14 -07004047 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004048 /* Most callers should already have preempt disabled, but make sure */
4049 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304050 list_for_each_entry_rcu(mod, &modules, list) {
4051 if (mod->state == MODULE_STATE_UNFORMED)
4052 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304053 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304054 }
Andi Kleend72b3752008-08-30 10:09:00 +02004055 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004056 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304057 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4058 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004062/* Generate the signature for all relevant module structures here.
4063 * If these change, we don't want to try to parse the module. */
4064void module_layout(struct module *mod,
4065 struct modversion_info *ver,
4066 struct kernel_param *kp,
4067 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004068 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004069{
4070}
4071EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072#endif