blob: e0db5c31cb53f9a57334f1680228101c6913d70e [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
105/*
106 * Use a latched RB-tree for __module_address(); this allows us to use
107 * RCU-sched lookups of the address from any context.
108 *
109 * Because modules have two address ranges: init and core, we need two
110 * latch_tree_nodes entries. Therefore we need the back-pointer from
111 * mod_tree_node.
112 *
113 * Because init ranges are short lived we mark them unlikely and have placed
114 * them outside the critical cacheline in struct module.
115 */
116
117static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
118{
119 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
120 struct module *mod = mtn->mod;
121
122 if (unlikely(mtn == &mod->mtn_init))
123 return (unsigned long)mod->module_init;
124
125 return (unsigned long)mod->module_core;
126}
127
128static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
129{
130 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
131 struct module *mod = mtn->mod;
132
133 if (unlikely(mtn == &mod->mtn_init))
134 return (unsigned long)mod->init_size;
135
136 return (unsigned long)mod->core_size;
137}
138
139static __always_inline bool
140mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
141{
142 return __mod_tree_val(a) < __mod_tree_val(b);
143}
144
145static __always_inline int
146mod_tree_comp(void *key, struct latch_tree_node *n)
147{
148 unsigned long val = (unsigned long)key;
149 unsigned long start, end;
150
151 start = __mod_tree_val(n);
152 if (val < start)
153 return -1;
154
155 end = start + __mod_tree_size(n);
156 if (val >= end)
157 return 1;
158
159 return 0;
160}
161
162static const struct latch_tree_ops mod_tree_ops = {
163 .less = mod_tree_less,
164 .comp = mod_tree_comp,
165};
166
167static struct latch_tree_root mod_tree __cacheline_aligned;
168
169/*
170 * These modifications: insert, remove_init and remove; are serialized by the
171 * module_mutex.
172 */
173static void mod_tree_insert(struct module *mod)
174{
175 mod->mtn_core.mod = mod;
176 mod->mtn_init.mod = mod;
177
178 latch_tree_insert(&mod->mtn_core.node, &mod_tree, &mod_tree_ops);
179 if (mod->init_size)
180 latch_tree_insert(&mod->mtn_init.node, &mod_tree, &mod_tree_ops);
181}
182
183static void mod_tree_remove_init(struct module *mod)
184{
185 if (mod->init_size)
186 latch_tree_erase(&mod->mtn_init.node, &mod_tree, &mod_tree_ops);
187}
188
189static void mod_tree_remove(struct module *mod)
190{
191 latch_tree_erase(&mod->mtn_core.node, &mod_tree, &mod_tree_ops);
192 mod_tree_remove_init(mod);
193}
194
195static struct module *mod_tree_find(unsigned long addr)
196{
197 struct latch_tree_node *ltn;
198
199 ltn = latch_tree_find((void *)addr, &mod_tree, &mod_tree_ops);
200 if (!ltn)
201 return NULL;
202
203 return container_of(ltn, struct mod_tree_node, node)->mod;
204}
205
Jason Wessel67fc4e02010-05-20 21:04:21 -0500206#ifdef CONFIG_KGDB_KDB
207struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
208#endif /* CONFIG_KGDB_KDB */
209
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930210static void module_assert_mutex(void)
211{
212 lockdep_assert_held(&module_mutex);
213}
214
215static void module_assert_mutex_or_preempt(void)
216{
217#ifdef CONFIG_LOCKDEP
218 if (unlikely(!debug_locks))
219 return;
220
221 WARN_ON(!rcu_read_lock_sched_held() &&
222 !lockdep_is_held(&module_mutex));
223#endif
224}
225
Rusty Russell106a4ee2012-09-26 10:09:40 +0100226#ifdef CONFIG_MODULE_SIG
227#ifdef CONFIG_MODULE_SIG_FORCE
228static bool sig_enforce = true;
229#else
230static bool sig_enforce = false;
231
232static int param_set_bool_enable_only(const char *val,
233 const struct kernel_param *kp)
234{
235 int err;
236 bool test;
237 struct kernel_param dummy_kp = *kp;
238
239 dummy_kp.arg = &test;
240
241 err = param_set_bool(val, &dummy_kp);
242 if (err)
243 return err;
244
245 /* Don't let them unset it once it's set! */
246 if (!test && sig_enforce)
247 return -EROFS;
248
249 if (test)
250 sig_enforce = true;
251 return 0;
252}
253
254static const struct kernel_param_ops param_ops_bool_enable_only = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930255 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell106a4ee2012-09-26 10:09:40 +0100256 .set = param_set_bool_enable_only,
257 .get = param_get_bool,
258};
259#define param_check_bool_enable_only param_check_bool
260
261module_param(sig_enforce, bool_enable_only, 0644);
262#endif /* !CONFIG_MODULE_SIG_FORCE */
263#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Stephen Rothwell19e45292009-04-14 17:27:18 +1000265/* Block module loading/unloading? */
266int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800267core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000268
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500269/* Waiting for a module to finish initializing? */
270static DECLARE_WAIT_QUEUE_HEAD(module_wq);
271
Alan Sterne041c682006-03-27 01:16:30 -0800272static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Rusty Russell75676502010-06-05 11:17:36 -0600274/* Bounds of module allocation, for speeding __module_address.
275 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500276static unsigned long module_addr_min = -1UL, module_addr_max = 0;
277
Ionut Alexa6da0b562014-11-10 09:31:29 +1030278int register_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Alan Sterne041c682006-03-27 01:16:30 -0800280 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282EXPORT_SYMBOL(register_module_notifier);
283
Ionut Alexa6da0b562014-11-10 09:31:29 +1030284int unregister_module_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Alan Sterne041c682006-03-27 01:16:30 -0800286 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288EXPORT_SYMBOL(unregister_module_notifier);
289
Rusty Russelleded41c2010-08-05 12:59:07 -0600290struct load_info {
291 Elf_Ehdr *hdr;
292 unsigned long len;
293 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600294 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600295 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600296 struct _ddebug *debug;
297 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100298 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600299 struct {
300 unsigned int sym, str, mod, vers, info, pcpu;
301 } index;
302};
303
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800304/* We require a truly strong try_module_get(): 0 means failure due to
305 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static inline int strong_try_module_get(struct module *mod)
307{
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030308 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500310 return -EBUSY;
311 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500313 else
314 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Rusty Russell373d4d02013-01-21 17:17:39 +1030317static inline void add_taint_module(struct module *mod, unsigned flag,
318 enum lockdep_ok lockdep_ok)
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700319{
Rusty Russell373d4d02013-01-21 17:17:39 +1030320 add_taint(flag, lockdep_ok);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700321 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700322}
323
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200324/*
325 * A thread that wants to hold a reference to a module only while it
326 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328void __module_put_and_exit(struct module *mod, long code)
329{
330 module_put(mod);
331 do_exit(code);
332}
333EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600336static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned int i;
339
Rusty Russell49668682010-08-05 12:59:10 -0600340 for (i = 1; i < info->hdr->e_shnum; i++) {
341 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600343 if ((shdr->sh_flags & SHF_ALLOC)
344 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
Rusty Russell5e458cc2008-10-22 10:00:13 -0500350/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600351static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500352{
353 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600354 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500355}
356
357/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600358static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500359 const char *name,
360 size_t object_size,
361 unsigned int *num)
362{
Rusty Russell49668682010-08-05 12:59:10 -0600363 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500364
365 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600366 *num = info->sechdrs[sec].sh_size / object_size;
367 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/* Provided by the linker */
371extern const struct kernel_symbol __start___ksymtab[];
372extern const struct kernel_symbol __stop___ksymtab[];
373extern const struct kernel_symbol __start___ksymtab_gpl[];
374extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800375extern const struct kernel_symbol __start___ksymtab_gpl_future[];
376extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377extern const unsigned long __start___kcrctab[];
378extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800379extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500380#ifdef CONFIG_UNUSED_SYMBOLS
381extern const struct kernel_symbol __start___ksymtab_unused[];
382extern const struct kernel_symbol __stop___ksymtab_unused[];
383extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
384extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700385extern const unsigned long __start___kcrctab_unused[];
386extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389#ifndef CONFIG_MODVERSIONS
390#define symversion(base, idx) NULL
391#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800392#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
394
Rusty Russelldafd0942008-07-22 19:24:25 -0500395static bool each_symbol_in_section(const struct symsearch *arr,
396 unsigned int arrsize,
397 struct module *owner,
398 bool (*fn)(const struct symsearch *syms,
399 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200400 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500401 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100402{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200403 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500404
405 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200406 if (fn(&arr[j], owner, data))
407 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500408 }
409
410 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100411}
412
Rusty Russelldafd0942008-07-22 19:24:25 -0500413/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200414bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
415 struct module *owner,
416 void *data),
417 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700418{
Rusty Russelldafd0942008-07-22 19:24:25 -0500419 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600420 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500421 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
422 NOT_GPL_ONLY, false },
423 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
424 __start___kcrctab_gpl,
425 GPL_ONLY, false },
426 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
427 __start___kcrctab_gpl_future,
428 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500429#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500430 { __start___ksymtab_unused, __stop___ksymtab_unused,
431 __start___kcrctab_unused,
432 NOT_GPL_ONLY, true },
433 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
434 __start___kcrctab_unused_gpl,
435 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500436#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500437 };
438
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930439 module_assert_mutex_or_preempt();
440
Rusty Russelldafd0942008-07-22 19:24:25 -0500441 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
442 return true;
443
Andi Kleend72b3752008-08-30 10:09:00 +0200444 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500445 struct symsearch arr[] = {
446 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
447 NOT_GPL_ONLY, false },
448 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
449 mod->gpl_crcs,
450 GPL_ONLY, false },
451 { mod->gpl_future_syms,
452 mod->gpl_future_syms + mod->num_gpl_future_syms,
453 mod->gpl_future_crcs,
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 { mod->unused_syms,
457 mod->unused_syms + mod->num_unused_syms,
458 mod->unused_crcs,
459 NOT_GPL_ONLY, true },
460 { mod->unused_gpl_syms,
461 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
462 mod->unused_gpl_crcs,
463 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500464#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500465 };
466
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030467 if (mod->state == MODULE_STATE_UNFORMED)
468 continue;
469
Rusty Russelldafd0942008-07-22 19:24:25 -0500470 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
471 return true;
472 }
473 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700474}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200475EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700476
Rusty Russelldafd0942008-07-22 19:24:25 -0500477struct find_symbol_arg {
478 /* Input */
479 const char *name;
480 bool gplok;
481 bool warn;
482
483 /* Output */
484 struct module *owner;
485 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500486 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500487};
488
Rusty Russellde4d8d52011-04-19 21:49:58 +0200489static bool check_symbol(const struct symsearch *syms,
490 struct module *owner,
491 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500492{
Rusty Russelldafd0942008-07-22 19:24:25 -0500493 struct find_symbol_arg *fsa = data;
494
Rusty Russelldafd0942008-07-22 19:24:25 -0500495 if (!fsa->gplok) {
496 if (syms->licence == GPL_ONLY)
497 return false;
498 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800499 pr_warn("Symbol %s is being used by a non-GPL module, "
500 "which will not be allowed in the future\n",
501 fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500502 }
503 }
504
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500505#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500506 if (syms->unused && fsa->warn) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800507 pr_warn("Symbol %s is marked as UNUSED, however this module is "
508 "using it.\n", fsa->name);
509 pr_warn("This symbol will go away in the future.\n");
Yannick Guerrini7b63c3a2015-03-24 12:31:40 +1030510 pr_warn("Please evaluate if this is the right api to use and "
511 "if it really is, submit a report to the linux kernel "
512 "mailing list together with submitting your code for "
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800513 "inclusion.\n");
Rusty Russellad9546c2008-05-01 21:14:59 -0500514 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500515#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500516
517 fsa->owner = owner;
518 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500519 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500520 return true;
521}
522
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200523static int cmp_name(const void *va, const void *vb)
524{
525 const char *a;
526 const struct kernel_symbol *b;
527 a = va; b = vb;
528 return strcmp(a, b->name);
529}
530
Rusty Russellde4d8d52011-04-19 21:49:58 +0200531static bool find_symbol_in_section(const struct symsearch *syms,
532 struct module *owner,
533 void *data)
534{
535 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200536 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200537
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200538 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
539 sizeof(struct kernel_symbol), cmp_name);
540
541 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
542 return true;
543
Rusty Russellde4d8d52011-04-19 21:49:58 +0200544 return false;
545}
546
Tim Abbott414fd312008-12-05 19:03:56 -0500547/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600548 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500549const struct kernel_symbol *find_symbol(const char *name,
550 struct module **owner,
551 const unsigned long **crc,
552 bool gplok,
553 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Rusty Russelldafd0942008-07-22 19:24:25 -0500555 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Rusty Russelldafd0942008-07-22 19:24:25 -0500557 fsa.name = name;
558 fsa.gplok = gplok;
559 fsa.warn = warn;
560
Rusty Russellde4d8d52011-04-19 21:49:58 +0200561 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500562 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500563 *owner = fsa.owner;
564 if (crc)
565 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500566 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500568
Jim Cromie5e124162011-12-06 12:11:31 -0700569 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500570 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
Tim Abbottc6b37802008-12-05 19:03:59 -0500572EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/* Search for module by name: must hold module_mutex. */
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930575static struct module *find_module_all(const char *name, size_t len,
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030576 bool even_unformed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct module *mod;
579
Peter Zijlstra0be964b2015-05-27 11:09:35 +0930580 module_assert_mutex();
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030583 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
584 continue;
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930585 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return mod;
587 }
588 return NULL;
589}
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030590
591struct module *find_module(const char *name)
592{
Mathias Krause4f6de4d2013-07-02 15:35:11 +0930593 return find_module_all(name, strlen(name), false);
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030594}
Tim Abbottc6b37802008-12-05 19:03:59 -0500595EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900598
Tejun Heo259354d2010-03-10 18:56:10 +0900599static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900600{
Tejun Heo259354d2010-03-10 18:56:10 +0900601 return mod->percpu;
602}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900603
Rusty Russell9eb76d72013-07-03 10:06:29 +0930604static int percpu_modalloc(struct module *mod, struct load_info *info)
Tejun Heo259354d2010-03-10 18:56:10 +0900605{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930606 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
607 unsigned long align = pcpusec->sh_addralign;
608
609 if (!pcpusec->sh_size)
610 return 0;
611
Tejun Heofbf59bc2009-02-20 16:29:08 +0900612 if (align > PAGE_SIZE) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800613 pr_warn("%s: per-cpu alignment %li > %li\n",
614 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900615 align = PAGE_SIZE;
616 }
617
Rusty Russell9eb76d72013-07-03 10:06:29 +0930618 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
Tejun Heo259354d2010-03-10 18:56:10 +0900619 if (!mod->percpu) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800620 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
621 mod->name, (unsigned long)pcpusec->sh_size);
Tejun Heo259354d2010-03-10 18:56:10 +0900622 return -ENOMEM;
623 }
Rusty Russell9eb76d72013-07-03 10:06:29 +0930624 mod->percpu_size = pcpusec->sh_size;
Tejun Heo259354d2010-03-10 18:56:10 +0900625 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900626}
627
Tejun Heo259354d2010-03-10 18:56:10 +0900628static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900629{
Tejun Heo259354d2010-03-10 18:56:10 +0900630 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900631}
632
Rusty Russell49668682010-08-05 12:59:10 -0600633static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900634{
Rusty Russell49668682010-08-05 12:59:10 -0600635 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900636}
637
Tejun Heo259354d2010-03-10 18:56:10 +0900638static void percpu_modcopy(struct module *mod,
639 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900640{
641 int cpu;
642
643 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900644 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900645}
646
Tejun Heo10fad5e2010-03-10 18:57:54 +0900647/**
648 * is_module_percpu_address - test whether address is from module static percpu
649 * @addr: address to test
650 *
651 * Test whether @addr belongs to module static percpu area.
652 *
653 * RETURNS:
654 * %true if @addr is from module static percpu area
655 */
656bool is_module_percpu_address(unsigned long addr)
657{
658 struct module *mod;
659 unsigned int cpu;
660
661 preempt_disable();
662
663 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +1030664 if (mod->state == MODULE_STATE_UNFORMED)
665 continue;
Tejun Heo10fad5e2010-03-10 18:57:54 +0900666 if (!mod->percpu_size)
667 continue;
668 for_each_possible_cpu(cpu) {
669 void *start = per_cpu_ptr(mod->percpu, cpu);
670
671 if ((void *)addr >= start &&
672 (void *)addr < start + mod->percpu_size) {
673 preempt_enable();
674 return true;
675 }
676 }
677 }
678
679 preempt_enable();
680 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900684
Tejun Heo259354d2010-03-10 18:56:10 +0900685static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 return NULL;
688}
Rusty Russell9eb76d72013-07-03 10:06:29 +0930689static int percpu_modalloc(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Rusty Russell9eb76d72013-07-03 10:06:29 +0930691 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
692 if (info->sechdrs[info->index.pcpu].sh_size != 0)
693 return -ENOMEM;
694 return 0;
Tejun Heo259354d2010-03-10 18:56:10 +0900695}
696static inline void percpu_modfree(struct module *mod)
697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Rusty Russell49668682010-08-05 12:59:10 -0600699static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 return 0;
702}
Tejun Heo259354d2010-03-10 18:56:10 +0900703static inline void percpu_modcopy(struct module *mod,
704 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 /* pcpusec should be 0, and size of that section should be 0. */
707 BUG_ON(size != 0);
708}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900709bool is_module_percpu_address(unsigned long addr)
710{
711 return false;
712}
Tejun Heo6b588c12009-02-20 16:29:07 +0900713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#endif /* CONFIG_SMP */
715
Matt Domschc988d2b2005-06-23 22:05:15 -0700716#define MODINFO_ATTR(field) \
717static void setup_modinfo_##field(struct module *mod, const char *s) \
718{ \
719 mod->field = kstrdup(s, GFP_KERNEL); \
720} \
721static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930722 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700723{ \
Chen Gangcc56ded2013-08-20 15:34:21 +0930724 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700725} \
726static int modinfo_##field##_exists(struct module *mod) \
727{ \
728 return mod->field != NULL; \
729} \
730static void free_modinfo_##field(struct module *mod) \
731{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700732 kfree(mod->field); \
733 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700734} \
735static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900736 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700737 .show = show_modinfo_##field, \
738 .setup = setup_modinfo_##field, \
739 .test = modinfo_##field##_exists, \
740 .free = free_modinfo_##field, \
741};
742
743MODINFO_ATTR(version);
744MODINFO_ATTR(srcversion);
745
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100746static char last_unloaded_module[MODULE_NAME_LEN+1];
747
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800748#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400749
750EXPORT_TRACEPOINT_SYMBOL(module_get);
751
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030752/* MODULE_REF_BASE is the base reference count by kmodule loader. */
753#define MODULE_REF_BASE 1
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600756static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030758 /*
759 * Initialize reference counter to MODULE_REF_BASE.
760 * refcnt == 0 means module is going.
761 */
762 atomic_set(&mod->refcnt, MODULE_REF_BASE);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600763
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700764 INIT_LIST_HEAD(&mod->source_list);
765 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Hold reference count during initialization. */
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030768 atomic_inc(&mod->refcnt);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600769
770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/* Does a already use b? */
774static int already_uses(struct module *a, struct module *b)
775{
776 struct module_use *use;
777
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700778 list_for_each_entry(use, &b->source_list, source_list) {
779 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700780 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 1;
782 }
783 }
Jim Cromie5e124162011-12-06 12:11:31 -0700784 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700788/*
789 * Module a uses b
790 * - we add 'a' as a "source", 'b' as a "target" of module use
791 * - the module_use is added to the list of 'b' sources (so
792 * 'b' can walk the list to see who sourced them), and of 'a'
793 * targets (so 'a' can see what modules it targets).
794 */
795static int add_module_usage(struct module *a, struct module *b)
796{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700797 struct module_use *use;
798
Jim Cromie5e124162011-12-06 12:11:31 -0700799 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700800 use = kmalloc(sizeof(*use), GFP_ATOMIC);
801 if (!use) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -0800802 pr_warn("%s: out of memory loading\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700803 return -ENOMEM;
804 }
805
806 use->source = a;
807 use->target = b;
808 list_add(&use->source_list, &b->source_list);
809 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700810 return 0;
811}
812
Rusty Russell75676502010-06-05 11:17:36 -0600813/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600814int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600816 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100817
Rusty Russell9bea7f22010-06-05 11:17:37 -0600818 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700819 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700820
Rusty Russell9bea7f22010-06-05 11:17:37 -0600821 /* If module isn't available, we fail. */
822 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500823 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700826 err = add_module_usage(a, b);
827 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600829 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600833EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835/* Clear the unload stuff of the module. */
836static void module_unload_free(struct module *mod)
837{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700838 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Rusty Russell75676502010-06-05 11:17:36 -0600840 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700841 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
842 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700843 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700844 module_put(i);
845 list_del(&use->source_list);
846 list_del(&use->target_list);
847 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Rusty Russell75676502010-06-05 11:17:36 -0600849 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800853static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 int ret = (flags & O_TRUNC);
856 if (ret)
Rusty Russell373d4d02013-01-21 17:17:39 +1030857 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
859}
860#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800861static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 return 0;
864}
865#endif /* CONFIG_MODULE_FORCE_UNLOAD */
866
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030867/* Try to release refcount of module, 0 means success. */
868static int try_release_module_ref(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030870 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030872 /* Try to decrement refcnt which we set at loading */
873 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
874 BUG_ON(ret < 0);
875 if (ret)
876 /* Someone can put this right now, recover with checking */
877 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static int try_stop_module(struct module *mod, int flags, int *forced)
883{
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030884 /* If it's not unused, quit unless we're forcing. */
885 if (try_release_module_ref(mod) != 0) {
886 *forced = try_force_unload(flags);
887 if (!(*forced))
888 return -EWOULDBLOCK;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Masami Hiramatsue513cc12014-11-10 09:30:29 +1030891 /* Mark it as dying. */
892 mod->state = MODULE_STATE_GOING;
893
894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Rusty Russelld5db1392015-01-22 11:13:14 +1030897/**
898 * module_refcount - return the refcount or -1 if unloading
899 *
900 * @mod: the module we're checking
901 *
902 * Returns:
903 * -1 if the module is in the process of unloading
904 * otherwise the number of references in the kernel to the module
905 */
906int module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Rusty Russelld5db1392015-01-22 11:13:14 +1030908 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910EXPORT_SYMBOL(module_refcount);
911
912/* This exists whether we can unload or not */
913static void free_module(struct module *mod);
914
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100915SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
916 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800919 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int ret, forced = 0;
921
Kees Cook3d433212009-04-02 15:49:29 -0700922 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800923 return -EPERM;
924
925 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
926 return -EFAULT;
927 name[MODULE_NAME_LEN-1] = '\0';
928
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200929 if (mutex_lock_interruptible(&module_mutex) != 0)
930 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 mod = find_module(name);
933 if (!mod) {
934 ret = -ENOENT;
935 goto out;
936 }
937
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700938 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* Other modules depend on us: get rid of them first. */
940 ret = -EWOULDBLOCK;
941 goto out;
942 }
943
944 /* Doing init or already dying? */
945 if (mod->state != MODULE_STATE_LIVE) {
Rusty Russell3f2b9c92013-09-17 05:48:51 +0930946 /* FIXME: if (force), slam module count damn the torpedoes */
Jim Cromie5e124162011-12-06 12:11:31 -0700947 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ret = -EBUSY;
949 goto out;
950 }
951
952 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700953 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800954 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!forced) {
956 /* This module can't be removed */
957 ret = -EBUSY;
958 goto out;
959 }
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Stop the machine so refcounts can't move and disable module. */
963 ret = try_stop_module(mod, flags, &forced);
964 if (ret != 0)
965 goto out;
966
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200967 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200969 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200971 blocking_notifier_call_chain(&module_notify_list,
972 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800973 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600974
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100975 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500976 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Rusty Russell75676502010-06-05 11:17:36 -0600978 free_module(mod);
979 return 0;
980out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800981 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return ret;
983}
984
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800985static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct module_use *use;
988 int printed_something = 0;
989
Rusty Russelld5db1392015-01-22 11:13:14 +1030990 seq_printf(m, " %i ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Ionut Alexa6da0b562014-11-10 09:31:29 +1030992 /*
993 * Always include a trailing , so userspace can differentiate
994 * between this and the old multi-field proc format.
995 */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700996 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700998 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (mod->init != NULL && mod->exit == NULL) {
1002 printed_something = 1;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301003 seq_puts(m, "[permanent],");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
1006 if (!printed_something)
Ionut Alexa6da0b562014-11-10 09:31:29 +10301007 seq_puts(m, "-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010void __symbol_put(const char *symbol)
1011{
1012 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Rusty Russell24da1cb2007-07-15 23:41:46 -07001014 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001015 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 BUG();
1017 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -07001018 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020EXPORT_SYMBOL(__symbol_put);
1021
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301022/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023void symbol_put_addr(void *addr)
1024{
Trent Piepho5e376612006-05-15 09:44:06 -07001025 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301026 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301028 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -07001029 return;
1030
Rusty Russella6e6abd2009-03-31 13:05:31 -06001031 /* module_text_address is safe here: we're supposed to have reference
1032 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +09301033 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -06001034 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -07001035 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037EXPORT_SYMBOL_GPL(symbol_put_addr);
1038
1039static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301040 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Rusty Russelld5db1392015-01-22 11:13:14 +10301042 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Kay Sieverscca3e702012-01-13 09:32:15 +10301045static struct module_attribute modinfo_refcnt =
1046 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Steven Rostedtd53799b2012-03-26 12:50:52 +10301048void __module_get(struct module *module)
1049{
1050 if (module) {
1051 preempt_disable();
Masami Hiramatsu2f35c412014-11-10 09:29:29 +10301052 atomic_inc(&module->refcnt);
Steven Rostedtd53799b2012-03-26 12:50:52 +10301053 trace_module_get(module, _RET_IP_);
1054 preempt_enable();
1055 }
1056}
1057EXPORT_SYMBOL(__module_get);
1058
1059bool try_module_get(struct module *module)
1060{
1061 bool ret = true;
1062
1063 if (module) {
1064 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301065 /* Note: here, we can fail to get a reference */
1066 if (likely(module_is_live(module) &&
1067 atomic_inc_not_zero(&module->refcnt) != 0))
Steven Rostedtd53799b2012-03-26 12:50:52 +10301068 trace_module_get(module, _RET_IP_);
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301069 else
Steven Rostedtd53799b2012-03-26 12:50:52 +10301070 ret = false;
1071
1072 preempt_enable();
1073 }
1074 return ret;
1075}
1076EXPORT_SYMBOL(try_module_get);
1077
Al Virof6a57032006-10-18 01:47:25 -04001078void module_put(struct module *module)
1079{
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301080 int ret;
1081
Al Virof6a57032006-10-18 01:47:25 -04001082 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +09001083 preempt_disable();
Masami Hiramatsue513cc12014-11-10 09:30:29 +10301084 ret = atomic_dec_if_positive(&module->refcnt);
1085 WARN_ON(ret < 0); /* Failed to put refcount */
Li Zefanae832d12010-03-24 10:57:43 +08001086 trace_module_put(module, _RET_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +09001087 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -04001088 }
1089}
1090EXPORT_SYMBOL(module_put);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +08001093static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 /* We don't know the usage count, or what modules are using. */
Ionut Alexa6da0b562014-11-10 09:31:29 +10301096 seq_puts(m, " - -");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099static inline void module_unload_free(struct module *mod)
1100{
1101}
1102
Rusty Russell9bea7f22010-06-05 11:17:37 -06001103int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001105 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001107EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001109static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113#endif /* CONFIG_MODULE_UNLOAD */
1114
Kevin Winchester53999bf2012-01-15 19:32:55 -04001115static size_t module_flags_taint(struct module *mod, char *buf)
1116{
1117 size_t l = 0;
1118
1119 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1120 buf[l++] = 'P';
1121 if (mod->taints & (1 << TAINT_OOT_MODULE))
1122 buf[l++] = 'O';
1123 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1124 buf[l++] = 'F';
1125 if (mod->taints & (1 << TAINT_CRAP))
1126 buf[l++] = 'C';
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10301127 if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
Rusty Russell57673c22014-03-31 14:39:57 +10301128 buf[l++] = 'E';
Kevin Winchester53999bf2012-01-15 19:32:55 -04001129 /*
1130 * TAINT_FORCED_RMMOD: could be added.
Dave Jones8c904872014-02-26 10:49:49 -05001131 * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
Kevin Winchester53999bf2012-01-15 19:32:55 -04001132 * apply to modules.
1133 */
1134 return l;
1135}
1136
Kay Sievers1f717402006-11-24 12:15:25 +01001137static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301138 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001139{
1140 const char *state = "unknown";
1141
Kay Sievers4befb022011-07-24 22:06:04 +09301142 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001143 case MODULE_STATE_LIVE:
1144 state = "live";
1145 break;
1146 case MODULE_STATE_COMING:
1147 state = "coming";
1148 break;
1149 case MODULE_STATE_GOING:
1150 state = "going";
1151 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301152 default:
1153 BUG();
Kay Sievers1f717402006-11-24 12:15:25 +01001154 }
1155 return sprintf(buffer, "%s\n", state);
1156}
1157
Kay Sieverscca3e702012-01-13 09:32:15 +10301158static struct module_attribute modinfo_initstate =
1159 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001160
Kay Sievers88bfa322011-07-24 22:06:04 +09301161static ssize_t store_uevent(struct module_attribute *mattr,
1162 struct module_kobject *mk,
1163 const char *buffer, size_t count)
1164{
1165 enum kobject_action action;
1166
1167 if (kobject_action_type(buffer, count, &action) == 0)
1168 kobject_uevent(&mk->kobj, action);
1169 return count;
1170}
1171
Kay Sieverscca3e702012-01-13 09:32:15 +10301172struct module_attribute module_uevent =
1173 __ATTR(uevent, 0200, NULL, store_uevent);
1174
1175static ssize_t show_coresize(struct module_attribute *mattr,
1176 struct module_kobject *mk, char *buffer)
1177{
1178 return sprintf(buffer, "%u\n", mk->mod->core_size);
1179}
1180
1181static struct module_attribute modinfo_coresize =
1182 __ATTR(coresize, 0444, show_coresize, NULL);
1183
1184static ssize_t show_initsize(struct module_attribute *mattr,
1185 struct module_kobject *mk, char *buffer)
1186{
1187 return sprintf(buffer, "%u\n", mk->mod->init_size);
1188}
1189
1190static struct module_attribute modinfo_initsize =
1191 __ATTR(initsize, 0444, show_initsize, NULL);
1192
1193static ssize_t show_taint(struct module_attribute *mattr,
1194 struct module_kobject *mk, char *buffer)
1195{
1196 size_t l;
1197
1198 l = module_flags_taint(mk->mod, buffer);
1199 buffer[l++] = '\n';
1200 return l;
1201}
1202
1203static struct module_attribute modinfo_taint =
1204 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301205
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001206static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301207 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001208 &modinfo_version,
1209 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301210 &modinfo_initstate,
1211 &modinfo_coresize,
1212 &modinfo_initsize,
1213 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001214#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301215 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001216#endif
1217 NULL,
1218};
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220static const char vermagic[] = VERMAGIC_STRING;
1221
Rusty Russellc6e665c2009-03-31 13:05:33 -06001222static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001223{
1224#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001225 if (!test_taint(TAINT_FORCED_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001226 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
Rusty Russell373d4d02013-01-21 17:17:39 +10301227 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds826e4502008-05-04 17:04:16 -07001228 return 0;
1229#else
1230 return -ENOEXEC;
1231#endif
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001235/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1236static unsigned long maybe_relocated(unsigned long crc,
1237 const struct module *crc_owner)
1238{
1239#ifdef ARCH_RELOCATES_KCRCTAB
1240 if (crc_owner == NULL)
1241 return crc - (unsigned long)reloc_start;
1242#endif
1243 return crc;
1244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static int check_version(Elf_Shdr *sechdrs,
1247 unsigned int versindex,
1248 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301249 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001250 const unsigned long *crc,
1251 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 unsigned int i, num_versions;
1254 struct modversion_info *versions;
1255
1256 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1257 if (!crc)
1258 return 1;
1259
Rusty Russella5dd6972008-05-09 16:24:21 +10001260 /* No versions at all? modprobe --force does this. */
1261 if (versindex == 0)
1262 return try_to_force_load(mod, symname) == 0;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 versions = (void *) sechdrs[versindex].sh_addr;
1265 num_versions = sechdrs[versindex].sh_size
1266 / sizeof(struct modversion_info);
1267
1268 for (i = 0; i < num_versions; i++) {
1269 if (strcmp(versions[i].name, symname) != 0)
1270 continue;
1271
Rusty Russelld4703ae2009-12-15 16:28:32 -06001272 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001274 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001275 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001276 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001278
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001279 pr_warn("%s: no symbol version for %s\n", mod->name, symname);
Rusty Russella5dd6972008-05-09 16:24:21 +10001280 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001281
1282bad_version:
Ionut Alexa6da0b562014-11-10 09:31:29 +10301283 pr_warn("%s: disagrees about version of symbol %s\n",
Linus Torvalds826e4502008-05-04 17:04:16 -07001284 mod->name, symname);
1285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1289 unsigned int versindex,
1290 struct module *mod)
1291{
1292 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301294 /*
1295 * Since this should be found in kernel (which can't be removed), no
1296 * locking is necessary -- use preempt_disable() to placate lockdep.
1297 */
1298 preempt_disable();
Rusty Russellb92021b2013-03-15 15:04:17 +10301299 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301300 &crc, true, false)) {
1301 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 BUG();
Peter Zijlstra926a59b2015-05-27 11:09:35 +09301303 }
1304 preempt_enable();
James Hogana4b6a772013-03-18 19:38:56 +10301305 return check_version(sechdrs, versindex,
1306 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001307 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Rusty Russell91e37a72008-05-09 16:25:28 +10001310/* First part is kernel version, which we ignore if module has crcs. */
1311static inline int same_magic(const char *amagic, const char *bmagic,
1312 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Rusty Russell91e37a72008-05-09 16:25:28 +10001314 if (has_crcs) {
1315 amagic += strcspn(amagic, " ");
1316 bmagic += strcspn(bmagic, " ");
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return strcmp(amagic, bmagic) == 0;
1319}
1320#else
1321static inline int check_version(Elf_Shdr *sechdrs,
1322 unsigned int versindex,
1323 const char *symname,
Ionut Alexa6da0b562014-11-10 09:31:29 +10301324 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001325 const unsigned long *crc,
1326 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 return 1;
1329}
1330
1331static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1332 unsigned int versindex,
1333 struct module *mod)
1334{
1335 return 1;
1336}
1337
Rusty Russell91e37a72008-05-09 16:25:28 +10001338static inline int same_magic(const char *amagic, const char *bmagic,
1339 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 return strcmp(amagic, bmagic) == 0;
1342}
1343#endif /* CONFIG_MODVERSIONS */
1344
Rusty Russell75676502010-06-05 11:17:36 -06001345/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001346static const struct kernel_symbol *resolve_symbol(struct module *mod,
1347 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001348 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001349 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001352 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001354 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Peter Zijlstrad64810f2015-02-11 15:01:13 +10301356 /*
1357 * The module_mutex should not be a heavily contended lock;
1358 * if we get the occasional sleep here, we'll go an extra iteration
1359 * in the wait_event_interruptible(), which is harmless.
1360 */
1361 sched_annotate_sleep();
Rusty Russell75676502010-06-05 11:17:36 -06001362 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001363 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001364 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001365 if (!sym)
1366 goto unlock;
1367
Rusty Russell49668682010-08-05 12:59:10 -06001368 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1369 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001370 sym = ERR_PTR(-EINVAL);
1371 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001373
1374 err = ref_module(mod, owner);
1375 if (err) {
1376 sym = ERR_PTR(err);
1377 goto getname;
1378 }
1379
1380getname:
1381 /* We must make copy under the lock if we failed to get ref. */
1382 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1383unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001384 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001385 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Rusty Russell49668682010-08-05 12:59:10 -06001388static const struct kernel_symbol *
1389resolve_symbol_wait(struct module *mod,
1390 const struct load_info *info,
1391 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001392{
1393 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001394 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001395
1396 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001397 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1398 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001399 30 * HZ) <= 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001400 pr_warn("%s: gave up waiting for init of module %s.\n",
1401 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001402 }
1403 return ksym;
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/*
1407 * /sys/module/foo/sections stuff
1408 * J. Corbet <corbet@lwn.net>
1409 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001410#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001411
Rusty Russell8f6d0372010-08-05 12:59:09 -06001412#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001413static inline bool sect_empty(const Elf_Shdr *sect)
1414{
1415 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1416}
1417
Ionut Alexa6da0b562014-11-10 09:31:29 +10301418struct module_sect_attr {
Rusty Russella58730c2008-03-13 09:03:44 +00001419 struct module_attribute mattr;
1420 char *name;
1421 unsigned long address;
1422};
1423
Ionut Alexa6da0b562014-11-10 09:31:29 +10301424struct module_sect_attrs {
Rusty Russella58730c2008-03-13 09:03:44 +00001425 struct attribute_group grp;
1426 unsigned int nsections;
1427 struct module_sect_attr attrs[0];
1428};
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301431 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct module_sect_attr *sattr =
1434 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001435 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001438static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1439{
Rusty Russella58730c2008-03-13 09:03:44 +00001440 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001441
1442 for (section = 0; section < sect_attrs->nsections; section++)
1443 kfree(sect_attrs->attrs[section].name);
1444 kfree(sect_attrs);
1445}
1446
Rusty Russell8f6d0372010-08-05 12:59:09 -06001447static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 unsigned int nloaded = 0, i, size[2];
1450 struct module_sect_attrs *sect_attrs;
1451 struct module_sect_attr *sattr;
1452 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001455 for (i = 0; i < info->hdr->e_shnum; i++)
1456 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 nloaded++;
1458 size[0] = ALIGN(sizeof(*sect_attrs)
1459 + nloaded * sizeof(sect_attrs->attrs[0]),
1460 sizeof(sect_attrs->grp.attrs[0]));
1461 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001462 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1463 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465
1466 /* Setup section attributes. */
1467 sect_attrs->grp.name = "sections";
1468 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1469
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001470 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 sattr = &sect_attrs->attrs[0];
1472 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001473 for (i = 0; i < info->hdr->e_shnum; i++) {
1474 Elf_Shdr *sec = &info->sechdrs[i];
1475 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001476 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001477 sattr->address = sec->sh_addr;
1478 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001479 GFP_KERNEL);
1480 if (sattr->name == NULL)
1481 goto out;
1482 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001483 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 sattr->mattr.show = module_sect_show;
1485 sattr->mattr.store = NULL;
1486 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sattr->mattr.attr.mode = S_IRUGO;
1488 *(gattr++) = &(sattr++)->mattr.attr;
1489 }
1490 *gattr = NULL;
1491
1492 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1493 goto out;
1494
1495 mod->sect_attrs = sect_attrs;
1496 return;
1497 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001498 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501static void remove_sect_attrs(struct module *mod)
1502{
1503 if (mod->sect_attrs) {
1504 sysfs_remove_group(&mod->mkobj.kobj,
1505 &mod->sect_attrs->grp);
1506 /* We are positive that no one is using any sect attrs
1507 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001508 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 mod->sect_attrs = NULL;
1510 }
1511}
1512
Roland McGrath6d760132007-10-16 23:26:40 -07001513/*
1514 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1515 */
1516
1517struct module_notes_attrs {
1518 struct kobject *dir;
1519 unsigned int notes;
1520 struct bin_attribute attrs[0];
1521};
1522
Chris Wright2c3c8be2010-05-12 18:28:57 -07001523static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001524 struct bin_attribute *bin_attr,
1525 char *buf, loff_t pos, size_t count)
1526{
1527 /*
1528 * The caller checked the pos and count against our size.
1529 */
1530 memcpy(buf, bin_attr->private + pos, count);
1531 return count;
1532}
1533
1534static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1535 unsigned int i)
1536{
1537 if (notes_attrs->dir) {
1538 while (i-- > 0)
1539 sysfs_remove_bin_file(notes_attrs->dir,
1540 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001541 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001542 }
1543 kfree(notes_attrs);
1544}
1545
Rusty Russell8f6d0372010-08-05 12:59:09 -06001546static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001547{
1548 unsigned int notes, loaded, i;
1549 struct module_notes_attrs *notes_attrs;
1550 struct bin_attribute *nattr;
1551
Ingo Molnarea6bff32009-08-28 10:44:56 +02001552 /* failed to create section attributes, so can't create notes */
1553 if (!mod->sect_attrs)
1554 return;
1555
Roland McGrath6d760132007-10-16 23:26:40 -07001556 /* Count notes sections and allocate structures. */
1557 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001558 for (i = 0; i < info->hdr->e_shnum; i++)
1559 if (!sect_empty(&info->sechdrs[i]) &&
1560 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001561 ++notes;
1562
1563 if (notes == 0)
1564 return;
1565
1566 notes_attrs = kzalloc(sizeof(*notes_attrs)
1567 + notes * sizeof(notes_attrs->attrs[0]),
1568 GFP_KERNEL);
1569 if (notes_attrs == NULL)
1570 return;
1571
1572 notes_attrs->notes = notes;
1573 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001574 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1575 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001576 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001577 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001578 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001579 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1580 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001581 nattr->size = info->sechdrs[i].sh_size;
1582 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001583 nattr->read = module_notes_read;
1584 ++nattr;
1585 }
1586 ++loaded;
1587 }
1588
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001589 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001590 if (!notes_attrs->dir)
1591 goto out;
1592
1593 for (i = 0; i < notes; ++i)
1594 if (sysfs_create_bin_file(notes_attrs->dir,
1595 &notes_attrs->attrs[i]))
1596 goto out;
1597
1598 mod->notes_attrs = notes_attrs;
1599 return;
1600
1601 out:
1602 free_notes_attrs(notes_attrs, i);
1603}
1604
1605static void remove_notes_attrs(struct module *mod)
1606{
1607 if (mod->notes_attrs)
1608 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001612
Rusty Russell8f6d0372010-08-05 12:59:09 -06001613static inline void add_sect_attrs(struct module *mod,
1614 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616}
1617
1618static inline void remove_sect_attrs(struct module *mod)
1619{
1620}
Roland McGrath6d760132007-10-16 23:26:40 -07001621
Rusty Russell8f6d0372010-08-05 12:59:09 -06001622static inline void add_notes_attrs(struct module *mod,
1623 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001624{
1625}
1626
1627static inline void remove_notes_attrs(struct module *mod)
1628{
1629}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001630#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001632static void add_usage_links(struct module *mod)
1633{
1634#ifdef CONFIG_MODULE_UNLOAD
1635 struct module_use *use;
1636 int nowarn;
1637
Rusty Russell75676502010-06-05 11:17:36 -06001638 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001639 list_for_each_entry(use, &mod->target_list, target_list) {
1640 nowarn = sysfs_create_link(use->target->holders_dir,
1641 &mod->mkobj.kobj, mod->name);
1642 }
Rusty Russell75676502010-06-05 11:17:36 -06001643 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001644#endif
1645}
1646
1647static void del_usage_links(struct module *mod)
1648{
1649#ifdef CONFIG_MODULE_UNLOAD
1650 struct module_use *use;
1651
Rusty Russell75676502010-06-05 11:17:36 -06001652 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001653 list_for_each_entry(use, &mod->target_list, target_list)
1654 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001655 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001656#endif
1657}
1658
Rusty Russell6407ebb22010-06-05 11:17:36 -06001659static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001660{
1661 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001662 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001663 int error = 0;
1664 int i;
1665
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001666 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1667 (ARRAY_SIZE(modinfo_attrs) + 1)),
1668 GFP_KERNEL);
1669 if (!mod->modinfo_attrs)
1670 return -ENOMEM;
1671
1672 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001673 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1674 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001675 (attr->test && attr->test(mod))) {
1676 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001677 sysfs_attr_init(&temp_attr->attr);
Ionut Alexa6da0b562014-11-10 09:31:29 +10301678 error = sysfs_create_file(&mod->mkobj.kobj,
1679 &temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001680 ++temp_attr;
1681 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001682 }
1683 return error;
1684}
1685
Rusty Russell6407ebb22010-06-05 11:17:36 -06001686static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001687{
1688 struct module_attribute *attr;
1689 int i;
1690
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001691 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1692 /* pick a field to test for end of list */
1693 if (!attr->attr.name)
1694 break;
Ionut Alexa6da0b562014-11-10 09:31:29 +10301695 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001696 if (attr->free)
1697 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001698 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001699 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Li Zhong942e4432013-09-03 16:33:57 +09301702static void mod_kobject_put(struct module *mod)
1703{
1704 DECLARE_COMPLETION_ONSTACK(c);
1705 mod->mkobj.kobj_completion = &c;
1706 kobject_put(&mod->mkobj.kobj);
1707 wait_for_completion(&c);
1708}
1709
Rusty Russell6407ebb22010-06-05 11:17:36 -06001710static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001713 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001715 if (!module_sysfs_initialized) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001716 pr_err("%s: module sysfs not initialized\n", mod->name);
Ed Swierk1cc5f712006-09-25 16:25:36 -07001717 err = -EINVAL;
1718 goto out;
1719 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001720
1721 kobj = kset_find_obj(module_kset, mod->name);
1722 if (kobj) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08001723 pr_err("%s: module is already loaded\n", mod->name);
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001724 kobject_put(kobj);
1725 err = -EINVAL;
1726 goto out;
1727 }
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001730
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001731 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1732 mod->mkobj.kobj.kset = module_kset;
1733 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1734 "%s", mod->name);
1735 if (err)
Li Zhong942e4432013-09-03 16:33:57 +09301736 mod_kobject_put(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001737
Kay Sievers97c146e2007-11-29 23:46:11 +01001738 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001739out:
1740 return err;
1741}
1742
Rusty Russell6407ebb22010-06-05 11:17:36 -06001743static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001744 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001745 struct kernel_param *kparam,
1746 unsigned int num_params)
1747{
1748 int err;
1749
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001750 err = mod_sysfs_init(mod);
1751 if (err)
1752 goto out;
1753
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001754 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001755 if (!mod->holders_dir) {
1756 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001757 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001758 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 err = module_param_sysfs_setup(mod, kparam, num_params);
1761 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001762 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Matt Domschc988d2b2005-06-23 22:05:15 -07001764 err = module_add_modinfo_attrs(mod);
1765 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001766 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001767
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001768 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001769 add_sect_attrs(mod, info);
1770 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001771
Kay Sieverse17e0f52006-11-24 12:15:25 +01001772 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774
Kay Sieverse17e0f52006-11-24 12:15:25 +01001775out_unreg_param:
1776 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001777out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001778 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001779out_unreg:
Li Zhong942e4432013-09-03 16:33:57 +09301780 mod_kobject_put(mod);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001781out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return err;
1783}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001784
1785static void mod_sysfs_fini(struct module *mod)
1786{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001787 remove_notes_attrs(mod);
1788 remove_sect_attrs(mod);
Li Zhong942e4432013-09-03 16:33:57 +09301789 mod_kobject_put(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001790}
1791
Rusty Russell8f6d0372010-08-05 12:59:09 -06001792#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001793
Rusty Russell8f6d0372010-08-05 12:59:09 -06001794static int mod_sysfs_setup(struct module *mod,
1795 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001796 struct kernel_param *kparam,
1797 unsigned int num_params)
1798{
1799 return 0;
1800}
1801
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001802static void mod_sysfs_fini(struct module *mod)
1803{
1804}
1805
Rusty Russell36b03602010-08-05 12:59:09 -06001806static void module_remove_modinfo_attrs(struct module *mod)
1807{
1808}
1809
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001810static void del_usage_links(struct module *mod)
1811{
1812}
1813
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001814#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Rusty Russell36b03602010-08-05 12:59:09 -06001816static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001818 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001819 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001821 kobject_put(mod->mkobj.drivers_dir);
1822 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001823 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
matthieu castet84e1c6b2010-11-16 22:35:16 +01001826#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1827/*
1828 * LKM RO/NX protection: protect module's text/ro-data
1829 * from modification and any data from execution.
1830 */
1831void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1832{
1833 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1834 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1835
1836 if (end_pfn > begin_pfn)
1837 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1838}
1839
1840static void set_section_ro_nx(void *base,
1841 unsigned long text_size,
1842 unsigned long ro_size,
1843 unsigned long total_size)
1844{
1845 /* begin and end PFNs of the current subsection */
1846 unsigned long begin_pfn;
1847 unsigned long end_pfn;
1848
1849 /*
1850 * Set RO for module text and RO-data:
1851 * - Always protect first page.
1852 * - Do not protect last partial page.
1853 */
1854 if (ro_size > 0)
1855 set_page_attributes(base, base + ro_size, set_memory_ro);
1856
1857 /*
1858 * Set NX permissions for module data:
1859 * - Do not protect first partial page.
1860 * - Always protect last page.
1861 */
1862 if (total_size > text_size) {
1863 begin_pfn = PFN_UP((unsigned long)base + text_size);
1864 end_pfn = PFN_UP((unsigned long)base + total_size);
1865 if (end_pfn > begin_pfn)
1866 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1867 }
1868}
1869
Jan Glauber01526ed2011-05-19 16:55:26 -06001870static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001871{
Jan Glauber01526ed2011-05-19 16:55:26 -06001872 set_page_attributes(mod->module_core + mod->core_text_size,
1873 mod->module_core + mod->core_size,
1874 set_memory_x);
1875 set_page_attributes(mod->module_core,
1876 mod->module_core + mod->core_ro_size,
1877 set_memory_rw);
1878}
1879
1880static void unset_module_init_ro_nx(struct module *mod)
1881{
1882 set_page_attributes(mod->module_init + mod->init_text_size,
1883 mod->module_init + mod->init_size,
1884 set_memory_x);
1885 set_page_attributes(mod->module_init,
1886 mod->module_init + mod->init_ro_size,
1887 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001888}
1889
1890/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001891void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001892{
1893 struct module *mod;
1894
1895 mutex_lock(&module_mutex);
1896 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301897 if (mod->state == MODULE_STATE_UNFORMED)
1898 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001899 if ((mod->module_core) && (mod->core_text_size)) {
1900 set_page_attributes(mod->module_core,
1901 mod->module_core + mod->core_text_size,
1902 set_memory_rw);
1903 }
1904 if ((mod->module_init) && (mod->init_text_size)) {
1905 set_page_attributes(mod->module_init,
1906 mod->module_init + mod->init_text_size,
1907 set_memory_rw);
1908 }
1909 }
1910 mutex_unlock(&module_mutex);
1911}
1912
1913/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001914void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001915{
1916 struct module *mod;
1917
1918 mutex_lock(&module_mutex);
1919 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10301920 if (mod->state == MODULE_STATE_UNFORMED)
1921 continue;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001922 if ((mod->module_core) && (mod->core_text_size)) {
1923 set_page_attributes(mod->module_core,
1924 mod->module_core + mod->core_text_size,
1925 set_memory_ro);
1926 }
1927 if ((mod->module_init) && (mod->init_text_size)) {
1928 set_page_attributes(mod->module_init,
1929 mod->module_init + mod->init_text_size,
1930 set_memory_ro);
1931 }
1932 }
1933 mutex_unlock(&module_mutex);
1934}
1935#else
1936static 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 -06001937static void unset_module_core_ro_nx(struct module *mod) { }
1938static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001939#endif
1940
Rusty Russellbe1f2212015-01-20 09:07:05 +10301941void __weak module_memfree(void *module_region)
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001942{
1943 vfree(module_region);
1944}
1945
1946void __weak module_arch_cleanup(struct module *mod)
1947{
1948}
1949
Rusty Russelld453cde2015-01-20 09:07:04 +10301950void __weak module_arch_freeing_init(struct module *mod)
1951{
1952}
1953
Rusty Russell75676502010-06-05 11:17:36 -06001954/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955static void free_module(struct module *mod)
1956{
Li Zefan7ead8b82009-08-17 16:56:28 +08001957 trace_module_free(mod);
1958
Rusty Russell36b03602010-08-05 12:59:09 -06001959 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Rusty Russell944a1fa2013-04-17 13:20:03 +09301961 /* We leave it in list to prevent duplicate loads, but make sure
1962 * that noone uses it while it's being deconstructed. */
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301963 mutex_lock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301964 mod->state = MODULE_STATE_UNFORMED;
Prarit Bhargavad3051b42014-10-14 02:51:39 +10301965 mutex_unlock(&module_mutex);
Rusty Russell944a1fa2013-04-17 13:20:03 +09301966
Jason Baronb82bab4b2010-07-27 13:18:01 -07001967 /* Remove dynamic debug info */
1968 ddebug_remove_module(mod->name);
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* Arch-specific cleanup. */
1971 module_arch_cleanup(mod);
1972
1973 /* Module unload stuff */
1974 module_unload_free(mod);
1975
Rusty Russelle180a6b2009-03-31 13:05:29 -06001976 /* Free any allocated parameters. */
1977 destroy_params(mod->kp, mod->num_kp);
1978
Rusty Russell944a1fa2013-04-17 13:20:03 +09301979 /* Now we can delete it from the lists */
1980 mutex_lock(&module_mutex);
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10301981 /* Unlink carefully: kallsyms could be walking list. */
1982 list_del_rcu(&mod->list);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09301983 mod_tree_remove(mod);
Masami Hiramatsu0286b5e2014-11-10 09:28:29 +10301984 /* Remove this module from bug list, this uses list_del_rcu */
Masami Hiramatsu461e34a2014-11-10 09:27:29 +10301985 module_bug_cleanup(mod);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09301986 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
1987 synchronize_sched();
Rusty Russell944a1fa2013-04-17 13:20:03 +09301988 mutex_unlock(&module_mutex);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001991 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10301992 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10301993 module_memfree(mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001995 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001996
Peter Zijlstra35a93932015-02-26 16:23:11 +01001997 /* Free lock-classes; relies on the preceding sync_rcu(). */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001998 lockdep_free_key_range(mod->module_core, mod->core_size);
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06002001 unset_module_core_ro_nx(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10302002 module_memfree(mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07002003
2004#ifdef CONFIG_MPU
2005 update_protections(current->mm);
2006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009void *__symbol_get(const char *symbol)
2010{
2011 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05002012 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Rusty Russell24da1cb2007-07-15 23:41:46 -07002014 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05002015 sym = find_symbol(symbol, &owner, NULL, true, true);
2016 if (sym && strong_try_module_get(owner))
2017 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002018 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Tim Abbott414fd312008-12-05 19:03:56 -05002020 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022EXPORT_SYMBOL_GPL(__symbol_get);
2023
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002024/*
2025 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02002026 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06002027 *
2028 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002029 */
2030static int verify_export_symbols(struct module *mod)
2031{
Rusty Russellb2111042008-05-01 21:15:00 -05002032 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002033 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05002034 const struct kernel_symbol *s;
2035 struct {
2036 const struct kernel_symbol *sym;
2037 unsigned int num;
2038 } arr[] = {
2039 { mod->syms, mod->num_syms },
2040 { mod->gpl_syms, mod->num_gpl_syms },
2041 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002042#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05002043 { mod->unused_syms, mod->num_unused_syms },
2044 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002045#endif
Rusty Russellb2111042008-05-01 21:15:00 -05002046 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002047
Rusty Russellb2111042008-05-01 21:15:00 -05002048 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2049 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06002050 if (find_symbol(s->name, &owner, NULL, true, false)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002051 pr_err("%s: exports duplicate symbol %s"
Rusty Russellb2111042008-05-01 21:15:00 -05002052 " (owned by %s)\n",
2053 mod->name, s->name, module_name(owner));
2054 return -ENOEXEC;
2055 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002056 }
Rusty Russellb2111042008-05-01 21:15:00 -05002057 }
2058 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002059}
2060
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08002061/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06002062static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Rusty Russell49668682010-08-05 12:59:10 -06002064 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2065 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06002067 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05002069 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Rusty Russell49668682010-08-05 12:59:10 -06002071 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2072 const char *name = info->strtab + sym[i].st_name;
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 switch (sym[i].st_shndx) {
2075 case SHN_COMMON:
Joe Mario80375982014-02-08 09:01:09 +01002076 /* Ignore common symbols */
2077 if (!strncmp(name, "__gnu_lto", 9))
2078 break;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* We compiled with -fno-common. These are not
2081 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07002082 pr_debug("Common symbol: %s\n", name);
Ionut Alexa6da0b562014-11-10 09:31:29 +10302083 pr_warn("%s: please compile with -fno-common\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 mod->name);
2085 ret = -ENOEXEC;
2086 break;
2087
2088 case SHN_ABS:
2089 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07002090 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 (long)sym[i].st_value);
2092 break;
2093
2094 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06002095 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002097 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05002098 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 break;
Tim Abbott414fd312008-12-05 19:03:56 -05002100 }
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06002103 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 break;
2105
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002106 pr_warn("%s: Unknown symbol %s (err %li)\n",
2107 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06002108 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 break;
2110
2111 default:
2112 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06002113 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09002114 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 else
Rusty Russell49668682010-08-05 12:59:10 -06002116 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 sym[i].st_value += secbase;
2118 break;
2119 }
2120 }
2121
2122 return ret;
2123}
2124
Rusty Russell49668682010-08-05 12:59:10 -06002125static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06002126{
2127 unsigned int i;
2128 int err = 0;
2129
2130 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06002131 for (i = 1; i < info->hdr->e_shnum; i++) {
2132 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002133
2134 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002135 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002136 continue;
2137
2138 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002139 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002140 continue;
2141
Rusty Russell49668682010-08-05 12:59:10 -06002142 if (info->sechdrs[i].sh_type == SHT_REL)
2143 err = apply_relocate(info->sechdrs, info->strtab,
2144 info->index.sym, i, mod);
2145 else if (info->sechdrs[i].sh_type == SHT_RELA)
2146 err = apply_relocate_add(info->sechdrs, info->strtab,
2147 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002148 if (err < 0)
2149 break;
2150 }
2151 return err;
2152}
2153
Helge Deller088af9a2008-12-31 12:31:18 +01002154/* Additional bytes needed by arch in front of individual sections */
2155unsigned int __weak arch_mod_section_prepend(struct module *mod,
2156 unsigned int section)
2157{
2158 /* default implementation just returns zero */
2159 return 0;
2160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002163static long get_offset(struct module *mod, unsigned int *size,
2164 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 long ret;
2167
Helge Deller088af9a2008-12-31 12:31:18 +01002168 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2170 *size = ret + sechdr->sh_size;
2171 return ret;
2172}
2173
2174/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2175 might -- code, read-only data, read-write data, small data. Tally
2176 sizes, and place the offsets into sh_entsize fields: high bit means it
2177 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002178static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 static unsigned long const masks[][2] = {
2181 /* NOTE: all executable code must be the first section
2182 * in this array; otherwise modify the text_size
2183 * finder in the two loops below */
2184 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2185 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2186 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2187 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2188 };
2189 unsigned int m, i;
2190
Rusty Russell49668682010-08-05 12:59:10 -06002191 for (i = 0; i < info->hdr->e_shnum; i++)
2192 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Jim Cromie5e124162011-12-06 12:11:31 -07002194 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002196 for (i = 0; i < info->hdr->e_shnum; ++i) {
2197 Elf_Shdr *s = &info->sechdrs[i];
2198 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2201 || (s->sh_flags & masks[m][1])
2202 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002203 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002205 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002206 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002208 switch (m) {
2209 case 0: /* executable */
2210 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002212 break;
2213 case 1: /* RO: text and ro-data */
2214 mod->core_size = debug_align(mod->core_size);
2215 mod->core_ro_size = mod->core_size;
2216 break;
2217 case 3: /* whole core */
2218 mod->core_size = debug_align(mod->core_size);
2219 break;
2220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
Jim Cromie5e124162011-12-06 12:11:31 -07002223 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002225 for (i = 0; i < info->hdr->e_shnum; ++i) {
2226 Elf_Shdr *s = &info->sechdrs[i];
2227 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2230 || (s->sh_flags & masks[m][1])
2231 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002232 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002234 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002236 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002238 switch (m) {
2239 case 0: /* executable */
2240 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002242 break;
2243 case 1: /* RO: text and ro-data */
2244 mod->init_size = debug_align(mod->init_size);
2245 mod->init_ro_size = mod->init_size;
2246 break;
2247 case 3: /* whole init */
2248 mod->init_size = debug_align(mod->init_size);
2249 break;
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252}
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254static void set_license(struct module *mod, const char *license)
2255{
2256 if (!license)
2257 license = "unspecified";
2258
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002259 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002260 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002261 pr_warn("%s: module license '%s' taints kernel.\n",
2262 mod->name, license);
Rusty Russell373d4d02013-01-21 17:17:39 +10302263 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2264 LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266}
2267
2268/* Parse tag=value strings from .modinfo section */
2269static char *next_string(char *string, unsigned long *secsize)
2270{
2271 /* Skip non-zero chars */
2272 while (string[0]) {
2273 string++;
2274 if ((*secsize)-- <= 1)
2275 return NULL;
2276 }
2277
2278 /* Skip any zero padding. */
2279 while (!string[0]) {
2280 string++;
2281 if ((*secsize)-- <= 1)
2282 return NULL;
2283 }
2284 return string;
2285}
2286
Rusty Russell49668682010-08-05 12:59:10 -06002287static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 char *p;
2290 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002291 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2292 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Rusty Russell49668682010-08-05 12:59:10 -06002294 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2296 return p + taglen + 1;
2297 }
2298 return NULL;
2299}
2300
Rusty Russell49668682010-08-05 12:59:10 -06002301static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002302{
2303 struct module_attribute *attr;
2304 int i;
2305
2306 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2307 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002308 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002309 }
2310}
Matt Domschc988d2b2005-06-23 22:05:15 -07002311
Rusty Russella263f772009-09-25 00:32:58 -06002312static void free_modinfo(struct module *mod)
2313{
2314 struct module_attribute *attr;
2315 int i;
2316
2317 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2318 if (attr->free)
2319 attr->free(mod);
2320 }
2321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002324
2325/* lookup symbol in given range of kernel_symbols */
2326static const struct kernel_symbol *lookup_symbol(const char *name,
2327 const struct kernel_symbol *start,
2328 const struct kernel_symbol *stop)
2329{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002330 return bsearch(name, start, stop - start,
2331 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002332}
2333
Tim Abbottca4787b2009-01-05 08:40:10 -06002334static int is_exported(const char *name, unsigned long value,
2335 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Tim Abbottca4787b2009-01-05 08:40:10 -06002337 const struct kernel_symbol *ks;
2338 if (!mod)
2339 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002340 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002341 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2342 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002346static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Rusty Russelleded41c2010-08-05 12:59:07 -06002348 const Elf_Shdr *sechdrs = info->sechdrs;
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2351 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2352 return 'v';
2353 else
2354 return 'w';
2355 }
2356 if (sym->st_shndx == SHN_UNDEF)
2357 return 'U';
2358 if (sym->st_shndx == SHN_ABS)
2359 return 'a';
2360 if (sym->st_shndx >= SHN_LORESERVE)
2361 return '?';
2362 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2363 return 't';
2364 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2365 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2366 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2367 return 'r';
2368 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2369 return 'g';
2370 else
2371 return 'd';
2372 }
2373 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2374 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2375 return 's';
2376 else
2377 return 'b';
2378 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002379 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2380 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return '?';
2384}
2385
Jan Beulich4a496222009-07-06 14:50:42 +01002386static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
Ionut Alexa6da0b562014-11-10 09:31:29 +10302387 unsigned int shnum)
Jan Beulich4a496222009-07-06 14:50:42 +01002388{
2389 const Elf_Shdr *sec;
2390
2391 if (src->st_shndx == SHN_UNDEF
2392 || src->st_shndx >= shnum
2393 || !src->st_name)
2394 return false;
2395
2396 sec = sechdrs + src->st_shndx;
2397 if (!(sec->sh_flags & SHF_ALLOC)
2398#ifndef CONFIG_KALLSYMS_ALL
2399 || !(sec->sh_flags & SHF_EXECINSTR)
2400#endif
2401 || (sec->sh_entsize & INIT_OFFSET_MASK))
2402 return false;
2403
2404 return true;
2405}
2406
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302407/*
2408 * We only allocate and copy the strings needed by the parts of symtab
2409 * we keep. This is simple, but has the effect of making multiple
2410 * copies of duplicates. We could be more sophisticated, see
2411 * linux-kernel thread starting with
2412 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2413 */
Rusty Russell49668682010-08-05 12:59:10 -06002414static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002415{
Rusty Russell49668682010-08-05 12:59:10 -06002416 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2417 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002418 const Elf_Sym *src;
Satoru Takeuchi54523ec2012-12-05 12:29:04 +10302419 unsigned int i, nsrc, ndst, strtab_size = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002420
2421 /* Put symbol section at end of init part of module. */
2422 symsect->sh_flags |= SHF_ALLOC;
2423 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002424 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002425 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002426
Rusty Russell49668682010-08-05 12:59:10 -06002427 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002428 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002429
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302430 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302431 for (ndst = i = 0; i < nsrc; i++) {
2432 if (i == 0 ||
2433 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2434 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302435 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002436 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302437 }
Jan Beulich4a496222009-07-06 14:50:42 +01002438
2439 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002440 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302441 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2442 mod->core_size += strtab_size;
Laura Abbott168e47f2015-02-25 14:14:57 -08002443 mod->core_size = debug_align(mod->core_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002444
Jan Beulich554bdfe2009-07-06 14:51:44 +01002445 /* Put string table section at end of init part of module. */
2446 strsect->sh_flags |= SHF_ALLOC;
2447 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002448 info->index.str) | INIT_OFFSET_MASK;
Laura Abbott168e47f2015-02-25 14:14:57 -08002449 mod->init_size = debug_align(mod->init_size);
Jim Cromie5e124162011-12-06 12:11:31 -07002450 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002451}
2452
Rusty Russell811d66a2010-08-05 12:59:12 -06002453static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Jan Beulich4a496222009-07-06 14:50:42 +01002455 unsigned int i, ndst;
2456 const Elf_Sym *src;
2457 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002458 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002459 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Rusty Russelleded41c2010-08-05 12:59:07 -06002461 mod->symtab = (void *)symsec->sh_addr;
2462 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002463 /* Make sure we get permanent strtab: don't use info->strtab. */
2464 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /* Set types up while we still have access to sections. */
2467 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002468 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002469
Rusty Russelld9131882010-08-05 12:59:08 -06002470 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302471 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002472 src = mod->symtab;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302473 for (ndst = i = 0; i < mod->num_symtab; i++) {
2474 if (i == 0 ||
2475 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2476 dst[ndst] = src[i];
2477 dst[ndst++].st_name = s - mod->core_strtab;
2478 s += strlcpy(s, &mod->strtab[src[i].st_name],
2479 KSYM_NAME_LEN) + 1;
2480 }
Jan Beulich4a496222009-07-06 14:50:42 +01002481 }
2482 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484#else
Rusty Russell49668682010-08-05 12:59:10 -06002485static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002486{
2487}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002488
Michał Mirosławabbce902010-09-20 01:58:08 +02002489static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491}
2492#endif /* CONFIG_KALLSYMS */
2493
Jason Barone9d376f2009-02-05 11:51:38 -05002494static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002495{
Rusty Russell811d66a2010-08-05 12:59:12 -06002496 if (!debug)
2497 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002498#ifdef CONFIG_DYNAMIC_DEBUG
2499 if (ddebug_add_module(debug, num, debug->modname))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002500 pr_err("dynamic debug error adding module: %s\n",
2501 debug->modname);
Jason Barone9d376f2009-02-05 11:51:38 -05002502#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002503}
Jason Baron346e15b2008-08-12 16:46:19 -04002504
Yehuda Sadehff49d742010-07-03 13:07:35 +10002505static void dynamic_debug_remove(struct _ddebug *debug)
2506{
2507 if (debug)
2508 ddebug_remove_module(debug->modname);
2509}
2510
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002511void * __weak module_alloc(unsigned long size)
2512{
Rusty Russell82fab442012-12-11 09:38:33 +10302513 return vmalloc_exec(size);
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002514}
2515
Rusty Russell3a642e92008-07-22 19:24:28 -05002516static void *module_alloc_update_bounds(unsigned long size)
2517{
2518 void *ret = module_alloc(size);
2519
2520 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002521 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002522 /* Update module bounds. */
2523 if ((unsigned long)ret < module_addr_min)
2524 module_addr_min = (unsigned long)ret;
2525 if ((unsigned long)ret + size > module_addr_max)
2526 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002527 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002528 }
2529 return ret;
2530}
2531
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002532#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002533static void kmemleak_load_module(const struct module *mod,
2534 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002535{
2536 unsigned int i;
2537
2538 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002539 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002540
Rusty Russell49668682010-08-05 12:59:10 -06002541 for (i = 1; i < info->hdr->e_shnum; i++) {
Steven Rostedt06c94942013-05-15 20:33:01 +01002542 /* Scan all writable sections that's not executable */
2543 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2544 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2545 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002546 continue;
2547
Rusty Russell49668682010-08-05 12:59:10 -06002548 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2549 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002550 }
2551}
2552#else
Rusty Russell49668682010-08-05 12:59:10 -06002553static inline void kmemleak_load_module(const struct module *mod,
2554 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002555{
2556}
2557#endif
2558
Rusty Russell106a4ee2012-09-26 10:09:40 +01002559#ifdef CONFIG_MODULE_SIG
Kees Cook34e11692012-10-16 07:31:07 +10302560static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002561{
2562 int err = -ENOKEY;
Kees Cook34e11692012-10-16 07:31:07 +10302563 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2564 const void *mod = info->hdr;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002565
Kees Cook34e11692012-10-16 07:31:07 +10302566 if (info->len > markerlen &&
2567 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
David Howellscaabe242012-10-20 01:19:29 +01002568 /* We truncate the module to discard the signature */
Kees Cook34e11692012-10-16 07:31:07 +10302569 info->len -= markerlen;
2570 err = mod_verify_sig(mod, &info->len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002571 }
2572
2573 if (!err) {
2574 info->sig_ok = true;
2575 return 0;
2576 }
2577
2578 /* Not having a signature is only an error if we're strict. */
2579 if (err == -ENOKEY && !sig_enforce)
2580 err = 0;
2581
2582 return err;
2583}
2584#else /* !CONFIG_MODULE_SIG */
Kees Cook34e11692012-10-16 07:31:07 +10302585static int module_sig_check(struct load_info *info)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002586{
2587 return 0;
2588}
2589#endif /* !CONFIG_MODULE_SIG */
2590
Kees Cook34e11692012-10-16 07:31:07 +10302591/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2592static int elf_header_check(struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002593{
Kees Cook34e11692012-10-16 07:31:07 +10302594 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002595 return -ENOEXEC;
2596
Kees Cook34e11692012-10-16 07:31:07 +10302597 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2598 || info->hdr->e_type != ET_REL
2599 || !elf_check_arch(info->hdr)
2600 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2601 return -ENOEXEC;
2602
2603 if (info->hdr->e_shoff >= info->len
2604 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2605 info->len - info->hdr->e_shoff))
2606 return -ENOEXEC;
2607
2608 return 0;
2609}
2610
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002611#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2612
2613static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2614{
2615 do {
2616 unsigned long n = min(len, COPY_CHUNK_SIZE);
2617
2618 if (copy_from_user(dst, usrc, n) != 0)
2619 return -EFAULT;
2620 cond_resched();
2621 dst += n;
2622 usrc += n;
2623 len -= n;
2624 } while (len);
2625 return 0;
2626}
2627
Kees Cook34e11692012-10-16 07:31:07 +10302628/* Sets info->hdr and info->len. */
2629static int copy_module_from_user(const void __user *umod, unsigned long len,
2630 struct load_info *info)
2631{
Kees Cook2e72d512012-10-16 07:32:07 +10302632 int err;
2633
Kees Cook34e11692012-10-16 07:31:07 +10302634 info->len = len;
2635 if (info->len < sizeof(*(info->hdr)))
Rusty Russell40dd2562010-08-05 12:59:03 -06002636 return -ENOEXEC;
2637
Kees Cook2e72d512012-10-16 07:32:07 +10302638 err = security_kernel_module_from_file(NULL);
2639 if (err)
2640 return err;
2641
Rusty Russell40dd2562010-08-05 12:59:03 -06002642 /* Suck in entire file: we'll want most of it. */
Kirill A. Shutemovcc9e6052015-03-24 12:31:40 +10302643 info->hdr = __vmalloc(info->len,
2644 GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
Kees Cook34e11692012-10-16 07:31:07 +10302645 if (!info->hdr)
Rusty Russell40dd2562010-08-05 12:59:03 -06002646 return -ENOMEM;
2647
Linus Torvalds3afe9f82015-04-07 10:33:49 -07002648 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
Kees Cook34e11692012-10-16 07:31:07 +10302649 vfree(info->hdr);
2650 return -EFAULT;
Rusty Russell40dd2562010-08-05 12:59:03 -06002651 }
2652
Rusty Russell40dd2562010-08-05 12:59:03 -06002653 return 0;
Kees Cook34e11692012-10-16 07:31:07 +10302654}
Rusty Russell40dd2562010-08-05 12:59:03 -06002655
Kees Cook34e11692012-10-16 07:31:07 +10302656/* Sets info->hdr and info->len. */
2657static int copy_module_from_fd(int fd, struct load_info *info)
2658{
Al Viroa2e05782013-08-30 12:41:41 -04002659 struct fd f = fdget(fd);
Kees Cook34e11692012-10-16 07:31:07 +10302660 int err;
2661 struct kstat stat;
2662 loff_t pos;
2663 ssize_t bytes = 0;
2664
Al Viroa2e05782013-08-30 12:41:41 -04002665 if (!f.file)
Kees Cook34e11692012-10-16 07:31:07 +10302666 return -ENOEXEC;
2667
Al Viroa2e05782013-08-30 12:41:41 -04002668 err = security_kernel_module_from_file(f.file);
Kees Cook2e72d512012-10-16 07:32:07 +10302669 if (err)
2670 goto out;
2671
Al Viroa2e05782013-08-30 12:41:41 -04002672 err = vfs_getattr(&f.file->f_path, &stat);
Kees Cook34e11692012-10-16 07:31:07 +10302673 if (err)
2674 goto out;
2675
2676 if (stat.size > INT_MAX) {
2677 err = -EFBIG;
2678 goto out;
2679 }
Sasha Levin52441fa2013-01-03 11:09:53 +10302680
2681 /* Don't hand 0 to vmalloc, it whines. */
2682 if (stat.size == 0) {
2683 err = -EINVAL;
2684 goto out;
2685 }
2686
Kees Cook34e11692012-10-16 07:31:07 +10302687 info->hdr = vmalloc(stat.size);
2688 if (!info->hdr) {
2689 err = -ENOMEM;
2690 goto out;
2691 }
2692
2693 pos = 0;
2694 while (pos < stat.size) {
Al Viroa2e05782013-08-30 12:41:41 -04002695 bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
Kees Cook34e11692012-10-16 07:31:07 +10302696 stat.size - pos);
2697 if (bytes < 0) {
2698 vfree(info->hdr);
2699 err = bytes;
2700 goto out;
2701 }
2702 if (bytes == 0)
2703 break;
2704 pos += bytes;
2705 }
2706 info->len = pos;
2707
2708out:
Al Viroa2e05782013-08-30 12:41:41 -04002709 fdput(f);
Rusty Russell40dd2562010-08-05 12:59:03 -06002710 return err;
2711}
2712
Rusty Russelld9131882010-08-05 12:59:08 -06002713static void free_copy(struct load_info *info)
2714{
Rusty Russelld9131882010-08-05 12:59:08 -06002715 vfree(info->hdr);
2716}
2717
Rusty Russell2f3238a2012-10-22 18:09:41 +10302718static int rewrite_section_headers(struct load_info *info, int flags)
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002719{
2720 unsigned int i;
2721
2722 /* This should always be true, but let's be sure. */
2723 info->sechdrs[0].sh_addr = 0;
2724
2725 for (i = 1; i < info->hdr->e_shnum; i++) {
2726 Elf_Shdr *shdr = &info->sechdrs[i];
2727 if (shdr->sh_type != SHT_NOBITS
2728 && info->len < shdr->sh_offset + shdr->sh_size) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002729 pr_err("Module len %lu truncated\n", info->len);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002730 return -ENOEXEC;
2731 }
2732
2733 /* Mark all sections sh_addr with their address in the
2734 temporary image. */
2735 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2736
2737#ifndef CONFIG_MODULE_UNLOAD
2738 /* Don't load .exit sections */
2739 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2740 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2741#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002742 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002743
2744 /* Track but don't keep modinfo and version sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302745 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2746 info->index.vers = 0; /* Pretend no __versions section! */
2747 else
2748 info->index.vers = find_sec(info, "__versions");
Rusty Russell49668682010-08-05 12:59:10 -06002749 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002750 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2751 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002752 return 0;
2753}
2754
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002755/*
2756 * Set up our basic convenience variables (pointers to section headers,
2757 * search for module section index etc), and do some basic section
2758 * verification.
2759 *
2760 * Return the temporary module pointer (we'll replace it with the final
2761 * one when we move the module sections around).
2762 */
Rusty Russell2f3238a2012-10-22 18:09:41 +10302763static struct module *setup_load_info(struct load_info *info, int flags)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002764{
2765 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002766 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002767 struct module *mod;
2768
2769 /* Set up the convenience variables */
2770 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002771 info->secstrings = (void *)info->hdr
2772 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002773
Rusty Russell2f3238a2012-10-22 18:09:41 +10302774 err = rewrite_section_headers(info, flags);
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002775 if (err)
2776 return ERR_PTR(err);
2777
2778 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002779 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002780 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2781 info->index.sym = i;
2782 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002783 info->strtab = (char *)info->hdr
2784 + info->sechdrs[info->index.str].sh_offset;
2785 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002786 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002787 }
2788
Rusty Russell49668682010-08-05 12:59:10 -06002789 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002790 if (!info->index.mod) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002791 pr_warn("No module found in object\n");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002792 return ERR_PTR(-ENOEXEC);
2793 }
2794 /* This is temporary: point mod into copy of data. */
2795 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2796
2797 if (info->index.sym == 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002798 pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002799 return ERR_PTR(-ENOEXEC);
2800 }
2801
Rusty Russell49668682010-08-05 12:59:10 -06002802 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002803
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002804 /* Check module struct version now, before we try to use module. */
2805 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2806 return ERR_PTR(-ENOEXEC);
2807
2808 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002809}
2810
Rusty Russell2f3238a2012-10-22 18:09:41 +10302811static int check_modinfo(struct module *mod, struct load_info *info, int flags)
Rusty Russell40dd2562010-08-05 12:59:03 -06002812{
Rusty Russell49668682010-08-05 12:59:10 -06002813 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002814 int err;
2815
Rusty Russell2f3238a2012-10-22 18:09:41 +10302816 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2817 modmagic = NULL;
2818
Rusty Russell40dd2562010-08-05 12:59:03 -06002819 /* This is allowed: modprobe --force will invalidate it. */
2820 if (!modmagic) {
2821 err = try_to_force_load(mod, "bad vermagic");
2822 if (err)
2823 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002824 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002825 pr_err("%s: version magic '%s' should be '%s'\n",
Rusty Russell40dd2562010-08-05 12:59:03 -06002826 mod->name, modmagic, vermagic);
2827 return -ENOEXEC;
2828 }
2829
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002830 if (!get_modinfo(info, "intree"))
Rusty Russell373d4d02013-01-21 17:17:39 +10302831 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002832
Rusty Russell49668682010-08-05 12:59:10 -06002833 if (get_modinfo(info, "staging")) {
Rusty Russell373d4d02013-01-21 17:17:39 +10302834 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002835 pr_warn("%s: module is from the staging directory, the quality "
2836 "is unknown, you have been warned.\n", mod->name);
Rusty Russell40dd2562010-08-05 12:59:03 -06002837 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002838
2839 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002840 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002841
Rusty Russell40dd2562010-08-05 12:59:03 -06002842 return 0;
2843}
2844
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302845static int find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002846{
Rusty Russell49668682010-08-05 12:59:10 -06002847 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002848 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002849 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002850 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002851 mod->crcs = section_addr(info, "__kcrctab");
2852 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002853 sizeof(*mod->gpl_syms),
2854 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002855 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2856 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002857 "__ksymtab_gpl_future",
2858 sizeof(*mod->gpl_future_syms),
2859 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002860 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002861
2862#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002863 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002864 sizeof(*mod->unused_syms),
2865 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002866 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2867 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002868 sizeof(*mod->unused_gpl_syms),
2869 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002870 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002871#endif
2872#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002873 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002874 sizeof(*mod->ctors), &mod->num_ctors);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302875 if (!mod->ctors)
2876 mod->ctors = section_objs(info, ".init_array",
2877 sizeof(*mod->ctors), &mod->num_ctors);
2878 else if (find_sec(info, ".init_array")) {
2879 /*
2880 * This shouldn't happen with same compiler and binutils
2881 * building all parts of the module.
2882 */
Ionut Alexa6da0b562014-11-10 09:31:29 +10302883 pr_warn("%s: has both .ctors and .init_array.\n",
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302884 mod->name);
2885 return -EINVAL;
2886 }
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002887#endif
2888
2889#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002890 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2891 sizeof(*mod->tracepoints_ptrs),
2892 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002893#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002894#ifdef HAVE_JUMP_LABEL
2895 mod->jump_entries = section_objs(info, "__jump_table",
2896 sizeof(*mod->jump_entries),
2897 &mod->num_jump_entries);
2898#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002899#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002900 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002901 sizeof(*mod->trace_events),
2902 &mod->num_trace_events);
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002903 mod->trace_enums = section_objs(info, "_ftrace_enum_map",
2904 sizeof(*mod->trace_enums),
2905 &mod->num_trace_enums);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002906#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002907#ifdef CONFIG_TRACING
2908 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2909 sizeof(*mod->trace_bprintk_fmt_start),
2910 &mod->num_trace_bprintk_fmt);
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002911#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002912#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2913 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002914 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002915 sizeof(*mod->ftrace_callsites),
2916 &mod->num_ftrace_callsites);
2917#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002918
Rusty Russell811d66a2010-08-05 12:59:12 -06002919 mod->extable = section_objs(info, "__ex_table",
2920 sizeof(*mod->extable), &mod->num_exentries);
2921
Rusty Russell49668682010-08-05 12:59:10 -06002922 if (section_addr(info, "__obsparm"))
Andrew Mortonbddb12b2013-11-12 15:11:28 -08002923 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002924
2925 info->debug = section_objs(info, "__verbose",
2926 sizeof(*info->debug), &info->num_debug);
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10302927
2928 return 0;
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002929}
2930
Rusty Russell49668682010-08-05 12:59:10 -06002931static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002932{
2933 int i;
2934 void *ptr;
2935
2936 /* Do the allocs. */
2937 ptr = module_alloc_update_bounds(mod->core_size);
2938 /*
2939 * The pointer to this block is stored in the module structure
2940 * which is inside the block. Just mark it as not being a
2941 * leak.
2942 */
2943 kmemleak_not_leak(ptr);
2944 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002945 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002946
2947 memset(ptr, 0, mod->core_size);
2948 mod->module_core = ptr;
2949
Rusty Russell82fab442012-12-11 09:38:33 +10302950 if (mod->init_size) {
2951 ptr = module_alloc_update_bounds(mod->init_size);
2952 /*
2953 * The pointer to this block is stored in the module structure
2954 * which is inside the block. This block doesn't need to be
2955 * scanned as it contains data and code that will be freed
2956 * after the module is initialized.
2957 */
2958 kmemleak_ignore(ptr);
2959 if (!ptr) {
Rusty Russellbe1f2212015-01-20 09:07:05 +10302960 module_memfree(mod->module_core);
Rusty Russell82fab442012-12-11 09:38:33 +10302961 return -ENOMEM;
2962 }
2963 memset(ptr, 0, mod->init_size);
2964 mod->module_init = ptr;
2965 } else
2966 mod->module_init = NULL;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002967
2968 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002969 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002970 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002971 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002972 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002973
Rusty Russell49668682010-08-05 12:59:10 -06002974 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002975 continue;
2976
Rusty Russell49668682010-08-05 12:59:10 -06002977 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002978 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002979 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002980 else
Rusty Russell49668682010-08-05 12:59:10 -06002981 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002982
Rusty Russell49668682010-08-05 12:59:10 -06002983 if (shdr->sh_type != SHT_NOBITS)
2984 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002985 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002986 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002987 pr_debug("\t0x%lx %s\n",
2988 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002989 }
Rusty Russelld9131882010-08-05 12:59:08 -06002990
2991 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002992}
2993
Rusty Russell49668682010-08-05 12:59:10 -06002994static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002995{
2996 /*
2997 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2998 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2999 * using GPL-only symbols it needs.
3000 */
3001 if (strcmp(mod->name, "ndiswrapper") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303002 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003003
3004 /* driverloader was caught wrongly pretending to be under GPL */
3005 if (strcmp(mod->name, "driverloader") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303006 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3007 LOCKDEP_NOW_UNRELIABLE);
Rusty Russell22e268e2010-08-05 12:59:05 -06003008
Matthew Garrettc99af372012-06-22 13:49:31 -04003009 /* lve claims to be GPL but upstream won't provide source */
3010 if (strcmp(mod->name, "lve") == 0)
Rusty Russell373d4d02013-01-21 17:17:39 +10303011 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3012 LOCKDEP_NOW_UNRELIABLE);
Matthew Garrettc99af372012-06-22 13:49:31 -04003013
Rusty Russell22e268e2010-08-05 12:59:05 -06003014#ifdef CONFIG_MODVERSIONS
3015 if ((mod->num_syms && !mod->crcs)
3016 || (mod->num_gpl_syms && !mod->gpl_crcs)
3017 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3018#ifdef CONFIG_UNUSED_SYMBOLS
3019 || (mod->num_unused_syms && !mod->unused_crcs)
3020 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3021#endif
3022 ) {
3023 return try_to_force_load(mod,
3024 "no versions for exported symbols");
3025 }
3026#endif
3027 return 0;
3028}
3029
3030static void flush_module_icache(const struct module *mod)
3031{
3032 mm_segment_t old_fs;
3033
3034 /* flush the icache in correct context */
3035 old_fs = get_fs();
3036 set_fs(KERNEL_DS);
3037
3038 /*
3039 * Flush the instruction cache, since we've played with text.
3040 * Do it before processing of module parameters, so the module
3041 * can provide parameter accessor functions of its own.
3042 */
3043 if (mod->module_init)
3044 flush_icache_range((unsigned long)mod->module_init,
3045 (unsigned long)mod->module_init
3046 + mod->init_size);
3047 flush_icache_range((unsigned long)mod->module_core,
3048 (unsigned long)mod->module_core + mod->core_size);
3049
3050 set_fs(old_fs);
3051}
3052
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003053int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3054 Elf_Shdr *sechdrs,
3055 char *secstrings,
3056 struct module *mod)
3057{
3058 return 0;
3059}
3060
Rusty Russell2f3238a2012-10-22 18:09:41 +10303061static struct module *layout_and_allocate(struct load_info *info, int flags)
Rusty Russelld9131882010-08-05 12:59:08 -06003062{
3063 /* Module within temporary copy. */
3064 struct module *mod;
3065 int err;
3066
Rusty Russell2f3238a2012-10-22 18:09:41 +10303067 mod = setup_load_info(info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003068 if (IS_ERR(mod))
3069 return mod;
3070
Rusty Russell2f3238a2012-10-22 18:09:41 +10303071 err = check_modinfo(mod, info, flags);
Rusty Russelld9131882010-08-05 12:59:08 -06003072 if (err)
3073 return ERR_PTR(err);
3074
3075 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06003076 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3077 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003078 if (err < 0)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303079 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003080
Rusty Russell8d8022e2013-07-03 10:06:28 +09303081 /* We will do a special allocation for per-cpu sections later. */
3082 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06003083
3084 /* Determine total sizes, and put offsets in sh_entsize. For now
3085 this is done generically; there doesn't appear to be any
3086 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06003087 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06003088 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003089
3090 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06003091 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003092 if (err)
Rusty Russell8d8022e2013-07-03 10:06:28 +09303093 return ERR_PTR(err);
Rusty Russelld9131882010-08-05 12:59:08 -06003094
3095 /* Module has been copied to its final place now: return it. */
3096 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06003097 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06003098 return mod;
Rusty Russelld9131882010-08-05 12:59:08 -06003099}
3100
3101/* mod is no longer valid after this! */
3102static void module_deallocate(struct module *mod, struct load_info *info)
3103{
Rusty Russelld9131882010-08-05 12:59:08 -06003104 percpu_modfree(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303105 module_arch_freeing_init(mod);
Rusty Russellbe1f2212015-01-20 09:07:05 +10303106 module_memfree(mod->module_init);
3107 module_memfree(mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06003108}
3109
Jonas Bonn74e08fc2011-06-30 21:22:11 +02003110int __weak module_finalize(const Elf_Ehdr *hdr,
3111 const Elf_Shdr *sechdrs,
3112 struct module *me)
3113{
3114 return 0;
3115}
3116
Rusty Russell811d66a2010-08-05 12:59:12 -06003117static int post_relocation(struct module *mod, const struct load_info *info)
3118{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003119 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003120 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3121
3122 /* Copy relocated percpu area over. */
3123 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3124 info->sechdrs[info->index.pcpu].sh_size);
3125
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003126 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06003127 add_kallsyms(mod, info);
3128
3129 /* Arch-specific module finalizing. */
3130 return module_finalize(info->hdr, info->sechdrs, mod);
3131}
3132
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303133/* Is this module of this name done loading? No locks held. */
3134static bool finished_loading(const char *name)
3135{
3136 struct module *mod;
3137 bool ret;
3138
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303139 /*
3140 * The module_mutex should not be a heavily contended lock;
3141 * if we get the occasional sleep here, we'll go an extra iteration
3142 * in the wait_event_interruptible(), which is harmless.
3143 */
3144 sched_annotate_sleep();
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303145 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303146 mod = find_module_all(name, strlen(name), true);
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303147 ret = !mod || mod->state == MODULE_STATE_LIVE
3148 || mod->state == MODULE_STATE_GOING;
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303149 mutex_unlock(&module_mutex);
3150
3151 return ret;
3152}
3153
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003154/* Call module constructors. */
3155static void do_mod_ctors(struct module *mod)
3156{
3157#ifdef CONFIG_CONSTRUCTORS
3158 unsigned long i;
3159
3160 for (i = 0; i < mod->num_ctors; i++)
3161 mod->ctors[i]();
3162#endif
3163}
3164
Rusty Russellc7496372015-01-20 09:07:05 +10303165/* For freeing module_init on success, in case kallsyms traversing */
3166struct mod_initfree {
3167 struct rcu_head rcu;
3168 void *module_init;
3169};
3170
3171static void do_free_init(struct rcu_head *head)
3172{
3173 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3174 module_memfree(m->module_init);
3175 kfree(m);
3176}
3177
Jan Kiszkabe02a182015-02-17 13:46:50 -08003178/*
3179 * This is where the real work happens.
3180 *
3181 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3182 * helper command 'lx-symbols'.
3183 */
3184static noinline int do_init_module(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 int ret = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303187 struct mod_initfree *freeinit;
3188
3189 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3190 if (!freeinit) {
3191 ret = -ENOMEM;
3192 goto fail;
3193 }
3194 freeinit->module_init = mod->module_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Tejun Heo774a1222013-01-15 18:52:51 -08003196 /*
3197 * We want to find out whether @mod uses async during init. Clear
3198 * PF_USED_ASYNC. async_schedule*() will set it.
3199 */
3200 current->flags &= ~PF_USED_ASYNC;
3201
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003202 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /* Start the module */
3204 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003205 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (ret < 0) {
Rusty Russellc7496372015-01-20 09:07:05 +10303207 goto fail_free_freeinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003209 if (ret > 0) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003210 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3211 "follow 0/-E convention\n"
3212 "%s: loading module anyway...\n",
3213 __func__, mod->name, ret, __func__);
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003214 dump_stack();
3215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Rusty Russell6f139092012-09-28 14:31:03 +09303217 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003219 blocking_notifier_call_chain(&module_notify_list,
3220 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003221
Tejun Heo774a1222013-01-15 18:52:51 -08003222 /*
3223 * We need to finish all async code before the module init sequence
3224 * is done. This has potential to deadlock. For example, a newly
3225 * detected block device can trigger request_module() of the
3226 * default iosched from async probing task. Once userland helper
3227 * reaches here, async_synchronize_full() will wait on the async
3228 * task waiting on request_module() and deadlock.
3229 *
3230 * This deadlock is avoided by perfomring async_synchronize_full()
3231 * iff module init queued any async jobs. This isn't a full
3232 * solution as it will deadlock the same if module loading from
3233 * async jobs nests more than once; however, due to the various
3234 * constraints, this hack seems to be the best option for now.
3235 * Please refer to the following thread for details.
3236 *
3237 * http://thread.gmane.org/gmane.linux.kernel/1420814
3238 */
3239 if (current->flags & PF_USED_ASYNC)
3240 async_synchronize_full();
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003241
Rusty Russell6c5db222008-03-10 11:43:52 -07003242 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 /* Drop initial reference. */
3244 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003245 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003246#ifdef CONFIG_KALLSYMS
3247 mod->num_symtab = mod->core_num_syms;
3248 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003249 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003250#endif
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303251 mod_tree_remove_init(mod);
Jan Glauber01526ed2011-05-19 16:55:26 -06003252 unset_module_init_ro_nx(mod);
Rusty Russelld453cde2015-01-20 09:07:04 +10303253 module_arch_freeing_init(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 mod->module_init = NULL;
3255 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003256 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 mod->init_text_size = 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303258 /*
3259 * We want to free module_init, but be aware that kallsyms may be
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303260 * walking this with preempt disabled. In all the failure paths, we
3261 * call synchronize_sched(), but we don't want to slow down the success
3262 * path, so use actual RCU here.
Rusty Russellc7496372015-01-20 09:07:05 +10303263 */
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303264 call_rcu_sched(&freeinit->rcu, do_free_init);
Ashutosh Naik6389a382006-03-23 03:00:46 -08003265 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303266 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 return 0;
Rusty Russellc7496372015-01-20 09:07:05 +10303269
3270fail_free_freeinit:
3271 kfree(freeinit);
3272fail:
3273 /* Try to protect us from buggy refcounters. */
3274 mod->state = MODULE_STATE_GOING;
3275 synchronize_sched();
3276 module_put(mod);
3277 blocking_notifier_call_chain(&module_notify_list,
3278 MODULE_STATE_GOING, mod);
3279 free_module(mod);
3280 wake_up_all(&module_wq);
3281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Kees Cook34e11692012-10-16 07:31:07 +10303284static int may_init_module(void)
3285{
3286 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3287 return -EPERM;
3288
3289 return 0;
3290}
3291
Rusty Russella3535c72013-01-21 17:18:59 +10303292/*
3293 * We try to place it in the list now to make sure it's unique before
3294 * we dedicate too many resources. In particular, temporary percpu
3295 * memory exhaustion.
3296 */
3297static int add_unformed_module(struct module *mod)
3298{
3299 int err;
3300 struct module *old;
3301
3302 mod->state = MODULE_STATE_UNFORMED;
3303
3304again:
3305 mutex_lock(&module_mutex);
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303306 old = find_module_all(mod->name, strlen(mod->name), true);
3307 if (old != NULL) {
Rusty Russella3535c72013-01-21 17:18:59 +10303308 if (old->state == MODULE_STATE_COMING
3309 || old->state == MODULE_STATE_UNFORMED) {
3310 /* Wait in case it fails to load. */
3311 mutex_unlock(&module_mutex);
Peter Zijlstra9cc019b2015-02-11 15:01:13 +10303312 err = wait_event_interruptible(module_wq,
3313 finished_loading(mod->name));
Rusty Russella3535c72013-01-21 17:18:59 +10303314 if (err)
3315 goto out_unlocked;
3316 goto again;
3317 }
3318 err = -EEXIST;
3319 goto out;
3320 }
3321 list_add_rcu(&mod->list, &modules);
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303322 mod_tree_insert(mod);
Rusty Russella3535c72013-01-21 17:18:59 +10303323 err = 0;
3324
3325out:
3326 mutex_unlock(&module_mutex);
3327out_unlocked:
3328 return err;
3329}
3330
3331static int complete_formation(struct module *mod, struct load_info *info)
3332{
3333 int err;
3334
3335 mutex_lock(&module_mutex);
3336
3337 /* Find duplicate symbols (must be called under lock). */
3338 err = verify_export_symbols(mod);
3339 if (err < 0)
3340 goto out;
3341
3342 /* This relies on module_mutex for list integrity. */
3343 module_bug_finalize(info->hdr, info->sechdrs, mod);
3344
Rusty Russell49822232014-05-14 10:54:19 +09303345 /* Set RO and NX regions for core */
3346 set_section_ro_nx(mod->module_core,
3347 mod->core_text_size,
3348 mod->core_ro_size,
3349 mod->core_size);
3350
3351 /* Set RO and NX regions for init */
3352 set_section_ro_nx(mod->module_init,
3353 mod->init_text_size,
3354 mod->init_ro_size,
3355 mod->init_size);
3356
Rusty Russella3535c72013-01-21 17:18:59 +10303357 /* Mark state as coming so strong_try_module_get() ignores us,
3358 * but kallsyms etc. can see us. */
3359 mod->state = MODULE_STATE_COMING;
Rusty Russell49822232014-05-14 10:54:19 +09303360 mutex_unlock(&module_mutex);
3361
3362 blocking_notifier_call_chain(&module_notify_list,
3363 MODULE_STATE_COMING, mod);
3364 return 0;
Rusty Russella3535c72013-01-21 17:18:59 +10303365
3366out:
3367 mutex_unlock(&module_mutex);
3368 return err;
3369}
3370
Rusty Russell54041d82013-07-02 15:35:12 +09303371static int unknown_module_param_cb(char *param, char *val, const char *modname)
3372{
Ionut Alexa6da0b562014-11-10 09:31:29 +10303373 /* Check for magic 'dyndbg' arg */
Rusty Russell54041d82013-07-02 15:35:12 +09303374 int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003375 if (ret != 0)
3376 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
Rusty Russell54041d82013-07-02 15:35:12 +09303377 return 0;
3378}
3379
Kees Cook34e11692012-10-16 07:31:07 +10303380/* Allocate and load the module: note that size of section 0 is always
3381 zero, and we rely on this for optional sections. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303382static int load_module(struct load_info *info, const char __user *uargs,
3383 int flags)
Kees Cook34e11692012-10-16 07:31:07 +10303384{
Rusty Russella3535c72013-01-21 17:18:59 +10303385 struct module *mod;
Kees Cook34e11692012-10-16 07:31:07 +10303386 long err;
Rusty Russell51e158c2014-04-28 11:34:33 +09303387 char *after_dashes;
Kees Cook34e11692012-10-16 07:31:07 +10303388
3389 err = module_sig_check(info);
3390 if (err)
3391 goto free_copy;
3392
3393 err = elf_header_check(info);
3394 if (err)
3395 goto free_copy;
3396
3397 /* Figure out module layout, and allocate all the memory. */
Rusty Russell2f3238a2012-10-22 18:09:41 +10303398 mod = layout_and_allocate(info, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303399 if (IS_ERR(mod)) {
3400 err = PTR_ERR(mod);
3401 goto free_copy;
3402 }
3403
Rusty Russella3535c72013-01-21 17:18:59 +10303404 /* Reserve our place in the list. */
3405 err = add_unformed_module(mod);
3406 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303407 goto free_module;
Rusty Russell1fb93412013-01-12 13:27:34 +10303408
Kees Cook34e11692012-10-16 07:31:07 +10303409#ifdef CONFIG_MODULE_SIG
3410 mod->sig_ok = info->sig_ok;
Rusty Russell64748a22013-01-21 17:03:02 +10303411 if (!mod->sig_ok) {
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003412 pr_notice_once("%s: module verification failed: signature "
Marcel Holtmannab92ebb2015-02-06 15:09:57 +10303413 "and/or required key missing - tainting "
Andrew Mortonbddb12b2013-11-12 15:11:28 -08003414 "kernel\n", mod->name);
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +10303415 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
Rusty Russell64748a22013-01-21 17:03:02 +10303416 }
Kees Cook34e11692012-10-16 07:31:07 +10303417#endif
3418
Rusty Russell8d8022e2013-07-03 10:06:28 +09303419 /* To avoid stressing percpu allocator, do this once we're unique. */
Rusty Russell9eb76d72013-07-03 10:06:29 +09303420 err = percpu_modalloc(mod, info);
Rusty Russell8d8022e2013-07-03 10:06:28 +09303421 if (err)
3422 goto unlink_mod;
3423
Kees Cook34e11692012-10-16 07:31:07 +10303424 /* Now module is in final location, initialize linked lists, etc. */
3425 err = module_unload_init(mod);
3426 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303427 goto unlink_mod;
Kees Cook34e11692012-10-16 07:31:07 +10303428
3429 /* Now we've got everything in the final locations, we can
3430 * find optional sections. */
Frantisek Hrbataeb3057d2013-10-14 18:08:46 +10303431 err = find_module_sections(mod, info);
3432 if (err)
3433 goto free_unload;
Kees Cook34e11692012-10-16 07:31:07 +10303434
3435 err = check_module_license_and_versions(mod);
3436 if (err)
3437 goto free_unload;
3438
3439 /* Set up MODINFO_ATTR fields */
3440 setup_modinfo(mod, info);
3441
3442 /* Fix up syms, so that st_value is a pointer to location. */
3443 err = simplify_symbols(mod, info);
3444 if (err < 0)
3445 goto free_modinfo;
3446
3447 err = apply_relocations(mod, info);
3448 if (err < 0)
3449 goto free_modinfo;
3450
3451 err = post_relocation(mod, info);
3452 if (err < 0)
3453 goto free_modinfo;
3454
3455 flush_module_icache(mod);
3456
3457 /* Now copy in args */
3458 mod->args = strndup_user(uargs, ~0UL >> 1);
3459 if (IS_ERR(mod->args)) {
3460 err = PTR_ERR(mod->args);
3461 goto free_arch_cleanup;
3462 }
3463
Kees Cook34e11692012-10-16 07:31:07 +10303464 dynamic_debug_setup(info->debug, info->num_debug);
3465
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04003466 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3467 ftrace_module_init(mod);
3468
Rusty Russella3535c72013-01-21 17:18:59 +10303469 /* Finally it's fully formed, ready to start executing. */
3470 err = complete_formation(mod, info);
3471 if (err)
Rusty Russell1fb93412013-01-12 13:27:34 +10303472 goto ddebug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303473
Kees Cook34e11692012-10-16 07:31:07 +10303474 /* Module is ready to execute: parsing args may do that. */
Rusty Russell51e158c2014-04-28 11:34:33 +09303475 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3476 -32768, 32767, unknown_module_param_cb);
3477 if (IS_ERR(after_dashes)) {
3478 err = PTR_ERR(after_dashes);
Rusty Russell1fb93412013-01-12 13:27:34 +10303479 goto bug_cleanup;
Rusty Russell51e158c2014-04-28 11:34:33 +09303480 } else if (after_dashes) {
3481 pr_warn("%s: parameters '%s' after `--' ignored\n",
3482 mod->name, after_dashes);
3483 }
Kees Cook34e11692012-10-16 07:31:07 +10303484
3485 /* Link in to syfs. */
3486 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3487 if (err < 0)
Rusty Russell1fb93412013-01-12 13:27:34 +10303488 goto bug_cleanup;
Kees Cook34e11692012-10-16 07:31:07 +10303489
3490 /* Get rid of temporary copy. */
3491 free_copy(info);
3492
3493 /* Done! */
3494 trace_module_load(mod);
3495
3496 return do_init_module(mod);
3497
Rusty Russell1fb93412013-01-12 13:27:34 +10303498 bug_cleanup:
3499 /* module_bug_cleanup needs module_mutex protection */
Kees Cook34e11692012-10-16 07:31:07 +10303500 mutex_lock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303501 module_bug_cleanup(mod);
Linus Torvaldsee61abb2013-01-20 20:22:58 -08003502 mutex_unlock(&module_mutex);
Andy Lutomirskiff7e0052014-08-16 04:13:37 +09303503
3504 /* we can't deallocate the module until we clear memory protection */
3505 unset_module_init_ro_nx(mod);
3506 unset_module_core_ro_nx(mod);
3507
Rusty Russella3535c72013-01-21 17:18:59 +10303508 ddebug_cleanup:
Rusty Russell1fb93412013-01-12 13:27:34 +10303509 dynamic_debug_remove(info->debug);
Kees Cook34e11692012-10-16 07:31:07 +10303510 synchronize_sched();
3511 kfree(mod->args);
3512 free_arch_cleanup:
3513 module_arch_cleanup(mod);
3514 free_modinfo:
3515 free_modinfo(mod);
3516 free_unload:
3517 module_unload_free(mod);
Rusty Russell1fb93412013-01-12 13:27:34 +10303518 unlink_mod:
3519 mutex_lock(&module_mutex);
3520 /* Unlink carefully: kallsyms could be walking list. */
3521 list_del_rcu(&mod->list);
3522 wake_up_all(&module_wq);
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303523 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3524 synchronize_sched();
Rusty Russell1fb93412013-01-12 13:27:34 +10303525 mutex_unlock(&module_mutex);
Kees Cook34e11692012-10-16 07:31:07 +10303526 free_module:
Peter Zijlstra35a93932015-02-26 16:23:11 +01003527 /* Free lock-classes; relies on the preceding sync_rcu() */
3528 lockdep_free_key_range(mod->module_core, mod->core_size);
3529
Kees Cook34e11692012-10-16 07:31:07 +10303530 module_deallocate(mod, info);
3531 free_copy:
3532 free_copy(info);
3533 return err;
3534}
3535
3536SYSCALL_DEFINE3(init_module, void __user *, umod,
3537 unsigned long, len, const char __user *, uargs)
3538{
3539 int err;
3540 struct load_info info = { };
3541
3542 err = may_init_module();
3543 if (err)
3544 return err;
3545
3546 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3547 umod, len, uargs);
3548
3549 err = copy_module_from_user(umod, len, &info);
3550 if (err)
3551 return err;
3552
Rusty Russell2f3238a2012-10-22 18:09:41 +10303553 return load_module(&info, uargs, 0);
Kees Cook34e11692012-10-16 07:31:07 +10303554}
3555
Rusty Russell2f3238a2012-10-22 18:09:41 +10303556SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Kees Cook34e11692012-10-16 07:31:07 +10303557{
3558 int err;
3559 struct load_info info = { };
3560
3561 err = may_init_module();
3562 if (err)
3563 return err;
3564
Rusty Russell2f3238a2012-10-22 18:09:41 +10303565 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3566
3567 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3568 |MODULE_INIT_IGNORE_VERMAGIC))
3569 return -EINVAL;
Kees Cook34e11692012-10-16 07:31:07 +10303570
3571 err = copy_module_from_fd(fd, &info);
3572 if (err)
3573 return err;
3574
Rusty Russell2f3238a2012-10-22 18:09:41 +10303575 return load_module(&info, uargs, flags);
Kees Cook34e11692012-10-16 07:31:07 +10303576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578static inline int within(unsigned long addr, void *start, unsigned long size)
3579{
3580 return ((void *)addr >= start && (void *)addr < start + size);
3581}
3582
3583#ifdef CONFIG_KALLSYMS
3584/*
3585 * This ignores the intensely annoying "mapping symbols" found
3586 * in ARM ELF files: $a, $t and $d.
3587 */
3588static inline int is_arm_mapping_symbol(const char *str)
3589{
Russell King2e3a10a2014-07-27 07:29:01 +09303590 if (str[0] == '.' && str[1] == 'L')
3591 return true;
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01003592 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 && (str[2] == '\0' || str[2] == '.');
3594}
3595
3596static const char *get_ksymbol(struct module *mod,
3597 unsigned long addr,
3598 unsigned long *size,
3599 unsigned long *offset)
3600{
3601 unsigned int i, best = 0;
3602 unsigned long nextval;
3603
3604 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003605 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003607 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3609
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003610 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003611 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 for (i = 1; i < mod->num_symtab; i++) {
3613 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3614 continue;
3615
3616 /* We ignore unnamed symbols: they're uninformative
3617 * and inserted at a whim. */
3618 if (mod->symtab[i].st_value <= addr
3619 && mod->symtab[i].st_value > mod->symtab[best].st_value
3620 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3621 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3622 best = i;
3623 if (mod->symtab[i].st_value > addr
3624 && mod->symtab[i].st_value < nextval
3625 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3626 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3627 nextval = mod->symtab[i].st_value;
3628 }
3629
3630 if (!best)
3631 return NULL;
3632
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003633 if (size)
3634 *size = nextval - mod->symtab[best].st_value;
3635 if (offset)
3636 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return mod->strtab + mod->symtab[best].st_name;
3638}
3639
Rusty Russell6dd06c92008-01-29 17:13:22 -05003640/* For kallsyms to ask for address resolution. NULL means not found. Careful
3641 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003642const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003643 unsigned long *size,
3644 unsigned long *offset,
3645 char **modname,
3646 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003649 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Rusty Russellcb2a5202008-01-14 00:55:03 -08003651 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003652 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303653 if (mod->state == MODULE_STATE_UNFORMED)
3654 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303655 if (within_module(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003656 if (modname)
3657 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003658 ret = get_ksymbol(mod, addr, size, offset);
3659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
3661 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003662 /* Make a copy in here where it's safe */
3663 if (ret) {
3664 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3665 ret = namebuf;
3666 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003667 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003671int lookup_module_symbol_name(unsigned long addr, char *symname)
3672{
3673 struct module *mod;
3674
Rusty Russellcb2a5202008-01-14 00:55:03 -08003675 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003676 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303677 if (mod->state == MODULE_STATE_UNFORMED)
3678 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303679 if (within_module(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003680 const char *sym;
3681
3682 sym = get_ksymbol(mod, addr, NULL, NULL);
3683 if (!sym)
3684 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003685 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003686 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003687 return 0;
3688 }
3689 }
3690out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003691 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003692 return -ERANGE;
3693}
3694
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003695int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3696 unsigned long *offset, char *modname, char *name)
3697{
3698 struct module *mod;
3699
Rusty Russellcb2a5202008-01-14 00:55:03 -08003700 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003701 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303702 if (mod->state == MODULE_STATE_UNFORMED)
3703 continue;
Petr Mladek9b20a352014-07-27 07:24:01 +09303704 if (within_module(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003705 const char *sym;
3706
3707 sym = get_ksymbol(mod, addr, size, offset);
3708 if (!sym)
3709 goto out;
3710 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003711 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003712 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003713 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003714 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003715 return 0;
3716 }
3717 }
3718out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003719 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003720 return -ERANGE;
3721}
3722
Alexey Dobriyanea078902007-05-08 00:28:39 -07003723int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3724 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
3726 struct module *mod;
3727
Rusty Russellcb2a5202008-01-14 00:55:03 -08003728 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003729 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303730 if (mod->state == MODULE_STATE_UNFORMED)
3731 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if (symnum < mod->num_symtab) {
3733 *value = mod->symtab[symnum].st_value;
3734 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003735 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003736 KSYM_NAME_LEN);
3737 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003738 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003739 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
3742 symnum -= mod->num_symtab;
3743 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003744 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003745 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
3748static unsigned long mod_find_symname(struct module *mod, const char *name)
3749{
3750 unsigned int i;
3751
3752 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003753 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3754 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return mod->symtab[i].st_value;
3756 return 0;
3757}
3758
3759/* Look for this name: can be of form module:name. */
3760unsigned long module_kallsyms_lookup_name(const char *name)
3761{
3762 struct module *mod;
3763 char *colon;
3764 unsigned long ret = 0;
3765
3766 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003767 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if ((colon = strchr(name, ':')) != NULL) {
Mathias Krause4f6de4d2013-07-02 15:35:11 +09303769 if ((mod = find_module_all(name, colon - name, false)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 ret = mod_find_symname(mod, colon+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 } else {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303772 list_for_each_entry_rcu(mod, &modules, list) {
3773 if (mod->state == MODULE_STATE_UNFORMED)
3774 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 if ((ret = mod_find_symname(mod, name)) != 0)
3776 break;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003779 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return ret;
3781}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003782
3783int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3784 struct module *, unsigned long),
3785 void *data)
3786{
3787 struct module *mod;
3788 unsigned int i;
3789 int ret;
3790
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303791 module_assert_mutex();
3792
Anders Kaseorg75a66612008-12-05 19:03:58 -05003793 list_for_each_entry(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303794 if (mod->state == MODULE_STATE_UNFORMED)
3795 continue;
Anders Kaseorg75a66612008-12-05 19:03:58 -05003796 for (i = 0; i < mod->num_symtab; i++) {
3797 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3798 mod, mod->symtab[i].st_value);
3799 if (ret != 0)
3800 return ret;
3801 }
3802 }
3803 return 0;
3804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805#endif /* CONFIG_KALLSYMS */
3806
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003807static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003808{
3809 int bx = 0;
3810
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303811 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003812 if (mod->taints ||
3813 mod->state == MODULE_STATE_GOING ||
3814 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003815 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303816 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003817 /* Show a - for module-is-being-unloaded */
3818 if (mod->state == MODULE_STATE_GOING)
3819 buf[bx++] = '-';
3820 /* Show a + for module-is-being-loaded */
3821 if (mod->state == MODULE_STATE_COMING)
3822 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003823 buf[bx++] = ')';
3824 }
3825 buf[bx] = '\0';
3826
3827 return buf;
3828}
3829
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003830#ifdef CONFIG_PROC_FS
3831/* Called by the /proc file system to return a list of modules. */
3832static void *m_start(struct seq_file *m, loff_t *pos)
3833{
3834 mutex_lock(&module_mutex);
3835 return seq_list_start(&modules, *pos);
3836}
3837
3838static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3839{
3840 return seq_list_next(p, &modules, pos);
3841}
3842
3843static void m_stop(struct seq_file *m, void *p)
3844{
3845 mutex_unlock(&module_mutex);
3846}
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848static int m_show(struct seq_file *m, void *p)
3849{
3850 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003851 char buf[8];
3852
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303853 /* We always ignore unformed modules. */
3854 if (mod->state == MODULE_STATE_UNFORMED)
3855 return 0;
3856
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003857 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 mod->name, mod->init_size + mod->core_size);
3859 print_unload_info(m, mod);
3860
3861 /* Informative for users. */
3862 seq_printf(m, " %s",
Ionut Alexa6da0b562014-11-10 09:31:29 +10303863 mod->state == MODULE_STATE_GOING ? "Unloading" :
3864 mod->state == MODULE_STATE_COMING ? "Loading" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 "Live");
3866 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003867 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003869 /* Taints info */
3870 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003871 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003872
Ionut Alexa6da0b562014-11-10 09:31:29 +10303873 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return 0;
3875}
3876
3877/* Format: modulename size refcount deps address
3878
3879 Where refcount is a number or -, and deps is a comma-separated list
3880 of depends or -.
3881*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003882static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 .start = m_start,
3884 .next = m_next,
3885 .stop = m_stop,
3886 .show = m_show
3887};
3888
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003889static int modules_open(struct inode *inode, struct file *file)
3890{
3891 return seq_open(file, &modules_op);
3892}
3893
3894static const struct file_operations proc_modules_operations = {
3895 .open = modules_open,
3896 .read = seq_read,
3897 .llseek = seq_lseek,
3898 .release = seq_release,
3899};
3900
3901static int __init proc_modules_init(void)
3902{
3903 proc_create("modules", 0, NULL, &proc_modules_operations);
3904 return 0;
3905}
3906module_init(proc_modules_init);
3907#endif
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909/* Given an address, look for it in the module exception tables. */
3910const struct exception_table_entry *search_module_extables(unsigned long addr)
3911{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 const struct exception_table_entry *e = NULL;
3913 struct module *mod;
3914
Rusty Russell24da1cb2007-07-15 23:41:46 -07003915 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003916 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303917 if (mod->state == MODULE_STATE_UNFORMED)
3918 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (mod->num_exentries == 0)
3920 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003921
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 e = search_extable(mod->extable,
3923 mod->extable + mod->num_exentries - 1,
3924 addr);
3925 if (e)
3926 break;
3927 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003928 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003931 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return e;
3933}
3934
Ingo Molnar4d435f92006-07-03 00:24:24 -07003935/*
Rusty Russelle6104992009-03-31 13:05:31 -06003936 * is_module_address - is this address inside a module?
3937 * @addr: the address to check.
3938 *
3939 * See is_module_text_address() if you simply want to see if the address
3940 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003941 */
Rusty Russelle6104992009-03-31 13:05:31 -06003942bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003943{
Rusty Russelle6104992009-03-31 13:05:31 -06003944 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003945
Rusty Russell24da1cb2007-07-15 23:41:46 -07003946 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003947 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003948 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003949
Rusty Russelle6104992009-03-31 13:05:31 -06003950 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003951}
3952
Rusty Russelle6104992009-03-31 13:05:31 -06003953/*
3954 * __module_address - get the module which contains an address.
3955 * @addr: the address.
3956 *
3957 * Must be called with preempt disabled or module mutex held so that
3958 * module doesn't get freed during this.
3959 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003960struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 struct module *mod;
3963
Rusty Russell3a642e92008-07-22 19:24:28 -05003964 if (addr < module_addr_min || addr > module_addr_max)
3965 return NULL;
3966
Peter Zijlstra0be964b2015-05-27 11:09:35 +09303967 module_assert_mutex_or_preempt();
3968
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303969 mod = mod_tree_find(addr);
3970 if (mod) {
3971 BUG_ON(!within_module(addr, mod));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303972 if (mod->state == MODULE_STATE_UNFORMED)
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303973 mod = NULL;
Rusty Russell0d21b0e2013-01-12 11:38:44 +10303974 }
Peter Zijlstra93c2e102015-05-27 11:09:37 +09303975 return mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
Tim Abbottc6b37802008-12-05 19:03:59 -05003977EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Rusty Russelle6104992009-03-31 13:05:31 -06003979/*
3980 * is_module_text_address - is this address inside module code?
3981 * @addr: the address to check.
3982 *
3983 * See is_module_address() if you simply want to see if the address is
3984 * anywhere in a module. See kernel_text_address() for testing if an
3985 * address corresponds to kernel or module code.
3986 */
3987bool is_module_text_address(unsigned long addr)
3988{
3989 bool ret;
3990
3991 preempt_disable();
3992 ret = __module_text_address(addr) != NULL;
3993 preempt_enable();
3994
3995 return ret;
3996}
3997
3998/*
3999 * __module_text_address - get the module whose code contains an address.
4000 * @addr: the address.
4001 *
4002 * Must be called with preempt disabled or module mutex held so that
4003 * module doesn't get freed during this.
4004 */
4005struct module *__module_text_address(unsigned long addr)
4006{
4007 struct module *mod = __module_address(addr);
4008 if (mod) {
4009 /* Make sure it's within the text section. */
4010 if (!within(addr, mod->module_init, mod->init_text_size)
4011 && !within(addr, mod->module_core, mod->core_text_size))
4012 mod = NULL;
4013 }
4014 return mod;
4015}
Tim Abbottc6b37802008-12-05 19:03:59 -05004016EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06004017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018/* Don't grab lock, we're oopsing. */
4019void print_modules(void)
4020{
4021 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07004022 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Linus Torvaldsb2311252009-06-16 11:07:14 -07004024 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02004025 /* Most callers should already have preempt disabled, but make sure */
4026 preempt_disable();
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304027 list_for_each_entry_rcu(mod, &modules, list) {
4028 if (mod->state == MODULE_STATE_UNFORMED)
4029 continue;
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304030 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
Rusty Russell0d21b0e2013-01-12 11:38:44 +10304031 }
Andi Kleend72b3752008-08-30 10:09:00 +02004032 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01004033 if (last_unloaded_module[0])
Jiri Slaby27bba4d2014-02-03 11:13:13 +10304034 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4035 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06004039/* Generate the signature for all relevant module structures here.
4040 * If these change, we don't want to try to parse the module. */
4041void module_layout(struct module *mod,
4042 struct modversion_info *ver,
4043 struct kernel_param *kp,
4044 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05004045 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06004046{
4047}
4048EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049#endif