blob: 6085f5ef88eaf1fd95159f3db724b16cf43b54d3 [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);
375 printk(KERN_WARNING "Please see the file "
376 "Documentation/feature-removal-schedule.txt "
377 "in the kernel source tree for more details.\n");
378 }
379 }
380
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500381#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500382 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500383 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500384 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500385 printk(KERN_WARNING
386 "This symbol will go away in the future.\n");
387 printk(KERN_WARNING
388 "Please evalute if this is the right api to use and if "
389 "it really is, submit a report the linux kernel "
390 "mailinglist together with submitting your code for "
391 "inclusion.\n");
392 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500393#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500394
395 fsa->owner = owner;
396 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500397 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500398 return true;
399}
400
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200401static int cmp_name(const void *va, const void *vb)
402{
403 const char *a;
404 const struct kernel_symbol *b;
405 a = va; b = vb;
406 return strcmp(a, b->name);
407}
408
Rusty Russellde4d8d52011-04-19 21:49:58 +0200409static bool find_symbol_in_section(const struct symsearch *syms,
410 struct module *owner,
411 void *data)
412{
413 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200414 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200415
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200416 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
417 sizeof(struct kernel_symbol), cmp_name);
418
419 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
420 return true;
421
Rusty Russellde4d8d52011-04-19 21:49:58 +0200422 return false;
423}
424
Tim Abbott414fd312008-12-05 19:03:56 -0500425/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600426 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500427const struct kernel_symbol *find_symbol(const char *name,
428 struct module **owner,
429 const unsigned long **crc,
430 bool gplok,
431 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Rusty Russelldafd0942008-07-22 19:24:25 -0500433 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Rusty Russelldafd0942008-07-22 19:24:25 -0500435 fsa.name = name;
436 fsa.gplok = gplok;
437 fsa.warn = warn;
438
Rusty Russellde4d8d52011-04-19 21:49:58 +0200439 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500440 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500441 *owner = fsa.owner;
442 if (crc)
443 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500444 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500446
Jim Cromie5e124162011-12-06 12:11:31 -0700447 pr_debug("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500448 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
Tim Abbottc6b37802008-12-05 19:03:59 -0500450EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500453struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct module *mod;
456
457 list_for_each_entry(mod, &modules, list) {
458 if (strcmp(mod->name, name) == 0)
459 return mod;
460 }
461 return NULL;
462}
Tim Abbottc6b37802008-12-05 19:03:59 -0500463EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900466
Tejun Heo259354d2010-03-10 18:56:10 +0900467static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900468{
Tejun Heo259354d2010-03-10 18:56:10 +0900469 return mod->percpu;
470}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900471
Tejun Heo259354d2010-03-10 18:56:10 +0900472static int percpu_modalloc(struct module *mod,
473 unsigned long size, unsigned long align)
474{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900475 if (align > PAGE_SIZE) {
476 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900477 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900478 align = PAGE_SIZE;
479 }
480
Tejun Heo259354d2010-03-10 18:56:10 +0900481 mod->percpu = __alloc_reserved_percpu(size, align);
482 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900483 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600484 "%s: Could not allocate %lu bytes percpu data\n",
485 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900486 return -ENOMEM;
487 }
488 mod->percpu_size = size;
489 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900490}
491
Tejun Heo259354d2010-03-10 18:56:10 +0900492static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900493{
Tejun Heo259354d2010-03-10 18:56:10 +0900494 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900495}
496
Rusty Russell49668682010-08-05 12:59:10 -0600497static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900498{
Rusty Russell49668682010-08-05 12:59:10 -0600499 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900500}
501
Tejun Heo259354d2010-03-10 18:56:10 +0900502static void percpu_modcopy(struct module *mod,
503 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900504{
505 int cpu;
506
507 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900508 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900509}
510
Tejun Heo10fad5e2010-03-10 18:57:54 +0900511/**
512 * is_module_percpu_address - test whether address is from module static percpu
513 * @addr: address to test
514 *
515 * Test whether @addr belongs to module static percpu area.
516 *
517 * RETURNS:
518 * %true if @addr is from module static percpu area
519 */
520bool is_module_percpu_address(unsigned long addr)
521{
522 struct module *mod;
523 unsigned int cpu;
524
525 preempt_disable();
526
527 list_for_each_entry_rcu(mod, &modules, list) {
528 if (!mod->percpu_size)
529 continue;
530 for_each_possible_cpu(cpu) {
531 void *start = per_cpu_ptr(mod->percpu, cpu);
532
533 if ((void *)addr >= start &&
534 (void *)addr < start + mod->percpu_size) {
535 preempt_enable();
536 return true;
537 }
538 }
539 }
540
541 preempt_enable();
542 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900546
Tejun Heo259354d2010-03-10 18:56:10 +0900547static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 return NULL;
550}
Tejun Heo259354d2010-03-10 18:56:10 +0900551static inline int percpu_modalloc(struct module *mod,
552 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Tejun Heo259354d2010-03-10 18:56:10 +0900554 return -ENOMEM;
555}
556static inline void percpu_modfree(struct module *mod)
557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Rusty Russell49668682010-08-05 12:59:10 -0600559static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 return 0;
562}
Tejun Heo259354d2010-03-10 18:56:10 +0900563static inline void percpu_modcopy(struct module *mod,
564 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 /* pcpusec should be 0, and size of that section should be 0. */
567 BUG_ON(size != 0);
568}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900569bool is_module_percpu_address(unsigned long addr)
570{
571 return false;
572}
Tejun Heo6b588c12009-02-20 16:29:07 +0900573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif /* CONFIG_SMP */
575
Matt Domschc988d2b2005-06-23 22:05:15 -0700576#define MODINFO_ATTR(field) \
577static void setup_modinfo_##field(struct module *mod, const char *s) \
578{ \
579 mod->field = kstrdup(s, GFP_KERNEL); \
580} \
581static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930582 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700583{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930584 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700585} \
586static int modinfo_##field##_exists(struct module *mod) \
587{ \
588 return mod->field != NULL; \
589} \
590static void free_modinfo_##field(struct module *mod) \
591{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700592 kfree(mod->field); \
593 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700594} \
595static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900596 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700597 .show = show_modinfo_##field, \
598 .setup = setup_modinfo_##field, \
599 .test = modinfo_##field##_exists, \
600 .free = free_modinfo_##field, \
601};
602
603MODINFO_ATTR(version);
604MODINFO_ATTR(srcversion);
605
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100606static char last_unloaded_module[MODULE_NAME_LEN+1];
607
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800608#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400609
610EXPORT_TRACEPOINT_SYMBOL(module_get);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600613static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600615 mod->refptr = alloc_percpu(struct module_ref);
616 if (!mod->refptr)
617 return -ENOMEM;
618
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700619 INIT_LIST_HEAD(&mod->source_list);
620 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100623 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Backwards compatibility macros put refcount during init. */
625 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600626
627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* Does a already use b? */
631static int already_uses(struct module *a, struct module *b)
632{
633 struct module_use *use;
634
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700635 list_for_each_entry(use, &b->source_list, source_list) {
636 if (use->source == a) {
Jim Cromie5e124162011-12-06 12:11:31 -0700637 pr_debug("%s uses %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 1;
639 }
640 }
Jim Cromie5e124162011-12-06 12:11:31 -0700641 pr_debug("%s does not use %s!\n", a->name, b->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643}
644
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700645/*
646 * Module a uses b
647 * - we add 'a' as a "source", 'b' as a "target" of module use
648 * - the module_use is added to the list of 'b' sources (so
649 * 'b' can walk the list to see who sourced them), and of 'a'
650 * targets (so 'a' can see what modules it targets).
651 */
652static int add_module_usage(struct module *a, struct module *b)
653{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700654 struct module_use *use;
655
Jim Cromie5e124162011-12-06 12:11:31 -0700656 pr_debug("Allocating new usage for %s.\n", a->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700657 use = kmalloc(sizeof(*use), GFP_ATOMIC);
658 if (!use) {
659 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
660 return -ENOMEM;
661 }
662
663 use->source = a;
664 use->target = b;
665 list_add(&use->source_list, &b->source_list);
666 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700667 return 0;
668}
669
Rusty Russell75676502010-06-05 11:17:36 -0600670/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600671int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600673 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100674
Rusty Russell9bea7f22010-06-05 11:17:37 -0600675 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700676 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700677
Rusty Russell9bea7f22010-06-05 11:17:37 -0600678 /* If module isn't available, we fail. */
679 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500680 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700683 err = add_module_usage(a, b);
684 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600690EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/* Clear the unload stuff of the module. */
693static void module_unload_free(struct module *mod)
694{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700695 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rusty Russell75676502010-06-05 11:17:36 -0600697 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700698 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
699 struct module *i = use->target;
Jim Cromie5e124162011-12-06 12:11:31 -0700700 pr_debug("%s unusing %s\n", mod->name, i->name);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700701 module_put(i);
702 list_del(&use->source_list);
703 list_del(&use->target_list);
704 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Rusty Russell75676502010-06-05 11:17:36 -0600706 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600707
708 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800712static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int ret = (flags & O_TRUNC);
715 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800716 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return ret;
718}
719#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800720static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 return 0;
723}
724#endif /* CONFIG_MODULE_FORCE_UNLOAD */
725
726struct stopref
727{
728 struct module *mod;
729 int flags;
730 int *forced;
731};
732
733/* Whole machine is stopped with interrupts off when this runs. */
734static int __try_stop_module(void *_sref)
735{
736 struct stopref *sref = _sref;
737
Rusty Russellda39ba52008-07-22 19:24:25 -0500738 /* If it's not unused, quit unless we're forcing. */
739 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800740 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EWOULDBLOCK;
742 }
743
744 /* Mark it as dying. */
745 sref->mod->state = MODULE_STATE_GOING;
746 return 0;
747}
748
749static int try_stop_module(struct module *mod, int flags, int *forced)
750{
Rusty Russellda39ba52008-07-22 19:24:25 -0500751 if (flags & O_NONBLOCK) {
752 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500754 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500755 } else {
756 /* We don't need to stop the machine for this. */
757 mod->state = MODULE_STATE_GOING;
758 synchronize_sched();
759 return 0;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Eric Dumazetbd77c042012-01-13 09:32:14 +1030763unsigned long module_refcount(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030765 unsigned long incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030766 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Eric Dumazet720eba32009-02-03 13:31:36 +1030768 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100769 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
770 /*
771 * ensure the incs are added up after the decs.
772 * module_put ensures incs are visible before decs with smp_wmb.
773 *
774 * This 2-count scheme avoids the situation where the refcount
775 * for CPU0 is read, then CPU0 increments the module refcount,
776 * then CPU1 drops that refcount, then the refcount for CPU1 is
777 * read. We would record a decrement but not its corresponding
778 * increment so we would see a low count (disaster).
779 *
780 * Rare situation? But module_refcount can be preempted, and we
781 * might be tallying up 4096+ CPUs. So it is not impossible.
782 */
783 smp_rmb();
784 for_each_possible_cpu(cpu)
785 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
786 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788EXPORT_SYMBOL(module_refcount);
789
790/* This exists whether we can unload or not */
791static void free_module(struct module *mod);
792
793static void wait_for_zero_refcount(struct module *mod)
794{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500795 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800796 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 for (;;) {
Jim Cromie5e124162011-12-06 12:11:31 -0700798 pr_debug("Looking at refcount...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 set_current_state(TASK_UNINTERRUPTIBLE);
800 if (module_refcount(mod) == 0)
801 break;
802 schedule();
803 }
804 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800805 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100808SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
809 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800812 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int ret, forced = 0;
814
Kees Cook3d433212009-04-02 15:49:29 -0700815 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800816 return -EPERM;
817
818 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
819 return -EFAULT;
820 name[MODULE_NAME_LEN-1] = '\0';
821
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200822 if (mutex_lock_interruptible(&module_mutex) != 0)
823 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 mod = find_module(name);
826 if (!mod) {
827 ret = -ENOENT;
828 goto out;
829 }
830
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700831 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Other modules depend on us: get rid of them first. */
833 ret = -EWOULDBLOCK;
834 goto out;
835 }
836
837 /* Doing init or already dying? */
838 if (mod->state != MODULE_STATE_LIVE) {
839 /* FIXME: if (force), slam module count and wake up
840 waiter --RR */
Jim Cromie5e124162011-12-06 12:11:31 -0700841 pr_debug("%s already dying\n", mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 ret = -EBUSY;
843 goto out;
844 }
845
846 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700847 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800848 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!forced) {
850 /* This module can't be removed */
851 ret = -EBUSY;
852 goto out;
853 }
854 }
855
856 /* Set this up before setting mod->state */
857 mod->waiter = current;
858
859 /* Stop the machine so refcounts can't move and disable module. */
860 ret = try_stop_module(mod, flags, &forced);
861 if (ret != 0)
862 goto out;
863
864 /* Never wait if forced. */
865 if (!forced && module_refcount(mod) != 0)
866 wait_for_zero_refcount(mod);
867
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200868 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300869 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200870 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200872 blocking_notifier_call_chain(&module_notify_list,
873 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800874 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600875
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100876 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500877 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Rusty Russell75676502010-06-05 11:17:36 -0600879 free_module(mod);
880 return 0;
881out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800882 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
884}
885
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800886static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct module_use *use;
889 int printed_something = 0;
890
Eric Dumazetbd77c042012-01-13 09:32:14 +1030891 seq_printf(m, " %lu ", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Always include a trailing , so userspace can differentiate
894 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700895 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700897 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (mod->init != NULL && mod->exit == NULL) {
901 printed_something = 1;
902 seq_printf(m, "[permanent],");
903 }
904
905 if (!printed_something)
906 seq_printf(m, "-");
907}
908
909void __symbol_put(const char *symbol)
910{
911 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Rusty Russell24da1cb2007-07-15 23:41:46 -0700913 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500914 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 BUG();
916 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700917 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919EXPORT_SYMBOL(__symbol_put);
920
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930921/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922void symbol_put_addr(void *addr)
923{
Trent Piepho5e376612006-05-15 09:44:06 -0700924 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930925 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930927 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700928 return;
929
Rusty Russella6e6abd2009-03-31 13:05:31 -0600930 /* module_text_address is safe here: we're supposed to have reference
931 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930932 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600933 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700934 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936EXPORT_SYMBOL_GPL(symbol_put_addr);
937
938static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930939 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Eric Dumazetbd77c042012-01-13 09:32:14 +1030941 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Kay Sieverscca3e702012-01-13 09:32:15 +1030944static struct module_attribute modinfo_refcnt =
945 __ATTR(refcnt, 0444, show_refcnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Steven Rostedtd53799b2012-03-26 12:50:52 +1030947void __module_get(struct module *module)
948{
949 if (module) {
950 preempt_disable();
951 __this_cpu_inc(module->refptr->incs);
952 trace_module_get(module, _RET_IP_);
953 preempt_enable();
954 }
955}
956EXPORT_SYMBOL(__module_get);
957
958bool try_module_get(struct module *module)
959{
960 bool ret = true;
961
962 if (module) {
963 preempt_disable();
964
965 if (likely(module_is_live(module))) {
966 __this_cpu_inc(module->refptr->incs);
967 trace_module_get(module, _RET_IP_);
968 } else
969 ret = false;
970
971 preempt_enable();
972 }
973 return ret;
974}
975EXPORT_SYMBOL(try_module_get);
976
Al Virof6a57032006-10-18 01:47:25 -0400977void module_put(struct module *module)
978{
979 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900980 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100981 smp_wmb(); /* see comment in module_refcount */
982 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900983
Li Zefanae832d12010-03-24 10:57:43 +0800984 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400985 /* Maybe they're waiting for us to drop reference? */
986 if (unlikely(!module_is_live(module)))
987 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900988 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400989 }
990}
991EXPORT_SYMBOL(module_put);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800994static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 /* We don't know the usage count, or what modules are using. */
997 seq_printf(m, " - -");
998}
999
1000static inline void module_unload_free(struct module *mod)
1001{
1002}
1003
Rusty Russell9bea7f22010-06-05 11:17:37 -06001004int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Rusty Russell9bea7f22010-06-05 11:17:37 -06001006 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
Rusty Russell9bea7f22010-06-05 11:17:37 -06001008EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001010static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014#endif /* CONFIG_MODULE_UNLOAD */
1015
Kevin Winchester53999bf2012-01-15 19:32:55 -04001016static size_t module_flags_taint(struct module *mod, char *buf)
1017{
1018 size_t l = 0;
1019
1020 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1021 buf[l++] = 'P';
1022 if (mod->taints & (1 << TAINT_OOT_MODULE))
1023 buf[l++] = 'O';
1024 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1025 buf[l++] = 'F';
1026 if (mod->taints & (1 << TAINT_CRAP))
1027 buf[l++] = 'C';
1028 /*
1029 * TAINT_FORCED_RMMOD: could be added.
1030 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1031 * apply to modules.
1032 */
1033 return l;
1034}
1035
Kay Sievers1f717402006-11-24 12:15:25 +01001036static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301037 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +01001038{
1039 const char *state = "unknown";
1040
Kay Sievers4befb022011-07-24 22:06:04 +09301041 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +01001042 case MODULE_STATE_LIVE:
1043 state = "live";
1044 break;
1045 case MODULE_STATE_COMING:
1046 state = "coming";
1047 break;
1048 case MODULE_STATE_GOING:
1049 state = "going";
1050 break;
1051 }
1052 return sprintf(buffer, "%s\n", state);
1053}
1054
Kay Sieverscca3e702012-01-13 09:32:15 +10301055static struct module_attribute modinfo_initstate =
1056 __ATTR(initstate, 0444, show_initstate, NULL);
Kay Sievers1f717402006-11-24 12:15:25 +01001057
Kay Sievers88bfa322011-07-24 22:06:04 +09301058static ssize_t store_uevent(struct module_attribute *mattr,
1059 struct module_kobject *mk,
1060 const char *buffer, size_t count)
1061{
1062 enum kobject_action action;
1063
1064 if (kobject_action_type(buffer, count, &action) == 0)
1065 kobject_uevent(&mk->kobj, action);
1066 return count;
1067}
1068
Kay Sieverscca3e702012-01-13 09:32:15 +10301069struct module_attribute module_uevent =
1070 __ATTR(uevent, 0200, NULL, store_uevent);
1071
1072static ssize_t show_coresize(struct module_attribute *mattr,
1073 struct module_kobject *mk, char *buffer)
1074{
1075 return sprintf(buffer, "%u\n", mk->mod->core_size);
1076}
1077
1078static struct module_attribute modinfo_coresize =
1079 __ATTR(coresize, 0444, show_coresize, NULL);
1080
1081static ssize_t show_initsize(struct module_attribute *mattr,
1082 struct module_kobject *mk, char *buffer)
1083{
1084 return sprintf(buffer, "%u\n", mk->mod->init_size);
1085}
1086
1087static struct module_attribute modinfo_initsize =
1088 __ATTR(initsize, 0444, show_initsize, NULL);
1089
1090static ssize_t show_taint(struct module_attribute *mattr,
1091 struct module_kobject *mk, char *buffer)
1092{
1093 size_t l;
1094
1095 l = module_flags_taint(mk->mod, buffer);
1096 buffer[l++] = '\n';
1097 return l;
1098}
1099
1100static struct module_attribute modinfo_taint =
1101 __ATTR(taint, 0444, show_taint, NULL);
Kay Sievers88bfa322011-07-24 22:06:04 +09301102
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001103static struct module_attribute *modinfo_attrs[] = {
Kay Sieverscca3e702012-01-13 09:32:15 +10301104 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001105 &modinfo_version,
1106 &modinfo_srcversion,
Kay Sieverscca3e702012-01-13 09:32:15 +10301107 &modinfo_initstate,
1108 &modinfo_coresize,
1109 &modinfo_initsize,
1110 &modinfo_taint,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001111#ifdef CONFIG_MODULE_UNLOAD
Kay Sieverscca3e702012-01-13 09:32:15 +10301112 &modinfo_refcnt,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001113#endif
1114 NULL,
1115};
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117static const char vermagic[] = VERMAGIC_STRING;
1118
Rusty Russellc6e665c2009-03-31 13:05:33 -06001119static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001120{
1121#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001122 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001123 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1124 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001125 add_taint_module(mod, TAINT_FORCED_MODULE);
1126 return 0;
1127#else
1128 return -ENOEXEC;
1129#endif
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001133/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1134static unsigned long maybe_relocated(unsigned long crc,
1135 const struct module *crc_owner)
1136{
1137#ifdef ARCH_RELOCATES_KCRCTAB
1138 if (crc_owner == NULL)
1139 return crc - (unsigned long)reloc_start;
1140#endif
1141 return crc;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static int check_version(Elf_Shdr *sechdrs,
1145 unsigned int versindex,
1146 const char *symname,
1147 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001148 const unsigned long *crc,
1149 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 unsigned int i, num_versions;
1152 struct modversion_info *versions;
1153
1154 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1155 if (!crc)
1156 return 1;
1157
Rusty Russella5dd6972008-05-09 16:24:21 +10001158 /* No versions at all? modprobe --force does this. */
1159 if (versindex == 0)
1160 return try_to_force_load(mod, symname) == 0;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 versions = (void *) sechdrs[versindex].sh_addr;
1163 num_versions = sechdrs[versindex].sh_size
1164 / sizeof(struct modversion_info);
1165
1166 for (i = 0; i < num_versions; i++) {
1167 if (strcmp(versions[i].name, symname) != 0)
1168 continue;
1169
Rusty Russelld4703ae2009-12-15 16:28:32 -06001170 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 1;
Jim Cromie5e124162011-12-06 12:11:31 -07001172 pr_debug("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001173 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001174 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001176
Rusty Russella5dd6972008-05-09 16:24:21 +10001177 printk(KERN_WARNING "%s: no symbol version for %s\n",
1178 mod->name, symname);
1179 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001180
1181bad_version:
1182 printk("%s: disagrees about version of symbol %s\n",
1183 mod->name, symname);
1184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1188 unsigned int versindex,
1189 struct module *mod)
1190{
1191 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Rusty Russell75676502010-06-05 11:17:36 -06001193 /* Since this should be found in kernel (which can't be removed),
1194 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301195 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1196 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001198 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1199 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Rusty Russell91e37a72008-05-09 16:25:28 +10001202/* First part is kernel version, which we ignore if module has crcs. */
1203static inline int same_magic(const char *amagic, const char *bmagic,
1204 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Rusty Russell91e37a72008-05-09 16:25:28 +10001206 if (has_crcs) {
1207 amagic += strcspn(amagic, " ");
1208 bmagic += strcspn(bmagic, " ");
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return strcmp(amagic, bmagic) == 0;
1211}
1212#else
1213static inline int check_version(Elf_Shdr *sechdrs,
1214 unsigned int versindex,
1215 const char *symname,
1216 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001217 const unsigned long *crc,
1218 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 return 1;
1221}
1222
1223static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1224 unsigned int versindex,
1225 struct module *mod)
1226{
1227 return 1;
1228}
1229
Rusty Russell91e37a72008-05-09 16:25:28 +10001230static inline int same_magic(const char *amagic, const char *bmagic,
1231 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 return strcmp(amagic, bmagic) == 0;
1234}
1235#endif /* CONFIG_MODVERSIONS */
1236
Rusty Russell75676502010-06-05 11:17:36 -06001237/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001238static const struct kernel_symbol *resolve_symbol(struct module *mod,
1239 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001240 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001241 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001244 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Rusty Russell75676502010-06-05 11:17:36 -06001248 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001249 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001250 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001251 if (!sym)
1252 goto unlock;
1253
Rusty Russell49668682010-08-05 12:59:10 -06001254 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1255 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001256 sym = ERR_PTR(-EINVAL);
1257 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001259
1260 err = ref_module(mod, owner);
1261 if (err) {
1262 sym = ERR_PTR(err);
1263 goto getname;
1264 }
1265
1266getname:
1267 /* We must make copy under the lock if we failed to get ref. */
1268 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1269unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001270 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001271 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Rusty Russell49668682010-08-05 12:59:10 -06001274static const struct kernel_symbol *
1275resolve_symbol_wait(struct module *mod,
1276 const struct load_info *info,
1277 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001278{
1279 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001280 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001281
1282 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001283 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1284 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001285 30 * HZ) <= 0) {
1286 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001287 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001288 }
1289 return ksym;
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/*
1293 * /sys/module/foo/sections stuff
1294 * J. Corbet <corbet@lwn.net>
1295 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001296#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001297
Rusty Russell8f6d0372010-08-05 12:59:09 -06001298#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001299static inline bool sect_empty(const Elf_Shdr *sect)
1300{
1301 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1302}
1303
Rusty Russella58730c2008-03-13 09:03:44 +00001304struct module_sect_attr
1305{
1306 struct module_attribute mattr;
1307 char *name;
1308 unsigned long address;
1309};
1310
1311struct module_sect_attrs
1312{
1313 struct attribute_group grp;
1314 unsigned int nsections;
1315 struct module_sect_attr attrs[0];
1316};
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301319 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct module_sect_attr *sattr =
1322 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001323 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001326static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1327{
Rusty Russella58730c2008-03-13 09:03:44 +00001328 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001329
1330 for (section = 0; section < sect_attrs->nsections; section++)
1331 kfree(sect_attrs->attrs[section].name);
1332 kfree(sect_attrs);
1333}
1334
Rusty Russell8f6d0372010-08-05 12:59:09 -06001335static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 unsigned int nloaded = 0, i, size[2];
1338 struct module_sect_attrs *sect_attrs;
1339 struct module_sect_attr *sattr;
1340 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001343 for (i = 0; i < info->hdr->e_shnum; i++)
1344 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 nloaded++;
1346 size[0] = ALIGN(sizeof(*sect_attrs)
1347 + nloaded * sizeof(sect_attrs->attrs[0]),
1348 sizeof(sect_attrs->grp.attrs[0]));
1349 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001350 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1351 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return;
1353
1354 /* Setup section attributes. */
1355 sect_attrs->grp.name = "sections";
1356 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1357
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001358 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 sattr = &sect_attrs->attrs[0];
1360 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001361 for (i = 0; i < info->hdr->e_shnum; i++) {
1362 Elf_Shdr *sec = &info->sechdrs[i];
1363 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001364 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001365 sattr->address = sec->sh_addr;
1366 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001367 GFP_KERNEL);
1368 if (sattr->name == NULL)
1369 goto out;
1370 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001371 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 sattr->mattr.show = module_sect_show;
1373 sattr->mattr.store = NULL;
1374 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 sattr->mattr.attr.mode = S_IRUGO;
1376 *(gattr++) = &(sattr++)->mattr.attr;
1377 }
1378 *gattr = NULL;
1379
1380 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1381 goto out;
1382
1383 mod->sect_attrs = sect_attrs;
1384 return;
1385 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001386 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389static void remove_sect_attrs(struct module *mod)
1390{
1391 if (mod->sect_attrs) {
1392 sysfs_remove_group(&mod->mkobj.kobj,
1393 &mod->sect_attrs->grp);
1394 /* We are positive that no one is using any sect attrs
1395 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001396 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 mod->sect_attrs = NULL;
1398 }
1399}
1400
Roland McGrath6d760132007-10-16 23:26:40 -07001401/*
1402 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1403 */
1404
1405struct module_notes_attrs {
1406 struct kobject *dir;
1407 unsigned int notes;
1408 struct bin_attribute attrs[0];
1409};
1410
Chris Wright2c3c8be2010-05-12 18:28:57 -07001411static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001412 struct bin_attribute *bin_attr,
1413 char *buf, loff_t pos, size_t count)
1414{
1415 /*
1416 * The caller checked the pos and count against our size.
1417 */
1418 memcpy(buf, bin_attr->private + pos, count);
1419 return count;
1420}
1421
1422static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1423 unsigned int i)
1424{
1425 if (notes_attrs->dir) {
1426 while (i-- > 0)
1427 sysfs_remove_bin_file(notes_attrs->dir,
1428 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001429 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001430 }
1431 kfree(notes_attrs);
1432}
1433
Rusty Russell8f6d0372010-08-05 12:59:09 -06001434static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001435{
1436 unsigned int notes, loaded, i;
1437 struct module_notes_attrs *notes_attrs;
1438 struct bin_attribute *nattr;
1439
Ingo Molnarea6bff32009-08-28 10:44:56 +02001440 /* failed to create section attributes, so can't create notes */
1441 if (!mod->sect_attrs)
1442 return;
1443
Roland McGrath6d760132007-10-16 23:26:40 -07001444 /* Count notes sections and allocate structures. */
1445 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001446 for (i = 0; i < info->hdr->e_shnum; i++)
1447 if (!sect_empty(&info->sechdrs[i]) &&
1448 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001449 ++notes;
1450
1451 if (notes == 0)
1452 return;
1453
1454 notes_attrs = kzalloc(sizeof(*notes_attrs)
1455 + notes * sizeof(notes_attrs->attrs[0]),
1456 GFP_KERNEL);
1457 if (notes_attrs == NULL)
1458 return;
1459
1460 notes_attrs->notes = notes;
1461 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001462 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1463 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001464 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001465 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001466 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001467 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1468 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001469 nattr->size = info->sechdrs[i].sh_size;
1470 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001471 nattr->read = module_notes_read;
1472 ++nattr;
1473 }
1474 ++loaded;
1475 }
1476
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001477 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001478 if (!notes_attrs->dir)
1479 goto out;
1480
1481 for (i = 0; i < notes; ++i)
1482 if (sysfs_create_bin_file(notes_attrs->dir,
1483 &notes_attrs->attrs[i]))
1484 goto out;
1485
1486 mod->notes_attrs = notes_attrs;
1487 return;
1488
1489 out:
1490 free_notes_attrs(notes_attrs, i);
1491}
1492
1493static void remove_notes_attrs(struct module *mod)
1494{
1495 if (mod->notes_attrs)
1496 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001500
Rusty Russell8f6d0372010-08-05 12:59:09 -06001501static inline void add_sect_attrs(struct module *mod,
1502 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504}
1505
1506static inline void remove_sect_attrs(struct module *mod)
1507{
1508}
Roland McGrath6d760132007-10-16 23:26:40 -07001509
Rusty Russell8f6d0372010-08-05 12:59:09 -06001510static inline void add_notes_attrs(struct module *mod,
1511 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001512{
1513}
1514
1515static inline void remove_notes_attrs(struct module *mod)
1516{
1517}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001518#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001520static void add_usage_links(struct module *mod)
1521{
1522#ifdef CONFIG_MODULE_UNLOAD
1523 struct module_use *use;
1524 int nowarn;
1525
Rusty Russell75676502010-06-05 11:17:36 -06001526 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001527 list_for_each_entry(use, &mod->target_list, target_list) {
1528 nowarn = sysfs_create_link(use->target->holders_dir,
1529 &mod->mkobj.kobj, mod->name);
1530 }
Rusty Russell75676502010-06-05 11:17:36 -06001531 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001532#endif
1533}
1534
1535static void del_usage_links(struct module *mod)
1536{
1537#ifdef CONFIG_MODULE_UNLOAD
1538 struct module_use *use;
1539
Rusty Russell75676502010-06-05 11:17:36 -06001540 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001541 list_for_each_entry(use, &mod->target_list, target_list)
1542 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001543 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001544#endif
1545}
1546
Rusty Russell6407ebb22010-06-05 11:17:36 -06001547static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001548{
1549 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001550 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001551 int error = 0;
1552 int i;
1553
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001554 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1555 (ARRAY_SIZE(modinfo_attrs) + 1)),
1556 GFP_KERNEL);
1557 if (!mod->modinfo_attrs)
1558 return -ENOMEM;
1559
1560 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001561 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1562 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001563 (attr->test && attr->test(mod))) {
1564 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001565 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001566 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1567 ++temp_attr;
1568 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001569 }
1570 return error;
1571}
1572
Rusty Russell6407ebb22010-06-05 11:17:36 -06001573static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001574{
1575 struct module_attribute *attr;
1576 int i;
1577
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001578 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1579 /* pick a field to test for end of list */
1580 if (!attr->attr.name)
1581 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001582 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001583 if (attr->free)
1584 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001585 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001586 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Rusty Russell6407ebb22010-06-05 11:17:36 -06001589static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001592 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001594 if (!module_sysfs_initialized) {
1595 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001596 mod->name);
1597 err = -EINVAL;
1598 goto out;
1599 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001600
1601 kobj = kset_find_obj(module_kset, mod->name);
1602 if (kobj) {
1603 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1604 kobject_put(kobj);
1605 err = -EINVAL;
1606 goto out;
1607 }
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001610
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001611 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1612 mod->mkobj.kobj.kset = module_kset;
1613 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1614 "%s", mod->name);
1615 if (err)
1616 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001617
Kay Sievers97c146e2007-11-29 23:46:11 +01001618 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001619out:
1620 return err;
1621}
1622
Rusty Russell6407ebb22010-06-05 11:17:36 -06001623static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001624 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001625 struct kernel_param *kparam,
1626 unsigned int num_params)
1627{
1628 int err;
1629
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001630 err = mod_sysfs_init(mod);
1631 if (err)
1632 goto out;
1633
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001634 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001635 if (!mod->holders_dir) {
1636 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001637 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001638 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 err = module_param_sysfs_setup(mod, kparam, num_params);
1641 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001642 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Matt Domschc988d2b2005-06-23 22:05:15 -07001644 err = module_add_modinfo_attrs(mod);
1645 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001646 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001647
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001648 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001649 add_sect_attrs(mod, info);
1650 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001651
Kay Sieverse17e0f52006-11-24 12:15:25 +01001652 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return 0;
1654
Kay Sieverse17e0f52006-11-24 12:15:25 +01001655out_unreg_param:
1656 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001657out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001658 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001659out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001660 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001661out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return err;
1663}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001664
1665static void mod_sysfs_fini(struct module *mod)
1666{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001667 remove_notes_attrs(mod);
1668 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001669 kobject_put(&mod->mkobj.kobj);
1670}
1671
Rusty Russell8f6d0372010-08-05 12:59:09 -06001672#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001673
Rusty Russell8f6d0372010-08-05 12:59:09 -06001674static int mod_sysfs_setup(struct module *mod,
1675 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001676 struct kernel_param *kparam,
1677 unsigned int num_params)
1678{
1679 return 0;
1680}
1681
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001682static void mod_sysfs_fini(struct module *mod)
1683{
1684}
1685
Rusty Russell36b03602010-08-05 12:59:09 -06001686static void module_remove_modinfo_attrs(struct module *mod)
1687{
1688}
1689
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001690static void del_usage_links(struct module *mod)
1691{
1692}
1693
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001694#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Rusty Russell36b03602010-08-05 12:59:09 -06001696static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001698 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001699 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001701 kobject_put(mod->mkobj.drivers_dir);
1702 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001703 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/*
1707 * unlink the module with the whole machine is stopped with interrupts off
1708 * - this defends against kallsyms not taking locks
1709 */
1710static int __unlink_module(void *_mod)
1711{
1712 struct module *mod = _mod;
1713 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001714 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return 0;
1716}
1717
matthieu castet84e1c6b2010-11-16 22:35:16 +01001718#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1719/*
1720 * LKM RO/NX protection: protect module's text/ro-data
1721 * from modification and any data from execution.
1722 */
1723void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1724{
1725 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1726 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1727
1728 if (end_pfn > begin_pfn)
1729 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1730}
1731
1732static void set_section_ro_nx(void *base,
1733 unsigned long text_size,
1734 unsigned long ro_size,
1735 unsigned long total_size)
1736{
1737 /* begin and end PFNs of the current subsection */
1738 unsigned long begin_pfn;
1739 unsigned long end_pfn;
1740
1741 /*
1742 * Set RO for module text and RO-data:
1743 * - Always protect first page.
1744 * - Do not protect last partial page.
1745 */
1746 if (ro_size > 0)
1747 set_page_attributes(base, base + ro_size, set_memory_ro);
1748
1749 /*
1750 * Set NX permissions for module data:
1751 * - Do not protect first partial page.
1752 * - Always protect last page.
1753 */
1754 if (total_size > text_size) {
1755 begin_pfn = PFN_UP((unsigned long)base + text_size);
1756 end_pfn = PFN_UP((unsigned long)base + total_size);
1757 if (end_pfn > begin_pfn)
1758 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1759 }
1760}
1761
Jan Glauber01526ed2011-05-19 16:55:26 -06001762static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001763{
Jan Glauber01526ed2011-05-19 16:55:26 -06001764 set_page_attributes(mod->module_core + mod->core_text_size,
1765 mod->module_core + mod->core_size,
1766 set_memory_x);
1767 set_page_attributes(mod->module_core,
1768 mod->module_core + mod->core_ro_size,
1769 set_memory_rw);
1770}
1771
1772static void unset_module_init_ro_nx(struct module *mod)
1773{
1774 set_page_attributes(mod->module_init + mod->init_text_size,
1775 mod->module_init + mod->init_size,
1776 set_memory_x);
1777 set_page_attributes(mod->module_init,
1778 mod->module_init + mod->init_ro_size,
1779 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001780}
1781
1782/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001783void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001784{
1785 struct module *mod;
1786
1787 mutex_lock(&module_mutex);
1788 list_for_each_entry_rcu(mod, &modules, list) {
1789 if ((mod->module_core) && (mod->core_text_size)) {
1790 set_page_attributes(mod->module_core,
1791 mod->module_core + mod->core_text_size,
1792 set_memory_rw);
1793 }
1794 if ((mod->module_init) && (mod->init_text_size)) {
1795 set_page_attributes(mod->module_init,
1796 mod->module_init + mod->init_text_size,
1797 set_memory_rw);
1798 }
1799 }
1800 mutex_unlock(&module_mutex);
1801}
1802
1803/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001804void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001805{
1806 struct module *mod;
1807
1808 mutex_lock(&module_mutex);
1809 list_for_each_entry_rcu(mod, &modules, list) {
1810 if ((mod->module_core) && (mod->core_text_size)) {
1811 set_page_attributes(mod->module_core,
1812 mod->module_core + mod->core_text_size,
1813 set_memory_ro);
1814 }
1815 if ((mod->module_init) && (mod->init_text_size)) {
1816 set_page_attributes(mod->module_init,
1817 mod->module_init + mod->init_text_size,
1818 set_memory_ro);
1819 }
1820 }
1821 mutex_unlock(&module_mutex);
1822}
1823#else
1824static 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 -06001825static void unset_module_core_ro_nx(struct module *mod) { }
1826static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001827#endif
1828
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001829void __weak module_free(struct module *mod, void *module_region)
1830{
1831 vfree(module_region);
1832}
1833
1834void __weak module_arch_cleanup(struct module *mod)
1835{
1836}
1837
Rusty Russell75676502010-06-05 11:17:36 -06001838/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static void free_module(struct module *mod)
1840{
Li Zefan7ead8b82009-08-17 16:56:28 +08001841 trace_module_free(mod);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001844 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001845 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001846 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001847 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Jason Baronb82bab4b2010-07-27 13:18:01 -07001849 /* Remove dynamic debug info */
1850 ddebug_remove_module(mod->name);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /* Arch-specific cleanup. */
1853 module_arch_cleanup(mod);
1854
1855 /* Module unload stuff */
1856 module_unload_free(mod);
1857
Rusty Russelle180a6b2009-03-31 13:05:29 -06001858 /* Free any allocated parameters. */
1859 destroy_params(mod->kp, mod->num_kp);
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001862 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 module_free(mod, mod->module_init);
1864 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001865 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001866
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001867 /* Free lock-classes: */
1868 lockdep_free_key_range(mod->module_core, mod->core_size);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001871 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001873
1874#ifdef CONFIG_MPU
1875 update_protections(current->mm);
1876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879void *__symbol_get(const char *symbol)
1880{
1881 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001882 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Rusty Russell24da1cb2007-07-15 23:41:46 -07001884 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001885 sym = find_symbol(symbol, &owner, NULL, true, true);
1886 if (sym && strong_try_module_get(owner))
1887 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001888 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Tim Abbott414fd312008-12-05 19:03:56 -05001890 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892EXPORT_SYMBOL_GPL(__symbol_get);
1893
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001894/*
1895 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001896 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001897 *
1898 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001899 */
1900static int verify_export_symbols(struct module *mod)
1901{
Rusty Russellb2111042008-05-01 21:15:00 -05001902 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001903 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001904 const struct kernel_symbol *s;
1905 struct {
1906 const struct kernel_symbol *sym;
1907 unsigned int num;
1908 } arr[] = {
1909 { mod->syms, mod->num_syms },
1910 { mod->gpl_syms, mod->num_gpl_syms },
1911 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001912#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001913 { mod->unused_syms, mod->num_unused_syms },
1914 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001915#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001916 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001917
Rusty Russellb2111042008-05-01 21:15:00 -05001918 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1919 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001920 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001921 printk(KERN_ERR
1922 "%s: exports duplicate symbol %s"
1923 " (owned by %s)\n",
1924 mod->name, s->name, module_name(owner));
1925 return -ENOEXEC;
1926 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001927 }
Rusty Russellb2111042008-05-01 21:15:00 -05001928 }
1929 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001930}
1931
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001932/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001933static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Rusty Russell49668682010-08-05 12:59:10 -06001935 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1936 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001938 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001940 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Rusty Russell49668682010-08-05 12:59:10 -06001942 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1943 const char *name = info->strtab + sym[i].st_name;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 switch (sym[i].st_shndx) {
1946 case SHN_COMMON:
1947 /* We compiled with -fno-common. These are not
1948 supposed to happen. */
Jim Cromie5e124162011-12-06 12:11:31 -07001949 pr_debug("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 printk("%s: please compile with -fno-common\n",
1951 mod->name);
1952 ret = -ENOEXEC;
1953 break;
1954
1955 case SHN_ABS:
1956 /* Don't need to do anything */
Jim Cromie5e124162011-12-06 12:11:31 -07001957 pr_debug("Absolute symbol: 0x%08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 (long)sym[i].st_value);
1959 break;
1960
1961 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001962 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001964 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001965 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001967 }
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001970 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972
Rusty Russell9bea7f22010-06-05 11:17:37 -06001973 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001974 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001975 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977
1978 default:
1979 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001980 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001981 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 else
Rusty Russell49668682010-08-05 12:59:10 -06001983 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 sym[i].st_value += secbase;
1985 break;
1986 }
1987 }
1988
1989 return ret;
1990}
1991
Rusty Russell49668682010-08-05 12:59:10 -06001992static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001993{
1994 unsigned int i;
1995 int err = 0;
1996
1997 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001998 for (i = 1; i < info->hdr->e_shnum; i++) {
1999 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06002000
2001 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06002002 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06002003 continue;
2004
2005 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06002006 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06002007 continue;
2008
Rusty Russell49668682010-08-05 12:59:10 -06002009 if (info->sechdrs[i].sh_type == SHT_REL)
2010 err = apply_relocate(info->sechdrs, info->strtab,
2011 info->index.sym, i, mod);
2012 else if (info->sechdrs[i].sh_type == SHT_RELA)
2013 err = apply_relocate_add(info->sechdrs, info->strtab,
2014 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002015 if (err < 0)
2016 break;
2017 }
2018 return err;
2019}
2020
Helge Deller088af9a2008-12-31 12:31:18 +01002021/* Additional bytes needed by arch in front of individual sections */
2022unsigned int __weak arch_mod_section_prepend(struct module *mod,
2023 unsigned int section)
2024{
2025 /* default implementation just returns zero */
2026 return 0;
2027}
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01002030static long get_offset(struct module *mod, unsigned int *size,
2031 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 long ret;
2034
Helge Deller088af9a2008-12-31 12:31:18 +01002035 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2037 *size = ret + sechdr->sh_size;
2038 return ret;
2039}
2040
2041/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2042 might -- code, read-only data, read-write data, small data. Tally
2043 sizes, and place the offsets into sh_entsize fields: high bit means it
2044 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06002045static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 static unsigned long const masks[][2] = {
2048 /* NOTE: all executable code must be the first section
2049 * in this array; otherwise modify the text_size
2050 * finder in the two loops below */
2051 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2052 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2053 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2054 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2055 };
2056 unsigned int m, i;
2057
Rusty Russell49668682010-08-05 12:59:10 -06002058 for (i = 0; i < info->hdr->e_shnum; i++)
2059 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Jim Cromie5e124162011-12-06 12:11:31 -07002061 pr_debug("Core section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002063 for (i = 0; i < info->hdr->e_shnum; ++i) {
2064 Elf_Shdr *s = &info->sechdrs[i];
2065 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2068 || (s->sh_flags & masks[m][1])
2069 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002070 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002072 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Jim Cromie5e124162011-12-06 12:11:31 -07002073 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002075 switch (m) {
2076 case 0: /* executable */
2077 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002079 break;
2080 case 1: /* RO: text and ro-data */
2081 mod->core_size = debug_align(mod->core_size);
2082 mod->core_ro_size = mod->core_size;
2083 break;
2084 case 3: /* whole core */
2085 mod->core_size = debug_align(mod->core_size);
2086 break;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Jim Cromie5e124162011-12-06 12:11:31 -07002090 pr_debug("Init section allocation order:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06002092 for (i = 0; i < info->hdr->e_shnum; ++i) {
2093 Elf_Shdr *s = &info->sechdrs[i];
2094 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2097 || (s->sh_flags & masks[m][1])
2098 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002099 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002101 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 | INIT_OFFSET_MASK);
Jim Cromie5e124162011-12-06 12:11:31 -07002103 pr_debug("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002105 switch (m) {
2106 case 0: /* executable */
2107 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002109 break;
2110 case 1: /* RO: text and ro-data */
2111 mod->init_size = debug_align(mod->init_size);
2112 mod->init_ro_size = mod->init_size;
2113 break;
2114 case 3: /* whole init */
2115 mod->init_size = debug_align(mod->init_size);
2116 break;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119}
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121static void set_license(struct module *mod, const char *license)
2122{
2123 if (!license)
2124 license = "unspecified";
2125
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002126 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002127 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002128 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002129 "kernel.\n", mod->name, license);
2130 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132}
2133
2134/* Parse tag=value strings from .modinfo section */
2135static char *next_string(char *string, unsigned long *secsize)
2136{
2137 /* Skip non-zero chars */
2138 while (string[0]) {
2139 string++;
2140 if ((*secsize)-- <= 1)
2141 return NULL;
2142 }
2143
2144 /* Skip any zero padding. */
2145 while (!string[0]) {
2146 string++;
2147 if ((*secsize)-- <= 1)
2148 return NULL;
2149 }
2150 return string;
2151}
2152
Rusty Russell49668682010-08-05 12:59:10 -06002153static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 char *p;
2156 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002157 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2158 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Rusty Russell49668682010-08-05 12:59:10 -06002160 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2162 return p + taglen + 1;
2163 }
2164 return NULL;
2165}
2166
Rusty Russell49668682010-08-05 12:59:10 -06002167static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002168{
2169 struct module_attribute *attr;
2170 int i;
2171
2172 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2173 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002174 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002175 }
2176}
Matt Domschc988d2b2005-06-23 22:05:15 -07002177
Rusty Russella263f772009-09-25 00:32:58 -06002178static void free_modinfo(struct module *mod)
2179{
2180 struct module_attribute *attr;
2181 int i;
2182
2183 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2184 if (attr->free)
2185 attr->free(mod);
2186 }
2187}
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002190
2191/* lookup symbol in given range of kernel_symbols */
2192static const struct kernel_symbol *lookup_symbol(const char *name,
2193 const struct kernel_symbol *start,
2194 const struct kernel_symbol *stop)
2195{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002196 return bsearch(name, start, stop - start,
2197 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002198}
2199
Tim Abbottca4787b2009-01-05 08:40:10 -06002200static int is_exported(const char *name, unsigned long value,
2201 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Tim Abbottca4787b2009-01-05 08:40:10 -06002203 const struct kernel_symbol *ks;
2204 if (!mod)
2205 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002206 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002207 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2208 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002212static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Rusty Russelleded41c2010-08-05 12:59:07 -06002214 const Elf_Shdr *sechdrs = info->sechdrs;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2217 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2218 return 'v';
2219 else
2220 return 'w';
2221 }
2222 if (sym->st_shndx == SHN_UNDEF)
2223 return 'U';
2224 if (sym->st_shndx == SHN_ABS)
2225 return 'a';
2226 if (sym->st_shndx >= SHN_LORESERVE)
2227 return '?';
2228 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2229 return 't';
2230 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2231 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2232 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2233 return 'r';
2234 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2235 return 'g';
2236 else
2237 return 'd';
2238 }
2239 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2240 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2241 return 's';
2242 else
2243 return 'b';
2244 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002245 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2246 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return '?';
2250}
2251
Jan Beulich4a496222009-07-06 14:50:42 +01002252static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2253 unsigned int shnum)
2254{
2255 const Elf_Shdr *sec;
2256
2257 if (src->st_shndx == SHN_UNDEF
2258 || src->st_shndx >= shnum
2259 || !src->st_name)
2260 return false;
2261
2262 sec = sechdrs + src->st_shndx;
2263 if (!(sec->sh_flags & SHF_ALLOC)
2264#ifndef CONFIG_KALLSYMS_ALL
2265 || !(sec->sh_flags & SHF_EXECINSTR)
2266#endif
2267 || (sec->sh_entsize & INIT_OFFSET_MASK))
2268 return false;
2269
2270 return true;
2271}
2272
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302273/*
2274 * We only allocate and copy the strings needed by the parts of symtab
2275 * we keep. This is simple, but has the effect of making multiple
2276 * copies of duplicates. We could be more sophisticated, see
2277 * linux-kernel thread starting with
2278 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2279 */
Rusty Russell49668682010-08-05 12:59:10 -06002280static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002281{
Rusty Russell49668682010-08-05 12:59:10 -06002282 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2283 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002284 const Elf_Sym *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302285 unsigned int i, nsrc, ndst, strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002286
2287 /* Put symbol section at end of init part of module. */
2288 symsect->sh_flags |= SHF_ALLOC;
2289 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002290 info->index.sym) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002291 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002292
Rusty Russell49668682010-08-05 12:59:10 -06002293 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002294 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002295
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302296 /* Compute total space required for the core symbols' strtab. */
2297 for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002298 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302299 strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2300 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002301 }
Jan Beulich4a496222009-07-06 14:50:42 +01002302
2303 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002304 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302305 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2306 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002307
Jan Beulich554bdfe2009-07-06 14:51:44 +01002308 /* Put string table section at end of init part of module. */
2309 strsect->sh_flags |= SHF_ALLOC;
2310 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002311 info->index.str) | INIT_OFFSET_MASK;
Jim Cromie5e124162011-12-06 12:11:31 -07002312 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002313}
2314
Rusty Russell811d66a2010-08-05 12:59:12 -06002315static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Jan Beulich4a496222009-07-06 14:50:42 +01002317 unsigned int i, ndst;
2318 const Elf_Sym *src;
2319 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002320 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002321 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Rusty Russelleded41c2010-08-05 12:59:07 -06002323 mod->symtab = (void *)symsec->sh_addr;
2324 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002325 /* Make sure we get permanent strtab: don't use info->strtab. */
2326 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 /* Set types up while we still have access to sections. */
2329 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002330 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002331
Rusty Russelld9131882010-08-05 12:59:08 -06002332 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302333 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002334 src = mod->symtab;
2335 *dst = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302336 *s++ = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002337 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002338 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002339 continue;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302340
Jan Beulich4a496222009-07-06 14:50:42 +01002341 dst[ndst] = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302342 dst[ndst++].st_name = s - mod->core_strtab;
2343 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
Jan Beulich4a496222009-07-06 14:50:42 +01002344 }
2345 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347#else
Rusty Russell49668682010-08-05 12:59:10 -06002348static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002349{
2350}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002351
Michał Mirosławabbce902010-09-20 01:58:08 +02002352static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354}
2355#endif /* CONFIG_KALLSYMS */
2356
Jason Barone9d376f2009-02-05 11:51:38 -05002357static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002358{
Rusty Russell811d66a2010-08-05 12:59:12 -06002359 if (!debug)
2360 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002361#ifdef CONFIG_DYNAMIC_DEBUG
2362 if (ddebug_add_module(debug, num, debug->modname))
2363 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2364 debug->modname);
2365#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002366}
Jason Baron346e15b2008-08-12 16:46:19 -04002367
Yehuda Sadehff49d742010-07-03 13:07:35 +10002368static void dynamic_debug_remove(struct _ddebug *debug)
2369{
2370 if (debug)
2371 ddebug_remove_module(debug->modname);
2372}
2373
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002374void * __weak module_alloc(unsigned long size)
2375{
2376 return size == 0 ? NULL : vmalloc_exec(size);
2377}
2378
Rusty Russell3a642e92008-07-22 19:24:28 -05002379static void *module_alloc_update_bounds(unsigned long size)
2380{
2381 void *ret = module_alloc(size);
2382
2383 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002384 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002385 /* Update module bounds. */
2386 if ((unsigned long)ret < module_addr_min)
2387 module_addr_min = (unsigned long)ret;
2388 if ((unsigned long)ret + size > module_addr_max)
2389 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002390 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002391 }
2392 return ret;
2393}
2394
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002395#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002396static void kmemleak_load_module(const struct module *mod,
2397 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002398{
2399 unsigned int i;
2400
2401 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002402 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002403
Rusty Russell49668682010-08-05 12:59:10 -06002404 for (i = 1; i < info->hdr->e_shnum; i++) {
2405 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2406 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002407 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002408 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002409 continue;
2410
Rusty Russell49668682010-08-05 12:59:10 -06002411 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2412 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002413 }
2414}
2415#else
Rusty Russell49668682010-08-05 12:59:10 -06002416static inline void kmemleak_load_module(const struct module *mod,
2417 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002418{
2419}
2420#endif
2421
Rusty Russell106a4ee2012-09-26 10:09:40 +01002422#ifdef CONFIG_MODULE_SIG
2423static int module_sig_check(struct load_info *info,
David Howellscaabe242012-10-20 01:19:29 +01002424 const void *mod, unsigned long *_len)
Rusty Russell106a4ee2012-09-26 10:09:40 +01002425{
2426 int err = -ENOKEY;
David Howellscaabe242012-10-20 01:19:29 +01002427 unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2428 unsigned long len = *_len;
Rusty Russell106a4ee2012-09-26 10:09:40 +01002429
David Howellscaabe242012-10-20 01:19:29 +01002430 if (len > markerlen &&
2431 memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2432 /* We truncate the module to discard the signature */
2433 *_len -= markerlen;
2434 err = mod_verify_sig(mod, _len);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002435 }
2436
2437 if (!err) {
2438 info->sig_ok = true;
2439 return 0;
2440 }
2441
2442 /* Not having a signature is only an error if we're strict. */
David Howells1d0059f2012-09-26 10:09:50 +01002443 if (err < 0 && fips_enabled)
2444 panic("Module verification failed with error %d in FIPS mode\n",
2445 err);
Rusty Russell106a4ee2012-09-26 10:09:40 +01002446 if (err == -ENOKEY && !sig_enforce)
2447 err = 0;
2448
2449 return err;
2450}
2451#else /* !CONFIG_MODULE_SIG */
2452static int module_sig_check(struct load_info *info,
2453 void *mod, unsigned long *len)
2454{
2455 return 0;
2456}
2457#endif /* !CONFIG_MODULE_SIG */
2458
2459/* Sets info->hdr, info->len and info->sig_ok. */
Rusty Russelld9131882010-08-05 12:59:08 -06002460static int copy_and_check(struct load_info *info,
2461 const void __user *umod, unsigned long len,
2462 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002463{
2464 int err;
2465 Elf_Ehdr *hdr;
2466
2467 if (len < sizeof(*hdr))
2468 return -ENOEXEC;
2469
2470 /* Suck in entire file: we'll want most of it. */
Sasha Levinf946eeb2012-01-30 23:07:22 -05002471 if ((hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002472 return -ENOMEM;
2473
2474 if (copy_from_user(hdr, umod, len) != 0) {
2475 err = -EFAULT;
2476 goto free_hdr;
2477 }
2478
Rusty Russell106a4ee2012-09-26 10:09:40 +01002479 err = module_sig_check(info, hdr, &len);
2480 if (err)
2481 goto free_hdr;
2482
Rusty Russell40dd2562010-08-05 12:59:03 -06002483 /* Sanity checks against insmoding binaries or wrong arch,
2484 weird elf version */
2485 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2486 || hdr->e_type != ET_REL
2487 || !elf_check_arch(hdr)
2488 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2489 err = -ENOEXEC;
2490 goto free_hdr;
2491 }
2492
David Howellsef26a5a2012-05-22 15:56:13 +01002493 if (hdr->e_shoff >= len ||
2494 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002495 err = -ENOEXEC;
2496 goto free_hdr;
2497 }
Rusty Russelld9131882010-08-05 12:59:08 -06002498
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002499 info->hdr = hdr;
2500 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002501 return 0;
2502
2503free_hdr:
2504 vfree(hdr);
2505 return err;
2506}
2507
Rusty Russelld9131882010-08-05 12:59:08 -06002508static void free_copy(struct load_info *info)
2509{
Rusty Russelld9131882010-08-05 12:59:08 -06002510 vfree(info->hdr);
2511}
2512
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002513static int rewrite_section_headers(struct load_info *info)
2514{
2515 unsigned int i;
2516
2517 /* This should always be true, but let's be sure. */
2518 info->sechdrs[0].sh_addr = 0;
2519
2520 for (i = 1; i < info->hdr->e_shnum; i++) {
2521 Elf_Shdr *shdr = &info->sechdrs[i];
2522 if (shdr->sh_type != SHT_NOBITS
2523 && info->len < shdr->sh_offset + shdr->sh_size) {
2524 printk(KERN_ERR "Module len %lu truncated\n",
2525 info->len);
2526 return -ENOEXEC;
2527 }
2528
2529 /* Mark all sections sh_addr with their address in the
2530 temporary image. */
2531 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2532
2533#ifndef CONFIG_MODULE_UNLOAD
2534 /* Don't load .exit sections */
2535 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2536 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2537#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002538 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002539
2540 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002541 info->index.vers = find_sec(info, "__versions");
2542 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002543 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2544 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002545 return 0;
2546}
2547
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002548/*
2549 * Set up our basic convenience variables (pointers to section headers,
2550 * search for module section index etc), and do some basic section
2551 * verification.
2552 *
2553 * Return the temporary module pointer (we'll replace it with the final
2554 * one when we move the module sections around).
2555 */
2556static struct module *setup_load_info(struct load_info *info)
2557{
2558 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002559 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002560 struct module *mod;
2561
2562 /* Set up the convenience variables */
2563 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002564 info->secstrings = (void *)info->hdr
2565 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002566
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002567 err = rewrite_section_headers(info);
2568 if (err)
2569 return ERR_PTR(err);
2570
2571 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002572 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002573 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2574 info->index.sym = i;
2575 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002576 info->strtab = (char *)info->hdr
2577 + info->sechdrs[info->index.str].sh_offset;
2578 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002579 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002580 }
2581
Rusty Russell49668682010-08-05 12:59:10 -06002582 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002583 if (!info->index.mod) {
2584 printk(KERN_WARNING "No module found in object\n");
2585 return ERR_PTR(-ENOEXEC);
2586 }
2587 /* This is temporary: point mod into copy of data. */
2588 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2589
2590 if (info->index.sym == 0) {
2591 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2592 mod->name);
2593 return ERR_PTR(-ENOEXEC);
2594 }
2595
Rusty Russell49668682010-08-05 12:59:10 -06002596 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002597
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002598 /* Check module struct version now, before we try to use module. */
2599 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2600 return ERR_PTR(-ENOEXEC);
2601
2602 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002603}
2604
Rusty Russell49668682010-08-05 12:59:10 -06002605static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002606{
Rusty Russell49668682010-08-05 12:59:10 -06002607 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002608 int err;
2609
2610 /* This is allowed: modprobe --force will invalidate it. */
2611 if (!modmagic) {
2612 err = try_to_force_load(mod, "bad vermagic");
2613 if (err)
2614 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002615 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002616 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2617 mod->name, modmagic, vermagic);
2618 return -ENOEXEC;
2619 }
2620
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002621 if (!get_modinfo(info, "intree"))
2622 add_taint_module(mod, TAINT_OOT_MODULE);
2623
Rusty Russell49668682010-08-05 12:59:10 -06002624 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002625 add_taint_module(mod, TAINT_CRAP);
2626 printk(KERN_WARNING "%s: module is from the staging directory,"
2627 " the quality is unknown, you have been warned.\n",
2628 mod->name);
2629 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002630
2631 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002632 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002633
Rusty Russell40dd2562010-08-05 12:59:03 -06002634 return 0;
2635}
2636
Rusty Russell811d66a2010-08-05 12:59:12 -06002637static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002638{
Rusty Russell49668682010-08-05 12:59:10 -06002639 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002640 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002641 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002642 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002643 mod->crcs = section_addr(info, "__kcrctab");
2644 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002645 sizeof(*mod->gpl_syms),
2646 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002647 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2648 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002649 "__ksymtab_gpl_future",
2650 sizeof(*mod->gpl_future_syms),
2651 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002652 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002653
2654#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002655 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002656 sizeof(*mod->unused_syms),
2657 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002658 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2659 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002660 sizeof(*mod->unused_gpl_syms),
2661 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002662 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002663#endif
2664#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002665 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002666 sizeof(*mod->ctors), &mod->num_ctors);
2667#endif
2668
2669#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002670 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2671 sizeof(*mod->tracepoints_ptrs),
2672 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002673#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002674#ifdef HAVE_JUMP_LABEL
2675 mod->jump_entries = section_objs(info, "__jump_table",
2676 sizeof(*mod->jump_entries),
2677 &mod->num_jump_entries);
2678#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002679#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002680 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002681 sizeof(*mod->trace_events),
2682 &mod->num_trace_events);
2683 /*
2684 * This section contains pointers to allocated objects in the trace
2685 * code and not scanning it leads to false positives.
2686 */
2687 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2688 mod->num_trace_events, GFP_KERNEL);
2689#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002690#ifdef CONFIG_TRACING
2691 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2692 sizeof(*mod->trace_bprintk_fmt_start),
2693 &mod->num_trace_bprintk_fmt);
2694 /*
2695 * This section contains pointers to allocated objects in the trace
2696 * code and not scanning it leads to false positives.
2697 */
2698 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2699 sizeof(*mod->trace_bprintk_fmt_start) *
2700 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2701#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002702#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2703 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002704 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002705 sizeof(*mod->ftrace_callsites),
2706 &mod->num_ftrace_callsites);
2707#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002708
Rusty Russell811d66a2010-08-05 12:59:12 -06002709 mod->extable = section_objs(info, "__ex_table",
2710 sizeof(*mod->extable), &mod->num_exentries);
2711
Rusty Russell49668682010-08-05 12:59:10 -06002712 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002713 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2714 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002715
2716 info->debug = section_objs(info, "__verbose",
2717 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002718}
2719
Rusty Russell49668682010-08-05 12:59:10 -06002720static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002721{
2722 int i;
2723 void *ptr;
2724
2725 /* Do the allocs. */
2726 ptr = module_alloc_update_bounds(mod->core_size);
2727 /*
2728 * The pointer to this block is stored in the module structure
2729 * which is inside the block. Just mark it as not being a
2730 * leak.
2731 */
2732 kmemleak_not_leak(ptr);
2733 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002734 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002735
2736 memset(ptr, 0, mod->core_size);
2737 mod->module_core = ptr;
2738
2739 ptr = module_alloc_update_bounds(mod->init_size);
2740 /*
2741 * The pointer to this block is stored in the module structure
2742 * which is inside the block. This block doesn't need to be
2743 * scanned as it contains data and code that will be freed
2744 * after the module is initialized.
2745 */
2746 kmemleak_ignore(ptr);
2747 if (!ptr && mod->init_size) {
2748 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002749 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002750 }
2751 memset(ptr, 0, mod->init_size);
2752 mod->module_init = ptr;
2753
2754 /* Transfer each section which specifies SHF_ALLOC */
Jim Cromie5e124162011-12-06 12:11:31 -07002755 pr_debug("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002756 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002757 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002758 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002759
Rusty Russell49668682010-08-05 12:59:10 -06002760 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002761 continue;
2762
Rusty Russell49668682010-08-05 12:59:10 -06002763 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002764 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002765 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002766 else
Rusty Russell49668682010-08-05 12:59:10 -06002767 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002768
Rusty Russell49668682010-08-05 12:59:10 -06002769 if (shdr->sh_type != SHT_NOBITS)
2770 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002771 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002772 shdr->sh_addr = (unsigned long)dest;
Jim Cromie5e124162011-12-06 12:11:31 -07002773 pr_debug("\t0x%lx %s\n",
2774 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002775 }
Rusty Russelld9131882010-08-05 12:59:08 -06002776
2777 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002778}
2779
Rusty Russell49668682010-08-05 12:59:10 -06002780static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002781{
2782 /*
2783 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2784 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2785 * using GPL-only symbols it needs.
2786 */
2787 if (strcmp(mod->name, "ndiswrapper") == 0)
2788 add_taint(TAINT_PROPRIETARY_MODULE);
2789
2790 /* driverloader was caught wrongly pretending to be under GPL */
2791 if (strcmp(mod->name, "driverloader") == 0)
2792 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2793
Matthew Garrettc99af372012-06-22 13:49:31 -04002794 /* lve claims to be GPL but upstream won't provide source */
2795 if (strcmp(mod->name, "lve") == 0)
2796 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2797
Rusty Russell22e268e2010-08-05 12:59:05 -06002798#ifdef CONFIG_MODVERSIONS
2799 if ((mod->num_syms && !mod->crcs)
2800 || (mod->num_gpl_syms && !mod->gpl_crcs)
2801 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2802#ifdef CONFIG_UNUSED_SYMBOLS
2803 || (mod->num_unused_syms && !mod->unused_crcs)
2804 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2805#endif
2806 ) {
2807 return try_to_force_load(mod,
2808 "no versions for exported symbols");
2809 }
2810#endif
2811 return 0;
2812}
2813
2814static void flush_module_icache(const struct module *mod)
2815{
2816 mm_segment_t old_fs;
2817
2818 /* flush the icache in correct context */
2819 old_fs = get_fs();
2820 set_fs(KERNEL_DS);
2821
2822 /*
2823 * Flush the instruction cache, since we've played with text.
2824 * Do it before processing of module parameters, so the module
2825 * can provide parameter accessor functions of its own.
2826 */
2827 if (mod->module_init)
2828 flush_icache_range((unsigned long)mod->module_init,
2829 (unsigned long)mod->module_init
2830 + mod->init_size);
2831 flush_icache_range((unsigned long)mod->module_core,
2832 (unsigned long)mod->module_core + mod->core_size);
2833
2834 set_fs(old_fs);
2835}
2836
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002837int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2838 Elf_Shdr *sechdrs,
2839 char *secstrings,
2840 struct module *mod)
2841{
2842 return 0;
2843}
2844
Rusty Russelld9131882010-08-05 12:59:08 -06002845static struct module *layout_and_allocate(struct load_info *info)
2846{
2847 /* Module within temporary copy. */
2848 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002849 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002850 int err;
2851
2852 mod = setup_load_info(info);
2853 if (IS_ERR(mod))
2854 return mod;
2855
Rusty Russell49668682010-08-05 12:59:10 -06002856 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002857 if (err)
2858 return ERR_PTR(err);
2859
2860 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002861 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2862 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002863 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002864 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002865
Rusty Russell49668682010-08-05 12:59:10 -06002866 pcpusec = &info->sechdrs[info->index.pcpu];
2867 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002868 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002869 err = percpu_modalloc(mod,
2870 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002871 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002872 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002873 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002874 }
2875
2876 /* Determine total sizes, and put offsets in sh_entsize. For now
2877 this is done generically; there doesn't appear to be any
2878 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002879 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002880 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002881
2882 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002883 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002884 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302885 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002886
2887 /* Module has been copied to its final place now: return it. */
2888 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002889 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002890 return mod;
2891
Rusty Russelld9131882010-08-05 12:59:08 -06002892free_percpu:
2893 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002894out:
Rusty Russelld9131882010-08-05 12:59:08 -06002895 return ERR_PTR(err);
2896}
2897
2898/* mod is no longer valid after this! */
2899static void module_deallocate(struct module *mod, struct load_info *info)
2900{
Rusty Russelld9131882010-08-05 12:59:08 -06002901 percpu_modfree(mod);
2902 module_free(mod, mod->module_init);
2903 module_free(mod, mod->module_core);
2904}
2905
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002906int __weak module_finalize(const Elf_Ehdr *hdr,
2907 const Elf_Shdr *sechdrs,
2908 struct module *me)
2909{
2910 return 0;
2911}
2912
Rusty Russell811d66a2010-08-05 12:59:12 -06002913static int post_relocation(struct module *mod, const struct load_info *info)
2914{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002915 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002916 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2917
2918 /* Copy relocated percpu area over. */
2919 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2920 info->sechdrs[info->index.pcpu].sh_size);
2921
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002922 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002923 add_kallsyms(mod, info);
2924
2925 /* Arch-specific module finalizing. */
2926 return module_finalize(info->hdr, info->sechdrs, mod);
2927}
2928
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302929/* Is this module of this name done loading? No locks held. */
2930static bool finished_loading(const char *name)
2931{
2932 struct module *mod;
2933 bool ret;
2934
2935 mutex_lock(&module_mutex);
2936 mod = find_module(name);
2937 ret = !mod || mod->state != MODULE_STATE_COMING;
2938 mutex_unlock(&module_mutex);
2939
2940 return ret;
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943/* Allocate and load the module: note that size of section 0 is always
2944 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002945static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 unsigned long len,
2947 const char __user *uargs)
2948{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002949 struct load_info info = { NULL, };
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09302950 struct module *mod, *old;
Rusty Russell40dd2562010-08-05 12:59:03 -06002951 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002952
Jim Cromie5e124162011-12-06 12:11:31 -07002953 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Rusty Russelld9131882010-08-05 12:59:08 -06002956 /* Copy in the blobs from userspace, check they are vaguely sane. */
2957 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002958 if (err)
2959 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Rusty Russelld9131882010-08-05 12:59:08 -06002961 /* Figure out module layout, and allocate all the memory. */
2962 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002963 if (IS_ERR(mod)) {
2964 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002965 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Rusty Russell106a4ee2012-09-26 10:09:40 +01002968#ifdef CONFIG_MODULE_SIG
2969 mod->sig_ok = info.sig_ok;
2970 if (!mod->sig_ok)
2971 add_taint_module(mod, TAINT_FORCED_MODULE);
2972#endif
2973
Rusty Russell49668682010-08-05 12:59:10 -06002974 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002975 err = module_unload_init(mod);
2976 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002977 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Rusty Russell22e268e2010-08-05 12:59:05 -06002979 /* Now we've got everything in the final locations, we can
2980 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002981 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Rusty Russell49668682010-08-05 12:59:10 -06002983 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002984 if (err)
2985 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002986
Matt Domschc988d2b2005-06-23 22:05:15 -07002987 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002988 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002991 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002993 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Rusty Russell49668682010-08-05 12:59:10 -06002995 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002996 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002997 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Rusty Russell811d66a2010-08-05 12:59:12 -06002999 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06003001 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Rusty Russell22e268e2010-08-05 12:59:05 -06003003 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07003004
Rusty Russell6526c532010-08-05 12:59:10 -06003005 /* Now copy in args */
3006 mod->args = strndup_user(uargs, ~0UL >> 1);
3007 if (IS_ERR(mod->args)) {
3008 err = PTR_ERR(mod->args);
3009 goto free_arch_cleanup;
3010 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08003011
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003012 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06003013 mod->state = MODULE_STATE_COMING;
3014
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003015 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003016 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02003017 * strong_try_module_get() will fail.
3018 * lockdep/oops can run asynchronous, so use the RCU list insertion
3019 * function to insert in a way safe to concurrent readers.
3020 * The mutex protects against concurrent writers.
3021 */
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303022again:
Rusty Russell75676502010-06-05 11:17:36 -06003023 mutex_lock(&module_mutex);
Rusty Russell9bb9c3b2012-09-28 14:31:03 +09303024 if ((old = find_module(mod->name)) != NULL) {
3025 if (old->state == MODULE_STATE_COMING) {
3026 /* Wait in case it fails to load. */
3027 mutex_unlock(&module_mutex);
3028 err = wait_event_interruptible(module_wq,
3029 finished_loading(mod->name));
3030 if (err)
3031 goto free_arch_cleanup;
3032 goto again;
3033 }
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003034 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06003035 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06003036 }
3037
Rusty Russell811d66a2010-08-05 12:59:12 -06003038 /* This has to be done once we're sure module name is unique. */
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003039 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003040
Rusty Russellbe593f42010-06-05 11:17:37 -06003041 /* Find duplicate symbols */
3042 err = verify_export_symbols(mod);
3043 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10003044 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06003045
Linus Torvalds53363772010-10-05 11:29:27 -07003046 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02003047 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06003048 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003049
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003050 /* Module is ready to execute: parsing args may do that. */
Pawel Moll026cee02012-03-26 12:50:51 +10303051 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
Jim Cromieb48420c2012-04-27 14:30:35 -06003052 -32768, 32767, &ddebug_dyndbg_module_param_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003054 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003056 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06003057 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003059 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06003060
Kevin Cernekee48fd1182012-01-13 09:32:14 +10303061 /* Get rid of temporary copy. */
Rusty Russelld9131882010-08-05 12:59:08 -06003062 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003065 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return mod;
3067
Rusty Russellbb9d3d52008-01-29 17:13:21 -05003068 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06003069 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003070 /* Unlink carefully: kallsyms could be walking list. */
3071 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07003072 module_bug_cleanup(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303073 wake_up_all(&module_wq);
Yehuda Sadehff49d742010-07-03 13:07:35 +10003074 ddebug:
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00003075 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06003076 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06003077 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06003078 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06003079 kfree(mod->args);
3080 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003082 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06003083 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06003084 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06003086 free_module:
3087 module_deallocate(mod, &info);
3088 free_copy:
3089 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08003090 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003093/* Call module constructors. */
3094static void do_mod_ctors(struct module *mod)
3095{
3096#ifdef CONFIG_CONSTRUCTORS
3097 unsigned long i;
3098
3099 for (i = 0; i < mod->num_ctors; i++)
3100 mod->ctors[i]();
3101#endif
3102}
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003105SYSCALL_DEFINE3(init_module, void __user *, umod,
3106 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 struct module *mod;
3109 int ret = 0;
3110
3111 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07003112 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return -EPERM;
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 /* Do all the hard work */
3116 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06003117 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Alan Sterne041c682006-03-27 01:16:30 -08003120 blocking_notifier_call_chain(&module_notify_list,
3121 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Steven Rostedt94462ad2010-11-29 13:15:42 -05003123 /* Set RO and NX regions for core */
3124 set_section_ro_nx(mod->module_core,
3125 mod->core_text_size,
3126 mod->core_ro_size,
3127 mod->core_size);
3128
3129 /* Set RO and NX regions for init */
3130 set_section_ro_nx(mod->module_init,
3131 mod->init_text_size,
3132 mod->init_ro_size,
3133 mod->init_size);
3134
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07003135 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /* Start the module */
3137 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07003138 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (ret < 0) {
3140 /* Init routine failed: abort. Try to protect us from
3141 buggy refcounters. */
3142 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003143 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07003144 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02003145 blocking_notifier_call_chain(&module_notify_list,
3146 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07003147 free_module(mod);
Rusty Russell6f139092012-09-28 14:31:03 +09303148 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return ret;
3150 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003151 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07003152 printk(KERN_WARNING
3153"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3154"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07003155 __func__, mod->name, ret,
3156 __func__);
3157 dump_stack();
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Rusty Russell6f139092012-09-28 14:31:03 +09303160 /* Now it's a first class citizen! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 mod->state = MODULE_STATE_LIVE;
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08003162 blocking_notifier_call_chain(&module_notify_list,
3163 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07003164
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07003165 /* We need to finish all async code before the module init sequence is done */
3166 async_synchronize_full();
3167
Rusty Russell6c5db222008-03-10 11:43:52 -07003168 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 /* Drop initial reference. */
3170 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003171 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003172#ifdef CONFIG_KALLSYMS
3173 mod->num_symtab = mod->core_num_syms;
3174 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003175 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003176#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003177 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 module_free(mod, mod->module_init);
3179 mod->module_init = NULL;
3180 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003181 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003183 mutex_unlock(&module_mutex);
Rusty Russell6f139092012-09-28 14:31:03 +09303184 wake_up_all(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 return 0;
3187}
3188
3189static inline int within(unsigned long addr, void *start, unsigned long size)
3190{
3191 return ((void *)addr >= start && (void *)addr < start + size);
3192}
3193
3194#ifdef CONFIG_KALLSYMS
3195/*
3196 * This ignores the intensely annoying "mapping symbols" found
3197 * in ARM ELF files: $a, $t and $d.
3198 */
3199static inline int is_arm_mapping_symbol(const char *str)
3200{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003201 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 && (str[2] == '\0' || str[2] == '.');
3203}
3204
3205static const char *get_ksymbol(struct module *mod,
3206 unsigned long addr,
3207 unsigned long *size,
3208 unsigned long *offset)
3209{
3210 unsigned int i, best = 0;
3211 unsigned long nextval;
3212
3213 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003214 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003216 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3218
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003219 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003220 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 for (i = 1; i < mod->num_symtab; i++) {
3222 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3223 continue;
3224
3225 /* We ignore unnamed symbols: they're uninformative
3226 * and inserted at a whim. */
3227 if (mod->symtab[i].st_value <= addr
3228 && mod->symtab[i].st_value > mod->symtab[best].st_value
3229 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3230 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3231 best = i;
3232 if (mod->symtab[i].st_value > addr
3233 && mod->symtab[i].st_value < nextval
3234 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3235 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3236 nextval = mod->symtab[i].st_value;
3237 }
3238
3239 if (!best)
3240 return NULL;
3241
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003242 if (size)
3243 *size = nextval - mod->symtab[best].st_value;
3244 if (offset)
3245 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return mod->strtab + mod->symtab[best].st_name;
3247}
3248
Rusty Russell6dd06c92008-01-29 17:13:22 -05003249/* For kallsyms to ask for address resolution. NULL means not found. Careful
3250 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003251const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003252 unsigned long *size,
3253 unsigned long *offset,
3254 char **modname,
3255 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
3257 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003258 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Rusty Russellcb2a5202008-01-14 00:55:03 -08003260 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003261 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003262 if (within_module_init(addr, mod) ||
3263 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003264 if (modname)
3265 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003266 ret = get_ksymbol(mod, addr, size, offset);
3267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003270 /* Make a copy in here where it's safe */
3271 if (ret) {
3272 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3273 ret = namebuf;
3274 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003275 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003279int lookup_module_symbol_name(unsigned long addr, char *symname)
3280{
3281 struct module *mod;
3282
Rusty Russellcb2a5202008-01-14 00:55:03 -08003283 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003284 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003285 if (within_module_init(addr, mod) ||
3286 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003287 const char *sym;
3288
3289 sym = get_ksymbol(mod, addr, NULL, NULL);
3290 if (!sym)
3291 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003292 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003293 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003294 return 0;
3295 }
3296 }
3297out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003298 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003299 return -ERANGE;
3300}
3301
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003302int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3303 unsigned long *offset, char *modname, char *name)
3304{
3305 struct module *mod;
3306
Rusty Russellcb2a5202008-01-14 00:55:03 -08003307 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003308 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003309 if (within_module_init(addr, mod) ||
3310 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003311 const char *sym;
3312
3313 sym = get_ksymbol(mod, addr, size, offset);
3314 if (!sym)
3315 goto out;
3316 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003317 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003318 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003319 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003320 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003321 return 0;
3322 }
3323 }
3324out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003325 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003326 return -ERANGE;
3327}
3328
Alexey Dobriyanea078902007-05-08 00:28:39 -07003329int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3330 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332 struct module *mod;
3333
Rusty Russellcb2a5202008-01-14 00:55:03 -08003334 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003335 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (symnum < mod->num_symtab) {
3337 *value = mod->symtab[symnum].st_value;
3338 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003339 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003340 KSYM_NAME_LEN);
3341 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003342 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003343 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346 symnum -= mod->num_symtab;
3347 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003348 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003349 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
3352static unsigned long mod_find_symname(struct module *mod, const char *name)
3353{
3354 unsigned int i;
3355
3356 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003357 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3358 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return mod->symtab[i].st_value;
3360 return 0;
3361}
3362
3363/* Look for this name: can be of form module:name. */
3364unsigned long module_kallsyms_lookup_name(const char *name)
3365{
3366 struct module *mod;
3367 char *colon;
3368 unsigned long ret = 0;
3369
3370 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003371 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if ((colon = strchr(name, ':')) != NULL) {
3373 *colon = '\0';
3374 if ((mod = find_module(name)) != NULL)
3375 ret = mod_find_symname(mod, colon+1);
3376 *colon = ':';
3377 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003378 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if ((ret = mod_find_symname(mod, name)) != 0)
3380 break;
3381 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003382 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return ret;
3384}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003385
3386int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3387 struct module *, unsigned long),
3388 void *data)
3389{
3390 struct module *mod;
3391 unsigned int i;
3392 int ret;
3393
3394 list_for_each_entry(mod, &modules, list) {
3395 for (i = 0; i < mod->num_symtab; i++) {
3396 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3397 mod, mod->symtab[i].st_value);
3398 if (ret != 0)
3399 return ret;
3400 }
3401 }
3402 return 0;
3403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404#endif /* CONFIG_KALLSYMS */
3405
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003406static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003407{
3408 int bx = 0;
3409
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003410 if (mod->taints ||
3411 mod->state == MODULE_STATE_GOING ||
3412 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003413 buf[bx++] = '(';
Kay Sieverscca3e702012-01-13 09:32:15 +10303414 bx += module_flags_taint(mod, buf + bx);
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003415 /* Show a - for module-is-being-unloaded */
3416 if (mod->state == MODULE_STATE_GOING)
3417 buf[bx++] = '-';
3418 /* Show a + for module-is-being-loaded */
3419 if (mod->state == MODULE_STATE_COMING)
3420 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003421 buf[bx++] = ')';
3422 }
3423 buf[bx] = '\0';
3424
3425 return buf;
3426}
3427
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003428#ifdef CONFIG_PROC_FS
3429/* Called by the /proc file system to return a list of modules. */
3430static void *m_start(struct seq_file *m, loff_t *pos)
3431{
3432 mutex_lock(&module_mutex);
3433 return seq_list_start(&modules, *pos);
3434}
3435
3436static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3437{
3438 return seq_list_next(p, &modules, pos);
3439}
3440
3441static void m_stop(struct seq_file *m, void *p)
3442{
3443 mutex_unlock(&module_mutex);
3444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446static int m_show(struct seq_file *m, void *p)
3447{
3448 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003449 char buf[8];
3450
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003451 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 mod->name, mod->init_size + mod->core_size);
3453 print_unload_info(m, mod);
3454
3455 /* Informative for users. */
3456 seq_printf(m, " %s",
3457 mod->state == MODULE_STATE_GOING ? "Unloading":
3458 mod->state == MODULE_STATE_COMING ? "Loading":
3459 "Live");
3460 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003461 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003463 /* Taints info */
3464 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003465 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 seq_printf(m, "\n");
3468 return 0;
3469}
3470
3471/* Format: modulename size refcount deps address
3472
3473 Where refcount is a number or -, and deps is a comma-separated list
3474 of depends or -.
3475*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003476static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 .start = m_start,
3478 .next = m_next,
3479 .stop = m_stop,
3480 .show = m_show
3481};
3482
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003483static int modules_open(struct inode *inode, struct file *file)
3484{
3485 return seq_open(file, &modules_op);
3486}
3487
3488static const struct file_operations proc_modules_operations = {
3489 .open = modules_open,
3490 .read = seq_read,
3491 .llseek = seq_lseek,
3492 .release = seq_release,
3493};
3494
3495static int __init proc_modules_init(void)
3496{
3497 proc_create("modules", 0, NULL, &proc_modules_operations);
3498 return 0;
3499}
3500module_init(proc_modules_init);
3501#endif
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/* Given an address, look for it in the module exception tables. */
3504const struct exception_table_entry *search_module_extables(unsigned long addr)
3505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 const struct exception_table_entry *e = NULL;
3507 struct module *mod;
3508
Rusty Russell24da1cb2007-07-15 23:41:46 -07003509 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003510 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (mod->num_exentries == 0)
3512 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 e = search_extable(mod->extable,
3515 mod->extable + mod->num_exentries - 1,
3516 addr);
3517 if (e)
3518 break;
3519 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003520 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003523 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return e;
3525}
3526
Ingo Molnar4d435f92006-07-03 00:24:24 -07003527/*
Rusty Russelle6104992009-03-31 13:05:31 -06003528 * is_module_address - is this address inside a module?
3529 * @addr: the address to check.
3530 *
3531 * See is_module_text_address() if you simply want to see if the address
3532 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003533 */
Rusty Russelle6104992009-03-31 13:05:31 -06003534bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003535{
Rusty Russelle6104992009-03-31 13:05:31 -06003536 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003537
Rusty Russell24da1cb2007-07-15 23:41:46 -07003538 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003539 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003540 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003541
Rusty Russelle6104992009-03-31 13:05:31 -06003542 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003543}
3544
Rusty Russelle6104992009-03-31 13:05:31 -06003545/*
3546 * __module_address - get the module which contains an address.
3547 * @addr: the address.
3548 *
3549 * Must be called with preempt disabled or module mutex held so that
3550 * module doesn't get freed during this.
3551 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003552struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 struct module *mod;
3555
Rusty Russell3a642e92008-07-22 19:24:28 -05003556 if (addr < module_addr_min || addr > module_addr_max)
3557 return NULL;
3558
Andi Kleend72b3752008-08-30 10:09:00 +02003559 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003560 if (within_module_core(addr, mod)
3561 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return mod;
3563 return NULL;
3564}
Tim Abbottc6b37802008-12-05 19:03:59 -05003565EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Rusty Russelle6104992009-03-31 13:05:31 -06003567/*
3568 * is_module_text_address - is this address inside module code?
3569 * @addr: the address to check.
3570 *
3571 * See is_module_address() if you simply want to see if the address is
3572 * anywhere in a module. See kernel_text_address() for testing if an
3573 * address corresponds to kernel or module code.
3574 */
3575bool is_module_text_address(unsigned long addr)
3576{
3577 bool ret;
3578
3579 preempt_disable();
3580 ret = __module_text_address(addr) != NULL;
3581 preempt_enable();
3582
3583 return ret;
3584}
3585
3586/*
3587 * __module_text_address - get the module whose code contains an address.
3588 * @addr: the address.
3589 *
3590 * Must be called with preempt disabled or module mutex held so that
3591 * module doesn't get freed during this.
3592 */
3593struct module *__module_text_address(unsigned long addr)
3594{
3595 struct module *mod = __module_address(addr);
3596 if (mod) {
3597 /* Make sure it's within the text section. */
3598 if (!within(addr, mod->module_init, mod->init_text_size)
3599 && !within(addr, mod->module_core, mod->core_text_size))
3600 mod = NULL;
3601 }
3602 return mod;
3603}
Tim Abbottc6b37802008-12-05 19:03:59 -05003604EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606/* Don't grab lock, we're oopsing. */
3607void print_modules(void)
3608{
3609 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003610 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Linus Torvaldsb2311252009-06-16 11:07:14 -07003612 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003613 /* Most callers should already have preempt disabled, but make sure */
3614 preempt_disable();
3615 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003616 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003617 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003618 if (last_unloaded_module[0])
3619 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 printk("\n");
3621}
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003624/* Generate the signature for all relevant module structures here.
3625 * If these change, we don't want to try to parse the module. */
3626void module_layout(struct module *mod,
3627 struct modversion_info *ver,
3628 struct kernel_param *kp,
3629 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003630 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003631{
3632}
3633EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634#endif