blob: 808bd62e17238cee6aceaa7e659f52a0127679f9 [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>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040058#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010059#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020060#include <linux/bsearch.h>
David Howells1d0059f2012-09-26 10:09:50 +010061#include <linux/fips.h>
Rusty Russell106a4ee2012-09-26 10:09:40 +010062#include "module-internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Li Zefan7ead8b82009-08-17 16:56:28 +080064#define CREATE_TRACE_POINTS
65#include <trace/events/module.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#ifndef ARCH_SHF_SMALL
68#define ARCH_SHF_SMALL 0
69#endif
70
matthieu castet84e1c6b2010-11-16 22:35:16 +010071/*
72 * Modules' sections will be aligned on page boundaries
73 * to ensure complete separation of code and data, but
74 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
75 */
76#ifdef CONFIG_DEBUG_SET_MODULE_RONX
77# define debug_align(X) ALIGN(X, PAGE_SIZE)
78#else
79# define debug_align(X) (X)
80#endif
81
82/*
83 * Given BASE and SIZE this macro calculates the number of pages the
84 * memory regions occupies
85 */
86#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
87 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
88 PFN_DOWN((unsigned long)BASE) + 1) \
89 : (0UL))
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* If this is set, the section belongs in the init part of the module */
92#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
93
Rusty Russell75676502010-06-05 11:17:36 -060094/*
95 * Mutex protects:
96 * 1) List of modules (also safely readable with preempt_disable),
97 * 2) module_use links,
98 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +020099 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500100DEFINE_MUTEX(module_mutex);
101EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500103#ifdef CONFIG_KGDB_KDB
104struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
105#endif /* CONFIG_KGDB_KDB */
106
Rusty Russell106a4ee2012-09-26 10:09:40 +0100107#ifdef CONFIG_MODULE_SIG
108#ifdef CONFIG_MODULE_SIG_FORCE
109static bool sig_enforce = true;
110#else
111static bool sig_enforce = false;
112
113static int param_set_bool_enable_only(const char *val,
114 const struct kernel_param *kp)
115{
116 int err;
117 bool test;
118 struct kernel_param dummy_kp = *kp;
119
120 dummy_kp.arg = &test;
121
122 err = param_set_bool(val, &dummy_kp);
123 if (err)
124 return err;
125
126 /* Don't let them unset it once it's set! */
127 if (!test && sig_enforce)
128 return -EROFS;
129
130 if (test)
131 sig_enforce = true;
132 return 0;
133}
134
135static const struct kernel_param_ops param_ops_bool_enable_only = {
136 .set = param_set_bool_enable_only,
137 .get = param_get_bool,
138};
139#define param_check_bool_enable_only param_check_bool
140
141module_param(sig_enforce, bool_enable_only, 0644);
142#endif /* !CONFIG_MODULE_SIG_FORCE */
143#endif /* CONFIG_MODULE_SIG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Stephen Rothwell19e45292009-04-14 17:27:18 +1000145/* Block module loading/unloading? */
146int modules_disabled = 0;
Dave Young02608be2012-02-01 10:33:14 +0800147core_param(nomodule, modules_disabled, bint, 0);
Stephen Rothwell19e45292009-04-14 17:27:18 +1000148
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500149/* Waiting for a module to finish initializing? */
150static DECLARE_WAIT_QUEUE_HEAD(module_wq);
151
Alan Sterne041c682006-03-27 01:16:30 -0800152static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Rusty Russell75676502010-06-05 11:17:36 -0600154/* Bounds of module allocation, for speeding __module_address.
155 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500156static unsigned long module_addr_min = -1UL, module_addr_max = 0;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158int register_module_notifier(struct notifier_block * nb)
159{
Alan Sterne041c682006-03-27 01:16:30 -0800160 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162EXPORT_SYMBOL(register_module_notifier);
163
164int unregister_module_notifier(struct notifier_block * nb)
165{
Alan Sterne041c682006-03-27 01:16:30 -0800166 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168EXPORT_SYMBOL(unregister_module_notifier);
169
Rusty Russelleded41c2010-08-05 12:59:07 -0600170struct load_info {
171 Elf_Ehdr *hdr;
172 unsigned long len;
173 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600174 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600175 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600176 struct _ddebug *debug;
177 unsigned int num_debug;
Rusty Russell106a4ee2012-09-26 10:09:40 +0100178 bool sig_ok;
Rusty Russelleded41c2010-08-05 12:59:07 -0600179 struct {
180 unsigned int sym, str, mod, vers, info, pcpu;
181 } index;
182};
183
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800184/* We require a truly strong try_module_get(): 0 means failure due to
185 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static inline int strong_try_module_get(struct module *mod)
187{
188 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500189 return -EBUSY;
190 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500192 else
193 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700196static inline void add_taint_module(struct module *mod, unsigned flag)
197{
198 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700199 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700200}
201
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200202/*
203 * A thread that wants to hold a reference to a module only while it
204 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206void __module_put_and_exit(struct module *mod, long code)
207{
208 module_put(mod);
209 do_exit(code);
210}
211EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600214static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned int i;
217
Rusty Russell49668682010-08-05 12:59:10 -0600218 for (i = 1; i < info->hdr->e_shnum; i++) {
219 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600221 if ((shdr->sh_flags & SHF_ALLOC)
222 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226}
227
Rusty Russell5e458cc2008-10-22 10:00:13 -0500228/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600229static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500230{
231 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600232 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500233}
234
235/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600236static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500237 const char *name,
238 size_t object_size,
239 unsigned int *num)
240{
Rusty Russell49668682010-08-05 12:59:10 -0600241 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500242
243 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600244 *num = info->sechdrs[sec].sh_size / object_size;
245 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* Provided by the linker */
249extern const struct kernel_symbol __start___ksymtab[];
250extern const struct kernel_symbol __stop___ksymtab[];
251extern const struct kernel_symbol __start___ksymtab_gpl[];
252extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800253extern const struct kernel_symbol __start___ksymtab_gpl_future[];
254extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255extern const unsigned long __start___kcrctab[];
256extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800257extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500258#ifdef CONFIG_UNUSED_SYMBOLS
259extern const struct kernel_symbol __start___ksymtab_unused[];
260extern const struct kernel_symbol __stop___ksymtab_unused[];
261extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
262extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700263extern const unsigned long __start___kcrctab_unused[];
264extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267#ifndef CONFIG_MODVERSIONS
268#define symversion(base, idx) NULL
269#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800270#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#endif
272
Rusty Russelldafd0942008-07-22 19:24:25 -0500273static bool each_symbol_in_section(const struct symsearch *arr,
274 unsigned int arrsize,
275 struct module *owner,
276 bool (*fn)(const struct symsearch *syms,
277 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200278 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500279 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100280{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200281 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500282
283 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200284 if (fn(&arr[j], owner, data))
285 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500286 }
287
288 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100289}
290
Rusty Russelldafd0942008-07-22 19:24:25 -0500291/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200292bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
293 struct module *owner,
294 void *data),
295 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700296{
Rusty Russelldafd0942008-07-22 19:24:25 -0500297 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600298 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500299 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
300 NOT_GPL_ONLY, false },
301 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
302 __start___kcrctab_gpl,
303 GPL_ONLY, false },
304 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
305 __start___kcrctab_gpl_future,
306 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500307#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500308 { __start___ksymtab_unused, __stop___ksymtab_unused,
309 __start___kcrctab_unused,
310 NOT_GPL_ONLY, true },
311 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
312 __start___kcrctab_unused_gpl,
313 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500314#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500315 };
316
317 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
318 return true;
319
Andi Kleend72b3752008-08-30 10:09:00 +0200320 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500321 struct symsearch arr[] = {
322 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
323 NOT_GPL_ONLY, false },
324 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
325 mod->gpl_crcs,
326 GPL_ONLY, false },
327 { mod->gpl_future_syms,
328 mod->gpl_future_syms + mod->num_gpl_future_syms,
329 mod->gpl_future_crcs,
330 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500331#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500332 { mod->unused_syms,
333 mod->unused_syms + mod->num_unused_syms,
334 mod->unused_crcs,
335 NOT_GPL_ONLY, true },
336 { mod->unused_gpl_syms,
337 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
338 mod->unused_gpl_crcs,
339 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500340#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500341 };
342
343 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
344 return true;
345 }
346 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700347}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200348EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700349
Rusty Russelldafd0942008-07-22 19:24:25 -0500350struct find_symbol_arg {
351 /* Input */
352 const char *name;
353 bool gplok;
354 bool warn;
355
356 /* Output */
357 struct module *owner;
358 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500359 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500360};
361
Rusty Russellde4d8d52011-04-19 21:49:58 +0200362static bool check_symbol(const struct symsearch *syms,
363 struct module *owner,
364 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500365{
Rusty Russelldafd0942008-07-22 19:24:25 -0500366 struct find_symbol_arg *fsa = data;
367
Rusty Russelldafd0942008-07-22 19:24:25 -0500368 if (!fsa->gplok) {
369 if (syms->licence == GPL_ONLY)
370 return false;
371 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
372 printk(KERN_WARNING "Symbol %s is being used "
373 "by a non-GPL module, which will not "
374 "be allowed in the future\n", fsa->name);
Rusty Russelldafd0942008-07-22 19:24:25 -0500375 }
376 }
377
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500378#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500379 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500380 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500381 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500382 printk(KERN_WARNING
383 "This symbol will go away in the future.\n");
384 printk(KERN_WARNING
385 "Please evalute if this is the right api to use and if "
386 "it really is, submit a report the linux kernel "
387 "mailinglist together with submitting your code for "
388 "inclusion.\n");
389 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500390#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500391
392 fsa->owner = owner;
393 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500394 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500395 return true;
396}
397
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200398static int cmp_name(const void *va, const void *vb)
399{
400 const char *a;
401 const struct kernel_symbol *b;
402 a = va; b = vb;
403 return strcmp(a, b->name);
404}
405
Rusty Russellde4d8d52011-04-19 21:49:58 +0200406static bool find_symbol_in_section(const struct symsearch *syms,
407 struct module *owner,
408 void *data)
409{
410 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200411 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200412
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200413 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
414 sizeof(struct kernel_symbol), cmp_name);
415
416 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
417 return true;
418
Rusty Russellde4d8d52011-04-19 21:49:58 +0200419 return false;
420}
421
Tim Abbott414fd312008-12-05 19:03:56 -0500422/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600423 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500424const struct kernel_symbol *find_symbol(const char *name,
425 struct module **owner,
426 const unsigned long **crc,
427 bool gplok,
428 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Rusty Russelldafd0942008-07-22 19:24:25 -0500430 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Rusty Russelldafd0942008-07-22 19:24:25 -0500432 fsa.name = name;
433 fsa.gplok = gplok;
434 fsa.warn = warn;
435
Rusty Russellde4d8d52011-04-19 21:49:58 +0200436 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500437 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500438 *owner = fsa.owner;
439 if (crc)
440 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500441 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500443
Jim Cromie5e124162011-12-06 12:11:31 -0700444 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Tim Abbottc6b37802008-12-05 19:03:59 -0500447EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500450struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct module *mod;
453
454 list_for_each_entry(mod, &modules, list) {
455 if (strcmp(mod->name, name) == 0)
456 return mod;
457 }
458 return NULL;
459}
Tim Abbottc6b37802008-12-05 19:03:59 -0500460EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900463
Tejun Heo259354d2010-03-10 18:56:10 +0900464static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900465{
Tejun Heo259354d2010-03-10 18:56:10 +0900466 return mod->percpu;
467}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900468
Tejun Heo259354d2010-03-10 18:56:10 +0900469static int percpu_modalloc(struct module *mod,
470 unsigned long size, unsigned long align)
471{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900472 if (align > PAGE_SIZE) {
473 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900474 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900475 align = PAGE_SIZE;
476 }
477
Tejun Heo259354d2010-03-10 18:56:10 +0900478 mod->percpu = __alloc_reserved_percpu(size, align);
479 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900480 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600481 "%s: Could not allocate %lu bytes percpu data\n",
482 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900483 return -ENOMEM;
484 }
485 mod->percpu_size = size;
486 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900487}
488
Tejun Heo259354d2010-03-10 18:56:10 +0900489static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900490{
Tejun Heo259354d2010-03-10 18:56:10 +0900491 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900492}
493
Rusty Russell49668682010-08-05 12:59:10 -0600494static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900495{
Rusty Russell49668682010-08-05 12:59:10 -0600496 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900497}
498
Tejun Heo259354d2010-03-10 18:56:10 +0900499static void percpu_modcopy(struct module *mod,
500 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900501{
502 int cpu;
503
504 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900505 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900506}
507
Tejun Heo10fad5e2010-03-10 18:57:54 +0900508/**
509 * is_module_percpu_address - test whether address is from module static percpu
510 * @addr: address to test
511 *
512 * Test whether @addr belongs to module static percpu area.
513 *
514 * RETURNS:
515 * %true if @addr is from module static percpu area
516 */
517bool is_module_percpu_address(unsigned long addr)
518{
519 struct module *mod;
520 unsigned int cpu;
521
522 preempt_disable();
523
524 list_for_each_entry_rcu(mod, &modules, list) {
525 if (!mod->percpu_size)
526 continue;
527 for_each_possible_cpu(cpu) {
528 void *start = per_cpu_ptr(mod->percpu, cpu);
529
530 if ((void *)addr >= start &&
531 (void *)addr < start + mod->percpu_size) {
532 preempt_enable();
533 return true;
534 }
535 }
536 }
537
538 preempt_enable();
539 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900543
Tejun Heo259354d2010-03-10 18:56:10 +0900544static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 return NULL;
547}
Tejun Heo259354d2010-03-10 18:56:10 +0900548static inline int percpu_modalloc(struct module *mod,
549 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Tejun Heo259354d2010-03-10 18:56:10 +0900551 return -ENOMEM;
552}
553static inline void percpu_modfree(struct module *mod)
554{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Rusty Russell49668682010-08-05 12:59:10 -0600556static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 return 0;
559}
Tejun Heo259354d2010-03-10 18:56:10 +0900560static inline void percpu_modcopy(struct module *mod,
561 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 /* pcpusec should be 0, and size of that section should be 0. */
564 BUG_ON(size != 0);
565}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900566bool is_module_percpu_address(unsigned long addr)
567{
568 return false;
569}
Tejun Heo6b588c12009-02-20 16:29:07 +0900570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif /* CONFIG_SMP */
572
Matt Domschc988d2b2005-06-23 22:05:15 -0700573#define MODINFO_ATTR(field) \
574static void setup_modinfo_##field(struct module *mod, const char *s) \
575{ \
576 mod->field = kstrdup(s, GFP_KERNEL); \
577} \
578static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930579 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700580{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930581 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700582} \
583static int modinfo_##field##_exists(struct module *mod) \
584{ \
585 return mod->field != NULL; \
586} \
587static void free_modinfo_##field(struct module *mod) \
588{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700589 kfree(mod->field); \
590 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700591} \
592static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900593 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700594 .show = show_modinfo_##field, \
595 .setup = setup_modinfo_##field, \
596 .test = modinfo_##field##_exists, \
597 .free = free_modinfo_##field, \
598};
599
600MODINFO_ATTR(version);
601MODINFO_ATTR(srcversion);
602
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100603static char last_unloaded_module[MODULE_NAME_LEN+1];
604
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800605#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400606
607EXPORT_TRACEPOINT_SYMBOL(module_get);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600610static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600612 mod->refptr = alloc_percpu(struct module_ref);
613 if (!mod->refptr)
614 return -ENOMEM;
615
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700616 INIT_LIST_HEAD(&mod->source_list);
617 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100620 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Backwards compatibility macros put refcount during init. */
622 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600623
624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/* Does a already use b? */
628static int already_uses(struct module *a, struct module *b)
629{
630 struct module_use *use;
631
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700632 list_for_each_entry(use, &b->source_list, source_list) {
633 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700634 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 1;
636 }
637 }
Jim Cromie5e124162011-12-06 12:11:31 -0700638 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
641
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700642/*
643 * Module a uses b
644 * - we add 'a' as a "source", 'b' as a "target" of module use
645 * - the module_use is added to the list of 'b' sources (so
646 * 'b' can walk the list to see who sourced them), and of 'a'
647 * targets (so 'a' can see what modules it targets).
648 */
649static int add_module_usage(struct module *a, struct module *b)
650{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700651 struct module_use *use;
652
Jim Cromie5e124162011-12-06 12:11:31 -0700653 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700654 use = kmalloc(sizeof(*use), GFP_ATOMIC);
655 if (!use) {
656 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
657 return -ENOMEM;
658 }
659
660 use->source = a;
661 use->target = b;
662 list_add(&use->source_list, &b->source_list);
663 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700664 return 0;
665}
666
Rusty Russell75676502010-06-05 11:17:36 -0600667/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600668int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600670 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100671
Rusty Russell9bea7f22010-06-05 11:17:37 -0600672 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700673 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700674
Rusty Russell9bea7f22010-06-05 11:17:37 -0600675 /* If module isn't available, we fail. */
676 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500677 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700680 err = add_module_usage(a, b);
681 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600687EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689/* Clear the unload stuff of the module. */
690static void module_unload_free(struct module *mod)
691{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700692 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Rusty Russell75676502010-06-05 11:17:36 -0600694 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700695 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
696 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700697 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700698 module_put(i);
699 list_del(&use->source_list);
700 list_del(&use->target_list);
701 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Rusty Russell75676502010-06-05 11:17:36 -0600703 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600704
705 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800709static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 int ret = (flags & O_TRUNC);
712 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800713 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return ret;
715}
716#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800717static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 return 0;
720}
721#endif /* CONFIG_MODULE_FORCE_UNLOAD */
722
723struct stopref
724{
725 struct module *mod;
726 int flags;
727 int *forced;
728};
729
730/* Whole machine is stopped with interrupts off when this runs. */
731static int __try_stop_module(void *_sref)
732{
733 struct stopref *sref = _sref;
734
Rusty Russellda39ba52008-07-22 19:24:25 -0500735 /* If it's not unused, quit unless we're forcing. */
736 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800737 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EWOULDBLOCK;
739 }
740
741 /* Mark it as dying. */
742 sref->mod->state = MODULE_STATE_GOING;
743 return 0;
744}
745
746static int try_stop_module(struct module *mod, int flags, int *forced)
747{
Rusty Russellda39ba52008-07-22 19:24:25 -0500748 if (flags & O_NONBLOCK) {
749 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500751 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500752 } else {
753 /* We don't need to stop the machine for this. */
754 mod->state = MODULE_STATE_GOING;
755 synchronize_sched();
756 return 0;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Eric Dumazetbd77c042012-01-13 09:32:14 +1030760unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030762 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030763 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Eric Dumazet720eba32009-02-03 13:31:36 +1030765 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100766 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
767 /*
768 * ensure the incs are added up after the decs.
769 * module_put ensures incs are visible before decs with smp_wmb.
770 *
771 * This 2-count scheme avoids the situation where the refcount
772 * for CPU0 is read, then CPU0 increments the module refcount,
773 * then CPU1 drops that refcount, then the refcount for CPU1 is
774 * read. We would record a decrement but not its corresponding
775 * increment so we would see a low count (disaster).
776 *
777 * Rare situation? But module_refcount can be preempted, and we
778 * might be tallying up 4096+ CPUs. So it is not impossible.
779 */
780 smp_rmb();
781 for_each_possible_cpu(cpu)
782 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
783 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785EXPORT_SYMBOL(module_refcount);
786
787/* This exists whether we can unload or not */
788static void free_module(struct module *mod);
789
790static void wait_for_zero_refcount(struct module *mod)
791{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500792 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800793 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700795 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 set_current_state(TASK_UNINTERRUPTIBLE);
797 if (module_refcount(mod) == 0)
798 break;
799 schedule();
800 }
801 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800802 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100805SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
806 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800809 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int ret, forced = 0;
811
Kees Cook3d433212009-04-02 15:49:29 -0700812 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800813 return -EPERM;
814
815 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
816 return -EFAULT;
817 name[MODULE_NAME_LEN-1] = '\0';
818
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200819 if (mutex_lock_interruptible(&module_mutex) != 0)
820 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 mod = find_module(name);
823 if (!mod) {
824 ret = -ENOENT;
825 goto out;
826 }
827
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700828 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Other modules depend on us: get rid of them first. */
830 ret = -EWOULDBLOCK;
831 goto out;
832 }
833
834 /* Doing init or already dying? */
835 if (mod->state != MODULE_STATE_LIVE) {
836 /* FIXME: if (force), slam module count and wake up
837 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700838 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ret = -EBUSY;
840 goto out;
841 }
842
843 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700844 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800845 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!forced) {
847 /* This module can't be removed */
848 ret = -EBUSY;
849 goto out;
850 }
851 }
852
853 /* Set this up before setting mod->state */
854 mod->waiter = current;
855
856 /* Stop the machine so refcounts can't move and disable module. */
857 ret = try_stop_module(mod, flags, &forced);
858 if (ret != 0)
859 goto out;
860
861 /* Never wait if forced. */
862 if (!forced && module_refcount(mod) != 0)
863 wait_for_zero_refcount(mod);
864
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200865 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300866 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200867 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200869 blocking_notifier_call_chain(&module_notify_list,
870 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800871 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600872
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100873 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500874 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rusty Russell75676502010-06-05 11:17:36 -0600876 free_module(mod);
877 return 0;
878out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800879 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return ret;
881}
882
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800883static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct module_use *use;
886 int printed_something = 0;
887
Eric Dumazetbd77c042012-01-13 09:32:14 +1030888 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* Always include a trailing , so userspace can differentiate
891 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700892 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700894 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (mod->init != NULL && mod->exit == NULL) {
898 printed_something = 1;
899 seq_printf(m, "[permanent],");
900 }
901
902 if (!printed_something)
903 seq_printf(m, "-");
904}
905
906void __symbol_put(const char *symbol)
907{
908 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Rusty Russell24da1cb2007-07-15 23:41:46 -0700910 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500911 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 BUG();
913 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700914 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916EXPORT_SYMBOL(__symbol_put);
917
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930918/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919void symbol_put_addr(void *addr)
920{
Trent Piepho5e376612006-05-15 09:44:06 -0700921 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930922 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930924 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700925 return;
926
Rusty Russella6e6abd2009-03-31 13:05:31 -0600927 /* module_text_address is safe here: we're supposed to have reference
928 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930929 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600930 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700931 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933EXPORT_SYMBOL_GPL(symbol_put_addr);
934
935static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930936 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030938 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Kay Sieverscca3e702012-01-13 09:32:15 +1030941static struct module_attribute modinfo_refcnt =
942 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Steven Rostedtd53799b2012-03-26 12:50:52 +1030944void __module_get(struct module *module)
945{
946 if (module) {
947 preempt_disable();
948 __this_cpu_inc(module->refptr->incs);
949 trace_module_get(module, _RET_IP_);
950 preempt_enable();
951 }
952}
953EXPORT_SYMBOL(__module_get);
954
955bool try_module_get(struct module *module)
956{
957 bool ret = true;
958
959 if (module) {
960 preempt_disable();
961
962 if (likely(module_is_live(module))) {
963 __this_cpu_inc(module->refptr->incs);
964 trace_module_get(module, _RET_IP_);
965 } else
966 ret = false;
967
968 preempt_enable();
969 }
970 return ret;
971}
972EXPORT_SYMBOL(try_module_get);
973
Al Virof6a57032006-10-18 01:47:25 -0400974void module_put(struct module *module)
975{
976 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900977 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100978 smp_wmb(); /* see comment in module_refcount */
979 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900980
Li Zefanae832d12010-03-24 10:57:43 +0800981 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400982 /* Maybe they're waiting for us to drop reference? */
983 if (unlikely(!module_is_live(module)))
984 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900985 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400986 }
987}
988EXPORT_SYMBOL(module_put);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800991static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 /* We don't know the usage count, or what modules are using. */
994 seq_printf(m, " - -");
995}
996
997static inline void module_unload_free(struct module *mod)
998{
999}
1000
Rusty Russell9bea7f22010-06-05 11:17:37 -06001001int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001003 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001005EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001007static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011#endif /* CONFIG_MODULE_UNLOAD */
1012
Kevin Winchester53999bf2012-01-15 19:32:55 -04001013static size_t module_flags_taint(struct module *mod, char *buf)
1014{
1015 size_t l = 0;
1016
1017 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1018 buf[l++] = 'P';
1019 if (mod->taints & (1 << TAINT_OOT_MODULE))
1020 buf[l++] = 'O';
1021 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1022 buf[l++] = 'F';
1023 if (mod->taints & (1 << TAINT_CRAP))
1024 buf[l++] = 'C';
1025 /*
1026 * TAINT_FORCED_RMMOD: could be added.
1027 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1028 * apply to modules.
1029 */
1030 return l;
1031}
1032
Kay Sievers1f717402006-11-24 12:15:25 +01001033static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301034 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001035{
1036 const char *state = "unknown";
1037
Kay Sievers4befb022011-07-24 22:06:04 +09301038 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001039 case MODULE_STATE_LIVE:
1040 state = "live";
1041 break;
1042 case MODULE_STATE_COMING:
1043 state = "coming";
1044 break;
1045 case MODULE_STATE_GOING:
1046 state = "going";
1047 break;
1048 }
1049 return sprintf(buffer, "%s\n", state);
1050}
1051
Kay Sieverscca3e702012-01-13 09:32:15 +10301052static struct module_attribute modinfo_initstate =
1053 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001054
Kay Sievers88bfa322011-07-24 22:06:04 +09301055static ssize_t store_uevent(struct module_attribute *mattr,
1056 struct module_kobject *mk,
1057 const char *buffer, size_t count)
1058{
1059 enum kobject_action action;
1060
1061 if (kobject_action_type(buffer, count, &action) == 0)
1062 kobject_uevent(&mk->kobj, action);
1063 return count;
1064}
1065
Kay Sieverscca3e702012-01-13 09:32:15 +10301066struct module_attribute module_uevent =
1067 __ATTR(uevent, 0200, NULL, store_uevent);
1068
1069static ssize_t show_coresize(struct module_attribute *mattr,
1070 struct module_kobject *mk, char *buffer)
1071{
1072 return sprintf(buffer, "%u\n", mk->mod->core_size);
1073}
1074
1075static struct module_attribute modinfo_coresize =
1076 __ATTR(coresize, 0444, show_coresize, NULL);
1077
1078static ssize_t show_initsize(struct module_attribute *mattr,
1079 struct module_kobject *mk, char *buffer)
1080{
1081 return sprintf(buffer, "%u\n", mk->mod->init_size);
1082}
1083
1084static struct module_attribute modinfo_initsize =
1085 __ATTR(initsize, 0444, show_initsize, NULL);
1086
1087static ssize_t show_taint(struct module_attribute *mattr,
1088 struct module_kobject *mk, char *buffer)
1089{
1090 size_t l;
1091
1092 l = module_flags_taint(mk->mod, buffer);
1093 buffer[l++] = '\n';
1094 return l;
1095}
1096
1097static struct module_attribute modinfo_taint =
1098 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301099
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001100static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301101 &module_uevent,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001102 &modinfo_version,
1103 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301104 &modinfo_initstate,
1105 &modinfo_coresize,
1106 &modinfo_initsize,
1107 &modinfo_taint,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001108#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301109 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001110#endif
1111 NULL,
1112};
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114static const char vermagic[] = VERMAGIC_STRING;
1115
Rusty Russellc6e665c2009-03-31 13:05:33 -06001116static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001117{
1118#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001119 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001120 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1121 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001122 add_taint_module(mod, TAINT_FORCED_MODULE);
1123 return 0;
1124#else
1125 return -ENOEXEC;
1126#endif
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001130/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1131static unsigned long maybe_relocated(unsigned long crc,
1132 const struct module *crc_owner)
1133{
1134#ifdef ARCH_RELOCATES_KCRCTAB
1135 if (crc_owner == NULL)
1136 return crc - (unsigned long)reloc_start;
1137#endif
1138 return crc;
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static int check_version(Elf_Shdr *sechdrs,
1142 unsigned int versindex,
1143 const char *symname,
1144 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001145 const unsigned long *crc,
1146 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 unsigned int i, num_versions;
1149 struct modversion_info *versions;
1150
1151 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1152 if (!crc)
1153 return 1;
1154
Rusty Russella5dd6972008-05-09 16:24:21 +10001155 /* No versions at all? modprobe --force does this. */
1156 if (versindex == 0)
1157 return try_to_force_load(mod, symname) == 0;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 versions = (void *) sechdrs[versindex].sh_addr;
1160 num_versions = sechdrs[versindex].sh_size
1161 / sizeof(struct modversion_info);
1162
1163 for (i = 0; i < num_versions; i++) {
1164 if (strcmp(versions[i].name, symname) != 0)
1165 continue;
1166
Rusty Russelld4703ae2009-12-15 16:28:32 -06001167 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001169 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001170 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001171 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001173
Rusty Russella5dd6972008-05-09 16:24:21 +10001174 printk(KERN_WARNING "%s: no symbol version for %s\n",
1175 mod->name, symname);
1176 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001177
1178bad_version:
1179 printk("%s: disagrees about version of symbol %s\n",
1180 mod->name, symname);
1181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1185 unsigned int versindex,
1186 struct module *mod)
1187{
1188 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Rusty Russell75676502010-06-05 11:17:36 -06001190 /* Since this should be found in kernel (which can't be removed),
1191 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301192 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1193 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001195 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1196 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Rusty Russell91e37a72008-05-09 16:25:28 +10001199/* First part is kernel version, which we ignore if module has crcs. */
1200static inline int same_magic(const char *amagic, const char *bmagic,
1201 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Rusty Russell91e37a72008-05-09 16:25:28 +10001203 if (has_crcs) {
1204 amagic += strcspn(amagic, " ");
1205 bmagic += strcspn(bmagic, " ");
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return strcmp(amagic, bmagic) == 0;
1208}
1209#else
1210static inline int check_version(Elf_Shdr *sechdrs,
1211 unsigned int versindex,
1212 const char *symname,
1213 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001214 const unsigned long *crc,
1215 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 return 1;
1218}
1219
1220static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1221 unsigned int versindex,
1222 struct module *mod)
1223{
1224 return 1;
1225}
1226
Rusty Russell91e37a72008-05-09 16:25:28 +10001227static inline int same_magic(const char *amagic, const char *bmagic,
1228 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 return strcmp(amagic, bmagic) == 0;
1231}
1232#endif /* CONFIG_MODVERSIONS */
1233
Rusty Russell75676502010-06-05 11:17:36 -06001234/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001235static const struct kernel_symbol *resolve_symbol(struct module *mod,
1236 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001237 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001238 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001241 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001243 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Rusty Russell75676502010-06-05 11:17:36 -06001245 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001246 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001247 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001248 if (!sym)
1249 goto unlock;
1250
Rusty Russell49668682010-08-05 12:59:10 -06001251 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1252 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001253 sym = ERR_PTR(-EINVAL);
1254 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001256
1257 err = ref_module(mod, owner);
1258 if (err) {
1259 sym = ERR_PTR(err);
1260 goto getname;
1261 }
1262
1263getname:
1264 /* We must make copy under the lock if we failed to get ref. */
1265 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1266unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001267 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001268 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Rusty Russell49668682010-08-05 12:59:10 -06001271static const struct kernel_symbol *
1272resolve_symbol_wait(struct module *mod,
1273 const struct load_info *info,
1274 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001275{
1276 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001277 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001278
1279 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001280 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1281 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001282 30 * HZ) <= 0) {
1283 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001284 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001285 }
1286 return ksym;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/*
1290 * /sys/module/foo/sections stuff
1291 * J. Corbet <corbet@lwn.net>
1292 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001293#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001294
Rusty Russell8f6d0372010-08-05 12:59:09 -06001295#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001296static inline bool sect_empty(const Elf_Shdr *sect)
1297{
1298 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1299}
1300
Rusty Russella58730c2008-03-13 09:03:44 +00001301struct module_sect_attr
1302{
1303 struct module_attribute mattr;
1304 char *name;
1305 unsigned long address;
1306};
1307
1308struct module_sect_attrs
1309{
1310 struct attribute_group grp;
1311 unsigned int nsections;
1312 struct module_sect_attr attrs[0];
1313};
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301316 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct module_sect_attr *sattr =
1319 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001320 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001323static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1324{
Rusty Russella58730c2008-03-13 09:03:44 +00001325 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001326
1327 for (section = 0; section < sect_attrs->nsections; section++)
1328 kfree(sect_attrs->attrs[section].name);
1329 kfree(sect_attrs);
1330}
1331
Rusty Russell8f6d0372010-08-05 12:59:09 -06001332static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 unsigned int nloaded = 0, i, size[2];
1335 struct module_sect_attrs *sect_attrs;
1336 struct module_sect_attr *sattr;
1337 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001340 for (i = 0; i < info->hdr->e_shnum; i++)
1341 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 nloaded++;
1343 size[0] = ALIGN(sizeof(*sect_attrs)
1344 + nloaded * sizeof(sect_attrs->attrs[0]),
1345 sizeof(sect_attrs->grp.attrs[0]));
1346 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001347 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1348 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return;
1350
1351 /* Setup section attributes. */
1352 sect_attrs->grp.name = "sections";
1353 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1354
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001355 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 sattr = &sect_attrs->attrs[0];
1357 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001358 for (i = 0; i < info->hdr->e_shnum; i++) {
1359 Elf_Shdr *sec = &info->sechdrs[i];
1360 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001361 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001362 sattr->address = sec->sh_addr;
1363 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001364 GFP_KERNEL);
1365 if (sattr->name == NULL)
1366 goto out;
1367 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001368 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 sattr->mattr.show = module_sect_show;
1370 sattr->mattr.store = NULL;
1371 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 sattr->mattr.attr.mode = S_IRUGO;
1373 *(gattr++) = &(sattr++)->mattr.attr;
1374 }
1375 *gattr = NULL;
1376
1377 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1378 goto out;
1379
1380 mod->sect_attrs = sect_attrs;
1381 return;
1382 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001383 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386static void remove_sect_attrs(struct module *mod)
1387{
1388 if (mod->sect_attrs) {
1389 sysfs_remove_group(&mod->mkobj.kobj,
1390 &mod->sect_attrs->grp);
1391 /* We are positive that no one is using any sect attrs
1392 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001393 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 mod->sect_attrs = NULL;
1395 }
1396}
1397
Roland McGrath6d760132007-10-16 23:26:40 -07001398/*
1399 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1400 */
1401
1402struct module_notes_attrs {
1403 struct kobject *dir;
1404 unsigned int notes;
1405 struct bin_attribute attrs[0];
1406};
1407
Chris Wright2c3c8be2010-05-12 18:28:57 -07001408static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001409 struct bin_attribute *bin_attr,
1410 char *buf, loff_t pos, size_t count)
1411{
1412 /*
1413 * The caller checked the pos and count against our size.
1414 */
1415 memcpy(buf, bin_attr->private + pos, count);
1416 return count;
1417}
1418
1419static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1420 unsigned int i)
1421{
1422 if (notes_attrs->dir) {
1423 while (i-- > 0)
1424 sysfs_remove_bin_file(notes_attrs->dir,
1425 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001426 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001427 }
1428 kfree(notes_attrs);
1429}
1430
Rusty Russell8f6d0372010-08-05 12:59:09 -06001431static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001432{
1433 unsigned int notes, loaded, i;
1434 struct module_notes_attrs *notes_attrs;
1435 struct bin_attribute *nattr;
1436
Ingo Molnarea6bff32009-08-28 10:44:56 +02001437 /* failed to create section attributes, so can't create notes */
1438 if (!mod->sect_attrs)
1439 return;
1440
Roland McGrath6d760132007-10-16 23:26:40 -07001441 /* Count notes sections and allocate structures. */
1442 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001443 for (i = 0; i < info->hdr->e_shnum; i++)
1444 if (!sect_empty(&info->sechdrs[i]) &&
1445 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001446 ++notes;
1447
1448 if (notes == 0)
1449 return;
1450
1451 notes_attrs = kzalloc(sizeof(*notes_attrs)
1452 + notes * sizeof(notes_attrs->attrs[0]),
1453 GFP_KERNEL);
1454 if (notes_attrs == NULL)
1455 return;
1456
1457 notes_attrs->notes = notes;
1458 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001459 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1460 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001461 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001462 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001463 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001464 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1465 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001466 nattr->size = info->sechdrs[i].sh_size;
1467 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001468 nattr->read = module_notes_read;
1469 ++nattr;
1470 }
1471 ++loaded;
1472 }
1473
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001474 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001475 if (!notes_attrs->dir)
1476 goto out;
1477
1478 for (i = 0; i < notes; ++i)
1479 if (sysfs_create_bin_file(notes_attrs->dir,
1480 &notes_attrs->attrs[i]))
1481 goto out;
1482
1483 mod->notes_attrs = notes_attrs;
1484 return;
1485
1486 out:
1487 free_notes_attrs(notes_attrs, i);
1488}
1489
1490static void remove_notes_attrs(struct module *mod)
1491{
1492 if (mod->notes_attrs)
1493 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001497
Rusty Russell8f6d0372010-08-05 12:59:09 -06001498static inline void add_sect_attrs(struct module *mod,
1499 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501}
1502
1503static inline void remove_sect_attrs(struct module *mod)
1504{
1505}
Roland McGrath6d760132007-10-16 23:26:40 -07001506
Rusty Russell8f6d0372010-08-05 12:59:09 -06001507static inline void add_notes_attrs(struct module *mod,
1508 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001509{
1510}
1511
1512static inline void remove_notes_attrs(struct module *mod)
1513{
1514}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001515#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001517static void add_usage_links(struct module *mod)
1518{
1519#ifdef CONFIG_MODULE_UNLOAD
1520 struct module_use *use;
1521 int nowarn;
1522
Rusty Russell75676502010-06-05 11:17:36 -06001523 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001524 list_for_each_entry(use, &mod->target_list, target_list) {
1525 nowarn = sysfs_create_link(use->target->holders_dir,
1526 &mod->mkobj.kobj, mod->name);
1527 }
Rusty Russell75676502010-06-05 11:17:36 -06001528 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001529#endif
1530}
1531
1532static void del_usage_links(struct module *mod)
1533{
1534#ifdef CONFIG_MODULE_UNLOAD
1535 struct module_use *use;
1536
Rusty Russell75676502010-06-05 11:17:36 -06001537 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001538 list_for_each_entry(use, &mod->target_list, target_list)
1539 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001540 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001541#endif
1542}
1543
Rusty Russell6407ebb22010-06-05 11:17:36 -06001544static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001545{
1546 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001547 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001548 int error = 0;
1549 int i;
1550
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001551 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1552 (ARRAY_SIZE(modinfo_attrs) + 1)),
1553 GFP_KERNEL);
1554 if (!mod->modinfo_attrs)
1555 return -ENOMEM;
1556
1557 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001558 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1559 if (!attr->test ||
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001560 (attr->test && attr->test(mod))) {
1561 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001562 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001563 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1564 ++temp_attr;
1565 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001566 }
1567 return error;
1568}
1569
Rusty Russell6407ebb22010-06-05 11:17:36 -06001570static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001571{
1572 struct module_attribute *attr;
1573 int i;
1574
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001575 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1576 /* pick a field to test for end of list */
1577 if (!attr->attr.name)
1578 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001579 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001580 if (attr->free)
1581 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001582 }
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001583 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Rusty Russell6407ebb22010-06-05 11:17:36 -06001586static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001589 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001591 if (!module_sysfs_initialized) {
1592 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001593 mod->name);
1594 err = -EINVAL;
1595 goto out;
1596 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001597
1598 kobj = kset_find_obj(module_kset, mod->name);
1599 if (kobj) {
1600 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1601 kobject_put(kobj);
1602 err = -EINVAL;
1603 goto out;
1604 }
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001607
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001608 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1609 mod->mkobj.kobj.kset = module_kset;
1610 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1611 "%s", mod->name);
1612 if (err)
1613 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001614
Kay Sievers97c146e2007-11-29 23:46:11 +01001615 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001616out:
1617 return err;
1618}
1619
Rusty Russell6407ebb22010-06-05 11:17:36 -06001620static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001621 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001622 struct kernel_param *kparam,
1623 unsigned int num_params)
1624{
1625 int err;
1626
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001627 err = mod_sysfs_init(mod);
1628 if (err)
1629 goto out;
1630
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001631 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001632 if (!mod->holders_dir) {
1633 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001634 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001635 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 err = module_param_sysfs_setup(mod, kparam, num_params);
1638 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001639 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Matt Domschc988d2b2005-06-23 22:05:15 -07001641 err = module_add_modinfo_attrs(mod);
1642 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001643 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001644
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001645 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001646 add_sect_attrs(mod, info);
1647 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001648
Kay Sieverse17e0f52006-11-24 12:15:25 +01001649 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return 0;
1651
Kay Sieverse17e0f52006-11-24 12:15:25 +01001652out_unreg_param:
1653 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001654out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001655 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001656out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001657 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return err;
1660}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001661
1662static void mod_sysfs_fini(struct module *mod)
1663{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001664 remove_notes_attrs(mod);
1665 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001666 kobject_put(&mod->mkobj.kobj);
1667}
1668
Rusty Russell8f6d0372010-08-05 12:59:09 -06001669#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001670
Rusty Russell8f6d0372010-08-05 12:59:09 -06001671static int mod_sysfs_setup(struct module *mod,
1672 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001673 struct kernel_param *kparam,
1674 unsigned int num_params)
1675{
1676 return 0;
1677}
1678
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001679static void mod_sysfs_fini(struct module *mod)
1680{
1681}
1682
Rusty Russell36b03602010-08-05 12:59:09 -06001683static void module_remove_modinfo_attrs(struct module *mod)
1684{
1685}
1686
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001687static void del_usage_links(struct module *mod)
1688{
1689}
1690
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001691#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Rusty Russell36b03602010-08-05 12:59:09 -06001693static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001695 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001696 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001698 kobject_put(mod->mkobj.drivers_dir);
1699 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001700 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703/*
1704 * unlink the module with the whole machine is stopped with interrupts off
1705 * - this defends against kallsyms not taking locks
1706 */
1707static int __unlink_module(void *_mod)
1708{
1709 struct module *mod = _mod;
1710 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001711 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713}
1714
matthieu castet84e1c6b2010-11-16 22:35:16 +01001715#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1716/*
1717 * LKM RO/NX protection: protect module's text/ro-data
1718 * from modification and any data from execution.
1719 */
1720void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1721{
1722 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1723 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1724
1725 if (end_pfn > begin_pfn)
1726 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1727}
1728
1729static void set_section_ro_nx(void *base,
1730 unsigned long text_size,
1731 unsigned long ro_size,
1732 unsigned long total_size)
1733{
1734 /* begin and end PFNs of the current subsection */
1735 unsigned long begin_pfn;
1736 unsigned long end_pfn;
1737
1738 /*
1739 * Set RO for module text and RO-data:
1740 * - Always protect first page.
1741 * - Do not protect last partial page.
1742 */
1743 if (ro_size > 0)
1744 set_page_attributes(base, base + ro_size, set_memory_ro);
1745
1746 /*
1747 * Set NX permissions for module data:
1748 * - Do not protect first partial page.
1749 * - Always protect last page.
1750 */
1751 if (total_size > text_size) {
1752 begin_pfn = PFN_UP((unsigned long)base + text_size);
1753 end_pfn = PFN_UP((unsigned long)base + total_size);
1754 if (end_pfn > begin_pfn)
1755 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1756 }
1757}
1758
Jan Glauber01526ed2011-05-19 16:55:26 -06001759static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001760{
Jan Glauber01526ed2011-05-19 16:55:26 -06001761 set_page_attributes(mod->module_core + mod->core_text_size,
1762 mod->module_core + mod->core_size,
1763 set_memory_x);
1764 set_page_attributes(mod->module_core,
1765 mod->module_core + mod->core_ro_size,
1766 set_memory_rw);
1767}
1768
1769static void unset_module_init_ro_nx(struct module *mod)
1770{
1771 set_page_attributes(mod->module_init + mod->init_text_size,
1772 mod->module_init + mod->init_size,
1773 set_memory_x);
1774 set_page_attributes(mod->module_init,
1775 mod->module_init + mod->init_ro_size,
1776 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001777}
1778
1779/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001780void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001781{
1782 struct module *mod;
1783
1784 mutex_lock(&module_mutex);
1785 list_for_each_entry_rcu(mod, &modules, list) {
1786 if ((mod->module_core) && (mod->core_text_size)) {
1787 set_page_attributes(mod->module_core,
1788 mod->module_core + mod->core_text_size,
1789 set_memory_rw);
1790 }
1791 if ((mod->module_init) && (mod->init_text_size)) {
1792 set_page_attributes(mod->module_init,
1793 mod->module_init + mod->init_text_size,
1794 set_memory_rw);
1795 }
1796 }
1797 mutex_unlock(&module_mutex);
1798}
1799
1800/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001801void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001802{
1803 struct module *mod;
1804
1805 mutex_lock(&module_mutex);
1806 list_for_each_entry_rcu(mod, &modules, list) {
1807 if ((mod->module_core) && (mod->core_text_size)) {
1808 set_page_attributes(mod->module_core,
1809 mod->module_core + mod->core_text_size,
1810 set_memory_ro);
1811 }
1812 if ((mod->module_init) && (mod->init_text_size)) {
1813 set_page_attributes(mod->module_init,
1814 mod->module_init + mod->init_text_size,
1815 set_memory_ro);
1816 }
1817 }
1818 mutex_unlock(&module_mutex);
1819}
1820#else
1821static 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 -06001822static void unset_module_core_ro_nx(struct module *mod) { }
1823static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001824#endif
1825
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001826void __weak module_free(struct module *mod, void *module_region)
1827{
1828 vfree(module_region);
1829}
1830
1831void __weak module_arch_cleanup(struct module *mod)
1832{
1833}
1834
Rusty Russell75676502010-06-05 11:17:36 -06001835/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836static void free_module(struct module *mod)
1837{
Li Zefan7ead8b82009-08-17 16:56:28 +08001838 trace_module_free(mod);
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001841 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001842 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001843 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001844 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Jason Baronb82bab4b2010-07-27 13:18:01 -07001846 /* Remove dynamic debug info */
1847 ddebug_remove_module(mod->name);
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* Arch-specific cleanup. */
1850 module_arch_cleanup(mod);
1851
1852 /* Module unload stuff */
1853 module_unload_free(mod);
1854
Rusty Russelle180a6b2009-03-31 13:05:29 -06001855 /* Free any allocated parameters. */
1856 destroy_params(mod->kp, mod->num_kp);
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001859 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 module_free(mod, mod->module_init);
1861 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001862 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001863
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001864 /* Free lock-classes: */
1865 lockdep_free_key_range(mod->module_core, mod->core_size);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001868 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001870
1871#ifdef CONFIG_MPU
1872 update_protections(current->mm);
1873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
1876void *__symbol_get(const char *symbol)
1877{
1878 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001879 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Rusty Russell24da1cb2007-07-15 23:41:46 -07001881 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001882 sym = find_symbol(symbol, &owner, NULL, true, true);
1883 if (sym && strong_try_module_get(owner))
1884 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001885 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Tim Abbott414fd312008-12-05 19:03:56 -05001887 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889EXPORT_SYMBOL_GPL(__symbol_get);
1890
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001891/*
1892 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001893 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001894 *
1895 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001896 */
1897static int verify_export_symbols(struct module *mod)
1898{
Rusty Russellb2111042008-05-01 21:15:00 -05001899 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001900 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001901 const struct kernel_symbol *s;
1902 struct {
1903 const struct kernel_symbol *sym;
1904 unsigned int num;
1905 } arr[] = {
1906 { mod->syms, mod->num_syms },
1907 { mod->gpl_syms, mod->num_gpl_syms },
1908 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001909#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001910 { mod->unused_syms, mod->num_unused_syms },
1911 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001912#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001913 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001914
Rusty Russellb2111042008-05-01 21:15:00 -05001915 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1916 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001917 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001918 printk(KERN_ERR
1919 "%s: exports duplicate symbol %s"
1920 " (owned by %s)\n",
1921 mod->name, s->name, module_name(owner));
1922 return -ENOEXEC;
1923 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001924 }
Rusty Russellb2111042008-05-01 21:15:00 -05001925 }
1926 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001927}
1928
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001929/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001930static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Rusty Russell49668682010-08-05 12:59:10 -06001932 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1933 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001935 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001937 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Rusty Russell49668682010-08-05 12:59:10 -06001939 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1940 const char *name = info->strtab + sym[i].st_name;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 switch (sym[i].st_shndx) {
1943 case SHN_COMMON:
1944 /* We compiled with -fno-common. These are not
1945 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001946 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 printk("%s: please compile with -fno-common\n",
1948 mod->name);
1949 ret = -ENOEXEC;
1950 break;
1951
1952 case SHN_ABS:
1953 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001954 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 (long)sym[i].st_value);
1956 break;
1957
1958 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001959 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001961 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001962 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001964 }
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001967 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
1969
Rusty Russell9bea7f22010-06-05 11:17:37 -06001970 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001971 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001972 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 break;
1974
1975 default:
1976 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001977 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001978 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 else
Rusty Russell49668682010-08-05 12:59:10 -06001980 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 sym[i].st_value += secbase;
1982 break;
1983 }
1984 }
1985
1986 return ret;
1987}
1988
Rusty Russell49668682010-08-05 12:59:10 -06001989static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001990{
1991 unsigned int i;
1992 int err = 0;
1993
1994 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001995 for (i = 1; i < info->hdr->e_shnum; i++) {
1996 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001997
1998 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001999 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002000 continue;
2001
2002 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002003 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002004 continue;
2005
Rusty Russell49668682010-08-05 12:59:10 -06002006 if (info->sechdrs[i].sh_type == SHT_REL)
2007 err = apply_relocate(info->sechdrs, info->strtab,
2008 info->index.sym, i, mod);
2009 else if (info->sechdrs[i].sh_type == SHT_RELA)
2010 err = apply_relocate_add(info->sechdrs, info->strtab,
2011 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002012 if (err < 0)
2013 break;
2014 }
2015 return err;
2016}
2017
Helge Deller088af9a2008-12-31 12:31:18 +01002018/* Additional bytes needed by arch in front of individual sections */
2019unsigned int __weak arch_mod_section_prepend(struct module *mod,
2020 unsigned int section)
2021{
2022 /* default implementation just returns zero */
2023 return 0;
2024}
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002027static long get_offset(struct module *mod, unsigned int *size,
2028 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 long ret;
2031
Helge Deller088af9a2008-12-31 12:31:18 +01002032 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2034 *size = ret + sechdr->sh_size;
2035 return ret;
2036}
2037
2038/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2039 might -- code, read-only data, read-write data, small data. Tally
2040 sizes, and place the offsets into sh_entsize fields: high bit means it
2041 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002042static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 static unsigned long const masks[][2] = {
2045 /* NOTE: all executable code must be the first section
2046 * in this array; otherwise modify the text_size
2047 * finder in the two loops below */
2048 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2049 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2050 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2051 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2052 };
2053 unsigned int m, i;
2054
Rusty Russell49668682010-08-05 12:59:10 -06002055 for (i = 0; i < info->hdr->e_shnum; i++)
2056 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Jim Cromie5e124162011-12-06 12:11:31 -07002058 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002060 for (i = 0; i < info->hdr->e_shnum; ++i) {
2061 Elf_Shdr *s = &info->sechdrs[i];
2062 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2065 || (s->sh_flags & masks[m][1])
2066 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002067 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002069 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002070 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002072 switch (m) {
2073 case 0: /* executable */
2074 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002076 break;
2077 case 1: /* RO: text and ro-data */
2078 mod->core_size = debug_align(mod->core_size);
2079 mod->core_ro_size = mod->core_size;
2080 break;
2081 case 3: /* whole core */
2082 mod->core_size = debug_align(mod->core_size);
2083 break;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086
Jim Cromie5e124162011-12-06 12:11:31 -07002087 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002089 for (i = 0; i < info->hdr->e_shnum; ++i) {
2090 Elf_Shdr *s = &info->sechdrs[i];
2091 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2094 || (s->sh_flags & masks[m][1])
2095 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002096 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002098 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002100 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002102 switch (m) {
2103 case 0: /* executable */
2104 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002106 break;
2107 case 1: /* RO: text and ro-data */
2108 mod->init_size = debug_align(mod->init_size);
2109 mod->init_ro_size = mod->init_size;
2110 break;
2111 case 3: /* whole init */
2112 mod->init_size = debug_align(mod->init_size);
2113 break;
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118static void set_license(struct module *mod, const char *license)
2119{
2120 if (!license)
2121 license = "unspecified";
2122
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002123 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002124 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002125 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002126 "kernel.\n", mod->name, license);
2127 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129}
2130
2131/* Parse tag=value strings from .modinfo section */
2132static char *next_string(char *string, unsigned long *secsize)
2133{
2134 /* Skip non-zero chars */
2135 while (string[0]) {
2136 string++;
2137 if ((*secsize)-- <= 1)
2138 return NULL;
2139 }
2140
2141 /* Skip any zero padding. */
2142 while (!string[0]) {
2143 string++;
2144 if ((*secsize)-- <= 1)
2145 return NULL;
2146 }
2147 return string;
2148}
2149
Rusty Russell49668682010-08-05 12:59:10 -06002150static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 char *p;
2153 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002154 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2155 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Rusty Russell49668682010-08-05 12:59:10 -06002157 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2159 return p + taglen + 1;
2160 }
2161 return NULL;
2162}
2163
Rusty Russell49668682010-08-05 12:59:10 -06002164static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002165{
2166 struct module_attribute *attr;
2167 int i;
2168
2169 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2170 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002171 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002172 }
2173}
Matt Domschc988d2b2005-06-23 22:05:15 -07002174
Rusty Russella263f772009-09-25 00:32:58 -06002175static void free_modinfo(struct module *mod)
2176{
2177 struct module_attribute *attr;
2178 int i;
2179
2180 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2181 if (attr->free)
2182 attr->free(mod);
2183 }
2184}
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002187
2188/* lookup symbol in given range of kernel_symbols */
2189static const struct kernel_symbol *lookup_symbol(const char *name,
2190 const struct kernel_symbol *start,
2191 const struct kernel_symbol *stop)
2192{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002193 return bsearch(name, start, stop - start,
2194 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002195}
2196
Tim Abbottca4787b2009-01-05 08:40:10 -06002197static int is_exported(const char *name, unsigned long value,
2198 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Tim Abbottca4787b2009-01-05 08:40:10 -06002200 const struct kernel_symbol *ks;
2201 if (!mod)
2202 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002203 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002204 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2205 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002209static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Rusty Russelleded41c2010-08-05 12:59:07 -06002211 const Elf_Shdr *sechdrs = info->sechdrs;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2214 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2215 return 'v';
2216 else
2217 return 'w';
2218 }
2219 if (sym->st_shndx == SHN_UNDEF)
2220 return 'U';
2221 if (sym->st_shndx == SHN_ABS)
2222 return 'a';
2223 if (sym->st_shndx >= SHN_LORESERVE)
2224 return '?';
2225 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2226 return 't';
2227 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2228 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2229 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2230 return 'r';
2231 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2232 return 'g';
2233 else
2234 return 'd';
2235 }
2236 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2237 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2238 return 's';
2239 else
2240 return 'b';
2241 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002242 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2243 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return '?';
2247}
2248
Jan Beulich4a496222009-07-06 14:50:42 +01002249static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2250 unsigned int shnum)
2251{
2252 const Elf_Shdr *sec;
2253
2254 if (src->st_shndx == SHN_UNDEF
2255 || src->st_shndx >= shnum
2256 || !src->st_name)
2257 return false;
2258
2259 sec = sechdrs + src->st_shndx;
2260 if (!(sec->sh_flags & SHF_ALLOC)
2261#ifndef CONFIG_KALLSYMS_ALL
2262 || !(sec->sh_flags & SHF_EXECINSTR)
2263#endif
2264 || (sec->sh_entsize & INIT_OFFSET_MASK))
2265 return false;
2266
2267 return true;
2268}
2269
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302270/*
2271 * We only allocate and copy the strings needed by the parts of symtab
2272 * we keep. This is simple, but has the effect of making multiple
2273 * copies of duplicates. We could be more sophisticated, see
2274 * linux-kernel thread starting with
2275 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2276 */
Rusty Russell49668682010-08-05 12:59:10 -06002277static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002278{
Rusty Russell49668682010-08-05 12:59:10 -06002279 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2280 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002281 const Elf_Sym *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302282 unsigned int i, nsrc, ndst, strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002283
2284 /* Put symbol section at end of init part of module. */
2285 symsect->sh_flags |= SHF_ALLOC;
2286 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002287 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002288 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002289
Rusty Russell49668682010-08-05 12:59:10 -06002290 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002291 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002292
Rusty Russell59ef28b2012-10-25 10:49:25 +10302293 /* strtab always starts with a nul, so offset 0 is the empty string. */
2294 strtab_size = 1;
2295
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302296 /* Compute total space required for the core symbols' strtab. */
Rusty Russell59ef28b2012-10-25 10:49:25 +10302297 for (ndst = i = 0; i < nsrc; i++) {
2298 if (i == 0 ||
2299 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2300 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302301 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002302 }
Rusty Russell59ef28b2012-10-25 10:49:25 +10302303 }
Jan Beulich4a496222009-07-06 14:50:42 +01002304
2305 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002306 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302307 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2308 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002309
Jan Beulich554bdfe2009-07-06 14:51:44 +01002310 /* Put string table section at end of init part of module. */
2311 strsect->sh_flags |= SHF_ALLOC;
2312 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002313 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002314 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002315}
2316
Rusty Russell811d66a2010-08-05 12:59:12 -06002317static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Jan Beulich4a496222009-07-06 14:50:42 +01002319 unsigned int i, ndst;
2320 const Elf_Sym *src;
2321 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002322 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002323 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Rusty Russelleded41c2010-08-05 12:59:07 -06002325 mod->symtab = (void *)symsec->sh_addr;
2326 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002327 /* Make sure we get permanent strtab: don't use info->strtab. */
2328 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 /* Set types up while we still have access to sections. */
2331 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002332 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002333
Rusty Russelld9131882010-08-05 12:59:08 -06002334 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302335 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002336 src = mod->symtab;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302337 *s++ = 0;
Rusty Russell59ef28b2012-10-25 10:49:25 +10302338 for (ndst = i = 0; i < mod->num_symtab; i++) {
2339 if (i == 0 ||
2340 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
2341 dst[ndst] = src[i];
2342 dst[ndst++].st_name = s - mod->core_strtab;
2343 s += strlcpy(s, &mod->strtab[src[i].st_name],
2344 KSYM_NAME_LEN) + 1;
2345 }
Jan Beulich4a496222009-07-06 14:50:42 +01002346 }
2347 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349#else
Rusty Russell49668682010-08-05 12:59:10 -06002350static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002351{
2352}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002353
Michał Mirosławabbce902010-09-20 01:58:08 +02002354static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356}
2357#endif /* CONFIG_KALLSYMS */
2358
Jason Barone9d376f2009-02-05 11:51:38 -05002359static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002360{
Rusty Russell811d66a2010-08-05 12:59:12 -06002361 if (!debug)
2362 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002363#ifdef CONFIG_DYNAMIC_DEBUG
2364 if (ddebug_add_module(debug, num, debug->modname))
2365 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2366 debug->modname);
2367#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002368}
Jason Baron346e15b2008-08-12 16:46:19 -04002369
Yehuda Sadehff49d742010-07-03 13:07:35 +10002370static void dynamic_debug_remove(struct _ddebug *debug)
2371{
2372 if (debug)
2373 ddebug_remove_module(debug->modname);
2374}
2375
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002376void * __weak module_alloc(unsigned long size)
2377{
2378 return size == 0 ? NULL : vmalloc_exec(size);
2379}
2380
Rusty Russell3a642e92008-07-22 19:24:28 -05002381static void *module_alloc_update_bounds(unsigned long size)
2382{
2383 void *ret = module_alloc(size);
2384
2385 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002386 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002387 /* Update module bounds. */
2388 if ((unsigned long)ret < module_addr_min)
2389 module_addr_min = (unsigned long)ret;
2390 if ((unsigned long)ret + size > module_addr_max)
2391 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002392 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002393 }
2394 return ret;
2395}
2396
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002397#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002398static void kmemleak_load_module(const struct module *mod,
2399 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002400{
2401 unsigned int i;
2402
2403 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002404 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002405
Rusty Russell49668682010-08-05 12:59:10 -06002406 for (i = 1; i < info->hdr->e_shnum; i++) {
2407 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2408 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002409 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002410 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002411 continue;
2412
Rusty Russell49668682010-08-05 12:59:10 -06002413 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2414 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002415 }
2416}
2417#else
Rusty Russell49668682010-08-05 12:59:10 -06002418static inline void kmemleak_load_module(const struct module *mod,
2419 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002420{
2421}
2422#endif
2423
Rusty Russell106a4ee2012-09-26 10:09:40 +01002424#ifdef CONFIG_MODULE_SIG
2425static int module_sig_check(struct load_info *info,
David Howellscaabe242012-10-20 01:19:29 +01002426 const void *mod, unsigned long *_len)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002427{
2428 int err = -ENOKEY;
David Howellscaabe242012-10-20 01:19:29 +01002429 unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2430 unsigned long len = *_len;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002431
David Howellscaabe242012-10-20 01:19:29 +01002432 if (len > markerlen &&
2433 memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2434 /* We truncate the module to discard the signature */
2435 *_len -= markerlen;
2436 err = mod_verify_sig(mod, _len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002437 }
2438
2439 if (!err) {
2440 info->sig_ok = true;
2441 return 0;
2442 }
2443
2444 /* Not having a signature is only an error if we're strict. */
David Howells1d0059f2012-09-26 10:09:50 +01002445 if (err < 0 && fips_enabled)
2446 panic("Module verification failed with error %d in FIPS mode\n",
2447 err);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002448 if (err == -ENOKEY && !sig_enforce)
2449 err = 0;
2450
2451 return err;
2452}
2453#else /* !CONFIG_MODULE_SIG */
2454static int module_sig_check(struct load_info *info,
2455 void *mod, unsigned long *len)
2456{
2457 return 0;
2458}
2459#endif /* !CONFIG_MODULE_SIG */
2460
2461/* Sets info->hdr, info->len and info->sig_ok. */
Rusty Russelld9131882010-08-05 12:59:08 -06002462static int copy_and_check(struct load_info *info,
2463 const void __user *umod, unsigned long len,
2464 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002465{
2466 int err;
2467 Elf_Ehdr *hdr;
2468
2469 if (len < sizeof(*hdr))
2470 return -ENOEXEC;
2471
2472 /* Suck in entire file: we'll want most of it. */
Sasha Levinf946eeb2012-01-30 23:07:22 -05002473 if ((hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002474 return -ENOMEM;
2475
2476 if (copy_from_user(hdr, umod, len) != 0) {
2477 err = -EFAULT;
2478 goto free_hdr;
2479 }
2480
Rusty Russell106a4ee2012-09-26 10:09:40 +01002481 err = module_sig_check(info, hdr, &len);
2482 if (err)
2483 goto free_hdr;
2484
Rusty Russell40dd2562010-08-05 12:59:03 -06002485 /* Sanity checks against insmoding binaries or wrong arch,
2486 weird elf version */
2487 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2488 || hdr->e_type != ET_REL
2489 || !elf_check_arch(hdr)
2490 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2491 err = -ENOEXEC;
2492 goto free_hdr;
2493 }
2494
David Howellsef26a5a2012-05-22 15:56:13 +01002495 if (hdr->e_shoff >= len ||
2496 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002497 err = -ENOEXEC;
2498 goto free_hdr;
2499 }
Rusty Russelld9131882010-08-05 12:59:08 -06002500
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002501 info->hdr = hdr;
2502 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002503 return 0;
2504
2505free_hdr:
2506 vfree(hdr);
2507 return err;
2508}
2509
Rusty Russelld9131882010-08-05 12:59:08 -06002510static void free_copy(struct load_info *info)
2511{
Rusty Russelld9131882010-08-05 12:59:08 -06002512 vfree(info->hdr);
2513}
2514
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002515static int rewrite_section_headers(struct load_info *info)
2516{
2517 unsigned int i;
2518
2519 /* This should always be true, but let's be sure. */
2520 info->sechdrs[0].sh_addr = 0;
2521
2522 for (i = 1; i < info->hdr->e_shnum; i++) {
2523 Elf_Shdr *shdr = &info->sechdrs[i];
2524 if (shdr->sh_type != SHT_NOBITS
2525 && info->len < shdr->sh_offset + shdr->sh_size) {
2526 printk(KERN_ERR "Module len %lu truncated\n",
2527 info->len);
2528 return -ENOEXEC;
2529 }
2530
2531 /* Mark all sections sh_addr with their address in the
2532 temporary image. */
2533 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2534
2535#ifndef CONFIG_MODULE_UNLOAD
2536 /* Don't load .exit sections */
2537 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2538 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2539#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002540 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002541
2542 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002543 info->index.vers = find_sec(info, "__versions");
2544 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002545 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2546 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002547 return 0;
2548}
2549
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002550/*
2551 * Set up our basic convenience variables (pointers to section headers,
2552 * search for module section index etc), and do some basic section
2553 * verification.
2554 *
2555 * Return the temporary module pointer (we'll replace it with the final
2556 * one when we move the module sections around).
2557 */
2558static struct module *setup_load_info(struct load_info *info)
2559{
2560 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002561 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002562 struct module *mod;
2563
2564 /* Set up the convenience variables */
2565 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002566 info->secstrings = (void *)info->hdr
2567 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002568
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002569 err = rewrite_section_headers(info);
2570 if (err)
2571 return ERR_PTR(err);
2572
2573 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002574 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002575 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2576 info->index.sym = i;
2577 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002578 info->strtab = (char *)info->hdr
2579 + info->sechdrs[info->index.str].sh_offset;
2580 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002581 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002582 }
2583
Rusty Russell49668682010-08-05 12:59:10 -06002584 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002585 if (!info->index.mod) {
2586 printk(KERN_WARNING "No module found in object\n");
2587 return ERR_PTR(-ENOEXEC);
2588 }
2589 /* This is temporary: point mod into copy of data. */
2590 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2591
2592 if (info->index.sym == 0) {
2593 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2594 mod->name);
2595 return ERR_PTR(-ENOEXEC);
2596 }
2597
Rusty Russell49668682010-08-05 12:59:10 -06002598 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002599
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002600 /* Check module struct version now, before we try to use module. */
2601 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2602 return ERR_PTR(-ENOEXEC);
2603
2604 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002605}
2606
Rusty Russell49668682010-08-05 12:59:10 -06002607static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002608{
Rusty Russell49668682010-08-05 12:59:10 -06002609 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002610 int err;
2611
2612 /* This is allowed: modprobe --force will invalidate it. */
2613 if (!modmagic) {
2614 err = try_to_force_load(mod, "bad vermagic");
2615 if (err)
2616 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002617 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002618 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2619 mod->name, modmagic, vermagic);
2620 return -ENOEXEC;
2621 }
2622
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002623 if (!get_modinfo(info, "intree"))
2624 add_taint_module(mod, TAINT_OOT_MODULE);
2625
Rusty Russell49668682010-08-05 12:59:10 -06002626 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002627 add_taint_module(mod, TAINT_CRAP);
2628 printk(KERN_WARNING "%s: module is from the staging directory,"
2629 " the quality is unknown, you have been warned.\n",
2630 mod->name);
2631 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002632
2633 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002634 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002635
Rusty Russell40dd2562010-08-05 12:59:03 -06002636 return 0;
2637}
2638
Rusty Russell811d66a2010-08-05 12:59:12 -06002639static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002640{
Rusty Russell49668682010-08-05 12:59:10 -06002641 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002642 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002643 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002644 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002645 mod->crcs = section_addr(info, "__kcrctab");
2646 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002647 sizeof(*mod->gpl_syms),
2648 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002649 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2650 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002651 "__ksymtab_gpl_future",
2652 sizeof(*mod->gpl_future_syms),
2653 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002654 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002655
2656#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002657 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002658 sizeof(*mod->unused_syms),
2659 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002660 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2661 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002662 sizeof(*mod->unused_gpl_syms),
2663 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002664 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002665#endif
2666#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002667 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002668 sizeof(*mod->ctors), &mod->num_ctors);
2669#endif
2670
2671#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002672 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2673 sizeof(*mod->tracepoints_ptrs),
2674 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002675#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002676#ifdef HAVE_JUMP_LABEL
2677 mod->jump_entries = section_objs(info, "__jump_table",
2678 sizeof(*mod->jump_entries),
2679 &mod->num_jump_entries);
2680#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002681#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002682 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002683 sizeof(*mod->trace_events),
2684 &mod->num_trace_events);
2685 /*
2686 * This section contains pointers to allocated objects in the trace
2687 * code and not scanning it leads to false positives.
2688 */
2689 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2690 mod->num_trace_events, GFP_KERNEL);
2691#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002692#ifdef CONFIG_TRACING
2693 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2694 sizeof(*mod->trace_bprintk_fmt_start),
2695 &mod->num_trace_bprintk_fmt);
2696 /*
2697 * This section contains pointers to allocated objects in the trace
2698 * code and not scanning it leads to false positives.
2699 */
2700 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2701 sizeof(*mod->trace_bprintk_fmt_start) *
2702 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2703#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002704#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2705 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002706 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002707 sizeof(*mod->ftrace_callsites),
2708 &mod->num_ftrace_callsites);
2709#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002710
Rusty Russell811d66a2010-08-05 12:59:12 -06002711 mod->extable = section_objs(info, "__ex_table",
2712 sizeof(*mod->extable), &mod->num_exentries);
2713
Rusty Russell49668682010-08-05 12:59:10 -06002714 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002715 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2716 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002717
2718 info->debug = section_objs(info, "__verbose",
2719 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002720}
2721
Rusty Russell49668682010-08-05 12:59:10 -06002722static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002723{
2724 int i;
2725 void *ptr;
2726
2727 /* Do the allocs. */
2728 ptr = module_alloc_update_bounds(mod->core_size);
2729 /*
2730 * The pointer to this block is stored in the module structure
2731 * which is inside the block. Just mark it as not being a
2732 * leak.
2733 */
2734 kmemleak_not_leak(ptr);
2735 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002736 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002737
2738 memset(ptr, 0, mod->core_size);
2739 mod->module_core = ptr;
2740
2741 ptr = module_alloc_update_bounds(mod->init_size);
2742 /*
2743 * The pointer to this block is stored in the module structure
2744 * which is inside the block. This block doesn't need to be
2745 * scanned as it contains data and code that will be freed
2746 * after the module is initialized.
2747 */
2748 kmemleak_ignore(ptr);
2749 if (!ptr && mod->init_size) {
2750 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002751 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002752 }
2753 memset(ptr, 0, mod->init_size);
2754 mod->module_init = ptr;
2755
2756 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002757 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002758 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002759 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002760 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002761
Rusty Russell49668682010-08-05 12:59:10 -06002762 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002763 continue;
2764
Rusty Russell49668682010-08-05 12:59:10 -06002765 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002766 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002767 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002768 else
Rusty Russell49668682010-08-05 12:59:10 -06002769 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002770
Rusty Russell49668682010-08-05 12:59:10 -06002771 if (shdr->sh_type != SHT_NOBITS)
2772 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002773 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002774 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002775 pr_debug("\t0x%lx %s\n",
2776 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002777 }
Rusty Russelld9131882010-08-05 12:59:08 -06002778
2779 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002780}
2781
Rusty Russell49668682010-08-05 12:59:10 -06002782static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002783{
2784 /*
2785 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2786 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2787 * using GPL-only symbols it needs.
2788 */
2789 if (strcmp(mod->name, "ndiswrapper") == 0)
2790 add_taint(TAINT_PROPRIETARY_MODULE);
2791
2792 /* driverloader was caught wrongly pretending to be under GPL */
2793 if (strcmp(mod->name, "driverloader") == 0)
2794 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2795
Matthew Garrettc99af372012-06-22 13:49:31 -04002796 /* lve claims to be GPL but upstream won't provide source */
2797 if (strcmp(mod->name, "lve") == 0)
2798 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2799
Rusty Russell22e268e2010-08-05 12:59:05 -06002800#ifdef CONFIG_MODVERSIONS
2801 if ((mod->num_syms && !mod->crcs)
2802 || (mod->num_gpl_syms && !mod->gpl_crcs)
2803 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2804#ifdef CONFIG_UNUSED_SYMBOLS
2805 || (mod->num_unused_syms && !mod->unused_crcs)
2806 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2807#endif
2808 ) {
2809 return try_to_force_load(mod,
2810 "no versions for exported symbols");
2811 }
2812#endif
2813 return 0;
2814}
2815
2816static void flush_module_icache(const struct module *mod)
2817{
2818 mm_segment_t old_fs;
2819
2820 /* flush the icache in correct context */
2821 old_fs = get_fs();
2822 set_fs(KERNEL_DS);
2823
2824 /*
2825 * Flush the instruction cache, since we've played with text.
2826 * Do it before processing of module parameters, so the module
2827 * can provide parameter accessor functions of its own.
2828 */
2829 if (mod->module_init)
2830 flush_icache_range((unsigned long)mod->module_init,
2831 (unsigned long)mod->module_init
2832 + mod->init_size);
2833 flush_icache_range((unsigned long)mod->module_core,
2834 (unsigned long)mod->module_core + mod->core_size);
2835
2836 set_fs(old_fs);
2837}
2838
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002839int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2840 Elf_Shdr *sechdrs,
2841 char *secstrings,
2842 struct module *mod)
2843{
2844 return 0;
2845}
2846
Rusty Russelld9131882010-08-05 12:59:08 -06002847static struct module *layout_and_allocate(struct load_info *info)
2848{
2849 /* Module within temporary copy. */
2850 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002851 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002852 int err;
2853
2854 mod = setup_load_info(info);
2855 if (IS_ERR(mod))
2856 return mod;
2857
Rusty Russell49668682010-08-05 12:59:10 -06002858 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002859 if (err)
2860 return ERR_PTR(err);
2861
2862 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002863 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2864 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002865 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002866 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002867
Rusty Russell49668682010-08-05 12:59:10 -06002868 pcpusec = &info->sechdrs[info->index.pcpu];
2869 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002870 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002871 err = percpu_modalloc(mod,
2872 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002873 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002874 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002875 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002876 }
2877
2878 /* Determine total sizes, and put offsets in sh_entsize. For now
2879 this is done generically; there doesn't appear to be any
2880 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002881 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002882 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002883
2884 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002885 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002886 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302887 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002888
2889 /* Module has been copied to its final place now: return it. */
2890 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002891 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002892 return mod;
2893
Rusty Russelld9131882010-08-05 12:59:08 -06002894free_percpu:
2895 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002896out:
Rusty Russelld9131882010-08-05 12:59:08 -06002897 return ERR_PTR(err);
2898}
2899
2900/* mod is no longer valid after this! */
2901static void module_deallocate(struct module *mod, struct load_info *info)
2902{
Rusty Russelld9131882010-08-05 12:59:08 -06002903 percpu_modfree(mod);
2904 module_free(mod, mod->module_init);
2905 module_free(mod, mod->module_core);
2906}
2907
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002908int __weak module_finalize(const Elf_Ehdr *hdr,
2909 const Elf_Shdr *sechdrs,
2910 struct module *me)
2911{
2912 return 0;
2913}
2914
Rusty Russell811d66a2010-08-05 12:59:12 -06002915static int post_relocation(struct module *mod, const struct load_info *info)
2916{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002917 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002918 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2919
2920 /* Copy relocated percpu area over. */
2921 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2922 info->sechdrs[info->index.pcpu].sh_size);
2923
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002924 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002925 add_kallsyms(mod, info);
2926
2927 /* Arch-specific module finalizing. */
2928 return module_finalize(info->hdr, info->sechdrs, mod);
2929}
2930
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302931/* Is this module of this name done loading? No locks held. */
2932static bool finished_loading(const char *name)
2933{
2934 struct module *mod;
2935 bool ret;
2936
2937 mutex_lock(&module_mutex);
2938 mod = find_module(name);
2939 ret = !mod || mod->state != MODULE_STATE_COMING;
2940 mutex_unlock(&module_mutex);
2941
2942 return ret;
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945/* Allocate and load the module: note that size of section 0 is always
2946 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002947static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 unsigned long len,
2949 const char __user *uargs)
2950{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002951 struct load_info info = { NULL, };
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302952 struct module *mod, *old;
Rusty Russell40dd2562010-08-05 12:59:03 -06002953 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002954
Jim Cromie5e124162011-12-06 12:11:31 -07002955 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Rusty Russelld9131882010-08-05 12:59:08 -06002958 /* Copy in the blobs from userspace, check they are vaguely sane. */
2959 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002960 if (err)
2961 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Rusty Russelld9131882010-08-05 12:59:08 -06002963 /* Figure out module layout, and allocate all the memory. */
2964 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002965 if (IS_ERR(mod)) {
2966 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002967 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Rusty Russell106a4ee2012-09-26 10:09:40 +01002970#ifdef CONFIG_MODULE_SIG
2971 mod->sig_ok = info.sig_ok;
2972 if (!mod->sig_ok)
2973 add_taint_module(mod, TAINT_FORCED_MODULE);
2974#endif
2975
Rusty Russell49668682010-08-05 12:59:10 -06002976 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002977 err = module_unload_init(mod);
2978 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002979 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Rusty Russell22e268e2010-08-05 12:59:05 -06002981 /* Now we've got everything in the final locations, we can
2982 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002983 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Rusty Russell49668682010-08-05 12:59:10 -06002985 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002986 if (err)
2987 goto free_unload;
Dave Jones9841d612006-01-08 01:03:41 -08002988
Matt Domschc988d2b2005-06-23 22:05:15 -07002989 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002990 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002993 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002995 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Rusty Russell49668682010-08-05 12:59:10 -06002997 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002998 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002999 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Rusty Russell811d66a2010-08-05 12:59:12 -06003001 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06003003 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Rusty Russell22e268e2010-08-05 12:59:05 -06003005 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07003006
Rusty Russell6526c532010-08-05 12:59:10 -06003007 /* Now copy in args */
3008 mod->args = strndup_user(uargs, ~0UL >> 1);
3009 if (IS_ERR(mod->args)) {
3010 err = PTR_ERR(mod->args);
3011 goto free_arch_cleanup;
3012 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08003013
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003014 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06003015 mod->state = MODULE_STATE_COMING;
3016
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003017 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003018 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02003019 * strong_try_module_get() will fail.
3020 * lockdep/oops can run asynchronous, so use the RCU list insertion
3021 * function to insert in a way safe to concurrent readers.
3022 * The mutex protects against concurrent writers.
3023 */
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303024again:
Rusty Russell75676502010-06-05 11:17:36 -06003025 mutex_lock(&module_mutex);
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303026 if ((old = find_module(mod->name)) != NULL) {
3027 if (old->state == MODULE_STATE_COMING) {
3028 /* Wait in case it fails to load. */
3029 mutex_unlock(&module_mutex);
3030 err = wait_event_interruptible(module_wq,
3031 finished_loading(mod->name));
3032 if (err)
3033 goto free_arch_cleanup;
3034 goto again;
3035 }
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003036 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06003037 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003038 }
3039
Rusty Russell811d66a2010-08-05 12:59:12 -06003040 /* This has to be done once we're sure module name is unique. */
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003041 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003042
Rusty Russellbe593f42010-06-05 11:17:37 -06003043 /* Find duplicate symbols */
3044 err = verify_export_symbols(mod);
3045 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10003046 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06003047
Linus Torvalds53363772010-10-05 11:29:27 -07003048 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02003049 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06003050 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003051
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003052 /* Module is ready to execute: parsing args may do that. */
Pawel Moll026cee02012-03-26 12:50:51 +10303053 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Jim Cromieb48420c2012-04-27 14:30:35 -06003054 -32768, 32767, &ddebug_dyndbg_module_param_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003056 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003058 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06003059 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003061 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06003062
Kevin Cernekee48fd1182012-01-13 09:32:14 +10303063 /* Get rid of temporary copy. */
Rusty Russelld9131882010-08-05 12:59:08 -06003064 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003067 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return mod;
3069
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003070 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06003071 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003072 /* Unlink carefully: kallsyms could be walking list. */
3073 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07003074 module_bug_cleanup(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303075 wake_up_all(&module_wq);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003076 ddebug:
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003077 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06003078 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06003079 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003080 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06003081 kfree(mod->args);
3082 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003084 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06003085 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06003086 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003088 free_module:
3089 module_deallocate(mod, &info);
3090 free_copy:
3091 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08003092 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003095/* Call module constructors. */
3096static void do_mod_ctors(struct module *mod)
3097{
3098#ifdef CONFIG_CONSTRUCTORS
3099 unsigned long i;
3100
3101 for (i = 0; i < mod->num_ctors; i++)
3102 mod->ctors[i]();
3103#endif
3104}
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003107SYSCALL_DEFINE3(init_module, void __user *, umod,
3108 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct module *mod;
3111 int ret = 0;
3112
3113 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07003114 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return -EPERM;
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* Do all the hard work */
3118 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06003119 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Alan Sterne041c682006-03-27 01:16:30 -08003122 blocking_notifier_call_chain(&module_notify_list,
3123 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Steven Rostedt94462ad2010-11-29 13:15:42 -05003125 /* Set RO and NX regions for core */
3126 set_section_ro_nx(mod->module_core,
3127 mod->core_text_size,
3128 mod->core_ro_size,
3129 mod->core_size);
3130
3131 /* Set RO and NX regions for init */
3132 set_section_ro_nx(mod->module_init,
3133 mod->init_text_size,
3134 mod->init_ro_size,
3135 mod->init_size);
3136
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003137 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 /* Start the module */
3139 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003140 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (ret < 0) {
3142 /* Init routine failed: abort. Try to protect us from
3143 buggy refcounters. */
3144 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003145 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003146 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003147 blocking_notifier_call_chain(&module_notify_list,
3148 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003149 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303150 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return ret;
3152 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003153 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003154 printk(KERN_WARNING
3155"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3156"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003157 __func__, mod->name, ret,
3158 __func__);
3159 dump_stack();
3160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Rusty Russell6f139092012-09-28 14:31:03 +09303162 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08003164 blocking_notifier_call_chain(&module_notify_list,
3165 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003166
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003167 /* We need to finish all async code before the module init sequence is done */
3168 async_synchronize_full();
3169
Rusty Russell6c5db222008-03-10 11:43:52 -07003170 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /* Drop initial reference. */
3172 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003173 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003174#ifdef CONFIG_KALLSYMS
3175 mod->num_symtab = mod->core_num_syms;
3176 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003177 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003178#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003179 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 module_free(mod, mod->module_init);
3181 mod->module_init = NULL;
3182 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003183 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003185 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303186 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 return 0;
3189}
3190
3191static inline int within(unsigned long addr, void *start, unsigned long size)
3192{
3193 return ((void *)addr >= start && (void *)addr < start + size);
3194}
3195
3196#ifdef CONFIG_KALLSYMS
3197/*
3198 * This ignores the intensely annoying "mapping symbols" found
3199 * in ARM ELF files: $a, $t and $d.
3200 */
3201static inline int is_arm_mapping_symbol(const char *str)
3202{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003203 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 && (str[2] == '\0' || str[2] == '.');
3205}
3206
3207static const char *get_ksymbol(struct module *mod,
3208 unsigned long addr,
3209 unsigned long *size,
3210 unsigned long *offset)
3211{
3212 unsigned int i, best = 0;
3213 unsigned long nextval;
3214
3215 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003216 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003218 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3220
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003221 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003222 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 for (i = 1; i < mod->num_symtab; i++) {
3224 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3225 continue;
3226
3227 /* We ignore unnamed symbols: they're uninformative
3228 * and inserted at a whim. */
3229 if (mod->symtab[i].st_value <= addr
3230 && mod->symtab[i].st_value > mod->symtab[best].st_value
3231 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3232 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3233 best = i;
3234 if (mod->symtab[i].st_value > addr
3235 && mod->symtab[i].st_value < nextval
3236 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3237 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3238 nextval = mod->symtab[i].st_value;
3239 }
3240
3241 if (!best)
3242 return NULL;
3243
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003244 if (size)
3245 *size = nextval - mod->symtab[best].st_value;
3246 if (offset)
3247 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return mod->strtab + mod->symtab[best].st_name;
3249}
3250
Rusty Russell6dd06c92008-01-29 17:13:22 -05003251/* For kallsyms to ask for address resolution. NULL means not found. Careful
3252 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003253const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003254 unsigned long *size,
3255 unsigned long *offset,
3256 char **modname,
3257 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003260 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Rusty Russellcb2a5202008-01-14 00:55:03 -08003262 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003263 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003264 if (within_module_init(addr, mod) ||
3265 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003266 if (modname)
3267 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003268 ret = get_ksymbol(mod, addr, size, offset);
3269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
3271 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003272 /* Make a copy in here where it's safe */
3273 if (ret) {
3274 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3275 ret = namebuf;
3276 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003277 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003281int lookup_module_symbol_name(unsigned long addr, char *symname)
3282{
3283 struct module *mod;
3284
Rusty Russellcb2a5202008-01-14 00:55:03 -08003285 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003286 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003287 if (within_module_init(addr, mod) ||
3288 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003289 const char *sym;
3290
3291 sym = get_ksymbol(mod, addr, NULL, NULL);
3292 if (!sym)
3293 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003294 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003295 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003296 return 0;
3297 }
3298 }
3299out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003300 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003301 return -ERANGE;
3302}
3303
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003304int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3305 unsigned long *offset, char *modname, char *name)
3306{
3307 struct module *mod;
3308
Rusty Russellcb2a5202008-01-14 00:55:03 -08003309 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003310 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003311 if (within_module_init(addr, mod) ||
3312 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003313 const char *sym;
3314
3315 sym = get_ksymbol(mod, addr, size, offset);
3316 if (!sym)
3317 goto out;
3318 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003319 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003320 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003321 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003322 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003323 return 0;
3324 }
3325 }
3326out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003327 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003328 return -ERANGE;
3329}
3330
Alexey Dobriyanea078902007-05-08 00:28:39 -07003331int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3332 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 struct module *mod;
3335
Rusty Russellcb2a5202008-01-14 00:55:03 -08003336 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003337 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (symnum < mod->num_symtab) {
3339 *value = mod->symtab[symnum].st_value;
3340 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003341 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003342 KSYM_NAME_LEN);
3343 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003344 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003345 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348 symnum -= mod->num_symtab;
3349 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003350 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003351 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354static unsigned long mod_find_symname(struct module *mod, const char *name)
3355{
3356 unsigned int i;
3357
3358 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003359 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3360 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return mod->symtab[i].st_value;
3362 return 0;
3363}
3364
3365/* Look for this name: can be of form module:name. */
3366unsigned long module_kallsyms_lookup_name(const char *name)
3367{
3368 struct module *mod;
3369 char *colon;
3370 unsigned long ret = 0;
3371
3372 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003373 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if ((colon = strchr(name, ':')) != NULL) {
3375 *colon = '\0';
3376 if ((mod = find_module(name)) != NULL)
3377 ret = mod_find_symname(mod, colon+1);
3378 *colon = ':';
3379 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003380 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if ((ret = mod_find_symname(mod, name)) != 0)
3382 break;
3383 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003384 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return ret;
3386}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003387
3388int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3389 struct module *, unsigned long),
3390 void *data)
3391{
3392 struct module *mod;
3393 unsigned int i;
3394 int ret;
3395
3396 list_for_each_entry(mod, &modules, list) {
3397 for (i = 0; i < mod->num_symtab; i++) {
3398 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3399 mod, mod->symtab[i].st_value);
3400 if (ret != 0)
3401 return ret;
3402 }
3403 }
3404 return 0;
3405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406#endif /* CONFIG_KALLSYMS */
3407
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003408static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003409{
3410 int bx = 0;
3411
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003412 if (mod->taints ||
3413 mod->state == MODULE_STATE_GOING ||
3414 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003415 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303416 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003417 /* Show a - for module-is-being-unloaded */
3418 if (mod->state == MODULE_STATE_GOING)
3419 buf[bx++] = '-';
3420 /* Show a + for module-is-being-loaded */
3421 if (mod->state == MODULE_STATE_COMING)
3422 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003423 buf[bx++] = ')';
3424 }
3425 buf[bx] = '\0';
3426
3427 return buf;
3428}
3429
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003430#ifdef CONFIG_PROC_FS
3431/* Called by the /proc file system to return a list of modules. */
3432static void *m_start(struct seq_file *m, loff_t *pos)
3433{
3434 mutex_lock(&module_mutex);
3435 return seq_list_start(&modules, *pos);
3436}
3437
3438static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3439{
3440 return seq_list_next(p, &modules, pos);
3441}
3442
3443static void m_stop(struct seq_file *m, void *p)
3444{
3445 mutex_unlock(&module_mutex);
3446}
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448static int m_show(struct seq_file *m, void *p)
3449{
3450 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003451 char buf[8];
3452
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003453 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 mod->name, mod->init_size + mod->core_size);
3455 print_unload_info(m, mod);
3456
3457 /* Informative for users. */
3458 seq_printf(m, " %s",
3459 mod->state == MODULE_STATE_GOING ? "Unloading":
3460 mod->state == MODULE_STATE_COMING ? "Loading":
3461 "Live");
3462 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003463 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003465 /* Taints info */
3466 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003467 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 seq_printf(m, "\n");
3470 return 0;
3471}
3472
3473/* Format: modulename size refcount deps address
3474
3475 Where refcount is a number or -, and deps is a comma-separated list
3476 of depends or -.
3477*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003478static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 .start = m_start,
3480 .next = m_next,
3481 .stop = m_stop,
3482 .show = m_show
3483};
3484
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003485static int modules_open(struct inode *inode, struct file *file)
3486{
3487 return seq_open(file, &modules_op);
3488}
3489
3490static const struct file_operations proc_modules_operations = {
3491 .open = modules_open,
3492 .read = seq_read,
3493 .llseek = seq_lseek,
3494 .release = seq_release,
3495};
3496
3497static int __init proc_modules_init(void)
3498{
3499 proc_create("modules", 0, NULL, &proc_modules_operations);
3500 return 0;
3501}
3502module_init(proc_modules_init);
3503#endif
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505/* Given an address, look for it in the module exception tables. */
3506const struct exception_table_entry *search_module_extables(unsigned long addr)
3507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 const struct exception_table_entry *e = NULL;
3509 struct module *mod;
3510
Rusty Russell24da1cb2007-07-15 23:41:46 -07003511 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003512 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (mod->num_exentries == 0)
3514 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 e = search_extable(mod->extable,
3517 mod->extable + mod->num_exentries - 1,
3518 addr);
3519 if (e)
3520 break;
3521 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003522 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003525 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return e;
3527}
3528
Ingo Molnar4d435f92006-07-03 00:24:24 -07003529/*
Rusty Russelle6104992009-03-31 13:05:31 -06003530 * is_module_address - is this address inside a module?
3531 * @addr: the address to check.
3532 *
3533 * See is_module_text_address() if you simply want to see if the address
3534 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003535 */
Rusty Russelle6104992009-03-31 13:05:31 -06003536bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003537{
Rusty Russelle6104992009-03-31 13:05:31 -06003538 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003539
Rusty Russell24da1cb2007-07-15 23:41:46 -07003540 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003541 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003542 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003543
Rusty Russelle6104992009-03-31 13:05:31 -06003544 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003545}
3546
Rusty Russelle6104992009-03-31 13:05:31 -06003547/*
3548 * __module_address - get the module which contains an address.
3549 * @addr: the address.
3550 *
3551 * Must be called with preempt disabled or module mutex held so that
3552 * module doesn't get freed during this.
3553 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003554struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
3556 struct module *mod;
3557
Rusty Russell3a642e92008-07-22 19:24:28 -05003558 if (addr < module_addr_min || addr > module_addr_max)
3559 return NULL;
3560
Andi Kleend72b3752008-08-30 10:09:00 +02003561 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003562 if (within_module_core(addr, mod)
3563 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return mod;
3565 return NULL;
3566}
Tim Abbottc6b37802008-12-05 19:03:59 -05003567EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Rusty Russelle6104992009-03-31 13:05:31 -06003569/*
3570 * is_module_text_address - is this address inside module code?
3571 * @addr: the address to check.
3572 *
3573 * See is_module_address() if you simply want to see if the address is
3574 * anywhere in a module. See kernel_text_address() for testing if an
3575 * address corresponds to kernel or module code.
3576 */
3577bool is_module_text_address(unsigned long addr)
3578{
3579 bool ret;
3580
3581 preempt_disable();
3582 ret = __module_text_address(addr) != NULL;
3583 preempt_enable();
3584
3585 return ret;
3586}
3587
3588/*
3589 * __module_text_address - get the module whose code contains an address.
3590 * @addr: the address.
3591 *
3592 * Must be called with preempt disabled or module mutex held so that
3593 * module doesn't get freed during this.
3594 */
3595struct module *__module_text_address(unsigned long addr)
3596{
3597 struct module *mod = __module_address(addr);
3598 if (mod) {
3599 /* Make sure it's within the text section. */
3600 if (!within(addr, mod->module_init, mod->init_text_size)
3601 && !within(addr, mod->module_core, mod->core_text_size))
3602 mod = NULL;
3603 }
3604 return mod;
3605}
Tim Abbottc6b37802008-12-05 19:03:59 -05003606EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608/* Don't grab lock, we're oopsing. */
3609void print_modules(void)
3610{
3611 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003612 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Linus Torvaldsb2311252009-06-16 11:07:14 -07003614 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003615 /* Most callers should already have preempt disabled, but make sure */
3616 preempt_disable();
3617 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003618 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003619 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003620 if (last_unloaded_module[0])
3621 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 printk("\n");
3623}
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003626/* Generate the signature for all relevant module structures here.
3627 * If these change, we don't want to try to parse the module. */
3628void module_layout(struct module *mod,
3629 struct modversion_info *ver,
3630 struct kernel_param *kp,
3631 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003632 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003633{
3634}
3635EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636#endif